Skip to content

Commit

Permalink
Merge pull request #206 from SciML/perf_ref
Browse files Browse the repository at this point in the history
Improve long performance and prevent repeat calls in PresetTimeCallback
  • Loading branch information
ChrisRackauckas authored Feb 27, 2024
2 parents e20fc6c + 8bd54d3 commit 44980a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
34 changes: 30 additions & 4 deletions src/preset_time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,36 @@ automatic.
Defaults to true. If false, then tstops can extend the interval of integration.
"""
function PresetTimeCallback(tstops, user_affect!;
initialize = SciMLBase.INITIALIZE_DEFAULT,
filter_tstops = true, kwargs...)
condition = function (u, t, integrator)
t in tstops
initialize = SciMLBase.INITIALIZE_DEFAULT,
filter_tstops = true,
sort_inplace = false, kwargs...)

local tdir
if tstops isa AbstractVector
if sort_inplace
sort!(tstops)
else
tstops = sort(tstops)
end
search_start, search_end = firstindex(tstops), lastindex(tstops)
condition = function (u, t, integrator)
t in @view(tstops[search_start:search_end])
end
else
search_start, search_end = 0, 0
condition = function (u, t, integrator)
t == tstops
end
end

# Call f, update tnext, and make sure we stop at the new tnext
affect! = function (integrator)
user_affect!(integrator)
if integrator.tdir > 0
search_start += 1
else
search_end -= 1
end
nothing
end

Expand All @@ -50,6 +71,11 @@ function PresetTimeCallback(tstops, user_affect!;
end
if t tstops
user_affect!(integrator)
if integrator.tdir > 0
search_start += 1
else
search_end -= 1
end
end
end
DiscreteCallback(condition, affect!; initialize = initialize_preset, kwargs...)
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ using Test
@time @testset "IndependentlyLinearized tests" begin
include("independentlylinearizedtests.jl")
end
@time @testset "Saving tests" begin
include("saving_tests.jl")
end
@time @testset "PresetTime tests" begin
include("preset_time.jl")
end
Expand All @@ -48,4 +45,7 @@ using Test
@time @testset "Integrating sum tests" begin
include("integrating_sum_tests.jl")
end
@time @testset "Saving tests" begin
include("saving_tests.jl")
end
end

0 comments on commit 44980a1

Please sign in to comment.