Skip to content

Commit

Permalink
fix: fix guesses for Real*Input and Real*Output
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Apr 29, 2024
1 parent 37faf7b commit ad7fc59
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/Blocks/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
input = true,
description = "Inner variable in RealInput $name"
]
u = collect(u)

This comment has been minimized.

Copy link
@baggepinnen

baggepinnen May 16, 2024

Contributor

This change broke Multibody.jl

@AayushSabharwal

Here's a test case that no longer works and it seems related to symbolic arrays, it uses this PR branch of multibody

using Multibody
using ModelingToolkit
using JuliaSimCompiler

t = Multibody.t
D = Differential(t)
world = Multibody.world

systems = @named begin
    # gearConstraint = GearConstraint(; ratio = 10)
    gearConstraint = Revolute()
    cyl1 = Body(; m = 1, r_cm = [0.4, 0, 0])
    cyl2 = Body(; m = 1, r_cm = [0.4, 0, 0])
    torque1 = Torque(resolveInFrame = :frame_b)
    fixed = Fixed() 
end

eqs = [
        connect(world.frame_b, gearConstraint.frame_a)
        connect(cyl1.frame_a, gearConstraint.frame_a)
        connect(gearConstraint.frame_b, cyl2.frame_a)
        connect(torque1.frame_b, cyl1.frame_a)
        connect(torque1.frame_a, world.frame_b)
        # connect(sine.output, torque1.torque)
        torque1.torque.u .~ [2sin(t), 0, 0]
    ]

@named model = ODESystem(eqs, t, systems = [world; systems])
cm = complete(model)
ssys = structural_simplify(IRSystem(model))
end
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => u_start])

Check warning on line 14 in src/Blocks/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L14

Added line #L14 was not covered by tests
end
@doc """
RealInput(;name, u_start)
Expand All @@ -31,8 +30,7 @@ Connector with one input signal of type Real.
input = true,
description = "Inner variable in RealInputArray $name"
]
u = collect(u)
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => u_start])

Check warning on line 33 in src/Blocks/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L33

Added line #L33 was not covered by tests
end
@doc """
RealInputArray(;name, nin, u_start)
Expand All @@ -59,9 +57,8 @@ Connector with an array of input signals of type Real.
output = true,
description = "Inner variable in RealOutput $name"
]
u = collect(u)
end
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => u_start])

Check warning on line 61 in src/Blocks/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L61

Added line #L61 was not covered by tests
end
@doc """
RealOutput(;name, u_start)
Expand All @@ -80,8 +77,7 @@ Connector with one output signal of type Real.
output = true,
description = "Inner variable in RealOutputArray $name"
]
u = collect(u)
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => u_start])

Check warning on line 80 in src/Blocks/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L80

Added line #L80 was not covered by tests
end
@doc """
RealOutputArray(;name, nout, u_start)
Expand Down
16 changes: 16 additions & 0 deletions test/Blocks/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit

@testset "Guesses" begin
for (block, guess) in [
(RealInput(; name = :a), 0.0),
(RealInput(; nin = 3, name = :a), zeros(3)),
(RealOutput(; name = :a), 0.0),
(RealOutput(; nout = 3, name = :a), zeros(3)),
(RealInputArray(; nin = 3, name = :a), zeros(3)),
(RealOutputArray(; nout = 3, name = :a), zeros(3)),
]
guesses = ModelingToolkit.guesses(block)
@test guesses[@nonamespace block.u] == guess
end
end
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ using SafeTestsets
end

# Blocks
@safetestset "Blocks: utils" begin
include("Blocks/utils.jl")
end
@safetestset "Blocks: math" begin
include("Blocks/math.jl")
end
Expand Down

0 comments on commit ad7fc59

Please sign in to comment.