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

handle units for analysis points #297

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 4 additions & 0 deletions src/Blocks/analysis_points.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
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 Down Expand Up @@ -436,7 +440,7 @@
sys = expand_connections(sys, find, replace)

permutation_u = _check_and_sort!(input_name, aps_u, namespace_u, multiplicities_u)
un = ModelingToolkit.renamespace.(namespace_u, u)

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".

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".
if output_name isa SymOrVec
permutation_y = _check_and_sort!(output_name, aps_y, namespace_y, multiplicities_y)
yn = ModelingToolkit.renamespace.(namespace_y, y) # permutation applied in _check_and_sort
Expand All @@ -450,7 +454,7 @@
for f in [:get_sensitivity, :get_comp_sensitivity, :get_looptransfer, :open_loop]
@eval function $f(sys, ap::AnalysisPoint, args...; kwargs...)
$f(sys, nameof(ap), args...; kwargs...)
end

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".

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".
end

function ModelingToolkit.linearize(sys, input::AnalysisPoint, output::AnalysisPoint;
Expand Down
24 changes: 24 additions & 0 deletions test/Blocks/test_analysis_points.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,27 @@ L = CS.ss(matrices...) |> sminreal
matrices, _ = linearize(sys_outer, [:inner_plant_input], [:inner_plant_output])
G = CS.ss(matrices...) |> sminreal
@test tf(G) ≈ tf(CS.feedback(Ps, Cs))

## unit test
@mtkmodel SingleIntegrator begin
@parameters begin
to_mps = 1, [unit = u"m/s"]
end
@variables begin
(x(t) = 0), [unit = u"m", description = "Position"]
control(t), [unit = u"m/s", description = "Control Velocity"]
end
@components begin
uIn = RealInput()
c1 = Blocks.Constant(k = 1)
end
@equations begin
control ~ (uIn.u) * to_mps
D(x) ~ control
connect(c1.output, :test_point, uIn)
end
end
@mtkbuild single_integrator = SingleIntegrator()
prob = ODEProblem(single_integrator, [], (0, 10))
sol = solve(prob, Tsit5())
@test sol(10)[] ≈ 10
Loading