-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffusion_complicated.jl
470 lines (384 loc) · 15.4 KB
/
diffusion_complicated.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# # Inferring diffusion operators - more complicated example
#
# In this example, we will consider the diffusion equation with Dirichlet and
# Neumann homogeneous boundary conditions, a variable diffusivity $\kappa(x)$,
# and a variable source term $s(x)$. You may change the profiles of $\kappa$
# and $s$.
#nb # ## The Julia bootstrap block (for Google Colab)
#nb # Source: https://colab.research.google.com/drive/1_4Yz3FKO5_uuYvamEfHqwtFT9WpCuSbm
#nb #
#nb # This should be run for the first time to install Julia kernel, and then refresh this page (e.g., Ctrl-R)
#nb # so that colab will redirect to the installed Julia kernel
#nb # and then doing your own work
#nb ## 1. install latest Julia using jill.py
#nb ## tip: one can install specific Julia version using e.g., `jill install 1.7`
#nb !pip install jill && jill install --upstream Official --confirm
#nb
#nb ## 2. install IJulia kernel
#nb ! julia -e 'using Pkg; pkg"add IJulia"; using IJulia; installkernel("Julia")'
#nb
#nb ## 3. hot-fix patch to strip the version suffix of the installed kernel so
#nb ## that this notebook kernelspec is version agnostic
#nb !jupyter kernelspec install $(jupyter kernelspec list | grep julia | tr -s ' ' | cut -d' ' -f3) --replace --name julia
#nb #-
#nb using Pkg; Pkg.add(["OrdinaryDiffEq", "DiffEqFlux", "Plots"])
# We start by loading some packages.
using LinearAlgebra
using SparseArrays
using OrdinaryDiffEq
using DiffEqFlux
using Plots
# Since the problems will consist of inferring various matrices, we define a small helper
# function for visualizing our results.
plotmat(A; kwargs...) = heatmap(
reverse(A; dims = 1);
aspect_ratio = :equal,
xlims = (1 / 2, size(A, 2) + 1 / 2),
ylims = (1 / 2, size(A, 1) + 1 / 2),
## xticks = nothing,
## yticks = nothing,
kwargs...,
)
plotmat(A::AbstractSparseMatrix; kwargs...) = plotmat(Matrix(A); kwargs...)
# ## Problem statement
#
# This time, we consider a linear ordinary differential equation (ODE)
# parameterized by an operator $\mathbf{A} \in \mathbb{R}^{(N + 1) \times (N +
# 1)}$ *and* a constant source term $\mathbf{s} \in \mathbb{R}^{N + 1}$:
#
# $$\frac{\mathrm{d} \mathbf{u}}{\mathrm{d} t} = \mathbf{f}(\mathbf{u},
# \mathbf{\theta}, t) := \mathbf{A} \mathbf{u} + \mathbf{s}, \quad
# \mathbf{u}(0) = \mathbf{u}_0$$
#
# where $\mathbf{u}_0 \in \mathbb{R}^{N + 1}$ are some initial conditions and
# $\mathbf{\theta} = \begin{pmatrix} \mathbf{A} & \mathbf{s} \end{pmatrix} \in
# \mathbb{R}^{(N + 1) \times (N + 2)}$. To solve this system, we will use the
# [OrdinaryDiffEq](https://github.com/SciML/OrdinaryDiffEq.jl) package. It
# provides differentiable ODE solvers for problems defined by a parametrized
# ODE function $f$ defining the right hand side of the ODE for a given state
# $\mathbf{u}$, time $t$ and parameters $\mathbf{\theta}$.
function f(u, θ, t)
A = @view θ[:, 1:end-1]
s = @view θ[:, end]
A * u .+ s
end
function f!(du, u, θ, t)
A = @view θ[:, 1:end-1]
s = @view θ[:, end]
mul!(du, A, u)
du .+= s
end
# Let us define the ODE solver (the in-place form `S!` may be faster for
# generating large datasets, but is not as easily differentiable):
function S(θ, u₀, t; kwargs...)
problem = ODEProblem(ODEFunction(f), u₀, extrema(t), θ)
solve(problem, Tsit5(); saveat = t, kwargs...)
end
function S!(θ, u₀, t; kwargs...)
problem = ODEProblem(ODEFunction(f!), u₀, extrema(t), θ)
solve(problem, Tsit5(); saveat = t, kwargs...)
end
# Consider now the diffusion equation
#
# $$\frac{\partial u}{\partial t}(x, t) = \kappa(x) \frac{\partial^2 u}{\partial x^2}(x, t) + s(x), \quad x \in
# \Omega = [0, 1]$$
#
# with diffusivity $\kappa > 0$, source term $s$, and left homogeneous Dirichlet boundary conditions $u(0, t) = 0$, right homogeneous Neumann boundary conditions $\frac{\partial u}{\partial x}(1, t) = 0$, and initial conditions $u(x, 0) = u_0(x)$. *You may change the expressions for $\kappa$ and $s$*.
κ(x) = x ≤ 1 / 2 ? 0.01 : 0.001
source(x) = 1 / 5 * sin(2π * x) * (x ≤ 1 / 2)
# The domain $\Omega$ may be discretized using a uniform grid $\mathbf{x} = (x_n)_{0 \leq n
# \leq N}$ of $N + 1$ equidistant points.
N = 50
x = LinRange(0.0, 1.0, N + 1)
Δx = 1 / N
# On the above grid, the diffusion operator $\frac{\partial^2}{\partial x^2}$ with left
# Dirichlet and right Neumann boundary conditions may be approximated using the matrix
#
# $$\mathbf{D} = \frac{1}{\Delta x^2} \begin{pmatrix}
# 0 & \dots & \dots & \dots & 0 \\
# 1 & -2 & 1 & & \\
# & \ddots & \ddots & \ddots & \\
# & & 1 & -2 & 1 \\
# & & 1 & -2 & 1 \\
# \end{pmatrix}.$$
#
# A second order accurate diffusion stencil is given by
inds = [-1, 0, 1]
stencil = [1, -2, 1] / Δx^2
# Alternatively, a fourth order accurate diffusion stencil may be used. For higher order stencils, see
# https://en.wikipedia.org/wiki/Finite_difference_coefficient
inds = -2:2
stencil = [-1 / 12, 4 / 3, -5 / 2, 4 / 3, -1 / 12] / Δx^2
#- We build diagonals from the stencil. Special treatment is needed for the boundaries.
A_ref =
diagm(κ.(x)) * diagm((i => fill(s, N + 1 - abs(i)) for (i, s) ∈ zip(inds, stencil))...)
A_ref[1, :] .= 0 # Left Dirichlet boundary conditions
A_ref[end, :] .= A_ref[end-1, :] # Right Neumann boundary conditions (first order)
## @. A_ref[end, :] = 2 / 3 * (2 * A_ref[end - 1, :] - 1 / 2 * A_ref[end - 2, :]) # Right Neumann boundary conditions (second order)
plotmat(A_ref)
# Discrete source term with boundary conditions
s_ref = source.(x)
s_ref[1] = 0
s_ref[end] = s_ref[end-1]
plot(x, s_ref)
# Reference parameters
θ_ref = [A_ref s_ref]
#-
function create_data(u₀, x, t, θ)
A = θ[:, 1:end-1]
s = θ[:, end]
u = S!(θ, u₀, t; abstol = 1e-8, reltol = 1e-10)
dudt = zeros(size(u))
for i = 1:length(t)
dudt[:, :, i] = A * u[i] .+ s
end
(; u, dudt)
end
#-
function apply_bc!(u)
u[1] = 0
u[end] = u[end-1]
u
end
# Four particular solutions are given below. We may use this solution to test
# our solver, from now on referred to as the *full order model* (FOM).
u₁ = apply_bc!(@. sin(4π * x))
u₂ = apply_bc!(@. exp(-x) * x)
u₃ = apply_bc!(@. 1 / 3 ≤ x ≤ 2 / 3)
u₄ = apply_bc!(zeros(size(x)))
tplot = LinRange(0.0, 1.0, 5)
## u, dudt = create_data(reshape(u₁, :, 1), x, tplot, θ_ref)
## u, dudt = create_data(reshape(u₂, :, 1), x, tplot, θ_ref)
u, dudt = create_data(reshape(u₃, :, 1), x, tplot, θ_ref)
## u, dudt = create_data(reshape(u₄, :, 1), x, tplot, θ_ref)
pl = plot();
for (i, t) ∈ enumerate(tplot)
plot!(pl, x, u[:, 1, i]; label = "t = $t")
end
pl
# ## Learning the operator intrusively
#
# Before inferring the unknown operator, we need som "training" data to compare
# with. This will consist of snapshots of different initial conditions diffused
# for different durations. We will sample normally distributed random
# coefficients decaying with the frequency $k$, and put the results in a
# snapshot tensor of size $(N + 1) \times n_\text{sample} \times n_t$.
tsnap = LinRange(0.0, 1.0, 51)[2:end]
nsample = 200
r = cumsum(randn(N + 1, nsample); dims = 1)
foreach(apply_bc!, eachcol(r))
train = create_data(r, x, tsnap, θ_ref)
# We also need an instantaneous performance metric (loss/cost/objective
# function). This function should compare our predictions with a snapshots of
# the exact solutions. Here we will use a simple $L^2$-distance (mean squared
# error). We also add regularizations for $\mathbf{A}$ and $\mathbf{s}$. Note
# that the `ODESolution` object behaves like an array of size $(N + 1) \times
# n_\text{sample} \times n_t$, meaning that we solve for all the different
# initial conditions at the same time.
function create_loss(u, t, λ = (1e-8, 1e-8))
λᴬ, λˢ = λ
nx, nu, _ = size(u)
u₀ = u[:, :, 1]
uₜ = u
loss(θ) =
sum(abs2, S(θ, u₀, t) - uₜ) / (nx * nu) +
λᴬ / nx^2 * sum(abs2, θ[:, 1:end-1]) +
λˢ / nx * sum(abs2, θ[:, end])
loss
end
# As an initial guess for the "unknown" operators $\mathbf{\theta}$ we will
# simply use an empty matrix.
θ = zeros(N + 1, N + 2)
# We may also visualize the predictions of our operators.
function ploterr(θ, u, tplot = LinRange(0.0, 1.0, 5); θ_ref = θ_ref)
sol_ref = S(θ_ref, u[:, :, 1], tplot)
sol = S(θ, u[:, :, 1], tplot)
pl = plot()
for (i, t) ∈ enumerate(tplot)
plot!(pl, x, sol_ref[i]; color = i, label = nothing)
scatter!(pl, x, sol[i]; label = "t = $t", color = i, markeralpha = 0.5)
end
pl
end
ploterr(θ_ref, u₃)
# A callback function is called after every iteration of gradient descent, allowing us to
# check the performance of our operator in real time during training. The return value
# `false` simply stops the function from stopping the iterations. We can already check how
# our initial guess for $\mathbf{A}$ performs.
function callback(θ, loss)
println(loss)
flush(stdout)
false
end
# The intrusive training consists of improving the operator through gradient
# descent applied to the loss function. The optimizer
# [`ADAM`](https://arxiv.org/abs/1412.6980) performs a first order gradient
# descent, but with some sophisiticated momentum terms exploiting the
# stochasticity of the loss function. For larger problems we could could use a
# subset of the different solutions $u$, time steps $t$ and spatial points $x$
# at every evaluation of `loss`, but for now we will just use the entire
# dataset.
#
# https://diffeqflux.sciml.ai/dev/ControllingAdjoints/
loss = create_loss(train.u, tsnap, (1e-8, 1e-10))
θ_fit = θ
result = DiffEqFlux.sciml_train(loss, θ_fit, ADAM(0.01); cb = callback, maxiters = 1000)
θ_fit = result.u
#-
A = θ[:, 1:end-1]
A_fit = θ_fit[:, 1:end-1]
A_ref = θ_ref[:, 1:end-1]
#-
plot(
plotmat(A; title = "Initial"),
plotmat(A_fit; title = "Final"),
plotmat(A_ref; title = "Reference");
layout = (1, 3),
)
#-
plot(x, θ[:, end]; label = "Initial")
plot!(x, θ_fit[:, end]; label = "Final")
plot!(x, θ_ref[:, end]; label = "Reference")
#-
## ploterr(θ_fit, u₁)
## ploterr(θ_fit, u₂)
ploterr(θ_fit, u₃)
## ploterr(θ_fit, u₄)
# Notice that at no point did we explicitly specify the gradient of `loss`, `S`
# or even `f` with respect to the parameters `θ`. Yet still we performed a
# gradient descent. Since the entire computational graph is composed of pure
# Julia code, automatic differentiation engines, in this particular case
# [Zygote](https://github.com/FluxML/Zygote.jl), can use the chain rule to
# compute gradients. We may access this gradient explicitly. Let us compare the
# gradients:
dLdθ = first(Zygote.gradient(loss, θ))
dLdθ_fit = first(Zygote.gradient(loss, θ_fit))
dLdθ_ref = first(Zygote.gradient(loss, θ_ref))
dLdA = dLdθ[:, 1:end-1]
dLdA_fit = dLdθ_fit[:, 1:end-1]
dLdA_ref = dLdθ_ref[:, 1:end-1]
dLds = dLdθ[:, end]
dLds_fit = dLdθ_fit[:, end]
dLds_ref = dLdθ_ref[:, end]
plot(
plotmat(dLdA; title = "Initial ($(norm(dLdA)))"),
plotmat(dLdA_fit; title = "Final ($(norm(dLdA_fit)))"),
plotmat(dLdA_ref; title = "Reference ($(norm(dLdA_ref)))");
layout = (1, 3),
)
# # Proper orthogonal decomposition (POD)
#
# Above we learned the discrete diffusion operator in the canonical basis of
# $\mathbb{R}^N$. Another useful basis is obtained from a *proper orthogonal
# decomposition* (POD). It is determined from snapshot data of the solution at
# different time steps (`tsnap`) and possibly different initial conditions.
# Truncating this basis at a level $P \ll N$ will yield *the* basis of size $P$
# with the smallest error energy for the training data (among all possibile
# bases spanning $P$-dimensional subspaces of $L^2(\Omega)$).
# The POD basis is simply just a collection of left singular vectors of our snapshot matrix
# $\mathbf{U}$. We will keep the $P$ first basis functions (they are the most important, as
# `svd` orders them by decreasing singular value). The basis functions will be stored as
# columns in the matrix $\mathbf{\Phi} \in \mathbb{R}^{N \times P}$.
U = reshape(Array(train.u), N + 1, :)
P = 20
decomp = svd(U)
Φ = decomp.U[:, 1:P]
A_pod_ref = Φ' * A_ref * Φ
s_pod_ref = Φ's_ref
θ_pod_ref = [A_pod_ref s_pod_ref]
plotmat(A_pod_ref)
# We may plot some POD modes.
pl = plot();
for k ∈ [1, 3, 7]
plot!(pl, x, Φ[:, k]; label = "Mode $k")
end
pl
# We may check the orthogonality by computing the inner product between each basis function
# pair.
plotmat(Φ'Φ)
# The matrix $\mathbf{Φ} \mathbf{Φ}^\mathsf{T}$ can be considered to be a so called
# "autoencoder", with "encoder" $\mathbf{Φ}^\mathsf{T}$ and "decoder" $\mathbf{Φ}$. The
# autoencoder should be closer to identity when keeping more modes, i.e. we may be tempted
# to write something like $\mathbf{Φ} \mathbf{Φ}^\mathsf{T} \underset{P \to N}{\to}
# \mathbf{I}$ (by abuse of mathematical notation).
plotmat(Φ * Φ')
# Projecting the full order model onto the POD basis yields the reduced order
# model
#
# $$\frac{\mathrm{d} \tilde{\mathbf{u}}}{\mathrm{d} t} = \tilde{\mathbf{A}}
# \tilde{\mathbf{u}} + \tilde{\mathbf{s}},$$
#
# where $\tilde{\mathbf{u}}$ are the coordinates of the ROM solution in the POD
# basis, $\tilde{\mathbf{A}} = \mathbf{\Phi}^\mathsf{T} \mathbf{A}
# \mathbf{\Phi} \in \mathbb{R}^{P \times P}$ is the reduced order operator, and
# $\tilde{\mathbf{s}} = \mathbf{\Phi}^\mathsf{T} \mathbf{s}. Later, we will try
# to infer this operator directly from data. Note that the ROM solution is
# given by $\mathbf{u}_\text{ROM} = \mathbf{\Phi} \tilde{\mathbf{u}}$.
#
# Note also that the FOM and ROM have the same form, just different sizes (and
# "tilde"s appearing everywhere). The ROM solution may thus simply be computed
# by
#
# $$\mathbf{u}_\text{ROM}(t) = \mathbf{\Phi} \mathbf{S}(\tilde{\mathbf{\theta}},
# \mathbf{\Phi}^\mathsf{T} \mathbf{u}_0, t).$$
#
# Let us compare the solution of the ROM and FOM:
## u₀ = u₁
## u₀ = u₂
u₀ = u₃
## u₀ = u₄
tplot = LinRange(0.0, 1.0, 5)
sol = S(θ_ref, u₀, tplot)
sol_pod = S(θ_pod_ref, Φ' * u₀, tplot)
p = plot();
for (i, t) ∈ enumerate(tplot)
plot!(p, x, sol[i]; label = "t = $t", color = i) # FOM
scatter!(p, x, Φ * sol_pod[:, i]; label = nothing, markeralpha = 0.5, color = i) # ROM
end
p
# ## Learning the operator in the POD basis
#
# Similarly to the full order model case, we may fit the POD operator
# $\tilde{A}$ using intrusive and non-intrusive approaches.
# First we need to create snapshot tensors of the POD solutions/observations:
u_pod = zeros(P, nsample, length(tsnap))
for i ∈ eachindex(tsnap)
u_pod[:, :, i] = Φ' * train.u[:, :, i]
end
# We may also reuse the loss function and ODE solver from the full order case
# (only the sizes change).
loss_pod = create_loss(u_pod, tsnap)
θ_pod = zeros(P, P+1)
A_pod = θ_pod[:, 1:end-1]
s_pod = θ_pod[:, end]
result = DiffEqFlux.sciml_train(
loss_pod,
θ_pod,
ADAM(0.01);
cb = callback,
maxiters = 1000,
)
θ_pod_fit = result.u
A_pod_fit = θ_pod_fit[:, 1:end-1]
s_pod_fit = θ_pod_fit[:, end]
#-
plot(
plotmat(A_pod; title = "Initial"),
plotmat(A_pod_fit; title = "Final"),
plotmat(A_pod_ref; title = "Reference");
layout = (1, 3),
)
#-
plot(
plotmat(Φ * A_pod * Φ'; title = "Initial"),
plotmat(Φ * A_pod_fit * Φ'; title = "Final"),
## plotmat(Φ * A_pod_ref * Φ'; title = "Reference");
plotmat(A_ref; title = "Reference");
layout = (1, 3),
)
#-
pl = plot(x, s_ref; label = "Reference")
plot!(pl, x, Φ * s_pod_ref; label = "Best approximation")
plot!(pl, x, Φ * s_pod; label = "Initial")
plot!(pl, x, Φ * s_pod_fit; label = "Final")
pl