Skip to content

Commit e40600f

Browse files
authored
Merge pull request #1 from augustinas1/main
Fix minor bugs
2 parents 4662123 + bf8d34e commit e40600f

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

src/build_rhs.jl

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ See also: [`build_rhs_header`](@ref), [`build_rhs`](@ref)
1010
"""
1111
function unpackparams(sys::FSPSystem, psym::Symbol)::Expr
1212
param_names = Expr(:tuple, map(par -> par.name, params(sys.rs))...)
13-
14-
quote
13+
14+
quote
1515
$(param_names) = ps
1616
end
1717
end
@@ -26,7 +26,7 @@ just unpacks parameters from `p`.
2626
See also: [`unpackparams`](@ref), [`build_rhs`](@ref)
2727
"""
2828
function build_rhs_header(sys::FSPSystem)::Expr
29-
quote
29+
quote
3030
ps::AbstractVector{Float64} = p
3131
$(unpackparams(sys, :ps))
3232
end
@@ -38,18 +38,18 @@ end
3838
build_rhs_firstpass(sys::FSPSystem, rfs)::Expr
3939
4040
Return code for the first pass of the RHS function, for the time-dependent
41-
FSP. Goes through all reactions and computes the negative part of the CME
42-
(probability flowing out of states). This is a simple array traversal and
41+
FSP. Goes through all reactions and computes the negative part of the CME
42+
(probability flowing out of states). This is a simple array traversal and
4343
can be done in one go for all reactions.
4444
4545
See also: [`build_rhs`](@ref)
4646
"""
4747
function build_rhs_firstpass(sys::FSPSystem)
48-
isempty(sys.rfs) && return quote end
49-
48+
isempty(sys.rfs) && return quote end
49+
5050
first_line = :(du[idx_in] = -u[idx_in] * $(sys.rfs[1].body))
5151
other_lines = (:(du[idx_in] -= u[idx_in] * $(rf.body)) for rf in sys.rfs[2:end])
52-
52+
5353
quote
5454
for idx_in in singleindices($(sys.ih), u)
5555
$first_line
@@ -66,55 +66,55 @@ end
6666
Return code for the second pass of the RHS function. Goes through
6767
all reactions and computes the positive part of the CME (probability
6868
flowing into states). This requires accessing `du` and `u` at different
69-
locations depending on the net stoichiometries. In order to reduce
69+
locations depending on the net stoichiometries. In order to reduce
7070
random memory access reactions are processed one by one.
7171
7272
See also: [`build_rhs`](@ref)
7373
"""
7474
function build_rhs_secondpass(sys::FSPSystem)::Expr
7575
isempty(sys.rfs) && return quote end
76-
76+
7777
S = netstoichmat(sys.rs)
7878
ret = Expr(:block)
79-
79+
8080
for (i, rf) in enumerate(sys.rfs)
8181
ex = quote
8282
for (idx_in, idx_out) in pairedindices($(sys.ih), u, $(CartesianIndex(S[i,:]...)))
8383
du[idx_out] += u[idx_in] * $(rf.body)
8484
end
8585
end
86-
86+
8787
append!(ret.args, ex.args)
8888
end
89-
89+
9090
return ret
9191
end
9292

9393
##
9494

95-
function build_rhs_ex(sys::FSPSystem; striplines::Bool=true)
95+
function build_rhs_ex(sys::FSPSystem; striplines::Bool=false)
9696
header = build_rhs_header(sys)
9797
first_pass = build_rhs_firstpass(sys)
9898
second_pass = build_rhs_secondpass(sys)
99-
99+
100100
body = Expr(:block, header, first_pass, second_pass)
101-
102-
ex = :((du, u, p, t) -> $(body))
103-
101+
102+
ex = :((du, u, p, t) -> $(body))
103+
104104
striplines && (ex = MacroTools.striplines(ex))
105-
105+
106106
ex = ex |> MacroTools.flatten |> MacroTools.prettify
107-
107+
108108
ex
109109
end
110110

111111
"""
112112
build_rhs(sys::FSPSystem)
113113
114114
Builds the function `f(du,u,p,t)` that defines the right-hand side of the CME
115-
for use with DifferentialEquations.jl.
115+
for use with DifferentialEquations.jl.
116116
"""
117-
function build_rhs(sys::FSPSystem)
117+
function build_rhs(sys::FSPSystem)
118118
@RuntimeGeneratedFunction(build_rhs_ex(sys; striplines=false))
119119
end
120120

@@ -126,14 +126,14 @@ end
126126
127127
Return an `ODEFunction` defining the right-hand side of the CME.
128128
129-
Creates an `ODEFunction` for use with `DifferentialEquations`. This is where most of
130-
the work in the package happens; for best performance it is suggested to build an `ODEFunction`
129+
Creates an `ODEFunction` for use with `DifferentialEquations`. This is where most of
130+
the work in the package happens; for best performance it is suggested to build an `ODEFunction`
131131
once for a given reaction system and reuse it instead of directly converting
132132
a reaction system to an `ODEProblem` (which implicitly calls this function).
133133
"""
134134
function Base.convert(::Type{ODEFunction}, sys::FSPSystem;
135135
combinatoric_ratelaw::Bool=true)::ODEFunction
136-
rhs = build_rhs(sys; striplines=false)
136+
rhs = build_rhs(sys)
137137
ODEFunction{true}(rhs)
138138
end
139139

src/indexhandlers.jl

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
"""
1+
"""
22
singleindices(idxhandler::AbstractIndexHandler, arr)
33
44
Returns all indices `I` in `arr`. Defaults to CartesianIndices, but can
5-
be overloaded for arbitrary index handlers.
5+
be overloaded for arbitrary index handlers.
66
"""
77
singleindices(::AbstractIndexHandler, arr::AbstractArray) = CartesianIndices(arr)
88
singleindices(::AbstractIndexHandler, arr::Tuple) = CartesianIndices(arr)
99

10-
"""
10+
"""
1111
pairedindices(idxhandler::AbstractIndexHandler, arr, shift::CartesianIndex)
1212
1313
Returns all pairs of indices `(I .- shift, I)` in `arr`.
@@ -17,16 +17,16 @@ function pairedindices end
1717
"""
1818
getsubstitutions(idxhandler::AbstractIndexHandler, rs::ReactionSystem; state_sym::Symbol)
1919
20-
Returns a dict of the form `S_i => f_i(state_sym)`, where each `f_i` is an expression
20+
Returns a dict of the form `S_i => f_i(state_sym)`, where each `f_i` is an expression
2121
for the abundance of species `S_i` in terms of the state variable `state_sym`.
2222
"""
2323
function getsubstitutions end
2424

2525
"""
2626
vec(idxhandler::AbstractIndexHandler, arr)
2727
28-
Converts the right-hand side defining the solution of the CME into a
29-
one-dimensional vector to which a matrix can be applied.
28+
Converts the right-hand side defining the solution of the CME into a
29+
one-dimensional vector to which a matrix can be applied.
3030
3131
See also: [`LinearIndices`](@ref Base.LinearIndices)
3232
"""
@@ -50,15 +50,15 @@ function LinearIndices end
5050
##
5151

5252

53-
"""
53+
"""
5454
struct NaiveIndexHandler <: AbstractIndexHandler
5555
offset::Int
5656
end
5757
5858
Basic index handler that stores the state of a system with
5959
`s` species in an `s`-dimensional array. The `offset` parameter
6060
denotes the offset by which the array is indexed (defaults to 1
61-
in Julia).
61+
in Julia).
6262
6363
This is the simplest index handler, but it will not be optimal
6464
if some states cannot be reached from the initial state, e.g.
@@ -79,24 +79,24 @@ NaiveIndexHandler() = NaiveIndexHandler(1)
7979
Base.vec(::NaiveIndexHandler, arr) = vec(arr)
8080
Base.LinearIndices(::NaiveIndexHandler, arr) = LinearIndices(arr)
8181

82-
function pairedindices(ih::NaiveIndexHandler, arr::AbstractArray{T,N},
82+
function pairedindices(ih::NaiveIndexHandler, arr::AbstractArray{T,N},
8383
shift::CartesianIndex{N}) where {T,N}
8484
pairedindices(ih, axes(arr), shift)
8585
end
8686

87-
function pairedindices(ih::NaiveIndexHandler, dims::NTuple{N,T},
87+
function pairedindices(ih::NaiveIndexHandler, dims::NTuple{N,T},
8888
shift::CartesianIndex{N}) where {N,T<:Number}
8989
pairedindices(ih, Base.OneTo.(dims), shift)
9090
end
9191

92-
function pairedindices(::NaiveIndexHandler, dims::NTuple{N,T},
92+
function pairedindices(::NaiveIndexHandler, dims::NTuple{N,T},
9393
shift::CartesianIndex{N}) where {N,T<:AbstractVector}
94-
ranges = tuple((UnitRange(max(first(ax), first(ax)+shift[i]),
95-
min(last(ax), last(ax)+shift[i]))
94+
ranges = tuple((UnitRange(max(first(ax), first(ax)+shift[i]),
95+
min(last(ax), last(ax)+shift[i]))
9696
for (i, ax) in enumerate(dims))...)
97-
97+
9898
ranges_shifted = tuple((rng .- shift[i] for (i, rng) in enumerate(ranges))...)
99-
99+
100100
zip(CartesianIndices(ranges_shifted), CartesianIndices(ranges))
101101
end
102102

@@ -111,13 +111,13 @@ end
111111

112112
##
113113

114-
"""
114+
"""
115115
struct ReducingIndexHandler <: AbstractIndexHandler
116116
117117
More efficient index handler that improves upon [`NaiveIndexHandler`](@ref)
118118
by eliminating variables whose abundances can be computed from other variables
119119
using conservation laws. Describes the system using a subset of the original
120-
species which can be obtained via [`reducedspecies`](@ref). Reduces the
120+
species which can be obtained via [`reducedspecies`](@ref). Reduces the
121121
dimensionality of the FSP by the number of conservation laws in the system.
122122
123123
Note: This feature is currently being moved into Catalyst.jl.
@@ -135,12 +135,12 @@ struct ReducingIndexHandler <: AbstractIndexHandler
135135
end
136136

137137
function ReducingIndexHandler(rs::ReactionSystem, offset::Int=1)
138-
cons_laws = conservationlaws(sys)
138+
cons_laws = conservationlaws(rs)
139139
specs_elided = elidedspecies(cons_laws)
140-
specs_red = [ i for i in 1:length(species(sys.rs)) if !(i in specs_elided) ]
141-
140+
specs_red = [ i for i in 1:length(species(rs)) if !(i in specs_elided) ]
141+
142142
cons_syms = [ gensym("c$i") for i in 1:size(cons_laws, 1) ]
143-
143+
144144
return ReducingIndexHandler(cons_laws, offset, specs_red, specs_elided, cons_syms)
145145
end
146146

@@ -170,7 +170,7 @@ elidedspecies(idxhandler::ReducingIndexHandler) = idxhandler.specs_elided
170170

171171
##
172172

173-
"""
173+
"""
174174
elidedspecies(cons_laws::AbstractMatrix{Int})::Vector
175175
176176
Returns a list of species ``[ s_1, ... ]`` which can be removed from the reaction system
@@ -186,9 +186,9 @@ function elidedspecies(cons_laws::AbstractMatrix{Int})::Vector{Int}
186186
idx = possible_specs[end]
187187
ret[i] = idx
188188
end
189-
189+
190190
@assert length(ret) == length(unique(ret))
191-
191+
192192
return ret
193193
end
194194

@@ -204,19 +204,19 @@ See also: [`getsubstitutions`](@ref)
204204
function elisions(idxhandler::ReducingIndexHandler, rs::ReactionSystem)
205205
ret = Dict()
206206
spec_syms = species(rs)
207-
207+
208208
for (i, spec) in enumerate(elidedspecies(idxhandler))
209209
sym = spec_syms[spec]
210210
cons_law = idxhandler.cons_laws[i,:]
211-
211+
212212
# Does this always work? What if some of the species on the RHS
213213
# also end up getting elided at some point?
214214
rhs = (Variable(idxhandler.cons_syms[i]) - sum(cons_law[j] * spec_syms[j] for j in 1:length(spec_syms) if j != spec))
215215
rhs /= cons_law[i]
216-
216+
217217
ret[sym] = rhs
218218
end
219-
219+
220220
ret
221221
end
222222

@@ -230,28 +230,28 @@ from the conserved quantities and the reduced species.
230230
"""
231231
function getsubstitutions(idxhandler::ReducingIndexHandler, rs::ReactionSystem; state_sym::Symbol)
232232
symbols = states(rs)
233-
234-
ret = Dict{Any,Any}(symbols[spec] => Term(Base.getindex, (state_sym, i)) - idxhandler.offset
233+
234+
ret = Dict{Any,Any}(symbols[spec] => Term(Base.getindex, (state_sym, i)) - idxhandler.offset
235235
for (i, spec) in enumerate(reducedspecies(idxhandler)))
236-
236+
237237
elision_dict = elisions(idxhandler, rs)
238238
for (spec, ex) in pairs(elision_dict)
239239
ret[spec] = substitute(ex, ret)
240240
end
241-
241+
242242
ret
243243
end
244244

245245
"""
246246
build_rhs_header(idxhandler::ReducingIndexHandler, sys::FSPSystem)::Expr
247247
248-
Assumes `p` is of the form `(params, cons::AbstractVector{Int})` where `params`
248+
Assumes `p` is of the form `(params, cons::AbstractVector{Int})` where `params`
249249
are the system parameters and `cons` the conserved quantities.
250250
"""
251251
function build_rhs_header(sys::FSPSystem{ReducingIndexHandler})::Expr
252252
cons_names = Expr(:tuple, sys.ih.cons_syms...)
253-
254-
quote
253+
254+
quote
255255
(ps, $(cons_names)) = p
256256
$(unpackparams(sys, :ps))
257257
end
@@ -263,13 +263,13 @@ end
263263
Similar to its `NaiveIndexHandler` variant, but converts the indices into indices into
264264
the reduced state space array.
265265
"""
266-
function pairedindices(idxhandler::ReducingIndexHandler, arr::AbstractArray{T,M},
266+
function pairedindices(idxhandler::ReducingIndexHandler, arr::AbstractArray{T,M},
267267
shift::CartesianIndex{N}) where {T,M,N}
268268
shift_red = CartesianIndex{M}(convert(Tuple, shift)[reducedspecies(idxhandler)]...)
269269
pairedindices(NaiveIndexHandler(idxhandler.offset), arr, shift_red)
270270
end
271271

272-
function pairedindices(idxhandler::ReducingIndexHandler, dims::NTuple{M},
272+
function pairedindices(idxhandler::ReducingIndexHandler, dims::NTuple{M},
273273
shift::CartesianIndex{N}) where {M,N}
274274
shift_red = CartesianIndex{M}(convert(Tuple, shift)[reducedspecies(idxhandler)]...)
275275
pairedindices(NaiveIndexHandler(idxhandler.offset), dims, shift_red)

0 commit comments

Comments
 (0)