Skip to content

Commit ed705d4

Browse files
committed
saved progress on ivs
1 parent 6e6f43b commit ed705d4

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/chemistry_functionality.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,38 @@ function make_compound(expr)
4545
((expr.args[2].args[1] isa Expr) && (expr.args[2].args[1].args[1] isa Expr) && expr.args[2].args[1].args[1].head == :call) && error(COMPOUND_CREATION_ERROR_DEPENDENT_VAR_GIVEN)
4646
end
4747

48-
# Extracts the composite species name, and a Vector{ReactantStruct} of its components.
49-
species_expr = expr.args[2] # E.g. :(CO2(t))
50-
species_expr = insert_independent_variable(species_expr, [:t]) # Add independent variable (e.g. goes from `CO2` to `CO2(t)`).
51-
species_name = find_varname_in_declaration(expr.args[2]) # E.g. :CO2
52-
composition = Catalyst.recursive_find_reactants!(expr.args[3], 1, Vector{ReactantStruct}(undef, 0))
53-
5448
# 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]))
49+
composition = Catalyst.recursive_find_reactants!(expr.args[3], 1, Vector{ReactantStruct}(undef, 0))
5550
components = :([]) # Becomes something like :([C, O]).
5651
coefficients = :([]) # Becomes something like :([1, 2]).
5752
for comp in composition
5853
push!(components.args, comp.reactant)
5954
push!(coefficients.args, comp.stoichiometry)
6055
end
6156

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+
6264
# 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)`
6566
species_declaration_expr = Expr(:escape, :(@species $species_expr)) # E.g. `@species CO2(t)`
6667
compound_designation_expr = Expr(:escape, :($species_name = ModelingToolkit.setmetadata($species_name, Catalyst.CompoundSpecies, true))) # E.g. `CO2 = ModelingToolkit.setmetadata(CO2, Catalyst.CompoundSpecies, true)`
6768
components_designation_expr = Expr(:escape, :($species_name = ModelingToolkit.setmetadata($species_name, Catalyst.CompoundComponents, $components))) # E.g. `CO2 = ModelingToolkit.setmetadata(CO2, Catalyst.CompoundSpecies, [C, O])`
6869
coefficients_designation_expr = Expr(:escape, :($species_name = ModelingToolkit.setmetadata($species_name, Catalyst.CompoundCoefficients, $coefficients))) # E.g. `CO2 = ModelingToolkit.setmetadata(CO2, Catalyst.CompoundSpecies, [1, 2])`
6970

70-
# 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.")))
71+
println(quote
72+
$species_declaration_expr
73+
$compound_designation_expr
74+
$components_designation_expr
75+
$coefficients_designation_expr
76+
end)
7277

7378
# Returns the rephrased expression.
7479
return quote
75-
$non_t_iv_error_check_expr
7680
$species_declaration_expr
7781
$compound_designation_expr
7882
$components_designation_expr

src/expression_utils.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,19 @@ end
5252
# (In this example the independent variable t was inserted).
5353
# 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).
5454
# - 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-
function insert_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+
function insert_independent_variable(expr_in, ivs)
5856
# If expr is a symbol, just attach the iv. If not we have to create a new expr and mutate it.
5957
# 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) && (return Expr(:call, expr_in, iv...))
58+
(expr_in isa Symbol) && (return Expr(:call, expr_in, :($ivs...)))
6159
expr = deepcopy(expr_in)
6260

6361
if expr.head == :(=) # Case: :(X = 1.0)
64-
expr.args[1] = Expr(:call, expr.args[1], iv...)
62+
expr.args[1] = Expr(:call, expr.args[1], :($ivs...))
6563
elseif expr.head == :tuple
6664
if expr.args[1] isa Symbol # Case: :(X, [metadata=true])
67-
expr.args[1] = Expr(:call, expr.args[1], iv...)
65+
expr.args[1] = Expr(:call, expr.args[1], :($ivs...))
6866
elseif (expr.args[1].head == :(=)) && (expr.args[1].args[1] isa Symbol) # Case: :(X = 1.0, [metadata=true])
69-
expr.args[1].args[1] = Expr(:call, expr.args[1].args[1], iv...)
67+
expr.args[1].args[1] = Expr(:call, expr.args[1].args[1], :($ivs...))
7068
end
7169
end
7270
(expr == expr_in) && error("Failed to add independent variable $(iv) to expression: $expr_in")

0 commit comments

Comments
 (0)