Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/examples/p4est_2d_dgsem/elixir_advection_diffusion_periodic.jl
2055 views
1
using OrdinaryDiffEqLowStorageRK
2
using Trixi
3
4
###############################################################################
5
# semidiscretization of the linear advection-diffusion equation
6
7
diffusivity() = 5.0e-2
8
advection_velocity = (1.0, 0.0)
9
equations = LinearScalarAdvectionEquation2D(advection_velocity)
10
equations_parabolic = LaplaceDiffusion2D(diffusivity(), equations)
11
12
function x_trans_periodic(x, domain_length = SVector(2 * pi), center = SVector(0.0))
13
x_normalized = x .- center
14
x_shifted = x_normalized .% domain_length
15
x_offset = ((x_shifted .< -0.5 * domain_length) - (x_shifted .> 0.5 * domain_length)) .*
16
domain_length
17
return center + x_shifted + x_offset
18
end
19
20
# Define initial condition (copied from "examples/tree_1d_dgsem/elixir_advection_diffusion.jl")
21
function initial_condition_diffusive_convergence_test(x, t,
22
equation::LinearScalarAdvectionEquation2D)
23
# Store translated coordinate for easy use of exact solution
24
# Assumes that advection_velocity[2] = 0 (effectively that we are solving a 1D equation)
25
x_trans = x_trans_periodic(x[1] - equation.advection_velocity[1] * t)
26
27
nu = diffusivity()
28
c = 0.0
29
A = 1.0
30
omega = 1.0
31
scalar = c + A * sin(omega * sum(x_trans)) * exp(-nu * omega^2 * t)
32
return SVector(scalar)
33
end
34
initial_condition = initial_condition_diffusive_convergence_test
35
36
# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux
37
solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)
38
39
coordinates_min = (-pi, -pi) # minimum coordinates (min(x), min(y))
40
coordinates_max = (pi, pi) # maximum coordinates (max(x), max(y))
41
42
trees_per_dimension = (4, 4)
43
mesh = P4estMesh(trees_per_dimension,
44
polydeg = 3, initial_refinement_level = 2,
45
coordinates_min = coordinates_min, coordinates_max = coordinates_max,
46
periodicity = true)
47
48
# A semidiscretization collects data structures and functions for the spatial discretization
49
semi = SemidiscretizationHyperbolicParabolic(mesh,
50
(equations, equations_parabolic),
51
initial_condition, solver)
52
53
###############################################################################
54
# ODE solvers, callbacks etc.
55
56
# Create ODE problem with time span `tspan`
57
tspan = (0.0, 1.0)
58
ode = semidiscretize(semi, tspan)
59
60
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
61
# and resets the timers
62
summary_callback = SummaryCallback()
63
64
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
65
analysis_interval = 100
66
analysis_callback = AnalysisCallback(semi, interval = analysis_interval)
67
68
# The AliveCallback prints short status information in regular intervals
69
alive_callback = AliveCallback(analysis_interval = analysis_interval)
70
71
# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
72
callbacks = CallbackSet(summary_callback, analysis_callback, alive_callback)
73
74
###############################################################################
75
# run the simulation
76
77
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
78
time_int_tol = 1.0e-11
79
sol = solve(ode, RDPK3SpFSAL49(); abstol = time_int_tol, reltol = time_int_tol,
80
ode_default_options()..., callback = callbacks)
81
82