Skip to content
Merged
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
162 changes: 162 additions & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,165 @@ macro mtkbuild(exprs...)
@mtkcompile $(exprs...)
end |> esc
end

for T in [:ODESystem, :NonlinearSystem, :DiscreteSystem, :ImplicitDiscreteSystem]
@eval @deprecate $T(args...; kwargs...) System(args...; kwargs...)
end

for T in [:ODEProblem, :DDEProblem, :SDEProblem, :SDDEProblem, :DAEProblem,
:BVProblem, :DiscreteProblem, :ImplicitDiscreteProblem]
for (pType, pCanonical) in [
(AbstractDict, :p),
(AbstractArray{<:Pair}, :(Dict(p))),
(AbstractArray, :(isempty(p) ? Dict() : Dict(parameters(sys) .=> p)))
],
(uType, uCanonical) in [
(Nothing, :(Dict())),
(AbstractDict, :u0),
(AbstractArray{<:Pair}, :(Dict(u0))),
(AbstractArray, :(isempty(u0) ? Dict() : Dict(unknowns(sys) .=> u0)))
]

@eval function SciMLBase.$T(sys::System, u0::$uType, tspan, p::$pType; kw...)
ctor = string($T)
uCan = string($(QuoteNode(uCanonical)))
pCan = string($(QuoteNode(pCanonical)))
@warn """
`$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use
`$ctor(sys, merge($uCan, $pCan), tspan)` instead.
"""
SciMLBase.$T(sys, merge($uCanonical, $pCanonical), tspan; kw...)
end
@eval function SciMLBase.$T{iip}(
sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip}
ctor = string($T{iip})
uCan = string($(QuoteNode(uCanonical)))
pCan = string($(QuoteNode(pCanonical)))
@warn """
`$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use
`$ctor(sys, merge($uCan, $pCan), tspan)` instead.
"""
return SciMLBase.$T{iip}(sys, merge($uCanonical, $pCanonical), tspan; kw...)
end
@eval function SciMLBase.$T{iip, spec}(
sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip, spec}
ctor = string($T{iip, spec})
uCan = string($(QuoteNode(uCanonical)))
pCan = string($(QuoteNode(pCanonical)))
@warn """
`$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use
`$ctor(sys, merge($uCan, $pCan), tspan)` instead.
"""
return $T{iip, spec}(sys, merge($uCanonical, $pCanonical), tspan; kw...)
end
end

for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing]
@eval function SciMLBase.$T(sys::System, u0::$uType, tspan, p::$pType; kw...)
ctor = string($T)
pT = string($(QuoteNode(pType)))
@warn """
`$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use
`$ctor(sys, u0, tspan)` instead.
"""
$T(sys, u0, tspan; kw...)
end
@eval function SciMLBase.$T{iip}(
sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip}
ctor = string($T{iip})
pT = string($(QuoteNode(pType)))
@warn """
`$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use
`$ctor(sys, u0, tspan)` instead.
"""
return $T{iip}(sys, u0, tspan; kw...)
end
@eval function SciMLBase.$T{iip, spec}(
sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip, spec}
ctor = string($T{iip, spec})
pT = string($(QuoteNode(pType)))
@warn """
`$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use
`$ctor(sys, u0, tspan)` instead.
"""
return $T{iip, spec}(sys, u0, tspan; kw...)
end
end
end

for T in [:NonlinearProblem, :NonlinearLeastSquaresProblem,
:SCCNonlinearProblem, :OptimizationProblem, :SteadyStateProblem]
for (pType, pCanonical) in [
(AbstractDict, :p),
(AbstractArray{<:Pair}, :(Dict(p))),
(AbstractArray, :(isempty(p) ? Dict() : Dict(parameters(sys) .=> p)))
],
(uType, uCanonical) in [
(Nothing, :(Dict())),
(AbstractDict, :u0),
(AbstractArray{<:Pair}, :(Dict(u0))),
(AbstractArray, :(isempty(u0) ? Dict() : Dict(unknowns(sys) .=> u0)))
]

@eval function SciMLBase.$T(sys::System, u0::$uType, p::$pType; kw...)
ctor = string($T)
uCan = string($(QuoteNode(uCanonical)))
pCan = string($(QuoteNode(pCanonical)))
@warn """
`$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))`
instead.
"""
$T(sys, merge($uCanonical, $pCanonical); kw...)
end
@eval function SciMLBase.$T{iip}(
sys::System, u0::$uType, p::$pType; kw...) where {iip}
ctor = string($T{iip})
uCan = string($(QuoteNode(uCanonical)))
pCan = string($(QuoteNode(pCanonical)))
@warn """
`$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))`
instead.
"""
return $T{iip}(sys, merge($uCanonical, $pCanonical); kw...)
end
@eval function SciMLBase.$T{iip, spec}(
sys::System, u0::$uType, p::$pType; kw...) where {iip, spec}
ctor = string($T{iip, spec})
uCan = string($(QuoteNode(uCanonical)))
pCan = string($(QuoteNode(pCanonical)))
@warn """
`$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))`
instead.
"""
return $T{iip, spec}(sys, merge($uCanonical, $pCanonical); kw...)
end
end
for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing]
@eval function SciMLBase.$T(sys::System, u0::$uType, p::$pType; kw...)
ctor = string($T)
pT = string($(QuoteNode(pType)))
@warn """
`$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead
"""
$T(sys, u0; kw...)
end
@eval function SciMLBase.$T{iip}(
sys::System, u0::$uType, p::$pType; kw...) where {iip}
ctor = string($T{iip})
pT = string($(QuoteNode(pType)))
@warn """
`$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead
"""
return $T{iip}(sys, u0; kw...)
end
@eval function SciMLBase.$T{iip, spec}(
sys::System, u0::$uType, p::$pType; kw...) where {iip, spec}
ctor = string($T{iip, spec})
pT = string($(QuoteNode(pType)))
@warn """
`$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead
"""
return $T{iip, spec}(sys, u0; kw...)
end
end
end
2 changes: 1 addition & 1 deletion src/discretedomain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function (xn::Num)(k::ShiftIndex)
@unpack clock, steps = k
x = value(xn)
# Verify that the independent variables of k and x match and that the expression doesn't have multiple variables
vars = Symbolics.get_variables(x)
vars = ModelingToolkit.vars(x)
if length(vars) != 1
error("Cannot shift a multivariate expression $x. Either create a new unknown and shift this, or shift the individual variables in the expression.")
end
Expand Down
3 changes: 2 additions & 1 deletion src/problems/bvproblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
check_compatibility && check_compatible_system(BVProblem, sys)
isnothing(callback) || error("BVP solvers do not support callbacks.")

dvs = unknowns(sys)
op = to_varmap(op, dvs)
# Systems without algebraic equations should use both fixed values + guesses
# for initialization.
_op = has_alg_eqs(sys) ? op : merge(Dict(op), Dict(guesses))
Expand All @@ -17,7 +19,6 @@
t = tspan !== nothing ? tspan[1] : tspan, check_compatibility = false, cse,
checkbounds, time_dependent_init = false, expression, kwargs...)

dvs = unknowns(sys)
stidxmap = Dict([v => i for (i, v) in enumerate(dvs)])
u0_idxs = has_alg_eqs(sys) ? collect(1:length(dvs)) :
[stidxmap[k] for (k, v) in op if haskey(stidxmap, k)]
Expand Down
4 changes: 2 additions & 2 deletions src/systems/nonlinear/initializesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function generate_initializesystem_timevarying(sys::AbstractSystem;
for k in keys(defs)
defs[k] = substitute(defs[k], paramsubs)
end
isys = System(eqs_ics,
isys = System(Vector{Equation}(eqs_ics),
vars,
pars;
defaults = defs,
Expand Down Expand Up @@ -280,7 +280,7 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem;
for k in keys(defs)
defs[k] = substitute(defs[k], paramsubs)
end
isys = System(eqs_ics,
isys = System(Vector{Equation}(eqs_ics),
vars,
pars;
defaults = defs,
Expand Down
9 changes: 4 additions & 5 deletions src/systems/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,11 @@ function System(eqs::Vector{Equation}; kwargs...)
for ssys in get(kwargs, :systems, System[])
collect_scoped_vars!(allunknowns, ps, ssys, nothing)
end
costs = get(kwargs, :costs, nothing)
if costs !== nothing
costunknowns, costps = process_costs(costs, allunknowns, ps, nothing)
union!(allunknowns, costunknowns)
union!(ps, costps)
costs = get(kwargs, :costs, [])
for val in costs
collect_vars!(allunknowns, ps, val, nothing)
end

cstrs = Vector{Union{Equation, Inequality}}(get(kwargs, :constraints, []))
for eq in cstrs
collect_vars!(allunknowns, ps, eq, nothing)
Expand Down
90 changes: 86 additions & 4 deletions test/sciml_problem_inputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Fetch packages
using ModelingToolkit, JumpProcesses, NonlinearSolve, OrdinaryDiffEq, StaticArrays,
SteadyStateDiffEq, StochasticDiffEq, Test
SteadyStateDiffEq, StochasticDiffEq, SciMLBase, Test
using ModelingToolkit: t_nounits as t, D_nounits as D

# Sets rnd number.
Expand Down Expand Up @@ -101,7 +101,7 @@ begin
end

# Perform ODE simulations (singular and ensemble).
let
@testset "ODE" begin
# Creates normal and ensemble problems.
base_oprob = ODEProblem(osys, [u0_alts[1]; p_alts[1]], tspan)
base_sol = solve(base_oprob, Tsit5(); saveat = 1.0)
Expand All @@ -119,7 +119,7 @@ let
end

# Solves a nonlinear problem (EnsembleProblems are not possible for these).
let
@testset "Nonlinear" begin
base_nlprob = NonlinearProblem(nsys, [u0_alts[1]; p_alts[1]])
base_sol = solve(base_nlprob, NewtonRaphson())
# Solves problems for all input types, checking that identical solutions are found.
Expand All @@ -130,7 +130,7 @@ let
end

# Perform steady state simulations (singular and ensemble).
let
@testset "SteadyState" begin
# Creates normal and ensemble problems.
base_ssprob = SteadyStateProblem(osys, [u0_alts[1]; p_alts[1]])
base_sol = solve(base_ssprob, DynamicSS(Tsit5()))
Expand All @@ -146,3 +146,85 @@ let
@test base_esol == solve(eprob, DynamicSS(Tsit5()); trajectories = 2)
end
end

@testset "Deprecations" begin
@variables _x(..) = 1.0
@parameters p = 1.0
@brownian a
x = _x(t)
k = ShiftIndex(t)

@test_deprecated ODESystem([D(x) ~ x * p], t; name = :a)
@mtkcompile odesys = System([D(x) ~ x * p], t)
@mtkcompile sdesys = System([D(x) ~ x * p + a], t)
@test_deprecated NonlinearSystem([0 ~ x^3 + p]; name = :a)
@mtkcompile nlsys = System([0 ~ x^3 + p])
@mtkcompile ddesys = System([D(x) ~ x * p + _x(t - 0.1)], t)
@mtkcompile sddesys = System([D(x) ~ x * p + _x(t - 0.1) + a], t)
@test_deprecated DiscreteSystem([x ~ x(k - 1) + x(k - 2)], t; name = :a)
@mtkcompile discsys = System([x ~ x(k - 1) * p], t)
@test_deprecated ImplicitDiscreteSystem([x ~ x(k - 1) + x(k - 2) * p * x], t; name = :a)
@mtkcompile idiscsys = System([x ~ x(k - 1) * p * x], t)
@mtkcompile optsys = OptimizationSystem(x^2 + p)

u0s = [
Dict(x => 1.0),
[x => 1.0],
[1.0],
[],
nothing
]
ps = [
Dict(p => 1.0),
[p => 1.0],
[1.0],
[],
nothing,
SciMLBase.NullParameters()
]
tspan = (0.0, 1.0)

@testset "$ctor" for (sys, ctor) in [
(odesys, ODEProblem),
(odesys, ODEProblem{true}),
(odesys, ODEProblem{true, SciMLBase.FullSpecialize}), (odesys, BVProblem),
(odesys, BVProblem{true}),
(odesys, BVProblem{true, SciMLBase.FullSpecialize}), (sdesys, SDEProblem),
(sdesys, SDEProblem{true}),
(sdesys, SDEProblem{true, SciMLBase.FullSpecialize}), (ddesys, DDEProblem),
(ddesys, DDEProblem{true}),
(ddesys, DDEProblem{true, SciMLBase.FullSpecialize}), (sddesys, SDDEProblem),
(sddesys, SDDEProblem{true}),
(sddesys, SDDEProblem{true, SciMLBase.FullSpecialize}),

# (discsys, DiscreteProblem),
# (discsys, DiscreteProblem{true}),
# (discsys, DiscreteProblem{true, SciMLBase.FullSpecialize}),

(idiscsys, ImplicitDiscreteProblem),
(idiscsys, ImplicitDiscreteProblem{true}),
(idiscsys, ImplicitDiscreteProblem{true, SciMLBase.FullSpecialize})
]
@testset "$(typeof(u0)) - $(typeof(p))" for u0 in u0s, p in ps
if u0 isa Vector{Float64} && ctor <: ImplicitDiscreteProblem
u0 = ones(2)
end
@test_warn ["deprecated"] ctor(sys, u0, tspan, p)
end
end
@testset "$ctor" for (sys, ctor) in [
(nlsys, NonlinearProblem),
(nlsys, NonlinearProblem{true}),
(nlsys, NonlinearProblem{true, SciMLBase.FullSpecialize}), (
nlsys, NonlinearLeastSquaresProblem),
(nlsys, NonlinearLeastSquaresProblem{true}),
(nlsys, NonlinearLeastSquaresProblem{true, SciMLBase.FullSpecialize}), (
nlsys, SCCNonlinearProblem),
(nlsys, SCCNonlinearProblem{true}), (optsys, OptimizationProblem),
(optsys, OptimizationProblem{true})
]
@testset "$(typeof(u0)) - $(typeof(p))" for u0 in u0s, p in ps
@test_warn ["deprecated"] ctor(sys, u0, p)
end
end
end
Loading