1
1
using ModelingToolkit, Unitful, OrdinaryDiffEq, DiffEqJump, IfElse
2
2
using Test
3
3
MT = ModelingToolkit
4
- @parameters τ [unit = u " ms" ]
4
+ @parameters τ [unit = u " ms" ] γ
5
5
@variables t [unit = u " ms" ] E (t) [unit = u " kJ" ] P (t) [unit = u " MW" ]
6
6
D = Differential (t)
7
7
8
+ # 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/ τ)^ γ))
12
+
13
+ # Basic access
8
14
@test MT. get_unit (t) == u " ms"
9
15
@test MT. get_unit (E) == u " kJ"
10
16
@test MT. get_unit (τ) == u " ms"
17
+ @test MT. get_unit (γ) == MT. unitless
18
+ @test MT. get_unit (0.5 ) == MT. unitless
11
19
20
+ # Prohibited unit types
12
21
@parameters β [unit = u " °" ] α [unit = u " °C" ] γ [unit = 1 u " s" ]
13
22
@test_throws MT. ValidationError MT. get_unit (β)
14
23
@test_throws MT. ValidationError MT. get_unit (α)
15
24
@test_throws MT. ValidationError MT. get_unit (γ)
16
25
17
- unitless = Unitful. unit (1 )
18
- @test MT. get_unit (0.5 ) == unitless
19
- @test MT. get_unit (t) == u " ms"
20
- @test MT. get_unit (P) == u " MW"
21
- @test MT. get_unit (τ) == u " ms"
22
-
26
+ # Non-trivial equivalence & operators
23
27
@test MT. get_unit (τ^- 1 ) == u " ms^-1"
24
28
@test MT. equivalent (MT. get_unit (D (E)),u " MW" )
25
29
@test MT. equivalent (MT. get_unit (E/ τ), u " MW" )
26
30
@test MT. get_unit (2 * P) == u " MW"
27
- @test MT. get_unit (t/ τ) == unitless
31
+ @test MT. get_unit (t/ τ) == MT . unitless
28
32
@test MT. equivalent (MT. get_unit (P - E/ τ),u " MW" )
29
33
@test MT. equivalent (MT. get_unit (D (D (E))),u " MW/ms" )
30
-
31
- @test MT. get_unit (1.0 ^ (t/ τ)) == unitless
32
- @test MT. get_unit (exp (t/ τ)) == unitless
33
- @test MT. get_unit (sin (t/ τ)) == unitless
34
- @test MT. get_unit (sin (1 u " rad" )) == unitless
34
+ @test MT . get_unit (IfElse . ifelse (t > t,P,E / τ)) == u " MW "
35
+ @test MT. get_unit (1.0 ^ (t/ τ)) == MT . unitless
36
+ @test MT. get_unit (exp (t/ τ)) == MT . unitless
37
+ @test MT. get_unit (sin (t/ τ)) == MT . unitless
38
+ @test MT. get_unit (sin (1 u " rad" )) == MT . unitless
35
39
@test MT. get_unit (t^ 2 ) == u " ms^2"
36
40
37
- @test ! MT. validate (E^ 1.5 ~ E^ (t/ τ))
38
- @test MT. validate (E^ (t/ τ) ~ E^ (t/ τ))
39
-
40
41
eqs = [D (E) ~ P - E/ τ
41
42
0 ~ P]
42
43
@test MT. validate (eqs)
43
44
@named sys = ODESystem (eqs)
44
- @named sys = ODESystem (eqs, t, [P, E], [τ])
45
45
46
46
@test ! MT. validate (D (D (E)) ~ P)
47
47
@test ! MT. validate (0 ~ P + E* τ)
48
48
49
- # Unit-free
50
- @variables x y z u
51
- @parameters σ ρ β
52
- eqs = [0 ~ σ* (y - x)]
53
- @test MT. validate (eqs)
54
-
55
- # #Array variables
49
+ # Array variables
56
50
@variables t [unit = u " s" ] x[1 : 3 ](t) [unit = u " m" ]
57
51
@parameters v[1 : 3 ] = [1 ,2 ,3 ] [unit = u " m/s" ]
58
52
D = Differential (t)
59
53
eqs = D .(x) .~ v
60
54
ODESystem (eqs,name= :sys )
61
55
62
- # Difference equation with units
56
+ # Difference equation
63
57
@parameters t [unit = u " s" ] a [unit = u " s" ^- 1 ]
64
58
@variables x (t) [unit = u " kg" ]
65
59
δ = Differential (t)
@@ -69,18 +63,6 @@ eqs = [
69
63
]
70
64
de = ODESystem (eqs, t, [x], [a],name= :sys )
71
65
72
-
73
- @parameters t
74
- @variables y[1 : 3 ](t)
75
- @parameters k[1 : 3 ]
76
- D = Differential (t)
77
-
78
- eqs = [D (y[1 ]) ~ - k[1 ]* y[1 ] + k[3 ]* y[2 ]* y[3 ],
79
- D (y[2 ]) ~ k[1 ]* y[1 ] - k[3 ]* y[2 ]* y[3 ] - k[2 ]* y[2 ]^ 2 ,
80
- 0 ~ y[1 ] + y[2 ] + y[3 ] - 1 ]
81
-
82
- @named sys = ODESystem (eqs,t,y,k)
83
-
84
66
# Nonlinear system
85
67
@parameters a [unit = u " kg" ^- 1 ]
86
68
@variables x [unit = u " kg" ]
@@ -99,16 +81,18 @@ eqs = [D(E) ~ P - E/τ
99
81
noiseeqs = [0.1 u " MW" ,
100
82
0.1 u " MW" ]
101
83
@named sys = SDESystem (eqs, noiseeqs, t, [P, E], [τ, Q])
84
+
102
85
# With noise matrix
103
86
noiseeqs = [0.1 u " MW" 0.1 u " MW"
104
87
0.1 u " MW" 0.1 u " MW" ]
105
88
@named sys = SDESystem (eqs,noiseeqs, t, [P, E], [τ, Q])
106
89
90
+ # Invalid noise matrix
107
91
noiseeqs = [0.1 u " MW" 0.1 u " MW"
108
92
0.1 u " MW" 0.1 u " s" ]
109
93
@test ! MT. validate (eqs,noiseeqs)
110
94
111
- # Test non -trivial simplifications
95
+ # Non -trivial simplifications
112
96
@variables t [unit = u " s" ] V (t) [unit = u " m" ^ 3 ] L (t) [unit = u " m" ]
113
97
@parameters v [unit = u " m/s" ] r [unit = u " m" ^ 3 / u " s" ]
114
98
D = Differential (t)
@@ -166,12 +150,3 @@ maj1 = MassActionJump(2.0, [0 => 1], [S => 1])
166
150
maj2 = MassActionJump (γ, [S => 1 ], [S => - 1 ])
167
151
@named js4 = JumpSystem ([maj1, maj2], t, [S], [β, γ])
168
152
169
- # Test comparisons
170
- @parameters t
171
- vars = @variables x (t)
172
- D = Differential (t)
173
- eqs = [
174
- D (x) ~ IfElse. ifelse (t> 0.1 ,2 ,1 )
175
- ]
176
- @named sys = ODESystem (eqs, t, vars, [])
177
-
0 commit comments