@@ -110,10 +110,10 @@ macro reaction_network(expr::Expr)
110
110
return make_rs_expr (:(gensym (:ReactionSystem )), expr)
111
111
end
112
112
113
- # Ideally, it would have been possible to combine the @reaction_network and @network_component macros.
114
- # However, this issue: https://github.com/JuliaLang/julia/issues/37691 causes problem with interpolations
115
- # if we make the @reaction_network macro call the @network_component macro. Instead, these uses the
116
- # same input, but passes `complete = false` to `make_rs_expr`.
113
+ # Ideally, it would have been possible to combine the @reaction_network and @network_component
114
+ # macros. However, this issue: https://github.com/JuliaLang/julia/issues/37691 causes problem
115
+ # with interpolations if we make the @reaction_network macro call the @network_component macro.
116
+ # Instead, these use the same input, but pass `complete = false` to `make_rs_expr`.
117
117
"""
118
118
@network_component
119
119
@@ -151,7 +151,7 @@ function make_rs_expr(name; complete = true)
151
151
return Expr (:block , :(@parameters t), rs_expr)
152
152
end
153
153
154
- # When both a name and a network expression is generated, dispatches these to the internal
154
+ # When both a name and a network expression are generated, dispatch these to the internal
155
155
# `make_reaction_system` function.
156
156
function make_rs_expr (name, network_expr; complete = true )
157
157
rs_expr = make_reaction_system (striplines (network_expr), name)
@@ -167,7 +167,7 @@ struct DSLReactant
167
167
stoichiometry:: ExprValues
168
168
end
169
169
170
- # Internal structure containing information about one Reaction. Contain all its substrates and
170
+ # Internal structure containing information about one Reaction. Contains all its substrates and
171
171
# products as well as its rate and potential metadata. Uses a specialized constructor.
172
172
struct DSLReaction
173
173
substrates:: Vector{DSLReactant}
188
188
# Recursive function that loops through the reaction line and finds the reactants and their
189
189
# stoichiometry. Recursion makes it able to handle weird cases like 2(X + Y + 3(Z + XY)). The
190
190
# reactants are stored in the `reactants` vector. As the expression tree is parsed, the
191
- # stoichiometry is updated and new reactants added.
191
+ # stoichiometry is updated and new reactants are added.
192
192
function recursive_find_reactants! (ex:: ExprValues , mult:: ExprValues ,
193
193
reactants:: Vector{DSLReactant} )
194
194
# We have reached the end of the expression tree and can finalise and return the reactants.
@@ -271,7 +271,7 @@ function make_reaction_system(ex::Expr, name)
271
271
# Handle interpolation of variables in the input.
272
272
ex = esc_dollars! (ex)
273
273
274
- # Extracts the lines with reactions, the lines with options, and the options. Check for input errors.
274
+ # Extract the lines with reactions, the lines with options, and the options. Check for input errors.
275
275
reaction_lines = Expr[x for x in ex. args if x. head == :tuple ]
276
276
option_lines = Expr[x for x in ex. args if x. head == :macrocall ]
277
277
options = Dict (Symbol (String (arg. args[1 ])[2 : end ]) => arg for arg in option_lines)
@@ -282,8 +282,8 @@ function make_reaction_system(ex::Expr, name)
282
282
any (! in (option_keys), keys (options)) &&
283
283
error (" The following unsupported options were used: $(filter (opt_in-> ! in (opt_in,option_keys), keys (options))) " )
284
284
285
- # Read options that explicitly declares some symbol (e.g. `@species`). Compiles a list of
286
- # all declared symbols and checks that there has been no double-declarations.
285
+ # Read options that explicitly declare some symbol (e.g. `@species`). Compiles a list of
286
+ # all declared symbols and checks that there have been no double-declarations.
287
287
sps_declared = extract_syms (options, :species )
288
288
ps_declared = extract_syms (options, :parameters )
289
289
vs_declared = extract_syms (options, :variables )
@@ -297,7 +297,7 @@ function make_reaction_system(ex::Expr, name)
297
297
error (" The following symbols $(unique (nonunique_syms)) have explicitly been declared as multiple types of components (e.g. occur in at least two of the `@species`, `@parameters`, `@variables`, `@ivs`, `@compounds`, `@differentials`). This is not allowed." )
298
298
end
299
299
300
- # Reads the reactions and equation. From these, infers species, variables, and parameters.
300
+ # Reads the reactions and equation. From these, infer species, variables, and parameters.
301
301
requiredec = haskey (options, :require_declaration )
302
302
reactions = get_reactions (reaction_lines)
303
303
sps_inferred, ps_pre_inferred = extract_sps_and_ps (reactions, syms_declared; requiredec)
@@ -319,16 +319,16 @@ function make_reaction_system(ex::Expr, name)
319
319
psexpr_init = get_psexpr (ps_inferred, options)
320
320
spsexpr_init = get_usexpr (sps_inferred, options; ivs)
321
321
vsexpr_init = get_usexpr (vs_inferred, options, :variables ; ivs)
322
- psexpr, psvar = scalarize_macro (psexpr_init, " ps" )
323
- spsexpr, spsvar = scalarize_macro (spsexpr_init, " specs" )
324
- vsexpr, vsvar = scalarize_macro (vsexpr_init, " vars" )
325
- cmpsexpr, cmpsvar = scalarize_macro (cmpexpr_init, " comps" )
322
+ psexpr, psvar = assign_var_to_symvar_declaration (psexpr_init, " ps" )
323
+ spsexpr, spsvar = assign_var_to_symvar_declaration (spsexpr_init, " specs" )
324
+ vsexpr, vsvar = assign_var_to_symvar_declaration (vsexpr_init, " vars" )
325
+ cmpsexpr, cmpsvar = assign_var_to_symvar_declaration (cmpexpr_init, " comps" )
326
326
rxsexprs = get_rxexprs (reactions, equations, all_syms)
327
327
328
328
# Assemblies the full expression that declares all required symbolic variables, and
329
329
# then the output `ReactionSystem`.
330
330
MacroTools. flatten (striplines (quote
331
- # Inserts the expressions which generates the `ReactionSystem` input.
331
+ # Inserts the expressions which generate the `ReactionSystem` input.
332
332
$ ivsexpr
333
333
$ psexpr
334
334
$ spsexpr
@@ -357,12 +357,12 @@ end
357
357
358
358
# ## DSL Reaction Reading Functions ###
359
359
360
- # Generates a vector of reaction structures, each containing the information about one reaction.
360
+ # Generates a vector of reaction structures, each containing information about one reaction.
361
361
function get_reactions (exprs:: Vector{Expr} )
362
362
# Declares an array to which we add all found reactions.
363
363
reactions = Vector {DSLReaction} (undef, 0 )
364
364
365
- # Loops through each line of reactions. Extracts and adds each lines 's reactions to `reactions`.
365
+ # Loops through each line of reactions. Extracts and adds each line 's reactions to `reactions`.
366
366
for line in exprs
367
367
# Reads core reaction information.
368
368
arrow, rate, reaction, metadata = read_reaction_line (line)
@@ -391,10 +391,10 @@ function get_reactions(exprs::Vector{Expr})
391
391
return reactions
392
392
end
393
393
394
- # Extracts the rate, reaction, and metadata fields (the last one optional) from a reaction line.
394
+ # Extract the rate, reaction, and metadata fields (the last one optional) from a reaction line.
395
395
function read_reaction_line (line:: Expr )
396
- # Handles rate, reaction, and arrow. Special routine required for the`-->` case, which
397
- # creates an expression different what the other arrows creates .
396
+ # Handles rate, reaction, and arrow. A special routine is required for the`-->` case,
397
+ # which creates an expression different from what the other arrows create .
398
398
rate = line. args[1 ]
399
399
reaction = line. args[2 ]
400
400
if reaction. head == :-->
450
450
# When the user have used the @species (or @parameters) options, extract species (or
451
451
# parameters) from its input.
452
452
function extract_syms (opts, vartype:: Symbol )
453
- # If the corresponding option have been used, uses `Symbolics._parse_vars` to find all
453
+ # If the corresponding option has been used, use `Symbolics._parse_vars` to find all
454
454
# variable within it (returning them in a vector).
455
455
return if haskey (opts, vartype)
456
456
ex = opts[vartype]
464
464
# Function looping through all reactions, to find undeclared symbols (species or
465
465
# parameters) and assign them to the right category.
466
466
function extract_sps_and_ps (reactions, excluded_syms; requiredec = false )
467
- # Loops through all reactant, extract undeclared ones as species.
467
+ # Loops through all reactants and extract undeclared ones as species.
468
468
species = OrderedSet {Union{Symbol, Expr}} ()
469
469
for reaction in reactions
470
470
for reactant in Iterators. flatten ((reaction. substrates, reaction. products))
509
509
510
510
# ## DSL Output Expression Builders ###
511
511
512
- # Given the extracted species (or variables) and the option dictionary, creates the
512
+ # Given the extracted species (or variables) and the option dictionary, create the
513
513
# `@species ...` (or `@variables ..`) expression which would declare these.
514
514
# If `key = :variables`, does this for variables (and not species).
515
515
function get_usexpr (us_extracted, options, key = :species ; ivs = (DEFAULT_IV_SYM,))
@@ -541,7 +541,7 @@ function get_psexpr(parameters_extracted, options)
541
541
end
542
542
543
543
# From the system reactions (as `DSLReaction`s) and equations (as expressions),
544
- # creates the expressions that evaluates to the reaction (+ equations) vector.
544
+ # creates the expression that evaluates to the reaction (+ equations) vector.
545
545
function get_rxexprs (reactions, equations, all_syms)
546
546
rxsexprs = :(Catalyst. CatalystEqType[])
547
547
foreach (rx -> push! (rxsexprs. args, get_rxexpr (rx)), reactions)
@@ -576,14 +576,14 @@ end
576
576
577
577
# Takes a ModelingToolkit declaration macro (like @parameters ...) and return and expression:
578
578
# That calls the macro and then scalarizes all the symbols created into a vector of Nums.
579
- # stores the created symbolic variables in a variable (which name is generated from `name`).
579
+ # stores the created symbolic variables in a variable (whose name is generated from `name`).
580
580
# It will also return the name used for the variable that stores the symbolic variables.
581
- function scalarize_macro (expr_init, name)
581
+ function assign_var_to_symvar_declaration (expr_init, name)
582
582
# Generates a random variable name which (in generated code) will store the produced
583
583
# symbolic variables (e.g. `var"##ps#384"`).
584
584
namesym = gensym (name)
585
585
586
- # If the input expression is non-empty, wraps it with additional information.
586
+ # If the input expression is non-empty, wrap it with additional information.
587
587
if expr_init != :(())
588
588
symvec = gensym ()
589
589
expr = quote
613
613
# more generic to account for other default reaction metadata. Practically, this will likely
614
614
# be the only relevant reaction metadata to have a default value via the DSL. If another becomes
615
615
# relevant, the code can be rewritten to take this into account.
616
- # Checks if the `@default_noise_scaling` option is used. If so, uses it as the default value of
616
+ # Checks if the `@default_noise_scaling` option is used. If so, use it as the default value of
617
617
# the `default_noise_scaling` reaction metadata, otherwise, returns an empty vector.
618
618
function read_default_noise_scaling_option (options)
619
619
if haskey (options, :default_noise_scaling )
@@ -625,7 +625,7 @@ function read_default_noise_scaling_option(options)
625
625
end
626
626
627
627
# When compound species are declared using the "@compound begin ... end" option, get a list
628
- # of the compound species, and also the expression that crates them.
628
+ # of the compound species, and also the expression that creates them.
629
629
function read_compound_option (options)
630
630
# If the compound option is used, retrieve a list of compound species and the option line
631
631
# that creates them (used to declare them as compounds at the end). Due to some expression
@@ -678,10 +678,10 @@ function read_events_option(options, event_type::Symbol)
678
678
end
679
679
680
680
# Reads the variables options. Outputs a list of the variables inferred from the equations,
681
- # as well as the equation vector. If the default differential was used, updates the `diffsexpr`
681
+ # as well as the equation vector. If the default differential was used, update the `diffsexpr`
682
682
# expression so that this declares this as well.
683
683
function read_equations_option! (diffsexpr, options, syms_unavailable, tiv; requiredec = false )
684
- # Prepares the equations. First, extracts equations from provided option (converting to block form if required).
684
+ # Prepares the equations. First, extract equations from the provided option (converting to block form if required).
685
685
# Next, uses MTK's `parse_equations!` function to split input into a vector with the equations.
686
686
eqs_input = haskey (options, :equations ) ? get_block_option (options[:equations ]) : :(begin end )
687
687
eqs_input = option_block_form (eqs_input)
@@ -696,7 +696,7 @@ function read_equations_option!(diffsexpr, options, syms_unavailable, tiv; requi
696
696
add_default_diff = false
697
697
for eq in equations
698
698
if (eq. head != :call ) || (eq. args[1 ] != :~ )
699
- error (" Malformed equation: \" $eq \" . Equation's left hand and right hand sides should be separated by a \" ~\" ." )
699
+ error (" Malformed equation: \" $eq \" . Equation's left hand and right- hand sides should be separated by a \" ~\" ." )
700
700
end
701
701
702
702
# If the default differential (`D`) is used, record that it should be declared later on.
@@ -713,7 +713,7 @@ function read_equations_option!(diffsexpr, options, syms_unavailable, tiv; requi
713
713
" Unrecognized symbol $(join (vs_inferred, " , " )) detected in equation expression: \" $(string (eq)) \" . Since the flag @require_declaration is declared, all symbolic variables must be explicitly declared with the @species, @variables, and @parameters options." ))
714
714
end
715
715
716
- # If `D` differential is used, add it to differential expression and inferred differentials list.
716
+ # If `D` differential is used, add it to the differential expression and inferred differentials list.
717
717
diffs_inferred = Union{Symbol, Expr}[]
718
718
if add_default_diff && ! any (diff_dec. args[1 ] == :D for diff_dec in diffsexpr. args)
719
719
diffs_inferred = [:D ]
@@ -723,7 +723,7 @@ function read_equations_option!(diffsexpr, options, syms_unavailable, tiv; requi
723
723
return collect (vs_inferred), diffs_inferred, equations
724
724
end
725
725
726
- # Searches an expression `expr` and returns true if it have any subexpression `D(...)` (where `...` can be anything).
726
+ # Searches an expression `expr` and returns true if it has any subexpression `D(...)` (where `...` can be anything).
727
727
# Used to determine whether the default differential D has been used in any equation provided to `@equations`.
728
728
function find_D_call (expr)
729
729
return if Base. isexpr (expr, :call ) && expr. args[1 ] == :D
739
739
# which is used by the default differential (if it is used).
740
740
function read_differentials_option (options)
741
741
# Creates the differential expression.
742
- # If differentials was provided as options, this is used as the initial expression.
742
+ # If differentials were provided as options, this is used as the initial expression.
743
743
# If the default differential (D(...)) was used in equations, this is added to the expression.
744
744
diffsexpr = (haskey (options, :differentials ) ?
745
745
get_block_option (options[:differentials ]) : striplines (:(begin end )))
@@ -793,7 +793,7 @@ function read_observed_option(options, all_ivs, us_declared, all_syms; requirede
793
793
((obs_name in us_declared) || is_escaped_expr (obs_eq. args[2 ])) && ! isnothing (metadata) &&
794
794
error (" Metadata was provided to observable $obs_name in the `@observables` macro. However, the observable was also declared separately (using either @species or @variables). When this is done, metadata should instead be provided within the original @species or @variable declaration." )
795
795
796
- # This bits adds the observables to the @variables vector which is given as output.
796
+ # This bit adds the observables to the @variables vector which is given as output.
797
797
# For Observables that have already been declared using @species/@variables,
798
798
# or are interpolated, this parts should not be carried out.
799
799
if ! ((obs_name in us_declared) || is_escaped_expr (obs_eq. args[2 ]))
@@ -825,7 +825,7 @@ function read_observed_option(options, all_ivs, us_declared, all_syms; requirede
825
825
(striplines (obsexpr) == striplines (Expr (:block , :(@variables )))) &&
826
826
(obsexpr = :())
827
827
else
828
- # If option is not used, return empty expression and vector.
828
+ # If observables option is not used, return empty expression and vector.
829
829
obsexpr = :()
830
830
obs_eqs = :([])
831
831
obs_syms = :([])
@@ -834,7 +834,7 @@ function read_observed_option(options, all_ivs, us_declared, all_syms; requirede
834
834
return obsexpr, obs_eqs, obs_syms
835
835
end
836
836
837
- # From the input to the @observables options, creates a vector containing one equation for
837
+ # From the input to the @observables options, create a vector containing one equation for
838
838
# each observable. `option_block_form` handles if single line declaration of `@observables`,
839
839
# i.e. without a `begin ... end` block, was used.
840
840
function make_obs_eqs (observables_expr)
@@ -845,7 +845,7 @@ function make_obs_eqs(observables_expr)
845
845
end
846
846
847
847
# Reads the combinatorial ratelaw options, which determines if a combinatorial rate law should
848
- # be used or not. If not provides, uses the default (true).
848
+ # be used or not. If not provided, use the default (true).
849
849
function read_combinatoric_ratelaws_option (options)
850
850
return haskey (options, :combinatoric_ratelaws ) ?
851
851
get_block_option (options[:combinatoric_ratelaws ]) : true
@@ -938,7 +938,7 @@ function make_reaction(ex::Expr)
938
938
reaction = get_reaction (ex)
939
939
species, parameters = extract_sps_and_ps ([reaction], [])
940
940
941
- # Checks for input errors. Needed here but not in `@reaction_network` as `ReactionSystem` perform this check but `Reaction` don 't.
941
+ # Checks for input errors. Needed here but not in `@reaction_network` as `ReactionSystem` performs this check but `Reaction` doesn 't.
942
942
forbidden_symbol_check (union (species, parameters))
943
943
944
944
# Creates expressions corresponding to code for declaring the parameters, species, and reaction.
@@ -978,14 +978,14 @@ function recursive_escape_functions!(expr::ExprValues, syms_skip = [])
978
978
expr
979
979
end
980
980
981
- # Returns the length of a expression tuple, or 1 if it is not an expression tuple (probably a Symbol/Numerical).
981
+ # Returns the length of an expression tuple, or 1 if it is not an expression tuple (probably a Symbol/Numerical).
982
982
function tup_leng (ex:: ExprValues )
983
983
(typeof (ex) == Expr && ex. head == :tuple ) && (return length (ex. args))
984
984
return 1
985
985
end
986
986
987
- # Gets the ith element in a expression tuple, or returns the input itself if it is not an expression tuple
988
- # (probably a Symbol/Numerical). This is used to handle bundled reaction (like `d, (X,Y) --> 0`).
987
+ # Gets the ith element in an expression tuple, or returns the input itself if it is not an expression tuple
988
+ # (probably a Symbol/Numerical). This is used to handle bundled reactions (like `d, (X,Y) --> 0`).
989
989
function get_tup_arg (ex:: ExprValues , i:: Int )
990
990
(tup_leng (ex) == 1 ) && (return ex)
991
991
return ex. args[i]
0 commit comments