Skip to content

Commit 839b53f

Browse files
Merge pull request #42 from ranocha/hr/controllers
adapt to new controller infrastructure
2 parents a4dafa8 + acccbdd commit 839b53f

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

src/StochasticDelayDiffEq.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module StochasticDelayDiffEq
66
# using DifferentialEquations
77
using Reexport
88
@reexport using StochasticDiffEq
9+
import StochasticDiffEq: stepsize_controller!, accept_step_controller, step_accept_controller!, step_reject_controller!, PIController
910
# import StochasticDiffEq: calc_J, calc_J!, calc_tderivative!, calc_tderivative
1011
using LinearAlgebra, StaticArrays
1112
using UnPack, DataStructures
@@ -33,5 +34,6 @@ include("integrators/utils.jl")
3334
include("functionwrapper.jl")
3435
include("utils.jl")
3536
include("solve.jl")
37+
include("stepsize_controllers.jl")
3638

3739
end # module

src/integrators/interface.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
if integrator.isout
1212
integrator.dtnew = integrator.dt*integrator.opts.qmin
1313
elseif !integrator.force_stepfail
14-
integrator.dtnew = integrator.dt/min(inv(integrator.opts.qmin),integrator.q11/integrator.opts.gamma)
14+
step_reject_controller!(integrator,getalg(integrator.alg))
1515
end
1616
StochasticDiffEq.choose_algorithm!(integrator,integrator.cache)
1717
StochasticDiffEq.fix_dtnew_at_bounds!(integrator)
@@ -37,13 +37,11 @@
3737
integrator.last_stepfail = true
3838
integrator.accept_step = false
3939
elseif integrator.opts.adaptive
40-
@fastmath integrator.q11 = integrator.EEst^integrator.opts.beta1
41-
@fastmath integrator.q = integrator.q11/(integrator.qold^integrator.opts.beta2)
42-
@fastmath integrator.q = max(inv(integrator.opts.qmax),min(inv(integrator.opts.qmin),integrator.q/integrator.opts.gamma))
43-
@fastmath integrator.dtnew = integrator.dt/integrator.q
40+
stepsize_controller!(integrator,getalg(integrator.alg))
4441
integrator.isout = integrator.opts.isoutofdomain(integrator.u,integrator.p,ttmp)
45-
integrator.accept_step = (!integrator.isout && integrator.EEst <= 1.0) || (integrator.opts.force_dtmin && integrator.dt <= integrator.opts.dtmin)
42+
integrator.accept_step = (!integrator.isout && accept_step_controller(integrator, integrator.opts.controller)) || (integrator.opts.force_dtmin && integrator.dt <= integrator.opts.dtmin)
4643
if integrator.accept_step # Accepted
44+
step_accept_controller!(integrator,getalg(integrator.alg))
4745
integrator.last_stepfail = false
4846
integrator.tprev = integrator.t
4947
if typeof(integrator.t)<:AbstractFloat && !isempty(integrator.opts.tstops)

src/solve.jl

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ function DiffEqBase.__init(prob::AbstractSDDEProblem,# TODO DiffEqBasee.Abstract
3232
reltol = nothing,
3333
qmax = StochasticDiffEq.qmax_default(getalg(alg)),
3434
qmin = StochasticDiffEq.qmin_default(getalg(alg)),
35+
qsteady_min = StochasticDiffEq.qsteady_min_default(alg),
36+
qsteady_max = StochasticDiffEq.qsteady_max_default(alg),
3537
qoldinit = 1 // 10^4, fullnormalize = true,
38+
controller = nothing,
3639
failfactor = 2,
37-
beta2 = StochasticDiffEq.beta2_default(getalg(alg)),
38-
beta1 = StochasticDiffEq.beta1_default(getalg(alg), beta2),
40+
beta2 = nothing,
41+
beta1 = nothing,
3942
delta = StochasticDiffEq.delta_default(getalg(alg)),
4043
maxiters = adaptive ? 1000000 : typemax(Int),
4144
dtmax = eltype(prob.tspan)((prob.tspan[end] - prob.tspan[1])),
@@ -334,22 +337,41 @@ function DiffEqBase.__init(prob::AbstractSDDEProblem,# TODO DiffEqBasee.Abstract
334337
save_end_user = save_end
335338
save_end = save_end === nothing ? save_everystep || isempty(saveat) || saveat isa Number || prob.tspan[2] in saveat : save_end
336339

340+
# Setting up the step size controller
341+
if (beta1 !== nothing || beta2 !== nothing) && controller !== nothing
342+
throw(ArgumentError(
343+
"Setting both the legacy PID parameters `beta1, beta2 = $((beta1, beta2))` and the `controller = $controller` is not allowed."))
344+
end
345+
346+
if (beta1 !== nothing || beta2 !== nothing)
347+
message = "Providing the legacy PID parameters `beta1, beta2` is deprecated. Use the keyword argument `controller` instead."
348+
Base.depwarn(message, :init)
349+
Base.depwarn(message, :solve)
350+
end
351+
352+
if controller === nothing
353+
controller = StochasticDiffEq.default_controller(getalg(alg), cache, qoldinit, beta1, beta2)
354+
end
355+
337356
opts = StochasticDiffEq.SDEOptions(maxiters, save_everystep,
338357
adaptive, abstol_internal,
339-
reltol_internal, QT(gamma),
358+
reltol_internal,
359+
QT(gamma),
340360
QT(qmax), QT(qmin),
361+
QT(qsteady_max),QT(qsteady_min),
362+
QT(qoldinit),
341363
QT(failfactor),
342-
tType(dtmax), tType(dtmin), internalnorm, save_idxs,
364+
tType(dtmax), tType(dtmin),
365+
controller,
366+
internalnorm, save_idxs,
343367
tstops_internal, saveat_internal,
344368
d_discontinuities_internal,
345369
tstops, saveat, d_discontinuities,
346370
userdata,
347371
progress, progress_steps,
348372
progress_name, progress_message,
349373
timeseries_errors, dense_errors,
350-
QT(beta1), QT(beta2),
351374
convert.(uBottomEltypeNoUnits, delta),
352-
QT(qoldinit),
353375
dense, save_on, save_start, save_end, save_end_user, save_noise,
354376
callbacks_internal, isoutofdomain, unstable_check,
355377
verbose, calck, force_dtmin,

src/stepsize_controllers.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
function stepsize_controller!(integrator::SDDEIntegrator, controller::PIController, alg)
3+
integrator.q11 = DiffEqBase.value(DiffEqBase.fastpow(integrator.EEst,controller.beta1))
4+
integrator.q = DiffEqBase.value(integrator.q11/DiffEqBase.fastpow(integrator.qold,controller.beta2))
5+
@fastmath integrator.q = DiffEqBase.value(max(inv(integrator.opts.qmax),min(inv(integrator.opts.qmin),integrator.q/integrator.opts.gamma)))
6+
end
7+
8+
@inline function step_accept_controller!(integrator::SDDEIntegrator, alg)
9+
step_accept_controller!(integrator, integrator.opts.controller, alg)
10+
end
11+
12+
function step_accept_controller!(integrator::SDDEIntegrator, controller::PIController, alg)
13+
integrator.dtnew = DiffEqBase.value(integrator.dt/integrator.q) * oneunit(integrator.dt)
14+
end
15+
16+
function step_reject_controller!(integrator::SDDEIntegrator, controller::PIController, alg)
17+
integrator.dtnew = integrator.dt/min(inv(integrator.opts.qmin),integrator.q11/integrator.opts.gamma)
18+
end

0 commit comments

Comments
 (0)