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

Commit 175553a

Browse files
Merge pull request #349 from SciML/mol
New MOLFiniteDifference Discretization
2 parents 086931d + c23eb0c commit 175553a

9 files changed

Lines changed: 324 additions & 615 deletions

File tree

docs/src/symbolic_tutorials/mol_heat.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ bcs = [u(0,x) ~ cos(x),
2525

2626
# Space and time domains
2727
domains = [t IntervalDomain(0.0,1.0),
28-
x IntervalDomain(0.0,1.0)]
28+
x IntervalDomain(0.0,1.0)]
2929

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

3333
# Method of lines discretization
3434
dx = 0.1
3535
order = 2
36-
discretization = MOLFiniteDifference(dx,order)
36+
discretization = MOLFiniteDifference([x=>dx],t)
3737

3838
# Convert the PDE problem into an ODE problem
3939
prob = discretize(pdesys,discretization)
@@ -43,16 +43,18 @@ using OrdinaryDiffEq
4343
sol = solve(prob,Tsit5(),saveat=0.2)
4444

4545
# Plot results and compare with exact solution
46-
x = prob.space[2]
46+
x = (0:dx:1)[2:end-1]
4747
t = sol.t
4848

4949
using Plots
5050
plt = plot()
51+
5152
for i in 1:length(t)
52-
plot!(x,Array(prob.extrapolation[1](t[i])*sol.u[i]),label="Numerical, t=$(t[i])")
53+
plot!(x,sol.u[i],label="Numerical, t=$(t[i])")
5354
scatter!(x, u_exact(x, t[i]),label="Exact, t=$(t[i])")
5455
end
5556
display(plt)
57+
savefig("plot.png")
5658
```
5759
### Neumann boundary conditions
5860

@@ -79,13 +81,13 @@ domains = [t ∈ IntervalDomain(0.0,1.0),
7981
x IntervalDomain(0.0,1.0)]
8082

8183
# PDE system
82-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
84+
pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
8385

8486
# Method of lines discretization
8587
# Need a small dx here for accuracy
8688
dx = 0.01
8789
order = 2
88-
discretization = MOLFiniteDifference(dx,order)
90+
discretization = MOLFiniteDifference([x=>dx],t)
8991

9092
# Convert the PDE problem into an ODE problem
9193
prob = discretize(pdesys,discretization)
@@ -95,16 +97,18 @@ using OrdinaryDiffEq
9597
sol = solve(prob,Tsit5(),saveat=0.2)
9698

9799
# Plot results and compare with exact solution
98-
x = prob.space[2]
100+
x = (0:dx:1)[2:end-1]
99101
t = sol.t
100102

101103
using Plots
102104
plt = plot()
105+
103106
for i in 1:length(t)
104-
plot!(x,Array(prob.extrapolation[1](t[i])*sol.u[i]),label="Numerical, t=$(t[i])")
107+
plot!(x,sol.u[i],label="Numerical, t=$(t[i])",lw=12)
105108
scatter!(x, u_exact(x, t[i]),label="Exact, t=$(t[i])")
106109
end
107110
display(plt)
111+
savefig("plot.png")
108112
```
109113

110114
### Robin boundary conditions
@@ -132,13 +136,13 @@ domains = [t ∈ IntervalDomain(0.0,1.0),
132136
x IntervalDomain(-1.0,1.0)]
133137

134138
# PDE system
135-
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
139+
pdesys = PDESystem(eq,bcs,domains,[t,x],[u(t,x)])
136140

137141
# Method of lines discretization
138142
# Need a small dx here for accuracy
139143
dx = 0.05
140144
order = 2
141-
discretization = MOLFiniteDifference(dx,order)
145+
discretization = MOLFiniteDifference([x=>dx],t)
142146

143147
# Convert the PDE problem into an ODE problem
144148
prob = discretize(pdesys,discretization)
@@ -148,14 +152,16 @@ using OrdinaryDiffEq
148152
sol = solve(prob,Tsit5(),saveat=0.2)
149153

150154
# Plot results and compare with exact solution
151-
x = prob.space[2]
155+
x = (0:dx:1)[2:end-1]
152156
t = sol.t
153157

154158
using Plots
155159
plt = plot()
160+
156161
for i in 1:length(t)
157-
plot!(x,Array(prob.extrapolation[1](t[i])*sol.u[i]),label="Numerical, t=$(t[i])")
162+
plot!(x,sol.u[i],label="Numerical, t=$(t[i])")
158163
scatter!(x, u_exact(x, t[i]),label="Exact, t=$(t[i])")
159164
end
160165
display(plt)
166+
savefig("plot.png")
161167
```

src/DiffEqOperators.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export AbstractDerivativeOperator, DerivativeOperator,
6363
export DirichletBC, Dirichlet0BC, NeumannBC, Neumann0BC, RobinBC, GeneralBC, MultiDimBC, PeriodicBC,
6464
MultiDimDirectionalBC, ComposedMultiDimBC
6565
export compose, decompose, perpsize
66+
export discretize, symbolic_discretize
6667

6768
export GhostDerivativeOperator
6869
export MOLFiniteDifference

0 commit comments

Comments
 (0)