@@ -141,6 +141,70 @@ function reset!(nps::NetworkProperties{I, V}) where {I, V}
141
141
end
142
142
143
143
144
+ # ## ReactionSystem Constructor Functions ###
145
+
146
+ # Used to sort the reaction/equation vector as reactions first, equations second.
147
+ eqsortby (eq:: CatalystEqType ) = eq isa Reaction ? 1 : 2
148
+
149
+ # Figures out a type.
150
+ function get_speciestype (iv, unknowns, systems)
151
+ T = Nothing
152
+ ! isempty (unknowns) && (T = typeof (first (unknowns)))
153
+
154
+ if ! isempty (systems)
155
+ for sys in Iterators. filter (s -> s isa ReactionSystem, systems)
156
+ sts = MT. unknowns (sys)
157
+ if ! isempty (sts)
158
+ T = typeof (first (sts))
159
+ break
160
+ end
161
+ end
162
+ end
163
+
164
+ if T <: Nothing
165
+ @variables A ($ iv)
166
+ T = typeof (MT. unwrap (A))
167
+ end
168
+
169
+ T
170
+ end
171
+
172
+ # search the symbolic expression for parameters or unknowns
173
+ # and save in ps and us respectively. vars is used to cache results
174
+ function findvars! (ps, us, exprtosearch, ivs, vars)
175
+ MT. get_variables! (vars, exprtosearch)
176
+ for var in vars
177
+ (var ∈ ivs) && continue
178
+ if MT. isparameter (var)
179
+ push! (ps, var)
180
+ else
181
+ push! (us, var)
182
+ end
183
+ end
184
+ empty! (vars)
185
+ end
186
+ # Special dispatch for equations, applied `findvars!` to left-hand and right-hand sides.
187
+ function findvars! (ps, us, eq_to_search:: Equation , ivs, vars)
188
+ findvars! (ps, us, eq_to_search. lhs, ivs, vars)
189
+ findvars! (ps, us, eq_to_search. rhs, ivs, vars)
190
+ end
191
+ # Special dispatch for Vectors (applies it to each vector element).
192
+ function findvars! (ps, us, exprs_to_search:: Vector , ivs, vars)
193
+ foreach (exprtosearch -> findvars! (ps, us, exprtosearch, ivs, vars), exprs_to_search)
194
+ end
195
+
196
+ # Loops through all events in an supplied event vector, adding all unknowns and parameters found in
197
+ # its condition and affect functions to their respective vectors (`ps` and `us`).
198
+ function find_event_vars! (ps, us, events:: Vector , ivs, vars)
199
+ foreach (event -> find_event_vars! (ps, us, event, ivs, vars), events)
200
+ end
201
+ # For a single event, adds quantitites from its condition and affect expression(s) to `ps` and `us`.
202
+ # Applies `findvars!` to the event's condition (`event[1])` and affec (`event[2]`).
203
+ function find_event_vars! (ps, us, event, ivs, vars)
204
+ findvars! (ps, us, event[1 ], ivs, vars)
205
+ findvars! (ps, us, event[2 ], ivs, vars)
206
+ end
207
+
144
208
# ## ReactionSystem Structure ###
145
209
146
210
"""
@@ -370,32 +434,6 @@ function ReactionSystem(eqs, iv, unknowns, ps;
370
434
ccallbacks, dcallbacks, metadata; checks = checks)
371
435
end
372
436
373
- # Used to sort the reaction/equation vector as reactions first, equations second.
374
- eqsortby (eq:: CatalystEqType ) = eq isa Reaction ? 1 : 2
375
-
376
- # Figures out a type.
377
- function get_speciestype (iv, unknowns, systems)
378
- T = Nothing
379
- ! isempty (unknowns) && (T = typeof (first (unknowns)))
380
-
381
- if ! isempty (systems)
382
- for sys in Iterators. filter (s -> s isa ReactionSystem, systems)
383
- sts = MT. unknowns (sys)
384
- if ! isempty (sts)
385
- T = typeof (first (sts))
386
- break
387
- end
388
- end
389
- end
390
-
391
- if T <: Nothing
392
- @variables A ($ iv)
393
- T = typeof (MT. unwrap (A))
394
- end
395
-
396
- T
397
- end
398
-
399
437
# Two-argument constructor (reactions/equations and time variable).
400
438
# Calls the `make_ReactionSystem_internal`, which in turn calls the four-argument constructor.
401
439
function ReactionSystem (rxs:: Vector , iv = Catalyst. DEFAULT_IV; kwargs... )
@@ -484,42 +522,6 @@ function make_ReactionSystem_internal(rxs_and_eqs::Vector, iv, us_in, ps_in; spa
484
522
ReactionSystem (fulleqs, t, usv, psv; spatial_ivs, continuous_events, discrete_events, observed, kwargs... )
485
523
end
486
524
487
- # search the symbolic expression for parameters or unknowns
488
- # and save in ps and us respectively. vars is used to cache results
489
- function findvars! (ps, us, exprtosearch, ivs, vars)
490
- MT. get_variables! (vars, exprtosearch)
491
- for var in vars
492
- (var ∈ ivs) && continue
493
- if MT. isparameter (var)
494
- push! (ps, var)
495
- else
496
- push! (us, var)
497
- end
498
- end
499
- empty! (vars)
500
- end
501
- # Special dispatch for equations, applied `findvars!` to left-hand and right-hand sides.
502
- function findvars! (ps, us, eq_to_search:: Equation , ivs, vars)
503
- findvars! (ps, us, eq_to_search. lhs, ivs, vars)
504
- findvars! (ps, us, eq_to_search. rhs, ivs, vars)
505
- end
506
- # Special dispatch for Vectors (applies it to each vector element).
507
- function findvars! (ps, us, exprs_to_search:: Vector , ivs, vars)
508
- foreach (exprtosearch -> findvars! (ps, us, exprtosearch, ivs, vars), exprs_to_search)
509
- end
510
-
511
- # Loops through all events in an supplied event vector, adding all unknowns and parameters found in
512
- # its condition and affect functions to their respective vectors (`ps` and `us`).
513
- function find_event_vars! (ps, us, events:: Vector , ivs, vars)
514
- foreach (event -> find_event_vars! (ps, us, event, ivs, vars), events)
515
- end
516
- # For a single event, adds quantitites from its condition and affect expression(s) to `ps` and `us`.
517
- # Applies `findvars!` to the event's condition (`event[1])` and affec (`event[2]`).
518
- function find_event_vars! (ps, us, event, ivs, vars)
519
- findvars! (ps, us, event[1 ], ivs, vars)
520
- findvars! (ps, us, event[2 ], ivs, vars)
521
- end
522
-
523
525
524
526
# ## Base Functions ###
525
527
@@ -974,6 +976,9 @@ function prodstoichmat(rn::ReactionSystem; sparse = false)
974
976
sparse ? prodstoichmat (SparseMatrixCSC{T, Int}, rn) : prodstoichmat (Matrix{T}, rn)
975
977
end
976
978
979
+ # Used in `netstoichmat` function.
980
+ netstoichtype (:: Vector{Pair{S, T}} ) where {S, T} = T
981
+
977
982
"""
978
983
netstoichmat(rn, sparse=false)
979
984
@@ -1044,11 +1049,18 @@ function netstoichmat(rn::ReactionSystem; sparse = false)
1044
1049
nsmat
1045
1050
end
1046
1051
1047
- netstoichtype (:: Vector{Pair{S, T}} ) where {S, T} = T
1048
-
1049
-
1050
1052
# ## General `ReactionSystem`-specific Functions ###
1051
1053
1054
+ # used in the `__unpacksys` function.
1055
+ function __unpacksys (rn)
1056
+ ex = :(begin end )
1057
+ for key in keys (get_var_to_name (rn))
1058
+ var = MT. getproperty (rn, key, namespace = false )
1059
+ push! (ex. args, :($ key = $ var))
1060
+ end
1061
+ ex
1062
+ end
1063
+
1052
1064
"""
1053
1065
@unpacksys sys::ModelingToolkit.AbstractSystem
1054
1066
@@ -1080,15 +1092,6 @@ macro unpacksys(rn)
1080
1092
end
1081
1093
end
1082
1094
1083
- function __unpacksys (rn)
1084
- ex = :(begin end )
1085
- for key in keys (get_var_to_name (rn))
1086
- var = MT. getproperty (rn, key, namespace = false )
1087
- push! (ex. args, :($ key = $ var))
1088
- end
1089
- ex
1090
- end
1091
-
1092
1095
"""
1093
1096
setdefaults!(rn, newdefs)
1094
1097
@@ -1263,6 +1266,21 @@ function make_empty_network(; iv = DEFAULT_IV, name = gensym(:ReactionSystem))
1263
1266
ReactionSystem (Reaction[], iv, [], []; name = name)
1264
1267
end
1265
1268
1269
+ # A helper function used in `flatten`.
1270
+ function getsubsystypes (sys)
1271
+ typeset = Set {Type} ()
1272
+ getsubsystypes! (typeset, sys)
1273
+ typeset
1274
+ end
1275
+
1276
+ function getsubsystypes! (typeset:: Set{Type} , sys:: T ) where {T <: MT.AbstractSystem }
1277
+ push! (typeset, T)
1278
+ for subsys in get_systems (sys)
1279
+ getsubsystypes! (typeset, subsys)
1280
+ end
1281
+ typeset
1282
+ end
1283
+
1266
1284
"""
1267
1285
Catalyst.flatten(rs::ReactionSystem)
1268
1286
@@ -1357,22 +1375,6 @@ function ModelingToolkit.extend(sys::MT.AbstractSystem, rs::ReactionSystem;
1357
1375
discrete_events)
1358
1376
end
1359
1377
1360
- # A helper function.
1361
- function getsubsystypes (sys)
1362
- typeset = Set {Type} ()
1363
- getsubsystypes! (typeset, sys)
1364
- typeset
1365
- end
1366
-
1367
- function getsubsystypes! (typeset:: Set{Type} , sys:: T ) where {T <: MT.AbstractSystem }
1368
- push! (typeset, T)
1369
- for subsys in get_systems (sys)
1370
- getsubsystypes! (typeset, subsys)
1371
- end
1372
- typeset
1373
- end
1374
-
1375
-
1376
1378
# ## Units Handling ###
1377
1379
1378
1380
"""
0 commit comments