Skip to content

Commit 40c9fb5

Browse files
fix: resolve ModelingToolkitBase method ambiguities (#4849)
Co-authored-by: ChrisRackauckas-Claude <accounts@chrisrackauckas.com>
1 parent 9c06367 commit 40c9fb5

7 files changed

Lines changed: 102 additions & 9 deletions

File tree

lib/ModelingToolkitBase/src/deprecations.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ for T in [
113113
"""
114114
return $T{iip, spec}(sys, merge($uCanonical, $pCanonical), tspan; kw...)
115115
end
116+
if T === :ODEProblem
117+
@eval function SciMLBase.ODEProblem{
118+
iip, SciMLBase.FunctionWrapperSpecialize,
119+
}(
120+
sys::System, u0::$uType, tspan, p::$pType; kw...
121+
) where {iip}
122+
ctor = string(ODEProblem{iip, SciMLBase.FunctionWrapperSpecialize})
123+
uCan = string($(QuoteNode(uCanonical)))
124+
pCan = string($(QuoteNode(pCanonical)))
125+
@warn """
126+
`$ctor(sys, u0, tspan, p; kw...)` is deprecated. Use
127+
`$ctor(sys, merge($uCan, $pCan), tspan)` instead.
128+
"""
129+
return ODEProblem{iip, SciMLBase.FunctionWrapperSpecialize}(
130+
sys, merge($uCanonical, $pCanonical), tspan; kw...
131+
)
132+
end
133+
end
116134
end
117135

118136
for pType in [SciMLBase.NullParameters, Nothing], uType in [Any, Nothing]
@@ -148,6 +166,23 @@ for T in [
148166
"""
149167
return $T{iip, spec}(sys, u0, tspan; kw...)
150168
end
169+
if T === :ODEProblem
170+
@eval function SciMLBase.ODEProblem{
171+
iip, SciMLBase.FunctionWrapperSpecialize,
172+
}(
173+
sys::System, u0::$uType, tspan, p::$pType; kw...
174+
) where {iip}
175+
ctor = string(ODEProblem{iip, SciMLBase.FunctionWrapperSpecialize})
176+
pT = string($(QuoteNode(pType)))
177+
@warn """
178+
`$ctor(sys, u0, tspan, p::$pT; kw...)` is deprecated. Use
179+
`$ctor(sys, u0, tspan)` instead.
180+
"""
181+
return ODEProblem{iip, SciMLBase.FunctionWrapperSpecialize}(
182+
sys, u0, tspan; kw...
183+
)
184+
end
185+
end
151186
end
152187
end
153188

lib/ModelingToolkitBase/src/problems/odeproblem.jl

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ function SciMLBase.ODEFunction{iip, spec}(
116116
return odefn
117117
end
118118

119-
Base.@nospecializeinfer @fallback_iip_specialize function SciMLBase.ODEProblem{iip, spec}(
120-
sys::System, @nospecialize(op), tspan;
119+
Base.@nospecializeinfer function _ode_problem(
120+
::Type{ODEProblem{iip, spec}}, sys::System, @nospecialize(op), tspan;
121121
@nospecialize(callback = nothing), check_length = true, eval_expression = false,
122122
expression = Val{false}, eval_module = @__MODULE__, check_compatibility = true,
123123
_skip_events = false, kwargs...
@@ -146,7 +146,36 @@ Base.@nospecializeinfer @fallback_iip_specialize function SciMLBase.ODEProblem{i
146146

147147
ptype = getmetadata(sys, ProblemTypeCtx, StandardODEProblem())
148148
args = (; f, u0, tspan, p, ptype)
149-
maybe_codegen_scimlproblem(expression, ODEProblem{_iip}, args; kwargs...)
149+
return maybe_codegen_scimlproblem(expression, ODEProblem{_iip}, args; kwargs...)
150+
end
151+
152+
Base.@nospecializeinfer @fallback_iip_specialize function SciMLBase.ODEProblem{iip, spec}(
153+
sys::System, @nospecialize(op), tspan;
154+
@nospecialize(callback = nothing), check_length = true, eval_expression = false,
155+
expression = Val{false}, eval_module = @__MODULE__, check_compatibility = true,
156+
_skip_events = false, kwargs...
157+
) where {iip, spec}
158+
return _ode_problem(
159+
ODEProblem{iip, spec}, sys, op, tspan;
160+
callback, check_length, eval_expression, expression, eval_module, check_compatibility,
161+
_skip_events, kwargs...
162+
)
163+
end
164+
165+
# SciMLBase also defines this fixed-specialization constructor for arbitrary functions.
166+
Base.@nospecializeinfer function SciMLBase.ODEProblem{
167+
iip, SciMLBase.FunctionWrapperSpecialize,
168+
}(
169+
sys::System, @nospecialize(op), tspan;
170+
@nospecialize(callback = nothing), check_length = true, eval_expression = false,
171+
expression = Val{false}, eval_module = @__MODULE__, check_compatibility = true,
172+
_skip_events = false, kwargs...
173+
) where {iip}
174+
return _ode_problem(
175+
ODEProblem{iip, SciMLBase.FunctionWrapperSpecialize}, sys, op, tspan;
176+
callback, check_length, eval_expression, expression, eval_module, check_compatibility,
177+
_skip_events, kwargs...
178+
)
150179
end
151180

152181
@fallback_iip_specialize function DiffEqBase.SteadyStateProblem{iip, spec}(

lib/ModelingToolkitBase/src/systems/abstractsystem.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,16 +1270,31 @@ end
12701270

12711271
const AllScopes = Union{LocalScope, ParentScope, GlobalScope}
12721272

1273+
_renamespace(names::AbstractVector, x) = foldr(renamespace, names, init = x)
1274+
12731275
renamespace(sys, eq::Equation) = namespace_equation(eq, sys)
1276+
renamespace(names::AbstractVector, eq::Equation) = _renamespace(names, eq)
12741277

1275-
renamespace(names::AbstractVector, x) = foldr(renamespace, names, init = x)
1278+
renamespace(names::AbstractVector, x) = _renamespace(names, x)
12761279

12771280
renamespace(sys, tgt::AbstractSystem) = rename(tgt, renamespace(sys, nameof(tgt)))
1281+
renamespace(names::AbstractVector, tgt::AbstractSystem) = _renamespace(names, tgt)
12781282
renamespace(sys, tgt::Symbol) = Symbol(getname(sys), NAMESPACE_SEPARATOR_SYMBOL, tgt)
1283+
renamespace(names::AbstractVector, tgt::Symbol) = _renamespace(names, tgt)
12791284
renamespace(sys, x::Num) = Num(renamespace(sys, unwrap(x)))
1285+
renamespace(names::AbstractVector, x::Num) = _renamespace(names, x)
12801286
renamespace(sys, x::Arr{T, N}) where {T, N} = Arr{T, N}(renamespace(sys, unwrap(x)))
1287+
function renamespace(names::AbstractVector, x::Arr{T, N}) where {T, N}
1288+
return _renamespace(names, x)
1289+
end
12811290
renamespace(sys, x::CallAndWrap{T}) where {T} = CallAndWrap{T}(renamespace(sys, unwrap(x)))
1291+
function renamespace(names::AbstractVector, x::CallAndWrap{T}) where {T}
1292+
return _renamespace(names, x)
1293+
end
12821294
renamespace(sys, x::AbstractArray{SymbolicT}) = map(Base.Fix1(renamespace, sys), x)
1295+
function renamespace(names::AbstractVector, x::AbstractArray{SymbolicT})
1296+
return _renamespace(names, x)
1297+
end
12831298

12841299
"""
12851300
$(TYPEDSIGNATURES)
@@ -1327,6 +1342,8 @@ function renamespace(sys, x::SymbolicT)
13271342
end
13281343
end
13291344

1345+
renamespace(names::AbstractVector, x::SymbolicT) = _renamespace(names, x)
1346+
13301347
namespace_variables(sys::AbstractSystem) = unknowns(sys, unknowns(sys))
13311348
namespace_parameters(sys::AbstractSystem) = parameters(sys, parameters(sys))
13321349

lib/ModelingToolkitBase/src/systems/analysis_points.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ function renamespace(sys, ap::AnalysisPoint)
177177
)
178178
end
179179

180+
renamespace(names::AbstractVector, ap::AnalysisPoint) = _renamespace(names, ap)
181+
180182
# create analysis points via `connect`
181183
function connect(in, ap::AnalysisPoint, outs...; verbose = true)
182184
return AnalysisPoint() ~ AnalysisPoint(unwrap(in), ap.name, collect(unwrap.(outs)); verbose)

lib/ModelingToolkitBase/src/systems/parameter_buffer.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,29 +305,28 @@ function _split_helper(buf_v::T, recurse, raw, idx) where {T}
305305
end
306306

307307
function _split_helper(::Type{<:AbstractArray}, buf_v, ::Val{N}, raw, idx) where {N}
308+
iszero(N) && return _split_helper((), buf_v, (), raw, idx)
308309
return map(b -> _split_helper(eltype(b), b, Val(N - 1), raw, idx), buf_v)
309310
end
310311

311312
function _split_helper(
312313
::Type{<:AbstractArray}, buf_v::BlockedArray, ::Val{N}, raw, idx
313314
) where {N}
315+
iszero(N) && return _split_helper((), buf_v, (), raw, idx)
314316
return BlockedArray(
315317
map(b -> _split_helper(eltype(b), b, Val(N - 1), raw, idx), buf_v),
316318
blocksizes(buf_v, 1)
317319
)
318320
end
319321

320322
function _split_helper(::Type{<:AbstractArray}, buf_v::Tuple, ::Val{N}, raw, idx) where {N}
323+
iszero(N) && return _split_helper((), buf_v, (), raw, idx)
321324
return ntuple(
322325
i -> _split_helper(eltype(buf_v[i]), buf_v[i], Val(N - 1), raw, idx),
323326
Val(length(buf_v))
324327
)
325328
end
326329

327-
function _split_helper(::Type{<:AbstractArray}, buf_v, ::Val{0}, raw, idx)
328-
return _split_helper((), buf_v, (), raw, idx)
329-
end
330-
331330
function _split_helper(_, buf_v, _, raw, idx)
332331
res = reshape(raw[idx[]:(idx[] + length(buf_v) - 1)], size(buf_v))
333332
idx[] += length(buf_v)

lib/ModelingToolkitBase/test/namespacing.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ nsys = toggle_namespacing(sys, false)
2828
Equation[], t; systems = [nsys], name = :a
2929
)
3030

31+
@testset "Vector namespaces" begin
32+
outer = System(Equation[], t; name = :outer)
33+
names = [outer, sys]
34+
for value in (:value, x, D(x) ~ x, [ModelingToolkitBase.unwrap(x)])
35+
expected = renamespace(outer, renamespace(sys, value))
36+
@test isequal(renamespace(names, value), expected)
37+
end
38+
end
39+
3140
@testset "Variables of variables" begin
3241
@variables x(t) y(x)
3342
@named inner = System([D(x) ~ x, y ~ 2x + 1], t)

lib/ModelingToolkitBase/test/sciml_problem_inputs.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ end
196196
@testset "$ctor" for (sys, ctor) in [
197197
(odesys, ODEProblem),
198198
(odesys, ODEProblem{true}),
199-
(odesys, ODEProblem{true, SciMLBase.FullSpecialize}), (odesys, BVProblem),
199+
(odesys, ODEProblem{true, SciMLBase.FullSpecialize}),
200+
(odesys, ODEProblem{true, SciMLBase.FunctionWrapperSpecialize}),
201+
(odesys, BVProblem),
200202
(odesys, BVProblem{true}),
201203
(odesys, BVProblem{true, SciMLBase.FullSpecialize}), (sdesys, SDEProblem),
202204
(sdesys, SDEProblem{true}),

0 commit comments

Comments
 (0)