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_cfl.jl
5586 views
1
2
using OrdinaryDiffEqLowStorageRK
3
using Trixi
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
solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)
14
15
coordinates_min = -convert(Float64, pi)
16
coordinates_max = convert(Float64, pi)
17
18
# Create a uniformly refined mesh with periodic boundaries
19
mesh = TreeMesh(coordinates_min, coordinates_max,
20
initial_refinement_level = 4,
21
n_cells_max = 30_000, periodicity = true) # set maximum capacity of tree data structure)
22
23
function x_trans_periodic(x, domain_length = SVector(2 * pi), center = SVector(0.0))
24
x_normalized = x .- center
25
x_shifted = x_normalized .% domain_length
26
x_offset = ((x_shifted .< -0.5 * domain_length) - (x_shifted .> 0.5 * domain_length)) .*
27
domain_length
28
return center + x_shifted + x_offset
29
end
30
31
function initial_condition_diffusive_convergence_test(x, t,
32
equation::LinearScalarAdvectionEquation1D)
33
# Store translated coordinate for easy use of exact solution
34
x_trans = x_trans_periodic(x - equation.advection_velocity * t)
35
36
nu = diffusivity()
37
c = 0.0
38
A = 1.0
39
L = 2
40
f = 1 / L
41
omega = 1.0
42
scalar = c + A * sin(omega * sum(x_trans)) * exp(-nu * omega^2 * t)
43
return SVector(scalar)
44
end
45
initial_condition = initial_condition_diffusive_convergence_test
46
47
semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic),
48
initial_condition, solver;
49
boundary_conditions = (boundary_condition_periodic,
50
boundary_condition_periodic))
51
52
###############################################################################
53
# ODE solvers, callbacks etc.
54
55
tspan = (0.0, 1.0)
56
ode = semidiscretize(semi, tspan);
57
58
summary_callback = SummaryCallback()
59
60
analysis_callback = AnalysisCallback(semi, interval = 100)
61
62
alive_callback = AliveCallback(analysis_interval = 100)
63
64
# Stepsize callback which selects the timestep according to the most restrictive CFL condition.
65
# For coarser grids, linear stability is governed by the hyperbolic CFL condition,
66
# while for high refinements the flow becomes diffusion-dominated.
67
stepsize_callback = StepsizeCallback(cfl = 1.6,
68
cfl_parabolic = 0.3)
69
70
callbacks = CallbackSet(summary_callback, analysis_callback, alive_callback,
71
stepsize_callback)
72
73
###############################################################################
74
# run the simulation
75
76
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false),
77
dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
78
save_everystep = false, callback = callbacks);
79
80