@@ -195,6 +195,23 @@ function to_mass_matrix_form(neweqs, ieq, graph, fullvars, isdervar::F,
195
195
end
196
196
end
197
197
198
+ #=
199
+ function check_diff_graph(var_to_diff, fullvars)
200
+ diff_to_var = invview(var_to_diff)
201
+ for (iv, v) in enumerate(fullvars)
202
+ ov, order = var_from_nested_derivative(v)
203
+ graph_order = 0
204
+ vv = iv
205
+ while true
206
+ vv = diff_to_var[vv]
207
+ vv === nothing && break
208
+ graph_order += 1
209
+ end
210
+ @assert graph_order==order "graph_order: $graph_order, order: $order for variable $v"
211
+ end
212
+ end
213
+ =#
214
+
198
215
function tearing_reassemble (state:: TearingState , var_eq_matching; simplify = false )
199
216
@unpack fullvars, sys = state
200
217
@unpack solvable_graph, var_to_diff, eq_to_diff, graph = state. structure
@@ -237,8 +254,12 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
237
254
possible_x_t[rhs] = i, lhs
238
255
end
239
256
240
- # removed_eqs = Int[]
241
- # removed_vars = Int[]
257
+ if ModelingToolkit. has_iv (state. sys)
258
+ iv = get_iv (state. sys)
259
+ D = Differential (iv)
260
+ else
261
+ iv = D = nothing
262
+ end
242
263
removed_obs = Int[]
243
264
diff_to_var = invview (var_to_diff)
244
265
dummy_sub = Dict ()
@@ -258,7 +279,19 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
258
279
neweqs[eq] = substitute (neweqs[eq], dd => v_t)
259
280
end
260
281
fullvars[dv] = v_t
282
+ # If we have:
283
+ # x -> D(x) -> D(D(x))
284
+ # We need to to transform it to:
285
+ # x x_t -> D(x_t)
261
286
# update the structural information
287
+ if (ddx = var_to_diff[dv]) != = nothing
288
+ dv_t = D (v_t)
289
+ # TODO : handle this recursively
290
+ for eq in 𝑑neighbors (graph, ddx)
291
+ neweqs[eq] = substitute (neweqs[eq], fullvars[ddx] => dv_t)
292
+ end
293
+ fullvars[ddx] = dv_t
294
+ end
262
295
diff_to_var[dv] = nothing
263
296
end
264
297
end
@@ -323,12 +356,6 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
323
356
# As a final note, in all the above cases where we need to introduce new
324
357
# variables and equations, don't add them when they already exist.
325
358
326
- if ModelingToolkit. has_iv (state. sys)
327
- iv = get_iv (state. sys)
328
- D = Differential (iv)
329
- else
330
- iv = D = nothing
331
- end
332
359
nvars = ndsts (graph)
333
360
processed = falses (nvars)
334
361
subinfo = NTuple{3 , Int}[]
@@ -488,7 +515,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
488
515
# 0 ~ a * var + b
489
516
# var ~ -b/a
490
517
if ModelingToolkit. _iszero (a)
491
- @warn " Tearing: $eq is a singular equation !"
518
+ @warn " Tearing: solving $eq for $var is singular !"
492
519
# push!(removed_eqs, ieq)
493
520
# push!(removed_vars, iv)
494
521
else
0 commit comments