Skip to content

Commit 17311e7

Browse files
committed
Flatten equations to handle complex equations.
Complex equations are expanded into a real and imagianry part meaning one equations entered by the user can be parsed into a Vector of two equations. This patch accumulates equations and pairs of equations during parsing and then flattens the whole list at the end. Fixes #2895
1 parent deb0b90 commit 17311e7

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/systems/model_parsing.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ 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+
flatten_equations(eqs::Vector{Union{Equation, Vector{Equation}}}) = foldl(flatten_equations, eqs; init=Equation[])
41+
3842
function _model_macro(mod, name, expr, isconnector)
3943
exprs = Expr(:block)
4044
dict = Dict{Symbol, Any}(
@@ -56,7 +60,7 @@ function _model_macro(mod, name, expr, isconnector)
5660
push!(exprs.args, :(variables = []))
5761
push!(exprs.args, :(parameters = []))
5862
push!(exprs.args, :(systems = ODESystem[]))
59-
push!(exprs.args, :(equations = Equation[]))
63+
push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[]))
6064
push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}()))
6165

6266
Base.remove_linenums!(expr)
@@ -106,7 +110,7 @@ function _model_macro(mod, name, expr, isconnector)
106110
@inline pop_structure_dict!.(
107111
Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters])
108112

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

112116
if ext[] === nothing

0 commit comments

Comments
 (0)