• 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

63.27
/src/utils.jl
1
getalg(alg0::DEAlgorithm) = alg0
1,021,916✔
2
getalg(alg0::MethodOfSteps) = alg0.alg
×
3

4
"""
5
    has_constant_lags(integrator::DDEIntegrator)
6

7
Return if the DDE problem of the `integrator` contains constant delays.
8
"""
9
has_constant_lags(integrator::SDDEIntegrator) = has_constant_lags(integrator.sol.prob)
78,003✔
10

11
"""
12
    has_dependent_lags(integrator::DDEIntegrator)
13

14
Return if the DDE problem of the `integrator` contains dependent delays.
15
"""
16
has_dependent_lags(integrator::SDDEIntegrator) = has_dependent_lags(integrator.sol.prob)
×
17

18
"""
19
    has_constant_lags(prob::DDEProblem)
20

21
Return if the DDE problem `prob` contains constant delays.
22
"""
23
function has_constant_lags(prob::SDDEProblem)
134,457✔
24
    prob.constant_lags !== nothing && !isempty(prob.constant_lags)
134,457✔
25
end
26

27
"""
28
    has_dependent_lags(prob::DDEProblem)
29

30
Return if the DDE problem `prob` contains dependent delays.
31
"""
32
function has_dependent_lags(prob::SDDEProblem)
56,452✔
33
    prob.dependent_lags !== nothing && !isempty(prob.dependent_lags)
56,452✔
34
end
35

36
function u_uprev(u0; alias_u0 = false)
282,260✔
37
    if u0 isa Tuple
112,904✔
38
        u = ArrayPartition(prob.u0, Val{true})
×
39
    else
40
        if alias_u0
112,904✔
41
            u = u0
×
42
        else
43
            u = recursivecopy(u0)
112,904✔
44
        end
45
    end
46
    uprev = recursivecopy(u)
112,904✔
47
    u, uprev
112,904✔
48
end
49

50
"""
51
    solution_arrays(u, tspan, rate_prototype; kwargs...)
52

53
Return arrays of saved time points, states, and rates, initialized with the solution at the
54
first time point if `save_start = true` (the default).
55
"""
56
function solution_arrays(u::uType, tspan, rate_prototype;
338,711✔
57
                         timeseries_init = typeof(u)[],
58
                         ts_init = eltype(tspan)[],
59
                         save_idxs = nothing,
60
                         save_start = true) where {uType}
61
    # determine types of time and state
62
    # uType = typeof(u)
63
    tType = eltype(tspan)
112,904✔
64

65
    # initialize vector of saved time points
66
    ts = convert(Vector{tType}, ts_init)
112,904✔
67
    alg_choice = Int[]
112,904✔
68

69
    # initialize vector of saved states
70
    if save_idxs === nothing
112,904✔
71
        timeseries = convert(Vector{uType}, timeseries_init)
112,904✔
72
    else
73
        u_initial = u[save_idxs]
×
74
        timeseries = convert(Vector{typeof(u_initial)}, timeseries_init)
×
75
    end
76

77
    # save solution at initial time point
78
    if save_start
112,904✔
79
        saveiter = 1
112,904✔
80
        copyat_or_push!(ts, 1, first(tspan))
225,808✔
81
        if save_idxs === nothing
112,904✔
82
            copyat_or_push!(timeseries, 1, u)
112,906✔
83
        else
84
            copyat_or_push!(timeseries, 1, u_initial, Val{false})
×
85
        end
86
    else
87
        saveiter = 0
×
88
    end
89

90
    ts, timeseries, saveiter
112,904✔
91
end
92
"""
93
    sizehint!(sol::DESolution, n)
94

95
Suggest that solution `sol` reserves capacity for at least `n` elements.
96
"""
97
function Base.sizehint!(sol::RODESolution, n)
56,452✔
98
    sizehint!(sol.u, n)
56,452✔
99
    sizehint!(sol.t, n)
56,452✔
100

101
    nothing
×
102
end
103

104
"""
105
    sizehint!(sol::DESolution, alg, tspan, tstops, saveat; kwargs...)
106

107
Suggest that solution `sol` reserves capacity for a number of elements that
108
depends on the parameter settings of the numerical solver.
109
"""
110
function Base.sizehint!(sol::RODESolution, alg, tspan, tstops, saveat;
112,904✔
111
                        save_everystep = isempty(saveat),
112
                        adaptive = StochasticDiffEq.isadaptive(getalg(alg)),
113
                        internalnorm = DiffEqBase.ODE_DEFAULT_NORM,
114
                        dt = zero(eltype(tspan)))
115
    # obtain integration time
116
    t0 = first(tspan)
56,452✔
117
    integrationtime = last(tspan) - t0
56,452✔
118

119
    if !adaptive && save_everystep && tspan[2] - tspan[1] != Inf
56,452✔
120
        # determine number of steps if known a priori
121
        if iszero(dt)
56,421✔
122
            steps = length(tstops)
×
123
        else
124
            steps = ceil(Int, internalnorm(integrationtime / dt, t0))
56,421✔
125
        end
126

127
        sizehint!(sol, steps + 1)
56,421✔
128
    elseif save_everystep
31✔
129
        sizehint!(sol, 50)
31✔
130
    elseif !isempty(saveat)
×
131
        sizehint!(sol, length(saveat) + 1)
×
132
    else
133
        sizehint!(sol, 2)
×
134
    end
135

136
    nothing
×
137
end
138

139
function build_history_function(prob, alg, reltol, rate_prototype, noise_rate_prototype,
112,904✔
140
                                jump_prototype, W, _seed, dense;
141
                                dt = zero(eltype(prob.tspan)),
142
                                adaptive = StochasticDiffEq.isadaptive(getalg(alg)),
143
                                calck = false,
144
                                internalnorm = DiffEqBase.ODE_DEFAULT_NORM)
145
    @unpack f, g, h, u0, tspan, p = prob
56,452✔
146

147
    t0 = first(tspan)
56,452✔
148
    tType = eltype(tspan)
56,452✔
149
    tTypeNoUnits = typeof(one(tType))
56,452✔
150
    tdir = sign(last(tspan) - t0)
56,452✔
151

152
    uEltypeNoUnits = recursive_unitless_eltype(u0)
56,452✔
153
    uBottomEltypeNoUnits = recursive_unitless_bottom_eltype(u0)
56,452✔
154

155
    # bootstrap an SDE integrator
156
    # - whose solution captures the dense history of the simulation
157
    # - that is used for extrapolation of the history for time points past the
158
    #   already fixed history
159
    # - that is used for interpolation of the history for time points in the
160
    #   current integration step (so the interpolation is fixed while updating the stages)
161
    # we wrap the user-provided history function such that function calls during the setup
162
    # of the integrator do not fail
163
    # sde_f = SDEFunctionWrapper(f, prob.h)
164
    # sde_g = SDEDiffusionTermWrapper(g,prob.h)
165

166
    sde_f, sde_g = wrap_functions_and_history(f, g, h)
56,452✔
167
    sde_prob = SDEProblem{isinplace(prob)}(sde_f, sde_g, u0, tspan, p)
56,452✔
168

169
    # get states of ODE integrator (do not alias uprev)
170
    sde_u, sde_uprev = u_uprev(u0; alias_u0 = false)
56,452✔
171

172
    # # initialize output arrays
173
    sde_ts, sde_timeseries, sde_saveiter = solution_arrays(sde_u, tspan, rate_prototype,
112,903✔
174
                                                           save_idxs = nothing,
175
                                                           save_start = true)
176

177
    # # obtain cache (we alias uprev2 and uprev)
178
    sde_cache = StochasticDiffEq.alg_cache(getalg(alg), prob, sde_u, W.dW, W.dZ, p,
56,452✔
179
                                           rate_prototype, noise_rate_prototype,
180
                                           jump_prototype, uEltypeNoUnits,
181
                                           uBottomEltypeNoUnits, tTypeNoUnits, sde_uprev, f,
182
                                           t0, dt, Val{isinplace(prob)})
183

184
    # build dense interpolation of history
185
    id = StochasticDiffEq.LinearInterpolationData(sde_timeseries, sde_ts)
56,452✔
186
    if typeof(getalg(alg)) <: StochasticDiffEq.StochasticDiffEqCompositeAlgorithm
56,452✔
187
        alg_choice = Int[]
×
188
        sde_sol = DiffEqBase.build_solution(prob, alg, sde_ts, sde_timeseries, W = W,
×
189
                                            stats = DiffEqBase.Stats(0),
190
                                            calculate_error = false,
191
                                            alg_choice = alg_choice,
192
                                            interp = id, dense = dense, seed = _seed)
193
    else
194
        sde_sol = DiffEqBase.build_solution(prob, alg, sde_ts, sde_timeseries, W = W,
56,452✔
195
                                            stats = DiffEqBase.Stats(0),
196
                                            calculate_error = false,
197
                                            interp = id, dense = dense, seed = _seed)
198
    end
199

200
    # # reserve capacity
201
    sizehint!(sde_sol, getalg(alg), tspan, (), ();
56,452✔
202
              save_everystep = true, adaptive = adaptive, internalnorm = internalnorm,
203
              dt = tType(dt))
204

205
    # # create simple integrator
206
    sde_integrator = HistorySDEIntegrator{typeof(getalg(alg)), isinplace(prob),
56,452✔
207
                                          typeof(prob.u0),
208
                                          tType, typeof(sde_sol), typeof(sde_cache)}(sde_sol,
209
                                                                                     sde_u,
210
                                                                                     t0,
211
                                                                                     zero(tType),
212
                                                                                     sde_uprev,
213
                                                                                     t0,
214
                                                                                     getalg(alg),
215
                                                                                     zero(tType),
216
                                                                                     tdir,
217
                                                                                     1,
218
                                                                                     sde_cache)
219

220
    # # combine the user-provided history function and the ODE integrator with dense solution
221
    # # to a joint dense history of the DDE
222
    # # we use this history information to create a problem function of the DDE with all
223
    # # available history information that is of the form f(du,u,p,t) or f(u,p,t) such that
224
    # # SDE algorithms can be applied
225
    HistoryFunction(prob.h, sde_integrator)
56,452✔
226
end
227

228
"""
229
    initialize_solution!(integrator::SDDEIntegrator)
230

231
Initialize the solution of an integrator by adjusting the cache for composite algorithms.
232
"""
233
function initialize_solution!(integrator::SDDEIntegrator)
×
234
    if iscomposite(getalg(integrator.alg))
×
235
        copyat_or_push!(integrator.integrator.sol.alg_choice, 1, integrator.cache.current)
×
236
        if integrator.opts.save_start
×
237
            copyat_or_push!(integrator.sol.alg_choice, 1, integrator.cache.current)
×
238
        end
239
    end
240

241
    nothing
×
242
end
243

244
function StochasticDiffEq.OrdinaryDiffEq.nlsolve_f(integrator::SDDEIntegrator)
84,607,481✔
245
    StochasticDiffEq.OrdinaryDiffEq.nlsolve_f(integrator.f, unwrap_alg(integrator, true))
84,607,481✔
246
end
247

248
function unwrap_alg(integrator::SDDEIntegrator, is_stiff)
84,607,481✔
249
    alg = integrator.alg
84,607,481✔
250
    iscomp = alg isa StochasticDiffEq.StochasticCompositeAlgorithm
84,607,481✔
251
    if !iscomp
84,607,481✔
252
        return alg
84,607,481✔
253
    elseif alg.choice_function isa DiffEqBase.AutoSwitch
×
254
        num = is_stiff ? 2 : 1
×
255
        return alg.algs[num]
×
256
    else
257
        return alg.algs[integrator.cache.current]
×
258
    end
259
end
260

261
function DiffEqBase.unwrap_cache(integrator::SDDEIntegrator, is_stiff)
×
262
    alg = getalg(integrator.alg)
×
263
    cache = integrator.cache
×
264
    iscomp = alg isa StochasticDiffEq.StochasticCompositeAlgorithm
×
265
    if !iscomp
×
266
        return cache
×
267
    elseif alg.choice_function isa DiffEqBase.AutoSwitch
×
268
        num = is_stiff ? 2 : 1
×
269
        return cache.caches[num]
×
270
    else
271
        return cache.caches[integrator.cache.current]
×
272
    end
273
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