Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/structural_transformation/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ end
### Structural and symbolic utilities
###

function find_eq_solvables!(state::TearingState, ieq; may_be_zero=false, allow_symbolic=true)
function find_eq_solvables!(state::TearingState, ieq; may_be_zero=false, allow_symbolic=false, allow_parameter=true)
fullvars = state.fullvars
@unpack graph, solvable_graph = state.structure
eq = equations(state)[ieq]
Expand All @@ -171,7 +171,13 @@ function find_eq_solvables!(state::TearingState, ieq; may_be_zero=false, allow_s
a = unwrap(a)
islinear || continue
if a isa Symbolic
allow_symbolic || continue
if !allow_symbolic
if allow_parameter
ModelingToolkit.isparameter(a) || continue
else
continue
end
end
add_edge!(solvable_graph, ieq, j)
continue
end
Expand All @@ -191,7 +197,7 @@ function find_eq_solvables!(state::TearingState, ieq; may_be_zero=false, allow_s
end
end

function find_solvables!(state::TearingState; allow_symbolic=true)
function find_solvables!(state::TearingState; allow_symbolic=false)
@assert state.structure.solvable_graph === nothing
eqs = equations(state)
graph = state.structure.graph
Expand Down
37 changes: 21 additions & 16 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,17 @@ const EMPTY_JAC = Matrix{Num}(undef, 0, 0)
function invalidate_cache!(sys::AbstractSystem)
if has_tgrad(sys)
get_tgrad(sys)[] = EMPTY_TGRAD
elseif has_jac(sys)
end
if has_jac(sys)
get_jac(sys)[] = EMPTY_JAC
elseif has_ctrl_jac(sys)
end
if has_ctrl_jac(sys)
get_ctrl_jac(sys)[] = EMPTY_JAC
elseif has_Wfact(sys)
end
if has_Wfact(sys)
get_Wfact(sys)[] = EMPTY_JAC
elseif has_Wfact_t(sys)
end
if has_Wfact_t(sys)
get_Wfact_t(sys)[] = EMPTY_JAC
end
return sys
Expand Down Expand Up @@ -380,6 +384,7 @@ renamespace(sys, eq::Equation) = namespace_equation(eq, sys)

renamespace(names::AbstractVector, x) = foldr(renamespace, names, init=x)
function renamespace(sys, x)
sys === nothing && return x
x = unwrap(x)
if x isa Symbolic
let scope = getmetadata(x, SymScope, LocalScope())
Expand Down Expand Up @@ -413,9 +418,9 @@ function namespace_equations(sys::AbstractSystem)
map(eq->namespace_equation(eq, sys), eqs)
end

function namespace_equation(eq::Equation, sys)
_lhs = namespace_expr(eq.lhs, sys)
_rhs = namespace_expr(eq.rhs, sys)
function namespace_equation(eq::Equation, sys, n=nameof(sys))
_lhs = namespace_expr(eq.lhs, sys, n)
_rhs = namespace_expr(eq.rhs, sys, n)
_lhs ~ _rhs
end

Expand All @@ -425,22 +430,22 @@ function namespace_assignment(eq::Assignment, sys)
Assignment(_lhs, _rhs)
end

function namespace_expr(O, sys) where {T}
function namespace_expr(O, sys, n=nameof(sys)) where {T}
ivs = independent_variables(sys)
O = unwrap(O)
if any(isequal(O), ivs)
return O
elseif isvariable(O)
renamespace(sys, O)
renamespace(n, O)
elseif istree(O)
renamed = map(a->namespace_expr(a, sys), arguments(O))
renamed = map(a->namespace_expr(a, sys, n), arguments(O))
if symtype(operation(O)) <: FnType
renamespace(sys, O)
renamespace(n, O)
else
similarterm(O, operation(O), renamed)
end
elseif O isa Array
map(Base.Fix2(namespace_expr, sys), O)
map(o->namespace_expr(o, sys, n), O)
else
O
end
Expand Down Expand Up @@ -504,7 +509,7 @@ for f in [:states, :parameters]
@eval $f(sys::AbstractSystem, vs::AbstractArray) = map(v->$f(sys, v), vs)
end

flatten(sys::AbstractSystem) = sys
flatten(sys::AbstractSystem, args...) = sys

function equations(sys::ModelingToolkit.AbstractSystem)
eqs = get_eqs(sys)
Expand Down Expand Up @@ -1034,14 +1039,14 @@ $(SIGNATURES)
compose multiple systems together. The resulting system would inherit the first
system's name.
"""
function compose(sys::AbstractSystem, systems::AbstractArray{<:AbstractSystem}; name=nameof(sys))
function compose(sys::AbstractSystem, systems::AbstractArray; name=nameof(sys))
nsys = length(systems)
nsys >= 1 || throw(ArgumentError("There must be at least 1 subsystem. Got $nsys subsystems."))
nsys == 0 && return sys
@set! sys.name = name
@set! sys.systems = [get_systems(sys); systems]
return sys
end
compose(syss::AbstractSystem...; name=nameof(first(syss))) = compose(first(syss), collect(syss[2:end]); name=name)
compose(syss...; name=nameof(first(syss))) = compose(first(syss), collect(syss[2:end]); name=name)
Base.:(∘)(sys1::AbstractSystem, sys2::AbstractSystem) = compose(sys1, sys2)

UnPack.unpack(sys::ModelingToolkit.AbstractSystem, ::Val{p}) where p = getproperty(sys, p; namespace=false)
Loading