Skip to content

Commit 449d3db

Browse files
committed
save progress
1 parent ab2f7e0 commit 449d3db

File tree

7 files changed

+337
-294
lines changed

7 files changed

+337
-294
lines changed

src/Catalyst.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,26 @@ function __init__()
6969
end
7070
end
7171

72-
# Declare constants.
72+
### Package Constants ###
73+
74+
# Declares symbols which may neither be used as parameters nor unknowns.
75+
const forbidden_symbols_skip = Set([:ℯ, :pi, , :t, :∅])
76+
const forbidden_symbols_error = union(Set([:im, :nothing, CONSERVED_CONSTANT_SYMBOL]),
77+
forbidden_symbols_skip)
78+
const forbidden_variables_error = let
79+
fvars = copy(forbidden_symbols_error)
80+
delete!(fvars, :t)
81+
fvars
82+
end
83+
84+
# Union type for `Reaction`s and `Eqiation`s.
7385
const CatalystEqType = Union{Reaction, Equation}
7486

87+
# The symbol used for conserved quantities in conservation law eliminations.
88+
const CONSERVED_CONSTANT_SYMBOL =
89+
90+
91+
7592
# base system type and features
7693
include("reactionsystem.jl")
7794
export isspecies

src/dsl.jl

Lines changed: 80 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Example systems:
5959
probJump = JumpProblem(prob,aggregator::Direct,rn)
6060
"""
6161

62-
### Declares various options and constants. ###
62+
### Constant Declarations ###
6363

6464
# Declare various arrow types symbols used for the empty set (also 0).
6565
const empty_set = Set{Symbol}([:∅])
@@ -69,24 +69,16 @@ const bwd_arrows = Set{Symbol}([:<, :(<=), :←, :↢, :↤, :⇽, :⟵, :⟻, :
6969
const double_arrows = Set{Symbol}([:, :, :, :, :, :, :, :, Symbol("<-->")])
7070
const pure_rate_arrows = Set{Symbol}([:(=>), :(<=), :, :, :, :, :, :])
7171

72-
const CONSERVED_CONSTANT_SYMBOL =
73-
74-
# Declares symbols which may neither be used as parameters not varriables.
75-
const forbidden_symbols_skip = Set([:ℯ, :pi, , :t, :∅])
76-
const forbidden_symbols_error = union(Set([:im, :nothing, CONSERVED_CONSTANT_SYMBOL]),
77-
forbidden_symbols_skip)
78-
const forbidden_variables_error = let
79-
fvars = copy(forbidden_symbols_error)
80-
delete!(fvars, :t)
81-
fvars
82-
end
8372

8473
# Declares the keys used for various options.
8574
const option_keys = (:species, :parameters, :variables, :ivs, :compounds, :observables,
8675
:default_noise_scaling, :differentials, :equations,
8776
:continuous_events, :discrete_events, :combinatoric_ratelaws)
8877

89-
### The @species macro, basically a copy of the @variables macro. ###
78+
79+
### `@species` Macro ###
80+
81+
# The @species macro, basically a copy of the @variables macro.
9082
macro species(ex...)
9183
vars = Symbolics._parse_vars(:variables, Real, ex)
9284

@@ -117,7 +109,9 @@ macro species(ex...)
117109
esc(vars)
118110
end
119111

120-
### The main macro, takes reaction network notation and returns a ReactionSystem. ###
112+
113+
### `@reaction_network` macro ###
114+
121115
"""
122116
@reaction_network
123117
@@ -214,54 +208,15 @@ macro network_component(name::Symbol = gensym(:ReactionSystem))
214208
:(ReactionSystem(Reaction[], t, [], []; name = $(QuoteNode(name)))))
215209
end
216210

211+
### Internal DSL Structures ###
217212

218-
### Macros used for manipulating, and successively builing up, reaction systems. ###
219-
@doc raw"""
220-
@reaction
221-
222-
Generates a single [`Reaction`](@ref) object.
223-
224-
Examples:
225-
```julia
226-
rx = @reaction k*v, A + B --> C + D
227-
228-
# is equivalent to
229-
t = default_t()
230-
@parameters k v
231-
@species A(t) B(t) C(t) D(t)
232-
rx == Reaction(k*v, [A,B], [C,D])
233-
```
234-
Here `k` and `v` will be parameters and `A`, `B`, `C` and `D` will be variables.
235-
Interpolation of existing parameters/variables also works
236-
```julia
237-
t = default_t()
238-
@parameters k b
239-
@species A(t)
240-
ex = k*A^2 + t
241-
rx = @reaction b*$ex*$A, $A --> C
242-
```
243-
244-
Notes:
245-
- Any symbols arising in the rate expression that aren't interpolated are treated as
246-
parameters. In the reaction part (`α*A + B --> C + D`), coefficients are treated as
247-
parameters, e.g. `α`, and rightmost symbols as species, e.g. `A,B,C,D`.
248-
- Works with any *single* arrow types supported by [`@reaction_network`](@ref).
249-
- Interpolation of Julia variables into the macro works similar to the `@reaction_network`
250-
macro. See [The Reaction DSL](@ref dsl_description) tutorial for more details.
251-
"""
252-
macro reaction(ex)
253-
make_reaction(ex)
254-
end
255-
256-
257-
### Internal DSL structures for representing reactants and reactions. ###
258-
259-
#Structure containing information about one reactant in one reaction.
213+
# Structure containing information about one reactant in one reaction.
260214
struct ReactantStruct
261215
reactant::Union{Symbol, Expr}
262216
stoichiometry::ExprValues
263217
end
264-
#Structure containing information about one Reaction. Contain all its substrates and products as well as its rate. Contains a specialized constructor.
218+
219+
# Structure containing information about one Reaction. Contain all its substrates and products as well as its rate. Contains a specialized constructor.
265220
struct ReactionStruct
266221
substrates::Vector{ReactantStruct}
267222
products::Vector{ReactantStruct}
@@ -279,23 +234,6 @@ end
279234

280235
### Functions rephrasing the macro input as a ReactionSystem structure. ###
281236

282-
function forbidden_variable_check(v)
283-
!isempty(intersect(forbidden_variables_error, v)) &&
284-
error("The following symbol(s) are used as variables: " *
285-
((map(s -> "'" * string(s) * "', ",
286-
intersect(forbidden_variables_error, v))...)) *
287-
"this is not permited.")
288-
end
289-
290-
function forbidden_symbol_check(v)
291-
!isempty(intersect(forbidden_symbols_error, v)) &&
292-
error("The following symbol(s) are used as species or parameters: " *
293-
((map(s -> "'" * string(s) * "', ",
294-
intersect(forbidden_symbols_error, v))...)) *
295-
"this is not permited.")
296-
nothing
297-
end
298-
299237
function unique_symbol_check(syms)
300238
allunique(syms) ||
301239
error("Reaction network independent variables, parameters, species, and variables must all have distinct names, but a duplicate has been detected. ")
@@ -430,34 +368,6 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
430368
end
431369
end
432370

433-
# Function for creating a Reaction structure (used by the @reaction macro).
434-
function make_reaction(ex::Expr)
435-
436-
# Handle interpolation of variables
437-
ex = esc_dollars!(ex)
438-
439-
# Parses reactions, species, and parameters.
440-
reaction = get_reaction(ex)
441-
species, parameters = extract_species_and_parameters!([reaction], [])
442-
443-
# Checks for input errors.
444-
forbidden_symbol_check(union(species, parameters))
445-
446-
# Creates expressions corresponding to actual code from the internal DSL representation.
447-
sexprs = get_sexpr(species, Dict{Symbol, Expr}())
448-
pexprs = get_pexpr(parameters, Dict{Symbol, Expr}())
449-
rxexpr = get_rxexprs(reaction)
450-
iv = :(@variables $(DEFAULT_IV_SYM))
451-
452-
# Returns the rephrased expression.
453-
quote
454-
$pexprs
455-
$iv
456-
$sexprs
457-
$rxexpr
458-
end
459-
end
460-
461371
### Auxiliary function called by the main make_reaction_system and make_reaction functions. ###
462372

463373
# Function that handles variable interpolation.
@@ -918,6 +828,74 @@ function recursive_expand_functions!(expr::ExprValues)
918828
expr
919829
end
920830

831+
### `@reaction` Macro & its Internals ###
832+
833+
@doc raw"""
834+
@reaction
835+
836+
Generates a single [`Reaction`](@ref) object.
837+
838+
Examples:
839+
```julia
840+
rx = @reaction k*v, A + B --> C + D
841+
842+
# is equivalent to
843+
t = default_t()
844+
@parameters k v
845+
@species A(t) B(t) C(t) D(t)
846+
rx == Reaction(k*v, [A,B], [C,D])
847+
```
848+
Here `k` and `v` will be parameters and `A`, `B`, `C` and `D` will be variables.
849+
Interpolation of existing parameters/variables also works
850+
```julia
851+
t = default_t()
852+
@parameters k b
853+
@species A(t)
854+
ex = k*A^2 + t
855+
rx = @reaction b*$ex*$A, $A --> C
856+
```
857+
858+
Notes:
859+
- Any symbols arising in the rate expression that aren't interpolated are treated as
860+
parameters. In the reaction part (`α*A + B --> C + D`), coefficients are treated as
861+
parameters, e.g. `α`, and rightmost symbols as species, e.g. `A,B,C,D`.
862+
- Works with any *single* arrow types supported by [`@reaction_network`](@ref).
863+
- Interpolation of Julia variables into the macro works similar to the `@reaction_network`
864+
macro. See [The Reaction DSL](@ref dsl_description) tutorial for more details.
865+
"""
866+
macro reaction(ex)
867+
make_reaction(ex)
868+
end
869+
870+
# Function for creating a Reaction structure (used by the @reaction macro).
871+
function make_reaction(ex::Expr)
872+
873+
# Handle interpolation of variables
874+
ex = esc_dollars!(ex)
875+
876+
# Parses reactions, species, and parameters.
877+
reaction = get_reaction(ex)
878+
species, parameters = extract_species_and_parameters!([reaction], [])
879+
880+
# Checks for input errors.
881+
forbidden_symbol_check(union(species, parameters))
882+
883+
# Creates expressions corresponding to actual code from the internal DSL representation.
884+
sexprs = get_sexpr(species, Dict{Symbol, Expr}())
885+
pexprs = get_pexpr(parameters, Dict{Symbol, Expr}())
886+
rxexpr = get_rxexprs(reaction)
887+
iv = :(@variables $(DEFAULT_IV_SYM))
888+
889+
# Returns the rephrased expression.
890+
quote
891+
$pexprs
892+
$iv
893+
$sexprs
894+
$rxexpr
895+
end
896+
end
897+
898+
921899
# ### Old functions (for deleting).
922900

923901
# function get_rx_species_deletethis(rxs, ps)

src/expression_utils.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
### Forbidden Symbols Checking ###
2+
3+
# Throws an error when a forbidden symbol is used.
4+
function forbidden_symbol_check(v)
5+
!isempty(intersect(forbidden_symbols_error, v)) &&
6+
error("The following symbol(s) are used as species or parameters: " *
7+
((map(s -> "'" * string(s) * "', ",
8+
intersect(forbidden_symbols_error, v))...)) *
9+
"this is not permited.")
10+
nothing
11+
end
12+
13+
# Throws an error when a forbidden variable is used (a forbidden symbol that is not `:t`).
14+
function forbidden_variable_check(v)
15+
!isempty(intersect(forbidden_variables_error, v)) &&
16+
error("The following symbol(s) are used as variables: " *
17+
((map(s -> "'" * string(s) * "', ",
18+
intersect(forbidden_variables_error, v))...)) *
19+
"this is not permited.")
20+
end
21+
22+
23+
24+
25+
26+
27+
28+
129
# Returns the length of a expression tuple, or 1 if it is not an expression tuple (probably a Symbol/Numerical).
230
function tup_leng(ex::ExprValues)
331
(typeof(ex) == Expr && ex.head == :tuple) && (return length(ex.args))

src/graphs.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ References:
3434
- DOT language guide: http://www.graphviz.org/pdf/dotguide.pdf
3535
"""
3636

37-
# AST
38-
#####
37+
38+
### AST ###
3939

4040
abstract type Expression end
4141
abstract type Statement <: Expression end
@@ -123,8 +123,8 @@ Edge(path::Vector{String}, attrs::AbstractDict) = Edge(map(NodeID, path), attrs)
123123
Edge(path::Vector{String}; attrs...) = Edge(map(NodeID, path), attrs)
124124
Edge(path::Vararg{String}; attrs...) = Edge(map(NodeID, collect(path)), attrs)
125125

126-
# Bindings
127-
##########
126+
127+
### Bindings ###
128128

129129
""" Run a Graphviz program.
130130
@@ -165,8 +165,7 @@ function Base.show(io::IO, ::MIME"image/svg+xml", graph::Graph)
165165
run_graphviz(io, graph, format = "svg")
166166
end
167167

168-
# Pretty-print
169-
##############
168+
### Pretty-print ###
170169

171170
""" Pretty-print the Graphviz expression.
172171
"""
@@ -320,6 +319,9 @@ function modifystrcomp(strcomp::Vector{String})
320319
strcomp = "<" .* strcomp .* ">"
321320
end
322321

322+
323+
### Public-facing API ###
324+
323325
"""
324326
complexgraph(rn::ReactionSystem; complexdata=reactioncomplexes(rn))
325327

0 commit comments

Comments
 (0)