@@ -319,6 +319,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, ag = nothing;
319
319
var -> diff_to_var[var] != = nothing
320
320
end
321
321
322
+ retear = BitSet ()
322
323
# There are three cases where we want to generate new variables to convert
323
324
# the system into first order (semi-implicit) ODEs.
324
325
#
@@ -469,20 +470,38 @@ function tearing_reassemble(state::TearingState, var_eq_matching, ag = nothing;
469
470
for (ogidx, dx_idx, x_t_idx) in Iterators. reverse (subinfo)
470
471
# We need a loop here because both `D(D(x))` and `D(x_t)` need to be
471
472
# substituted to `x_tt`.
472
- for idx in (ogidx, dx_idx)
473
+ for idx in (ogidx == dx_idx ? ogidx : (ogidx , dx_idx) )
473
474
subidx = ((idx => x_t_idx),)
474
475
# This handles case 2.2
475
- if var_eq_matching[idx] isa Int
476
- var_eq_matching[x_t_idx] = var_eq_matching[idx]
477
- end
478
476
substitute_vars! (structure, subidx, idx_buffer, sub_callback!;
479
477
exclude = order_lowering_eqs)
478
+ if var_eq_matching[idx] isa Int
479
+ original_assigned_eq = var_eq_matching[idx]
480
+ # This removes the assignment of the variable `idx`, so we
481
+ # should consider assign them again later.
482
+ var_eq_matching[x_t_idx] = original_assigned_eq
483
+ if ! isempty (𝑑neighbors (graph, idx))
484
+ push! (retear, idx)
485
+ end
486
+ end
480
487
end
481
488
end
482
489
empty! (subinfo)
483
490
empty! (subs)
484
491
end
485
492
493
+ ict = IncrementalCycleTracker (DiCMOBiGraph {true} (graph, var_eq_matching); dir = :in )
494
+ for idx in retear
495
+ for alternative_eq in 𝑑neighbors (solvable_graph, idx)
496
+ # skip actually differentiated variables
497
+ any (𝑠neighbors (graph, alternative_eq)) do alternative_v
498
+ ((vv = diff_to_var[alternative_v]) != = nothing &&
499
+ var_eq_matching[vv] === SelectedState ())
500
+ end && continue
501
+ try_assign_eq! (ict, idx, alternative_eq) && break
502
+ end
503
+ end
504
+
486
505
# Will reorder equations and states to be:
487
506
# [diffeqs; ...]
488
507
# [diffvars; ...]
0 commit comments