You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Loops through all components, add the component and the coefficients to the corresponding vectors (cannot extract directly using e.g. "getfield.(composition, :reactant)" because then we get something like :([:C, :O]), rather than :([C, O]))
components = :([]) # Becomes something like :([C, O]).
56
51
coefficients = :([]) # Becomes something like :([1, 2]).
57
52
for comp in composition
58
53
push!(components.args, comp.reactant)
59
54
push!(coefficients.args, comp.stoichiometry)
60
55
end
61
56
57
+
# Extracts the composite species name, as well as the expression which creates it (with e.g. meta data and default values included).
58
+
species_expr = expr.args[2] # E.g. :(CO2 = 1.0, [metadata=true])
59
+
iv_expr = :(unique(reduce(vcat, arguments.(ModelingToolkit.unwrap.($(components)))))) # Expression which, when evaluated, becoems a list of all the independent variables the compound should depend on.
60
+
species_expr =insert_independent_variable(species_expr, iv_expr) # Add independent variable (e.g. goes from `CO2` to `CO2(t)`).
61
+
species_name =find_varname_in_declaration(expr.args[2]) # E.g. :CO2
62
+
63
+
62
64
# Creates the found expressions that will create the compound species.
63
-
# The `Expr(:escape, :(...))` is required so that teh expressions are evaluated in the scope the users use the macro in (to e.g. detect already exiting species).
64
-
non_t_iv_error_check_expr =Expr(:escape, :(@species$species_expr)) # E.g. `@species CO2(t)`
65
+
# The `Expr(:escape, :(...))` is required so that teh expressions are evaluated in the scope the users use the macro in (to e.g. detect already exiting species). # E.g. `@species CO2(t)`
65
66
species_declaration_expr =Expr(:escape, :(@species$species_expr)) # E.g. `@species CO2(t)`
# Currently, non-t independent variables are not supported for compounds. If there are any like these, we throw an error:
71
-
non_t_iv_error_check_expr =Expr(:escape, :(issetequal(unique(reduce(vcat, arguments.(ModelingToolkit.unwrap.($components)))), [t]) ||error("Currently, compounds depending on components that are not \"t\" are not supported.")))
Copy file name to clipboardExpand all lines: src/expression_utils.jl
+5-7Lines changed: 5 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -52,21 +52,19 @@ end
52
52
# (In this example the independent variable t was inserted).
53
53
# Extra dispatch is to ensure so that iv is a vector (so that we can handle both single or multiple iv insertion in one function).
54
54
# - This way we don't have to make a check for how to create the final expression (which is different for symbol or vector of symbols).
55
-
functioninsert_independent_variable(expr_in, iv)
56
-
# If iv is a symbol (e.g. :t), we convert to vector (e.g. [:t]). This way we can handle single and multiple ivs using the same code, without needing to check at the end.
57
-
(iv isa Symbol) && (iv = [iv])
55
+
functioninsert_independent_variable(expr_in, ivs)
58
56
# If expr is a symbol, just attach the iv. If not we have to create a new expr and mutate it.
59
57
# Because Symbols (a possible input) cannot be mutated, this function cannot mutate the input (would have been easier if Expr input was guaranteed).
60
-
(expr_in isa Symbol) && (returnExpr(:call, expr_in, iv...))
58
+
(expr_in isa Symbol) && (returnExpr(:call, expr_in, :($ivs...)))
0 commit comments