forked from SciML/ModelingToolkit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_variable_metadata.jl
144 lines (119 loc) · 3.89 KB
/
test_variable_metadata.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using ModelingToolkit
# Bounds
@variables u [bounds = (-1, 1)]
@test getbounds(u) == (-1, 1)
@test hasbounds(u)
@test ModelingToolkit.dump_variable_metadata(u).bounds == (-1, 1)
@variables y
@test !hasbounds(y)
@test !haskey(ModelingToolkit.dump_variable_metadata(y), :bounds)
# Guess
@variables y [guess = 0]
@test getguess(y) === 0
@test hasguess(y) === true
@test ModelingToolkit.dump_variable_metadata(y).guess == 0
# Default
@variables y = 0
@test ModelingToolkit.getdefault(y) === 0
@test ModelingToolkit.hasdefault(y) === true
@test ModelingToolkit.dump_variable_metadata(y).default == 0
# Issue#2653
@variables y[1:3] [guess = ones(3)]
@test getguess(y) == ones(3)
@test hasguess(y) === true
@test ModelingToolkit.dump_variable_metadata(y).guess == ones(3)
for i in 1:3
@test getguess(y[i]) == 1.0
@test hasguess(y[i]) === true
@test ModelingToolkit.dump_variable_metadata(y[i]).guess == 1.0
end
@variables y
@test hasguess(y) === false
@test !haskey(ModelingToolkit.dump_variable_metadata(y), :guess)
# Disturbance
@variables u [disturbance = true]
@test isdisturbance(u)
@test ModelingToolkit.dump_variable_metadata(u).disturbance
@variables y
@test !isdisturbance(y)
@test !haskey(ModelingToolkit.dump_variable_metadata(y), :disturbance)
# Tunable
@parameters u [tunable = true]
@test istunable(u)
@test ModelingToolkit.dump_variable_metadata(u).tunable
@parameters u2 [tunable = false]
@test !istunable(u2)
@test !ModelingToolkit.dump_variable_metadata(u2).tunable
@parameters y
@test istunable(y)
@test ModelingToolkit.dump_variable_metadata(y).tunable
# Distributions
struct FakeNormal end
d = FakeNormal()
@parameters u [dist = d]
@test hasdist(u)
@test getdist(u) == d
@test ModelingToolkit.dump_variable_metadata(u).dist == d
@parameters y
@test !hasdist(y)
@test !haskey(ModelingToolkit.dump_variable_metadata(y), :dist)
## System interface
@independent_variables t
Dₜ = Differential(t)
@variables x(t)=0 [bounds = (-10, 10)] u(t)=0 [input = true] y(t)=0 [output = true]
@parameters T [bounds = (0, Inf)]
@parameters k [tunable = true, bounds = (0, Inf)]
@parameters k2 [tunable = false]
eqs = [Dₜ(x) ~ (-k2 * x + k * u) / T
y ~ x]
sys = ODESystem(eqs, t, name = :tunable_first_order)
unk_meta = ModelingToolkit.dump_unknowns(sys)
@test length(unk_meta) == 3
@test all(iszero, meta.default for meta in unk_meta)
param_meta = ModelingToolkit.dump_parameters(sys)
@test length(param_meta) == 3
@test all(!haskey(meta, :default) for meta in param_meta)
p = tunable_parameters(sys)
sp = Set(p)
@test k ∈ sp
@test T ∈ sp
@test k2 ∉ sp
@test length(p) == 2
lb, ub = getbounds(p)
@test lb == [0, 0]
@test ub == [Inf, Inf]
b = getbounds(sys)
@test b[T] == (0, Inf)
b = getbounds(sys, unknowns(sys))
@test b[x] == (-10, 10)
p = tunable_parameters(sys, default = false)
sp = Set(p)
@test k ∈ sp
@test T ∉ sp
@test k2 ∉ sp
@test length(p) == 1
## Descriptions
@variables u [description = "This is my input"]
@test getdescription(u) == "This is my input"
@test hasdescription(u)
@test ModelingToolkit.dump_variable_metadata(u).desc == "This is my input"
@variables u
@test getdescription(u) == ""
@test !hasdescription(u)
@test !haskey(ModelingToolkit.dump_variable_metadata(u), :desc)
@independent_variables t
@variables u(t) [description = "A short description of u"]
@parameters p [description = "A description of p"]
@named sys = ODESystem([u ~ p], t)
@test_nowarn show(stdout, "text/plain", sys)
# Defaults overridden by system, parameter dependencies
@variables x(t) = 1.0
@parameters p=2.0 q
@named sys = ODESystem(Equation[], t, [x], [p]; defaults = Dict(x => 2.0, p => 3.0),
parameter_dependencies = [q => 2p])
x_meta = ModelingToolkit.dump_unknowns(sys)[]
@test x_meta.default == 2.0
params_meta = ModelingToolkit.dump_parameters(sys)
params_meta = Dict([ModelingToolkit.getname(meta.var) => meta for meta in params_meta])
@test params_meta[:p].default == 3.0
@test isequal(params_meta[:q].dependency, 2p)