Skip to content

Commit

Permalink
fix: handle loop openings in same ap as IO
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed May 30, 2024
1 parent 200b13a commit 00601f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/Blocks/analysis_points.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function Base.hash(ap::AnalysisPoint, seed::UInt)
h3 (0xd29cdc51aa6562d4 % UInt)
end

function ModelingToolkit.get_unit(ap::AnalysisPoint)
ModelingToolkit.unitless

Check warning on line 22 in src/Blocks/analysis_points.jl

View check run for this annotation

Codecov / codecov/patch

src/Blocks/analysis_points.jl#L21-L22

Added lines #L21 - L22 were not covered by tests
end

function ap_var(sys)
if hasproperty(sys, :u)
# collect to turn symbolic arrays into arrays of symbols
Expand All @@ -28,6 +32,23 @@ function ap_var(sys)
error("Could not determine the analysis-point variable in system $(nameof(sys)). To use an analysis point, apply it to a connection between two causal blocks containing connectors of type `RealInput/RealOutput` from ModelingToolkitStandardLibrary.Blocks.")
end

"""
find_analysis_points(sys)
Return a list of all analysis points in `sys`. If none are found, the list is empty.
"""
function find_analysis_points(sys)
sys = ModelingToolkit.flatten(sys)
eqs = equations(sys)
aps = []
for eq in eqs
if eq.rhs isa AnalysisPoint
push!(aps, eq.rhs)
end
end
aps

Check warning on line 49 in src/Blocks/analysis_points.jl

View check run for this annotation

Codecov / codecov/patch

src/Blocks/analysis_points.jl#L49

Added line #L49 was not covered by tests
end

"""
AnalysisPoint(in, out, name::Symbol)
AnalysisPoint(in, out; name::Symbol)
Expand Down Expand Up @@ -418,16 +439,26 @@ function ModelingToolkit.linearization_function(sys::ModelingToolkit.AbstractSys
ui = get_perturbation_var(ap_var(ap.out), "u")
push!(multiplicities_u, length(ui)) # one ap may yield several new vars
append!(u, ui)
[ap_var(ap.out) .~ ap_var(ap.in) + ui;], ui
if loop_openings !== nothing && ap.name loop_openings
# In thise case, we break the existing connection.

Check warning on line 443 in src/Blocks/analysis_points.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"thise" should be "these" or "this".
[ap_var(ap.out) .~ ui;], ui
else
[ap_var(ap.out) .~ ap_var(ap.in) + ui;], ui
end
#input.in.u ~ 0] # We only need to ground one of the ends, hence not including this equation
elseif output_name isa SymOrVec && namespaced_ap_match(ap, ns, output_name, nothing)
push!(namespace_y, ns) # Save the namespace to make it available for renamespace below
push!(aps_y, ap)
yi = get_perturbation_var(ap_var(ap.in), "y")
push!(multiplicities_y, length(yi))
append!(y, yi)
[ap_var(ap.in) .~ yi;
ap_var(ap.out) .~ ap_var(ap.in)], yi
if loop_openings !== nothing && ap.name loop_openings
[ap_var(ap.in) .~ yi;
ap_var(ap.out) .~ 0], yi # In thise case, we break the existing connection.

Check warning on line 457 in src/Blocks/analysis_points.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"thise" should be "these" or "this".
else
[ap_var(ap.in) .~ yi;
ap_var(ap.out) .~ ap_var(ap.in)], yi
end
else # loop opening
[ap_var(ap.out) .~ 0;], []
end
Expand Down
8 changes: 8 additions & 0 deletions test/Blocks/test_analysis_points.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ Si = ss(matrices...)
t,
systems = [P_inner, feedback, ref])

@test_nowarn Blocks.find_analysis_points(sys_inner)
P_not_broken, _ = linearize(sys_inner, :u, :y)
@test P_not_broken.A[] == -2
P_broken, _ = linearize(sys_inner, :u, :y, loop_openings = [:u])
@test P_broken.A[] == -1
P_broken, _ = linearize(sys_inner, :u, :y, loop_openings = [:y])
@test P_broken.A[] == -1

Sinner = sminreal(ss(get_sensitivity(sys_inner, :u)[1]...))

@named sys_inner = ODESystem(
Expand Down

0 comments on commit 00601f2

Please sign in to comment.