Skip to content

Commit f469dc8

Browse files
Merge pull request #62 from JuliaComputing/as/unused-var-interface
feat: add `always_present` field to `TearingState`, implement `is_unused_var`
2 parents 80241c6 + f22e8af commit f469dc8

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

lib/ModelingToolkitTearing/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ OrderedCollections = "1.8.1"
3636
SciMLBase = "2.108"
3737
Setfield = "0.7, 0.8, 1"
3838
SparseArrays = "1"
39-
StateSelection = "1.8"
39+
StateSelection = "1.9"
4040
SymbolicIndexingInterface = "0.3"
4141
SymbolicUtils = "4.3"
4242
Symbolics = "7.15.1"

lib/ModelingToolkitTearing/src/clock_inference/interface.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function system_subset(ts::TearingState, ieqs::Vector{Int}, iieqs::Vector{Int},
6969
@set! ts.sys.initialization_eqs = initeqs[iieqs]
7070
@set! ts.original_eqs = ts.original_eqs[ieqs]
7171
@set! ts.structure = system_subset(ts.structure, ieqs, ivars)
72+
@set! ts.always_present = ts.always_present[ivars]
7273
if !isempty(ts.eqs_source)
7374
@set! ts.eqs_source = ts.eqs_source[ieqs]
7475
end

lib/ModelingToolkitTearing/src/stateselection_interface.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ function StateSelection.var_derivative!(ts::TearingState, v::Int)
55
D = Differential(MTKBase.get_iv(sys))
66
push!(ts.fullvars, D(ts.fullvars[v]))
77
push!(ts.structure.state_priorities, ts.structure.state_priorities[v])
8+
push!(ts.always_present, ts.always_present[v])
89
return var_diff
910
end
1011

@@ -348,6 +349,7 @@ function StateSelection.rm_eqs_vars!(
348349
structure, eqs_to_rm, vars_to_rm; eqs_sorted_and_uniqued, vars_sorted_and_uniqued
349350
)
350351
deleteat!(state.fullvars, vars_to_rm)
352+
deleteat!(state.always_present, vars_to_rm)
351353
eqs = copy(MTKBase.get_eqs(state.sys))
352354
deleteat!(eqs, eqs_to_rm)
353355
deleteat!(state.original_eqs, eqs_to_rm)
@@ -361,3 +363,6 @@ function StateSelection.rm_eqs_vars!(
361363
return old_to_new_eq, old_to_new_var
362364
end
363365

366+
function StateSelection.is_unused_var(state::TearingState, var::Integer)
367+
return !state.always_present[var] && isempty(𝑑neighbors(state.structure.graph, var))
368+
end

lib/ModelingToolkitTearing/src/tearingstate.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ mutable struct TearingState <: StateSelection.TransformationState{System}
5959
are not used in the rest of the system.
6060
"""
6161
additional_observed::Vector{Equation}
62+
"""
63+
Corresponding to `fullvars`, marks variables which may not be structurally present in
64+
any equation according to `structure.graph` but should not be considered as unused.
65+
This is typically used by variables on the RHS of equations in `additional_observed`,
66+
and is useful for ensuring the consistency check is valid. For example, a simplification
67+
pass prior to `StateSelection.check_consistency` may process the equations
68+
```julia
69+
a ~ b
70+
b ~ c
71+
c ~ a
72+
```
73+
Into `additional_observed = [a ~ b, c ~ b]`, and thus end up in a state where
74+
there are no equations and `fullvars = [b]`. The consistency check would consider `b` as
75+
unused and the system as fully determined, but in reality `b` should be considered
76+
as used and the system singular.
77+
"""
78+
always_present::BitVector
6279
statemachines::Vector{System}
6380
"""
6481
Source information for each equation in the `TearingState`. `Vector{Symbol}` for each
@@ -325,7 +342,8 @@ function TearingState(sys::System, source_info::Union{Nothing, MTKBase.EquationS
325342
structure = SystemStructure(complete(var_to_diff), complete(eq_to_diff),
326343
complete(graph), nothing, var_types, state_priorities, false)
327344
return TearingState(sys, fullvars, structure, Equation[], param_derivative_map,
328-
no_deriv_params, original_eqs, Equation[], typeof(sys)[], sources)
345+
no_deriv_params, original_eqs, Equation[], falses(length(fullvars)),
346+
typeof(sys)[], sources)
329347
end
330348

331349
function build_state_priorities(sys::System, fullvars::Vector{SymbolicT}, var_to_diff::StateSelection.DiffGraph)

0 commit comments

Comments
 (0)