Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/examples/p4est_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 = P4estMesh(trees_per_dimension, polydeg = 3,
21
coordinates_min = coordinates_min, coordinates_max = coordinates_max,
22
initial_refinement_level = 2,
23
periodicity = true)
24
25
# Refine bottom left quadrant of each tree to level 3
26
function refine_fn(p8est, which_tree, quadrant)
27
quadrant_obj = unsafe_load(quadrant)
28
if quadrant_obj.x == 0 && quadrant_obj.y == 0 && quadrant_obj.z == 0 &&
29
quadrant_obj.level < 3
30
# return true (refine)
31
return Cint(1)
32
else
33
# return false (don't refine)
34
return Cint(0)
35
end
36
end
37
38
# Refine recursively until each bottom left quadrant of a tree has level 3
39
# The mesh will be rebalanced before the simulation starts
40
refine_fn_c = @cfunction(refine_fn, Cint,
41
(Ptr{Trixi.p8est_t}, Ptr{Trixi.p4est_topidx_t},
42
Ptr{Trixi.p8est_quadrant_t}))
43
Trixi.refine_p4est!(mesh.p4est, true, refine_fn_c, C_NULL)
44
45
# A semidiscretization collects data structures and functions for the spatial discretization
46
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergence_test,
47
solver;
48
boundary_conditions = boundary_condition_periodic)
49
50
###############################################################################
51
# ODE solvers, callbacks etc.
52
53
# Create ODE problem with time span from 0.0 to 1.0
54
tspan = (0.0, 1.0)
55
ode = semidiscretize(semi, tspan)
56
57
# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
58
# and resets the timers
59
summary_callback = SummaryCallback()
60
61
# The AnalysisCallback allows to analyse the solution in regular intervals and prints the results
62
analysis_callback = AnalysisCallback(semi, interval = 100)
63
64
# The SaveSolutionCallback allows to save the solution to a file in regular intervals
65
save_solution = SaveSolutionCallback(interval = 100,
66
solution_variables = cons2prim)
67
68
# The StepsizeCallback handles the re-calculation of the maximum Δt after each time step
69
stepsize_callback = StepsizeCallback(cfl = 1.6)
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, save_solution,
73
stepsize_callback)
74
75
###############################################################################
76
# run the simulation
77
78
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes the passed callbacks
79
sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false);
80
dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
81
ode_default_options()..., callback = callbacks);
82
83