Skip to content
  • Sponsor
  • Notifications You must be signed in to change notification settings
  • Fork 79
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor "src/dsl.jl" file #985

Merged
merged 52 commits into from
Feb 13, 2025
Merged
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
efd44be
save progress
TorkelE May 27, 2024
d2985c0
save progress
TorkelE May 28, 2024
b06068f
save progress
TorkelE May 28, 2024
5cc8f41
save progress
TorkelE May 28, 2024
15273f1
save progress
TorkelE May 28, 2024
b105b3e
add additional tests
TorkelE May 28, 2024
1324428
up
TorkelE May 28, 2024
71c9b5c
fix
TorkelE Jun 2, 2024
9ab9290
Merge branch 'master' into dsl_file_improvements
TorkelE Jun 11, 2024
d45f0a7
merge fixes
TorkelE Jun 11, 2024
b6ba044
Merge branch 'master' into src___refactoring___dsl_file
TorkelE Jul 13, 2024
cdac225
merge fixes
TorkelE Jul 13, 2024
ea9aae3
up
TorkelE Jul 13, 2024
7084133
test fixes
TorkelE Jul 14, 2024
a6b4c3a
format
TorkelE Jul 14, 2024
160668f
up
TorkelE Jul 14, 2024
9c265ee
observables fix
TorkelE Jul 14, 2024
34fc2c0
spatial transport reaction dsl fix
TorkelE Jul 14, 2024
e9fa7ba
Merge branch 'master' into src___refactoring___dsl_file
TorkelE Jul 16, 2024
37ea1d9
iv is now a parmaeter
TorkelE Jul 16, 2024
87464f7
Add tests for various erroneous declarations
TorkelE Jul 17, 2024
8156eca
update old @variable test
TorkelE Jul 17, 2024
fba3f5e
Merge branch 'master' into src___refactoring___dsl_file
TorkelE Jan 16, 2025
50f8f82
save progress
TorkelE Jan 16, 2025
4e720ee
save progress
TorkelE Jan 16, 2025
a1e967a
save progress
TorkelE Jan 17, 2025
fadeb67
Merge branch 'master' into src___refactoring___dsl_file
TorkelE Jan 18, 2025
b235784
save progress
TorkelE Jan 18, 2025
18fcbf1
prepare formatting round
TorkelE Jan 18, 2025
9230667
up
TorkelE Jan 18, 2025
78ca087
multiple fixes
TorkelE Jan 18, 2025
9d62c28
docstring fix
TorkelE Jan 18, 2025
9a928fe
fixes
TorkelE Jan 18, 2025
53a6254
up
TorkelE Jan 18, 2025
27f7add
fix function expansion of differentials
TorkelE Jan 18, 2025
99e54d2
spelling fix
TorkelE Jan 18, 2025
7ec7383
better handling of forbidden symbols and more tests
TorkelE Jan 19, 2025
5f35480
improve option error handling, more tests
TorkelE Jan 19, 2025
292da20
minor upodates after read through
TorkelE Jan 19, 2025
2902101
doc fix, combinatorial_ratelaw error handling
TorkelE Jan 20, 2025
5e342fb
minor ups
TorkelE Jan 20, 2025
7ff3f7d
code spell check
TorkelE Jan 20, 2025
97cd2ac
Update api.md
TorkelE Jan 21, 2025
109cfa2
Merge remote-tracking branch 'origin/src___refactoring___dsl_file' in…
TorkelE Jan 21, 2025
06b9ca4
Update docs/src/api.md
TorkelE Feb 12, 2025
204809e
Update src/dsl.jl
TorkelE Feb 12, 2025
970db09
Update docs/src/model_creation/dsl_advanced.md
TorkelE Feb 12, 2025
2f1ad05
Update src/dsl.jl
TorkelE Feb 12, 2025
3c9942b
Update src/dsl.jl
TorkelE Feb 12, 2025
ecea402
Update src/dsl.jl
TorkelE Feb 12, 2025
aac44f2
fixes
TorkelE Feb 12, 2025
43ac751
change names of e.g. observed in DSL call
TorkelE Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
minor ups
TorkelE committed Jan 20, 2025
commit 5e342fb8c43bcced25de4142be65045b11f9dde6
89 changes: 44 additions & 45 deletions src/dsl.jl
Original file line number Diff line number Diff line change
@@ -422,9 +422,8 @@ function push_reactions!(reactions::Vector{DSLReaction}, subs::ExprValues,
# This finds these tuples' lengths (or 1 for non-tuple forms). Inconsistent lengths yield error.
lengs = (tup_leng(subs), tup_leng(prods), tup_leng(rate), tup_leng(metadata))
maxl = maximum(lengs)
if any(!(leng == 1 || leng == maxl) for leng in lengs)
any(!(leng == 1 || leng == maxl) for leng in lengs) &&
error("Malformed reaction, rate: $rate, subs: $subs, prods: $prods, metadata: $metadata.")
end

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

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

# Takes a ModelingToolkit declaration macro (like @parameters ...) and return and expression:
# That calls the macro and then scalarizes all the symbols created into a vector of Nums.
# stores the created symbolic variables in a variable (which name is generated from `name`).
# It will also return the name used for the variable that stores the symbolic variables.
function scalarize_macro(expr_init, name)
# Generates a random variable name which (in generated code) will store the produced
# symbolic variables (e.g. `var"##ps#384"`).
namesym = gensym(name)

# If the input expression is non-empty, wraps it with additional information.
if expr_init != :(())
symvec = gensym()
expr = quote
$symvec = $expr_init
$namesym = reduce(vcat, Symbolics.scalarize($symvec))
end
else
expr = :($namesym = Num[])
end

return expr, namesym
end

# From the system reactions (as `DSLReaction`s) and equations (as expressions),
# creates the expressions that evaluates to the reaction (+ equations) vector.
function get_rxexprs(reactions, equations, all_syms)
@@ -598,6 +574,29 @@ function get_rxexpr(rx::DSLReaction)
return rx_constructor
end

# Takes a ModelingToolkit declaration macro (like @parameters ...) and return and expression:
# That calls the macro and then scalarizes all the symbols created into a vector of Nums.
# stores the created symbolic variables in a variable (which name is generated from `name`).
# It will also return the name used for the variable that stores the symbolic variables.
function scalarize_macro(expr_init, name)
# Generates a random variable name which (in generated code) will store the produced
# symbolic variables (e.g. `var"##ps#384"`).
namesym = gensym(name)

# If the input expression is non-empty, wraps it with additional information.
if expr_init != :(())
symvec = gensym()
expr = quote
$symvec = $expr_init
$namesym = reduce(vcat, Symbolics.scalarize($symvec))
end
else
expr = :($namesym = Num[])
end

return expr, namesym
end

# Recursively escape functions within equations of an equation written using user-defined functions.
# Does not escape special function calls like "hill(...)" and differential operators. Does
# also not escape stuff corresponding to e.g. species or parameters (required for good error
@@ -610,25 +609,6 @@ end

### DSL Option Handling ###

# Finds the time independent variable, and any potential spatial independent variables.
# Returns these (individually and combined), as well as an expression for declaring them.
function read_ivs_option(options)
# Creates the independent variables expressions (depends on whether the `ivs` option was used).
if haskey(options, :ivs)
ivs = Tuple(extract_syms(options, :ivs))
ivsexpr = copy(options[:ivs])
ivsexpr.args[1] = Symbol("@", "parameters")
else
ivs = (DEFAULT_IV_SYM,)
ivsexpr = :($(DEFAULT_IV_SYM) = default_t())
end

# Extracts the independent variables symbols (time and spatial), and returns the output.
tiv = ivs[1]
sivs = (length(ivs) > 1) ? Expr(:vect, ivs[2:end]...) : nothing
return tiv, sivs, ivs, ivsexpr
end

# Returns the `default_reaction_metadata` output. Technically Catalyst's code could have been made
# more generic to account for other default reaction metadata. Practically, this will likely
# 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)
get_block_option(options[:combinatoric_ratelaws]) : true
end

# Finds the time independent variable, and any potential spatial independent variables.
# Returns these (individually and combined), as well as an expression for declaring them.
function read_ivs_option(options)
# Creates the independent variables expressions (depends on whether the `ivs` option was used).
if haskey(options, :ivs)
ivs = Tuple(extract_syms(options, :ivs))
ivsexpr = copy(options[:ivs])
ivsexpr.args[1] = Symbol("@", "parameters")
else
ivs = (DEFAULT_IV_SYM,)
ivsexpr = :($(DEFAULT_IV_SYM) = default_t())
end

# Extracts the independent variables symbols (time and spatial), and returns the output.
tiv = ivs[1]
sivs = (length(ivs) > 1) ? Expr(:vect, ivs[2:end]...) : nothing
return tiv, sivs, ivs, ivsexpr
end

### `@reaction` Macro & its Internals ###

"""
6 changes: 3 additions & 3 deletions src/expression_utils.jl
Original file line number Diff line number Diff line change
@@ -38,10 +38,10 @@ end
# to throw an error if there is more inputs (suggesting e.g. multiple inputs on a single line).
# Note that there are only some options for which we wish to make this check.
function get_block_option(expr)
(length(expr.args) > 3) &&
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.")
(length(expr.args) < 3) &&
error("An option input ($expr) is missformatted. It seems that it has no inputs, which is expected.")
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.")
(length(expr.args) > 3) &&
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.")
return expr.args[3]
end