Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/callbacks/post_process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ end

@inline function backup_condition(cb::PostprocessCallback{Int}, integrator)
return integrator.stats.naccept > 0 &&
round(integrator.stats.naccept / cb.interval) % cb.write_file_interval == 0
(integrator.stats.naccept ÷ cb.interval) % cb.write_file_interval == 0
end

@inline function backup_condition(cb::PostprocessCallback, integrator)
return integrator.stats.naccept > 0 &&
round(Int, integrator.t / cb.interval) % cb.write_file_interval == 0
get_iter(cb.interval, integrator) % cb.write_file_interval == 0
end

# After the simulation has finished, this function is called to write the data to a JSON file
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TrixiParticles = "66699cd8-9c01-4e9d-a059-b96c86d16b3a"
TrixiTest = "0a316866-cbd0-4425-8bcb-08103b2c1f26"

[compat]
Expand Down
76 changes: 76 additions & 0 deletions test/callbacks/postprocess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,80 @@
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", callback) == show_box
end

@testset verbose=true "backup_condition" begin
function example_function(system, data, t)
return t
end

# Helper to create a mock integrator for backup_condition tests
function make_integrator(; naccept, t, tspan)
return (; stats=(; naccept), t, sol=(; prob=(; tspan)))
end

# Integer interval
@testset "interval-based (Int)" begin
cb_interval = PostprocessCallback(; interval=10, write_file_interval=3,
example_function)
pp = cb_interval.affect!

# No write at naccept=0 (initialization)
integrator = make_integrator(naccept=0, t=0.0, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

# Write at 1st, 2nd trigger: no write (write_file_interval=3)
integrator = make_integrator(naccept=10, t=0.1, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

integrator = make_integrator(naccept=20, t=0.2, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

# Write at 3rd trigger (3 * interval = 30)
integrator = make_integrator(naccept=30, t=0.3, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == true

integrator = make_integrator(naccept=40, t=0.4, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

# Write every write_file_interval triggers, regardless of t_0
# (naccept resets after restart, so counting is relative to new start)
integrator = make_integrator(naccept=30, t=10.3, tspan=(10.0, 11.0))
@test TrixiParticles.backup_condition(pp, integrator) == true
end

# Float (dt-based)
@testset "dt-based (Float)" begin
cb_dt = PostprocessCallback(; dt=0.1, write_file_interval=3, example_function)
pp = cb_dt.affect!.affect!

# No write at naccept=0 (initialization)
integrator = make_integrator(naccept=0, t=0.0, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

# 1st and 2nd trigger: no write
integrator = make_integrator(naccept=1, t=0.1, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

integrator = make_integrator(naccept=2, t=0.2, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

# 3rd trigger: write
integrator = make_integrator(naccept=3, t=0.3, tspan=(0.0, 1.0))
@test TrixiParticles.backup_condition(pp, integrator) == true

# After restart at t_0=1.0: counting restarts from tspan[1]
integrator = make_integrator(naccept=0, t=1.0, tspan=(1.0, 2.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

integrator = make_integrator(naccept=1, t=1.1, tspan=(1.0, 2.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

integrator = make_integrator(naccept=2, t=1.2, tspan=(1.0, 2.0))
@test TrixiParticles.backup_condition(pp, integrator) == false

# 3rd trigger after restart: write (not at t=1.2 as the old code would give)
integrator = make_integrator(naccept=3, t=1.3, tspan=(1.0, 2.0))
@test TrixiParticles.backup_condition(pp, integrator) == true
end
end
end