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