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

Commit c552b76

Browse files
Merge pull request #306 from tinosulzer/issue-300-Robin-Neumann-bcs
Issue 300 robin neumann bcs
2 parents b9e3315 + ce1a3f1 commit c552b76

5 files changed

Lines changed: 641 additions & 79 deletions

File tree

docs/src/examples.md

Lines changed: 126 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
## Heat equation
44

5+
### Dirichlet boundary conditions
6+
57
```julia
68
using ModelingToolkit, DiffEqOperators
9+
# Method of Manufactured Solutions: exact solution
10+
u_exact = (x,t) -> exp.(-t) * cos.(x)
711

812
# Parameters, variables, and derivatives
913
@parameters t x
@@ -13,13 +17,13 @@ using ModelingToolkit, DiffEqOperators
1317

1418
# 1D PDE and boundary conditions
1519
eq = Dt(u(t,x)) ~ Dxx(u(t,x))
16-
bcs = [u(0,x) ~ -x*(x-1)*sin(x),
17-
u(t,0) ~ 0.0,
18-
u(t,1) ~ 0.0]
20+
bcs = [u(0,x) ~ cos(x),
21+
u(t,0) ~ exp(-t),
22+
u(t,1) ~ exp(-t) * cos(1)]
1923

2024
# Space and time domains
2125
domains = [t IntervalDomain(0.0,1.0),
22-
x IntervalDomain(0.0,1.0)]
26+
x IntervalDomain(0.0,1.0)]
2327

2428
# PDE system
2529
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
@@ -34,5 +38,122 @@ prob = discretize(pdesys,discretization)
3438

3539
# Solve ODE problem
3640
using OrdinaryDiffEq
37-
sol = solve(prob,Tsit5(),saveat=0.1)
41+
sol = solve(prob,Tsit5(),saveat=0.2)
42+
43+
# Plot results and compare with exact solution
44+
x = prob.space[2]
45+
t = sol.t
46+
47+
using Plots
48+
plt = plot()
49+
for i in 1:length(t)
50+
plot!(x,Array(prob.extrapolation[1](t[i])*sol.u[i]),label="Numerical, t=$(t[i])")
51+
scatter!(x, u_exact(x, t[i]),label="Exact, t=$(t[i])")
52+
end
53+
display(plt)
54+
```
55+
### Neumann boundary conditions
56+
57+
```julia
58+
using ModelingToolkit, DiffEqOperators
59+
# Method of Manufactured Solutions: exact solution
60+
u_exact = (x,t) -> exp.(-t) * cos.(x)
61+
62+
# Parameters, variables, and derivatives
63+
@parameters t x
64+
@variables u(..)
65+
@derivatives Dt'~t
66+
@derivatives Dx'~x
67+
@derivatives Dxx''~x
68+
69+
# 1D PDE and boundary conditions
70+
eq = Dt(u(t,x)) ~ Dxx(u(t,x))
71+
bcs = [u(0,x) ~ cos(x),
72+
Dx(u(t,0)) ~ 0.0,
73+
Dx(u(t,1)) ~ -exp(-t) * sin(1)]
74+
75+
# Space and time domains
76+
domains = [t IntervalDomain(0.0,1.0),
77+
x IntervalDomain(0.0,1.0)]
78+
79+
# PDE system
80+
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
81+
82+
# Method of lines discretization
83+
# Need a small dx here for accuracy
84+
dx = 0.01
85+
order = 2
86+
discretization = MOLFiniteDifference(dx,order)
87+
88+
# Convert the PDE problem into an ODE problem
89+
prob = discretize(pdesys,discretization)
90+
91+
# Solve ODE problem
92+
using OrdinaryDiffEq
93+
sol = solve(prob,Tsit5(),saveat=0.2)
94+
95+
# Plot results and compare with exact solution
96+
x = prob.space[2]
97+
t = sol.t
98+
99+
using Plots
100+
plt = plot()
101+
for i in 1:length(t)
102+
plot!(x,Array(prob.extrapolation[1](t[i])*sol.u[i]),label="Numerical, t=$(t[i])")
103+
scatter!(x, u_exact(x, t[i]),label="Exact, t=$(t[i])")
104+
end
105+
display(plt)
106+
```
107+
108+
### Robin boundary conditions
109+
110+
```julia
111+
using ModelingToolkit, DiffEqOperators
112+
# Method of Manufactured Solutions
113+
u_exact = (x,t) -> exp.(-t) * sin.(x)
114+
115+
# Parameters, variables, and derivatives
116+
@parameters t x
117+
@variables u(..)
118+
@derivatives Dt'~t
119+
@derivatives Dx'~x
120+
@derivatives Dxx''~x
121+
122+
# 1D PDE and boundary conditions
123+
eq = Dt(u(t,x)) ~ Dxx(u(t,x))
124+
bcs = [u(0,x) ~ sin(x),
125+
u(t,-1.0) + 3Dx(u(t,-1.0)) ~ exp(-t) * (sin(-1.0) + 3cos(-1.0)),
126+
u(t,1.0) + Dx(u(t,1.0)) ~ exp(-t) * (sin(1.0) + cos(1.0))]
127+
128+
# Space and time domains
129+
domains = [t IntervalDomain(0.0,1.0),
130+
x IntervalDomain(-1.0,1.0)]
131+
132+
# PDE system
133+
pdesys = PDESystem(eq,bcs,domains,[t,x],[u])
134+
135+
# Method of lines discretization
136+
# Need a small dx here for accuracy
137+
dx = 0.05
138+
order = 2
139+
discretization = MOLFiniteDifference(dx,order)
140+
141+
# Convert the PDE problem into an ODE problem
142+
prob = discretize(pdesys,discretization)
143+
144+
# Solve ODE problem
145+
using OrdinaryDiffEq
146+
sol = solve(prob,Tsit5(),saveat=0.2)
147+
148+
# Plot results and compare with exact solution
149+
x = prob.space[2]
150+
t = sol.t
151+
152+
using Plots
153+
plt = plot()
154+
for i in 1:length(t)
155+
plot!(x,Array(prob.extrapolation[1](t[i])*sol.u[i]),label="Numerical, t=$(t[i])")
156+
scatter!(x, u_exact(x, t[i]),label="Exact, t=$(t[i])")
157+
end
158+
display(plt)
38159
```

src/DiffEqOperators.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ include("jacvec_operators.jl")
2121
### Utilities
2222
include("utils.jl")
2323

24+
### Exceptions
25+
include("exceptions.jl")
26+
2427
### Boundary Padded Arrays
2528
include("boundary_padded_arrays.jl")
2629

@@ -63,4 +66,5 @@ export compose, decompose, perpsize
6366

6467
export GhostDerivativeOperator
6568
export MOLFiniteDifference
69+
export BoundaryConditionError
6670
end # module

0 commit comments

Comments
 (0)