Skip to content

Commit 0848284

Browse files
Merge pull request #54 from JuliaComputing/as/fix-diffcache-reference
fix: fix reference used for inline linear SCC diffcache
2 parents 48d5ba6 + 4712852 commit 0848284

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

lib/ModelingToolkitTearing/src/reassemble.jl

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,16 +576,35 @@ function get_linear_scc_linsol(state::TearingState, alg_eqs::Vector{Int},
576576
end
577577
# Turn into symbolic arrays
578578
sys = state.sys
579-
reference_idx = findfirst(!SU.isconst, A)
580-
if reference_idx === nothing
581-
reference_idx = findfirst(!SU.isconst, b)
579+
# Prefer using a differential variable
580+
state_idx = findfirst(
581+
Base.Fix2(isa, SelectedState) Base.Fix1(getindex, var_eq_matching),
582+
eachindex(var_eq_matching)
583+
)
584+
if state_idx === nothing
585+
# Find something in `A`
586+
reference_idx = @something(
587+
findfirst(iscall unwrap, A),
588+
findfirst(!SU.isconst unwrap, A),
589+
Some(nothing)
590+
)
582591
if reference_idx === nothing
583-
reference = first(A)
592+
# Find something in `b`
593+
reference_idx = @something(
594+
findfirst(iscall, b),
595+
findfirst(!SU.isconst, b),
596+
Some(nothing)
597+
)
598+
if reference_idx === nothing
599+
reference = first(A)
600+
else
601+
reference = b[reference_idx]
602+
end
584603
else
585604
reference = A[reference_idx]
586605
end
587606
else
588-
reference = A[reference_idx]
607+
reference = fullvars[state_idx]
589608
end
590609
sys, A_cache = MTKBase.add_diffcache(sys, length(A))
591610
A_allocator = A_cache(reference)
@@ -1059,6 +1078,7 @@ function (alg::DefaultReassembleAlgorithm)(state::TearingState,
10591078
(; simplify, array_hack, inline_linear_sccs, analytical_linear_scc_limit) = alg
10601079
(; var_eq_matching, full_var_eq_matching, var_sccs) = tearing_result
10611080

1081+
10621082
extra_eqs_vars = get_extra_eqs_vars(
10631083
state, var_eq_matching, full_var_eq_matching, fully_determined)
10641084
neweqs = collect(equations(state))
@@ -1074,6 +1094,12 @@ function (alg::DefaultReassembleAlgorithm)(state::TearingState,
10741094
else
10751095
iv = D = nothing
10761096
end
1097+
# `iv === nothing` implies a nonlinear system. `SCCNonlinearProblem` should be
1098+
# used instead of this pass. `D isa Shift` implies a discrete system, which currently
1099+
# doesn't support inline linear SCCs.
1100+
if iv === nothing || D isa Shift
1101+
inline_linear_sccs = false
1102+
end
10771103

10781104
extra_unknowns = state.fullvars[extra_eqs_vars[2]]
10791105
if StateSelection.is_only_discrete(state.structure)

0 commit comments

Comments
 (0)