Skip to content

Commit 47aa59d

Browse files
Merge pull request SciML#2935 from contradict/flatten_for_complex
Flatten for complex
2 parents d3681d7 + 9eb7e8c commit 47aa59d

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/systems/model_parsing.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ for f in (:connector, :mtkmodel)
3535
end
3636
end
3737

38+
flatten_equations(eqs::Vector{Equation}, eq::Equation) = vcat(eqs, [eq])
39+
flatten_equations(eq::Vector{Equation}, eqs::Vector{Equation}) = vcat(eq, eqs)
40+
function flatten_equations(eqs::Vector{Union{Equation, Vector{Equation}}})
41+
foldl(flatten_equations, eqs; init = Equation[])
42+
end
43+
3844
function _model_macro(mod, name, expr, isconnector)
3945
exprs = Expr(:block)
4046
dict = Dict{Symbol, Any}(
@@ -56,7 +62,7 @@ function _model_macro(mod, name, expr, isconnector)
5662
push!(exprs.args, :(variables = []))
5763
push!(exprs.args, :(parameters = []))
5864
push!(exprs.args, :(systems = ODESystem[]))
59-
push!(exprs.args, :(equations = Equation[]))
65+
push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[]))
6066
push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}()))
6167

6268
Base.remove_linenums!(expr)
@@ -106,7 +112,7 @@ function _model_macro(mod, name, expr, isconnector)
106112
@inline pop_structure_dict!.(
107113
Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters])
108114

109-
sys = :($ODESystem($Equation[equations...], $iv, variables, parameters;
115+
sys = :($ODESystem($(flatten_equations)(equations), $iv, variables, parameters;
110116
name, systems, gui_metadata = $gui_metadata, defaults))
111117

112118
if ext[] === nothing

test/complex.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using ModelingToolkit
2+
using ModelingToolkit: t_nounits as t
3+
using Test
4+
5+
@mtkmodel ComplexModel begin
6+
@variables begin
7+
x(t)
8+
y(t)
9+
z(t)::Complex
10+
end
11+
@equations begin
12+
z ~ x + im * y
13+
end
14+
end
15+
@named mixed = ComplexModel()
16+
@test length(equations(mixed)) == 2

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ end
7373
@safetestset "Initial Values Test" include("initial_values.jl")
7474
@safetestset "Discrete System" include("discrete_system.jl")
7575
@safetestset "Equation Type Accessors Test" include("equation_type_accessors.jl")
76+
@safetestset "Equations with complex values" include("complex.jl")
7677
end
7778
end
7879

0 commit comments

Comments
 (0)