Skip to content

Commit d0fea7d

Browse files
authored
Merge pull request #985 from SciML/src___refactoring___dsl_file
Refactor "src/dsl.jl" file
2 parents f086599 + 43ac751 commit d0fea7d

File tree

10 files changed

+971
-632
lines changed

10 files changed

+971
-632
lines changed

docs/src/api.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ ReactionSystem
8686
```
8787

8888
## [Options for the `@reaction_network` DSL](@id api_dsl_options)
89-
We have [previously described](@ref dsl_advanced_options) how options permits the user to supply non-reaction information to [`ReactionSystem`](@ref) created through the DSL. Here follows a list
89+
We have [previously described](@ref dsl_advanced_options) how options allow one to supply additional information to a [`ReactionSystem`](@ref) created with the DSL. Here follows a list
9090
of all options currently available.
91-
- [`parameters`]:(@ref dsl_advanced_options_declaring_species_and_parameters) Allows the designation of a set of symbols as system parameter.
91+
- [`parameters`](@ref dsl_advanced_options_declaring_species_and_parameters): Allows the designation of a set of symbols as system parameters.
9292
- [`species`](@ref dsl_advanced_options_declaring_species_and_parameters): Allows the designation of a set of symbols as system species.
9393
- [`variables`](@ref dsl_advanced_options_declaring_species_and_parameters): Allows the designation of a set of symbols as system non-species variables.
9494
- [`ivs`](@ref dsl_advanced_options_ivs): Allows the designation of a set of symbols as system independent variables.
@@ -100,6 +100,7 @@ of all options currently available.
100100
- [`continuous_events`](@ref constraint_equations_events): Allows the creation of continuous events.
101101
- [`discrete_events`](@ref constraint_equations_events): Allows the creation of discrete events.
102102
- [`combinatoric_ratelaws`](@ref faq_combinatoric_ratelaws): Takes a single option (`true` or `false`), which sets whether to use combinatorial rate laws.
103+
- [`require_declaration`](@ref dsl_advanced_options_require_dec): Turns off all inference of parameters, species, variables, the default differential, and observables (requiring these to be explicitly declared using e.g. `@species`).
103104

104105
## [ModelingToolkit and Catalyst accessor functions](@id api_accessor_functions)
105106
A [`ReactionSystem`](@ref) is an instance of a

docs/src/model_creation/dsl_advanced.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ ModelingToolkit.getdescription(two_state_system.kA)
204204
```
205205

206206
### [Designating constant-valued/fixed species parameters](@id dsl_advanced_options_constant_species)
207-
208207
Catalyst enables the designation of parameters as `constantspecies`. These parameters can be used as species in reactions, however, their values are not changed by the reaction and remain constant throughout the simulation (unless changed by e.g. the [occurrence of an event](@ref constraint_equations_events). Practically, this is done by setting the parameter's `isconstantspecies` metadata to `true`. Here, we create a simple reaction where the species `X` is converted to `Xᴾ` at rate `k`. By designating `X` as a constant species parameter, we ensure that its quantity is unchanged by the occurrence of the reaction.
209208
```@example dsl_advanced_constant_species
210209
using Catalyst # hide
@@ -575,6 +574,45 @@ nothing # hide
575574
!!! note
576575
When using interpolation, expressions like `2$spec` won't work; the multiplication symbol must be explicitly included like `2*$spec`.
577576

577+
## [Creating individual reaction using the `@reaction` macro](@id dsl_advanced_options_reaction_macro)
578+
Catalyst exports a macro `@reaction`, which can be used to generate a singular [`Reaction`](@ref) object of the same type which is stored within the [`ReactionSystem`](@ref) structure (which in turn can be generated by `@reaction_network`). Generally, `@reaction` follows [identical rules to those of `@reaction_network`](@ref dsl_description_reactions) for writing and interpreting reactions (however, bi-directional reactions are not permitted). E.g. here we create a simple dimerisation reaction:
579+
```@example dsl_advanced_reaction_macro
580+
using Catalyst # hide
581+
rx_dimerisation = @reaction kD, 2X --> X2
582+
```
583+
Here, `@reaction` is followed by a single line consisting of three parts:
584+
- A rate (at which the reaction occurs).
585+
- Any number of substrates (which are consumed by the reaction).
586+
- Any number of products (which are produced by the reaction).
587+
588+
In the next example, we first create a simple [SIR model](@ref basic_CRN_library_sir). Then, we specify the same model by instead creating its individual reaction components using the `@reaction` macro. Finally, we confirm that these are identical to those stored in the initial model (using the [`reactions`](@ref) function).
589+
```@example dsl_advanced_reaction_macro
590+
sir_model = @reaction_network begin
591+
α, S + I --> 2I
592+
β, I --> R
593+
end
594+
infection_rx = @reaction α, S + I --> 2I
595+
recovery_rx = @reaction β, I --> R
596+
sir_rxs = [infection_rx, recovery_rx]
597+
issetequal(reactions(sir_model), sir_rxs)
598+
```
599+
One of the primary uses of the `@reaction` macro is to provide some of the convenience of the DSL to [*programmatic modelling](@ref programmatic_CRN_construction). E.g. here we can combine our reactions to create a `ReactionSystem` directly, and also confirm that this is identical to the model created through the DSL:
600+
```@example dsl_advanced_reaction_macro
601+
sir_programmatic = complete(ReactionSystem(sir_rxs, default_t(); name = :sir))
602+
sir_programmatic == sir_model
603+
```
604+
605+
During programmatic modelling, it can be good to keep in mind that already declared symbolic variables can be [*interpolated*](@ref dsl_advanced_options_symbolics_and_DSL_interpolation). E.g. here we create two production reactions both depending on the same Michaelis-Menten function:
606+
```@example dsl_advanced_reaction_macro
607+
t = default_t()
608+
@species X(t)
609+
@parameters v K
610+
mm_term = Catalyst.mm(X, v, K)
611+
rx1 = @reaction $mm_term, 0 --> P1
612+
rx2 = @reaction $mm_term, 0 --> P2
613+
nothing # hide
614+
```
615+
578616
## [Disabling mass action for reactions](@id dsl_advanced_options_disable_ma)
579617

580618
As [described previously](@ref math_models_in_catalyst_rre_odes), Catalyst uses *mass action kinetics* to generate ODEs from reactions. Here, each reaction generates a term for each of its reactants, which consists of the reaction's rate, substrates, and the reactant's stoichiometry. E.g. the following reaction:

src/Catalyst.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ const CONSERVED_CONSTANT_SYMBOL = :Γ
7272
const forbidden_symbols_skip = Set([:ℯ, :pi, , :t, :∅])
7373
const forbidden_symbols_error = union(Set([:im, :nothing, CONSERVED_CONSTANT_SYMBOL]),
7474
forbidden_symbols_skip)
75-
const forbidden_variables_error = let
76-
fvars = copy(forbidden_symbols_error)
77-
delete!(fvars, :t)
78-
fvars
79-
end
8075

8176
### Package Main ###
8277

src/chemistry_functionality.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function make_compound(expr)
8686
# Cannot extract directly using e.g. "getfield.(composition, :reactant)" because then
8787
# we get something like :([:C, :O]), rather than :([C, O]).
8888
composition = Catalyst.recursive_find_reactants!(expr.args[3], 1,
89-
Vector{ReactantStruct}(undef, 0))
89+
Vector{DSLReactant}(undef, 0))
9090
components = :([]) # Becomes something like :([C, O]).
9191
coefficients = :([]) # Becomes something like :([1, 2]).
9292
for comp in composition

0 commit comments

Comments
 (0)