Skip to content

Commit 3aebc3d

Browse files
committed
move functions back
1 parent f125ee8 commit 3aebc3d

File tree

3 files changed

+127
-123
lines changed

3 files changed

+127
-123
lines changed

src/reaction_structure.jl

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ Test if a species is valid as a reactant (i.e. a species variable or a constant
6363
isvalidreactant(s) = MT.isparameter(s) ? isconstant(s) : (isspecies(s) && !isconstant(s))
6464

6565

66+
### Reaction Constructor Functions ###
67+
68+
# Checks if a metadata input has an entry :only_use_rate => true
69+
function metadata_only_use_rate_check(metadata)
70+
only_use_rate_idx = findfirst(:only_use_rate == entry[1] for entry in metadata)
71+
isnothing(only_use_rate_idx) && return false
72+
return Bool(metadata[only_use_rate_idx][2])
73+
end
74+
75+
# calculates the net stoichiometry of a reaction as a vector of pairs (sub,substoich)
76+
function get_netstoich(subs, prods, sstoich, pstoich)
77+
# stoichiometry as a Dictionary
78+
nsdict = Dict{Any, eltype(sstoich)}(sub => -sstoich[i] for (i, sub) in enumerate(subs))
79+
for (i, p) in enumerate(prods)
80+
coef = pstoich[i]
81+
@inbounds nsdict[p] = haskey(nsdict, p) ? nsdict[p] + coef : coef
82+
end
83+
84+
# stoichiometry as a vector
85+
[el for el in nsdict if !_iszero(el[2])]
86+
end
87+
88+
# Get the net stoichiometries' type.
89+
netstoich_stoichtype(::Vector{Pair{S, T}}) where {S, T} = T
90+
6691
### Reaction Structure ###
6792

6893
"""
@@ -214,29 +239,6 @@ function Reaction(rate, subs, prods; kwargs...)
214239
Reaction(rate, subs, prods, sstoich, pstoich; kwargs...)
215240
end
216241

217-
# Checks if a metadata input has an entry :only_use_rate => true
218-
function metadata_only_use_rate_check(metadata)
219-
only_use_rate_idx = findfirst(:only_use_rate == entry[1] for entry in metadata)
220-
isnothing(only_use_rate_idx) && return false
221-
return Bool(metadata[only_use_rate_idx][2])
222-
end
223-
224-
# calculates the net stoichiometry of a reaction as a vector of pairs (sub,substoich)
225-
function get_netstoich(subs, prods, sstoich, pstoich)
226-
# stoichiometry as a Dictionary
227-
nsdict = Dict{Any, eltype(sstoich)}(sub => -sstoich[i] for (i, sub) in enumerate(subs))
228-
for (i, p) in enumerate(prods)
229-
coef = pstoich[i]
230-
@inbounds nsdict[p] = haskey(nsdict, p) ? nsdict[p] + coef : coef
231-
end
232-
233-
# stoichiometry as a vector
234-
[el for el in nsdict if !_iszero(el[2])]
235-
end
236-
237-
# Get the net stoichiometries' type.
238-
netstoich_stoichtype(::Vector{Pair{S, T}}) where {S, T} = T
239-
240242
# Union type for `Reaction`s and `Equation`s.
241243
const CatalystEqType = Union{Reaction, Equation}
242244

src/reactionsystem_conversions.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,16 @@ function spatial_convert_err(rs::ReactionSystem, systype)
441441
isspatial(rs) && error("Conversion to $systype is not supported for spatial networks.")
442442
end
443443

444+
# Finds and differentials in an expression, and sets these to 0.
445+
function remove_diffs(expr)
446+
if Symbolics._occursin(Symbolics.is_derivative, expr)
447+
return Symbolics.replace(expr, diff_2_zero)
448+
else
449+
return expr
450+
end
451+
end
452+
diff_2_zero(expr) = (Symbolics.is_derivative(expr) ? 0.0 : expr)
453+
444454
COMPLETENESS_ERROR = "A ReactionSystem must be complete before it can be converted to other system types. A ReactionSystem can be marked as complete using the `complete` function."
445455

446456

@@ -531,16 +541,6 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
531541
kwargs...)
532542
end
533543

534-
# Finds and differentials in an expression, and sets these to 0.
535-
function remove_diffs(expr)
536-
if Symbolics._occursin(Symbolics.is_derivative, expr)
537-
return Symbolics.replace(expr, diff_2_zero)
538-
else
539-
return expr
540-
end
541-
end
542-
diff_2_zero(expr) = (Symbolics.is_derivative(expr) ? 0.0 : expr)
543-
544544
# Ideally, when `ReactionSystem`s are converted to `NonlinearSystem`s, any coupled ODEs should be
545545
# on the form D(X) ~ ..., where lhs is the time derivative w.r.t. a single variable, and the rhs
546546
# does not contain any differentials. If this is not teh case, we throw a warning to let the user

src/reactionsystem_structure.jl

Lines changed: 92 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,70 @@ function reset!(nps::NetworkProperties{I, V}) where {I, V}
141141
end
142142

143143

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+
144208
### ReactionSystem Structure ###
145209

146210
"""
@@ -370,32 +434,6 @@ function ReactionSystem(eqs, iv, unknowns, ps;
370434
ccallbacks, dcallbacks, metadata; checks = checks)
371435
end
372436

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-
399437
# Two-argument constructor (reactions/equations and time variable).
400438
# Calls the `make_ReactionSystem_internal`, which in turn calls the four-argument constructor.
401439
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
484522
ReactionSystem(fulleqs, t, usv, psv; spatial_ivs, continuous_events, discrete_events, observed, kwargs...)
485523
end
486524

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-
523525

524526
### Base Functions ###
525527

@@ -974,6 +976,9 @@ function prodstoichmat(rn::ReactionSystem; sparse = false)
974976
sparse ? prodstoichmat(SparseMatrixCSC{T, Int}, rn) : prodstoichmat(Matrix{T}, rn)
975977
end
976978

979+
# Used in `netstoichmat` function.
980+
netstoichtype(::Vector{Pair{S, T}}) where {S, T} = T
981+
977982
"""
978983
netstoichmat(rn, sparse=false)
979984
@@ -1044,11 +1049,18 @@ function netstoichmat(rn::ReactionSystem; sparse = false)
10441049
nsmat
10451050
end
10461051

1047-
netstoichtype(::Vector{Pair{S, T}}) where {S, T} = T
1048-
1049-
10501052
### General `ReactionSystem`-specific Functions ###
10511053

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+
10521064
"""
10531065
@unpacksys sys::ModelingToolkit.AbstractSystem
10541066
@@ -1080,15 +1092,6 @@ macro unpacksys(rn)
10801092
end
10811093
end
10821094

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-
10921095
"""
10931096
setdefaults!(rn, newdefs)
10941097
@@ -1263,6 +1266,21 @@ function make_empty_network(; iv = DEFAULT_IV, name = gensym(:ReactionSystem))
12631266
ReactionSystem(Reaction[], iv, [], []; name = name)
12641267
end
12651268

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+
12661284
"""
12671285
Catalyst.flatten(rs::ReactionSystem)
12681286
@@ -1357,22 +1375,6 @@ function ModelingToolkit.extend(sys::MT.AbstractSystem, rs::ReactionSystem;
13571375
discrete_events)
13581376
end
13591377

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-
13761378
### Units Handling ###
13771379

13781380
"""

0 commit comments

Comments
 (0)