Path: blob/main/src/callbacks_step/steady_state.jl
2055 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"""13mutable struct 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)2122DiscreteCallback(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, ")")32end3334function Base.show(io::IO, ::MIME"text/plain",35cb::DiscreteCallback{<:Any, <:SteadyStateCallback})36@nospecialize cb # reduce precompilation time3738if get(io, :compact, false)39show(io, cb)40else41steady_state_callback = cb.affect!4243setup = [44"absolute tolerance" => steady_state_callback.abstol,45"relative tolerance" => steady_state_callback.reltol46]47summary_box(io, "SteadyStateCallback", setup)48end49end5051# affect!52(::SteadyStateCallback)(integrator) = terminate!(integrator)5354# the condition55function (steady_state_callback::SteadyStateCallback)(u_ode, t, integrator)56semi = integrator.p5758u = wrap_array(u_ode, semi)59du = wrap_array(get_du(integrator), semi)60terminate = steady_state_callback(du, u, semi)61if mpi_isparallel()62# MPI.jl doesn't seem to have MPI_C_BOOL63terminate_integer = Int(terminate)64terminate = !iszero(MPI.Allreduce!(Ref(terminate_integer), +, mpi_comm())[])65end66if mpi_isroot() && terminate67@info " Steady state tolerance reached" steady_state_callback t68end69return terminate70end7172function (steady_state_callback::SteadyStateCallback)(du, u,73semi::AbstractSemidiscretization)74steady_state_callback(du, u, mesh_equations_solver_cache(semi)...)75end7677include("steady_state_dg1d.jl")78include("steady_state_dg2d.jl")79include("steady_state_dg3d.jl")80end # @muladd818283