Skip to content

Commit 1100e95

Browse files
Merge pull request #81 from ValentinKaisermayer/patch-io
Adds input/output meta data
2 parents c9af29e + e4eb600 commit 1100e95

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
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

src/Blocks/utils.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@connector function RealInput(;name, nin=1, u_start=nin > 1 ? zeros(nin) : 0.0)
22
if nin == 1
3-
@variables u(t) = u_start
3+
@variables u(t) = u_start [input=true]
44
else
5-
@variables u[1:nin](t) = u_start
5+
@variables u[1:nin](t) = u_start [input=true]
66
u = collect(u)
77
end
88
ODESystem(Equation[], t, [u...], []; name=name)
@@ -22,9 +22,9 @@ Connector with one input signal of type Real.
2222

2323
@connector function RealOutput(;name, nout=1, u_start=nout > 1 ? zeros(nout) : 0.0)
2424
if nout == 1
25-
@variables u(t) = u_start
25+
@variables u(t) = u_start [output=true]
2626
else
27-
@variables u[1:nout](t) = u_start
27+
@variables u[1:nout](t) = u_start [output=true]
2828
u = collect(u)
2929
end
3030
ODESystem(Equation[], t, [u...], []; name=name)
@@ -88,4 +88,4 @@ function MIMO(;name, nin=1, nout=1, u_start=zeros(nin), y_start=zeros(nout))
8888
[y[i] ~ output.u[i] for i in 1:nout]...,
8989
]
9090
return ODESystem(eqs, t, vcat(u..., y...), []; name=name, systems=[input, output])
91-
end
91+
end

src/Thermal/HeatTransfer/ideal_components.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function HeatCapacitor(; name, C, T_start=273.15 + 20)
1616
@parameters C=C
1717
sts = @variables begin
1818
T(t)=T_start
19-
der_T(t)
19+
der_T(t)=0.0
2020
end
2121

2222
D = Differential(t)
@@ -81,7 +81,7 @@ function ConvectiveConductor(; name, G)
8181
@named solid = HeatPort()
8282
@named fluid = HeatPort()
8383
@parameters G=G
84-
sts = @variables Q_flow(t) dT(t)
84+
sts = @variables Q_flow(t)=0.0 dT(t)=0.0
8585
eqs = [
8686
dT ~ solid.T - fluid.T
8787
solid.Q_flow ~ Q_flow
@@ -107,7 +107,7 @@ function ConvectiveResistor(; name, R)
107107
@named solidport = HeatPort()
108108
@named fluidport = HeatPort()
109109
@parameters R=R
110-
sts = @variables Q_flow(t) dT(t)
110+
sts = @variables Q_flow(t)=0.0 dT(t)=0.0
111111
eqs = [
112112
dT ~ solidport.T - fluidport.T
113113
solidport.Q_flow ~ Q_flow

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, 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)]
210-
@named source = Sine(frequency=1)
219+
@info "testing $block"
220+
@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)
271+
@test sol[int.input.u] atan.(sol[c1.output.u], sol[c2.output.u])
254272
end

0 commit comments

Comments
 (0)