Path: blob/main/src/callbacks_step/steady_state.jl
5586 views
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs).1# Since these FMAs can increase the performance of many numerical algorithms,2# we need to opt-in explicitly.3# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details.4@muladd begin5#! format: noindent67"""8SteadyStateCallback(; abstol=1.0e-8, reltol=1.0e-6)910Terminates the integration when the [`residual_steady_state(du, equations)`](@ref)11falls below the threshold specified by `abstol, reltol`.12"""13struct SteadyStateCallback{RealT <: Real}14abstol::RealT15reltol::RealT16end1718function SteadyStateCallback(; abstol = 1.0e-8, reltol = 1.0e-6)19abstol, reltol = promote(abstol, reltol)20steady_state_callback = SteadyStateCallback(abstol, reltol)2122return DiscreteCallback(steady_state_callback, steady_state_callback,23save_positions = (false, false))24end2526function Base.show(io::IO, cb::DiscreteCallback{<:Any, <:SteadyStateCallback})27@nospecialize cb # reduce precompilation time2829steady_state_callback = cb.affect!30print(io, "SteadyStateCallback(abstol=", steady_state_callback.abstol, ", ",31"reltol=", steady_state_callback.reltol, ")")32return nothing33end3435function Base.show(io::IO, ::MIME"text/plain",36cb::DiscreteCallback{<:Any, <:SteadyStateCallback})37@nospecialize cb # reduce precompilation time3839if get(io, :compact, false)40show(io, cb)41else42steady_state_callback = cb.affect!4344setup = [45"absolute tolerance" => steady_state_callback.abstol,46"relative tolerance" => steady_state_callback.reltol47]48summary_box(io, "SteadyStateCallback", setup)49end50end5152# affect!53(::SteadyStateCallback)(integrator) = terminate!(integrator)5455# the condition56function (steady_state_callback::SteadyStateCallback)(u_ode, t, integrator)57semi = integrator.p5859u = wrap_array(u_ode, semi)60du = wrap_array(get_du(integrator), semi)61terminate = steady_state_callback(du, u, semi)62if mpi_isparallel()63# MPI.jl doesn't seem to have MPI_C_BOOL64terminate_integer = Int(terminate)65terminate = !iszero(MPI.Allreduce!(Ref(terminate_integer), +, mpi_comm())[])66end67if mpi_isroot() && terminate68@info " Steady state tolerance reached" steady_state_callback t69end70return terminate71end7273function (steady_state_callback::SteadyStateCallback)(du, u,74semi::AbstractSemidiscretization)75return steady_state_callback(du, u, mesh_equations_solver_cache(semi)...)76end7778include("steady_state_dg1d.jl")79include("steady_state_dg2d.jl")80include("steady_state_dg3d.jl")81end # @muladd828384