Skip to content

Commit 5e342fb

Browse files
committed
minor ups
1 parent 2902101 commit 5e342fb

File tree

2 files changed

+47
-48
lines changed

2 files changed

+47
-48
lines changed

src/dsl.jl

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,8 @@ function push_reactions!(reactions::Vector{DSLReaction}, subs::ExprValues,
422422
# This finds these tuples' lengths (or 1 for non-tuple forms). Inconsistent lengths yield error.
423423
lengs = (tup_leng(subs), tup_leng(prods), tup_leng(rate), tup_leng(metadata))
424424
maxl = maximum(lengs)
425-
if any(!(leng == 1 || leng == maxl) for leng in lengs)
425+
any(!(leng == 1 || leng == maxl) for leng in lengs) &&
426426
error("Malformed reaction, rate: $rate, subs: $subs, prods: $prods, metadata: $metadata.")
427-
end
428427

429428
# Loops through each reaction encoded by the reaction's different components.
430429
# Creates a `DSLReaction` representation and adds it to `reactions`.
@@ -492,7 +491,7 @@ function extract_sps_and_ps(reactions, excluded_syms; requiredec = false)
492491
collect(species), collect(parameters)
493492
end
494493

495-
# Function called by extract_sps_and_ps, recursively loops through an
494+
# Function called by `extract_sps_and_ps`, recursively loops through an
496495
# expression and find symbols (adding them to the push_symbols vector).
497496
function add_syms_from_expr!(push_symbols::AbstractSet, expr::ExprValues, excluded_syms)
498497
# If we have encountered a Symbol in the recursion, we can try extracting it.
@@ -541,29 +540,6 @@ function get_psexpr(parameters_extracted, options)
541540
pexprs
542541
end
543542

544-
# Takes a ModelingToolkit declaration macro (like @parameters ...) and return and expression:
545-
# That calls the macro and then scalarizes all the symbols created into a vector of Nums.
546-
# stores the created symbolic variables in a variable (which name is generated from `name`).
547-
# It will also return the name used for the variable that stores the symbolic variables.
548-
function scalarize_macro(expr_init, name)
549-
# Generates a random variable name which (in generated code) will store the produced
550-
# symbolic variables (e.g. `var"##ps#384"`).
551-
namesym = gensym(name)
552-
553-
# If the input expression is non-empty, wraps it with additional information.
554-
if expr_init != :(())
555-
symvec = gensym()
556-
expr = quote
557-
$symvec = $expr_init
558-
$namesym = reduce(vcat, Symbolics.scalarize($symvec))
559-
end
560-
else
561-
expr = :($namesym = Num[])
562-
end
563-
564-
return expr, namesym
565-
end
566-
567543
# From the system reactions (as `DSLReaction`s) and equations (as expressions),
568544
# creates the expressions that evaluates to the reaction (+ equations) vector.
569545
function get_rxexprs(reactions, equations, all_syms)
@@ -598,6 +574,29 @@ function get_rxexpr(rx::DSLReaction)
598574
return rx_constructor
599575
end
600576

577+
# Takes a ModelingToolkit declaration macro (like @parameters ...) and return and expression:
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`).
580+
# It will also return the name used for the variable that stores the symbolic variables.
581+
function scalarize_macro(expr_init, name)
582+
# Generates a random variable name which (in generated code) will store the produced
583+
# symbolic variables (e.g. `var"##ps#384"`).
584+
namesym = gensym(name)
585+
586+
# If the input expression is non-empty, wraps it with additional information.
587+
if expr_init != :(())
588+
symvec = gensym()
589+
expr = quote
590+
$symvec = $expr_init
591+
$namesym = reduce(vcat, Symbolics.scalarize($symvec))
592+
end
593+
else
594+
expr = :($namesym = Num[])
595+
end
596+
597+
return expr, namesym
598+
end
599+
601600
# Recursively escape functions within equations of an equation written using user-defined functions.
602601
# Does not escape special function calls like "hill(...)" and differential operators. Does
603602
# also not escape stuff corresponding to e.g. species or parameters (required for good error
@@ -610,25 +609,6 @@ end
610609

611610
### DSL Option Handling ###
612611

613-
# Finds the time independent variable, and any potential spatial independent variables.
614-
# Returns these (individually and combined), as well as an expression for declaring them.
615-
function read_ivs_option(options)
616-
# Creates the independent variables expressions (depends on whether the `ivs` option was used).
617-
if haskey(options, :ivs)
618-
ivs = Tuple(extract_syms(options, :ivs))
619-
ivsexpr = copy(options[:ivs])
620-
ivsexpr.args[1] = Symbol("@", "parameters")
621-
else
622-
ivs = (DEFAULT_IV_SYM,)
623-
ivsexpr = :($(DEFAULT_IV_SYM) = default_t())
624-
end
625-
626-
# Extracts the independent variables symbols (time and spatial), and returns the output.
627-
tiv = ivs[1]
628-
sivs = (length(ivs) > 1) ? Expr(:vect, ivs[2:end]...) : nothing
629-
return tiv, sivs, ivs, ivsexpr
630-
end
631-
632612
# Returns the `default_reaction_metadata` output. Technically Catalyst's code could have been made
633613
# more generic to account for other default reaction metadata. Practically, this will likely
634614
# be the only relevant reaction metadata to have a default value via the DSL. If another becomes
@@ -871,6 +851,25 @@ function read_combinatoric_ratelaws_option(options)
871851
get_block_option(options[:combinatoric_ratelaws]) : true
872852
end
873853

854+
# Finds the time independent variable, and any potential spatial independent variables.
855+
# Returns these (individually and combined), as well as an expression for declaring them.
856+
function read_ivs_option(options)
857+
# Creates the independent variables expressions (depends on whether the `ivs` option was used).
858+
if haskey(options, :ivs)
859+
ivs = Tuple(extract_syms(options, :ivs))
860+
ivsexpr = copy(options[:ivs])
861+
ivsexpr.args[1] = Symbol("@", "parameters")
862+
else
863+
ivs = (DEFAULT_IV_SYM,)
864+
ivsexpr = :($(DEFAULT_IV_SYM) = default_t())
865+
end
866+
867+
# Extracts the independent variables symbols (time and spatial), and returns the output.
868+
tiv = ivs[1]
869+
sivs = (length(ivs) > 1) ? Expr(:vect, ivs[2:end]...) : nothing
870+
return tiv, sivs, ivs, ivsexpr
871+
end
872+
874873
### `@reaction` Macro & its Internals ###
875874

876875
"""

src/expression_utils.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ end
3838
# to throw an error if there is more inputs (suggesting e.g. multiple inputs on a single line).
3939
# Note that there are only some options for which we wish to make this check.
4040
function get_block_option(expr)
41-
(length(expr.args) > 3) &&
42-
error("An option input ($expr) is missformatted. Potentially, it has multiple inputs on a single lines, and these should be split across multiple lines using a `begin ... end` block.")
4341
(length(expr.args) < 3) &&
44-
error("An option input ($expr) is missformatted. It seems that it has no inputs, which is expected.")
42+
error("The $(expr.args[1]) option's input was misformatted (full declaration: `$expr`). It seems that it has no inputs, whereas some input is expected.")
43+
(length(expr.args) > 3) &&
44+
error("The $(expr.args[1]) option's input was misformatted (full declaration: `$expr`). Potentially, it has multiple inputs on a single line, in which case these should be split across multiple lines using a `begin ... end` block.")
4545
return expr.args[3]
4646
end
4747

0 commit comments

Comments
 (0)