|
1 | | -using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks, ControlSystemsBase |
2 | | -using ModelingToolkitStandardLibrary.Mechanical.Rotational |
3 | | -using ModelingToolkitStandardLibrary.Blocks |
4 | | -using OrdinaryDiffEq, LinearAlgebra |
5 | | -using Test |
| 1 | +using ModelingToolkit |
6 | 2 | using ModelingToolkit: t_nounits as t, D_nounits as D, AnalysisPoint, AbstractSystem |
7 | 3 | import ModelingToolkit as MTK |
| 4 | +using ModelingToolkitStandardLibrary |
| 5 | +using ModelingToolkitStandardLibrary.Blocks |
| 6 | +using ModelingToolkitStandardLibrary.Mechanical.Rotational |
| 7 | +using ControlSystemsBase |
8 | 8 | import ControlSystemsBase as CS |
| 9 | +using OrdinaryDiffEq, LinearAlgebra |
| 10 | +using Test |
9 | 11 | using Symbolics: NAMESPACE_SEPARATOR |
| 12 | +using Unitful |
| 13 | + |
| 14 | +@testset "AnalysisPoint is ignored when verifying units" begin |
| 15 | + # no units first |
| 16 | + @mtkmodel FirstOrderTest begin |
| 17 | + @components begin |
| 18 | + in = Step() |
| 19 | + fb = Feedback() |
| 20 | + fo = SecondOrder(k = 1, w = 1, d = 0.1) |
| 21 | + end |
| 22 | + @equations begin |
| 23 | + connect(in.output, :u, fb.input1) |
| 24 | + connect(fb.output, :e, fo.input) |
| 25 | + connect(fo.output, :y, fb.input2) |
| 26 | + end |
| 27 | + end |
| 28 | + @named model = FirstOrderTest() |
| 29 | + @test model isa System |
| 30 | + |
| 31 | + @connector function UnitfulOutput(; name) |
| 32 | + vars = @variables begin |
| 33 | + u(t), [unit=u"m", output=true] |
| 34 | + end |
| 35 | + return System(Equation[], t, vars, []; name) |
| 36 | + end |
| 37 | + @connector function UnitfulInput(; name) |
| 38 | + vars = @variables begin |
| 39 | + u(t), [unit=u"m", input=true] |
| 40 | + end |
| 41 | + return System(Equation[], t, vars, []; name) |
| 42 | + end |
| 43 | + @component function UnitfulBlock(; name) |
| 44 | + pars = @parameters begin |
| 45 | + offset, [unit=u"m"] |
| 46 | + start_time |
| 47 | + height, [unit=u"m"] |
| 48 | + duration |
| 49 | + end |
| 50 | + systems = @named begin |
| 51 | + output = UnitfulOutput() |
| 52 | + end |
| 53 | + eqs = [ |
| 54 | + output.u ~ offset + height*(0.5 + (1/pi)*atan(1e5*(t - start_time))) |
| 55 | + ] |
| 56 | + return System(eqs, t, [], pars; systems, name) |
| 57 | + end |
| 58 | + @mtkmodel TestAPAroundUnits begin |
| 59 | + @components begin |
| 60 | + input = UnitfulInput() |
| 61 | + end |
| 62 | + @variables begin |
| 63 | + output(t), [output=true, unit=u"m^2"] |
| 64 | + end |
| 65 | + @components begin |
| 66 | + ub = UnitfulBlock() |
| 67 | + end |
| 68 | + @equations begin |
| 69 | + connect(ub.output, :ap, input) |
| 70 | + output ~ input.u^2 |
| 71 | + end |
| 72 | + end |
| 73 | + @named sys = TestAPAroundUnits() |
| 74 | + @test sys isa System |
| 75 | + |
| 76 | + @mtkmodel TestAPWithNoOutputs begin |
| 77 | + @components begin |
| 78 | + input = UnitfulInput() |
| 79 | + end |
| 80 | + @variables begin |
| 81 | + output(t), [output=true, unit=u"m^2"] |
| 82 | + end |
| 83 | + @components begin |
| 84 | + ub = UnitfulBlock() |
| 85 | + end |
| 86 | + @equations begin |
| 87 | + connect(ub.output, :ap, input) |
| 88 | + output ~ input.u^2 |
| 89 | + end |
| 90 | + end |
| 91 | + @named sys2 = TestAPWithNoOutputs() |
| 92 | + @test sys2 isa System |
| 93 | +end |
10 | 94 |
|
11 | 95 | @testset "AnalysisPoint is lowered to `connect`" begin |
12 | 96 | @named P = FirstOrder(k = 1, T = 1) |
13 | | - @named C = Gain(; k = -1) |
| 97 | + @named C = ModelingToolkitStandardLibrary.Blocks.Gain(; k = -1) |
14 | 98 |
|
15 | 99 | ap = AnalysisPoint(:plant_input) |
16 | 100 | eqs = [connect(P.output, C.input) |
|
30 | 114 |
|
31 | 115 | @testset "Inverse causality throws a warning" begin |
32 | 116 | @named P = FirstOrder(k = 1, T = 1) |
33 | | - @named C = Gain(; k = -1) |
| 117 | + @named C = ModelingToolkitStandardLibrary.Blocks.Gain(; k = -1) |
34 | 118 |
|
35 | 119 | ap = AnalysisPoint(:plant_input) |
36 | 120 | @test_warn ["1-th argument", "plant_input", "not a output"] connect( |
|
41 | 125 | # also tests `connect(input, name::Symbol, outputs...)` syntax |
42 | 126 | @testset "AnalysisPoint is accessible via `getproperty`" begin |
43 | 127 | @named P = FirstOrder(k = 1, T = 1) |
44 | | - @named C = Gain(; k = -1) |
| 128 | + @named C = ModelingToolkitStandardLibrary.Blocks.Gain(; k = -1) |
45 | 129 |
|
46 | 130 | eqs = [connect(P.output, C.input), connect(C.output, :plant_input, P.input)] |
47 | 131 | sys_ap = System(eqs, t, systems = [P, C], name = :hej) |
|
61 | 145 | ### Ported from MTKStdlib |
62 | 146 |
|
63 | 147 | @named P = FirstOrder(k = 1, T = 1) |
64 | | -@named C = Gain(; k = -1) |
| 148 | +@named C = ModelingToolkitStandardLibrary.Blocks.Gain(; k = -1) |
65 | 149 |
|
66 | 150 | ap = AnalysisPoint(:plant_input) |
67 | 151 | eqs = [connect(P.output, C.input), connect(C.output, ap, P.input)] |
|
266 | 350 |
|
267 | 351 | @testset "Duplicate `connect` statements across subsystems with AP transforms - standard `connect`" begin |
268 | 352 | @named P = FirstOrder(k = 1, T = 1) |
269 | | - @named C = Gain(; k = 1) |
| 353 | + @named C = ModelingToolkitStandardLibrary.Blocks.Gain(; k = 1) |
270 | 354 | @named add = Blocks.Add(k2 = -1) |
271 | 355 |
|
272 | 356 | eqs = [connect(P.output, :plant_output, add.input2) |
|
302 | 386 |
|
303 | 387 | @testset "Duplicate `connect` statements across subsystems with AP transforms - causal variable `connect`" begin |
304 | 388 | @named P = FirstOrder(k = 1, T = 1) |
305 | | - @named C = Gain(; k = 1) |
| 389 | + @named C = ModelingToolkitStandardLibrary.Blocks.Gain(; k = 1) |
306 | 390 | @named add = Blocks.Add(k2 = -1) |
307 | 391 |
|
308 | 392 | eqs = [connect(P.output.u, :plant_output, add.input2.u) |
|
338 | 422 |
|
339 | 423 | @testset "Duplicate `connect` statements across subsystems with AP transforms - mixed `connect`" begin |
340 | 424 | @named P = FirstOrder(k = 1, T = 1) |
341 | | - @named C = Gain(; k = 1) |
| 425 | + @named C = ModelingToolkitStandardLibrary.Blocks.Gain(; k = 1) |
342 | 426 | @named add = Blocks.Add(k2 = -1) |
343 | 427 |
|
344 | 428 | eqs = [connect(P.output.u, :plant_output, add.input2.u) |
|
460 | 544 |
|
461 | 545 | @testset "multiple analysis points" begin |
462 | 546 | @named P = FirstOrder(k = 1, T = 1) |
463 | | - @named C = Gain(; k = 1) |
| 547 | + @named C = ModelingToolkitStandardLibrary.Blocks.Gain(; k = 1) |
464 | 548 | @named add = Blocks.Add(k2 = -1) |
465 | 549 |
|
466 | 550 | eqs = [connect(P.output, :plant_output, add.input2) |
|
0 commit comments