@@ -8,6 +8,17 @@ struct LatticeTransportODEf{S,T}
8
8
num_verts:: Int64
9
9
""" The number of species."""
10
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
+ The non-spatial `ReactionSystem` which was used to create the `LatticeReactionSystem` contain
17
+ a set of parameters (either identical to, or a sub set of, `parameters(lrs)`). This vector
18
+ contain the indexes of the non-spatial system's parameters in `parameters(lrs)`. These are
19
+ required to manage the non-spatial ODEFunction in the spatial call.
20
+ """
21
+ nonspatial_rs_p_idxs:: Vector{Int64}
11
22
""" The values of the parameters that are tied to vertices."""
12
23
vert_ps:: Vector{Vector{T}}
13
24
"""
@@ -17,10 +28,10 @@ struct LatticeTransportODEf{S,T}
17
28
the parameter values in a new vertex. To avoid relocating these values repeatedly, we write them
18
29
to this vector.
19
30
"""
20
- work_vert_ps :: Vector{T}
31
+ work_ps :: Vector{T}
21
32
"""
22
33
For each parameter in vert_ps, its value is a vector with a length of either num_verts or 1.
23
- To know whenever a parameter's value needs expanding to the work_vert_ps array, its length needs checking.
34
+ To know whenever a parameter's value needs expanding to the work_ps array, its length needs checking.
24
35
This check is done once, and the value is stored in this array. True means a uniform value.
25
36
"""
26
37
v_ps_idx_types:: Vector{Bool}
@@ -55,7 +66,13 @@ struct LatticeTransportODEf{S,T}
55
66
# Records which parameters and rates are uniform and which are not.
56
67
v_ps_idx_types = map (vp -> length (vp[2 ]) == 1 , vert_ps)
57
68
t_rate_idx_types = map (tr -> size (tr[2 ]) == (1 ,1 ), transport_rates)
58
-
69
+
70
+ # Computes the indexes of various parameters in in the `parameters(lrs)` vector.
71
+ vert_p_idxs = subset_indexes_of (vertex_parameters (lrs), parameters (lrs))
72
+ edge_p_idxs = subset_indexes_of (edge_parameters (lrs), parameters (lrs))
73
+ nonspatial_rs_p_idxs = subset_indexes_of (parameters (reactionsystem (lrs)), parameters (lrs))
74
+
75
+ # Computes the indexes of the vertex parameters in the vector of parameters.
59
76
# Input `vert_ps` is a vector map taking each parameter symbolic to its value (potentially a
60
77
# vector). This vector is already sorted according to the order of the parameters. Here, we extract
61
78
# its values only and put them into `vert_ps`.
@@ -70,11 +87,12 @@ struct LatticeTransportODEf{S,T}
70
87
end
71
88
end
72
89
73
- # Declares `work_vert_ps ` (used as storage during computation) and the edge iterator.
74
- work_vert_ps = zeros (length (vert_ps ))
90
+ # Declares `work_ps ` (used as storage during computation) and the edge iterator.
91
+ work_ps = zeros (length (parameters (lrs) ))
75
92
edge_iterator = Catalyst. edge_iterator (lrs)
76
- new {S,T} (ofunc, num_verts (lrs), num_species (lrs), vert_ps, work_vert_ps,
77
- v_ps_idx_types, transport_rates, t_rate_idx_types, leaving_rates, edge_iterator)
93
+ new {S,T} (ofunc, num_verts (lrs), num_species (lrs), vert_p_idxs, edge_p_idxs,
94
+ nonspatial_rs_p_idxs, vert_ps, work_ps, v_ps_idx_types, transport_rates,
95
+ t_rate_idx_types, leaving_rates, edge_iterator)
78
96
end
79
97
end
80
98
@@ -86,6 +104,17 @@ struct LatticeTransportODEjac{R,S,T}
86
104
num_verts:: Int64
87
105
""" The number of species."""
88
106
num_species:: Int64
107
+ """ The indexes of the vertex parameters in the parameter vector (`parameters(lrs)`)."""
108
+ vert_p_idxs:: Vector{Int64}
109
+ """ The indexes of the edge parameters in the parameter vector (`parameters(lrs)`)."""
110
+ edge_p_idxs:: Vector{Int64}
111
+ """
112
+ The non-spatial `ReactionSystem` which was used to create the `LatticeReactionSystem` contain
113
+ a set of parameters (either identical to, or a sub set of, `parameters(lrs)`). This vector
114
+ contain the indexes of the non-spatial system's parameters in `parameters(lrs)`. These are
115
+ required to manage the non-spatial ODEFunction in the spatial call.
116
+ """
117
+ nonspatial_rs_p_idxs:: Vector{Int64}
89
118
""" The values of the parameters that are tied to vertices."""
90
119
vert_ps:: Vector{Vector{S}}
91
120
"""
@@ -95,10 +124,10 @@ struct LatticeTransportODEjac{R,S,T}
95
124
the parameter values in a new vertex. To avoid relocating these values repeatedly, we write them
96
125
to this vector.
97
126
"""
98
- work_vert_ps :: Vector{S}
127
+ work_ps :: Vector{S}
99
128
"""
100
129
For each parameter in vert_ps, its value is a vector with a length of either num_verts or 1.
101
- To know whenever a parameter's value needs expanding to the work_vert_ps array, its length needs checking.
130
+ To know whenever a parameter's value needs expanding to the work_ps array, its length needs checking.
102
131
This check is done once, and the value is stored in this array. True means a uniform value.
103
132
"""
104
133
v_ps_idx_types:: Vector{Bool}
@@ -110,18 +139,30 @@ struct LatticeTransportODEjac{R,S,T}
110
139
function LatticeTransportODEjac (ofunc:: R , vert_ps:: Vector{Pair{BasicSymbolic{Real},Vector{S}}} ,
111
140
jac_transport:: Union{Nothing, SparseMatrixCSC{Float64, Int64}} ,
112
141
lrs:: LatticeReactionSystem , sparse:: Bool ) where {R,S}
142
+
143
+ # Computes the indexes of various parameters in in the `parameters(lrs)` vector.
144
+ vert_p_idxs = subset_indexes_of (vertex_parameters (lrs), parameters (lrs))
145
+ edge_p_idxs = subset_indexes_of (edge_parameters (lrs), parameters (lrs))
146
+ nonspatial_rs_p_idxs = subset_indexes_of (parameters (reactionsystem (lrs)), parameters (lrs))
147
+
113
148
# Input `vert_ps` is a vector map taking each parameter symbolic to its value (potentially a
114
149
# vector). This vector is already sorted according to the order of the parameters. Here, we extract
115
150
# its values only and put them into `vert_ps`.
116
151
vert_ps = [vp[2 ] for vp in vert_ps]
117
152
118
- work_vert_ps = zeros (num_verts ( lrs))
153
+ work_ps = zeros (length ( parameters ( lrs) ))
119
154
v_ps_idx_types = map (vp -> length (vp) == 1 , vert_ps)
120
- new {R,S,typeof(jac_transport)} (ofunc, num_verts (lrs), num_species (lrs) , vert_ps,
121
- work_vert_ps, v_ps_idx_types, sparse, jac_transport)
155
+ new {R,S,typeof(jac_transport)} (ofunc, num_verts (lrs), num_species (lrs) , vert_p_idxs,
156
+ edge_p_idxs, nonspatial_rs_p_idxs, vert_ps,
157
+ work_ps, v_ps_idx_types, sparse, jac_transport)
122
158
end
123
159
end
124
160
161
+ # For each symbolic in syms1, returns a vector with their indexes in syms2.
162
+ function subset_indexes_of (syms1, syms2)
163
+ [findfirst (isequal (sym1, sym2) for sym2 in syms2) for sym1 in syms1]
164
+ end
165
+
125
166
# ## ODEProblem ###
126
167
127
168
# Creates an ODEProblem from a LatticeReactionSystem.
@@ -155,7 +196,8 @@ function DiffEqBase.ODEProblem(lrs::LatticeReactionSystem, u0_in, tspan,
155
196
combinatoric_ratelaws, remove_conserved, checks)
156
197
157
198
# Combines `vert_ps` and `edge_ps` to a single vector with values only (not a map). Creates ODEProblem.
158
- ps = [p[2 ] for p in [vert_ps; edge_ps]]
199
+ pval_dict = Dict ([vert_ps; edge_ps])
200
+ ps = [pval_dict[p] for p in parameters (lrs)]
159
201
return ODEProblem (ofun, u0, tspan, ps, args... ; kwargs... )
160
202
end
161
203
@@ -288,7 +330,7 @@ function (f_func::LatticeTransportODEf)(du, u, p, t)
288
330
update_work_vert_ps! (f_func, p, vert_i)
289
331
290
332
# Evaluate reaction contributions to du at vert_i.
291
- f_func. ofunc ((@view du[idxs]), (@view u[idxs]), f_func. work_vert_ps , t)
333
+ f_func. ofunc ((@view du[idxs]), (@view u[idxs]), nonspatial_ps ( f_func) , t)
292
334
end
293
335
294
336
# s_idx is the species index among transport species, s is the index among all species.
@@ -316,7 +358,7 @@ function (jac_func::LatticeTransportODEjac)(J, u, p, t)
316
358
for vert_i in 1 : (jac_func. num_verts)
317
359
idxs = get_indexes (vert_i, jac_func. num_species)
318
360
update_work_vert_ps! (jac_func, p, vert_i)
319
- jac_func. ofunc. jac ((@view J[idxs, idxs]), (@view u[idxs]), jac_func. work_vert_ps , t)
361
+ jac_func. ofunc. jac ((@view J[idxs, idxs]), (@view u[idxs]), nonspatial_ps ( jac_func) , t)
320
362
end
321
363
322
364
# Updates for the spatial reactions (adds the Jacobian values from the transportation reactions).
0 commit comments