Skip to content

Commit

Permalink
Generalize one-sided limiting (#125)
Browse files Browse the repository at this point in the history
* Start to align both entropy limiters

* Adapt calc_bounds_onesided!

* Add wrapper function for entropy limiting

* Rename keys in Dict

* use variable and bound_function as parameter

* Use same function for both entropies

* First working version of general onesided limiting

* Rename minmax limiting to twosided limiting

* Update comment

* Clean up default vector

* Last stuff

* Fix unit test

* fmt

* Fix tests

* Correct order

* Rework docstring

* Rename operator to min_or_max

* Call initial check with min_or_max

* fmt

* Implement suggestions

* Remove type stuff

* Fix allocations due to non-specialized routine
  • Loading branch information
bennibolm committed Mar 1, 2024
1 parent ba9439b commit 37b8117
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ surface_flux = flux_lax_friedrichs
volume_flux = flux_ranocha
basis = LobattoLegendreBasis(3)
limiter_idp = SubcellLimiterIDP(equations, basis;
local_minmax_variables_cons = ["rho"],
math_entropy = true)
local_twosided_variables_cons = ["rho"],
local_onesided_variables_nonlinear = [(Trixi.entropy_math,
max)])
volume_integral = VolumeIntegralSubcellLimiting(limiter_idp;
volume_flux_dg = volume_flux,
volume_flux_fv = surface_flux)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ surface_flux = flux_lax_friedrichs
volume_flux = flux_chandrashekar
basis = LobattoLegendreBasis(3)
limiter_idp = SubcellLimiterIDP(equations, basis;
local_minmax_variables_cons = ["rho"],
spec_entropy = true)
local_twosided_variables_cons = ["rho"],
local_onesided_variables_nonlinear = [(Trixi.entropy_spec,
min)])
volume_integral = VolumeIntegralSubcellLimiting(limiter_idp;
volume_flux_dg = volume_flux,
volume_flux_fv = surface_flux)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ volume_flux = flux_ranocha
basis = LobattoLegendreBasis(3)

limiter_idp = SubcellLimiterIDP(equations, basis;
local_minmax_variables_cons = ["rho" * string(i)
for i in eachcomponent(equations)])
local_twosided_variables_cons = ["rho" * string(i)
for i in eachcomponent(equations)])
volume_integral = VolumeIntegralSubcellLimiting(limiter_idp;
volume_flux_dg = volume_flux,
volume_flux_fv = surface_flux)
Expand Down
41 changes: 21 additions & 20 deletions src/callbacks_stage/subcell_bounds_check.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,27 @@ function init_callback(callback::BoundsCheckCallback, semi, limiter::SubcellLimi
return nothing
end

(; local_minmax, positivity, spec_entropy, math_entropy) = limiter
(; local_twosided, positivity, local_onesided) = limiter
(; output_directory) = callback
variables = varnames(cons2cons, semi.equations)

mkpath(output_directory)
open("$output_directory/deviations.txt", "a") do f
print(f, "# iter, simu_time")
if local_minmax
for v in limiter.local_minmax_variables_cons
if local_twosided
for v in limiter.local_twosided_variables_cons
variable_string = string(variables[v])
print(f, ", " * variable_string * "_min, " * variable_string * "_max")
end
end
if spec_entropy
print(f, ", specEntr_min")
end
if math_entropy
print(f, ", mathEntr_max")
if local_onesided
for (variable, min_or_max) in limiter.local_onesided_variables_nonlinear
print(f, ", " * string(variable) * "_" * string(min_or_max))
end
end
if positivity
for v in limiter.positivity_variables_cons
if v in limiter.local_minmax_variables_cons
if v in limiter.local_twosided_variables_cons
continue
end
print(f, ", " * string(variables[v]) * "_min")
Expand Down Expand Up @@ -126,15 +125,15 @@ end

@inline function finalize_callback(callback::BoundsCheckCallback, semi,
limiter::SubcellLimiterIDP)
(; local_minmax, positivity, spec_entropy, math_entropy) = limiter
(; local_twosided, positivity, local_onesided) = limiter
(; idp_bounds_delta_global) = limiter.cache
variables = varnames(cons2cons, semi.equations)

println(""^100)
println("Maximum deviation from bounds:")
println(""^100)
if local_minmax
for v in limiter.local_minmax_variables_cons
if local_twosided
for v in limiter.local_twosided_variables_cons
v_string = string(v)
println("$(variables[v]):")
println("- lower bound: ",
Expand All @@ -143,17 +142,19 @@ end
idp_bounds_delta_global[Symbol(v_string, "_max")])
end
end
if spec_entropy
println("spec. entropy:\n- lower bound: ",
idp_bounds_delta_global[:spec_entropy_min])
end
if math_entropy
println("math. entropy:\n- upper bound: ",
idp_bounds_delta_global[:math_entropy_max])
if local_onesided
for (variable, min_or_max) in limiter.local_onesided_variables_nonlinear
variable_string = string(variable)
minmax_string = string(min_or_max)
println("$variable_string:")
println("- $minmax_string bound: ",
idp_bounds_delta_global[Symbol(variable_string, "_",
minmax_string)])
end
end
if positivity
for v in limiter.positivity_variables_cons
if v in limiter.local_minmax_variables_cons
if v in limiter.local_twosided_variables_cons
continue
end
println(string(variables[v]) * ":\n- positivity: ",
Expand Down
64 changes: 30 additions & 34 deletions src/callbacks_stage/subcell_bounds_check_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@inline function check_bounds(u, mesh::AbstractMesh{2}, equations, solver, cache,
limiter::SubcellLimiterIDP,
time, iter, output_directory, save_errors)
(; local_minmax, positivity, spec_entropy, math_entropy) = solver.volume_integral.limiter
(; local_twosided, positivity, local_onesided) = solver.volume_integral.limiter
(; variable_bounds) = limiter.cache.subcell_limiter_coefficients
(; idp_bounds_delta_local, idp_bounds_delta_global) = limiter.cache

Expand All @@ -21,8 +21,8 @@
# Since there are no processors with caches over 128B, we use `n = 128B / size(uEltype)`
stride_size = div(128, sizeof(eltype(u))) # = n

if local_minmax
for v in limiter.local_minmax_variables_cons
if local_twosided
for v in limiter.local_twosided_variables_cons
v_string = string(v)
key_min = Symbol(v_string, "_min")
key_max = Symbol(v_string, "_max")
Expand All @@ -43,35 +43,30 @@
end
end
end
if spec_entropy
key = :spec_entropy_min
deviation_threaded = idp_bounds_delta_local[key]
@threaded for element in eachelement(solver, cache)
deviation = deviation_threaded[stride_size * Threads.threadid()]
for j in eachnode(solver), i in eachnode(solver)
s = entropy_spec(get_node_vars(u, equations, solver, i, j, element),
equations)
deviation = max(deviation, variable_bounds[key][i, j, element] - s)
end
deviation_threaded[stride_size * Threads.threadid()] = deviation
end
end
if math_entropy
key = :math_entropy_max
deviation_threaded = idp_bounds_delta_local[key]
@threaded for element in eachelement(solver, cache)
deviation = deviation_threaded[stride_size * Threads.threadid()]
for j in eachnode(solver), i in eachnode(solver)
s = entropy_math(get_node_vars(u, equations, solver, i, j, element),
if local_onesided
foreach(limiter.local_onesided_variables_nonlinear) do (variable, min_or_max)
key = Symbol(string(variable), "_", string(min_or_max))
deviation_threaded = idp_bounds_delta_local[key]
@threaded for element in eachelement(solver, cache)
deviation = deviation_threaded[stride_size * Threads.threadid()]
for j in eachnode(solver), i in eachnode(solver)
v = variable(get_node_vars(u, equations, solver, i, j, element),
equations)
deviation = max(deviation, s - variable_bounds[key][i, j, element])
if min_or_max === max
deviation = max(deviation,
v - variable_bounds[key][i, j, element])
else # min_or_max === min
deviation = max(deviation,
variable_bounds[key][i, j, element] - v)
end
end
deviation_threaded[stride_size * Threads.threadid()] = deviation
end
deviation_threaded[stride_size * Threads.threadid()] = deviation
end
end
if positivity
for v in limiter.positivity_variables_cons
if v in limiter.local_minmax_variables_cons
if v in limiter.local_twosided_variables_cons
continue
end
key = Symbol(string(v), "_min")
Expand Down Expand Up @@ -115,24 +110,25 @@
# Print to output file
open("$output_directory/deviations.txt", "a") do f
print(f, iter, ", ", time)
if local_minmax
for v in limiter.local_minmax_variables_cons
if local_twosided
for v in limiter.local_twosided_variables_cons
v_string = string(v)
print(f, ", ",
idp_bounds_delta_local[Symbol(v_string, "_min")][stride_size],
", ",
idp_bounds_delta_local[Symbol(v_string, "_max")][stride_size])
end
end
if spec_entropy
print(f, ", ", idp_bounds_delta_local[:spec_entropy_min][stride_size])
end
if math_entropy
print(f, ", ", idp_bounds_delta_local[:math_entropy_max][stride_size])
if local_onesided
for (variable, min_or_max) in limiter.local_onesided_variables_nonlinear
print(f, ", ",
idp_bounds_delta_local[Symbol(string(variable), "_",
string(min_or_max))][stride_size])
end
end
if positivity
for v in limiter.positivity_variables_cons
if v in limiter.local_minmax_variables_cons
if v in limiter.local_twosided_variables_cons
continue
end
print(f, ", ",
Expand Down
Loading

0 comments on commit 37b8117

Please sign in to comment.