Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/src/callbacks_step/steady_state.jl
2055 views
1
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs).
2
# Since these FMAs can increase the performance of many numerical algorithms,
3
# we need to opt-in explicitly.
4
# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details.
5
@muladd begin
6
#! format: noindent
7
8
"""
9
SteadyStateCallback(; abstol=1.0e-8, reltol=1.0e-6)
10
11
Terminates the integration when the [`residual_steady_state(du, equations)`](@ref)
12
falls below the threshold specified by `abstol, reltol`.
13
"""
14
mutable struct SteadyStateCallback{RealT <: Real}
15
abstol::RealT
16
reltol::RealT
17
end
18
19
function SteadyStateCallback(; abstol = 1.0e-8, reltol = 1.0e-6)
20
abstol, reltol = promote(abstol, reltol)
21
steady_state_callback = SteadyStateCallback(abstol, reltol)
22
23
DiscreteCallback(steady_state_callback, steady_state_callback,
24
save_positions = (false, false))
25
end
26
27
function Base.show(io::IO, cb::DiscreteCallback{<:Any, <:SteadyStateCallback})
28
@nospecialize cb # reduce precompilation time
29
30
steady_state_callback = cb.affect!
31
print(io, "SteadyStateCallback(abstol=", steady_state_callback.abstol, ", ",
32
"reltol=", steady_state_callback.reltol, ")")
33
end
34
35
function Base.show(io::IO, ::MIME"text/plain",
36
cb::DiscreteCallback{<:Any, <:SteadyStateCallback})
37
@nospecialize cb # reduce precompilation time
38
39
if get(io, :compact, false)
40
show(io, cb)
41
else
42
steady_state_callback = cb.affect!
43
44
setup = [
45
"absolute tolerance" => steady_state_callback.abstol,
46
"relative tolerance" => steady_state_callback.reltol
47
]
48
summary_box(io, "SteadyStateCallback", setup)
49
end
50
end
51
52
# affect!
53
(::SteadyStateCallback)(integrator) = terminate!(integrator)
54
55
# the condition
56
function (steady_state_callback::SteadyStateCallback)(u_ode, t, integrator)
57
semi = integrator.p
58
59
u = wrap_array(u_ode, semi)
60
du = wrap_array(get_du(integrator), semi)
61
terminate = steady_state_callback(du, u, semi)
62
if mpi_isparallel()
63
# MPI.jl doesn't seem to have MPI_C_BOOL
64
terminate_integer = Int(terminate)
65
terminate = !iszero(MPI.Allreduce!(Ref(terminate_integer), +, mpi_comm())[])
66
end
67
if mpi_isroot() && terminate
68
@info " Steady state tolerance reached" steady_state_callback t
69
end
70
return terminate
71
end
72
73
function (steady_state_callback::SteadyStateCallback)(du, u,
74
semi::AbstractSemidiscretization)
75
steady_state_callback(du, u, mesh_equations_solver_cache(semi)...)
76
end
77
78
include("steady_state_dg1d.jl")
79
include("steady_state_dg2d.jl")
80
include("steady_state_dg3d.jl")
81
end # @muladd
82
83