Skip to content

Commit 4400d14

Browse files
Merge pull request #35 from SciML/dw/discrete
Fix #34
2 parents 0b95521 + f5d3c7f commit 4400d14

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StochasticDelayDiffEq"
22
uuid = "29a0d76e-afc8-11e9-03a4-eda52ae4b960"
33
authors = ["Henrik Sykora <[email protected]>"]
4-
version = "0.2.5"
4+
version = "0.2.6"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

src/integrators/interface.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,28 @@ end
221221

222222
DiffEqBase.change_t_via_interpolation!(integrator::SDDEIntegrator,t,modify_save_endpoint::Type{Val{T}}=Val{false}) where T = StochasticDiffEq.change_t_via_interpolation!(integrator, t, modify_save_endpoint)
223223

224-
function u_modified!(integrator::SDDEIntegrator,bool::Bool)
225-
integrator.u_modified = bool
224+
# update integrator when u is modified by callbacks
225+
function StochasticDiffEq.handle_callback_modifiers!(integrator::SDDEIntegrator)
226+
# copied from StochasticDiffEq
227+
if integrator.P !== nothing && integrator.opts.adaptive
228+
if integrator.cache isa StochasticDiffEqMutableCache
229+
oldrate = integrator.P.cache.currate
230+
integrator.P.cache.rate(oldrate, integrator.u, integrator.p, integrator.t)
231+
else
232+
integrator.P.cache.currate = integrator.P.cache.rate(
233+
integrator.u, integrator.p, integrator.t
234+
)
235+
end
236+
end
237+
238+
# update heap of discontinuities
239+
# discontinuity is assumed to be of order 0, i.e. solution x is discontinuous
240+
push!(integrator.opts.d_discontinuities, Discontinuity(integrator.tdir * integrator.t, 0))
241+
end
242+
243+
function DiffEqBase.u_modified!(integrator::SDDEIntegrator, bool::Bool)
244+
integrator.u_modified = bool
245+
nothing
226246
end
227247

228248
get_proposed_dt(integrator::SDDEIntegrator) = integrator.dtpropose

test/events.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using StochasticDelayDiffEq
2+
using Test
3+
4+
@testset "discrete callback (#34)" begin
5+
f(u, h, p, t) = 0.3 * h(p, t - p)
6+
g(u, h, p, t) = 0.1
7+
h(p, t) = 0.0
8+
prob = SDDEProblem(f, g, h, (0.0, 4.0), 0.5; constant_lags=(0.5,))
9+
10+
# event at `t = 3`
11+
cb = DiscreteCallback(
12+
(u, t, integrator) -> t == 3,
13+
integrator -> (integrator.u = -integrator.u)
14+
)
15+
16+
sol = solve(prob, RKMil(), tstops=(3,), callback=cb)
17+
ts = findall(x -> x == 3, sol.t)
18+
@test length(ts) == 2
19+
@test sol.u[ts[1]] == -sol.u[ts[2]]
20+
@test sol(3.0; continuity=:right) == -sol(3.0; continuity=:left)
21+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ using SafeTestsets
22

33
@safetestset "SDDEProblem, solve" begin include("test_prob_sol.jl") end
44
@safetestset "Analyticless Convergence Tests" begin include("analyticless_convergence_tests.jl") end
5+
@safetestset "Event handling" begin include("events.jl") end

0 commit comments

Comments
 (0)