Skip to content

Commit eec3725

Browse files
fixes tests
1 parent b51ba0c commit eec3725

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/Blocks/math.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Output first input divided by second input.
175175
"""
176176
function Division(;name)
177177
@named input1 = RealInput()
178-
@named input2 = RealInput()
178+
@named input2 = RealInput(u_start=1.0) # denominator can not be zero
179179
@named output = RealOutput()
180180
eqs= [
181181
output.u ~ input1.u / input2.u

test/Blocks/math.jl

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
using ModelingToolkitStandardLibrary.Blocks
2-
using ModelingToolkit, OrdinaryDiffEq
2+
using ModelingToolkit, OrdinaryDiffEq, Plots, Test
3+
using ModelingToolkitStandardLibrary.Blocks: _clamp, _dead_zone
4+
using ModelingToolkit: inputs, unbound_inputs, bound_inputs
35

46
@parameters t
57

6-
78
@testset "Gain" begin
89
@named c = Constant(; k=1)
910
@named gain = Gain(1;)
1011
@named int = Integrator(; k=1)
1112
@named model = ODESystem([connect(c.output, gain.input), connect(gain.output, int.input)], t, systems=[int, gain, c])
13+
1214
sys = structural_simplify(model)
13-
1415
prob = ODEProblem(sys, Pair[int.x=>1.0], (0.0, 1.0))
15-
1616
sol = solve(prob, Rodas4())
1717

18+
@test isequal(unbound_inputs(sys), [])
1819
@test sol.retcode == :Success
1920
@test all(sol[c.output.u] .≈ 1)
2021
@test sol[int.output.u][end] 2 # expected solution after 1s
@@ -40,6 +41,7 @@ end
4041
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 100.0))
4142

4243
sol = solve(prob, Rodas4())
44+
@test isequal(unbound_inputs(sys), [])
4345
@test sol.retcode == :Success
4446
@test sol[int.output.u][end] 2 # expected solution after 1s
4547
end
@@ -61,6 +63,7 @@ end
6163
sys = structural_simplify(model)
6264
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
6365
sol = solve(prob, Rodas4())
66+
@test isequal(unbound_inputs(sys), [])
6467
@test sol.retcode == :Success
6568
@test sol[add.output.u] 1 .+ sin.(2*pi*sol.t)
6669

@@ -80,6 +83,7 @@ end
8083
sys = structural_simplify(model)
8184
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
8285
sol = solve(prob, Rodas4())
86+
@test isequal(unbound_inputs(sys), [])
8387
@test sol.retcode == :Success
8488
@test sol[add.output.u] k1 .* 1 .+ k2 .* sin.(2*pi*sol.t)
8589
end
@@ -104,6 +108,7 @@ end
104108
sys = structural_simplify(model)
105109
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
106110
sol = solve(prob, Rodas4())
111+
@test isequal(unbound_inputs(sys), [])
107112
@test sol.retcode == :Success
108113
@test sol[add.output.u] 1 .+ sin.(2*pi*sol.t) .+ sin.(2*pi*2*sol.t)
109114

@@ -125,6 +130,7 @@ end
125130
sys = structural_simplify(model)
126131
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
127132
sol = solve(prob, Rodas4())
133+
@test isequal(unbound_inputs(sys), [])
128134
@test sol.retcode == :Success
129135
@test sol[add.output.u] k1 .* 1 .+ k2 .* sin.(2*pi*sol.t) .+ k3 .* sin.(2*pi*2*sol.t)
130136

@@ -148,6 +154,7 @@ end
148154
sys = structural_simplify(model)
149155
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
150156
sol = solve(prob, Rodas4())
157+
@test isequal(unbound_inputs(sys), [])
151158
@test sol.retcode == :Success
152159
@test sol[prod.output.u] 2 * sin.(2*pi*sol.t)
153160
end
@@ -169,6 +176,7 @@ end
169176
sys = structural_simplify(model)
170177
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
171178
sol = solve(prob, Rodas4())
179+
@test isequal(unbound_inputs(sys), [])
172180
@test sol.retcode == :Success
173181
@test sol[div.output.u] sin.(2*pi*sol.t) ./ 2
174182
end
@@ -188,6 +196,7 @@ end
188196
sys = structural_simplify(model)
189197
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
190198
sol = solve(prob, Rodas4())
199+
@test isequal(unbound_inputs(sys), [])
191200
@test sol.retcode == :Success
192201
@test sol[absb.output.u] abs.(sin.(2*pi*sol.t))
193202
end
@@ -207,34 +216,38 @@ end
207216

208217
@testset "Math" begin
209218
for (block, func) in [(Abs, abs), (Sign, sign), (Sin, sin), (Cos, cos), (Tan, tan), (Asin, asin), (Acos, acos), (Atan, atan), (Sinh, sinh), (Cosh, cosh), (Tanh, tanh), (Exp, exp)]
219+
@info "testing $block"
210220
@named source = Sine(frequency=1, amplitude=0.5)
211221
@named b = block()
212222
@named int = Integrator()
213223
@named model = ODESystem([connect(source.output, b.input), connect(b.output, int.input)], t, systems=[int, b, source])
214224
sys = structural_simplify(model)
215225
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
216226
sol = solve(prob, Rodas4())
227+
@test isequal(unbound_inputs(sys), [])
217228
@test sol.retcode == :Success
218229
@test sol[b.output.u] func.(sol[source.output.u])
219230
end
220231

221232
# input must be positive
222-
for (block, func) in [(Sqrt, sqrt), (Log, log), (Log10, log10)]
223-
@named source = Sine(; frequency=1, offset=2)
233+
for (block, func) in [(Sqrt, sqrt), (Log, log), (Log10, log10)]
234+
@info "testing $block"
235+
@named source = Sine(; frequency=1, offset=2, amplitude=0.5)
224236
@named b = block()
225237
@named int = Integrator()
226238
@named model = ODESystem([connect(source.output, b.input), connect(b.output, int.input)], t, systems=[int, b, source])
227239
sys = structural_simplify(model)
228-
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
240+
prob = ODEProblem(sys, Pair[int.x=>0.0, b.input.u=>2.0], (0.0, 1.0))
229241
sol = solve(prob, Rodas4())
242+
@test isequal(unbound_inputs(sys), [])
230243
@test sol.retcode == :Success
231244
@test sol[b.output.u] func.(sol[source.output.u])
232245
end
233246
end
234247

235248
@testset "Atan2" begin
236-
@named c1 = Constant(; k=1)
237-
@named c2 = Constant(; k=2)
249+
@named c1 = Sine(; frequency=1, offset=2)
250+
@named c2 = Sine(; frequency=1, offset=1)
238251
@named b = Atan2(;)
239252
@named int = Integrator(; k=1)
240253
@named model = ODESystem(
@@ -246,9 +259,14 @@ end
246259
t,
247260
systems=[int, b, c1, c2]
248261
)
262+
249263
sys = structural_simplify(model)
250-
prob = ODEProblem(sys, Pair[int.x=>0.0], (0.0, 1.0))
264+
prob = ODEProblem(sys, Pair[int.x=>0.0, b.input1.u=>2, b.input2.u=>1], (0.0, 1.0))
251265
sol = solve(prob, Rodas4())
266+
267+
@test isequal(unbound_inputs(sys), [])
268+
@test all(map(u->u in Set([b.input1.u, b.input2.u, int.input.u]), bound_inputs(sys)))
269+
@test all(map(u->u in Set([b.input1.u, b.input2.u, int.input.u]), inputs(sys)))
252270
@test sol.retcode == :Success
253-
@test sol[int.output.u][end] atan(1, 2)
254-
end
271+
@test sol[int.input.u] atan.(sol[c1.output.u], sol[c2.output.u])
272+
end

0 commit comments

Comments
 (0)