Skip to content

Commit

Permalink
Merge branch 'main' into subcell-limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
bennibolm committed Nov 20, 2023
2 parents af19dbf + 1635d31 commit ad64cd7
Show file tree
Hide file tree
Showing 26 changed files with 468 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Trixi"
uuid = "a7f1ee26-1774-49b1-8366-f1abc58fbfcb"
authors = ["Michael Schlottke-Lakemper <[email protected]>", "Gregor Gassner <[email protected]>", "Hendrik Ranocha <[email protected]>", "Andrew R. Winters <[email protected]>", "Jesse Chan <[email protected]>"]
version = "0.6.1-pre"
version = "0.6.2-pre"

[deps]
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
Expand Down
2 changes: 1 addition & 1 deletion examples/p4est_2d_dgsem/elixir_advection_restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ save_solution.condition.save_initial_solution = false

integrator = init(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = dt, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);
save_everystep = false, callback = callbacks, maxiters = 100_000);

# Get the last time index and work with that.
load_timestep!(integrator, restart_filename)
Expand Down
2 changes: 1 addition & 1 deletion examples/p4est_3d_dgsem/elixir_advection_restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ save_solution.condition.save_initial_solution = false

integrator = init(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = dt, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);
save_everystep = false, callback = callbacks, maxiters = 100_000);

# Get the last time index and work with that.
load_timestep!(integrator, restart_filename)
Expand Down
2 changes: 1 addition & 1 deletion examples/structured_2d_dgsem/elixir_advection_restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ save_solution.condition.save_initial_solution = false

integrator = init(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = dt, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);
save_everystep = false, callback = callbacks, maxiters = 100_000);

# Get the last time index and work with that.
load_timestep!(integrator, restart_filename)
Expand Down
2 changes: 1 addition & 1 deletion examples/structured_3d_dgsem/elixir_advection_restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ save_solution.condition.save_initial_solution = false

integrator = init(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = dt, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);
save_everystep = false, callback = callbacks, maxiters = 100_000);

# Get the last time index and work with that.
load_timestep!(integrator, restart_filename)
Expand Down
2 changes: 1 addition & 1 deletion examples/tree_2d_dgsem/elixir_advection_restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ save_solution.condition.save_initial_solution = false

integrator = init(ode, alg,
dt = dt, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks; ode_default_options()...)
callback = callbacks, maxiters = 100_000; ode_default_options()...)

# Load saved context for adaptive time integrator
if integrator.opts.adaptive
Expand Down
63 changes: 63 additions & 0 deletions examples/tree_2d_dgsem/elixir_eulerpolytropic_convergence.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

using OrdinaryDiffEq
using Trixi

###############################################################################
# semidiscretization of the polytropic Euler equations

gamma = 1.4
kappa = 0.5 # Scaling factor for the pressure.
equations = PolytropicEulerEquations2D(gamma, kappa)

initial_condition = initial_condition_convergence_test

volume_flux = flux_winters_etal
solver = DGSEM(polydeg = 3, surface_flux = FluxHLL(min_max_speed_davis),
volume_integral = VolumeIntegralFluxDifferencing(volume_flux))

coordinates_min = (0.0, 0.0)
coordinates_max = (1.0, 1.0)

# Create a uniformly refined mesh with periodic boundaries
mesh = TreeMesh(coordinates_min, coordinates_max,
initial_refinement_level = 2,
periodicity = true,
n_cells_max = 30_000)

semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
source_terms = source_terms_convergence_test)

###############################################################################
# ODE solvers, callbacks etc.

tspan = (0.0, 0.1)
ode = semidiscretize(semi, tspan)

summary_callback = SummaryCallback()

analysis_interval = 100
analysis_callback = AnalysisCallback(semi, interval = analysis_interval,
extra_analysis_errors = (:l2_error_primitive,
:linf_error_primitive))

alive_callback = AliveCallback(analysis_interval = analysis_interval)

save_solution = SaveSolutionCallback(interval = 100,
save_initial_solution = true,
save_final_solution = true,
solution_variables = cons2prim)

stepsize_callback = StepsizeCallback(cfl = 0.1)

callbacks = CallbackSet(summary_callback,
analysis_callback, alive_callback,
save_solution,
stepsize_callback)

###############################################################################
# run the simulation

sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);
summary_callback() # print the timer summary
2 changes: 1 addition & 1 deletion examples/tree_3d_dgsem/elixir_advection_restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ save_solution.condition.save_initial_solution = false

integrator = init(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = dt, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);
save_everystep = false, callback = callbacks, maxiters = 100_000);

# Get the last time index and work with that.
load_timestep!(integrator, restart_filename)
Expand Down
2 changes: 1 addition & 1 deletion examples/unstructured_2d_dgsem/elixir_euler_restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ save_solution.condition.save_initial_solution = false

integrator = init(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = dt, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);
save_everystep = false, callback = callbacks, maxiters = 100_000);

# Get the last time index and work with that.
load_timestep!(integrator, restart_filename)
Expand Down
45 changes: 36 additions & 9 deletions src/auxiliary/special_elixirs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ julia> redirect_stdout(devnull) do
```
"""
function trixi_include(mod::Module, elixir::AbstractString; kwargs...)
# Check that all kwargs exist as assignments
code = read(elixir, String)
expr = Meta.parse("begin \n$code \nend")
expr = insert_maxiters(expr)

for (key, val) in kwargs
# This will throw an error when `key` is not found
find_assignment(expr, key)
end

# Print information on potential wait time only in non-parallel case
if !mpi_isparallel()
@info "You just called `trixi_include`. Julia may now compile the code, please be patient."
Expand Down Expand Up @@ -243,19 +253,25 @@ end
function find_assignment(expr, destination)
# declare result to be able to assign to it in the closure
local result
found = false

# find explicit and keyword assignments
walkexpr(expr) do x
if x isa Expr
if (x.head === Symbol("=") || x.head === :kw) &&
x.args[1] === Symbol(destination)
result = x.args[2]
found = true
# dump(x)
end
end
return x
end

if !found
throw(ArgumentError("assignment `$destination` not found in expression"))
end

result
end

Expand All @@ -274,17 +290,28 @@ function extract_initial_resolution(elixir, kwargs)
return initial_refinement_level
end
catch e
if isa(e, UndefVarError)
# get cells_per_dimension from the elixir
cells_per_dimension = eval(find_assignment(expr, :cells_per_dimension))

if haskey(kwargs, :cells_per_dimension)
return kwargs[:cells_per_dimension]
else
return cells_per_dimension
# If `initial_refinement_level` is not found, we will get an `ArgumentError`
if isa(e, ArgumentError)
try
# get cells_per_dimension from the elixir
cells_per_dimension = eval(find_assignment(expr, :cells_per_dimension))

if haskey(kwargs, :cells_per_dimension)
return kwargs[:cells_per_dimension]
else
return cells_per_dimension
end
catch e2
# If `cells_per_dimension` is not found either
if isa(e2, ArgumentError)
throw(ArgumentError("`convergence_test` requires the elixir to define " *
"`initial_refinement_level` or `cells_per_dimension`"))
else
rethrow()
end
end
else
throw(e)
rethrow()
end
end
end
Expand Down
102 changes: 100 additions & 2 deletions src/equations/polytropic_euler_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function initial_condition_weak_blast_wave(x, t, equations::PolytropicEulerEquat
return prim2cons(SVector(rho, v1, v2), equations)
end

# Calculate 1D flux for a single point in the normal direction
# Calculate 2D flux for a single point in the normal direction
# Note, this directional vector is not normalized
@inline function flux(u, normal_direction::AbstractVector,
equations::PolytropicEulerEquations2D)
Expand All @@ -135,8 +135,28 @@ end
return SVector(f1, f2, f3)
end

# Calculate 2D flux for a single point
@inline function flux(u, orientation::Integer, equations::PolytropicEulerEquations2D)
_, v1, v2 = cons2prim(u, equations)
p = pressure(u, equations)

rho_v1 = u[2]
rho_v2 = u[3]

if orientation == 1
f1 = rho_v1
f2 = rho_v1 * v1 + p
f3 = rho_v1 * v2
else
f1 = rho_v2
f2 = rho_v2 * v1
f3 = rho_v2 * v2 + p
end
return SVector(f1, f2, f3)
end

"""
flux_winters_etal(u_ll, u_rr, normal_direction,
flux_winters_etal(u_ll, u_rr, orientation_or_normal_direction,
equations::PolytropicEulerEquations2D)
Entropy conserving two-point flux for isothermal or polytropic gases.
Expand Down Expand Up @@ -178,6 +198,37 @@ For details see Section 3.2 of the following reference
return SVector(f1, f2, f3)
end

@inline function flux_winters_etal(u_ll, u_rr, orientation::Integer,
equations::PolytropicEulerEquations2D)
# Unpack left and right state
rho_ll, v1_ll, v2_ll = cons2prim(u_ll, equations)
rho_rr, v1_rr, v2_rr = cons2prim(u_rr, equations)
p_ll = equations.kappa * rho_ll^equations.gamma
p_rr = equations.kappa * rho_rr^equations.gamma

# Compute the necessary mean values
if equations.gamma == 1.0 # isothermal gas
rho_mean = ln_mean(rho_ll, rho_rr)
else # equations.gamma > 1 # polytropic gas
rho_mean = stolarsky_mean(rho_ll, rho_rr, equations.gamma)
end
v1_avg = 0.5 * (v1_ll + v1_rr)
v2_avg = 0.5 * (v2_ll + v2_rr)
p_avg = 0.5 * (p_ll + p_rr)

if orientation == 1 # x-direction
f1 = rho_mean * 0.5 * (v1_ll + v1_rr)
f2 = f1 * v1_avg + p_avg
f3 = f1 * v2_avg
else # y-direction
f1 = rho_mean * 0.5 * (v2_ll + v2_rr)
f2 = f1 * v1_avg
f3 = f1 * v2_avg + p_avg
end

return SVector(f1, f2, f3)
end

@inline function min_max_speed_naive(u_ll, u_rr, normal_direction::AbstractVector,
equations::PolytropicEulerEquations2D)
rho_ll, v1_ll, v2_ll = cons2prim(u_ll, equations)
Expand All @@ -196,6 +247,53 @@ end
return lambda_min, lambda_max
end

# More refined estimates for minimum and maximum wave speeds for HLL-type fluxes
@inline function min_max_speed_davis(u_ll, u_rr, orientation::Integer,
equations::PolytropicEulerEquations2D)
rho_ll, v1_ll, v2_ll = cons2prim(u_ll, equations)
rho_rr, v1_rr, v2_rr = cons2prim(u_rr, equations)
# Pressure for polytropic Euler
p_ll = equations.kappa * rho_ll^equations.gamma
p_rr = equations.kappa * rho_rr^equations.gamma

c_ll = sqrt(equations.gamma * p_ll / rho_ll)
c_rr = sqrt(equations.gamma * p_rr / rho_rr)

if orientation == 1 # x-direction
λ_min = min(v1_ll - c_ll, v1_rr - c_rr)
λ_max = max(v1_ll + c_ll, v1_rr + c_rr)
else # y-direction
λ_min = min(v2_ll - c_ll, v2_rr - c_rr)
λ_max = max(v2_ll + c_ll, v2_rr + c_rr)
end

return λ_min, λ_max
end

# More refined estimates for minimum and maximum wave speeds for HLL-type fluxes
@inline function min_max_speed_davis(u_ll, u_rr, normal_direction::AbstractVector,
equations::PolytropicEulerEquations2D)
rho_ll, v1_ll, v2_ll = cons2prim(u_ll, equations)
rho_rr, v1_rr, v2_rr = cons2prim(u_rr, equations)
# Pressure for polytropic Euler
p_ll = equations.kappa * rho_ll^equations.gamma
p_rr = equations.kappa * rho_rr^equations.gamma

norm_ = norm(normal_direction)

c_ll = sqrt(equations.gamma * p_ll / rho_ll) * norm_
c_rr = sqrt(equations.gamma * p_rr / rho_rr) * norm_

v_normal_ll = v1_ll * normal_direction[1] + v2_ll * normal_direction[2]
v_normal_rr = v1_rr * normal_direction[1] + v2_rr * normal_direction[2]

# The v_normals are already scaled by the norm
λ_min = min(v_normal_ll - c_ll, v_normal_rr - c_rr)
λ_max = max(v_normal_ll + c_ll, v_normal_rr + c_rr)

return λ_min, λ_max
end

@inline function max_abs_speeds(u, equations::PolytropicEulerEquations2D)
rho, v1, v2 = cons2prim(u, equations)
c = sqrt(equations.gamma * equations.kappa * rho^(equations.gamma - 1))
Expand Down
5 changes: 4 additions & 1 deletion test/test_mpi_p4est_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ const EXAMPLES_DIR = pkgdir(Trixi, "examples", "p4est_2d_dgsem")
@trixi_testset "elixir_advection_restart.jl" begin
@test_trixi_include(joinpath(EXAMPLES_DIR, "elixir_advection_restart.jl"),
l2=[4.507575525876275e-6],
linf=[6.21489667023134e-5])
linf=[6.21489667023134e-5],
# With the default `maxiters = 1` in coverage tests,
# there would be no time steps after the restart.
coverage_override=(maxiters = 100_000,))
end

@trixi_testset "elixir_euler_source_terms_nonconforming_unstructured_flag.jl" begin
Expand Down
5 changes: 4 additions & 1 deletion test/test_mpi_p4est_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ const EXAMPLES_DIR = pkgdir(Trixi, "examples", "p4est_3d_dgsem")
@trixi_testset "elixir_advection_restart.jl" begin
@test_trixi_include(joinpath(EXAMPLES_DIR, "elixir_advection_restart.jl"),
l2=[0.002590388934758452],
linf=[0.01840757696885409])
linf=[0.01840757696885409],
# With the default `maxiters = 1` in coverage tests,
# there would be no time steps after the restart.
coverage_override=(maxiters = 100_000,))
end

@trixi_testset "elixir_advection_cubed_sphere.jl" begin
Expand Down
9 changes: 6 additions & 3 deletions test/test_p4est_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ end
@trixi_testset "elixir_advection_restart.jl" begin
@test_trixi_include(joinpath(EXAMPLES_DIR, "elixir_advection_restart.jl"),
l2=[4.507575525876275e-6],
linf=[6.21489667023134e-5])
linf=[6.21489667023134e-5],
# With the default `maxiters = 1` in coverage tests,
# there would be no time steps after the restart.
coverage_override=(maxiters = 100_000,))
# Ensure that we do not have excessive memory allocations
# (e.g., from type instabilities)
let
Expand Down Expand Up @@ -216,8 +219,8 @@ end
],
surface_flux=flux_hlle,
tspan=(0.0, 0.3))
# Ensure that we do not have excessive memory allocations
# (e.g., from type instabilities)
# Ensure that we do not have excessive memory allocations
# (e.g., from type instabilities)
let
t = sol.t[end]
u_ode = sol.u[end]
Expand Down
Loading

0 comments on commit ad64cd7

Please sign in to comment.