@@ -65,6 +65,14 @@ bwd_arrows = Set{Symbol}([:<, :←, :↢, :↤, :⇽, :⟵, :⟻, :⥚, :⥞, :
65
65
double_arrows = Set {Symbol} ([:↔ , :⟷ , :⇄ , :⇆ , :⇌ , :⇋ , :⇔ , :⟺ ])
66
66
pure_rate_arrows = Set {Symbol} ([:⇐ , :⟽ , :⇒ , :⟾ , :⇔ , :⟺ ])
67
67
68
+ # unfortunately the following doesn't seem to work on 1.5, so supporting these
69
+ # operators seems to require us to only support 1.6 and up...
70
+ # @static if VERSION >= v"1.6.0"
71
+ # push!(bwd_arrows, :<--)
72
+ # push!(double_arrows, :<-->)
73
+ # end
74
+
75
+
68
76
# Declares symbols which may neither be used as parameters not varriables.
69
77
forbidden_symbols = [:t , :π , :pi , :ℯ , :im , :nothing , :∅ ]
70
78
@@ -78,26 +86,50 @@ network.
78
86
79
87
See the [Catalyst.jl for Reaction Models](@ref) documentation for details on
80
88
parameters to the macro.
89
+
90
+ Examples:
91
+ ```julia
92
+ # a basic SIR model, with name SIR
93
+ sir_model = @reaction_network SIR begin
94
+ c1, s + i --> 2i
95
+ c2, i --> r
96
+ end c1 c2
97
+
98
+ # a basic SIR model, with random generated name
99
+ sir_model = @reaction_network begin
100
+ c1, s + i --> 2i
101
+ c2, i --> r
102
+ end c1 c2
103
+
104
+ # an empty network with name empty
105
+ emptyrn = @reaction_network empty
106
+
107
+ # an empty network with random generated name
108
+ emptyrn = @reaction_network
109
+ ```
81
110
"""
111
+ macro reaction_network (name:: Symbol , ex:: Expr , parameters... )
112
+ make_reaction_system (MacroTools. striplines (ex), parameters; name= name)
113
+ end
114
+
82
115
macro reaction_network (ex:: Expr , parameters... )
83
116
make_reaction_system (MacroTools. striplines (ex), parameters)
84
117
end
85
118
86
- # ## Macros used for manipulating, and successively builing up, reaction systems. ###
87
-
88
- # Returns a empty network (with, or without, parameters declared)
89
- macro reaction_network (parameters... )
90
- ! isempty (intersect (forbidden_symbols,parameters)) && error (" The following symbol(s) are used as parameters: " * ((map (s -> " '" * string (s)* " ', " ,intersect (forbidden_symbols,parameters))... ))* " this is not permited." )
91
- return Expr (:block ,:(@parameters $ ((:t ,parameters... ). .. )),
119
+ # Returns a empty network (with, or without, a declared name)
120
+ macro reaction_network (name:: Symbol = gensym (:ReactionSystem ))
121
+ return Expr (:block ,:(@parameters t),
92
122
:(ReactionSystem (Reaction[],
93
123
t,
94
124
[],
95
- [$ (parameters ... ) ],
125
+ [],
96
126
Equation[],
97
- gensym ( :ReactionSystem ),
127
+ $ ( QuoteNode (name) ),
98
128
ReactionSystem[])))
99
129
end
100
130
131
+ # ## Macros used for manipulating, and successively builing up, reaction systems. ###
132
+
101
133
"""
102
134
@add_reactions
103
135
@@ -139,16 +171,15 @@ end
139
171
# ## Functions that process the input and rephrase it as a reaction system ###
140
172
141
173
# Takes the reactions, and rephrases it as a "ReactionSystem" call, as designated by the ModelingToolkit IR.
142
- function make_reaction_system (ex:: Expr , parameters)
174
+ function make_reaction_system (ex:: Expr , parameters; name = gensym ( :ReactionSystem ) )
143
175
reactions = get_reactions (ex)
144
176
reactants = get_reactants (reactions)
145
- ! isempty (intersect (forbidden_symbols,union (reactants,parameters))) && error (" The following symbol(s) are used as reactants or parameters: " * ((map (s -> " '" * string (s)* " ', " ,intersect (forbidden_symbols,union (reactants,parameters)))... ))* " this is not permited." )
146
-
147
- network_code = Expr (:block ,:(@parameters t),:(@variables ), :(ReactionSystem ([],t,[],[])))
177
+ ! isempty (intersect (forbidden_symbols,union (reactants,parameters))) && error (" The following symbol(s) are used as reactants or parameters: " * ((map (s -> " '" * string (s)* " ', " ,intersect (forbidden_symbols,union (reactants,parameters)))... ))* " this is not permited." )
178
+ network_code = Expr (:block ,:(@parameters t),:(@variables ), :(ReactionSystem ([],t,[],[]; name= $ (QuoteNode (name)))))
148
179
foreach (parameter-> push! (network_code. args[1 ]. args, parameter), parameters)
149
180
foreach (reactant -> push! (network_code. args[2 ]. args, Expr (:call ,reactant,:t )), reactants)
150
- foreach (parameter-> push! (network_code. args[3 ]. args[5 ]. args, parameter), parameters)
151
- foreach (reactant -> push! (network_code. args[3 ]. args[4 ]. args, reactant), reactants)
181
+ foreach (parameter-> push! (network_code. args[3 ]. args[6 ]. args, parameter), parameters)
182
+ foreach (reactant -> push! (network_code. args[3 ]. args[5 ]. args, reactant), reactants)
152
183
for reaction in reactions
153
184
subs_init = isempty (reaction. substrates) ? nothing : :([]); subs_stoich_init = deepcopy (subs_init)
154
185
prod_init = isempty (reaction. products) ? nothing : :([]); prod_stoich_init = deepcopy (prod_init)
@@ -161,7 +192,7 @@ function make_reaction_system(ex::Expr, parameters)
161
192
push! (reaction_func. args[4 ]. args, prod. reactant)
162
193
push! (reaction_func. args[6 ]. args, prod. stoichiometry)
163
194
end
164
- push! (network_code. args[3 ]. args[2 ]. args,reaction_func)
195
+ push! (network_code. args[3 ]. args[3 ]. args,reaction_func)
165
196
end
166
197
return network_code
167
198
end
0 commit comments