Skip to content

Commit ba118ad

Browse files
Remove affect_neg! from VectorContinuousCallback constructor
`VectorContinuousCallback` never honored `affect_neg!` — its `apply_callback!` dispatch invokes `affect!(integrator, sims)` and ignores the field — so the v6 4-arg form `VectorContinuousCallback(condition, affect!, affect_neg!, len; …)` and the matching `affect_neg! = affect!` keyword on the 3-arg form were silent footguns: users who passed `affect_neg!` got it accepted by the constructor and then dropped at dispatch time (see SciML/OrdinaryDiffEq.jl#3613). Drop the 4-arg outer constructor and the `affect_neg!` keyword on the 3-arg form. The struct field is preserved for downstream code that still reads `cb.affect_neg!`, and is now always set to `affect!`. Co-Authored-By: Chris Rackauckas <[email protected]>
1 parent 80ff6a7 commit ba118ad

2 files changed

Lines changed: 26 additions & 59 deletions

File tree

‎src/callbacks.jl‎

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ struct ContinuousCallback{F1, F2, F3, F4, F5, T, T2, T3, T4, I, R, SCP} <:
145145
initialize::F4, finalize::F5, idxs::I, rootfind,
146146
interp_points, save_positions, dtrelax::R, abstol::T,
147147
reltol::T2, repeat_nudge::T3, initializealg::T4 = nothing,
148-
saved_clock_partitions::SCP = (), maybe_discontinuity::Bool = true,
148+
saved_clock_partitions::SCP = (), maybe_discontinuity::Bool = true,
149149
initialize_save_discretes::Bool = true
150150
) where {
151151
F1, F2, F3, F4, F5, T, T2, T3, T4, I, R, SCP,
@@ -158,7 +158,7 @@ struct ContinuousCallback{F1, F2, F3, F4, F5, T, T2, T3, T4, I, R, SCP} <:
158158
interp_points,
159159
BitArray(collect(save_positions)),
160160
dtrelax, abstol, reltol, repeat_nudge, initializealg, saved_clock_partitions,
161-
maybe_discontinuity, initialize_save_discretes
161+
maybe_discontinuity, initialize_save_discretes
162162
)
163163
end
164164
end
@@ -200,7 +200,7 @@ function ContinuousCallback(
200200
interp_points = 10,
201201
dtrelax = 1,
202202
abstol = 10eps(), reltol = 0, repeat_nudge = 1 // 100,
203-
initializealg = nothing, saved_clock_partitions = (),
203+
initializealg = nothing, saved_clock_partitions = (),
204204
maybe_discontinuity = true,
205205
initialize_save_discretes = true,
206206
)
@@ -214,26 +214,13 @@ function ContinuousCallback(
214214
end
215215

216216
"""
217-
```julia
218-
VectorContinuousCallback(condition, affect!, affect_neg!, len;
219-
initialize = INITIALIZE_DEFAULT,
220-
finalize = FINALIZE_DEFAULT,
221-
idxs = nothing,
222-
rootfind = LeftRootFind,
223-
save_positions = (true, true),
224-
interp_points = 10,
225-
abstol = 10eps(), reltol = 0, repeat_nudge = 1 // 100,
226-
initializealg = nothing, maybe_discontinuity = true)
227-
```
228-
229217
```julia
230218
VectorContinuousCallback(condition, affect!, len;
231219
initialize = INITIALIZE_DEFAULT,
232220
finalize = FINALIZE_DEFAULT,
233221
idxs = nothing,
234222
rootfind = LeftRootFind,
235223
save_positions = (true, true),
236-
affect_neg! = affect!,
237224
interp_points = 10,
238225
abstol = 10eps(), reltol = 0, repeat_nudge = 1 // 100,
239226
initializealg = nothing, maybe_discontinuity = true)
@@ -306,39 +293,13 @@ struct VectorContinuousCallback{F1, F2, F3, F4, F5, T, T2, T3, T4, I, R, SCP} <:
306293
end
307294
end
308295

309-
function VectorContinuousCallback(
310-
condition, affect!, affect_neg!, len;
311-
initialize = INITIALIZE_DEFAULT,
312-
finalize = FINALIZE_DEFAULT,
313-
idxs = nothing,
314-
rootfind = LeftRootFind,
315-
save_positions = (true, true),
316-
interp_points = 10,
317-
dtrelax = 1,
318-
abstol = 10eps(), reltol = 0, repeat_nudge = 1 // 100,
319-
initializealg = nothing, saved_clock_partitions = (),
320-
maybe_discontinuity = true,
321-
initialize_save_discretes = true
322-
)
323-
return VectorContinuousCallback(
324-
condition, affect!, affect_neg!, len,
325-
initialize, finalize,
326-
idxs,
327-
rootfind, interp_points,
328-
save_positions, dtrelax,
329-
abstol, reltol, repeat_nudge, initializealg, saved_clock_partitions,
330-
maybe_discontinuity, initialize_save_discretes
331-
)
332-
end
333-
334296
function VectorContinuousCallback(
335297
condition, affect!, len;
336298
initialize = INITIALIZE_DEFAULT,
337299
finalize = FINALIZE_DEFAULT,
338300
idxs = nothing,
339301
rootfind = LeftRootFind,
340302
save_positions = (true, true),
341-
affect_neg! = affect!,
342303
interp_points = 10,
343304
dtrelax = 1,
344305
abstol = 10eps(), reltol = 0, repeat_nudge = 1 // 100,
@@ -347,7 +308,7 @@ function VectorContinuousCallback(
347308
initialize_save_discretes = true
348309
)
349310
return VectorContinuousCallback(
350-
condition, affect!, affect_neg!, len, initialize, finalize,
311+
condition, affect!, affect!, len, initialize, finalize,
351312
idxs,
352313
rootfind, interp_points,
353314
collect(save_positions),

‎test/callback_constructors.jl‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
11
using SciMLBase
22
using Test
33

4-
# Regression: the 4-arg `VectorContinuousCallback` keyword constructor used to
5-
# forward arguments to the inner positional constructor in the wrong order,
6-
# placing `maybe_discontinuity` in the slot expected for `abstol`.
7-
@testset "VectorContinuousCallback 4-arg kwarg constructor" begin
8-
cond(out, u, t, integ) = (out[1] = u[1]; out[2] = u[2])
4+
@testset "VectorContinuousCallback 3-arg kwarg constructor" begin
5+
cond(out, u, t, integ) = (out[1] = u[1])
96
aff!(integ, idx) = nothing
107

11-
cb = VectorContinuousCallback(cond, aff!, nothing, 2)
12-
@test cb isa VectorContinuousCallback
8+
cb = VectorContinuousCallback(cond, aff!, 1; abstol = 1.0e-12)
9+
@test cb.abstol == 1.0e-12
10+
@test cb.maybe_discontinuity == true
1311

14-
cb_kw = VectorContinuousCallback(cond, aff!, nothing, 2;
12+
cb_kw = VectorContinuousCallback(
13+
cond, aff!, 2;
1514
save_positions = (true, false),
1615
rootfind = SciMLBase.LeftRootFind,
17-
abstol = 1e-9, reltol = 0.0,
16+
abstol = 1.0e-9, reltol = 0.0,
1817
saved_clock_partitions = (),
1918
maybe_discontinuity = false,
20-
initialize_save_discretes = false)
19+
initialize_save_discretes = false
20+
)
2121
@test cb_kw.save_positions == BitVector([true, false])
22-
@test cb_kw.abstol == 1e-9
22+
@test cb_kw.abstol == 1.0e-9
2323
@test cb_kw.maybe_discontinuity == false
2424
@test cb_kw.initialize_save_discretes == false
2525
@test cb_kw.saved_clock_partitions == ()
2626
end
2727

28-
# Sanity check: the 3-arg form (which already had the right order) still works.
29-
@testset "VectorContinuousCallback 3-arg kwarg constructor" begin
28+
# `VectorContinuousCallback` should not accept `affect_neg!`. The
29+
# 4-arg positional form was a leftover from the v6 `ContinuousCallback`
30+
# API that was supposed to be removed in the v7 update; downstream
31+
# code that passed `affect_neg!` had it silently ignored, so the
32+
# constructor itself is now removed (and so is the kwarg).
33+
@testset "VectorContinuousCallback rejects affect_neg!" begin
3034
cond(out, u, t, integ) = (out[1] = u[1])
3135
aff!(integ, idx) = nothing
36+
aff_neg!(integ, idx) = nothing
3237

33-
cb = VectorContinuousCallback(cond, aff!, 1; abstol = 1e-12)
34-
@test cb.abstol == 1e-12
35-
@test cb.maybe_discontinuity == true
38+
@test_throws MethodError VectorContinuousCallback(cond, aff!, aff_neg!, 1)
39+
@test_throws MethodError VectorContinuousCallback(
40+
cond, aff!, 1; affect_neg! = aff_neg!
41+
)
3642
end

0 commit comments

Comments
 (0)