• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SciML / StochasticDelayDiffEq.jl / 8599171049

08 Apr 2024 11:20AM UTC coverage: 63.902% (-1.4%) from 65.284%
8599171049

push

github

web-flow
Bump julia-actions/setup-julia from 1 to 2 (#77)

Bumps [julia-actions/setup-julia](https://github.com/julia-actions/setup-julia) from 1 to 2.
- [Release notes](https://github.com/julia-actions/setup-julia/releases)
- [Commits](https://github.com/julia-actions/setup-julia/compare/v1...v2)

---
updated-dependencies:
- dependency-name: julia-actions/setup-julia
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

416 of 651 relevant lines covered (63.9%)

8944997.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.06
/src/integrators/interface.jl
1
@inline function loopheader!(integrator::SDDEIntegrator)
94,764,988✔
2
    # Apply right after iterators / callbacks
3

4
    # Accept or reject the step
5
    if integrator.iter > 0
94,764,988✔
6
        if ((integrator.opts.adaptive && integrator.accept_step) ||
189,406,315✔
7
            !integrator.opts.adaptive) && !integrator.force_stepfail
8
            integrator.success_iter += 1
94,708,436✔
9
            # StochasticDiffEq.apply_step!(integrator)
10
            StochasticDiffEq.apply_step!(integrator)
115,186,515✔
11
        elseif integrator.opts.adaptive && !integrator.accept_step
100✔
12
            if integrator.isout
100✔
13
                integrator.dtnew = integrator.dt * integrator.opts.qmin
×
14
            elseif !integrator.force_stepfail
100✔
15
                step_reject_controller!(integrator, getalg(integrator.alg))
100✔
16
            end
17
            StochasticDiffEq.choose_algorithm!(integrator, integrator.cache)
100✔
18
            StochasticDiffEq.fix_dtnew_at_bounds!(integrator)
100✔
19
            StochasticDiffEq.modify_dtnew_for_tstops!(integrator)
100✔
20
            reject_step!(integrator, integrator.dtnew)
100✔
21
            integrator.dt = integrator.dtnew
100✔
22
            integrator.sqdt = sqrt(abs(integrator.dt))
100✔
23
        end
24
    end
25

26
    integrator.iter += 1
94,764,988✔
27
    integrator.force_stepfail = false
94,764,988✔
28
end
29

30
@inline function loopfooter!(integrator::SDDEIntegrator)
94,764,987✔
31
    ttmp = integrator.t + integrator.dt
94,764,987✔
32
    if integrator.force_stepfail
94,764,987✔
33
        if integrator.opts.adaptive
×
34
            integrator.dtnew = integrator.dt / integrator.opts.failfactor
×
35
        elseif integrator.last_stepfail
×
36
            return
×
37
        end
38
        integrator.last_stepfail = true
×
39
        integrator.accept_step = false
×
40
    elseif integrator.opts.adaptive
94,764,987✔
41
        stepsize_controller!(integrator, getalg(integrator.alg))
10,887✔
42
        integrator.isout = integrator.opts.isoutofdomain(integrator.u, integrator.p, ttmp)
10,887✔
43
        integrator.accept_step = (!integrator.isout && accept_step_controller(integrator,
10,987✔
44
                                                         integrator.opts.controller)) ||
45
                                 (integrator.opts.force_dtmin &&
46
                                  integrator.dt <= integrator.opts.dtmin)
47
        if integrator.accept_step # Accepted
10,887✔
48
            step_accept_controller!(integrator, getalg(integrator.alg))
10,787✔
49
            integrator.last_stepfail = false
10,787✔
50
            integrator.tprev = integrator.t
10,787✔
51
            if integrator.t isa AbstractFloat && !isempty(integrator.opts.tstops)
10,787✔
52
                tstop = integrator.tdir * first(integrator.opts.tstops)
10,787✔
53
                @fastmath abs(ttmp - tstop) < 10eps(integrator.t) ? (integrator.t = tstop) :
21,541✔
54
                          (integrator.t = ttmp)
55
            else
56
                integrator.t = ttmp
×
57
            end
58
            StochasticDiffEq.calc_dt_propose!(integrator)
10,787✔
59
            StochasticDiffEq.handle_callbacks!(integrator)
10,787✔
60
        end
61
    else # Non adaptive
62
        integrator.tprev = integrator.t
94,754,100✔
63
        if integrator.t isa AbstractFloat && !isempty(integrator.opts.tstops)
94,754,100✔
64
            tstop = integrator.tdir * first(integrator.opts.tstops)
94,754,100✔
65
            # For some reason 10eps(integrator.t) is slow here
66
            # TODO: Allow higher precision but profile
67
            @fastmath abs(ttmp - tstop) < 10eps(max(integrator.t, tstop)) ?
189,317,379✔
68
                      (integrator.t = tstop) : (integrator.t = ttmp)
69
        else
70
            integrator.t = ttmp
×
71
        end
72
        integrator.last_stepfail = false
94,754,100✔
73
        integrator.accept_step = true
94,754,100✔
74
        integrator.dtpropose = integrator.dt
94,754,100✔
75
        StochasticDiffEq.handle_callbacks!(integrator)
94,754,100✔
76
    end
77
    if integrator.opts.progress && integrator.iter % integrator.opts.progress_steps == 0
94,764,987✔
78
        @logmsg(-1,
×
79
                integrator.opts.progress_name,
80
                _id=:StochasticDelayDiffEq,
81
                message=integrator.opts.progress_message(integrator.dt, integrator.u,
82
                                                         integrator.p, integrator.t),
83
                progress=integrator.t / integrator.sol.prob.tspan[2])
84
    end
85
end
86

87
@inline function postamble!(integrator::SDDEIntegrator)
56,451✔
88
    StochasticDiffEq.solution_endpoint_match_cur_integrator!(integrator)
56,451✔
89
    resize!(integrator.sol.t, integrator.saveiter)
112,902✔
90
    resize!(integrator.sol.u, integrator.saveiter)
112,902✔
91
    if integrator.opts.progress
56,451✔
92
        @logmsg(-1,
×
93
                integrator.opts.progress_name,
94
                _id=:StochasticDiffEq,
95
                message=integrator.opts.progress_message(integrator.dt, integrator.u,
96
                                                         integrator.p, integrator.t),
97
                progress="done")
98
    end
99
    return nothing
56,451✔
100
end
101

102
function DiffEqBase.auto_dt_reset!(integrator::SDDEIntegrator)
2✔
103
    # @unpack f,g, u, t, tdir, opts, sol, stats = integrator
104
    @unpack f, g, u, t, tdir, opts, sol = integrator
2✔
105
    @unpack prob = sol
2✔
106
    @unpack abstol, reltol, internalnorm = opts
2✔
107

108
    # determine maximal time step
109
    if has_constant_lags(prob)
2✔
110
        dtmax = tdir * min(abs(opts.dtmax), minimum(abs, prob.constant_lags))
1✔
111
    else
112
        dtmax = opts.dtmax
1✔
113
    end
114

115
    # determine initial time step
116
    sde_prob = SDEProblem(f, g, prob.u0, prob.tspan, prob.p;
2✔
117
                          noise_rate_prototype = prob.noise_rate_prototype,
118
                          noise = prob.noise,
119
                          seed = prob.seed,
120
                          prob.kwargs...)
121
    integrator.dt = StochasticDiffEq.sde_determine_initdt(integrator.u, integrator.t,
2✔
122
                                                          integrator.tdir,
123
                                                          integrator.opts.dtmax,
124
                                                          integrator.opts.abstol,
125
                                                          integrator.opts.reltol,
126
                                                          integrator.opts.internalnorm,
127
                                                          sde_prob,
128
                                                          StochasticDiffEq.get_current_alg_order(getalg(integrator.alg),
129
                                                                                                 integrator.cache),
130
                                                          integrator)
131
    # update statistics
132
    # destats.nf += 2
133
    # destats.nf2 += 2
134

135
    nothing
×
136
end
137

138
DiffEqBase.has_reinit(integrator::SDDEIntegrator) = false # TODO true - syncronize with DDE!
×
139
function DiffEqBase.reinit!(integrator::SDDEIntegrator, u0 = integrator.sol.prob.u0;
×
140
                            t0 = integrator.sol.prob.tspan[1],
141
                            tf = integrator.sol.prob.tspan[2],
142
                            erase_sol = true,
143
                            tstops = integrator.opts.tstops_cache,
144
                            saveat = integrator.opts.saveat_cache,
145
                            d_discontinuities = integrator.opts.d_discontinuities_cache,
146
                            order_discontinuity_t0 = t0 == integrator.sol.prob.tspan[1] &&
147
                                u0 == integrator.sol.prob.u0 ?
148
                                                     integrator.order_discontinuity_t0 : 0,
149
                            reinit_cache = true, reinit_callbacks = true,
150
                            initialize_save = true,
151
                            reset_dt = (integrator.dtcache == zero(integrator.dt)) &&
152
                                integrator.opts.adaptive)
153
    if DiffEqBase.isinplace(integrator.sol.prob)
×
154
        recursivecopy!(integrator.u, u0)
×
155
        recursivecopy!(integrator.uprev, integrator.u)
×
156
    else
157
        integrator.u = u0
×
158
        integrator.uprev = integrator.u
×
159
    end
160

161
    integrator.t = t0
×
162
    integrator.tprev = t0
×
163

164
    tType = typeof(integrator.t)
×
165
    maximum_order = StochasticDiffEq.alg_order(getalg(integrator.alg))
×
166
    tstops_internal, saveat_internal, d_discontinuities_internal = tstop_saveat_disc_handling(tstops,
×
167
                                                                                              saveat,
168
                                                                                              d_discontinuities,
169
                                                                                              (tType(t0),
170
                                                                                               tType(tf)),
171
                                                                                              order_discontinuity_t0,
172
                                                                                              maximum_order,
173
                                                                                              integrator.sol.prob.constant_lags,
174
                                                                                              integrator.sol.prob.neutral)
175

176
    integrator.opts.tstops = tstops_internal
×
177
    integrator.opts.saveat = saveat_internal
×
178
    integrator.opts.d_discontinuities = d_discontinuities_internal
×
179
    integrator.order_discontinuity_t0 = order_discontinuity_t0
×
180

181
    if erase_sol
×
182
        if integrator.opts.save_start
×
183
            resize_start = 1
×
184
        else
185
            resize_start = 0
×
186
        end
187
        resize!(integrator.sol.u, resize_start)
×
188
        resize!(integrator.sol.t, resize_start)
×
189
        if integrator.sol.u_analytic !== nothing
×
190
            resize!(integrator.sol.u_analytic, 0)
×
191
        end
192
        if typeof(getalg(integrator.alg)) <:
×
193
           StochasticDiffEq.StochasticDiffEqCompositeAlgorithm
194
            resize!(integrator.sol.alg_choice, resize_start)
×
195
        end
196
        integrator.saveiter = resize_start
×
197

198
        if order_discontinuity_t0 ≤ maximum_order
×
199
            resize!(integrator.tracked_discontinuities, 1)
×
200
            integrator.tracked_discontinuities[1] = Discontinuity(integrator.tdir *
×
201
                                                                  integrator.t,
202
                                                                  Rational{Int}(order_discontinuity_t0))
203
        else
204
            resize!(integrator.tracked_discontinuities, 0)
×
205
        end
206
    end
207

208
    integrator.iter = 0
×
209
    integrator.success_iter = 0
×
210

211
    # full re-initialize the PI in timestepping
212
    integrator.qold = integrator.opts.qoldinit
×
213
    integrator.q11 = typeof(integrator.t)(1)
×
214
    integrator.u_modified = false
×
215

216
    if reset_dt
×
217
        DiffEqBase.auto_dt_reset!(integrator)
×
218
    end
219

220
    if reinit_callbacks
×
221
        StochasticDiffEq.initialize_callbacks!(integrator, initialize_save)
×
222
    end
223

224
    if reinit_cache
×
225
        StochasticDiffEq.initialize!(integrator.integrator, integrator.cache)
×
226
        # StochasticDiffEq.initialize!(integrator,integrator.cache)
227
    end
228

229
    reinit!(integrator.W, integrator.dt)
×
230
    nothing
×
231
end
232

233
@inline function DiffEqBase.get_du(integrator::SDDEIntegrator)
×
234
    (integrator.u - integrator.uprev) / integrator.dt
×
235
end
236

237
@inline function DiffEqBase.get_du!(out, integrator::SDDEIntegrator)
×
238
    DiffEqBase.@.. out = (integrator.u - integrator.uprev) / integrator.dt
×
239
end
240

241
@inline function DiffEqBase.add_tstop!(integrator::SDDEIntegrator, t)
×
242
    t < integrator.t &&
×
243
        error("Tried to add a tstop that is behind the current time. This is strictly forbidden")
244
    push!(integrator.opts.tstops, integrator.tdir * t)
×
245
end
246

247
function DiffEqBase.change_t_via_interpolation!(integrator::SDDEIntegrator, t,
×
248
                                                modify_save_endpoint::Type{Val{T}} = Val{
249
                                                                                         false
250
                                                                                         }) where {
251
                                                                                                   T
252
                                                                                                   }
253
    StochasticDiffEq.change_t_via_interpolation!(integrator, t, modify_save_endpoint)
×
254
end
255

256
# update integrator when u is modified by callbacks
257
function StochasticDiffEq.handle_callback_modifiers!(integrator::SDDEIntegrator)
13✔
258
    # copied from StochasticDiffEq
259
    if integrator.P !== nothing && integrator.opts.adaptive
13✔
260
        if integrator.cache isa StochasticDiffEqMutableCache
×
261
            oldrate = integrator.P.cache.currate
×
262
            integrator.P.cache.rate(oldrate, integrator.u, integrator.p, integrator.t)
×
263
        else
264
            integrator.P.cache.currate = integrator.P.cache.rate(integrator.u, integrator.p,
×
265
                                                                 integrator.t)
266
        end
267
    end
268

269
    # update heap of discontinuities
270
    # discontinuity is assumed to be of order 0, i.e. solution x is discontinuous
271
    push!(integrator.opts.d_discontinuities,
13✔
272
          Discontinuity(integrator.tdir * integrator.t, 0 // 1))
273
end
274

275
function DiffEqBase.u_modified!(integrator::SDDEIntegrator, bool::Bool)
2✔
276
    integrator.u_modified = bool
2✔
277
    nothing
×
278
end
279

280
DiffEqBase.get_proposed_dt(integrator::SDDEIntegrator) = integrator.dtpropose
×
281
function DiffEqBase.set_proposed_dt!(integrator::SDDEIntegrator, dt::Number)
×
282
    (integrator.dtpropose = dt; integrator.dtcache = dt)
×
283
end
284

285
function DiffEqBase.set_proposed_dt!(integrator::SDDEIntegrator,
×
286
                                     integrator2::SDDEIntegrator)
287
    integrator.dtpropose = integrator2.dtpropose
×
288
    integrator.qold = integrator2.qold
×
289
end
290

291
@inline function postamble!(integrator::HistorySDEIntegrator)
1✔
292
    if integrator.saveiter == 0 || integrator.sol.t[integrator.saveiter] != integrator.t
2✔
293
        integrator.saveiter += 1
×
294
        copyat_or_push!(integrator.sol.t, integrator.saveiter, integrator.t)
×
295
        if integrator.opts.save_idxs === nothing
×
296
            copyat_or_push!(integrator.sol.u, integrator.saveiter, integrator.u)
×
297
        else
298
            copyat_or_push!(integrator.sol.u, integrator.saveiter,
×
299
                            integrator.u[integrator.opts.save_idxs], Val{false})
300
        end
301
    end
302

303
    resize!(integrator.sol.t, integrator.saveiter)
2✔
304
    resize!(integrator.sol.u, integrator.saveiter)
1✔
305
end
306

307
function DiffEqBase.postamble!(integrator::SDDEIntegrator)
1✔
308
    # clean up solution of the SDE integrator
309
    postamble!(integrator.integrator)
1✔
310

311
    # # clean solution of the SDDE integrator
312
    # StochasticDiffEq._postamble!(integrator)
313
end
314

315
@inline function DiffEqBase.savevalues!(integrator::SDDEIntegrator,
189,529,775✔
316
                                        force_save = false)::Tuple{Bool, Bool}
317
    saved, savedexactly = false, false
284,294,662✔
318
    !integrator.opts.save_on && return saved, savedexactly
94,764,888✔
319
    tdir_t = integrator.tdir * integrator.t
94,764,888✔
320
    while !isempty(integrator.opts.saveat) && first(integrator.opts.saveat) <= tdir_t # Perform saveat
94,764,888✔
321
        integrator.saveiter += 1
×
322
        saved = true
×
323
        curt = integrator.tdir * pop!(integrator.opts.saveat)
×
324
        if curt != integrator.t # If <t, interpolate
×
325
            Θ = (curt - integrator.tprev) / integrator.dt
×
326
            val = StochasticDiffEq.sde_interpolant(Θ, integrator, integrator.opts.save_idxs,
×
327
                                                   Val{0}) # out of place, but force copy later
328
            save_val = val
×
329
            copyat_or_push!(integrator.sol.t, integrator.saveiter, curt)
×
330
            copyat_or_push!(integrator.sol.u, integrator.saveiter, save_val, Val{false})
×
331
            if integrator.alg isa StochasticDiffEq.StochasticDiffEqCompositeAlgorithm
×
332
                copyat_or_push!(integrator.sol.alg_choice, integrator.saveiter,
×
333
                                integrator.cache.current)
334
            end
335
        else # ==t, just save
336
            savedexactly = true
×
337
            copyat_or_push!(integrator.sol.t, integrator.saveiter, integrator.t)
×
338
            if integrator.opts.save_idxs === nothing
×
339
                copyat_or_push!(integrator.sol.u, integrator.saveiter, integrator.u)
×
340
            else
341
                copyat_or_push!(integrator.sol.u, integrator.saveiter,
×
342
                                integrator.u[integrator.opts.save_idxs], Val{false})
343
            end
344
            if integrator.alg isa
×
345
               Union{StochasticDiffEq.StochasticDiffEqCompositeAlgorithm,
346
                     StochasticDiffEq.StochasticDiffEqRODECompositeAlgorithm}
347
                copyat_or_push!(integrator.sol.alg_choice, integrator.saveiter,
×
348
                                integrator.cache.current)
349
            end
350
        end
351
    end
×
352
    if force_save || integrator.opts.save_everystep
94,764,888✔
353
        integrator.saveiter += 1
94,764,888✔
354
        saved, savedexactly = true, true
94,764,888✔
355
        if integrator.opts.save_idxs === nothing
94,764,888✔
356
            copyat_or_push!(integrator.sol.u, integrator.saveiter, integrator.u)
94,765,483✔
357
        else
358
            copyat_or_push!(integrator.sol.u, integrator.saveiter,
×
359
                            integrator.u[integrator.opts.save_idxs], Val{false})
360
        end
361
        copyat_or_push!(integrator.sol.t, integrator.saveiter, integrator.t)
189,529,776✔
362
        if integrator.alg isa
94,764,888✔
363
           Union{StochasticDiffEq.StochasticDiffEqCompositeAlgorithm,
364
                 StochasticDiffEq.StochasticDiffEqRODECompositeAlgorithm}
365
            copyat_or_push!(integrator.sol.alg_choice, integrator.saveiter,
×
366
                            integrator.cache.current)
367
        end
368
    end
369
    return saved, savedexactly
94,764,888✔
370
end
371

372
@inline function DiffEqNoiseProcess.setup_next_step!(integrator::SDDEIntegrator)
56,452✔
373
    !isnothing(integrator.W) &&
60,473✔
374
        DiffEqNoiseProcess.setup_next_step!(integrator.W, integrator.u, integrator.p)
375
    !isnothing(integrator.P) &&
56,452✔
376
        DiffEqNoiseProcess.setup_next_step!(integrator.P, integrator.u, integrator.p)
377
end
378

379
@inline function DiffEqNoiseProcess.reject_step!(integrator::SDDEIntegrator,
100✔
380
                                                 dtnew = integrator.dtnew)
381
    !isnothing(integrator.W) &&
100✔
382
        reject_step!(integrator.W, dtnew, integrator.u, integrator.p)
383
    !isnothing(integrator.P) &&
100✔
384
        reject_step!(integrator.P, dtnew, integrator.u, integrator.p)
385
end
386

387
@inline function DiffEqNoiseProcess.accept_step!(integrator::SDDEIntegrator, setup)
94,764,887✔
388
    !isnothing(integrator.W) &&
115,242,966✔
389
        accept_step!(integrator.W, integrator.dt, integrator.u, integrator.p, setup)
390
    !isnothing(integrator.P) &&
94,764,887✔
391
        accept_step!(integrator.P, integrator.dt, integrator.u, integrator.p, setup)
392
end
393

394
@inline function DiffEqNoiseProcess.save_noise!(integrator::SDDEIntegrator)
51✔
395
    !isnothing(integrator.W) && DiffEqNoiseProcess.save_noise!(integrator.W)
51✔
396
    !isnothing(integrator.P) && DiffEqNoiseProcess.save_noise!(integrator.P)
51✔
397
end
398

399
@inline function DiffEqBase.get_tmp_cache(integrator::SDDEIntegrator)
2,510✔
400
    get_tmp_cache(integrator, integrator.alg, integrator.cache)
2,510✔
401
end
402
# avoid method ambiguity
403
for typ in (StochasticDiffEq.StochasticDiffEqAlgorithm,
404
            StochasticDiffEq.StochasticDiffEqNewtonAdaptiveAlgorithm)
405
    @eval @inline function DiffEqBase.get_tmp_cache(integrator::SDDEIntegrator, alg::$typ,
×
406
                                                    cache::StochasticDiffEq.StochasticDiffEqConstantCache)
407
        nothing
×
408
    end
409
end
410
@inline DiffEqBase.get_tmp_cache(integrator::SDDEIntegrator, alg, cache) = (cache.tmp,)
2,510✔
411
@inline function DiffEqBase.get_tmp_cache(integrator::SDDEIntegrator,
×
412
                                          alg::StochasticDiffEq.StochasticDiffEqNewtonAdaptiveAlgorithm,
413
                                          cache)
414
    (cache.nlsolver.tmp, cache.nlsolver.ztmp)
×
415
end
416
@inline function DiffEqBase.get_tmp_cache(integrator::SDDEIntegrator,
×
417
                                          alg::StochasticDiffEq.StochasticCompositeAlgorithm,
418
                                          cache)
419
    get_tmp_cache(integrator, alg.algs[1], cache.caches[1])
×
420
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc