You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Temporary vector. For parameters which values are identical across the lattice, at some point these have to be converted of a length num_verts vector. To avoid re-allocation they are written to this vector."""
14
14
work_vert_ps::Vector{S}
15
15
"""For each parameter in vert_ps, its value is a vector with length either num_verts or 1. To know whenever a parameter's value need expanding to the work_vert_ps array, its length needs checking. This check is done once, and the value stored to this array. This field (specifically) is an enumerate over that array."""
"""A vector of pairs, with a value for each species with transportation. The first value is the species index (in the species(::ReactionSystem) vector), and the second is a vector with its transport rate values. If the transport rate is uniform (across all edges), that value is the only value in the vector. Else, there is one value for each edge in the lattice."""
18
18
transport_rates::Vector{Pair{Int64, Vector{S}}}
19
19
"""A matrix, NxM, where N is the number of species with transportation and M the number of vertexes. Each value is the total rate at which that species leaves that vertex (e.g. for a species with constant diffusion rate D, in a vertex with n neighbours, this value is n*D)."""
20
20
leaving_rates::Matrix{S}
21
21
"""An (enumerate'ed) iterator over all the edges of the lattice."""
"""The edge parameters used to create the spatial ODEProblem. Currently unused, but will be needed to support changing these (e.g. due to events). Contain one vector for each edge parameter (length one if uniform, else one value for each edge)."""
24
+
edge_ps::Vector{Vector{T}}
23
25
24
-
functionLatticeTransportODEf(ofunc::R, vert_ps::Vector{Vector{S}}, transport_rates::Vector{Pair{Int64, Vector{S}}}, lrs::LatticeReactionSystem) where {R,S}
26
+
functionLatticeTransportODEf(ofunc::R, vert_ps::Vector{Vector{S}}, transport_rates::Vector{Pair{Int64, Vector{S}}}, edge_ps::Vector{Vector{T}}, lrs::LatticeReactionSystem) where {R,S,T}
# Functor structure containing the information for the forcing function of a spatial ODE with spatial movement on a lattice.
42
-
struct LatticeTransportODEjac{R,S,T}
44
+
struct LatticeTransportODEjac{Q,R,S,T}
43
45
"""The ODEFunction of the (non-spatial) reaction system which generated this function."""
44
-
ofunc::R
46
+
ofunc::Q
45
47
"""The number of vertices."""
46
48
num_verts::Int64
47
49
"""The number of species."""
48
50
num_species::Int64
49
51
"""The values of the parameters which values are tied to vertexes."""
50
-
vert_ps::Vector{Vector{S}}
52
+
vert_ps::Vector{Vector{R}}
51
53
"""Temporary vector. For parameters which values are identical across the lattice, at some point these have to be converted of a length(num_verts) vector. To avoid re-allocation they are written to this vector."""
52
-
work_vert_ps::Vector{S}
54
+
work_vert_ps::Vector{R}
53
55
"""For each parameter in vert_ps, it either have length num_verts or 1. To know whenever a parameter's value need expanding to the work_vert_ps array, its length needs checking. This check is done once, and the value stored to this array. This field (specifically) is an enumerate over that array."""
"""The transport rates. Can be a dense matrix (for non-sparse) or as the "nzval" field if sparse."""
58
-
jac_values::T
60
+
jac_transport::S
61
+
"""The edge parameters used to create the spatial ODEProblem. Currently unused, but will be needed to support changing these (e.g. due to events). Contain one vector for each edge parameter (length one if uniform, else one value for each edge)."""
62
+
edge_ps::Vector{Vector{T}}
59
63
60
-
functionLatticeTransportODEjac(ofunc::R, vert_ps::Vector{Vector{S}}, lrs::LatticeReactionSystem, jac_prototype::Union{Nothing, SparseMatrixCSC{Float64, Int64}}, sparse::Bool) where {R,S}
64
+
functionLatticeTransportODEjac(ofunc::R, vert_ps::Vector{Vector{S}}, lrs::LatticeReactionSystem, jac_transport::Union{Nothing, SparseMatrixCSC{Float64, Int64}}, edge_ps::Vector{Vector{T}}, sparse::Bool) where {R,S,T}
is_transport_system(lrs) ||error("Currently lattice ODE simulations are only supported when all spatial reactions are TransportReactions.")
75
81
76
82
# Converts potential symmaps to varmaps (parameter conversion is more involved since the vertex and edge parameters may be given in a tuple, or in a common vector).
@@ -85,26 +91,49 @@ function DiffEqBase.ODEProblem(lrs::LatticeReactionSystem, u0_in, tspan,
edge_ps::Vector{Vector{T}}, use_jac::Bool, sparse::Bool) where {T}
95
-
# Prepares (non-spatial) ODE functions and list of spatially moving species and their rates.
96
-
ofunc =ODEFunction(convert(ODESystem, lrs.rs); jac = use_jac, sparse =false) # Creates the (non-spatial) ODEFunction corresponding to the (non-spatial) reaction network.
97
-
ofunc_sparse =ODEFunction(convert(ODESystem, lrs.rs); jac = use_jac, sparse =true) # Creates the same function, but sparse. Could insert so this is only computed for sparse cases.
98
-
transport_rates_speciesmap =compute_all_transport_rates(vert_ps, edge_ps, lrs) # Creates a map (Vector{Pair}), mapping each species that is transported to a vector with its transportation rate. If the rate is uniform across all edges, the vector will be length 1 (with this value), else there will be a separate value for each edge.
for spat_rates in transport_rates_speciesmap] # Remakes "transport_rates_speciesmap". Rates are identical, but the species are represented as their index (in the species(::ReactionSystem) vector). In "transport_rates_speciesmap" they instead were Symbolics. Pair{Int64, Vector{T}}[] is required in case vector is empty (otherwise it becomes Any[], causing type error later).
101
-
102
-
f =LatticeTransportODEf(ofunc, vert_ps, transport_rates, lrs)
lrs; set_nonzero = use_jac) :nothing# Computes the Jacobian prototype (nothing if `jac=false`).
106
-
jac = use_jac ?LatticeTransportODEjac(ofunc, vert_ps, lrs, jac_prototype, sparse) :nothing# (Potentially) Creates a functor for the ODE Jacobian function (incorporating spatial and non-spatial reactions).
107
-
returnODEFunction(f; jac = jac, jac_prototype = (sparse ? jac_prototype :nothing)) # Creates the ODEFunction used in the ODEProblem.
# Builds a jacobian prototype. If requested, populate it with the Jacobian's (constant) values as well.
@@ -120,7 +149,7 @@ function build_jac_prototype(ns_jac_prototype::SparseMatrixCSC{Float64, Int64},
120
149
ns_j_idxs = ns_jac_prototype_idxs[2]
121
150
122
151
# List the indexes of all non-zero Jacobian terms.
123
-
non_spat_terms = [[get_index(vert, s_i, lrs.num_species), get_index(vert, s_j, lrs.num_species)] for vert in1:(lrs.num_verts) for (s_i, s_j) inzip(ns_i_idxs,ns_j_idxs)] # Indexes of elements due to non-spatial dynamics.
152
+
non_spat_terms = [[get_index(vert, s_i, lrs.num_species), get_index(vert, s_j, lrs.num_species)] for vert in1:(lrs.num_verts) for (s_i, s_j) inzip(ns_i_idxs,ns_j_idxs)] # Indexes of elements due to non-spatial dynamics.
124
153
trans_only_leaving_terms = [[get_index(e.src, s_idx, lrs.num_species), get_index(e.src, s_idx, lrs.num_species)] for e inedges(lrs.lattice) for s_idx in trans_only_species] # Indexes due to terms for a species leaves its current vertex (but does not have non-spatial dynamics). If the non-spatial Jacobian is fully dense, these would already be accounted for.
125
154
trans_arriving_terms = [[get_index(e.src, s_idx, lrs.num_species), get_index(e.dst, s_idx, lrs.num_species)] for e inedges(lrs.lattice) for s_idx in trans_species] # Indexes due to terms for species arriving into a new vertex.
@@ -130,7 +159,7 @@ function build_jac_prototype(ns_jac_prototype::SparseMatrixCSC{Float64, Int64},
130
159
131
160
# Set element values.
132
161
if set_nonzero
133
-
for (s, rates) in trans_rates, (e_idx, e) inenumerate(edges(lrs.lattice))# Loops through all species with transportation and all edges along which the can be transported.
162
+
for (s, rates) in trans_rates, (e_idx, e) inenumerate(edges(lrs.lattice))
134
163
jac_prototype[get_index(e.src, s, lrs.num_species), get_index(e.src, s, lrs.num_species)] -=get_component_value(rates, e_idx) # Term due to species leaving source vertex.
135
164
jac_prototype[get_index(e.src, s, lrs.num_species), get_index(e.dst, s, lrs.num_species)] +=get_component_value(rates, e_idx) # Term due to species arriving to destination vertex.
136
165
end
@@ -147,7 +176,7 @@ function (f_func::LatticeTransportODEf)(du, u, p, t)
0 commit comments