-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs). | ||
# Since these FMAs can increase the performance of many numerical algorithms, | ||
# we need to opt-in explicitly. | ||
# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details. | ||
@muladd begin | ||
#! format: noindent | ||
|
||
""" | ||
BoundsCheckCallback(; output_directory="out", save_errors=false, interval=1) | ||
Bounds checking routine for [`SubcellLimiterIDP`](@ref). Applied as a stage callback for SSPRK | ||
methods. If `save_errors` is `true`, the resulting deviations are saved in | ||
`output_directory/deviations.txt` for every `interval` time steps. | ||
""" | ||
struct BoundsCheckCallback | ||
output_directory::String | ||
save_errors::Bool | ||
interval::Int | ||
end | ||
|
||
function BoundsCheckCallback(; output_directory = "out", save_errors = false, | ||
interval = 1) | ||
BoundsCheckCallback(output_directory, save_errors, interval) | ||
end | ||
|
||
function (callback::BoundsCheckCallback)(u_ode, integrator, stage) | ||
mesh, equations, solver, cache = mesh_equations_solver_cache(integrator.p) | ||
(; t, iter, alg) = integrator | ||
u = wrap_array(u_ode, mesh, equations, solver, cache) | ||
|
||
save_errors_ = callback.save_errors && (callback.interval > 0) && | ||
(stage == length(alg.c)) | ||
@trixi_timeit timer() "check_bounds" check_bounds(u, mesh, equations, solver, cache, | ||
t, iter + 1, | ||
callback.output_directory, | ||
save_errors_, callback.interval) | ||
end | ||
|
||
function check_bounds(u, mesh, equations, solver, cache, t, iter, output_directory, | ||
save_errors, interval) | ||
check_bounds(u, mesh, equations, solver, cache, solver.volume_integral, t, iter, | ||
output_directory, save_errors, interval) | ||
end | ||
|
||
function check_bounds(u, mesh, equations, solver, cache, | ||
volume_integral::AbstractVolumeIntegral, | ||
t, iter, output_directory, save_errors, interval) | ||
return nothing | ||
end | ||
|
||
function check_bounds(u, mesh, equations, solver, cache, | ||
volume_integral::VolumeIntegralSubcellLimiting, | ||
t, iter, output_directory, save_errors, interval) | ||
check_bounds(u, mesh, equations, solver, cache, volume_integral.limiter, t, iter, | ||
output_directory, save_errors, interval) | ||
end | ||
|
||
function init_callback(callback, semi) | ||
init_callback(callback, semi, semi.solver.volume_integral) | ||
end | ||
|
||
init_callback(callback, semi, volume_integral::AbstractVolumeIntegral) = nothing | ||
|
||
function init_callback(callback, semi, volume_integral::VolumeIntegralSubcellLimiting) | ||
init_callback(callback, semi, volume_integral.limiter) | ||
end | ||
|
||
function init_callback(callback::BoundsCheckCallback, semi, limiter::SubcellLimiterIDP) | ||
if !callback.save_errors || (callback.interval == 0) | ||
return nothing | ||
end | ||
|
||
(; positivity) = 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 positivity | ||
for index in limiter.positivity_variables_cons | ||
print(f, ", $(variables[index])_min") | ||
end | ||
end | ||
println(f) | ||
end | ||
|
||
return nothing | ||
end | ||
|
||
function finalize_callback(callback, semi) | ||
finalize_callback(callback, semi, semi.solver.volume_integral) | ||
end | ||
|
||
finalize_callback(callback, semi, volume_integral::AbstractVolumeIntegral) = nothing | ||
|
||
function finalize_callback(callback, semi, | ||
volume_integral::VolumeIntegralSubcellLimiting) | ||
finalize_callback(callback, semi, volume_integral.limiter) | ||
end | ||
|
||
@inline function finalize_callback(callback::BoundsCheckCallback, semi, | ||
limiter::SubcellLimiterIDP) | ||
(; positivity) = limiter | ||
(; idp_bounds_delta) = limiter.cache | ||
variables = varnames(cons2cons, semi.equations) | ||
|
||
println("─"^100) | ||
println("Maximum deviation from bounds:") | ||
println("─"^100) | ||
if positivity | ||
for v in limiter.positivity_variables_cons | ||
println("$(variables[v]):\n- positivity: ", | ||
idp_bounds_delta[Symbol("$(v)_min")]) | ||
end | ||
end | ||
println("─"^100 * "\n") | ||
|
||
return nothing | ||
end | ||
|
||
include("subcell_bounds_check_2d.jl") | ||
end # @muladd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs). | ||
# Since these FMAs can increase the performance of many numerical algorithms, | ||
# we need to opt-in explicitly. | ||
# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details. | ||
@muladd begin | ||
#! format: noindent | ||
|
||
@inline function check_bounds(u, mesh::AbstractMesh{2}, equations, solver, cache, | ||
limiter::SubcellLimiterIDP, | ||
time, iter, output_directory, save_errors, interval) | ||
(; positivity) = solver.volume_integral.limiter | ||
(; variable_bounds) = limiter.cache.subcell_limiter_coefficients | ||
(; idp_bounds_delta) = limiter.cache | ||
|
||
save_errors_ = save_errors && (iter % interval == 0) | ||
if save_errors_ | ||
open("$output_directory/deviations.txt", "a") do f | ||
print(f, iter, ", ", time) | ||
end | ||
end | ||
if positivity | ||
for v in limiter.positivity_variables_cons | ||
key = Symbol("$(v)_min") | ||
deviation_min = zero(eltype(u)) | ||
for element in eachelement(solver, cache), j in eachnode(solver), | ||
i in eachnode(solver) | ||
|
||
var = u[v, i, j, element] | ||
deviation_min = max(deviation_min, | ||
variable_bounds[key][i, j, element] - var) | ||
end | ||
idp_bounds_delta[key] = max(idp_bounds_delta[key], deviation_min) | ||
if save_errors_ | ||
deviation_min_ = deviation_min | ||
open("$output_directory/deviations.txt", "a") do f | ||
print(f, ", ", deviation_min_) | ||
end | ||
end | ||
end | ||
end | ||
if save_errors_ | ||
open("$output_directory/deviations.txt", "a") do f | ||
println(f) | ||
end | ||
end | ||
|
||
return nothing | ||
end | ||
end # @muladd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters