Skip to content
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

add StructIO #265

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using ModelingToolkit: getdefault
D = Differential(t)

export RealInput, RealOutput, SISO
using Symbolics: Struct, StructElement, getelements, symstruct
include("utils.jl")

export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division
Expand Down
37 changes: 37 additions & 0 deletions src/Blocks/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,40 @@
]
return ODESystem(eqs, t, vcat(u..., y...), []; name = name, systems = [input, output])
end



using Symbolics: Struct, symbolic_getproperty
@connector function StructInput(; structdef, name)
@variables u(t)::Struct [input = true] # Dummy default value due to bug in Symbolics
ODESystem(Equation[], t, [u], []; name)

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

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L114-L116

Added lines #L114 - L116 were not covered by tests
end

@connector function StructOutput(; structdef, name)
@variables u(t)::Struct [output = true] # Dummy default value due to bug in Symbolics
ODESystem(Equation[], t, [u], []; name)

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

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L119-L121

Added lines #L119 - L121 were not covered by tests
end

function _structelem2connector(elem::StructElement)
T = Symbolics.decodetyp(elem.typ)
if T <: Bool
return BoolOutput(; name = elem.name)
elseif T <: Real
return RealOutput(; name = elem.name)

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

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L124-L129

Added lines #L124 - L129 were not covered by tests
end
end

@component function BusSelect(;name, structdef, selected_fields)
@parameters t
nout = length(selected_fields)
inputbus = Blocks.StructInput(; structdef, name = Symbol("inputbus"))
@variables input(t)

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

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L133-L137

Added lines #L133 - L137 were not covered by tests

output_elements = filter(e->e.name in selected_fields, getelements(structdef))
output_connectors = map(_structelem2connector, output_elements)

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

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L139-L140

Added lines #L139 - L140 were not covered by tests

eqs = [

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

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L142

Added line #L142 was not covered by tests
symbolic_getproperty(inputbus.u, field) ~ con.u for (field, con) in zip(selected_fields, output_connectors)
]
return ODESystem(eqs, t; name = name, systems = [inputbus; output_connectors])

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

View check run for this annotation

Codecov / codecov/patch

src/Blocks/utils.jl#L145

Added line #L145 was not covered by tests
end
57 changes: 56 additions & 1 deletion test/Blocks/sources.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ModelingToolkit, ModelingToolkitStandardLibrary, OrdinaryDiffEq
using ModelingToolkit, ModelingToolkitStandardLibrary, OrdinaryDiffEq, Test
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkitStandardLibrary.Blocks: smooth_sin, smooth_cos, smooth_damped_sin,
smooth_square, smooth_step, smooth_ramp,
Expand Down Expand Up @@ -474,3 +474,58 @@ end
@test sol[ddy][end]≈2 atol=1e-3
end
end

using Symbolics
using Symbolics: Struct, StructElement, getelements, symstruct
using Test
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkitStandardLibrary.Blocks: BusSelect
#using ModelingToolkitStandardLibrary.Blocks: structelem2connector

# Test struct
struct BarStruct
speed::Float64
isSpeedValid::Int
end

bar = BarStruct(2.0, 1)
structdef = symstruct(BarStruct)
selected_fields = [:speed]

@parameters bar_param::Struct
systems = @named begin
inputbus = Blocks.StructOutput(; structdef)
output = BusSelect(; structdef, selected_fields)
end
eqs = [inputbus.u ~ bar_param
connect(inputbus, output.inputbus)]
@named sys = ODESystem(eqs, t; systems)
sys = complete(sys)
ssys = structural_simplify(sys)
prob = ODEProblem(ssys, [], (0.0, 1.0), [sys.bar_param => bar], tofloat=false)
sol = solve(prob, Rodas4())
@test sol(1.0, idxs = sys.output.speed.u) == 2.0

@mtkmodel BusSelectTest begin
@parameters bar_param::Struct
@components begin
inputbus = Blocks.StructOutput(; structdef)
output = BusSelect(; structdef, selected_fields)
end
@equations begin
inputbus.u ~ bar_param
connect(inputbus, output.inputbus)
end
end

@named sys = BusSelectTest()
sys = complete(sys)
ssys = structural_simplify(sys)
@test_broken begin
prob = ODEProblem(ssys, [
sys.bar_param => bar
], (0.0, 1.0))
sol = solve(prob, Rodas4())
@test sol.retcode == ReturnCode.Success
@test sol(1.0, idxs = sys.output.speed.u) == 2.0
end
Loading