|
1 | 1 | using ModelingToolkit, OrdinaryDiffEq, JumpProcesses, IfElse |
2 | 2 | using Test |
3 | 3 | MT = ModelingToolkit |
| 4 | +UMT = ModelingToolkit.UnitfulUnitCheck |
4 | 5 | @parameters τ [unit = u"ms"] γ |
5 | 6 | @variables t [unit = u"ms"] E(t) [unit = u"kJ"] P(t) [unit = u"MW"] |
6 | 7 | D = Differential(t) |
7 | 8 |
|
8 | 9 | #This is how equivalent works: |
9 | | -@test MT.equivalent(u"MW", u"kJ/ms") |
10 | | -@test !MT.equivalent(u"m", u"cm") |
11 | | -@test MT.equivalent(MT.get_unit(P^γ), MT.get_unit((E / τ)^γ)) |
| 10 | +@test UMT.equivalent(u"MW", u"kJ/ms") |
| 11 | +@test !UMT.equivalent(u"m", u"cm") |
| 12 | +@test UMT.equivalent(UMT.get_unit(P^γ), UMT.get_unit((E / τ)^γ)) |
12 | 13 |
|
13 | 14 | # Basic access |
14 | | -@test MT.get_unit(t) == u"ms" |
15 | | -@test MT.get_unit(E) == u"kJ" |
16 | | -@test MT.get_unit(τ) == u"ms" |
17 | | -@test MT.get_unit(γ) == MT.unitless |
18 | | -@test MT.get_unit(0.5) == MT.unitless |
19 | | -@test MT.get_unit(MT.SciMLBase.NullParameters()) == MT.unitless |
| 15 | +@test UMT.get_unit(t) == u"ms" |
| 16 | +@test UMT.get_unit(E) == u"kJ" |
| 17 | +@test UMT.get_unit(τ) == u"ms" |
| 18 | +@test UMT.get_unit(γ) == UMT.unitless |
| 19 | +@test UMT.get_unit(0.5) == UMT.unitless |
| 20 | +@test UMT.get_unit(UMT.SciMLBase.NullParameters()) == UMT.unitless |
20 | 21 |
|
21 | 22 | # Prohibited unit types |
22 | 23 | @parameters β [unit = u"°"] α [unit = u"°C"] γ [unit = 1u"s"] |
23 | | -@test_throws MT.ValidationError MT.get_unit(β) |
24 | | -@test_throws MT.ValidationError MT.get_unit(α) |
25 | | -@test_throws MT.ValidationError MT.get_unit(γ) |
| 24 | +@test_throws UMT.ValidationError UMT.get_unit(β) |
| 25 | +@test_throws UMT.ValidationError UMT.get_unit(α) |
| 26 | +@test_throws UMT.ValidationError UMT.get_unit(γ) |
26 | 27 |
|
27 | 28 | # Non-trivial equivalence & operators |
28 | | -@test MT.get_unit(τ^-1) == u"ms^-1" |
29 | | -@test MT.equivalent(MT.get_unit(D(E)), u"MW") |
30 | | -@test MT.equivalent(MT.get_unit(E / τ), u"MW") |
31 | | -@test MT.get_unit(2 * P) == u"MW" |
32 | | -@test MT.get_unit(t / τ) == MT.unitless |
33 | | -@test MT.equivalent(MT.get_unit(P - E / τ), u"MW") |
34 | | -@test MT.equivalent(MT.get_unit(D(D(E))), u"MW/ms") |
35 | | -@test MT.get_unit(IfElse.ifelse(t > t, P, E / τ)) == u"MW" |
36 | | -@test MT.get_unit(1.0^(t / τ)) == MT.unitless |
37 | | -@test MT.get_unit(exp(t / τ)) == MT.unitless |
38 | | -@test MT.get_unit(sin(t / τ)) == MT.unitless |
39 | | -@test MT.get_unit(sin(1u"rad")) == MT.unitless |
40 | | -@test MT.get_unit(t^2) == u"ms^2" |
| 29 | +@test UMT.get_unit(τ^-1) == u"ms^-1" |
| 30 | +@test UMT.equivalent(UMT.get_unit(D(E)), u"MW") |
| 31 | +@test UMT.equivalent(UMT.get_unit(E / τ), u"MW") |
| 32 | +@test UMT.get_unit(2 * P) == u"MW" |
| 33 | +@test UMT.get_unit(t / τ) == UMT.unitless |
| 34 | +@test UMT.equivalent(UMT.get_unit(P - E / τ), u"MW") |
| 35 | +@test UMT.equivalent(UMT.get_unit(D(D(E))), u"MW/ms") |
| 36 | +@test UMT.get_unit(IfElse.ifelse(t > t, P, E / τ)) == u"MW" |
| 37 | +@test UMT.get_unit(1.0^(t / τ)) == UMT.unitless |
| 38 | +@test UMT.get_unit(exp(t / τ)) == UMT.unitless |
| 39 | +@test UMT.get_unit(sin(t / τ)) == UMT.unitless |
| 40 | +@test UMT.get_unit(sin(1u"rad")) == UMT.unitless |
| 41 | +@test UMT.get_unit(t^2) == u"ms^2" |
41 | 42 |
|
42 | 43 | eqs = [D(E) ~ P - E / τ |
43 | 44 | 0 ~ P] |
44 | | -@test MT.validate(eqs) |
| 45 | +@test UMT.validate(eqs) |
45 | 46 | @named sys = ODESystem(eqs) |
46 | 47 |
|
47 | | -@test !MT.validate(D(D(E)) ~ P) |
48 | | -@test !MT.validate(0 ~ P + E * τ) |
| 48 | +@test !UMT.validate(D(D(E)) ~ P) |
| 49 | +@test !UMT.validate(0 ~ P + E * τ) |
49 | 50 |
|
50 | 51 | # Disabling unit validation/checks selectively |
51 | 52 | @test_throws MT.ArgumentError ODESystem(eqs, t, [E, P, t], [τ], name = :sys) |
|
86 | 87 | good_eqs = [connect(p1, p2)] |
87 | 88 | bad_eqs = [connect(p1, p2, op)] |
88 | 89 | bad_length_eqs = [connect(op, lp)] |
89 | | -@test MT.validate(good_eqs) |
90 | | -@test !MT.validate(bad_eqs) |
91 | | -@test !MT.validate(bad_length_eqs) |
| 90 | +@test UMT.validate(good_eqs) |
| 91 | +@test !UMT.validate(bad_eqs) |
| 92 | +@test !UMT.validate(bad_length_eqs) |
92 | 93 | @named sys = ODESystem(good_eqs, t, [], []) |
93 | 94 | @test_throws MT.ValidationError ODESystem(bad_eqs, t, [], []; name = :sys) |
94 | 95 |
|
@@ -136,7 +137,7 @@ noiseeqs = [0.1u"MW" 0.1u"MW" |
136 | 137 | # Invalid noise matrix |
137 | 138 | noiseeqs = [0.1u"MW" 0.1u"MW" |
138 | 139 | 0.1u"MW" 0.1u"s"] |
139 | | -@test !MT.validate(eqs, noiseeqs) |
| 140 | +@test !UMT.validate(eqs, noiseeqs) |
140 | 141 |
|
141 | 142 | # Non-trivial simplifications |
142 | 143 | @variables t [unit = u"s"] V(t) [unit = u"m"^3] L(t) [unit = u"m"] |
|
0 commit comments