203
203
mutable struct TearingState{T <: AbstractSystem } <: AbstractTearingState{T}
204
204
""" The system of equations."""
205
205
sys:: T
206
+ original_eqs:: Vector{Equation}
206
207
""" The set of variables of the system."""
207
208
fullvars:: Vector{BasicSymbolic}
208
209
structure:: SystemStructure
219
220
TransformationState (sys:: AbstractSystem ) = TearingState (sys)
220
221
function system_subset (ts:: TearingState , ieqs:: Vector{Int} )
221
222
eqs = equations (ts)
223
+ @set! ts. original_eqs = ts. original_eqs[ieqs]
222
224
@set! ts. sys. eqs = eqs[ieqs]
223
225
@set! ts. original_eqs = ts. original_eqs[ieqs]
224
226
@set! ts. structure = system_subset (ts. structure, ieqs)
@@ -524,7 +526,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true)
524
526
525
527
eq_to_diff = DiffGraph (nsrcs (graph))
526
528
527
- ts = TearingState (sys, fullvars,
529
+ ts = TearingState (sys, original_eqs, fullvars,
528
530
SystemStructure (complete (var_to_diff), complete (eq_to_diff),
529
531
complete (graph), nothing , var_types, false ),
530
532
Any[], param_derivative_map, original_eqs, Equation[])
@@ -810,6 +812,22 @@ function Base.show(io::IO, mime::MIME"text/plain", ms::MatchedSystemStructure)
810
812
printstyled (io, " SelectedState" )
811
813
end
812
814
815
+ function make_eqs_zero_equals! (ts:: TearingState )
816
+ neweqs = map (enumerate (get_eqs (ts. sys))) do kvp
817
+ i, eq = kvp
818
+ isalgeq = true
819
+ for j in 𝑠neighbors (ts. structure. graph, i)
820
+ isalgeq &= invview (ts. structure. var_to_diff)[j] === nothing
821
+ end
822
+ if isalgeq
823
+ return 0 ~ eq. rhs - eq. lhs
824
+ else
825
+ return eq
826
+ end
827
+ end
828
+ copyto! (get_eqs (ts. sys), neweqs)
829
+ end
830
+
813
831
function mtkcompile! (state:: TearingState ; simplify = false ,
814
832
check_consistency = true , fully_determined = true , warn_initialize_determined = true ,
815
833
inputs = Any[], outputs = Any[],
@@ -836,6 +854,7 @@ function mtkcompile!(state::TearingState; simplify = false,
836
854
""" ))
837
855
end
838
856
if length (tss) > 1
857
+ make_eqs_zero_equals! (tss[continuous_id])
839
858
# simplify as normal
840
859
sys = _mtkcompile! (tss[continuous_id]; simplify,
841
860
inputs = [inputs; clocked_inputs[continuous_id]], outputs, disturbance_inputs,
0 commit comments