Skip to content

Commit 9cb28d5

Browse files
committed
Add Power, Modulo, and UnaryMinus blocks; update math operations and tests
1 parent e037aec commit 9cb28d5

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

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
13+
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo, UnaryMinus
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ Output the remainder when the first input is divided by second input.
248248
output = RealOutput()
249249
end
250250
@equations begin
251-
output.u ~ input1.u % input2.u
251+
output.u ~ mod(input1.u, input2.u)
252252
end
253253
end
254254

test/Blocks/math.jl

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,62 @@ end
158158
end
159159

160160
@testset "Power" begin
161-
@named c1 = Constant(; k = 2)
161+
@named c1 = Sine(; frequency = 1)
162162
@named c2 = Constant(; k = 2)
163163
@named pow = Power(;)
164+
@named int = Integrator(; k = 1)
164165
@named model = ODESystem(
165166
[
166167
connect(c1.output, pow.input1),
167-
connect(c2.output, pow.input2)
168+
connect(c2.output, pow.input2),
169+
connect(pow.output, int.input)
170+
],
171+
t,
172+
systems = [int, pow, c1, c2])
173+
sys = structural_simplify(model)
174+
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
175+
sol = solve(prob, Rodas4())
176+
@test isequal(unbound_inputs(sys), [])
177+
@test sol.retcode == Success
178+
@test sol[pow.output.u] sin.(2 * pi * sol.t) .^ 2
179+
end
180+
181+
@testset "Modulo" begin
182+
@named c1 = Ramp(height = 2, duration = 1, offset = 1, start_time = 0, smooth = false)
183+
@named c2 = Constant(; k = 1)
184+
@named modl = Modulo(;)
185+
@named model = ODESystem(
186+
[
187+
connect(c1.output, modl.input1),
188+
connect(c2.output, modl.input2)
168189
],
169190
t,
170-
systems = [pow, c1, c2])
191+
systems = [modl, c1, c2])
171192
sys = structural_simplify(model)
172193
prob = ODEProblem(sys, [], (0.0, 1.0))
173194
sol = solve(prob, Rodas4())
174195
@test isequal(unbound_inputs(sys), [])
175196
@test sol.retcode == Success
176-
@test sol[pow.output.u] 4
197+
@test sol[modl.output.u] mod.(2 * sol.t,1)
198+
end
199+
200+
@testset "UnaryMinus" begin
201+
@named c1 = Sine(; frequency = 1)
202+
@named minu = UnaryMinus(;)
203+
@named int = Integrator(; k = 1)
204+
@named model = ODESystem(
205+
[
206+
connect(c1.output, minu.input),
207+
connect(minu.output, int.input)
208+
],
209+
t,
210+
systems = [int, minu, c1])
211+
sys = structural_simplify(model)
212+
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
213+
sol = solve(prob, Rodas4())
214+
@test isequal(unbound_inputs(sys), [])
215+
@test sol.retcode == Success
216+
@test sol[minu.output.u] - sin.(2 * pi * sol.t)
177217
end
178218

179219
@testset "Division" begin

0 commit comments

Comments
 (0)