22
33## Heat equation
44
5+ ### Dirichlet boundary conditions
6+
57``` julia
68using 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
1519eq = 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
2125domains = [t ∈ IntervalDomain (0.0 ,1.0 ),
22- x ∈ IntervalDomain (0.0 ,1.0 )]
26+ x ∈ IntervalDomain (0.0 ,1.0 )]
2327
2428# PDE system
2529pdesys = PDESystem (eq,bcs,domains,[t,x],[u])
@@ -34,5 +38,122 @@ prob = discretize(pdesys,discretization)
3438
3539# Solve ODE problem
3640using 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 ) + 3 Dx (u (t,- 1.0 )) ~ exp (- t) * (sin (- 1.0 ) + 3 cos (- 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```
0 commit comments