Skip to content

Commit 7159cd6

Browse files
committed
save progress
1 parent 0e68e2a commit 7159cd6

File tree

1 file changed

+74
-51
lines changed

1 file changed

+74
-51
lines changed

src/reactionsystem_structure.jl

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### ReactionComplex structures ###
1+
### ReactionComplex Structures ###
22

33
"""
44
$(TYPEDEF)
@@ -40,15 +40,24 @@ function ReactionComplex(speciesids::Vector{Int},
4040
ReactionComplex{V}(speciesids, speciesstoichs)
4141
end
4242

43+
### ReactionComplex Functions ###
44+
45+
# Equality.
4346
function (==)(a::ReactionComplex{V}, b::ReactionComplex{V}) where {V <: Integer}
4447
(a.speciesids == b.speciesids) &&
4548
(a.speciesstoichs == b.speciesstoichs)
4649
end
50+
51+
# Hash.
4752
function hash(rc::ReactionComplex, h::UInt)
4853
Base.hash(rc.speciesids, Base.hash(rc.speciesstoichs, h))
4954
end
55+
56+
# Size, length.
5057
Base.size(rc::ReactionComplex) = size(rc.speciesids)
5158
Base.length(rc::ReactionComplex) = length(rc.speciesids)
59+
60+
# Get/set index.
5261
function Base.getindex(rc::ReactionComplex, i...)
5362
ReactionComplexElement(getindex(rc.speciesids, i...), getindex(rc.speciesstoichs, i...))
5463
end
@@ -58,12 +67,15 @@ function Base.setindex!(rc::ReactionComplex, t::ReactionComplexElement, i...)
5867
t.speciesstoich, i...);
5968
rc)
6069
end
70+
71+
# Is less, sorting related.
6172
function Base.isless(a::ReactionComplexElement, b::ReactionComplexElement)
6273
isless(a.speciesid, b.speciesid)
6374
end
6475
Base.Sort.defalg(::ReactionComplex) = Base.DEFAULT_UNSTABLE
6576

66-
### NetworkProperties structure ###
77+
78+
### NetworkProperties Structure ###
6779

6880
#! format: off
6981
# Internal cache for various ReactionSystem calculated properties
@@ -90,6 +102,9 @@ Base.@kwdef mutable struct NetworkProperties{I <: Integer, V <: BasicSymbolic{Re
90102
end
91103
#! format: on
92104

105+
### NetworkProperties Functions ###
106+
107+
# Show
93108
function Base.show(io::IO, nps::NetworkProperties)
94109
if (nps.conservationmat !== nothing)
95110
println(io, "Conserved Equations: ")
@@ -98,13 +113,16 @@ function Base.show(io::IO, nps::NetworkProperties)
98113
end
99114
end
100115

116+
# Is empty.
101117
Base.isempty(nps::NetworkProperties) = getfield(nps, :isempty)
102118

119+
# Setproperty.
103120
function Base.setproperty!(nps::NetworkProperties, sym::Symbol, x)
104121
(sym !== :isempty) && setfield!(nps, :isempty, false)
105122
setfield!(nps, sym, x)
106123
end
107124

125+
# Resets computed properties.
108126
function reset!(nps::NetworkProperties{I, V}) where {I, V}
109127
nps.isempty && return
110128
nps.netstoichmat = Matrix{Int}(undef, 0, 0)
@@ -265,30 +283,8 @@ struct ReactionSystem{V <: NetworkProperties} <:
265283
end
266284
end
267285

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.
292288
function ReactionSystem(eqs, iv, unknowns, ps;
293289
observed = Equation[],
294290
systems = [],
@@ -383,32 +379,41 @@ function ReactionSystem(eqs, iv, unknowns, ps;
383379
ccallbacks, dcallbacks, metadata; checks = checks)
384380
end
385381

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
389384

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
400397
end
401398
end
402-
empty!(vars)
399+
400+
if T <: Nothing
401+
@variables A($iv)
402+
T = typeof(MT.unwrap(A))
403+
end
404+
405+
T
403406
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...)
408412
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...)
412417
end
413418

414419
# 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
488493
ReactionSystem(fulleqs, t, usv, psv; spatial_ivs, continuous_events, discrete_events, observed, kwargs...)
489494
end
490495

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)
493518
end
494519

495520
# 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)
504529
findvars!(ps, us, event[2], ivs, vars)
505530
end
506531

507-
508-
509532
### Basic Functions ###
510533

511534
### ModelingToolkit Inherited Accessors ###

0 commit comments

Comments
 (0)