@@ -135,7 +135,7 @@ function substitute_vars!(graph::BipartiteGraph, subs, cache = Int[], callback!
135
135
end
136
136
137
137
function tearing_reassemble (state:: TearingState , var_eq_matching; simplify = false )
138
- fullvars = state. fullvars
138
+ @unpack fullvars, sys = state
139
139
@unpack solvable_graph, var_to_diff, eq_to_diff, graph = state. structure
140
140
141
141
neweqs = collect (equations (state))
@@ -165,16 +165,32 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
165
165
# Step 1:
166
166
# Replace derivatives of non-selected states by dummy derivatives
167
167
168
+ possible_x_t = Dict ()
169
+ oldobs = observed (sys)
170
+ for (i, eq) in enumerate (oldobs)
171
+ lhs = eq. lhs
172
+ rhs = eq. rhs
173
+ isdifferential (lhs) && continue
174
+ # TODO : should we hanlde negative alias as well?
175
+ isdifferential (rhs) || continue
176
+ possible_x_t[rhs] = i, lhs
177
+ end
178
+
168
179
removed_eqs = Int[]
169
180
removed_vars = Int[]
181
+ removed_obs = Int[]
170
182
diff_to_var = invview (var_to_diff)
171
183
for var in 1 : length (fullvars)
172
184
dv = var_to_diff[var]
173
185
dv === nothing && continue
174
186
if var_eq_matching[var] != = SelectedState ()
175
187
dd = fullvars[dv]
176
- # TODO : check if observed has it
177
- v_t = diff2term (unwrap (dd))
188
+ if (i_v_t = get (possible_x_t, rhs, nothing )) === nothing
189
+ v_t = diff2term (unwrap (dd))
190
+ else
191
+ idx, v_t = i_v_t
192
+ push! (removed_obs, idx)
193
+ end
178
194
for eq in 𝑑neighbors (graph, dv)
179
195
neweqs[eq] = substitute (neweqs[eq], fullvars[dv] => v_t)
180
196
end
@@ -312,8 +328,14 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
312
328
dx = fullvars[dx_idx]
313
329
end
314
330
315
- # TODO : check if it's already in observed
316
- x_t = ModelingToolkit. lower_varname (ogx, iv, o)
331
+ if (i_x_t = get (possible_x_t, dx, nothing )) === nothing &&
332
+ (ogidx != = nothing &&
333
+ (i_x_t = get (possible_x_t, fullvars[ogidx], nothing )) === nothing )
334
+ x_t = ModelingToolkit. lower_varname (ogx, iv, o)
335
+ else
336
+ idx, x_t = i_x_t
337
+ push! (removed_obs, idx)
338
+ end
317
339
push! (fullvars, x_t)
318
340
x_t_idx = add_vertex! (var_to_diff)
319
341
add_vertex! (graph, DST)
@@ -491,7 +513,8 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
491
513
sys = state. sys
492
514
@set! sys. eqs = neweqs
493
515
@set! sys. states = [fullvars[i] for i in active_vars if diff_to_var[i] === nothing ]
494
- @set! sys. observed = [observed (sys); subeqs]
516
+ deleteat! (oldobs, sort! (removed_obs))
517
+ @set! sys. observed = [oldobs; subeqs]
495
518
@set! sys. substitutions = Substitutions (subeqs, deps)
496
519
@set! state. sys = sys
497
520
@set! sys. tearing_state = state
0 commit comments