Skip to content

Commit deb0b9b

Browse files
committed
Add structural_simplify support for OptimizationSystem
1 parent 9de96de commit deb0b9b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/systems/abstractsystem.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,15 +1031,17 @@ This will convert all `inputs` to parameters and allow them to be unconnected, i
10311031
simplification will allow models where `n_states = n_equations - n_inputs`.
10321032
"""
10331033
function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false,
1034-
simplify_constants = true, kwargs...)
1034+
simplify_constants = true, check_consistency = true, kwargs...)
10351035
sys = expand_connections(sys)
10361036
sys isa DiscreteSystem && return sys
10371037
state = TearingState(sys)
10381038
has_io = io !== nothing
10391039
has_io && markio!(state, io...)
10401040
state, input_idxs = inputs_to_parameters!(state, io)
10411041
sys, ag = alias_elimination!(state; kwargs...)
1042-
check_consistency(state, ag)
1042+
if check_consistency
1043+
ModelingToolkit.check_consistency(state, ag)
1044+
end
10431045
sys = dummy_derivative(sys, state, ag; simplify)
10441046
fullstates = [map(eq -> eq.lhs, observed(sys)); states(sys)]
10451047
@set! sys.observed = topsort_equations(observed(sys), fullstates)

src/systems/optimization/optimizationsystem.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,32 @@ function OptimizationProblemExpr{iip}(sys::OptimizationSystem, u0,
543543
end
544544
end
545545
end
546+
547+
function structural_simplify(sys::OptimizationSystem; kwargs...)
548+
sys = flatten(sys)
549+
cons = constraints(sys)
550+
econs = Equation[]
551+
icons = similar(cons, 0)
552+
for e in cons
553+
if e isa Equation
554+
push!(econs, e)
555+
else
556+
push!(icons, e)
557+
end
558+
end
559+
nlsys = NonlinearSystem(econs, states(sys), parameters(sys); name = :___tmp_nlsystem)
560+
snlsys = structural_simplify(nlsys; check_consistency = false, kwargs...)
561+
obs = observed(snlsys)
562+
subs = Dict(eq.lhs => eq.rhs for eq in observed(snlsys))
563+
seqs = equations(snlsys)
564+
sizehint!(icons, length(icons) + length(seqs))
565+
for eq in seqs
566+
push!(icons, substitute(eq, subs))
567+
end
568+
newsts = setdiff(states(sys), keys(subs))
569+
@set! sys.constraints = icons
570+
@set! sys.observed = [observed(sys); obs]
571+
@set! sys.op = substitute(equations(sys), subs)
572+
@set! sys.states = newsts
573+
return sys
574+
end

0 commit comments

Comments
 (0)