Skip to content

Commit 27f7add

Browse files
committed
fix function expansion of differentials
1 parent 53a6254 commit 27f7add

File tree

4 files changed

+18
-38
lines changed

4 files changed

+18
-38
lines changed

src/dsl.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,11 @@ function make_reaction_system(ex::Expr, name)
305305
union(syms_declared, sps_inferred), tiv; requiredec)
306306
ps_inferred = setdiff(ps_pre_inferred, vs_inferred, diffs_inferred)
307307
syms_inferred = union(sps_inferred, ps_inferred, vs_inferred, diffs_inferred)
308+
all_syms = union(syms_declared, syms_inferred)
308309

309310
# Read options not related to the declaration or inference of symbols.
310311
obsexpr, obs_eqs, obs_syms = read_observed_options(options, ivs,
311-
union(sps_declared, vs_declared), union(syms_declared, syms_inferred); requiredec)
312+
union(sps_declared, vs_declared), all_syms; requiredec)
312313
continuous_events_expr = read_events_option(options, :continuous_events)
313314
discrete_events_expr = read_events_option(options, :discrete_events)
314315
default_reaction_metadata = read_default_noise_scaling_option(options)
@@ -322,7 +323,7 @@ function make_reaction_system(ex::Expr, name)
322323
spsexpr, spsvar = scalarize_macro(spsexpr_init, "specs")
323324
vsexpr, vsvar = scalarize_macro(vsexpr_init, "vars")
324325
cmpsexpr, cmpsvar = scalarize_macro(cmpexpr_init, "comps")
325-
rxsexprs = get_rxexprs(reactions, equations, union(diffs_declared, diffs_inferred))
326+
rxsexprs = get_rxexprs(reactions, equations, all_syms)
326327

327328
# Assemblies the full expression that declares all required symbolic variables, and
328329
# then the output `ReactionSystem`.
@@ -565,10 +566,10 @@ end
565566

566567
# From the system reactions (as `DSLReaction`s) and equations (as expressions),
567568
# creates the expressions that evaluates to the reaction (+ equations) vector.
568-
function get_rxexprs(reactions, equations, diffsyms)
569+
function get_rxexprs(reactions, equations, all_syms)
569570
rxsexprs = :(Catalyst.CatalystEqType[])
570571
foreach(rx -> push!(rxsexprs.args, get_rxexpr(rx)), reactions)
571-
foreach(eq -> push!(rxsexprs.args, escape_equation!(eq, diffsyms)), equations)
572+
foreach(eq -> push!(rxsexprs.args, escape_equation!(eq, all_syms)), equations)
572573
return rxsexprs
573574
end
574575

@@ -598,10 +599,12 @@ function get_rxexpr(rx::DSLReaction)
598599
end
599600

600601
# Recursively escape functions within equations of an equation written using user-defined functions.
601-
# Does not expand special function calls like "hill(...)" and differential operators.
602-
function escape_equation!(eqexpr::Expr, diffsyms)
603-
eqexpr.args[2] = recursive_escape_functions!(eqexpr.args[2], diffsyms)
604-
eqexpr.args[3] = recursive_escape_functions!(eqexpr.args[3], diffsyms)
602+
# Does not escape special function calls like "hill(...)" and differential operators. Does
603+
# also not escape stuff corresponding to e.g. species or parameters (required for good error
604+
# for when e.g. a species is used as a differential, or for time delays in the future).
605+
function escape_equation!(eqexpr::Expr, all_syms)
606+
eqexpr.args[2] = recursive_escape_functions!(eqexpr.args[2], all_syms)
607+
eqexpr.args[3] = recursive_escape_functions!(eqexpr.args[3], all_syms)
605608
eqexpr
606609
end
607610

@@ -934,9 +937,6 @@ function make_reaction(ex::Expr)
934937
reaction = get_reaction(ex)
935938
species, parameters = extract_sps_and_ps([reaction], [])
936939

937-
# Checks for input errors.
938-
forbidden_symbol_check(union(species, parameters))
939-
940940
# Creates expressions corresponding to code for declaring the parameters, species, and reaction.
941941
spexprs = get_usexpr(species, Dict{Symbol, Expr}())
942942
pexprs = get_psexpr(parameters, Dict{Symbol, Expr}())
@@ -964,11 +964,11 @@ end
964964

965965
# Recursively traverses an expression and escapes all the user-defined functions.
966966
# Special function calls like "hill(...)" are not expanded.
967-
function recursive_escape_functions!(expr::ExprValues, diffsyms = [])
967+
function recursive_escape_functions!(expr::ExprValues, syms_skip = [])
968968
(typeof(expr) != Expr) && (return expr)
969-
foreach(i -> expr.args[i] = recursive_escape_functions!(expr.args[i], diffsyms),
969+
foreach(i -> expr.args[i] = recursive_escape_functions!(expr.args[i], syms_skip),
970970
1:length(expr.args))
971-
if (expr.head == :call) && !isdefined(Catalyst, expr.args[1]) && expr.args[1] diffsyms
971+
if (expr.head == :call) && !isdefined(Catalyst, expr.args[1]) && expr.args[1] syms_skip
972972
expr.args[1] = esc(expr.args[1])
973973
end
974974
expr

src/expression_utils.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ function is_escaped_expr(expr)
2222
return (expr isa Expr) && (expr.head == :escape) && (length(expr.args) == 1)
2323
end
2424

25-
### Parameters/Species/Variables Symbols Correctness Checking ###
26-
27-
# Throws an error when a forbidden symbol is used.
28-
function forbidden_symbol_check(syms)
29-
used_forbidden_syms = intersect(forbidden_symbols_error, syms)
30-
isempty(used_forbidden_syms) ||
31-
error("The following symbol(s) are used as species or parameters: $used_forbidden_syms, this is not permitted.")
32-
end
33-
3425
### Catalyst-specific Expressions Manipulation ###
3526

3627
# Some options takes input on form that is either `@option ...` or `@option begin ... end`.

src/spatial_reaction_systems/spatial_reactions.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ function make_transport_reaction(rateex, species)
5555
find_parameters_in_rate!(parameters, rateex)
5656

5757
# Checks for input errors.
58-
forbidden_symbol_check(union([species], parameters))
58+
if isempty(intersect(forbidden_symbols_error, union([species], parameters)))
59+
error("The following symbol(s) are used as species or parameters: $(intersect(forbidden_symbols_error, union([species], parameters)))), this is not permitted.")
60+
end
5961

6062
# Creates expressions corresponding to actual code from the internal DSL representation.
6163
sexprs = get_usexpr([species], Dict{Symbol, Expr}())
62-
pexprs = get_pexpr(parameters, Dict{Symbol, Expr}())
64+
pexprs = get_psexpr(parameters, Dict{Symbol, Expr}())
6365
iv = :($(DEFAULT_IV_SYM) = default_t())
6466
trxexpr = :(TransportReaction($rateex, $species))
6567

test/runtests.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ end
1515
### Run Tests ###
1616
@time begin
1717
if GROUP == "All" || GROUP == "Core"
18-
# Tests the `ReactionSystem` structure and its properties.
19-
@time @safetestset "Reaction Structure" begin include("reactionsystem_core/reaction.jl") end
20-
@time @safetestset "ReactionSystem Structure" begin include("reactionsystem_core/reactionsystem.jl") end
21-
@time @safetestset "Higher Order Reactions" begin include("reactionsystem_core/higher_order_reactions.jl") end
22-
@time @safetestset "Symbolic Stoichiometry" begin include("reactionsystem_core/symbolic_stoichiometry.jl") end
23-
@time @safetestset "Parameter Type Designation" begin include("reactionsystem_core/parameter_type_designation.jl") end
24-
@time @safetestset "Custom CRN Functions" begin include("reactionsystem_core/custom_crn_functions.jl") end
25-
@time @safetestset "Coupled CRN/Equation Systems" begin include("reactionsystem_core/coupled_equation_crn_systems.jl") end
26-
@time @safetestset "Events" begin include("reactionsystem_core/events.jl") end
27-
28-
# Tests model creation via the @reaction_network DSL.
29-
@time @safetestset "DSL Basic Model Construction" begin include("dsl/dsl_basic_model_construction.jl") end
30-
@time @safetestset "DSL Advanced Model Construction" begin include("dsl/dsl_advanced_model_construction.jl") end
3118
@time @safetestset "DSL Options" begin include("dsl/dsl_options.jl") end
3219

3320
# Tests compositional and hierarchical modelling.

0 commit comments

Comments
 (0)