Skip to content

Commit 7ec57c4

Browse files
committed
add floor and ceiling blocks with tests
1 parent 9cb28d5 commit 7ec57c4

File tree

4 files changed

+81
-80
lines changed

4 files changed

+81
-80
lines changed

docs/src/API/blocks.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Division
3939
UnaryMinus
4040
Power
4141
Modulo
42+
Floor
43+
Ceil
4244
StaticNonLinearity
4345
Abs
4446
Sign

src/Blocks/Blocks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D
1010
export RealInput, RealInputArray, RealOutput, RealOutputArray, SISO
1111
include("utils.jl")
1212

13-
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo, UnaryMinus
13+
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo, UnaryMinus, Floor, Ceil
1414
export Abs, Sign, Sqrt, Sin, Cos, Tan, Asin, Acos, Atan, Atan2, Sinh, Cosh, Tanh, Exp
1515
export Log, Log10
1616
include("math.jl")

src/Blocks/math.jl

Lines changed: 40 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -273,85 +273,46 @@ Output the product of -1 and the input.
273273
end
274274

275275
## Rounding functions add after the symbolic functions are registered
276-
# """
277-
# Floor(; name)
278-
279-
# Output the floor rounding of the input.
280-
281-
# # Connectors:
282-
283-
# - `input`
284-
# - `output`
285-
# """
286-
# @mtkmodel Floor begin
287-
# @components begin
288-
# input = RealInput()
289-
# output = RealOutput()
290-
# end
291-
# @equations begin
292-
# output.u ~ floor(input.u)
293-
# end
294-
# end
295-
296-
# """
297-
# Ceil(; name)
298-
299-
# Output the ceiling rounding of the input.
300-
301-
# # Connectors:
302-
303-
# - `input`
304-
# - `output`
305-
# """
306-
# @mtkmodel Ceil begin
307-
# @components begin
308-
# input = RealInput()
309-
# output = RealOutput()
310-
# end
311-
# @equations begin
312-
# output.u ~ ceil(input.u)
313-
# end
314-
# end
315-
316-
# """
317-
# Round(; name)
318-
319-
# Output the rounding to the nearest integer of the input.
320-
321-
# # Connectors:
322-
323-
# - `input`
324-
# - `output`
325-
# """
326-
# @mtkmodel Round begin
327-
# @components begin
328-
# input = RealInput()
329-
# output = RealOutput()
330-
# end
331-
# @equations begin
332-
# output.u ~ round(input.u)
333-
# end
334-
# end
335-
336-
# """
337-
# Trunc(; name)
338-
339-
# Output the truncation rounding of the input.
340-
341-
# # Connectors:
342-
343-
# - `input`
344-
# - `output`
345-
# """
346-
# @mtkmodel Trunc begin
347-
# @components begin
348-
# input = RealInput()
349-
# output = RealOutput()
350-
# end
351-
# @equations begin
352-
# output.u ~ trunc(input.u)
353-
# end
354-
# end
276+
"""
277+
Floor(; name)
278+
279+
Output the floor rounding of the input.
280+
281+
# Connectors:
282+
283+
- `input`
284+
- `output`
285+
"""
286+
@mtkmodel Floor begin
287+
@components begin
288+
input = RealInput()
289+
output = RealOutput()
290+
end
291+
@equations begin
292+
output.u ~ floor(input.u)
293+
end
294+
end
295+
296+
"""
297+
Ceil(; name)
298+
299+
Output the ceiling rounding of the input.
300+
301+
# Connectors:
302+
303+
- `input`
304+
- `output`
305+
"""
306+
@mtkmodel Ceil begin
307+
@components begin
308+
input = RealInput()
309+
output = RealOutput()
310+
end
311+
@equations begin
312+
output.u ~ ceil(input.u)
313+
end
314+
end
315+
355316

356317
"""
357318
StaticNonLinearity(func; name)

test/Blocks/math.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,44 @@ end
216216
@test sol[minu.output.u] - sin.(2 * pi * sol.t)
217217
end
218218

219+
@testset "Floor" begin
220+
@named c1 = Sine(; frequency = 1)
221+
@named flr = Floor(;)
222+
@named int = Integrator(; k = 1)
223+
@named model = ODESystem(
224+
[
225+
connect(c1.output, flr.input),
226+
connect(flr.output, int.input)
227+
],
228+
t,
229+
systems = [int, flr, c1])
230+
sys = structural_simplify(model)
231+
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
232+
sol = solve(prob, Rodas4())
233+
@test isequal(unbound_inputs(sys), [])
234+
@test sol.retcode == Success
235+
@test sol[flr.output.u] floor.(sin.(2 * pi * sol.t))
236+
end
237+
238+
@testset "Ceil" begin
239+
@named c1 = Sine(; frequency = 1)
240+
@named cel = Ceil(;)
241+
@named int = Integrator(; k = 1)
242+
@named model = ODESystem(
243+
[
244+
connect(c1.output, cel.input),
245+
connect(cel.output, int.input)
246+
],
247+
t,
248+
systems = [int, cel, c1])
249+
sys = structural_simplify(model)
250+
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
251+
sol = solve(prob, Rodas4())
252+
@test isequal(unbound_inputs(sys), [])
253+
@test sol.retcode == Success
254+
@test sol[cel.output.u] ceil.(sin.(2 * pi * sol.t))
255+
end
256+
219257
@testset "Division" begin
220258
@named c1 = Sine(; frequency = 1)
221259
@named c2 = Constant(; k = 2)

0 commit comments

Comments
 (0)