144
144
function make_rs_expr (name; complete = true )
145
145
rs_expr = :(ReactionSystem (Reaction[], t, [], []; name = $ name))
146
146
complete && (rs_expr = :(complete ($ rs_expr)))
147
- return Expr (:block , :(@parameters t ), rs_expr)
147
+ return Expr (:block , :(t = default_t () ), rs_expr)
148
148
end
149
149
150
150
# When both a name and a network expression are generated, dispatch these to the internal
@@ -270,12 +270,12 @@ function make_reaction_system(ex::Expr, name)
270
270
# Extract the lines with reactions, the lines with options, and the options. Check for input errors.
271
271
reaction_lines = Expr[x for x in ex. args if x. head == :tuple ]
272
272
option_lines = Expr[x for x in ex. args if x. head == :macrocall ]
273
- options = Dict (Symbol (String (arg. args[1 ])[2 : end ]) => arg for arg in option_lines)
274
273
allunique (arg. args[1 ] for arg in option_lines) ||
275
274
error (" Some options where given multiple times." )
276
275
numlines = length (reaction_lines) + length (option_lines)
277
276
(numlines != length (ex. args)) &&
278
- error (" @reaction_network input contain $(length (ex. args) - $ numlines) malformed lines." )
277
+ error (" @reaction_network input contain $(length (ex. args) - numlines) malformed lines." )
278
+ options = Dict (Symbol (String (arg. args[1 ])[2 : end ]) => arg for arg in option_lines)
279
279
any (! in (option_keys), keys (options)) &&
280
280
error (" The following unsupported options were used: $(filter (opt_in-> ! in (opt_in,option_keys), keys (options))) " )
281
281
@@ -316,7 +316,7 @@ function make_reaction_system(ex::Expr, name)
316
316
psexpr_init = get_psexpr (ps_inferred, options)
317
317
spsexpr_init = get_usexpr (sps_inferred, options; ivs)
318
318
vsexpr_init = get_usexpr (vs_inferred, options, :variables ; ivs)
319
- psexpr, psvar = assign_var_to_symvar_declaration (psexpr_init, " ps" )
319
+ psexpr, psvar = assign_var_to_symvar_declaration (psexpr_init, " ps" , scalarize = false )
320
320
spsexpr, spsvar = assign_var_to_symvar_declaration (spsexpr_init, " specs" )
321
321
vsexpr, vsvar = assign_var_to_symvar_declaration (vsexpr_init, " vars" )
322
322
cmpsexpr, cmpsvar = assign_var_to_symvar_declaration (cmpexpr_init, " comps" )
@@ -328,8 +328,8 @@ function make_reaction_system(ex::Expr, name)
328
328
# Inserts the expressions which generate the `ReactionSystem` input.
329
329
$ ivsexpr
330
330
$ psexpr
331
- $ spsexpr
332
331
$ vsexpr
332
+ $ spsexpr
333
333
$ obsexpr
334
334
$ cmpsexpr
335
335
$ diffsexpr
@@ -419,12 +419,12 @@ function push_reactions!(reactions::Vector{DSLReaction}, subs::ExprValues,
419
419
# This finds these tuples' lengths (or 1 for non-tuple forms). Inconsistent lengths yield error.
420
420
lengs = (tup_leng (subs), tup_leng (prods), tup_leng (rate), tup_leng (metadata))
421
421
maxlen = maximum (lengs)
422
- any (! (leng == 1 || leng == maxl ) for leng in lengs) &&
422
+ any (! (leng == 1 || leng == maxlen ) for leng in lengs) &&
423
423
error (" Malformed reaction, rate: $rate , subs: $subs , prods: $prods , metadata: $metadata ." )
424
424
425
425
# Loops through each reaction encoded by the reaction's different components.
426
426
# Creates a `DSLReaction` representation and adds it to `reactions`.
427
- for i in 1 : maxl
427
+ for i in 1 : maxlen
428
428
# If the `only_use_rate` metadata was not provided, this must be inferred from the arrow.
429
429
metadata_i = get_tup_arg (metadata, i)
430
430
if all (arg. args[1 ] != :only_use_rate for arg in metadata_i. args)
@@ -488,8 +488,8 @@ function extract_sps_and_ps(reactions, excluded_syms; requiredec = false)
488
488
collect (species), collect (parameters)
489
489
end
490
490
491
- # Function called by `extract_sps_and_ps`, recursively loops through an
492
- # expression and find symbols (adding them to the push_symbols vector).
491
+ # Function called by `extract_sps_and_ps`, recursively loops through an expression and find
492
+ # symbols (adding them to the push_symbols vector). Returns `nothing` to ensure type stability .
493
493
function add_syms_from_expr! (push_symbols:: AbstractSet , expr:: ExprValues , excluded_syms)
494
494
# If we have encountered a Symbol in the recursion, we can try extracting it.
495
495
if expr isa Symbol
@@ -502,6 +502,7 @@ function add_syms_from_expr!(push_symbols::AbstractSet, expr::ExprValues, exclud
502
502
add_syms_from_expr! (push_symbols, expr. args[i], excluded_syms)
503
503
end
504
504
end
505
+ nothing
505
506
end
506
507
507
508
# ## DSL Output Expression Builders ###
@@ -575,22 +576,28 @@ end
575
576
# That calls the macro and then scalarizes all the symbols created into a vector of Nums.
576
577
# stores the created symbolic variables in a variable (whose name is generated from `name`).
577
578
# It will also return the name used for the variable that stores the symbolic variables.
578
- function assign_var_to_symvar_declaration (expr_init, name)
579
+ # If requested, performs scalarization.
580
+ function assign_var_to_symvar_declaration (expr_init, name; scalarize = true )
579
581
# Generates a random variable name which (in generated code) will store the produced
580
582
# symbolic variables (e.g. `var"##ps#384"`).
581
583
namesym = gensym (name)
582
584
583
585
# If the input expression is non-empty, wrap it with additional information.
584
586
if expr_init != :(())
585
- symvec = gensym ()
586
- expr = quote
587
- $ symvec = $ expr_init
588
- $ namesym = reduce (vcat, Symbolics. scalarize ($ symvec))
587
+ if scalarize
588
+ symvec = gensym ()
589
+ expr = quote
590
+ $ symvec = $ expr_init
591
+ $ namesym = reduce (vcat, Symbolics. scalarize ($ symvec))
592
+ end
593
+ else
594
+ expr = quote
595
+ $ namesym = $ expr_init
596
+ end
589
597
end
590
598
else
591
599
expr = :($ namesym = Num[])
592
600
end
593
-
594
601
return expr, namesym
595
602
end
596
603
@@ -855,7 +862,7 @@ function read_ivs_option(options)
855
862
if haskey (options, :ivs )
856
863
ivs = Tuple (extract_syms (options, :ivs ))
857
864
ivsexpr = copy (options[:ivs ])
858
- ivsexpr. args[1 ] = Symbol (" @" , " parameters " )
865
+ ivsexpr. args[1 ] = Symbol (" @" , " independent_variables " )
859
866
else
860
867
ivs = (DEFAULT_IV_SYM,)
861
868
ivsexpr = :($ (DEFAULT_IV_SYM) = default_t ())
0 commit comments