Skip to content

Adding Power, Modulo, UnaryMinus, Floor and Ceil blocks #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/API/blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Add
Add3
Product
Division
UnaryMinus
Power
Modulo
Floor
Ceil
StaticNonLinearity
Abs
Sign
Expand Down
2 changes: 1 addition & 1 deletion src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D
export RealInput, RealInputArray, RealOutput, RealOutputArray, SISO
include("utils.jl")

export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division, Power, Modulo, UnaryMinus, Floor, Ceil
export Abs, Sign, Sqrt, Sin, Cos, Tan, Asin, Acos, Atan, Atan2, Sinh, Cosh, Tanh, Exp
export Log, Log10
include("math.jl")
Expand Down
106 changes: 106 additions & 0 deletions src/Blocks/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,112 @@ Output first input divided by second input.
end
end

"""
Power(; name)

Output the exponential with base as the first input and exponent as second input i.e u1^u2

# Connectors:

- `input1`
- `input2`
- `output`
"""
@mtkmodel Power begin
@components begin
input1 = RealInput()
input2 = RealInput() # denominator can not be zero
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these could have had better names, like exponent instead of input2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, could you follow up with some suggested names? It hasn't been released yet so it's still fine to change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base, power or base, exp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this PR here that updates names for the Modulo block as well : #359

output = RealOutput()
end
@equations begin
output.u ~ input1.u ^ input2.u
end
end

"""
Modulo(; name)

Output the remainder when the first input is divided by second input.

# Connectors:

- `input1`
- `input2`
- `output`
"""
@mtkmodel Modulo begin
@components begin
input1 = RealInput()
input2 = RealInput(guess = 1.0) # denominator can not be zero
output = RealOutput()
end
@equations begin
output.u ~ mod(input1.u, input2.u)
end
end

"""
UnaryMinus(; name)

Output the product of -1 and the input.

# Connectors:

- `input`
- `output`
"""
@mtkmodel UnaryMinus begin
@components begin
input = RealInput()
output = RealOutput()
end
@equations begin
output.u ~ -(input.u)
end
end

## Rounding functions add after the symbolic functions are registered
"""
Floor(; name)

Output the floor rounding of the input.

# Connectors:

- `input`
- `output`
"""
@mtkmodel Floor begin
@components begin
input = RealInput()
output = RealOutput()
end
@equations begin
output.u ~ floor(input.u)
end
end

"""
Ceil(; name)

Output the ceiling rounding of the input.

# Connectors:

- `input`
- `output`
"""
@mtkmodel Ceil begin
@components begin
input = RealInput()
output = RealOutput()
end
@equations begin
output.u ~ ceil(input.u)
end
end


"""
StaticNonLinearity(func; name)

Expand Down
97 changes: 97 additions & 0 deletions test/Blocks/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,103 @@ end
@test sol[prod.output.u] ≈ 2 * sin.(2 * pi * sol.t)
end

@testset "Power" begin
@named c1 = Sine(; frequency = 1)
@named c2 = Constant(; k = 2)
@named pow = Power(;)
@named int = Integrator(; k = 1)
@named model = ODESystem(
[
connect(c1.output, pow.input1),
connect(c2.output, pow.input2),
connect(pow.output, int.input)
],
t,
systems = [int, pow, c1, c2])
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
sol = solve(prob, Rodas4())
@test isequal(unbound_inputs(sys), [])
@test sol.retcode == Success
@test sol[pow.output.u] ≈ sin.(2 * pi * sol.t) .^ 2
end

@testset "Modulo" begin
@named c1 = Ramp(height = 2, duration = 1, offset = 1, start_time = 0, smooth = false)
@named c2 = Constant(; k = 1)
@named modl = Modulo(;)
@named model = ODESystem(
[
connect(c1.output, modl.input1),
connect(c2.output, modl.input2)
],
t,
systems = [modl, c1, c2])
sys = structural_simplify(model)
prob = ODEProblem(sys, [], (0.0, 1.0))
sol = solve(prob, Rodas4())
@test isequal(unbound_inputs(sys), [])
@test sol.retcode == Success
@test sol[modl.output.u] ≈ mod.(2 * sol.t,1)
end

@testset "UnaryMinus" begin
@named c1 = Sine(; frequency = 1)
@named minu = UnaryMinus(;)
@named int = Integrator(; k = 1)
@named model = ODESystem(
[
connect(c1.output, minu.input),
connect(minu.output, int.input)
],
t,
systems = [int, minu, c1])
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
sol = solve(prob, Rodas4())
@test isequal(unbound_inputs(sys), [])
@test sol.retcode == Success
@test sol[minu.output.u] ≈ - sin.(2 * pi * sol.t)
end

@testset "Floor" begin
@named c1 = Sine(; frequency = 1)
@named flr = Floor(;)
@named int = Integrator(; k = 1)
@named model = ODESystem(
[
connect(c1.output, flr.input),
connect(flr.output, int.input)
],
t,
systems = [int, flr, c1])
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
sol = solve(prob, Rodas4())
@test isequal(unbound_inputs(sys), [])
@test sol.retcode == Success
@test sol[flr.output.u] ≈ floor.(sin.(2 * pi * sol.t))
end

@testset "Ceil" begin
@named c1 = Sine(; frequency = 1)
@named cel = Ceil(;)
@named int = Integrator(; k = 1)
@named model = ODESystem(
[
connect(c1.output, cel.input),
connect(cel.output, int.input)
],
t,
systems = [int, cel, c1])
sys = structural_simplify(model)
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 1.0))
sol = solve(prob, Rodas4())
@test isequal(unbound_inputs(sys), [])
@test sol.retcode == Success
@test sol[cel.output.u] ≈ ceil.(sin.(2 * pi * sol.t))
end

@testset "Division" begin
@named c1 = Sine(; frequency = 1)
@named c2 = Constant(; k = 2)
Expand Down
Loading