Skip to content

Commit 594171d

Browse files
committed
Higher Order Passing
1 parent 59481d7 commit 594171d

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

src/MOL_discretization.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ function SciMLBase.symbolic_discretize(pdesys::PDESystem, discretization::Method
6060
BoundaryHandler!!(u0, bceqs, pdesys.bcs, s, depvar_ops, tspan, derivweights)
6161

6262
# Find the indexes on the interior
63-
Iinterior = interior(s.Igrid, nparams(s))
64-
63+
64+
65+
Iinterior = s.Igrid[[let bcs = get_bc_counts(i, s, pdesys.bcs)
66+
(1 + first(bcs)):length(s.grid[x])-last(bcs)
67+
end
68+
for (i,x) in enumerate(s.x̄)]...]
69+
70+
71+
6572
# Discretize the equation on the interior
6673
pdeeqs = vec(map(Iinterior) do II
6774
rules = vcat(generate_finite_difference_rules(II, s, pde, derivweights), valmaps(s, II))

src/bcs/generate_bc_eqs.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ function generate_boundary_matching_rules(s, orders)
2222
return (lower, upper)
2323
end
2424

25+
26+
#---- Count Boundary Equations --------------------
27+
# Count the number of boundary equations that lie at the spatial boundary on
28+
# both the left and right side. This will be used to determine number of
29+
# interior equations s.t. we have a balanced system of equations.
30+
31+
# get the depvar boundary terms for given depvar and indvar index.
32+
get_depvarbcs(u, s, i) = substitute.((u,), get_edgevals(s, i))
33+
34+
# return the counts of the boundary-conditions that reference the "left" and
35+
# "right" edges of the given independent variable. Note that we return the
36+
# max of the count for each depvar in the system of equations.
37+
@inline function get_bc_counts(i, s, bcs)
38+
left = 0
39+
right = 0
40+
for u in s.
41+
depvaredges = get_depvarbcs(u, s, i)
42+
counts = [map(u -> occursin(u, bc.lhs), depvaredges) for bc in bcs]
43+
left = max(left, sum([c[1] for c in counts]))
44+
right = max(right, sum([c[2] for c in counts]))
45+
end
46+
return [left, right]
47+
end
48+
49+
2550
"""
2651
Mutates bceqs and u0 by finding relevant equations and discretizing them.
2752
TODO: return a handler for use with generate_finite_difference_rules and pull out initial condition. Important to remember that BCs can have

src/discretization/discretize_vars.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ end
1010
Return a map of of variables to the gridpoints at the edge of the domain
1111
"""
1212
@inline function get_edgevals(s, i)
13-
return [s.x̄[i] => first(s.axies[x̄[i]]), s.x̄[i] => last(s.axies[x̄[i]])]
13+
return [s.x̄[i] => first(s.axies[s.x̄[i]]), s.x̄[i] => last(s.axies[s.x̄[i]])]
1414
end
1515

1616
@inline function subvar(depvar, edge_vals)
@@ -75,9 +75,9 @@ varmaps(s::DiscreteSpace, II) = [u => s.discvars[u][II] for u in s.ū]
7575
valmaps(s::DiscreteSpace, II) = vcat(varmaps(s,II), gridvals(s,II))
7676

7777

78-
7978
interior(Igrid, N) = Igrid[[2:(size(Igrid, i)-1) for i in 1:N]...]
8079

80+
8181
map_symbolic_to_discrete(II::CartesianIndex, s::DiscreteSpace{N,M}) where {N,M} = vcat([s.ū[k] => s.discvars[k][II] for k = 1:M], [s.x̄[j] => s.grid[j][II[j]] for j = 1:N])
8282

8383
# TODO: Allow other grids
@@ -170,4 +170,4 @@ edges(s::DiscreteSpace) = s.Iedges
170170
Ivalid = setdiff(s.Igrid, vcat(vec(Iinterior), vec(Iedges)))
171171
#TODO: change this when vars have different domains
172172
return mapreduce(u -> vec(s.discvars[u][Ivalid]) .~ 0., vcat, s.ū)::Vector
173-
end
173+
end

test/pde_systems/MOL_1D_HigherOrder.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,24 @@ end
107107
@named pdesys = PDESystem(eq,bcs,domains,[x,t],[u(x,t)])
108108
prob = discretize(pdesys,discretization)
109109

110-
sol = solve(prob,Tsit5(),saveat=0.1,dt=dt)
110+
sol = solve(prob,Rosenbrock23(),saveat=0.1,dt=dt)
111111

112112
@test sol.retcode == :Success
113113

114-
xs = domains[1].domain.lower+dx+dx+dx:dx:domains[1].domain.upper-dx-dx
114+
xs = domains[1].domain.lower+dx+dx+dx:dx:domains[1].domain.upper
115115
ts = sol.t
116116

117117
u_predict = sol.u
118118
u_real = [[u_analytic(x, t) for x in xs] for t in ts]
119+
120+
121+
# anim = @animate for (i,T) in enumerate(ts)
122+
# plot(xs, u_real[i], seriestype = :scatter,label="Analytic solution")
123+
# plot!(xs, sol.u[i], label="Numeric solution")
124+
# plot!(xs, log.(abs.(u_real[i]-sol.u[i])), label="Log Error at t = $(ts[i])")
125+
# end
126+
# gif(anim, "plots/MOL_Higher_order_1D_KdV_single_soliton.gif", fps = 5)
127+
128+
119129
@test all(isapprox.(u_predict, u_real, rtol = 0.03))
120130
end

test/runtests.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const is_TRAVIS = haskey(ENV,"TRAVIS")
99
@time begin
1010

1111
if GROUP == "All" || GROUP == "Component Tests"
12-
# @time @safetestset "Test for regression against original code" begin include("regression_test.jl") end
13-
# @time @safetestset "MOLFiniteDifference Utils" begin include("utils_test.jl") end
14-
# @time @safetestset "Discretization of space and grid types" begin include("components/DiscreteSpace.jl") end
15-
#@time @safetestset "Finite Difference Schemes" begin include("components/finite_diff_schemes.jl") end
12+
#@time @safetestset "Test for regression against original code" begin include("regression_test.jl") end
13+
@time @safetestset "MOLFiniteDifference Utils" begin include("utils_test.jl") end
14+
@time @safetestset "Discretization of space and grid types" begin include("components/DiscreteSpace.jl") end
15+
@time @safetestset "Finite Difference Schemes" begin include("components/finite_diff_schemes.jl") end
1616
end
1717

1818
if GROUP == "All" || GROUP == "Integration Tests"
19-
#@time @safetestset "MOLFiniteDifference Interface" begin include("pde_systems/MOLtest.jl") end
19+
@time @safetestset "MOLFiniteDifference Interface" begin include("pde_systems/MOLtest.jl") end
2020
#@time @safetestset "MOLFiniteDifference Interface: Linear Convection" begin include("pde_systems/MOL_1D_Linear_Convection.jl") end
2121
@time @safetestset "MOLFiniteDifference Interface: 1D Linear Diffusion" begin include("pde_systems/MOL_1D_Linear_Diffusion.jl") end
2222
@time @safetestset "MOLFiniteDifference Interface: 1D Non-Linear Diffusion" begin include("pde_systems/MOL_1D_NonLinear_Diffusion.jl") end

0 commit comments

Comments
 (0)