From 17311e72f735d328e8bf4da05f6ece50b7a81da3 Mon Sep 17 00:00:00 2001 From: contradict Date: Tue, 6 Aug 2024 16:38:28 -0700 Subject: [PATCH] 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 --- src/systems/model_parsing.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index b10b7125a2..420c028c47 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -35,6 +35,10 @@ for f in (:connector, :mtkmodel) end end +flatten_equations(eqs::Vector{Equation}, eq::Equation) = vcat(eqs, [eq]) +flatten_equations(eq::Vector{Equation}, eqs::Vector{Equation}) = vcat(eq, eqs) +flatten_equations(eqs::Vector{Union{Equation, Vector{Equation}}}) = foldl(flatten_equations, eqs; init=Equation[]) + function _model_macro(mod, name, expr, isconnector) exprs = Expr(:block) dict = Dict{Symbol, Any}( @@ -56,7 +60,7 @@ function _model_macro(mod, name, expr, isconnector) push!(exprs.args, :(variables = [])) push!(exprs.args, :(parameters = [])) push!(exprs.args, :(systems = ODESystem[])) - push!(exprs.args, :(equations = Equation[])) + push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[])) push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}())) Base.remove_linenums!(expr) @@ -106,7 +110,7 @@ function _model_macro(mod, name, expr, isconnector) @inline pop_structure_dict!.( Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters]) - sys = :($ODESystem($Equation[equations...], $iv, variables, parameters; + sys = :($ODESystem($(flatten_equations)(equations), $iv, variables, parameters; name, systems, gui_metadata = $gui_metadata, defaults)) if ext[] === nothing