Skip to content

Commit 3e74ac4

Browse files
committed
Improved formatting + tests for Difference & array operations.
1 parent 4dea72a commit 3e74ac4

File tree

1 file changed

+71
-14
lines changed

1 file changed

+71
-14
lines changed

test/units.jl

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using ModelingToolkit, Unitful
22
using Test
33
MT = ModelingToolkit
4-
@parameters τ [unit=u"ms"]
5-
@variables t [unit=u"ms"] E(t) [unit=u"kJ"] P(t) [unit=u"MW"]
4+
@parameters τ [unit = u"ms"]
5+
@variables t [unit = u"ms"] E(t) [unit = u"kJ"] P(t) [unit = u"MW"]
66
D = Differential(t)
77

88
@test MT.vartype(t) == u"ms"
99
@test MT.vartype(E) == u"kJ"
1010
@test MT.vartype(τ) == u"ms"
1111

12-
eqs = [D(E) ~ P-E/τ ]
12+
eqs = [D(E) ~ P - E/τ]
1313
sys = ODESystem(eqs)
1414

1515
@test MT.instantiate(eqs[1].lhs) == 1.0u"MW"
@@ -24,35 +24,92 @@ sys = ODESystem(eqs)
2424

2525
@test MT.instantiate^-1) == 1/u"ms"
2626
@test MT.instantiate(D(E)) == 1.0u"MW"
27-
@test MT.instantiate(E/τ) == 1.0u"MW"
27+
@test MT.instantiate(E/τ) == 1.0u"MW"
2828
@test MT.instantiate(2*P) == 1.0u"MW"
2929
@test MT.instantiate(t/τ) == 1.0
30-
@test MT.instantiate(P-E/τ)/1.0u"MW" == 1.0
30+
@test MT.instantiate(P - E/τ)/1.0u"MW" == 1.0
3131

3232
@test MT.instantiate(1.0^(t/τ)) == 1.0
3333
@test MT.instantiate(exp(t/τ)) == 1.0
3434
@test MT.instantiate(sin(t/τ)) == 1.0
3535
@test MT.instantiate(sin(1u"rad")) == 1.0
3636
@test MT.instantiate(t^2) == 1.0u"ms"^2
3737

38-
@test !MT.validate(E^1.5~ E^(t/τ))
39-
@test MT.validate(E^(t/τ)~ E^(t/τ))
38+
@test !MT.validate(E^1.5 ~ E^(t/τ))
39+
@test MT.validate(E^(t/τ) ~ E^(t/τ))
4040

41-
sys = ODESystem(eqs,t,[P,E],[τ])
41+
sys = ODESystem(eqs, t, [P, E], [τ])
4242
@test MT.validate(sys)
4343

44-
@test !MT.validate(D(D(E))~P)
45-
@test !MT.validate(0~P+E*τ)
44+
@test !MT.validate(D(D(E)) ~ P)
45+
@test !MT.validate(0 ~ P + E*τ)
4646
@test_logs (:warn,) MT.validate(0 ~ P + E*τ)
4747
@test_logs (:warn,) MT.validate(P + E*τ ~ 0)
4848
@test_logs (:warn,) MT.validate(P ~ 0)
4949

50+
#Unit-free
5051
@variables x y z u
5152
@parameters σ ρ β
52-
eqs = [0 ~ σ*(y-x)]
53-
@test MT.validate(eqs) #should cope with unit-free
53+
eqs = [0 ~ σ*(y - x)]
54+
@test MT.validate(eqs)
5455

55-
@variables t x[1:3,1:3](t) #should cope with arrays
56+
#Array variables
57+
@variables t x[1:3,1:3](t)
5658
D = Differential(t)
5759
eqs = D.(x) .~ x
58-
ODESystem(eqs)
60+
ODESystem(eqs)
61+
62+
# Array ops
63+
using Symbolics: unwrap, wrap
64+
using LinearAlgebra
65+
@variables t
66+
sts = @variables x[1:3](t) y(t)
67+
ps = @parameters p[1:3] = [1, 2, 3]
68+
D = Differential(t)
69+
eqs = [
70+
collect(D.(x) ~ x)
71+
D(y) ~ norm(x)*y
72+
]
73+
ODESystem(eqs, t, [sts...;], [ps...;])
74+
75+
#= Not supported yet b/c iterate doesn't work on unitful array
76+
# Array ops with units
77+
@variables t [unit =u"s"]
78+
sts = @variables x[1:3](t) [unit = u"kg"] y(t) [unit = u"kg"]
79+
ps = @parameters b [unit = u"s"^-1]
80+
D = Differential(t)
81+
eqs = [
82+
collect(D.(x) ~ b*x)
83+
D(y) ~ b*norm(x)
84+
]
85+
ODESystem(eqs, t, [sts...;], [ps...;])
86+
87+
#Array variables with units
88+
@variables t [unit = u"s"] x[1:3,1:3](t) [unit = u"kg"]
89+
@parameters a [unit = u"s"^-1]
90+
D = Differential(t)
91+
eqs = D.(x) .~ a*x
92+
ODESystem(eqs)
93+
=#
94+
95+
#Difference equation with units
96+
@parameters t [unit = u"s"] a [unit = u"s"^-1]
97+
@variables x(t) [unit = u"kg"]
98+
δ = Differential(t)
99+
D = Difference(t; dt = 0.1u"s")
100+
eqs = [
101+
δ(x) ~ a*x
102+
]
103+
de = ODESystem(eqs, t, [x, y], [a])
104+
105+
106+
@parameters t
107+
@variables y[1:3](t)
108+
@parameters k[1:3]
109+
D = Differential(t)
110+
111+
eqs = [D(y[1]) ~ -k[1]*y[1] + k[3]*y[2]*y[3],
112+
D(y[2]) ~ k[1]*y[1] - k[3]*y[2]*y[3] - k[2]*y[2]^2,
113+
0 ~ y[1] + y[2] + y[3] - 1]
114+
115+
sys = ODESystem(eqs,t,y,k)

0 commit comments

Comments
 (0)