Skip to content

Commit ef6ef28

Browse files
authored
Merge pull request #1129 from vyudu/dsl-no-infer-docs
`@require_declaration` docs
2 parents ecd28d6 + 5165672 commit ef6ef28

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

HISTORY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@
2525
StructuralIdentifiability has with Julia 1.10.5 and 1.11.
2626
- A tutorial on making interactive plot displays using Makie has been added.
2727
- The BifurcationKit extension has been updated to v.4.
28+
- There is a new DSL option `@require_declaration` that will turn off automatic inferring for species, parameters, and variables in the DSL. For example, the following will now error:
29+
```julia
30+
rn = @reaction_network begin
31+
@require_declaration
32+
(k1, k2), A <--> B
33+
end
34+
```
35+
When this flag is set, all symbolics must be explicitly declared.
36+
```julia
37+
rn = @reaction_network begin
38+
@species A(t) B(t)
39+
@parameters k1 k2
40+
@require_declaration
41+
(k1, k2), A <--> B
42+
end
43+
```
2844

2945
## Catalyst 14.4.1
3046
- Support for user-defined functions on the RHS when providing coupled equations

docs/src/faqs.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,16 @@ end
309309
In some cases, it may be necessary or desirable to register functions with
310310
Symbolics.jl before their use in Catalyst, see the discussion
311311
[here](https://symbolics.juliasymbolics.org/stable/manual/functions/).
312+
313+
## How can I turn off automatic inferring of species and parameters when using the DSL?
314+
This option can be set using the `@require_declaration` option inside `@reaction_network`. In this case all the species, parameters, and variables in the system must be pre-declared using one of the `@species`, `@parameters`, or `@variables` macros. For more information about what is inferred automatically and not, please see the section on [`@require_declaration`](@ref dsl_advanced_options_require_dec).
315+
316+
```@example faq9
317+
using Catalyst
318+
rn = @reaction_network begin
319+
@require_declaration
320+
@species A(t) B(t)
321+
@parameters k1 k2
322+
(k1, k2), A <--> B
323+
end
324+
```

docs/src/model_creation/dsl_advanced.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ ModelingToolkit.getdescription(two_state_system.kA)
205205

206206
### [Designating constant-valued/fixed species parameters](@id dsl_advanced_options_constant_species)
207207

208-
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.
208+
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.
209209
```@example dsl_advanced_constant_species
210210
using Catalyst # hide
211211
rn = @reaction_network begin
@@ -272,6 +272,39 @@ sol = solve(oprob)
272272
plot(sol)
273273
```
274274

275+
### [Turning off species, parameter, and variable inferring](@id dsl_advanced_options_require_dec)
276+
In some cases it may be desirable for Catalyst to not infer species and parameters from the DSL, as in the case of reaction networks with very many variables, or as a sanity check that variable names are written correctly. To turn off inferring, simply add the `@require_declaration` macro to one of the lines of the `@reaction_network` declaration. Having this macro means that every single variable, species, or parameter will have to be explicitly declared using the `@variable`, `@species`, or `@parameter` macro. In the case that the DSL parser encounters an undeclared symbolic, it will error with an `UndeclaredSymbolicError` and print the reaction or equation that the undeclared symbolic was found in.
277+
278+
```@example dsl_advanced_no_infer
279+
using Catalyst
280+
# The following case will throw an UndeclaredSymbolicError.
281+
try @macroexpand @reaction_network begin
282+
@require_declaration
283+
(k1, k2), A <--> B
284+
end
285+
catch e
286+
println(e.msg)
287+
end
288+
```
289+
In order to avoid an error in this case all the relevant species and parameters will have to be declared.
290+
```
291+
# The following case will not error.
292+
t = default_t()
293+
rn = @reaction_network begin
294+
@require_declaration
295+
@species A(t) B(t)
296+
@parameters k1 k2
297+
(k1, k2), A <--> B
298+
end
299+
```
300+
301+
The following cases in which the DSL would normally infer variables will all throw errors if `@require_declaration` is set and the variables are not explicitly declared.
302+
- Inferring a species in a reaction, as in the example above
303+
- Inferring a parameter in a reaction rate expression, as in the reaction line `k*n, A --> B`
304+
- Inferring a parameter in the stoichiometry of a species, as in the reaction line `k, n*A --> B`
305+
- Inferring a differential variable on the LHS of a coupled differential equation, as in `A` in `@equations D(A) ~ A^2`
306+
- Inferring an [observable](@ref dsl_advanced_options_observables) that is declared using `@observables`
307+
275308
## [Naming reaction networks](@id dsl_advanced_options_naming)
276309
Each reaction network model has a name. It can be accessed using the `nameof` function. By default, some generic name is used:
277310
```@example dsl_advanced_names

0 commit comments

Comments
 (0)