Skip to content

Commit ef95ef9

Browse files
committed
Re-tear
1 parent 2521506 commit ef95ef9

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/structural_transformation/symbolics_tearing.jl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching, ag = nothing;
319319
var -> diff_to_var[var] !== nothing
320320
end
321321

322+
retear = BitSet()
322323
# There are three cases where we want to generate new variables to convert
323324
# the system into first order (semi-implicit) ODEs.
324325
#
@@ -469,20 +470,38 @@ function tearing_reassemble(state::TearingState, var_eq_matching, ag = nothing;
469470
for (ogidx, dx_idx, x_t_idx) in Iterators.reverse(subinfo)
470471
# We need a loop here because both `D(D(x))` and `D(x_t)` need to be
471472
# substituted to `x_tt`.
472-
for idx in (ogidx, dx_idx)
473+
for idx in (ogidx == dx_idx ? ogidx : (ogidx, dx_idx))
473474
subidx = ((idx => x_t_idx),)
474475
# 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
478476
substitute_vars!(structure, subidx, idx_buffer, sub_callback!;
479477
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
480487
end
481488
end
482489
empty!(subinfo)
483490
empty!(subs)
484491
end
485492

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+
486505
# Will reorder equations and states to be:
487506
# [diffeqs; ...]
488507
# [diffvars; ...]

src/systems/abstractsystem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ function structural_simplify(sys::AbstractSystem, io = nothing; simplify = false
10361036
has_io && markio!(state, io...)
10371037
state, input_idxs = inputs_to_parameters!(state, io)
10381038
sys, ag = alias_elimination!(state)
1039+
#ag = AliasGraph(length(ag))
10391040
# TODO: avoid construct `TearingState` again.
10401041
#state = TearingState(sys)
10411042
#has_io && markio!(state, io..., check = false)

0 commit comments

Comments
 (0)