From 6e38240df6e627088b743c4d4a972d66d74e150c Mon Sep 17 00:00:00 2001 From: Yingbo Ma Date: Wed, 21 Feb 2024 09:11:22 -0500 Subject: [PATCH] Use https://github.com/JuliaSymbolics/Symbolics.jl/pull/1062 --- src/Blocks/Blocks.jl | 1 + src/Blocks/utils.jl | 11 +++++------ test/Blocks/sources.jl | 11 +++++++---- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Blocks/Blocks.jl b/src/Blocks/Blocks.jl index cf0679a37..9bf4f8580 100644 --- a/src/Blocks/Blocks.jl +++ b/src/Blocks/Blocks.jl @@ -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 diff --git a/src/Blocks/utils.jl b/src/Blocks/utils.jl index 2f0cf59bf..5b224dd06 100644 --- a/src/Blocks/utils.jl +++ b/src/Blocks/utils.jl @@ -110,15 +110,14 @@ end +using Symbolics: Struct, symbolic_getproperty @connector function StructInput(; structdef, name) - n = Symbolics.juliatype(structdef) - @variables u(t)::n [input = true] # Dummy default value due to bug in Symbolics + @variables u(t)::Struct [input = true] # Dummy default value due to bug in Symbolics ODESystem(Equation[], t, [u], []; name) end @connector function StructOutput(; structdef, name) - n = Symbolics.juliatype(structdef) - @variables u(t)::n [output = true] # Dummy default value due to bug in Symbolics + @variables u(t)::Struct [output = true] # Dummy default value due to bug in Symbolics ODESystem(Equation[], t, [u], []; name) end @@ -141,7 +140,7 @@ end output_connectors = map(_structelem2connector, output_elements) eqs = [ - getproperty(inputbus.u, field) ~ con.u for (field, con) in zip(selected_fields, output_connectors) + 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]) -end \ No newline at end of file +end diff --git a/test/Blocks/sources.jl b/test/Blocks/sources.jl index 5080e9b11..700a4db79 100644 --- a/test/Blocks/sources.jl +++ b/test/Blocks/sources.jl @@ -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, @@ -479,7 +479,8 @@ using Symbolics using Symbolics: Struct, StructElement, getelements, symstruct using Test using ModelingToolkitStandardLibrary.Blocks -using ModelingToolkitStandardLibrary.Blocks: structelem2connector +using ModelingToolkitStandardLibrary.Blocks: BusSelect +#using ModelingToolkitStandardLibrary.Blocks: structelem2connector # Test struct struct BarStruct @@ -491,14 +492,16 @@ bar = BarStruct(1.0, 1) structdef = symstruct(BarStruct) selected_fields = [:speed] +using Symbolics: Struct +@parameters bar_param::Struct @mtkmodel BusSelectTest begin @components begin inputbus = Blocks.StructOutput(; structdef) output = BusSelect(; structdef, selected_fields) end @equations begin - inputbus.u ~ bar - connect(inputbus, output) + inputbus.u ~ bar_param + connect(inputbus, output.inputbus) end end