Skip to content

Commit

Permalink
remove update bool
Browse files Browse the repository at this point in the history
  • Loading branch information
LasNikas committed Apr 19, 2024
1 parent 66f863b commit 08b47f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 31 deletions.
24 changes: 10 additions & 14 deletions src/callbacks/update.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
struct UpdateCallback{I}
interval :: I
update :: Bool
interval::I
end

"""
Expand All @@ -14,7 +13,7 @@ regular intervals at `dt` in terms of integration time.
- `interval`: Update quantities at the end of every `interval` time steps (default `interval=1`)
- `dt`: Update quantities in regular intervals of `dt` in terms of integration time
"""
function UpdateCallback(; update=true, interval::Integer=-1, dt=0.0)
function UpdateCallback(; interval::Integer=-1, dt=0.0)
if dt > 0 && interval !== -1
throw(ArgumentError("Setting both interval and dt is not supported!"))
end
Expand All @@ -24,13 +23,13 @@ function UpdateCallback(; update=true, interval::Integer=-1, dt=0.0)
interval = Float64(dt)

# Update every time step (default)
elseif update && interval == -1
elseif interval == -1
interval = 1
end

update_callback! = UpdateCallback(interval, update)
update_callback! = UpdateCallback(interval)

if dt > 0 && update
if dt > 0
# Add a `tstop` every `dt`, and save the final solution.
return PeriodicCallback(update_callback!, dt,
initialize=initial_update!,
Expand All @@ -52,18 +51,18 @@ function initial_update!(cb, u, t, integrator)
initial_update!(cb.affect!, u, t, integrator)
end

initial_update!(cb::UpdateCallback, u, t, integrator) = cb.update && cb(integrator)
initial_update!(cb::UpdateCallback, u, t, integrator) = cb(integrator)

# condition
function (update_callback!::UpdateCallback)(u, t, integrator)
(; interval, update) = update_callback!
(; interval) = update_callback!

# With error-based step size control, some steps can be rejected. Thus,
# `integrator.iter >= integrator.stats.naccept`
# (total #steps) (#accepted steps)
# We need to check the number of accepted steps since callbacks are not
# activated after a rejected step.
return update && (integrator.stats.naccept % interval == 0)
return integrator.stats.naccept % interval == 0
end

# affect
Expand Down Expand Up @@ -93,8 +92,7 @@ end

function Base.show(io::IO, cb::DiscreteCallback{<:Any, <:UpdateCallback})
@nospecialize cb # reduce precompilation time
print(io, "UpdateCallback(interval=", (cb.affect!.update ? cb.affect!.interval : "-"),
")")
print(io, "UpdateCallback(interval=", cb.affect!.interval, ")")
end

function Base.show(io::IO,
Expand All @@ -113,8 +111,7 @@ function Base.show(io::IO, ::MIME"text/plain",
else
update_cb = cb.affect!
setup = [
"interval" => update_cb.update ? update_cb.interval : "-",
"update" => update_cb.update ? "yes" : "no",
"interval" => update_cb.interval,
]
summary_box(io, "UpdateCallback", setup)
end
Expand All @@ -131,7 +128,6 @@ function Base.show(io::IO, ::MIME"text/plain",
update_cb = cb.affect!.affect!
setup = [
"dt" => update_cb.interval,
"update" => update_cb.update ? "yes" : "no",
]
summary_box(io, "UpdateCallback", setup)
end
Expand Down
17 changes: 0 additions & 17 deletions test/callbacks/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
│ UpdateCallback │
│ ══════════════ │
│ interval: ……………………………………………………… 1 │
│ update: …………………………………………………………… yes │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", callback0) == show_box

Expand All @@ -25,7 +24,6 @@
│ UpdateCallback │
│ ══════════════ │
│ interval: ……………………………………………………… 11 │
│ update: …………………………………………………………… yes │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", callback1) == show_box

Expand All @@ -39,23 +37,8 @@
│ UpdateCallback │
│ ══════════════ │
│ dt: ……………………………………………………………………… 1.2 │
│ update: …………………………………………………………… yes │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", callback2) == show_box

callback3 = UpdateCallback(update=false)

show_compact = "UpdateCallback(interval=-)"
@test repr(callback3) == show_compact

show_box = """
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ UpdateCallback │
│ ══════════════ │
│ interval: ……………………………………………………… - │
│ update: …………………………………………………………… no │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", callback3) == show_box
end

@testset "Illegal Input" begin
Expand Down

0 comments on commit 08b47f2

Please sign in to comment.