Skip to content

Commit d364719

Browse files
committed
stash
1 parent 5120a2b commit d364719

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

src/discretization/MOL_discretization.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,16 @@ function SciMLBase.symbolic_discretize(pdesys::PDESystem, discretization::Method
7070

7171
s = DiscreteSpace(pdesys.domain, depvars, indvars, nottime, grid_align, discretization)
7272

73+
derivweights = DifferentialDiscretizer(pde, s, discretization)
74+
7375
interior = s.Igrid[[2:(length(grid[x])-1) for x in s.nottime]
7476

7577
### PDE EQUATIONS ###
7678
# Create a stencil in the required dimension centered around 0
7779
# e.g. (-1,0,1) for 2nd order, (-2,-1,0,1,2) for 4th order, etc
7880
# For all cartesian indices in the interior, generate finite difference rules
7981
pdeeqs = vec(map(interior) do II
80-
rules = generate_finite_difference_rules(II, s, pde, discretization)
82+
rules = generate_finite_difference_rules(II, s, pde, derivweights)
8183
substitute(pde.lhs,rules) ~ substitute(pde.rhs,rules)
8284
end)
8385

src/discretization/discretize_vars.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct DiscreteSpace{N,M}
2727
Iaxies
2828
Igrid
2929
Iedge
30+
x2i
3031
end
3132

3233
nparams(::DiscreteSpace{N,M}) where {N,M} = N
@@ -52,7 +53,8 @@ map_symbolic_to_discrete(II::CartesianIndex, s::DiscreteSpace{N,M}) where {N,M}
5253

5354
# TODO: Allow other grids
5455

55-
function DiscreteSpace(domain, depvars, indvars, nottime, grid_align, discretization)
56+
function DiscreteSpace(domain, depvars, indvars, nottime, discretization)
57+
grid_align = discretization.grid_align
5658
t = discretization.time
5759
nspace = length(nottime)
5860
# Discretize space
@@ -78,7 +80,7 @@ function DiscreteSpace(domain, depvars, indvars, nottime, grid_align, discretiza
7880
grid = map(nottime) do x
7981
xdomain = domain[findfirst(d -> isequal(x, d.variables), domain)]
8082
dx = discretization.dxs[findfirst(dxs -> isequal(x, dxs[1].val), discretization.dxs)][2]
81-
dx isa Number ? x => ((DomainSets.infimum(xdomain.domain)-dx/2):dx:(DomainSets.supremum(xdomain.domain)+dx/2) : x => dx
83+
dx isa Number ? (x => ((DomainSets.infimum(xdomain.domain)-dx/2):dx:(DomainSets.supremum(xdomain.domain)+dx/2))) : x => dx
8284
end
8385
# TODO: allow depvar-specific center/edge choice?
8486
end
@@ -94,18 +96,18 @@ function DiscreteSpace(domain, depvars, indvars, nottime, grid_align, discretiza
9496
u => [u for II in s.Igrid]
9597
else
9698
sym = nameof(operation(u))
97-
u => collect(first(@variables $sym[collect(axes(g.second)[1] for g in grid)...](t)))
99+
u => collect(first(@variables $sym[collect(axes(g.second)[1] for g in grid)...](t)))
98100
end
99101
end
100102

101103

102104
# Build symbolic maps for boundaries
103-
Iedge = reduce(vcat, [[vcat([Colon() for j = 1:i-1], 1, [Colon() for j = i+1:nspace]),
104-
vcat([Colon() for j = 1:i-1], length(axies[i].second), [Colon() for j = i+1:nspace])] for i = 1:nspace])
105+
Iedge = reduce(vcat, [[Igrid[vcat([Colon() for j = 1:i-1], 1, [Colon() for j = i+1:nspace])],
106+
Igrid[vcat([Colon() for j = 1:i-1], length(axies[i].second), [Colon() for j = i+1:nspace])]] for i = 1:nspace])
105107

106108
nottime2dim = [nottime[i] => i for i in 1:nspace]
107109
dim2nottime = [i => nottime[i] for i in 1:nspace]
108-
return DiscreteSpace{nspace,length(depvars)}(depvars, Dict(depvarsdisc), nottime, indvars, Dict(axies), Dict(grid), Dict(dxs), Iaxies, Igrid, Iedge)
110+
return DiscreteSpace{nspace,length(depvars)}(depvars, Dict(depvarsdisc), nottime, indvars, Dict(axies), Dict(grid), Dict(dxs), Iaxies, Igrid, Iedge, nottime2dim)
109111
end
110112

111113

test/DiscreteSpace.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using ModelingToolkit, MethodOfLines, DomainSets, Test
2+
3+
@parameters x, y, t
4+
@variables u(..) v(..)
5+
6+
indvars = [x, y, t]
7+
nottime = [x, y]
8+
depvars = [u, v]
9+
10+
t_min= 0.
11+
t_max = 2.0
12+
x_min = 0.
13+
x_max = 2.
14+
y_min = 0.
15+
y_max = 2.
16+
dx = 0.1; dy = 0.2
17+
order = 2
18+
19+
domains = [t Interval(t_min, t_max), x Interval(x_min, x_max), y Interval(y_min,y_max)]
20+
21+
@testset "Test 01: discretization of variables, center aligned grid" begin
22+
# Test centered order
23+
disc = MOLFiniteDifference([x=>dx, y=>dy], t; centered_order=order)
24+
25+
s = MethodOfLines.DiscreteSpace(domains, depvars, indvars, nottime, disc)
26+
27+
discx = x_min:dx:x_max
28+
discy = y_min:dy:y_max
29+
30+
@test s.grid[x] == discx
31+
@test s.grid[y] == discy
32+
33+
@test s.axies[x] == s.grid[x]
34+
@test s.axies[y] == s.grid[y]
35+
36+
@test s.discvars[u] == collect(first(@variables u[axes(discx), axes(discy)]))
37+
@test s.discvars[v] == collect(first(@variables v[axes(discx), axes(discy)]))
38+
39+
@test CartesianIndex(1, 10) s.Iedge
40+
@test all([I s.Igrid for I in s.Iedge])
41+
42+
@test s.Iaxies == s.Igrid
43+
44+
end
45+
46+
@testset "Test 02: discretization of variables, edge aligned grid" begin
47+
# Test centered order
48+
disc = MOLFiniteDifference([x=>dx, y=>dy], t; centered_order=order, grid_align=edge_align)
49+
50+
s = MethodOfLines.DiscreteSpace(domains, depvars, indvars, nottime, disc)
51+
52+
discx = (x_min-dx/2): dx : (x_max+dx/2)
53+
discy = (y_min-dy/2): dy : (y_max+dy/2)
54+
55+
@test s.grid[x] == discx
56+
@test s.grid[y] == discy
57+
58+
@test s.axies[x] != s.grid[x]
59+
@test s.axies[y] != s.grid[y]
60+
61+
@test s.discvars[u] == collect(first(@variables u[axes(discx), axes(discy)]))
62+
@test s.discvars[v] == collect(first(@variables v[axes(discx), axes(discy)]))
63+
64+
@test CartesianIndex(1, 10) s.Iedge
65+
@test all([I s.Igrid for I in s.Iedge])
66+
67+
@test s.Iaxies != s.Igrid
68+
69+
end

0 commit comments

Comments
 (0)