Skip to content

feat: reduce reliance on metadata in structural_simplify #3540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
7f37457
fix: don't `toparam` inside `Initial`
AayushSabharwal Apr 4, 2025
e583e0a
feat: reduce reliance on metadata in `structural_simplify`
AayushSabharwal Apr 4, 2025
42063ec
fix: fix `linearization_function` with analysis points mutating the s…
AayushSabharwal May 19, 2025
49f0a40
fix: properly handle array equations during variable discovery in `Sy…
AayushSabharwal May 19, 2025
05e48c8
test: fix broken tearing test
AayushSabharwal May 19, 2025
7b9c6ab
test: fix missing subcomponent in components test
AayushSabharwal May 19, 2025
7888ca7
fix: fix incorrect default in DDE test
AayushSabharwal May 19, 2025
fb01ce5
fix: fix parameter not passed to `System` in units test
AayushSabharwal May 19, 2025
ffd73ae
fix: fix parameter not passed to system in error handling test
AayushSabharwal May 19, 2025
4909677
test: update test with new simplification result
AayushSabharwal May 19, 2025
99978e2
test: fix variable not passed to `System` in odesystem test
AayushSabharwal May 19, 2025
96bbec2
refactor: don't check `isparameter` in `check_variables` and `check_p…
AayushSabharwal May 20, 2025
d855dfd
fix: don't use `isparameter` in `find_eq_solvables!`
AayushSabharwal May 20, 2025
6fdb54b
fix: don't use `isparameter` in `generate_initializesystem`
AayushSabharwal May 20, 2025
81eb182
test: test simplification independent of metadata
AayushSabharwal May 20, 2025
566b288
fix: unwrap brownians in `noise_to_brownians`
AayushSabharwal May 21, 2025
249325c
test: pass `@constants` parameter to `System` constructor
AayushSabharwal May 21, 2025
4deb198
test: pass brownians to `System` constructor
AayushSabharwal May 21, 2025
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
2 changes: 1 addition & 1 deletion src/inputoutput.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function inputs_to_parameters!(state::TransformationState, inputsyms)

@set! sys.ps = [ps; new_parameters]
@set! state.sys = sys
@set! state.fullvars = new_fullvars
@set! state.fullvars = Vector{BasicSymbolic}(new_fullvars)
@set! state.structure = structure
return state
end
Expand Down
1 change: 1 addition & 0 deletions src/structural_transformation/StructuralTransformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ using ModelingToolkit: algeqs, EquationsView,
dervars_range, diffvars_range, algvars_range,
DiffGraph, complete!,
get_fullvars, system_subset
using SymbolicIndexingInterface: symbolic_type, ArraySymbolic

using ModelingToolkit.DiffEqBase
using ModelingToolkit.StaticArrays
Expand Down
12 changes: 9 additions & 3 deletions src/structural_transformation/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,15 @@ function find_eq_solvables!(state::TearingState, ieq, to_rm = Int[], coeffs = no
all_int_vars = false
if !allow_symbolic
if allow_parameter
all(
x -> ModelingToolkit.isparameter(x),
vars(a)) || continue
# if any of the variables in `a` are present in fullvars (taking into account arrays)
if any(
v -> any(isequal(v), fullvars) ||
symbolic_type(v) == ArraySymbolic() &&
Symbolics.shape(v) != Symbolics.Unknown() &&
any(x -> any(isequal(x), fullvars), collect(v)),
vars(a))
continue
end
else
continue
end
Expand Down
6 changes: 3 additions & 3 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -648,14 +648,14 @@ function (f::Initial)(x)
iscall(x) && operation(x) isa Initial && return x
result = if symbolic_type(x) == ArraySymbolic()
# create an array for `Initial(array)`
Symbolics.array_term(f, toparam(x))
Symbolics.array_term(f, x)
elseif iscall(x) && operation(x) == getindex
# instead of `Initial(x[1])` create `Initial(x)[1]`
# which allows parameter indexing to handle this case automatically.
arr = arguments(x)[1]
term(getindex, f(toparam(arr)), arguments(x)[2:end]...)
term(getindex, f(arr), arguments(x)[2:end]...)
else
term(f, toparam(x))
term(f, x)
end
# the result should be a parameter
result = toparam(result)
Expand Down
2 changes: 1 addition & 1 deletion src/systems/analysis_points.jl
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ function linearization_function(sys::AbstractSystem,
if output isa AnalysisPoint
sys, (output_var,) = apply_transformation(AddVariable(output), sys)
sys, (input_var,) = apply_transformation(GetInput(output), sys)
push!(get_eqs(sys), output_var ~ input_var)
@set! sys.eqs = [get_eqs(sys); output_var ~ input_var]
else
output_var = output
end
Expand Down
2 changes: 1 addition & 1 deletion src/systems/diffeqs/basic_transformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ function noise_to_brownians(sys::System; names::Union{Symbol, Vector{Symbol}} =
"""))
end
brownvars = map(names) do name
only(@brownian $name)
unwrap(only(@brownian $name))
end

terms = if ndims(neqs) == 1
Expand Down
10 changes: 9 additions & 1 deletion src/systems/nonlinear/initializesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,15 @@ function generate_initializesystem_timeindependent(sys::AbstractSystem;
initialization_eqs = filter(initialization_eqs) do eq
empty!(vs)
vars!(vs, eq; op = Initial)
non_params = filter(!isparameter, vs)
allpars = full_parameters(sys)
for p in allpars
if symbolic_type(p) == ArraySymbolic() &&
Symbolics.shape(p) != Symbolics.Unknown()
append!(allpars, Symbolics.scalarize(p))
end
end
allpars = Set(allpars)
non_params = filter(!in(allpars), vs)
# error if non-parameters are present in the initialization equations
if !isempty(non_params)
throw(UnknownsInTimeIndependentInitializationError(eq, non_params))
Expand Down
2 changes: 1 addition & 1 deletion src/systems/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function System(eqs::Vector{Equation}, iv; kwargs...)
diffeqs = Equation[]
othereqs = Equation[]
for eq in eqs
if !(eq.lhs isa Union{Symbolic, Number})
if !(eq.lhs isa Union{Symbolic, Number, AbstractArray})
push!(othereqs, eq)
continue
end
Expand Down
Loading
Loading