Skip to content
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

fix: fix complete not properly expanding systems #3168

Merged
merged 1 commit into from
Oct 30, 2024
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
4 changes: 2 additions & 2 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,8 @@ function complete(sys::AbstractSystem; split = true, flatten = true)
@set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams)))
end
if flatten
if (eqs = equations(sys)) isa Vector &&
any(eq -> eq isa Equation && isconnection(eq.lhs), eqs)
eqs = equations(sys)
if eqs isa AbstractArray && eltype(eqs) <: Equation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a case where it's not a vector?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OptimizationSystem. equations(sys) returns the objective.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but is there any case that is an abstractarray that's not a vector?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JumpSystem stores it in an ArrayPartition, which is an AbstractVector but we don't want to expand_connections on that, and it would fail eltype <: Equation anyway.

newsys = expand_connections(sys)
else
newsys = sys
Expand Down
28 changes: 28 additions & 0 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1467,3 +1467,31 @@ end
obsfn(buf, prob.u0, prob.p, 0.0)
@test buf ≈ [1.0, 1.0, 2.0]
end

@testset "`complete` expands connections" begin
using ModelingToolkitStandardLibrary.Electrical
@mtkmodel RC begin
@parameters begin
R = 1.0
C = 1.0
V = 1.0
end
@components begin
resistor = Resistor(R = R)
capacitor = Capacitor(C = C, v = 0.0)
source = Voltage()
constant = Constant(k = V)
ground = Ground()
end
@equations begin
connect(constant.output, source.V)
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)
end
end
@named sys = RC()
total_eqs = length(equations(expand_connections(sys)))
sys2 = complete(sys)
@test length(equations(sys2)) == total_eqs
end
Loading