Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/examples/tree_1d_dgsem/elixir_advection_diffusion.jl
5586 views
1
using OrdinaryDiffEqSDIRK, ADTypes
2
using Trixi
3
import LinearSolve: LUFactorization
4
5
###############################################################################
6
# semidiscretization of the linear advection diffusion equation
7
8
advection_velocity = 0.1
9
equations = LinearScalarAdvectionEquation1D(advection_velocity)
10
diffusivity() = 0.1
11
equations_parabolic = LaplaceDiffusion1D(diffusivity(), equations)
12
13
# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux
14
solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)
15
16
coordinates_min = -convert(Float64, pi) # minimum coordinate
17
coordinates_max = convert(Float64, pi) # maximum coordinate
18
19
# Create a uniformly refined mesh with periodic boundaries
20
mesh = TreeMesh(coordinates_min, coordinates_max,
21
initial_refinement_level = 4,
22
n_cells_max = 30_000, # set maximum capacity of tree data structure
23
periodicity = true)
24
25
function x_trans_periodic(x, domain_length = SVector(oftype(x[1], 2 * pi)),
26
center = SVector(oftype(x[1], 0)))
27
x_normalized = x .- center
28
x_shifted = x_normalized .% domain_length
29
x_offset = ((x_shifted .< -0.5f0 * domain_length) -
30
(x_shifted .> 0.5f0 * domain_length)) .*
31
domain_length
32
return center + x_shifted + x_offset
33
end
34
35
# Define initial condition
36
function initial_condition_diffusive_convergence_test(x, t,
37
equation::LinearScalarAdvectionEquation1D)
38
# Store translated coordinate for easy use of exact solution
39
x_trans = x_trans_periodic(x - equation.advection_velocity * t)
40
41
nu = diffusivity()
42
c = 0
43
A = 1
44
omega = 1
45
scalar = c + A * sin(omega * sum(x_trans)) * exp(-nu * omega^2 * t)
46
return SVector(scalar)
47
end
48
initial_condition = initial_condition_diffusive_convergence_test
49
50
# define periodic boundary conditions everywhere
51
boundary_conditions = boundary_condition_periodic
52
boundary_conditions_parabolic = boundary_condition_periodic
53
54
# A semidiscretization collects data structures and functions for the spatial discretization
55
semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic),
56
initial_condition,
57
solver;
58
boundary_conditions = (boundary_conditions,
59
boundary_conditions_parabolic))
60
61
###############################################################################
62
# ODE solvers, callbacks etc.
63
64
# Create ODE problem with time span from 0.0 to 1.0
65
tspan = (0.0, 1.0)
66
ode = semidiscretize(semi, tspan)
67
68
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
69
# and resets the timers
70
summary_callback = SummaryCallback()
71
72
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
73
analysis_callback = AnalysisCallback(semi, interval = 100)
74
75
# The AliveCallback prints short status information in regular intervals
76
alive_callback = AliveCallback(analysis_interval = 100)
77
78
# The SaveRestartCallback allows to save a file from which a Trixi.jl simulation can be restarted
79
save_restart = SaveRestartCallback(interval = 100,
80
save_final_restart = true)
81
82
# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
83
callbacks = CallbackSet(summary_callback, analysis_callback, alive_callback, save_restart)
84
85
###############################################################################
86
# run the simulation
87
88
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
89
time_int_tol = 1.0e-10
90
time_abs_tol = 1.0e-10
91
# This is an IMEX SDIRK method
92
ode_alg = KenCarp4(autodiff = AutoFiniteDiff(), linsolve = LUFactorization())
93
sol = solve(ode, ode_alg;
94
abstol = time_abs_tol, reltol = time_int_tol,
95
ode_default_options()..., callback = callbacks)
96
97