|
1 | 1 | ### Spatial ODE Functor Structures ###
|
2 | 2 |
|
| 3 | +# Functor with information for the forcing function of a spatial ODE with spatial movement on a lattice. |
| 4 | +struct LatticeTransportODEFunction{R,S,T} |
| 5 | + """The ODEFunction of the (non-spatial) ReactionSystem that generated this LatticeTransportODEf instance.""" |
| 6 | + ofunc::S |
| 7 | + """The number of vertices.""" |
| 8 | + num_verts::Int64 |
| 9 | + """The number of species.""" |
| 10 | + num_species::Int64 |
| 11 | + """The indexes of the vertex parameters in the parameter vector (`parameters(lrs)`).""" |
| 12 | + vert_p_idxs::Vector{Int64} |
| 13 | + """The indexes of the edge parameters in the parameter vector (`parameters(lrs)`).""" |
| 14 | + edge_p_idxs::Vector{Int64} |
| 15 | + """ |
| 16 | + Work in progress. |
| 17 | + """ |
| 18 | + mtk_ps |
| 19 | + """ |
| 20 | + Work in progress. |
| 21 | + """ |
| 22 | + p_setters |
| 23 | + """ |
| 24 | + The non-spatial `ReactionSystem` which was used to create the `LatticeReactionSystem` contain |
| 25 | + a set of parameters (either identical to, or a sub set of, `parameters(lrs)`). This vector |
| 26 | + contain the indexes of the non-spatial system's parameters in `parameters(lrs)`. These are |
| 27 | + required to manage the non-spatial ODEFunction in the spatial call. |
| 28 | + """ |
| 29 | + nonspatial_rs_p_idxs::Vector{Int64} |
| 30 | + """The values of the parameters that are tied to vertices.""" |
| 31 | + vert_ps::Vector{Vector{T}} |
| 32 | + """ |
| 33 | + Vector for storing temporary values. Repeatedly during simulations, we need to retrieve the |
| 34 | + parameter values in a certain vertex. However, since most parameters (likely) are uniform |
| 35 | + (and hence only have 1 value stored), we need to create a new vector each time we need to retrieve |
| 36 | + the parameter values in a new vertex. To avoid relocating these values repeatedly, we write them |
| 37 | + to this vector. |
| 38 | + """ |
| 39 | + work_ps::Vector{T} |
| 40 | + """ |
| 41 | + For each parameter in vert_ps, its value is a vector with a length of either num_verts or 1. |
| 42 | + To know whenever a parameter's value needs expanding to the work_ps array, its length needs checking. |
| 43 | + This check is done once, and the value is stored in this array. True means a uniform value. |
| 44 | + """ |
| 45 | + v_ps_idx_types::Vector{Bool} |
| 46 | + """ |
| 47 | + A vector that stores, for each species with transportation, its transportation rate(s). |
| 48 | + Each entry is a pair from (the index of) the transported species (in the `species(lrs)` vector) |
| 49 | + to its transportation rate (each species only has a single transportation rate, the sum of all |
| 50 | + its transportation reactions' rates). If the transportation rate is uniform across all edges, |
| 51 | + stores a single value (in a size (1,1) sparse matrix). Otherwise, stores these in a sparse matrix |
| 52 | + where value (i,j) is the species transportation rate from vertex i to vertex j. |
| 53 | + """ |
| 54 | + transport_rates::Vector{Pair{Int64,SparseMatrixCSC{T, Int64}}} |
| 55 | + """ |
| 56 | + For each transport rate in transport_rates, its value is a (sparse) matrix with a size of either |
| 57 | + (num_verts,num_verts) or (1,1). In the second case, the transportation rate is uniform across |
| 58 | + all edges. To avoid having to check which case holds for each transportation rate, we store the |
| 59 | + corresponding case in this value. `true` means that a species has a uniform transportation rate. |
| 60 | + """ |
| 61 | + t_rate_idx_types::Vector{Bool} |
| 62 | + """ |
| 63 | + A matrix, NxM, where N is the number of species with transportation and M is the number of vertices. |
| 64 | + Each value is the total rate at which that species leaves that vertex |
| 65 | + (e.g. for a species with constant diffusion rate D, in a vertex with n neighbours, this value is n*D). |
| 66 | + """ |
| 67 | + leaving_rates::Matrix{T} |
| 68 | + """An iterator over all the edges of the lattice.""" |
| 69 | + edge_iterator::Vector{Pair{Int64, Int64}} |
| 70 | + """Whether the Jacobian is sparse or not.""" |
| 71 | + sparse::Bool |
| 72 | + """The transport rates. This is a dense or sparse matrix (depending on what type of Jacobian is used).""" |
| 73 | + jac_transport::R |
| 74 | + |
| 75 | + function LatticeTransportODEFunction(ofunc::S, vert_ps::Vector{Pair{BasicSymbolic{Real},Vector{T}}}, edge_ps, |
| 76 | + transport_rates::Vector{Pair{Int64, SparseMatrixCSC{T, Int64}}}, |
| 77 | + lrs::LatticeReactionSystem) where {S,T} |
| 78 | + # Records which parameters and rates are uniform and which are not. |
| 79 | + v_ps_idx_types = map(vp -> length(vp[2]) == 1, vert_ps) |
| 80 | + t_rate_idx_types = map(tr -> size(tr[2]) == (1,1), transport_rates) |
| 81 | + |
| 82 | + # Computes the indexes of various parameters in in the `parameters(lrs)` vector. |
| 83 | + vert_p_idxs = subset_indexes_of(vertex_parameters(lrs), parameters(lrs)) |
| 84 | + edge_p_idxs = subset_indexes_of(edge_parameters(lrs), parameters(lrs)) |
| 85 | + nonspatial_rs_p_idxs = subset_indexes_of(parameters(reactionsystem(lrs)), parameters(lrs)) |
| 86 | + |
| 87 | + # WIP. |
| 88 | + nonspatial_osys = complete(convert(ODESystem, reactionsystem(lrs))) |
| 89 | + p_init = [p => Dict([vert_ps; edge_ps])[p][1] for p in parameters(nonspatial_osys)] |
| 90 | + mtk_ps = MT.MTKParameters(nonspatial_osys, p_init) |
| 91 | + p_setters = [MT.setp(nonspatial_osys, p) for p in parameters(nonspatial_osys)] |
| 92 | + |
| 93 | + # Computes the indexes of the vertex parameters in the vector of parameters. |
| 94 | + # Input `vert_ps` is a vector map taking each parameter symbolic to its value (potentially a |
| 95 | + # vector). This vector is already sorted according to the order of the parameters. Here, we extract |
| 96 | + # its values only and put them into `vert_ps`. |
| 97 | + vert_ps = [vp[2] for vp in vert_ps] |
| 98 | + |
| 99 | + # Computes the leaving rate matrix. |
| 100 | + leaving_rates = zeros(length(transport_rates), num_verts(lrs)) |
| 101 | + for (s_idx, tr_pair) in enumerate(transport_rates) |
| 102 | + for e in Catalyst.edge_iterator(lrs) |
| 103 | + # Updates the exit rate for species s_idx from vertex e.src. |
| 104 | + leaving_rates[s_idx, e[1]] += get_transport_rate(tr_pair[2], e, t_rate_idx_types[s_idx]) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + # Declares `work_ps` (used as storage during computation) and the edge iterator. |
| 109 | + work_ps = zeros(length(parameters(lrs))) |
| 110 | + edge_iterator = Catalyst.edge_iterator(lrs) |
| 111 | + new{S,T}(ofunc, num_verts(lrs), num_species(lrs), vert_p_idxs, edge_p_idxs, mtk_ps, p_setters, |
| 112 | + nonspatial_rs_p_idxs, vert_ps, work_ps, v_ps_idx_types, transport_rates, |
| 113 | + t_rate_idx_types, leaving_rates, edge_iterator) |
| 114 | + end |
| 115 | +end |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
3 | 128 | # Functor with information for the forcing function of a spatial ODE with spatial movement on a lattice.
|
4 | 129 | struct LatticeTransportODEf{S,T}
|
5 | 130 | """The ODEFunction of the (non-spatial) ReactionSystem that generated this LatticeTransportODEf instance."""
|
|
0 commit comments