Skip to content

Commit

Permalink
feat: add utility function for obtaining defaults and guesses
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed May 7, 2024
1 parent 40626e9 commit cc31bf3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,10 @@ function defaults(sys::AbstractSystem)
isempty(systems) ? defs : mapfoldr(namespace_defaults, merge, systems; init = defs)
end

function defaults_and_guesses(sys::AbstractSystem)
merge(guesses(sys), defaults(sys))
end

unknowns(sys::Union{AbstractSystem, Nothing}, v) = renamespace(sys, v)
for vType in [Symbolics.Arr, Symbolics.Symbolic{<:AbstractArray}]
@eval unknowns(sys::AbstractSystem, v::$vType) = renamespace(sys, v)
Expand Down
8 changes: 4 additions & 4 deletions test/downstream/linearize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ lsyss, _ = ModelingToolkit.linearize_symbolic(pid, [reference.u, measurement.u],
[ctr_output.u])

@test substitute(
lsyss.A, merge(ModelingToolkit.defaults(pid), ModelingToolkit.guesses(pid))) == lsys.A
lsyss.A, ModelingToolkit.defaults_and_guesses(pid)) == lsys.A
@test substitute(
lsyss.B, merge(ModelingToolkit.defaults(pid), ModelingToolkit.guesses(pid))) == lsys.B
lsyss.B, ModelingToolkit.defaults_and_guesses(pid)) == lsys.B
@test substitute(
lsyss.C, merge(ModelingToolkit.defaults(pid), ModelingToolkit.guesses(pid))) == lsys.C
lsyss.C, ModelingToolkit.defaults_and_guesses(pid)) == lsys.C
@test substitute(
lsyss.D, merge(ModelingToolkit.defaults(pid), ModelingToolkit.guesses(pid))) == lsys.D
lsyss.D, ModelingToolkit.defaults_and_guesses(pid)) == lsys.D

# Test with the reverse desired unknown order as well to verify that similarity transform and reoreder_unknowns really works
lsys = ModelingToolkit.reorder_unknowns(lsys, unknowns(ssys), reverse(desired_order))
Expand Down
2 changes: 1 addition & 1 deletion test/input_output_handling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ if VERSION >= v"1.8" # :opaque_closure not supported before
x = randn(size(A, 1))
u = randn(size(B, 2))
p = getindex.(
Ref(merge(ModelingToolkit.defaults(ssys), ModelingToolkit.guesses(ssys))),
Ref(ModelingToolkit.defaults_and_guesses(ssys)),
parameters(ssys))
y1 = obsf(x, u, p, 0)
y2 = C * x + D * u
Expand Down

0 comments on commit cc31bf3

Please sign in to comment.