Skip to content

Commit f2c6da1

Browse files
committed
toodesystem -> convert_system
1 parent b240dcc commit f2c6da1

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ include("systems/alias_elimination.jl")
136136
include("structural_transformation/StructuralTransformations.jl")
137137
@reexport using .StructuralTransformations
138138

139-
export ODESystem, ODEFunction, ODEFunctionExpr, ODEProblemExpr, toodesystem
139+
export ODESystem, ODEFunction, ODEFunctionExpr, ODEProblemExpr, convert_system
140140
export DAEFunctionExpr, DAEProblemExpr
141141
export SDESystem, SDEFunction, SDEFunctionExpr, SDESystemExpr
142142
export SystemStructure

src/systems/abstractsystem.jl

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -737,40 +737,3 @@ end
737737
function connect(syss...)
738738
connect(promote_connect_type(map(get_connection_type, syss)...), syss...)
739739
end
740-
741-
###
742-
### system promotion
743-
###
744-
745-
# We have a stand-alone function to convert a `NonlinearSystem` or `ODESystem`
746-
# to an `ODESystem` to connect systems, and we later can reply on
747-
# `structural_simplify` to convert `ODESystem`s to `NonlinearSystem`s.
748-
function toodesystem(sys, t; name=nameof(sys))
749-
isempty(observed(sys)) || throw(ArgumentError("`toodesystem` cannot handle reduced model (i.e. observed(sys) is non-empty)."))
750-
t = value(t)
751-
varmap = Dict()
752-
sts = states(sys)
753-
newsts = similar(sts, Any)
754-
for (i, s) in enumerate(sts)
755-
if istree(s)
756-
args = arguments(s)
757-
length(args) == 1 || throw(InvalidSystemException("Illegal state: $s. The state can have at most one argument like `x(t)`."))
758-
arg = args[1]
759-
if isequal(arg, t)
760-
newsts[i] = s
761-
continue
762-
end
763-
ns = operation(s)(t)
764-
newsts[i] = ns
765-
varmap[s] = ns
766-
else
767-
ns = indepvar2depvar(s, t)
768-
newsts[i] = ns
769-
varmap[s] = ns
770-
end
771-
end
772-
sub = Base.Fix2(substitute, varmap)
773-
neweqs = map(sub, equations(sys))
774-
defs = Dict(sub(k) => sub(v) for (k, v) in defaults(sys))
775-
return ODESystem(neweqs, t, newsts, parameters(sys); defaults=defs, name=name)
776-
end

src/systems/diffeqs/odesystem.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,42 @@ function collect_differential_variables(sys::ODESystem)
300300
end
301301
return diffvars
302302
end
303+
304+
# We have a stand-alone function to convert a `NonlinearSystem` or `ODESystem`
305+
# to an `ODESystem` to connect systems, and we later can reply on
306+
# `structural_simplify` to convert `ODESystem`s to `NonlinearSystem`s.
307+
"""
308+
$(TYPEDSIGNATURES)
309+
310+
Convert a `NonlinearSystem` to an `ODESystem` or converts an `ODESystem` to a
311+
new `ODESystem` with a different independent variable.
312+
"""
313+
function convert_system(::Type{<:ODESystem}, sys, t; name=nameof(sys))
314+
isempty(observed(sys)) || throw(ArgumentError("`convert_system` cannot handle reduced model (i.e. observed(sys) is non-empty)."))
315+
t = value(t)
316+
varmap = Dict()
317+
sts = states(sys)
318+
newsts = similar(sts, Any)
319+
for (i, s) in enumerate(sts)
320+
if istree(s)
321+
args = arguments(s)
322+
length(args) == 1 || throw(InvalidSystemException("Illegal state: $s. The state can have at most one argument like `x(t)`."))
323+
arg = args[1]
324+
if isequal(arg, t)
325+
newsts[i] = s
326+
continue
327+
end
328+
ns = operation(s)(t)
329+
newsts[i] = ns
330+
varmap[s] = ns
331+
else
332+
ns = indepvar2depvar(s, t)
333+
newsts[i] = ns
334+
varmap[s] = ns
335+
end
336+
end
337+
sub = Base.Fix2(substitute, varmap)
338+
neweqs = map(sub, equations(sys))
339+
defs = Dict(sub(k) => sub(v) for (k, v) in defaults(sys))
340+
return ODESystem(neweqs, t, newsts, parameters(sys); defaults=defs, name=name)
341+
end

test/nonlinearsystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ connected = NonlinearSystem([s ~ a + lorenz1.x
100100
using OrdinaryDiffEq
101101
@variables t
102102
D = Differential(t)
103-
@named subsys = toodesystem(lorenz1, t)
103+
@named subsys = convert_system(ODESystem, lorenz1, t)
104104
@named sys = ODESystem([D(subsys.x) ~ subsys.x + subsys.x], t, systems=[subsys])
105105
sys = structural_simplify(sys)
106106
prob = ODEProblem(sys, [subsys.x => 1, subsys.z => 2.0], (0, 1.0), [subsys.σ=>1,subsys.ρ=>2,subsys.β=>3])
107107
sol = solve(prob, Rodas5())
108108
@test sol[subsys.x] + sol[subsys.y] - sol[subsys.z] sol[subsys.u]
109-
@test_throws ArgumentError toodesystem(sys, t)
109+
@test_throws ArgumentError convert_system(ODESystem, sys, t)

0 commit comments

Comments
 (0)