Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 57 additions & 24 deletions lib/OrdinaryDiffEqCore/src/alg_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,30 +380,26 @@ function default_controller_v7(QT, alg)
end

function default_controller_v7(QT, alg::OrdinaryDiffEqCompositeAlgorithm)
return nothing # This forces a fall-back to the legacy implementation
# beta2 = convert(QT, beta2_default(alg.algs[1]))
# beta1 = convert(QT, beta1_default(alg.algs[1], beta2))
# return PIController(beta1, beta2)
# TODO Uncomment this code below to when removing the legacy controllers on OrdinaryDiffEq v7.
# return CompositeController(
# __default_controller_v7(QT, alg.algs)
# )
end

# @generated function __default_controller_v7(
# QT, algs::T
# ) where {
# T <: Tuple
# }
# return Expr(
# :tuple,
# map(1:length(T.types)) do i
# :(
# default_controller_v7(QT, algs[$i])
# )
# end...
# )
# end
# Use CompositeController for type stability with composite algorithms
return CompositeController(
_default_controller_v7_tuple(QT, alg.algs)
)
end

@generated function _default_controller_v7_tuple(
QT, algs::T
) where {
T <: Tuple
}
return Expr(
:tuple,
map(1:length(T.types)) do i
:(
default_controller_v7(QT, algs[$i])
)
end...
)
end

function legacy_default_controller(alg, cache, qoldinit, _beta1 = nothing, _beta2 = nothing)
if ispredictive(alg)
Expand All @@ -420,6 +416,43 @@ end
# TODO remove this when done
default_controller(args...) = legacy_default_controller(args...)

"""
new_controller_from_legacy_params(QT, alg, beta1, beta2, qmin, qmax, gamma, qsteady_min, qsteady_max, qoldinit)

Create a new-style controller from legacy parameters. This is used for type stability
when the user provides legacy controller parameters instead of a controller object.
"""
function new_controller_from_legacy_params(QT, alg, beta1, beta2, qmin, qmax, gamma, qsteady_min, qsteady_max, qoldinit)
if ispredictive(alg)
return NewPredictiveController(QT, alg;
qmin = qmin,
qmax = qmax,
gamma = gamma,
qsteady_min = qsteady_min,
qsteady_max = qsteady_max
)
elseif isstandard(alg)
return NewIController(QT, alg;
qmin = qmin,
qmax = qmax,
gamma = gamma,
qsteady_min = qsteady_min,
qsteady_max = qsteady_max
)
else # Default is PI-controller
return NewPIController(QT, alg;
beta1 = beta1,
beta2 = beta2,
qmin = qmin,
qmax = qmax,
gamma = gamma,
qsteady_min = qsteady_min,
qsteady_max = qsteady_max,
qoldinit = qoldinit
)
end
end

function _digest_beta1_beta2(alg, cache, ::Val{QT}, _beta1, _beta2) where {QT}
if alg isa OrdinaryDiffEqCompositeAlgorithm
beta2 = _beta2 === nothing ?
Expand Down
7 changes: 4 additions & 3 deletions lib/OrdinaryDiffEqCore/src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function SciMLBase.__init(
isoutofdomain = ODE_DEFAULT_ISOUTOFDOMAIN,
unstable_check = ODE_DEFAULT_UNSTABLE_CHECK,
verbose = Standard(),
controller = any((gamma, qmin, qmax, qsteady_min, qsteady_max, beta1, beta2, qoldinit) .!== nothing) ? nothing : default_controller_v7(determine_controller_datatype(prob.u0, internalnorm, prob.tspan), alg), # We have to reconstruct the old controller before breaking release.,
controller = default_controller_v7(determine_controller_datatype(prob.u0, internalnorm, prob.tspan), alg),
timeseries_errors = true,
dense_errors = false,
advance_to_tstop = false,
Expand Down Expand Up @@ -503,7 +503,9 @@ function SciMLBase.__init(

# The following code provides an upgrade path for users by preserving the old behavior.
legacy_controller_parameters = (gamma, qmin, qmax, qsteady_min, qsteady_max, beta1, beta2, qoldinit)
if controller === nothing # We have to reconstruct the old controller before breaking release.
if controller === nothing # Fallback for composite algorithms or explicit controller=nothing
# Use the new controller constructor with legacy parameters for type stability
controller = new_controller_from_legacy_params(QT, _alg, beta1, beta2, qmin, qmax, gamma, qsteady_min, qsteady_max, qoldinit)
if any(legacy_controller_parameters .== nothing)
gamma = convert(QT, gamma === nothing ? gamma_default(alg) : gamma)
qmin = convert(QT, qmin === nothing ? qmin_default(alg) : qmin)
Expand All @@ -512,7 +514,6 @@ function SciMLBase.__init(
qsteady_max = convert(QT, qsteady_max === nothing ? qsteady_max_default(alg) : qsteady_max)
qoldinit = convert(QT, qoldinit === nothing ? (anyadaptive(alg) ? 1 // 10^4 : 0) : qoldinit)
end
controller = legacy_default_controller(_alg, cache, qoldinit, beta1, beta2)
else # Controller has been passed
gamma = hasfield(typeof(controller), :gamma) ? controller.gamma : gamma_default(alg)
qmin = hasfield(typeof(controller), :qmin) ? controller.qmin : qmin_default(alg)
Expand Down
Loading