Skip to content

Commit 34a82a7

Browse files
Merge pull request #374 from killah-t-cell/gb-zm-heterogeneous-input
[WIP] ZDM / GB heterogeneous input
2 parents 62b365c + 5c3b19c commit 34a82a7

18 files changed

+391
-211
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ chain = FastChain(FastDense(dim,16,Flux.σ),FastDense(16,16,Flux.σ),FastDense(1
6262

6363
discretization = PhysicsInformedNN(chain, QuadratureTraining())
6464

65-
@named pde_system = PDESystem(eq,bcs,domains,[x,y],[u])
65+
@named pde_system = PDESystem(eq,bcs,domains,[x,y],[u(x, y)])
6666
prob = discretize(pde_system,discretization)
6767

6868
cb = function (p,l)

docs/src/pinn/2D.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ discretization = PhysicsInformedNN(chain,
7474
strategy;
7575
init_params = initθ)
7676

77-
@named pde_system = PDESystem(eq,bcs,domains,[t,x,y],[u])
77+
@named pde_system = PDESystem(eq,bcs,domains,[t,x,y],[u(t, x, y)])
7878
prob = discretize(pde_system,discretization)
7979
symprob = symbolic_discretize(pde_system,discretization)
8080

@@ -126,8 +126,8 @@ plot_(res)
126126

127127
![3pde](https://user-images.githubusercontent.com/12683885/129949743-9471d230-c14f-4105-945f-6bc52677d40e.gif)
128128

129-
130129
## Performance benchmarks
130+
131131
Here are some performance benchmarks for 2d-pde with various number of input points and the number of neurons in the hidden layer, measuring the time for 100 iterations. Сomparing runtime with GPU and CPU.
132132

133133
```julia

docs/src/pinn/3rd.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ domains = [x ∈ Interval(0.0,1.0)]
3838
chain = FastChain(FastDense(1,8,Flux.σ),FastDense(8,1))
3939

4040
discretization = PhysicsInformedNN(chain, QuasiRandomTraining(20))
41-
@named pde_system = PDESystem(eq,bcs,domains,[x],[u])
41+
@named pde_system = PDESystem(eq,bcs,domains,[x],[u(x)])
4242
prob = discretize(pde_system,discretization)
4343

4444
cb = function (p,l)
@@ -66,4 +66,5 @@ x_plot = collect(xs)
6666
plot(x_plot ,u_real,title = "real")
6767
plot!(x_plot ,u_predict,title = "predict")
6868
```
69+
6970
![hodeplot](https://user-images.githubusercontent.com/12683885/90276340-69bc3e00-de6c-11ea-89a7-7d291123a38b.png)

docs/src/pinn/debugging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ isapprox(dphi[1][2], dphi2, atol=1e-8)
4949

5050

5151
indvars = [x,t]
52-
depvars = [u]
52+
depvars = [u(x, t)]
5353
dim = length(domains)
5454
dx = 0.1
5555
strategy = NeuralPDE.GridTraining(dx)

docs/src/pinn/fp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ discretization = PhysicsInformedNN(chain,
6969
init_params = initθ,
7070
additional_loss=norm_loss_function)
7171

72-
@named pde_system = PDESystem(eq,bcs,domains,[x],[p])
72+
@named pde_system = PDESystem(eq,bcs,domains,[x],[p(x)])
7373
prob = discretize(pde_system,discretization)
7474

7575
pde_inner_loss_functions = prob.f.f.loss_function.pde_loss_function.pde_loss_functions.contents

docs/src/pinn/heterogeneous.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Differential Equations with Heterogeneous Inputs
2+
3+
A differential equation is said to have heterogeneous inputs when its dependent variables depend on different independent variables:
4+
5+
```math
6+
u(x) + w(x, v) = \frac{\partial w(x, v)}{\partial w}
7+
```
8+
9+
Here, we write an arbitrary heterogeneous system:
10+
11+
```julia
12+
@parameters x y
13+
@variables p(..) q(..) r(..) s(..)
14+
Dx = Differential(x)
15+
Dy = Differential(y)
16+
17+
# 2D PDE
18+
eq = p(x) + q(y) + Dx(r(x, y)) + Dy(s(y, x)) ~ 0
19+
20+
# Initial and boundary conditions
21+
bcs = [p(1) ~ 0.f0, q(-1) ~ 0.0f0,
22+
r(x, -1) ~ 0.f0, r(1, y) ~ 0.0f0,
23+
s(y, 1) ~ 0.0f0, s(-1, x) ~ 0.0f0]
24+
25+
# Space and time domains
26+
domains = [x Interval(0.0, 1.0),
27+
y Interval(0.0, 1.0)]
28+
29+
numhid = 3
30+
fastchains = [[FastChain(FastDense(1, numhid, Flux.σ), FastDense(numhid, numhid, Flux.σ), FastDense(numhid, 1)) for i in 1:2];
31+
[FastChain(FastDense(2, numhid, Flux.σ), FastDense(numhid, numhid, Flux.σ), FastDense(numhid, 1)) for i in 1:2]]
32+
discretization = NeuralPDE.PhysicsInformedNN(fastchains, QuadratureTraining()
33+
34+
@named pde_system = PDESystem(eq, bcs, domains, [x,y], [p(x), q(y), r(x, y), s(y, x)])
35+
prob = SciMLBase.discretize(pde_system, discretization)
36+
res = GalacticOptim.solve(prob, BFGS(); cb=cb, maxiters=100)
37+
```

docs/src/pinn/integro_diff.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Integro Differential Equations
32

43
The integral of function u(x),
@@ -31,14 +30,16 @@ Similarly a rectangular or cuboidal domain can be defined using `ProductDomain`
3130
Ix = Integral((x,y) in DomainSets.ProductDomain(ClosedInterval(0 ,1), ClosedInterval(0 ,x)))
3231
```
3332

34-
3533
## 1-dimensional example
3634

3735
Lets take an example of an integro differential equation:
36+
3837
```math
3938
\frac{∂}{∂x} u(x) + 2u(x) + 5 \int_{0}^{x}u(t)dt = 1 for x \geq 0
4039
```
40+
4141
and boundary condition
42+
4243
```math
4344
u(0) = 0
4445
```
@@ -63,7 +64,7 @@ discretization = PhysicsInformedNN(chain,
6364
init_params = nothing,
6465
phi = nothing,
6566
derivative = nothing)
66-
pde_system = PDESystem(eq,bcs,domains,[t],[i])
67+
pde_system = PDESystem(eq,bcs,domains,[t],[i(t)])
6768
prob = NeuralPDE.discretize(pde_system,discretization)
6869
cb = function (p,l)
6970
println("Current loss is: $l")

docs/src/pinn/ks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Let's consider the Kuramoto–Sivashinsky equation, which contains a 4th-order d
66
∂_t u(x, t) + u(x, t) ∂_x u(x, t) + \alpha ∂^2_x u(x, t) + \beta ∂^3_x u(x, t) + \gamma ∂^4_x u(x, t) = 0 \, ,
77
```
88

9-
where ``\alpha = \gamma = 1`` and ``\beta = 4``. The exact solution is:
9+
where `\alpha = \gamma = 1` and `\beta = 4`. The exact solution is:
1010

1111
```math
1212
u_e(x, t) = 11 + 15 \tanh \theta - 15 \tanh^2 \theta - 15 \tanh^3 \theta \, ,
1313
```
1414

15-
where ``\theta = 1 - x/2`` and with initial and boundary conditions:
15+
where `\theta = 1 - x/2` and with initial and boundary conditions:
1616

1717
```math
1818
\begin{align*}
@@ -62,7 +62,7 @@ dx = 0.4; dt = 0.2
6262
chain = FastChain(FastDense(2,12,Flux.σ),FastDense(12,12,Flux.σ),FastDense(12,1))
6363

6464
discretization = PhysicsInformedNN(chain, GridTraining([dx,dt]))
65-
@named pde_system = PDESystem(eq,bcs,domains,[x,t],[u])
65+
@named pde_system = PDESystem(eq,bcs,domains,[x,t],[u(x, t)])
6666
prob = discretize(pde_system,discretization)
6767

6868
cb = function (p,l)

docs/src/pinn/neural_adapter.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ discretization = NeuralPDE.PhysicsInformedNN(chain1,
4444
quadrature_strategy;
4545
init_params = initθ)
4646

47-
@named pde_system = PDESystem(eq,bcs,domains,[x,y],[u])
47+
@named pde_system = PDESystem(eq,bcs,domains,[x,y],[u(x, y)])
4848
prob = NeuralPDE.discretize(pde_system,discretization)
4949
sym_prob = NeuralPDE.symbolic_discretize(pde_system,discretization)
5050

@@ -95,11 +95,12 @@ p5 = plot(xs, ys, diff_u_,linetype=:contourf,title = "error 2");
9595
plot(p1,p2,p3,p4,p5)
9696

9797
```
98+
9899
![neural_adapter](https://user-images.githubusercontent.com/12683885/127645487-24226cc0-fff6-4bb9-8509-77cf3e120563.png)
99100

100101
## Domain decomposition
101102

102-
In this example, we first obtain a prediction of 2D Poisson equation on subdomains. We split up full domain into 10 sub problems by x, and create separate neural networks for each sub interval. If x domain ∈ [x_0, x_end] so, it is decomposed on 10 part: sub x domains = {[x_0, x_1], ... [x_i,x_i+1], ..., x_9,x_end]}.
103+
In this example, we first obtain a prediction of 2D Poisson equation on subdomains. We split up full domain into 10 sub problems by x, and create separate neural networks for each sub interval. If x domain ∈ [x_0, x_end] so, it is decomposed on 10 part: sub x domains = {[x_0, x_1], ... [x_i,x_i+1], ..., x_9,x_end]}.
103104
And then using the method neural_adapter, we retrain the banch of 10 predictions to the one prediction for full domain of task.
104105

105106
![domain_decomposition](https://user-images.githubusercontent.com/12683885/127149752-a4ecea50-2984-45d8-b0d4-d2eadecf58e7.png)
@@ -171,7 +172,7 @@ for i in 1:count_decomp
171172
@register phi_bound(x,y)
172173
Base.Broadcast.broadcasted(::typeof(phi_bound), x,y) = phi_bound(x,y)
173174
bcs_ = create_bcs(bcs,domains_[1].domain, phi_bound)
174-
@named pde_system_ = PDESystem(eq, bcs_, domains_, [x, y], [u])
175+
@named pde_system_ = PDESystem(eq, bcs_, domains_, [x, y], [u(x, y)])
175176
push!(pde_system_map,pde_system_)
176177
strategy = NeuralPDE.GridTraining([0.1/count_decomp, 0.1])
177178

@@ -225,7 +226,7 @@ chain2 = FastChain(FastDense(2,inner_,af),
225226

226227
initθ2 =Float64.(DiffEqFlux.initial_params(chain2))
227228

228-
@named pde_system = PDESystem(eq, bcs, domains, [x, y], [u])
229+
@named pde_system = PDESystem(eq, bcs, domains, [x, y], [u(x, y)])
229230

230231
losses = map(1:count_decomp) do i
231232
loss(cord,θ) = chain2(cord,θ) .- phis[i](cord,reses[i].minimizer)

docs/src/pinn/parm_estim.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Optimising Parameters of a Lorenz System
2-
Consider a Lorenz System ,
2+
3+
Consider a Lorenz System ,
34

45
```math
56
\begin{align*}
@@ -9,7 +10,7 @@
910
\end{align*}
1011
```
1112

12-
with Physics-Informed Neural Networks. Now we would consider the case where we want to optimise the parameters ``\sigma``, ``\beta``, and ``\rho``.
13+
with Physics-Informed Neural Networks. Now we would consider the case where we want to optimise the parameters `\sigma`, `\beta`, and `\rho`.
1314

1415
We start by defining the the problem,
1516

@@ -27,14 +28,17 @@ bcs = [x(0) ~ 1.0, y(0) ~ 0.0, z(0) ~ 0.0]
2728
domains = [t Interval(0.0,1.0)]
2829
dt = 0.01
2930
```
31+
3032
And the neural networks as,
33+
3134
```julia
3235
input_ = length(domains)
3336
n = 8
3437
chain1 = FastChain(FastDense(input_,n,Flux.σ),FastDense(n,n,Flux.σ),FastDense(n,n,Flux.σ),FastDense(n,1))
3538
chain2 = FastChain(FastDense(input_,n,Flux.σ),FastDense(n,n,Flux.σ),FastDense(n,n,Flux.σ),FastDense(n,1))
3639
chain3 = FastChain(FastDense(input_,n,Flux.σ),FastDense(n,n,Flux.σ),FastDense(n,n,Flux.σ),FastDense(n,1))
3740
```
41+
3842
We will add an additional loss term based on the data that we have in order to optimise the parameters.
3943

4044
Here we simply calculate the solution of the lorenz system with [OrdinaryDiffEq.jl](https://diffeq.sciml.ai/v1.10/tutorials/ode_example.html#In-Place-Updates-1) based on the adaptivity of the ODE solver. This is used to introduce non-uniformity to the time series.
@@ -64,17 +68,20 @@ sep = [acum[i]+1 : acum[i+1] for i in 1:length(acum)-1]
6468
(u_ , t_) = data
6569
len = length(data[2])
6670
```
71+
6772
Then we define the additional loss funciton `additional_loss(phi, θ , p)`, the function has three arguments, `phi` the trial solution, `θ` the parameters of neural networks, and the hyperparameters `p` .
6873

6974
```julia
7075
function additional_loss(phi, θ , p)
7176
return sum(sum(abs2, phi[i](t_ , θ[sep[i]]) .- u_[[i], :])/len for i in 1:1:3)
7277
end
7378
```
79+
7480
Then finally defining and optimising using the `PhysicsInformedNN` interface.
81+
7582
```julia
7683
discretization = NeuralPDE.PhysicsInformedNN([chain1 , chain2, chain3],NeuralPDE.GridTraining(dt), param_estim=true, additional_loss=additional_loss)
77-
@named pde_system = PDESystem(eqs,bcs,domains,[t],[x, y, z],[σ_, ρ, β], defaults=Dict([p .=> 1.0 for p in [σ_, ρ, β]]))
84+
@named pde_system = PDESystem(eqs,bcs,domains,[t],[x(t), y(t), z(t)],[σ_, ρ, β], defaults=Dict([p .=> 1.0 for p in [σ_, ρ, β]]))
7885
prob = NeuralPDE.discretize(pde_system,discretization)
7986
cb = function (p,l)
8087
println("Current loss is: $l")
@@ -83,7 +90,9 @@ end
8390
res = GalacticOptim.solve(prob, BFGS(); cb = cb, maxiters=5000)
8491
p_ = res.minimizer[end-2:end] # p_ = [9.93, 28.002, 2.667]
8592
```
93+
8694
And then finally some analyisis by plotting.
95+
8796
```julia
8897
initθ = discretization.init_params
8998
acum = [0;accumulate(+, length.(initθ))]

docs/src/pinn/poisson.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ chain = FastChain(FastDense(dim,16,Flux.σ),FastDense(16,16,Flux.σ),FastDense(1
5454
dx = 0.05
5555
discretization = PhysicsInformedNN(chain,GridTraining(dx))
5656

57-
@named pde_system = PDESystem(eq,bcs,domains,[x,y],[u])
57+
@named pde_system = PDESystem(eq,bcs,domains,[x,y],[u(x, y)])
5858
prob = discretize(pde_system,discretization)
5959

6060
#Optimizer
@@ -117,6 +117,7 @@ chain = FastChain(FastDense(dim,16,Flux.σ),FastDense(16,16,Flux.σ),FastDense(1
117117
```
118118

119119
Here, we build PhysicsInformedNN algorithm where `dx` is the step of discretization and `strategy` stores information for choosing a training strategy.
120+
120121
```julia
121122
# Discretization
122123
dx = 0.05
@@ -126,7 +127,7 @@ discretization = PhysicsInformedNN(chain, GridTraining(dx))
126127
As described in the API docs, we now need to define the `PDESystem` and create PINNs problem using the `discretize` method.
127128

128129
```julia
129-
@named pde_system = PDESystem(eq,bcs,domains,[x,y],[u])
130+
@named pde_system = PDESystem(eq,bcs,domains,[x,y],[u(x, y)])
130131
prob = discretize(pde_system,discretization)
131132
```
132133

@@ -161,4 +162,5 @@ p2 = plot(xs, ys, u_predict, linetype=:contourf,title = "predict");
161162
p3 = plot(xs, ys, diff_u,linetype=:contourf,title = "error");
162163
plot(p1,p2,p3)
163164
```
165+
164166
![poissonplot](https://user-images.githubusercontent.com/12683885/90962648-2db35980-e4ba-11ea-8e58-f4f07c77bcb9.png)

0 commit comments

Comments
 (0)