1D example
using FaADE
As an example we'll solve the 1D heat equation with no parallel mapping. The PDE is
\[ \frac{\partial u}{\partial t} = k\frac{\partial}{\partial x}\frac{\partial u}{\partial x}\]
with boundary conditions
\[ u(0,t) = 0, \qquad \partial_x u(1,t) = 0\]
and initial condition
\[ u(x,0) = \exp\left(\frac{-(x-0.5)^2}{0.02}\right)\]
We first create a domain to solve the PDE using Grid1D
,
𝒟 = [0.0,1.0]
n = 41
grid = Grid1D(𝒟,n)
Grid1D{Float64, CartesianMetric, Vector{Float64}, Float64}([0.0, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.175, 0.2, 0.225 … 0.775, 0.8, 0.825, 0.85, 0.875, 0.9, 0.925, 0.95, 0.975, 1.0], 0.025, 41)
The initial condition is a simple Gaussian
u₀(x) = exp.(-(x.-0.5).^2 ./ 0.02)
u₀ (generic function with 1 method)
Create a few more things we'll need for the PDE and the solver such as the order (2) and the solver (conjugate gradient implicit euler)
order = 2;
The boundary conditions are defined by creating SimultanousApproximationTerm
objects, which will then be fed to the PDE structure
BoundaryLeft = SAT_Dirichlet(t->0.0, grid.Δx, Left, order)
BoundaryRight = SAT_Neumann(t->0.0, grid.Δx, Right, order)
BCs = (BoundaryLeft, BoundaryRight)
(SAT_Dirichlet{FaADE.Helpers.NodeType{:Left, 1}, :Cartesian, Float64, Vector{Float64}, Main.var"#1#2", typeof(eachcol)}(FaADE.Helpers.NodeType{:Left, 1}(), 1, 2, Main.var"#1#2"(), 6400.0, [3200.0, -1600.0], 0.025, 1.0, [-2.0], eachcol, 0.0, :Cartesian), SAT_Neumann{FaADE.Helpers.NodeType{:Right, 1}, :Cartesian, Float64, Vector{Float64}, Main.var"#3#4", typeof(eachcol)}(FaADE.Helpers.NodeType{:Right, 1}(), 1, 2, Main.var"#3#4"(), -80.0, [-40.0, 40.0], 0.025, 1.0, eachcol, 0.0, :Cartesian))
We will set the diffusion coefficient to 1 eveywhere in the domain
K = 1.0
1.0
Now we can create a PDEProblem
object to pass to the solver,
P = Problem1D(order,u₀,K,grid,BCs)
1 dimensional PDE Problem
Lastly before solving we define our time step and simulation time,
Δt = 0.01grid.Δx;
t_f = 100Δt;
Finally we call the solver (currently not working with Documenter.jl
)
soln = solve(P,grid,Δt,t_f)
FaADE.solvers.solution{Float64, Vector{Float64}, Grid1D{Float64, CartesianMetric, Vector{Float64}, Float64}, Problem1D{Float64, 1, Float64, FaADE.Helpers.SourceTerm{Nothing}, Tuple{SAT_Dirichlet{FaADE.Helpers.NodeType{:Left, 1}, :Cartesian, Float64, Vector{Float64}, Main.var"#1#2", typeof(eachcol)}, SAT_Neumann{FaADE.Helpers.NodeType{:Right, 1}, :Cartesian, Float64, Vector{Float64}, Main.var"#3#4", typeof(eachcol)}}, Nothing}}([[3.726653172078671e-6, 1.2607105177048523e-5, 4.006529739295107e-5, 0.00011961288358102458, 0.00033546262790251126, 0.00088382630693505, 0.0021874911181828873, 0.005086069231012701, 0.011108996538242306, 0.022794180883612337 … 0.022794180883612337, 0.011108996538242297, 0.005086069231012709, 0.0021874911181828873, 0.00088382630693505, 0.00033546262790251126, 0.00011961288358102415, 4.0065297392951136e-5, 1.2607105177048523e-5, 3.726653172078671e-6], [7.918980349308757e-9, 0.021152869879810986, 0.04256098354497417, 0.06445847910277686, 0.08703822411191105, 0.11043352224618926, 0.13470259635866993, 0.15981670099969636, 0.1856526272894754, 0.211990222878665 … 0.22231299944449556, 0.19951240466718279, 0.17823767919703276, 0.15893879539116548, 0.14199849926627836, 0.12773194221080134, 0.11638883605147725, 0.10815712721370743, 0.10316716722134703, 0.10149544894152006]], Grid1D{Float64, CartesianMetric, Vector{Float64}, Float64}([0.0, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.175, 0.2, 0.225 … 0.775, 0.8, 0.825, 0.85, 0.875, 0.9, 0.925, 0.95, 0.975, 1.0], 0.025, 41), [0.00025, 0.00025], [0.0, 0.025], 1 dimensional PDE Problem, 0.0036615900896113684, Float64[])
The solver outputs a solution
data structure, with everything packaged in that we would need to reconstruct the problem from the final state if we wanted to restart.
using Plots
plot(grid.grid, soln.u[2])
This page was generated using Literate.jl.