Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 177ae11

Browse files
Merge pull request #467 from SciML/pdesys
Update to MTK 6 by using new PDESystem `name`
2 parents 70ff49f + 2936fa1 commit 177ae11

File tree

11 files changed

+75
-68
lines changed

11 files changed

+75
-68
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ForwardDiff = "0.10"
3232
LazyArrays = "0.17, 0.18, 0.19, 0.20, 0.21"
3333
LazyBandedMatrices = "0.5, 0.6"
3434
LoopVectorization = "0.12"
35-
ModelingToolkit = "4 - 5.20"
35+
ModelingToolkit = "6.3.1"
3636
NNlib = "0.6, 0.7"
3737
NonlinearSolve = "0.3.7"
3838
RuntimeGeneratedFunctions = "0.4, 0.5"

docs/src/symbolic_tutorials/mol_heat.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ domains = [t ∈ Interval(0.0,1.0),
2828
x Interval(0.0,1.0)]
2929

3030
# PDE system
31-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
31+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
3232

3333
# Method of lines discretization
3434
dx = 0.1
@@ -81,7 +81,7 @@ domains = [t ∈ Interval(0.0,1.0),
8181
x Interval(0.0,1.0)]
8282

8383
# PDE system
84-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
84+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
8585

8686
# Method of lines discretization
8787
# Need a small dx here for accuracy
@@ -136,7 +136,7 @@ domains = [t ∈ Interval(0.0,1.0),
136136
x Interval(-1.0,1.0)]
137137

138138
# PDE system
139-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
139+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
140140

141141
# Method of lines discretization
142142
# Need a small dx here for accuracy
@@ -188,7 +188,7 @@ bcs = [u(0,x) ~ sin(pi*x/2),
188188
domains = [t Interval(0.0,1.0),
189189
x Interval(0.0,1.0)]
190190

191-
pdesys = PDESystem(eqs,bcs,domains,[t,x],[u(t,x),v(t,x)],[Dn=>0.5, Dp=>2])
191+
@named pdesys = PDESystem(eqs,bcs,domains,[t,x],[u(t,x),v(t,x)],[Dn=>0.5, Dp=>2])
192192
discretization = MOLFiniteDifference([x=>0.1],t)
193193
prob = discretize(pdesys,discretization) # This gives an ODEProblem since it's time-dependent
194194
sol = solve(prob,Tsit5())
@@ -225,7 +225,7 @@ bcs = [u(0) ~ 1,
225225
# Space and time domains
226226
domains = [x Interval(0.0,1.0)]
227227

228-
pdesys = PDESystem([eq],bcs,domains,[x],[u(x)])
228+
@named pdesys = PDESystem([eq],bcs,domains,[x],[u(x)])
229229

230230
# Note that we pass in `nothing` for the time variable `t` here since we
231231
# are creating a stationary problem without a dependence on time, only space.
@@ -262,7 +262,7 @@ bcs = [u(0,y) ~ 0.0,
262262
domains = [x Interval(0.0,1.0),
263263
y Interval(0.0,1.0)]
264264

265-
pdesys = PDESystem([eq],bcs,domains,[x,y],[u(x,y)])
265+
@named pdesys = PDESystem([eq],bcs,domains,[x,y],[u(x,y)])
266266

267267
# Note that we pass in `nothing` for the time variable `t` here since we
268268
# are creating a stationary problem without a dependence on time, only space.

src/MOLFiniteDifference/MOL_discretization.jl

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ function calculate_weights_spherical(order::Int, x0::T, x::AbstractVector, idxs:
3333
# but for now everything is an Interval
3434
# @assert length(x) == 3
3535
# TODO: nonlinear diffusion in a spherical domain
36-
i = idxs[2]
36+
i = idxs[2]
3737
dx1 = x[i] - x[i-1]
3838
dx2 = x[i+1] - x[i]
3939
i0 = i - 1 # indexing starts at 0 in the paper and starts at 1 in julia
4040
1 / (i0 * dx1 * dx2) * [i0-1, -2i0, i0+1]
4141
end
42-
42+
4343

4444
function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discretization::DiffEqOperators.MOLFiniteDifference)
4545
grid_align = discretization.grid_align
@@ -54,7 +54,7 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
5454
end
5555

5656
depvar_ops = map(x->operation(x.val),pdesys.depvars)
57-
57+
5858
u0 = []
5959
bceqs = []
6060
alleqs = []
@@ -93,7 +93,7 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
9393
dx = discretization.dxs[findfirst(dxs->isequal(x, dxs[1].val),discretization.dxs)][2]
9494
dx isa Number ? (DomainSets.infimum(xdomain.domain):dx:DomainSets.supremum(xdomain.domain)) : dx
9595
end
96-
dxs = map(nottime) do x
96+
dxs = map(nottime) do x
9797
dx = discretization.dxs[findfirst(dxs->isequal(x, dxs[1].val),discretization.dxs)][2]
9898
end
9999

@@ -117,11 +117,13 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
117117
grid_indices = CartesianIndices(((axes(g)[1] for g in grid)...,))
118118
depvarsdisc = map(depvars) do u
119119
if t == nothing
120-
[Num(Variable{Real}(Base.nameof(operation(u)),II.I...)) for II in grid_indices]
120+
sym = nameof(operation(u))
121+
collect(first(@variables $sym[collect(axes(g)[1] for g in grid)...]))
121122
elseif isequal(arguments(u),[t])
122123
[u for II in grid_indices]
123124
else
124-
[Num(Variable{Symbolics.FnType{Tuple{Any}, Real}}(Base.nameof(operation(u)),II.I...))(t) for II in grid_indices]
125+
sym = nameof(operation(u))
126+
collect(first(@variables $sym[collect(axes(g)[1] for g in grid)...](t)))
125127
end
126128
end
127129
spacevals = map(y->[Pair(nottime[i],space[i][y.I[i]]) for i in 1:nspace],space_indices)
@@ -174,15 +176,15 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
174176
push!(depvarderivbcmaps, r)
175177
end
176178
end
177-
179+
178180
if grid_align == edge_align
179-
# Constructs symbolic spatially discretized terms of the form e.g. (u₁ + u₂) / 2
181+
# Constructs symbolic spatially discretized terms of the form e.g. (u₁ + u₂) / 2
180182
bcvars = [[dot(ones(2)/2,depvar[left_idxs(1)]), dot(ones(2)/2,depvar[right_idxs(1,1)])]
181183
for depvar in depvarsdisc]
182184
# replace u(t,0) with (u₁ + u₂) / 2, etc
183185
depvarbcmaps = reduce(vcat,[subvar(depvar) .=> bcvars[i]
184186
for (i, depvar) in enumerate(depvars) for s in nottime])
185-
187+
186188
end
187189
else
188190
# Higher dimension
@@ -191,7 +193,7 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
191193
end
192194
# All unique order of derivates in BCs
193195
bc_der_orders = filter(!iszero,sort(unique([count_differentials(bc.lhs, nottime[1]) for bc in pdesys.bcs])))
194-
# no. of different orders in BCs
196+
# no. of different orders in BCs
195197
n = length(bc_der_orders)
196198
# Generate initial conditions and bc equations
197199
for bc in pdesys.bcs
@@ -276,21 +278,21 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
276278
eqs = vec(map(interior) do II
277279
# Use max and min to apply buffers
278280
central_neighbor_idxs(II,j,order) = stencil(j,order) .+ max(Imin(order),min(II,Imax(order)))
279-
central_weights_cartesian(d_order,II,j) = calculate_weights_cartesian(d_order, grid[j][II[j]], grid[j], vec(map(i->i[j],
281+
central_weights_cartesian(d_order,II,j) = calculate_weights_cartesian(d_order, grid[j][II[j]], grid[j], vec(map(i->i[j],
280282
central_neighbor_idxs(II,j,approx_order))))
281283
central_deriv(d_order, II,j,k) = dot(central_weights(d_order, II,j),depvarsdisc[k][central_neighbor_idxs(II,j,approx_order)])
282284

283285
central_deriv_cartesian(d_order,II,j,k) = dot(central_weights_cartesian(d_order,II,j),depvarsdisc[k][central_neighbor_idxs(II,j,approx_order)])
284-
286+
285287
# spherical Laplacian has a hardcoded order of 2 (only 2nd order is implemented)
286288
# both for derivative order and discretization order
287289
central_weights_spherical(II,j) = calculate_weights_spherical(2, grid[j][II[j]], grid[j], vec(map(i->i[j], central_neighbor_idxs(II,j,2))))
288290
central_deriv_spherical(II,j,k) = dot(central_weights_spherical(II,j),depvarsdisc[k][central_neighbor_idxs(II,j,2)])
289-
291+
290292
# get a sorted list derivative order such that highest order is first. This is useful when substituting rules
291293
# starting from highest to lowest order.
292294
d_orders(s) = reverse(sort(collect(union(differential_order(eq.rhs, s), differential_order(eq.lhs, s)))))
293-
295+
294296
# central_deriv_rules = [(Differential(s)^2)(u) => central_deriv(2,II,j,k) for (j,s) in enumerate(nottime), (k,u) in enumerate(depvars)]
295297
central_deriv_rules_cartesian = Array{Pair{Num,Num},1}()
296298
for (j,s) in enumerate(nottime)
@@ -300,12 +302,12 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
300302
end
301303
end
302304

303-
central_deriv_rules_spherical = [Differential(s)(s^2*Differential(s)(u))/s^2 => central_deriv_spherical(II,j,k)
305+
central_deriv_rules_spherical = [Differential(s)(s^2*Differential(s)(u))/s^2 => central_deriv_spherical(II,j,k)
304306
for (j,s) in enumerate(nottime), (k,u) in enumerate(depvars)]
305-
307+
306308
valrules = vcat([depvars[k] => depvarsdisc[k][II] for k in 1:length(depvars)],
307309
[nottime[j] => grid[j][II[j]] for j in 1:nspace])
308-
310+
309311
# TODO: upwind rules needs interpolation into `@rule`
310312
forward_weights(II,j) = DiffEqOperators.calculate_weights(discretization.upwind_order, 0.0, grid[j][[II[j],II[j]+1]])
311313
reverse_weights(II,j) = DiffEqOperators.calculate_weights(discretization.upwind_order, 0.0, grid[j][[II[j]-1,II[j]]])
@@ -314,14 +316,14 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
314316
# *(~~a..., ~~b..., dot(forward_weights(II,j),depvars[k][central_neighbor_idxs(II,j)[2:3]]))))
315317
# for j in 1:nspace, k in 1:length(pdesys.depvars)]
316318

317-
## Discretization of non-linear laplacian.
319+
## Discretization of non-linear laplacian.
318320
# d/dx( a du/dx ) ~ (a(x+1/2) * (u[i+1] - u[i]) - a(x-1/2) * (u[i] - u[i-1]) / dx^2
319321
b1(II, j, k) = dot(reverse_weights(II, j), depvarsdisc[k][central_neighbor_idxs(II, j, approx_order)[1:2]]) / dxs[j]
320322
b2(II, j, k) = dot(forward_weights(II, j), depvarsdisc[k][central_neighbor_idxs(II, j, approx_order)[2:3]]) / dxs[j]
321323
# TODO: improve interpolation of g(x) = u(x) for calculating u(x+-dx/2)
322324
g(II, j, k, l) = sum([depvarsdisc[k][central_neighbor_idxs(II, j, approx_order)][s] for s in (l == 1 ? [2,3] : [1,2])]) / 2.
323325
# iv_mid returns middle space values. E.g. x(i-1/2) or y(i+1/2).
324-
iv_mid(II, j, l) = (grid[j][II[j]] + grid[j][II[j]+l]) / 2.0
326+
iv_mid(II, j, l) = (grid[j][II[j]] + grid[j][II[j]+l]) / 2.0
325327
# Dependent variable rules
326328
r_mid_dep(II, j, k, l) = [depvars[k] => g(II, j, k, l) for k in 1:length(depvars)]
327329
# Independent variable rules
@@ -377,10 +379,10 @@ function SciMLBase.symbolic_discretize(pdesys::ModelingToolkit.PDESystem,discret
377379
# 0 ~ ...
378380
# Thus, before creating a NonlinearSystem we normalize the equations s.t. the lhs is zero.
379381
eqs = map(eq -> 0 ~ eq.rhs - eq.lhs, vcat(alleqs,unique(bceqs)))
380-
sys = NonlinearSystem(eqs,vec(reduce(vcat,vec(alldepvarsdisc))),ps,defaults=Dict(defaults))
382+
sys = NonlinearSystem(eqs,vec(reduce(vcat,vec(alldepvarsdisc))),ps,defaults=Dict(defaults),name=pdesys.name)
381383
return sys, nothing
382384
else
383-
sys = ODESystem(vcat(alleqs,unique(bceqs)),t,vec(reduce(vcat,vec(alldepvarsdisc))),ps,defaults=Dict(defaults))
385+
sys = ODESystem(vcat(alleqs,unique(bceqs)),t,vec(reduce(vcat,vec(alldepvarsdisc))),ps,defaults=Dict(defaults),name=pdesys.name)
384386
return sys, tspan
385387
end
386388
end

test/MOL/MOL_1D_HigherOrder.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ using ModelingToolkit: Differential
3333
domains = [t Interval(0.0,1.0),
3434
x Interval(0.0,L)]
3535

36-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
36+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
3737
discretization = MOLFiniteDifference([x=>dx],t, centered_order=4)
3838
prob = discretize(pdesys,discretization)
3939
end
@@ -69,7 +69,7 @@ end
6969
domains = [t Interval(0.0,1.0),
7070
x Interval(0.0,L)]
7171

72-
pdesys = PDESystem(eqs,bcs,domains,[t,x],[u(t,x),v(t,x)])
72+
@named pdesys = PDESystem(eqs,bcs,domains,[t,x],[u(t,x),v(t,x)])
7373
discretization = MOLFiniteDifference([x=>dx],t, centered_order=4)
7474
prob = discretize(pdesys,discretization)
7575
end
@@ -104,7 +104,7 @@ end
104104
dx = 0.4; dt = 0.2
105105

106106
discretization = MOLFiniteDifference([x=>dx],t;centered_order=4,grid_align=center_align)
107-
pdesys = PDESystem(eq,bcs,domains,[x,t],[u(x,t)])
107+
@named pdesys = PDESystem(eq,bcs,domains,[x,t],[u(x,t)])
108108
prob = discretize(pdesys,discretization)
109109

110110
sol = solve(prob,Tsit5(),saveat=0.1,dt=dt)

test/MOL/MOL_1D_Linear_Convection.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using ModelingToolkit,DiffEqOperators,DiffEqBase,LinearAlgebra,Test, DomainSets
2323
x Interval(0.0,2.0)]
2424

2525
# PDE system
26-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
26+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
2727

2828
# Method of lines discretization
2929
dx = 2/80
@@ -79,7 +79,7 @@ end
7979
x Interval(0.0,2.0)]
8080

8181
# PDE system
82-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
82+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
8383

8484
# Method of lines discretization
8585
dx = 2/80
@@ -131,7 +131,7 @@ end
131131
x Interval(0.0,2.0)]
132132

133133
# PDE system
134-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
134+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
135135

136136
# Method of lines discretization
137137
dx = 2/80
@@ -185,7 +185,7 @@ end
185185
x Interval(0.0,2.0)]
186186

187187
# PDE system
188-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u,v])
188+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u,v])
189189

190190
# Method of lines discretization
191191
dx = 2/80
@@ -239,7 +239,7 @@ end
239239
x Interval(0.0,2.0)]
240240

241241
# PDE system
242-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u,v])
242+
@named pdesys = PDESystem(eq,bcs,domains,[t,x],[u,v])
243243

244244
# Method of lines discretization
245245
dx = 2/80

0 commit comments

Comments
 (0)