1
- # ## ReactionComplex structures ###
1
+ # ## ReactionComplex Structures ###
2
2
3
3
"""
4
4
$(TYPEDEF)
@@ -40,15 +40,24 @@ function ReactionComplex(speciesids::Vector{Int},
40
40
ReactionComplex {V} (speciesids, speciesstoichs)
41
41
end
42
42
43
+ # ## ReactionComplex Functions ###
44
+
45
+ # Equality.
43
46
function (== )(a:: ReactionComplex{V} , b:: ReactionComplex{V} ) where {V <: Integer }
44
47
(a. speciesids == b. speciesids) &&
45
48
(a. speciesstoichs == b. speciesstoichs)
46
49
end
50
+
51
+ # Hash.
47
52
function hash (rc:: ReactionComplex , h:: UInt )
48
53
Base. hash (rc. speciesids, Base. hash (rc. speciesstoichs, h))
49
54
end
55
+
56
+ # Size, length.
50
57
Base. size (rc:: ReactionComplex ) = size (rc. speciesids)
51
58
Base. length (rc:: ReactionComplex ) = length (rc. speciesids)
59
+
60
+ # Get/set index.
52
61
function Base. getindex (rc:: ReactionComplex , i... )
53
62
ReactionComplexElement (getindex (rc. speciesids, i... ), getindex (rc. speciesstoichs, i... ))
54
63
end
@@ -58,12 +67,15 @@ function Base.setindex!(rc::ReactionComplex, t::ReactionComplexElement, i...)
58
67
t. speciesstoich, i... );
59
68
rc)
60
69
end
70
+
71
+ # Is less, sorting related.
61
72
function Base. isless (a:: ReactionComplexElement , b:: ReactionComplexElement )
62
73
isless (a. speciesid, b. speciesid)
63
74
end
64
75
Base. Sort. defalg (:: ReactionComplex ) = Base. DEFAULT_UNSTABLE
65
76
66
- # ## NetworkProperties structure ###
77
+
78
+ # ## NetworkProperties Structure ###
67
79
68
80
# ! format: off
69
81
# Internal cache for various ReactionSystem calculated properties
@@ -90,6 +102,9 @@ Base.@kwdef mutable struct NetworkProperties{I <: Integer, V <: BasicSymbolic{Re
90
102
end
91
103
# ! format: on
92
104
105
+ # ## NetworkProperties Functions ###
106
+
107
+ # Show
93
108
function Base. show (io:: IO , nps:: NetworkProperties )
94
109
if (nps. conservationmat != = nothing )
95
110
println (io, " Conserved Equations: " )
@@ -98,13 +113,16 @@ function Base.show(io::IO, nps::NetworkProperties)
98
113
end
99
114
end
100
115
116
+ # Is empty.
101
117
Base. isempty (nps:: NetworkProperties ) = getfield (nps, :isempty )
102
118
119
+ # Setproperty.
103
120
function Base. setproperty! (nps:: NetworkProperties , sym:: Symbol , x)
104
121
(sym != = :isempty ) && setfield! (nps, :isempty , false )
105
122
setfield! (nps, sym, x)
106
123
end
107
124
125
+ # Resets computed properties.
108
126
function reset! (nps:: NetworkProperties{I, V} ) where {I, V}
109
127
nps. isempty && return
110
128
nps. netstoichmat = Matrix {Int} (undef, 0 , 0 )
@@ -265,30 +283,8 @@ struct ReactionSystem{V <: NetworkProperties} <:
265
283
end
266
284
end
267
285
268
- function get_speciestype (iv, unknowns, systems)
269
- T = Nothing
270
- ! isempty (unknowns) && (T = typeof (first (unknowns)))
271
-
272
- if ! isempty (systems)
273
- for sys in Iterators. filter (s -> s isa ReactionSystem, systems)
274
- sts = MT. unknowns (sys)
275
- if ! isempty (sts)
276
- T = typeof (first (sts))
277
- break
278
- end
279
- end
280
- end
281
-
282
- if T <: Nothing
283
- @variables A ($ iv)
284
- T = typeof (MT. unwrap (A))
285
- end
286
-
287
- T
288
- end
289
-
290
- eqsortby (eq:: CatalystEqType ) = eq isa Reaction ? 1 : 2
291
-
286
+ # Four-argument constructor. Permits additional inputs as optional arguments.
287
+ # Calls the full constructor.
292
288
function ReactionSystem (eqs, iv, unknowns, ps;
293
289
observed = Equation[],
294
290
systems = [],
@@ -383,32 +379,41 @@ function ReactionSystem(eqs, iv, unknowns, ps;
383
379
ccallbacks, dcallbacks, metadata; checks = checks)
384
380
end
385
381
386
- function ReactionSystem (rxs:: Vector , iv = Catalyst. DEFAULT_IV; kwargs... )
387
- make_ReactionSystem_internal (rxs, iv, Vector {Num} (), Vector {Num} (); kwargs... )
388
- end
382
+ # Used to sort the reaction/equation vector as reactions first, equations second.
383
+ eqsortby (eq:: CatalystEqType ) = eq isa Reaction ? 1 : 2
389
384
390
- # search the symbolic expression for parameters or unknowns
391
- # and save in ps and us respectively. vars is used to cache results
392
- function findvars! (ps, us, exprtosearch, ivs, vars)
393
- MT. get_variables! (vars, exprtosearch)
394
- for var in vars
395
- (var ∈ ivs) && continue
396
- if MT. isparameter (var)
397
- push! (ps, var)
398
- else
399
- push! (us, var)
385
+ # Figures out a type.
386
+ function get_speciestype (iv, unknowns, systems)
387
+ T = Nothing
388
+ ! isempty (unknowns) && (T = typeof (first (unknowns)))
389
+
390
+ if ! isempty (systems)
391
+ for sys in Iterators. filter (s -> s isa ReactionSystem, systems)
392
+ sts = MT. unknowns (sys)
393
+ if ! isempty (sts)
394
+ T = typeof (first (sts))
395
+ break
396
+ end
400
397
end
401
398
end
402
- empty! (vars)
399
+
400
+ if T <: Nothing
401
+ @variables A ($ iv)
402
+ T = typeof (MT. unwrap (A))
403
+ end
404
+
405
+ T
403
406
end
404
- # Special dispatch for equations, applied `findvars!` to left-hand and right-hand sides.
405
- function findvars! (ps, us, eq_to_search:: Equation , ivs, vars)
406
- findvars! (ps, us, eq_to_search. lhs, ivs, vars)
407
- findvars! (ps, us, eq_to_search. rhs, ivs, vars)
407
+
408
+ # Two-argument constructor (reactions/equations and time variable).
409
+ # Calls the `make_ReactionSystem_internal`, which in turn calls the four-argument constructor.
410
+ function ReactionSystem (rxs:: Vector , iv = Catalyst. DEFAULT_IV; kwargs... )
411
+ make_ReactionSystem_internal (rxs, iv, Vector {Num} (), Vector {Num} (); kwargs... )
408
412
end
409
- # Special dispatch for Vectors (applies it to each vector element).
410
- function findvars! (ps, us, exprs_to_search:: Vector , ivs, vars)
411
- foreach (exprtosearch -> findvars! (ps, us, exprtosearch, ivs, vars), exprs_to_search)
413
+
414
+ # One-argument constructor. Creates an emtoy `ReactionSystem` from a time independent variable only.
415
+ function ReactionSystem (iv; kwargs... )
416
+ ReactionSystem (Reaction[], iv, [], []; kwargs... )
412
417
end
413
418
414
419
# Called internally (whether DSL-based or programmtic model creation is used).
@@ -488,8 +493,28 @@ function make_ReactionSystem_internal(rxs_and_eqs::Vector, iv, us_in, ps_in; spa
488
493
ReactionSystem (fulleqs, t, usv, psv; spatial_ivs, continuous_events, discrete_events, observed, kwargs... )
489
494
end
490
495
491
- function ReactionSystem (iv; kwargs... )
492
- ReactionSystem (Reaction[], iv, [], []; kwargs... )
496
+ # search the symbolic expression for parameters or unknowns
497
+ # and save in ps and us respectively. vars is used to cache results
498
+ function findvars! (ps, us, exprtosearch, ivs, vars)
499
+ MT. get_variables! (vars, exprtosearch)
500
+ for var in vars
501
+ (var ∈ ivs) && continue
502
+ if MT. isparameter (var)
503
+ push! (ps, var)
504
+ else
505
+ push! (us, var)
506
+ end
507
+ end
508
+ empty! (vars)
509
+ end
510
+ # Special dispatch for equations, applied `findvars!` to left-hand and right-hand sides.
511
+ function findvars! (ps, us, eq_to_search:: Equation , ivs, vars)
512
+ findvars! (ps, us, eq_to_search. lhs, ivs, vars)
513
+ findvars! (ps, us, eq_to_search. rhs, ivs, vars)
514
+ end
515
+ # Special dispatch for Vectors (applies it to each vector element).
516
+ function findvars! (ps, us, exprs_to_search:: Vector , ivs, vars)
517
+ foreach (exprtosearch -> findvars! (ps, us, exprtosearch, ivs, vars), exprs_to_search)
493
518
end
494
519
495
520
# Loops through all events in an supplied event vector, adding all unknowns and parameters found in
@@ -504,8 +529,6 @@ function find_event_vars!(ps, us, event, ivs, vars)
504
529
findvars! (ps, us, event[2 ], ivs, vars)
505
530
end
506
531
507
-
508
-
509
532
# ## Basic Functions ###
510
533
511
534
# ## ModelingToolkit Inherited Accessors ###
0 commit comments