Skip to content

Commit bf7518b

Browse files
refactor: support reverting to old behavior by passing split=false
1 parent 2d74b57 commit bf7518b

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
`@parameters p::Int`. Array-valued parameters must be array symbolics; `@parameters p = [1.0, 2.0]`
3131
is now invalid and must be changed to `@parameters p[1:2] = [1.0, 2.0]`. The index of a parameter
3232
in the system is also not guaranteed to be an `Int`, and will instead be a custom undocumented type.
33-
- To restore the old behavior, use `ModelingToolkit.@set sys.index_cache = nothing` before creating
34-
a problem, and after calling `structural_simplify`.
33+
To restore the old behavior:
34+
- Pass the `split = false` keyword to `structural_simplify`. E.g. `ss = structural_simplify(sys; split = false)`.
35+
- Pass `split = false` to `@mtkbuild`. E.g. `@mtkbuild sys = ODESystem(...) split = false`.
3536
- Discrete-time system using `Difference` are unsupported. Instead, use the new `Clock`-based syntax.

src/systems/abstractsystem.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ $(TYPEDSIGNATURES)
373373
Mark a system as completed. If a system is complete, the system will no longer
374374
namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`.
375375
"""
376-
function complete(sys::AbstractSystem)
377-
if has_index_cache(sys)
376+
function complete(sys::AbstractSystem; split = true)
377+
if split && has_index_cache(sys)
378378
@set! sys.index_cache = IndexCache(sys)
379379
end
380380
isdefined(sys, :complete) ? (@set! sys.complete = true) : sys
@@ -1380,12 +1380,19 @@ macro component(expr)
13801380
esc(component_post_processing(expr, false))
13811381
end
13821382

1383-
macro mtkbuild(expr)
1383+
macro mtkbuild(exprs...)
1384+
expr = exprs[1]
13841385
named_expr = ModelingToolkit.named_expr(expr)
13851386
name = named_expr.args[1]
1387+
kwargs = if length(exprs) > 1
1388+
NamedTuple{Tuple(ex.args[1] for ex in Base.tail(exprs))}(Tuple(ex.args[2] for ex in Base.tail(exprs)))
1389+
else
1390+
(;)
1391+
end
1392+
@show kwargs
13861393
esc(quote
13871394
$named_expr
1388-
$name = $structural_simplify($name)
1395+
$name = $structural_simplify($name; $(kwargs)...)
13891396
end)
13901397
end
13911398

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap;
793793
ps = parameters(sys)
794794
iv = get_iv(sys)
795795

796-
u0, _, defs = get_u0_p(sys,
796+
u0, _p, defs = get_u0_p(sys,
797797
u0map,
798798
parammap;
799799
tofloat,
@@ -803,7 +803,11 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap;
803803
u0 = u0_constructor(u0)
804804
end
805805

806-
p = MTKParameters(sys, parammap)
806+
if has_index_cache(sys) && get_index_cache(sys) !== nothing
807+
p = MTKParameters(sys, parammap)
808+
else
809+
p = _p
810+
end
807811

808812
if implicit_dae && du0map !== nothing
809813
ddvs = map(Differential(iv), dvs)

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ function process_NonlinearProblem(constructor, sys::NonlinearSystem, u0map, para
353353
ps = parameters(sys)
354354

355355
u0, p, defs = get_u0_p(sys, u0map, parammap; tofloat, use_union)
356-
p = MTKParameters(sys, parammap)
356+
if has_index_cache(sys) && get_index_cache(sys) !== nothing
357+
p = MTKParameters(sys, parammap)
358+
end
357359
check_eqs_u0(eqs, dvs, u0; kwargs...)
358360

359361
f = constructor(sys, dvs, ps, u0; jac = jac, checkbounds = checkbounds,

src/systems/optimization/optimizationsystem.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0map,
623623
end
624624
end
625625

626-
function structural_simplify(sys::OptimizationSystem; kwargs...)
626+
function structural_simplify(sys::OptimizationSystem; split = true, kwargs...)
627627
sys = flatten(sys)
628628
cons = constraints(sys)
629629
econs = Equation[]
@@ -650,6 +650,6 @@ function structural_simplify(sys::OptimizationSystem; kwargs...)
650650
neweqs = fixpoint_sub.(equations(sys), (subs,))
651651
@set! sys.op = length(neweqs) == 1 ? first(neweqs) : neweqs
652652
@set! sys.unknowns = newsts
653-
sys = complete(sys)
653+
sys = complete(sys; split)
654654
return sys
655655
end

src/systems/systems.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The optional argument `io` may take a tuple `(inputs, outputs)`.
1616
This will convert all `inputs` to parameters and allow them to be unconnected, i.e.,
1717
simplification will allow models where `n_unknowns = n_equations - n_inputs`.
1818
"""
19-
function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false,
19+
function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false, split = true,
2020
kwargs...)
2121
newsys′ = __structural_simplify(sys, io; simplify, kwargs...)
2222
if newsys′ isa Tuple
@@ -25,8 +25,8 @@ function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false
2525
else
2626
newsys = newsys′
2727
end
28-
@set! newsys.parent = complete(sys)
29-
newsys = complete(newsys)
28+
@set! newsys.parent = complete(sys; split)
29+
newsys = complete(newsys; split)
3030
if newsys′ isa Tuple
3131
idxs = [parameter_index(newsys, i) for i in io[1]]
3232
return newsys, idxs

0 commit comments

Comments
 (0)