-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Steady state callback #601
Open
LasNikas
wants to merge
13
commits into
trixi-framework:main
Choose a base branch
from
LasNikas:steady-state-callback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a475e82
add `SteadyStateCallback`
LasNikas 4db105e
Merge branch 'main' into steady-state-callback
LasNikas bfc3bca
add write csv
LasNikas ecb23be
Merge branch 'main' into steady-state-callback
LasNikas 6a8e7e7
add periodic cb
LasNikas 8543619
save size in pp_callback
LasNikas f75a858
fix
LasNikas f3fe009
undo changes pp_callback
LasNikas c121c37
periodic callback support
LasNikas a1d69d6
implement suggestions
LasNikas 0871515
Merge branch 'main' into steady-state-callback
LasNikas 941a758
implement suggestions
LasNikas bc9933f
terminate simulation with setting `maxiters` to `iter`
LasNikas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
""" | ||
SteadyStateCallback(; interval::Integer=0, dt=0.0, interval_size::Integer=10, | ||
abstol=1.0e-8, reltol=1.0e-6) | ||
|
||
Terminates the integration when the residual of the change in kinetic energy | ||
falls below the threshold specified by `abstol, reltol`. | ||
|
||
# Keywords | ||
- `interval=0`: Check steady state condition every `interval` time steps. | ||
- `dt=0.0`: Check steady state condition in regular intervals of `dt` in terms | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
of integration time by adding additional `tstops`. | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `interval_size`: The interval in which the change of the kinetic energy is considered. | ||
`interval_size` is a (integer) multiple of `interval` or `dt`. | ||
- `abstol`: Absolute tolerance. | ||
- `reltol`: Relative tolerance. | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
mutable struct SteadyStateCallback{I, RealT <: Real} | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
interval :: I | ||
abstol :: RealT | ||
reltol :: RealT | ||
previous_ekin :: Vector{Float64} | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
interval_size :: Int | ||
end | ||
|
||
function SteadyStateCallback(; interval::Integer=0, dt=0.0, interval_size::Integer=10, | ||
abstol=1.0e-8, reltol=1.0e-6) | ||
abstol, reltol = promote(abstol, reltol) | ||
|
||
if dt > 0 && interval > 0 | ||
throw(ArgumentError("setting both `interval` and `dt` is not supported")) | ||
end | ||
|
||
if dt > 0 | ||
interval = Float64(dt) | ||
end | ||
|
||
steady_state_callback = SteadyStateCallback(interval, abstol, reltol, [Inf64], | ||
interval_size) | ||
|
||
if dt > 0 | ||
return PeriodicCallback(steady_state_callback, dt, save_positions=(false, false), | ||
final_affect=true) | ||
else | ||
return DiscreteCallback(steady_state_callback, steady_state_callback, | ||
save_positions=(false, false)) | ||
end | ||
end | ||
|
||
function Base.show(io::IO, cb::DiscreteCallback{<:Any, <:SteadyStateCallback}) | ||
@nospecialize cb # reduce precompilation time | ||
|
||
cb_ = cb.affect! | ||
|
||
print(io, "SteadyStateCallback(abstol=", cb_.abstol, ", ", "reltol=", cb_.reltol, ")") | ||
end | ||
|
||
function Base.show(io::IO, | ||
cb::DiscreteCallback{<:Any, | ||
<:PeriodicCallbackAffect{<:SteadyStateCallback}}) | ||
@nospecialize cb # reduce precompilation time | ||
|
||
cb_ = cb.affect!.affect! | ||
|
||
print(io, "SteadyStateCallback(abstol=", cb_.abstol, ", reltol=", cb_.reltol, ")") | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", | ||
cb::DiscreteCallback{<:Any, <:SteadyStateCallback}) | ||
@nospecialize cb # reduce precompilation time | ||
|
||
if get(io, :compact, false) | ||
show(io, cb) | ||
else | ||
cb_ = cb.affect! | ||
|
||
setup = ["absolute tolerance" => cb_.abstol, | ||
"relative tolerance" => cb_.reltol, | ||
"interval" => cb_.interval, | ||
"interval size" => cb_.interval_size] | ||
summary_box(io, "SteadyStateCallback", setup) | ||
end | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", | ||
cb::DiscreteCallback{<:Any, | ||
<:PeriodicCallbackAffect{<:SteadyStateCallback}}) | ||
@nospecialize cb # reduce precompilation time | ||
|
||
if get(io, :compact, false) | ||
show(io, cb) | ||
else | ||
cb_ = cb.affect!.affect! | ||
|
||
setup = ["absolute tolerance" => cb_.abstol, | ||
"relative tolerance" => cb_.reltol, | ||
"interval" => cb_.interval, | ||
"interval_size" => cb_.interval_size] | ||
summary_box(io, "SteadyStateCallback", setup) | ||
end | ||
end | ||
|
||
# `affect!` (`PeriodicCallback`) | ||
function (cb::SteadyStateCallback)(integrator) | ||
steady_state_condition(cb, integrator) || return nothing | ||
|
||
print_summary(integrator) | ||
|
||
terminate!(integrator) | ||
end | ||
|
||
# `affect!` (`DiscreteCallback`) | ||
function (cb::SteadyStateCallback{Int})(integrator) | ||
print_summary(integrator) | ||
|
||
terminate!(integrator) | ||
end | ||
|
||
# `condition` (`DiscreteCallback`) | ||
function (steady_state_callback::SteadyStateCallback)(vu_ode, t, integrator) | ||
return steady_state_condition(steady_state_callback, integrator) | ||
end | ||
|
||
@inline function steady_state_condition(cb, integrator) | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(; abstol, reltol, previous_ekin, interval_size) = cb | ||
|
||
vu_ode = integrator.u | ||
v_ode, u_ode = vu_ode.x | ||
semi = integrator.p | ||
|
||
# Calculate kinetic energy | ||
ekin = 0.0 | ||
foreach_system(semi) do system | ||
v = wrap_v(v_ode, system, semi) | ||
|
||
# Calculate kintetic energy | ||
for particle in each_moving_particle(system) | ||
velocity = current_velocity(v, system, particle) | ||
ekin += 0.5 * system.mass[particle] * dot(velocity, velocity) | ||
end | ||
end | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
push!(previous_ekin, ekin) | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
terminate = false | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if interval_size_condition(cb, integrator) | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
popfirst!(previous_ekin) | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Calculate MSE only over the `interval_size` | ||
mse = 0.0 | ||
for index in 1:interval_size | ||
mse += (previous_ekin[index] - ekin)^2 | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
mse /= interval_size | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
threshold = abstol + reltol * ekin | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
terminate = mse <= threshold | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return terminate | ||
LasNikas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
# `DiscreteCallback` | ||
@inline function interval_size_condition(cb::SteadyStateCallback{Int}, integrator) | ||
return integrator.stats.naccept > 0 && | ||
round(integrator.stats.naccept / cb.interval) > cb.interval_size | ||
end | ||
|
||
# `PeriodicCallback` | ||
@inline function interval_size_condition(cb::SteadyStateCallback, integrator) | ||
return integrator.stats.naccept > 0 && | ||
round(Int, integrator.t / cb.interval) > cb.interval_size | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the name a bit misleading. In grid-based methods, when simulating a physical instability arising from a slightly perturbed steady state, you sometimes subtract the right-hand side of the steady state to remove errors.
From this name, I would expect something like this here.
I'll think about other naming suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConvergenceConditionCallBack
ConvergenceTerminationCallBack
TerminationConditionCallBack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SteadyStateDetector
SteadyStateTerminator