Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/test/test_tree_1d.jl
2055 views
1
module TestExamplesTree1D
2
3
using Test
4
using Trixi
5
6
include("test_trixi.jl")
7
8
EXAMPLES_DIR = joinpath(examples_dir(), "tree_1d_dgsem")
9
10
# Start with a clean environment: remove Trixi.jl output directory if it exists
11
outdir = "out"
12
isdir(outdir) && rm(outdir, recursive = true)
13
14
@testset "TreeMesh1D" begin
15
#! format: noindent
16
17
# Run basic tests
18
@testset "Examples 1D" begin
19
# Linear scalar advection
20
include("test_tree_1d_advection.jl")
21
22
# Burgers
23
include("test_tree_1d_burgers.jl")
24
25
# Hyperbolic diffusion
26
include("test_tree_1d_hypdiff.jl")
27
28
# Compressible Euler
29
include("test_tree_1d_euler.jl")
30
31
# Compressible Euler Multicomponent
32
include("test_tree_1d_eulermulti.jl")
33
34
# MHD
35
include("test_tree_1d_mhd.jl")
36
37
# MHD Multicomponent
38
include("test_tree_1d_mhdmulti.jl")
39
40
# Compressible Euler with self-gravity
41
include("test_tree_1d_eulergravity.jl")
42
43
# FDSBP methods on the TreeMesh
44
include("test_tree_1d_fdsbp.jl")
45
46
# Traffic flow LWR
47
include("test_tree_1d_traffic_flow_lwr.jl")
48
49
# Linearized Euler
50
include("test_tree_1d_linearizedeuler.jl")
51
52
# Maxwell
53
include("test_tree_1d_maxwell.jl")
54
55
# Passive tracers
56
include("test_tree_1d_passive_tracers.jl")
57
end
58
59
# Coverage test for all initial conditions
60
@testset "Tests for initial conditions" begin
61
# Linear scalar advection
62
@trixi_testset "elixir_advection_extended.jl with initial_condition_sin" begin
63
@test_trixi_include(joinpath(EXAMPLES_DIR, "elixir_advection_extended.jl"),
64
l2=[0.00017373554109980247],
65
linf=[0.0006021275678165239],
66
maxiters=1,
67
initial_condition=Trixi.initial_condition_sin,
68
visualization=TrivialCallback())
69
end
70
71
@trixi_testset "elixir_advection_extended.jl with initial_condition_constant" begin
72
@test_trixi_include(joinpath(EXAMPLES_DIR, "elixir_advection_extended.jl"),
73
l2=[2.441369287653687e-16],
74
linf=[4.440892098500626e-16],
75
maxiters=1,
76
initial_condition=initial_condition_constant,
77
visualization=TrivialCallback())
78
end
79
80
@trixi_testset "elixir_advection_extended.jl with initial_condition_linear_x" begin
81
@test_trixi_include(joinpath(EXAMPLES_DIR, "elixir_advection_extended.jl"),
82
l2=[1.9882464973192864e-16],
83
linf=[1.4432899320127035e-15],
84
maxiters=1,
85
initial_condition=Trixi.initial_condition_linear_x,
86
boundary_conditions=Trixi.boundary_condition_linear_x,
87
periodicity=false,
88
visualization=TrivialCallback())
89
end
90
91
@trixi_testset "elixir_advection_extended.jl with initial_condition_convergence_test" begin
92
@test_trixi_include(joinpath(EXAMPLES_DIR, "elixir_advection_extended.jl"),
93
l2=[6.1803596620800215e-6],
94
linf=[2.4858560899509996e-5],
95
maxiters=1,
96
initial_condition=initial_condition_convergence_test,
97
boundary_conditions=BoundaryConditionDirichlet(initial_condition_convergence_test),
98
periodicity=false,
99
visualization=TrivialCallback())
100
end
101
end
102
103
@testset "Displaying components 1D" begin
104
@test_nowarn include(joinpath(EXAMPLES_DIR, "elixir_advection_amr.jl"))
105
106
# test both short and long printing formats
107
@test_nowarn show(mesh)
108
println()
109
@test_nowarn println(mesh)
110
@test_nowarn display(mesh)
111
112
@test_nowarn show(equations)
113
println()
114
@test_nowarn println(equations)
115
@test_nowarn display(equations)
116
117
@test_nowarn show(solver)
118
println()
119
@test_nowarn println(solver)
120
@test_nowarn display(solver)
121
122
@test_nowarn show(solver.basis)
123
println()
124
@test_nowarn println(solver.basis)
125
@test_nowarn display(solver.basis)
126
127
@test_nowarn show(solver.mortar)
128
println()
129
@test_nowarn println(solver.mortar)
130
@test_nowarn display(solver.mortar)
131
132
@test_nowarn show(solver.volume_integral)
133
println()
134
@test_nowarn println(solver.volume_integral)
135
@test_nowarn display(solver.volume_integral)
136
137
@test_nowarn show(semi)
138
println()
139
@test_nowarn println(semi)
140
@test_nowarn display(semi)
141
142
@test_nowarn show(summary_callback)
143
println()
144
@test_nowarn println(summary_callback)
145
@test_nowarn display(summary_callback)
146
147
@test_nowarn show(amr_controller)
148
println()
149
@test_nowarn println(amr_controller)
150
@test_nowarn display(amr_controller)
151
152
@test_nowarn show(amr_callback)
153
println()
154
@test_nowarn println(amr_callback)
155
@test_nowarn display(amr_callback)
156
157
@test_nowarn show(stepsize_callback)
158
println()
159
@test_nowarn println(stepsize_callback)
160
@test_nowarn display(stepsize_callback)
161
162
@test_nowarn show(save_solution)
163
println()
164
@test_nowarn println(save_solution)
165
@test_nowarn display(save_solution)
166
167
@test_nowarn show(analysis_callback)
168
println()
169
@test_nowarn println(analysis_callback)
170
@test_nowarn display(analysis_callback)
171
172
@test_nowarn show(alive_callback)
173
println()
174
@test_nowarn println(alive_callback)
175
@test_nowarn display(alive_callback)
176
177
@test_nowarn println(callbacks)
178
179
# Check whether all output is suppressed if the summary, analysis and alive
180
# callbacks are set to the TrivialCallback(). Modelled using `@test_nowarn`
181
# as basis.
182
let fname = tempname()
183
try
184
open(fname, "w") do f
185
redirect_stderr(f) do
186
trixi_include(joinpath(EXAMPLES_DIR,
187
"elixir_advection_extended.jl"),
188
visualization = TrivialCallback(),
189
summary_callback = TrivialCallback(),
190
analysis_callback = TrivialCallback(),
191
alive_callback = TrivialCallback())
192
end
193
end
194
output = read(fname, String)
195
output = replace(output,
196
"[ Info: You just called `trixi_include`. Julia may now compile the code, please be patient.\n" => "")
197
@test isempty(output)
198
finally
199
rm(fname, force = true)
200
end
201
end
202
end
203
204
@testset "Additional tests in 1D" begin
205
@testset "compressible Euler" begin
206
eqn = CompressibleEulerEquations1D(1.4)
207
208
@test isapprox(Trixi.entropy_thermodynamic([1.0, 2.0, 20.0], eqn),
209
1.9740810260220094)
210
@test isapprox(Trixi.entropy_math([1.0, 2.0, 20.0], eqn), -4.935202565055024)
211
@test isapprox(Trixi.entropy([1.0, 2.0, 20.0], eqn), -4.935202565055024)
212
213
@test isapprox(energy_total([1.0, 2.0, 20.0], eqn), 20.0)
214
@test isapprox(energy_kinetic([1.0, 2.0, 20.0], eqn), 2.0)
215
@test isapprox(energy_internal([1.0, 2.0, 20.0], eqn), 18.0)
216
end
217
end
218
219
@trixi_testset "Nonconservative terms in 1D (linear advection)" begin
220
# Same setup as docs/src/adding_new_equations/nonconservative_advection.md
221
222
# Define new physics
223
using Trixi
224
using Trixi: AbstractEquations, get_node_vars
225
226
# Since there is no native support for variable coefficients, we use two
227
# variables: one for the basic unknown `u` and another one for the coefficient `a`
228
struct NonconservativeLinearAdvectionEquation <: AbstractEquations{1, #= spatial dimension =#
229
2} #= two variables (u,a) =#
230
end
231
232
Trixi.varnames(::typeof(cons2cons), ::NonconservativeLinearAdvectionEquation) = ("scalar",
233
"advection_velocity")
234
235
Trixi.default_analysis_integrals(::NonconservativeLinearAdvectionEquation) = ()
236
237
# The conservative part of the flux is zero
238
Trixi.flux(u, orientation, equation::NonconservativeLinearAdvectionEquation) = zero(u)
239
240
# Calculate maximum wave speed for local Lax-Friedrichs-type dissipation
241
function Trixi.max_abs_speed_naive(u_ll, u_rr, orientation::Integer,
242
::NonconservativeLinearAdvectionEquation)
243
_, advection_velocity_ll = u_ll
244
_, advection_velocity_rr = u_rr
245
246
return max(abs(advection_velocity_ll), abs(advection_velocity_rr))
247
end
248
249
# We use nonconservative terms
250
Trixi.have_nonconservative_terms(::NonconservativeLinearAdvectionEquation) = Trixi.True()
251
252
function flux_nonconservative(u_mine, u_other, orientation,
253
equations::NonconservativeLinearAdvectionEquation)
254
_, advection_velocity = u_mine
255
scalar, _ = u_other
256
257
return SVector(advection_velocity * scalar, zero(scalar))
258
end
259
260
# Create a simulation setup
261
using Trixi
262
using OrdinaryDiffEqTsit5
263
264
equation = NonconservativeLinearAdvectionEquation()
265
266
# You can derive the exact solution for this setup using the method of
267
# characteristics
268
function initial_condition_sine(x, t,
269
equation::NonconservativeLinearAdvectionEquation)
270
x0 = -2 * atan(sqrt(3) * tan(sqrt(3) / 2 * t - atan(tan(x[1] / 2) / sqrt(3))))
271
scalar = sin(x0)
272
advection_velocity = 2 + cos(x[1])
273
SVector(scalar, advection_velocity)
274
end
275
276
# Create a uniform mesh in 1D in the interval [-π, π] with periodic boundaries
277
mesh = TreeMesh(-Float64(π), Float64(π), # min/max coordinates
278
initial_refinement_level = 4, n_cells_max = 10^4)
279
280
# Create a DGSEM solver with polynomials of degree `polydeg`
281
volume_flux = (flux_central, flux_nonconservative)
282
surface_flux = (FluxLaxFriedrichs(max_abs_speed_naive), flux_nonconservative)
283
solver = DGSEM(polydeg = 3, surface_flux = surface_flux,
284
volume_integral = VolumeIntegralFluxDifferencing(volume_flux))
285
286
# Setup the spatial semidiscretization containing all ingredients
287
semi = SemidiscretizationHyperbolic(mesh, equation, initial_condition_sine, solver)
288
289
# Create an ODE problem with given time span
290
tspan = (0.0, 1.0)
291
ode = semidiscretize(semi, tspan)
292
293
summary_callback = SummaryCallback()
294
analysis_callback = AnalysisCallback(semi, interval = 50)
295
callbacks = CallbackSet(summary_callback, analysis_callback)
296
297
# OrdinaryDiffEq's `solve` method evolves the solution in time and executes
298
# the passed callbacks
299
sol = solve(ode, Tsit5(), abstol = 1.0e-6, reltol = 1.0e-6;
300
ode_default_options()..., callback = callbacks)
301
302
@test analysis_callback(sol).l2 [0.00029609575838969394, 5.5681704039507985e-6]
303
end
304
305
# Clean up afterwards: delete Trixi.jl output directory
306
@test_nowarn rm(outdir, recursive = true)
307
end # TreeMesh1D
308
309
end # module
310
311