Skip to content

Commit

Permalink
Flatten equations to handle complex equations.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
contradict committed Aug 7, 2024
1 parent deb0b90 commit 17311e7
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}(
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 17311e7

Please sign in to comment.