@@ -297,7 +297,7 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
297
297
298
298
# Get macro options.
299
299
if length (unique (arg. args[1 ] for arg in option_lines)) < length (option_lines)
300
- error (" Some options where given multiple times." )
300
+ error (" Some options were given multiple times." )
301
301
end
302
302
options = Dict (map (arg -> Symbol (String (arg. args[1 ])[2 : end ]) => arg,
303
303
option_lines))
@@ -315,12 +315,12 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
315
315
parameters_declared = extract_syms (options, :parameters )
316
316
variables_declared = extract_syms (options, :variables )
317
317
318
- # Reads more options.
318
+ # Reads equations.
319
319
vars_extracted, add_default_diff, equations = read_equations_options (
320
320
options, variables_declared)
321
321
variables = vcat (variables_declared, vars_extracted)
322
322
323
- # handle independent variables
323
+ # Handle independent variables
324
324
if haskey (options, :ivs )
325
325
ivs = Tuple (extract_syms (options, :ivs ))
326
326
ivexpr = copy (options[:ivs ])
@@ -339,14 +339,16 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
339
339
combinatoric_ratelaws = true
340
340
end
341
341
342
- # Reads more options .
342
+ # Reads observables .
343
343
observed_vars, observed_eqs, obs_syms = read_observed_options (
344
344
options, [species_declared; variables], all_ivs)
345
345
346
+ # Collect species and parameters, including ones inferred from the reactions.
346
347
declared_syms = Set (Iterators. flatten ((parameters_declared, species_declared,
347
348
variables)))
348
- species_extracted, parameters_extracted = extract_species_and_parameters! (reactions,
349
- declared_syms)
349
+ species_extracted, parameters_extracted = extract_species_and_parameters! (
350
+ reactions, declared_syms)
351
+
350
352
species = vcat (species_declared, species_extracted)
351
353
parameters = vcat (parameters_declared, parameters_extracted)
352
354
@@ -376,9 +378,11 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
376
378
push! (rxexprs. args, get_rxexprs (reaction))
377
379
end
378
380
for equation in equations
381
+ equation = escape_equation_RHS! (equation)
379
382
push! (rxexprs. args, equation)
380
383
end
381
384
385
+ # Output code corresponding to the reaction system.
382
386
quote
383
387
$ ivexpr
384
388
$ ps
@@ -572,7 +576,7 @@ function get_rxexprs(rxstruct)
572
576
subs_stoich_init = deepcopy (subs_init)
573
577
prod_init = isempty (rxstruct. products) ? nothing : :([])
574
578
prod_stoich_init = deepcopy (prod_init)
575
- reaction_func = :(Reaction ($ (recursive_expand_functions ! (rxstruct. rate)), $ subs_init,
579
+ reaction_func = :(Reaction ($ (recursive_escape_functions ! (rxstruct. rate)), $ subs_init,
576
580
$ prod_init, $ subs_stoich_init, $ prod_stoich_init,
577
581
metadata = $ (rxstruct. metadata)))
578
582
for sub in rxstruct. substrates
@@ -904,17 +908,24 @@ end
904
908
905
909
# ## Generic Expression Manipulation ###
906
910
907
- # Recursively traverses an expression and replaces special function call like "hill(...)" with the actual corresponding expression.
908
- function recursive_expand_functions ! (expr:: ExprValues )
911
+ # Recursively traverses an expression and escapes all the user-defined functions. Special function calls like "hill(...)" are not expanded.
912
+ function recursive_escape_functions ! (expr:: ExprValues )
909
913
(typeof (expr) != Expr) && (return expr)
910
- foreach (i -> expr. args[i] = recursive_expand_functions ! (expr. args[i]),
914
+ foreach (i -> expr. args[i] = recursive_escape_functions ! (expr. args[i]),
911
915
1 : length (expr. args))
912
916
if expr. head == :call
913
917
! isdefined (Catalyst, expr. args[1 ]) && (expr. args[1 ] = esc (expr. args[1 ]))
914
918
end
915
919
expr
916
920
end
917
921
922
+ # Recursively escape functions in the right-hand-side of an equation written using user-defined functions. Special function calls like "hill(...)" are not expanded.
923
+ function escape_equation_RHS! (eqexpr:: Expr )
924
+ rhs = recursive_escape_functions! (eqexpr. args[3 ])
925
+ eqexpr. args[3 ] = rhs
926
+ eqexpr
927
+ end
928
+
918
929
# Returns the length of a expression tuple, or 1 if it is not an expression tuple (probably a Symbol/Numerical).
919
930
function tup_leng (ex:: ExprValues )
920
931
(typeof (ex) == Expr && ex. head == :tuple ) && (return length (ex. args))
0 commit comments