Skip to content

Commit 368d2f7

Browse files
Merge pull request #3682 from AayushSabharwal/as/fallbacks
refactor: add depwarns for old system and problem constructors
2 parents eb1177f + 1d446ca commit 368d2f7

File tree

6 files changed

+257
-13
lines changed

6 files changed

+257
-13
lines changed

src/deprecations.jl

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,165 @@ macro mtkbuild(exprs...)
88
@mtkcompile $(exprs...)
99
end |> esc
1010
end
11+
12+
for T in [:ODESystem, :NonlinearSystem, :DiscreteSystem, :ImplicitDiscreteSystem]
13+
@eval @deprecate $T(args...; kwargs...) System(args...; kwargs...)
14+
end
15+
16+
for T in [:ODEProblem, :DDEProblem, :SDEProblem, :SDDEProblem, :DAEProblem,
17+
:BVProblem, :DiscreteProblem, :ImplicitDiscreteProblem]
18+
for (pType, pCanonical) in [
19+
(AbstractDict, :p),
20+
(AbstractArray{<:Pair}, :(Dict(p))),
21+
(AbstractArray, :(isempty(p) ? Dict() : Dict(parameters(sys) .=> p)))
22+
],
23+
(uType, uCanonical) in [
24+
(Nothing, :(Dict())),
25+
(AbstractDict, :u0),
26+
(AbstractArray{<:Pair}, :(Dict(u0))),
27+
(AbstractArray, :(isempty(u0) ? Dict() : Dict(unknowns(sys) .=> u0)))
28+
]
29+
30+
@eval function SciMLBase.$T(sys::System, u0::$uType, tspan, p::$pType; kw...)
31+
ctor = string($T)
32+
uCan = string($(QuoteNode(uCanonical)))
33+
pCan = string($(QuoteNode(pCanonical)))
34+
@warn """
35+
`$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use
36+
`$ctor(sys, merge($uCan, $pCan), tspan)` instead.
37+
"""
38+
SciMLBase.$T(sys, merge($uCanonical, $pCanonical), tspan; kw...)
39+
end
40+
@eval function SciMLBase.$T{iip}(
41+
sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip}
42+
ctor = string($T{iip})
43+
uCan = string($(QuoteNode(uCanonical)))
44+
pCan = string($(QuoteNode(pCanonical)))
45+
@warn """
46+
`$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use
47+
`$ctor(sys, merge($uCan, $pCan), tspan)` instead.
48+
"""
49+
return SciMLBase.$T{iip}(sys, merge($uCanonical, $pCanonical), tspan; kw...)
50+
end
51+
@eval function SciMLBase.$T{iip, spec}(
52+
sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip, spec}
53+
ctor = string($T{iip, spec})
54+
uCan = string($(QuoteNode(uCanonical)))
55+
pCan = string($(QuoteNode(pCanonical)))
56+
@warn """
57+
`$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use
58+
`$ctor(sys, merge($uCan, $pCan), tspan)` instead.
59+
"""
60+
return $T{iip, spec}(sys, merge($uCanonical, $pCanonical), tspan; kw...)
61+
end
62+
end
63+
64+
for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing]
65+
@eval function SciMLBase.$T(sys::System, u0::$uType, tspan, p::$pType; kw...)
66+
ctor = string($T)
67+
pT = string($(QuoteNode(pType)))
68+
@warn """
69+
`$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use
70+
`$ctor(sys, u0, tspan)` instead.
71+
"""
72+
$T(sys, u0, tspan; kw...)
73+
end
74+
@eval function SciMLBase.$T{iip}(
75+
sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip}
76+
ctor = string($T{iip})
77+
pT = string($(QuoteNode(pType)))
78+
@warn """
79+
`$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use
80+
`$ctor(sys, u0, tspan)` instead.
81+
"""
82+
return $T{iip}(sys, u0, tspan; kw...)
83+
end
84+
@eval function SciMLBase.$T{iip, spec}(
85+
sys::System, u0::$uType, tspan, p::$pType; kw...) where {iip, spec}
86+
ctor = string($T{iip, spec})
87+
pT = string($(QuoteNode(pType)))
88+
@warn """
89+
`$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use
90+
`$ctor(sys, u0, tspan)` instead.
91+
"""
92+
return $T{iip, spec}(sys, u0, tspan; kw...)
93+
end
94+
end
95+
end
96+
97+
for T in [:NonlinearProblem, :NonlinearLeastSquaresProblem,
98+
:SCCNonlinearProblem, :OptimizationProblem, :SteadyStateProblem]
99+
for (pType, pCanonical) in [
100+
(AbstractDict, :p),
101+
(AbstractArray{<:Pair}, :(Dict(p))),
102+
(AbstractArray, :(isempty(p) ? Dict() : Dict(parameters(sys) .=> p)))
103+
],
104+
(uType, uCanonical) in [
105+
(Nothing, :(Dict())),
106+
(AbstractDict, :u0),
107+
(AbstractArray{<:Pair}, :(Dict(u0))),
108+
(AbstractArray, :(isempty(u0) ? Dict() : Dict(unknowns(sys) .=> u0)))
109+
]
110+
111+
@eval function SciMLBase.$T(sys::System, u0::$uType, p::$pType; kw...)
112+
ctor = string($T)
113+
uCan = string($(QuoteNode(uCanonical)))
114+
pCan = string($(QuoteNode(pCanonical)))
115+
@warn """
116+
`$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))`
117+
instead.
118+
"""
119+
$T(sys, merge($uCanonical, $pCanonical); kw...)
120+
end
121+
@eval function SciMLBase.$T{iip}(
122+
sys::System, u0::$uType, p::$pType; kw...) where {iip}
123+
ctor = string($T{iip})
124+
uCan = string($(QuoteNode(uCanonical)))
125+
pCan = string($(QuoteNode(pCanonical)))
126+
@warn """
127+
`$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))`
128+
instead.
129+
"""
130+
return $T{iip}(sys, merge($uCanonical, $pCanonical); kw...)
131+
end
132+
@eval function SciMLBase.$T{iip, spec}(
133+
sys::System, u0::$uType, p::$pType; kw...) where {iip, spec}
134+
ctor = string($T{iip, spec})
135+
uCan = string($(QuoteNode(uCanonical)))
136+
pCan = string($(QuoteNode(pCanonical)))
137+
@warn """
138+
`$ctor(sys, u0, p; kw...)` is deprecated. Use `$ctor(sys, merge($uCan, $pCan))`
139+
instead.
140+
"""
141+
return $T{iip, spec}(sys, merge($uCanonical, $pCanonical); kw...)
142+
end
143+
end
144+
for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing]
145+
@eval function SciMLBase.$T(sys::System, u0::$uType, p::$pType; kw...)
146+
ctor = string($T)
147+
pT = string($(QuoteNode(pType)))
148+
@warn """
149+
`$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead
150+
"""
151+
$T(sys, u0; kw...)
152+
end
153+
@eval function SciMLBase.$T{iip}(
154+
sys::System, u0::$uType, p::$pType; kw...) where {iip}
155+
ctor = string($T{iip})
156+
pT = string($(QuoteNode(pType)))
157+
@warn """
158+
`$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead
159+
"""
160+
return $T{iip}(sys, u0; kw...)
161+
end
162+
@eval function SciMLBase.$T{iip, spec}(
163+
sys::System, u0::$uType, p::$pType; kw...) where {iip, spec}
164+
ctor = string($T{iip, spec})
165+
pT = string($(QuoteNode(pType)))
166+
@warn """
167+
`$ctor(sys, u0, p::$pT; kw...)` is deprecated. Use `$ctor(sys, u0)` instead
168+
"""
169+
return $T{iip, spec}(sys, u0; kw...)
170+
end
171+
end
172+
end

src/discretedomain.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function (xn::Num)(k::ShiftIndex)
231231
@unpack clock, steps = k
232232
x = value(xn)
233233
# Verify that the independent variables of k and x match and that the expression doesn't have multiple variables
234-
vars = Symbolics.get_variables(x)
234+
vars = ModelingToolkit.vars(x)
235235
if length(vars) != 1
236236
error("Cannot shift a multivariate expression $x. Either create a new unknown and shift this, or shift the individual variables in the expression.")
237237
end

src/problems/bvproblem.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
check_compatibility && check_compatible_system(BVProblem, sys)
99
isnothing(callback) || error("BVP solvers do not support callbacks.")
1010

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

20-
dvs = unknowns(sys)
2122
stidxmap = Dict([v => i for (i, v) in enumerate(dvs)])
2223
u0_idxs = has_alg_eqs(sys) ? collect(1:length(dvs)) :
2324
[stidxmap[k] for (k, v) in op if haskey(stidxmap, k)]

src/systems/nonlinear/initializesystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function generate_initializesystem_timevarying(sys::AbstractSystem;
167167
for k in keys(defs)
168168
defs[k] = substitute(defs[k], paramsubs)
169169
end
170-
isys = System(eqs_ics,
170+
isys = System(Vector{Equation}(eqs_ics),
171171
vars,
172172
pars;
173173
defaults = defs,
@@ -280,7 +280,7 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem;
280280
for k in keys(defs)
281281
defs[k] = substitute(defs[k], paramsubs)
282282
end
283-
isys = System(eqs_ics,
283+
isys = System(Vector{Equation}(eqs_ics),
284284
vars,
285285
pars;
286286
defaults = defs,

src/systems/system.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,11 @@ function System(eqs::Vector{Equation}; kwargs...)
543543
for ssys in get(kwargs, :systems, System[])
544544
collect_scoped_vars!(allunknowns, ps, ssys, nothing)
545545
end
546-
costs = get(kwargs, :costs, nothing)
547-
if costs !== nothing
548-
costunknowns, costps = process_costs(costs, allunknowns, ps, nothing)
549-
union!(allunknowns, costunknowns)
550-
union!(ps, costps)
546+
costs = get(kwargs, :costs, [])
547+
for val in costs
548+
collect_vars!(allunknowns, ps, val, nothing)
551549
end
550+
552551
cstrs = Vector{Union{Equation, Inequality}}(get(kwargs, :constraints, []))
553552
for eq in cstrs
554553
collect_vars!(allunknowns, ps, eq, nothing)

test/sciml_problem_inputs.jl

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Fetch packages
44
using ModelingToolkit, JumpProcesses, NonlinearSolve, OrdinaryDiffEq, StaticArrays,
5-
SteadyStateDiffEq, StochasticDiffEq, Test
5+
SteadyStateDiffEq, StochasticDiffEq, SciMLBase, Test
66
using ModelingToolkit: t_nounits as t, D_nounits as D
77

88
# Sets rnd number.
@@ -101,7 +101,7 @@ begin
101101
end
102102

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

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

132132
# Perform steady state simulations (singular and ensemble).
133-
let
133+
@testset "SteadyState" begin
134134
# Creates normal and ensemble problems.
135135
base_ssprob = SteadyStateProblem(osys, [u0_alts[1]; p_alts[1]])
136136
base_sol = solve(base_ssprob, DynamicSS(Tsit5()))
@@ -146,3 +146,85 @@ let
146146
@test base_esol == solve(eprob, DynamicSS(Tsit5()); trajectories = 2)
147147
end
148148
end
149+
150+
@testset "Deprecations" begin
151+
@variables _x(..) = 1.0
152+
@parameters p = 1.0
153+
@brownian a
154+
x = _x(t)
155+
k = ShiftIndex(t)
156+
157+
@test_deprecated ODESystem([D(x) ~ x * p], t; name = :a)
158+
@mtkcompile odesys = System([D(x) ~ x * p], t)
159+
@mtkcompile sdesys = System([D(x) ~ x * p + a], t)
160+
@test_deprecated NonlinearSystem([0 ~ x^3 + p]; name = :a)
161+
@mtkcompile nlsys = System([0 ~ x^3 + p])
162+
@mtkcompile ddesys = System([D(x) ~ x * p + _x(t - 0.1)], t)
163+
@mtkcompile sddesys = System([D(x) ~ x * p + _x(t - 0.1) + a], t)
164+
@test_deprecated DiscreteSystem([x ~ x(k - 1) + x(k - 2)], t; name = :a)
165+
@mtkcompile discsys = System([x ~ x(k - 1) * p], t)
166+
@test_deprecated ImplicitDiscreteSystem([x ~ x(k - 1) + x(k - 2) * p * x], t; name = :a)
167+
@mtkcompile idiscsys = System([x ~ x(k - 1) * p * x], t)
168+
@mtkcompile optsys = OptimizationSystem(x^2 + p)
169+
170+
u0s = [
171+
Dict(x => 1.0),
172+
[x => 1.0],
173+
[1.0],
174+
[],
175+
nothing
176+
]
177+
ps = [
178+
Dict(p => 1.0),
179+
[p => 1.0],
180+
[1.0],
181+
[],
182+
nothing,
183+
SciMLBase.NullParameters()
184+
]
185+
tspan = (0.0, 1.0)
186+
187+
@testset "$ctor" for (sys, ctor) in [
188+
(odesys, ODEProblem),
189+
(odesys, ODEProblem{true}),
190+
(odesys, ODEProblem{true, SciMLBase.FullSpecialize}), (odesys, BVProblem),
191+
(odesys, BVProblem{true}),
192+
(odesys, BVProblem{true, SciMLBase.FullSpecialize}), (sdesys, SDEProblem),
193+
(sdesys, SDEProblem{true}),
194+
(sdesys, SDEProblem{true, SciMLBase.FullSpecialize}), (ddesys, DDEProblem),
195+
(ddesys, DDEProblem{true}),
196+
(ddesys, DDEProblem{true, SciMLBase.FullSpecialize}), (sddesys, SDDEProblem),
197+
(sddesys, SDDEProblem{true}),
198+
(sddesys, SDDEProblem{true, SciMLBase.FullSpecialize}),
199+
200+
# (discsys, DiscreteProblem),
201+
# (discsys, DiscreteProblem{true}),
202+
# (discsys, DiscreteProblem{true, SciMLBase.FullSpecialize}),
203+
204+
(idiscsys, ImplicitDiscreteProblem),
205+
(idiscsys, ImplicitDiscreteProblem{true}),
206+
(idiscsys, ImplicitDiscreteProblem{true, SciMLBase.FullSpecialize})
207+
]
208+
@testset "$(typeof(u0)) - $(typeof(p))" for u0 in u0s, p in ps
209+
if u0 isa Vector{Float64} && ctor <: ImplicitDiscreteProblem
210+
u0 = ones(2)
211+
end
212+
@test_warn ["deprecated"] ctor(sys, u0, tspan, p)
213+
end
214+
end
215+
@testset "$ctor" for (sys, ctor) in [
216+
(nlsys, NonlinearProblem),
217+
(nlsys, NonlinearProblem{true}),
218+
(nlsys, NonlinearProblem{true, SciMLBase.FullSpecialize}), (
219+
nlsys, NonlinearLeastSquaresProblem),
220+
(nlsys, NonlinearLeastSquaresProblem{true}),
221+
(nlsys, NonlinearLeastSquaresProblem{true, SciMLBase.FullSpecialize}), (
222+
nlsys, SCCNonlinearProblem),
223+
(nlsys, SCCNonlinearProblem{true}), (optsys, OptimizationProblem),
224+
(optsys, OptimizationProblem{true})
225+
]
226+
@testset "$(typeof(u0)) - $(typeof(p))" for u0 in u0s, p in ps
227+
@test_warn ["deprecated"] ctor(sys, u0, p)
228+
end
229+
end
230+
end

0 commit comments

Comments
 (0)