Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/examples/t8code_3d_dgsem/elixir_advection_nonconforming.jl
5586 views
1
using OrdinaryDiffEqLowStorageRK
2
using Trixi
3
4
###############################################################################
5
# Semidiscretization of the linear advection equation.
6
7
advection_velocity = (0.2, -0.7, 0.5)
8
equations = LinearScalarAdvectionEquation3D(advection_velocity)
9
10
# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux.
11
solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)
12
13
coordinates_min = (-1.0, -1.0, -1.0) # minimum coordinates (min(x), min(y), min(z))
14
coordinates_max = (1.0, 1.0, 1.0) # maximum coordinates (max(x), max(y), max(z))
15
trees_per_dimension = (1, 1, 1)
16
17
# Note that it is not necessary to use mesh polydeg lower than the solver polydeg
18
# on a Cartesian mesh.
19
# See https://doi.org/10.1007/s10915-018-00897-9, Section 6.
20
mesh = T8codeMesh(trees_per_dimension, polydeg = 3,
21
coordinates_min = coordinates_min, coordinates_max = coordinates_max,
22
initial_refinement_level = 2,
23
periodicity = true)
24
25
# Note: This is actually a `p8est_quadrant_t` which is much bigger than the
26
# following struct. But we only need the first four fields for our purpose.
27
struct t8_dhex_t
28
x::Int32
29
y::Int32
30
z::Int32
31
level::Int8
32
# [...] # See `p8est.h` in `p4est` for more info.
33
end
34
35
# Refine bottom left quadrant of each second tree to level 2
36
function adapt_callback(forest, ltreeid, eclass_scheme, lelemntid, elements, is_family,
37
user_data)
38
el = unsafe_load(Ptr{t8_dhex_t}(elements[1]))
39
40
if iseven(convert(Int, ltreeid)) && el.x == 0 && el.y == 0 && el.z == 0 &&
41
el.level < 3
42
# return true (refine)
43
return 1
44
else
45
# return false (don't refine)
46
return 0
47
end
48
end
49
50
Trixi.adapt!(mesh, adapt_callback)
51
52
# A semidiscretization collects data structures and functions for the spatial discretization
53
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergence_test,
54
solver;
55
boundary_conditions = boundary_condition_periodic)
56
57
###############################################################################
58
# ODE solvers, callbacks etc.
59
60
# Create ODE problem with time span from 0.0 to 1.0
61
tspan = (0.0, 1.0)
62
ode = semidiscretize(semi, tspan)
63
64
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
65
# and resets the timers
66
summary_callback = SummaryCallback()
67
68
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
69
analysis_callback = AnalysisCallback(semi, interval = 100)
70
71
# The SaveSolutionCallback allows to save the solution to a file in regular intervals
72
save_solution = SaveSolutionCallback(interval = 100,
73
solution_variables = cons2prim)
74
75
# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
76
stepsize_callback = StepsizeCallback(cfl = 1.6)
77
78
# Create a CallbackSet to collect all callbacks such that they can be passed to the ODE solver
79
callbacks = CallbackSet(summary_callback, analysis_callback, save_solution,
80
stepsize_callback)
81
82
###############################################################################
83
# run the simulation
84
85
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
86
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false);
87
dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
88
ode_default_options()..., callback = callbacks);
89
90