Skip to content

Commit a98f4be

Browse files
committed
format
1 parent 28743ab commit a98f4be

File tree

7 files changed

+223
-174
lines changed

7 files changed

+223
-174
lines changed

src/Catalyst.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ include("spatial_reaction_systems/lattice_reaction_systems.jl")
176176
export LatticeReactionSystem
177177
export spatial_species, vertex_parameters, edge_parameters
178178
export CartesianGrid, CartesianGridReJ # (Implemented in JumpProcesses)
179-
export has_cartesian_lattice, has_masked_lattice, has_grid_lattice, has_graph_lattice, grid_dims, grid_size
179+
export has_cartesian_lattice, has_masked_lattice, has_grid_lattice, has_graph_lattice,
180+
grid_dims, grid_size
180181
export make_edge_p_values, make_directed_edge_values
181182

182183
# Specific spatial problem types.

src/spatial_reaction_systems/lattice_jump_systems.jl

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@ function DiffEqBase.DiscreteProblem(lrs::LatticeReactionSystem, u0_in, tspan,
99

1010
# Converts potential symmaps to varmaps.
1111
u0_in = symmap_to_varmap(lrs, u0_in)
12-
p_in = symmap_to_varmap(lrs, p_in)
12+
p_in = symmap_to_varmap(lrs, p_in)
1313

1414
# Converts u0 and p to their internal forms.
1515
# u0 is simply a vector with all the species' initial condition values across all vertices.
1616
# u0 is [spec 1 at vert 1, spec 2 at vert 1, ..., spec 1 at vert 2, ...].
17-
u0 = lattice_process_u0(u0_in, species(lrs), lrs)
17+
u0 = lattice_process_u0(u0_in, species(lrs), lrs)
1818
# vert_ps and `edge_ps` are vector maps, taking each parameter's Symbolics representation to its value(s).
1919
# vert_ps values are vectors. Here, index (i) is a parameter's value in vertex i.
2020
# edge_ps values are sparse matrices. Here, index (i,j) is a parameter's value in the edge from vertex i to vertex j.
2121
# Uniform vertex/edge parameters store only a single value (a length 1 vector, or size 1x1 sparse matrix).
22-
vert_ps, edge_ps = lattice_process_p(p_in, vertex_parameters(lrs), edge_parameters(lrs), lrs)
22+
vert_ps, edge_ps = lattice_process_p(p_in, vertex_parameters(lrs), edge_parameters(lrs),
23+
lrs)
2324

2425
# Returns a DiscreteProblem (which basically just stores the processed input).
2526
return DiscreteProblem(u0, tspan, [vert_ps; edge_ps], args...; kwargs...)
2627
end
2728

2829
# Builds a spatial JumpProblem from a DiscreteProblem containing a `LatticeReactionSystem`.
29-
function JumpProcesses.JumpProblem(lrs::LatticeReactionSystem, dprob, aggregator, args...; name = nameof(reactionsystem(lrs)),
30-
combinatoric_ratelaws = get_combinatoric_ratelaws(reactionsystem(lrs)), kwargs...)
30+
function JumpProcesses.JumpProblem(lrs::LatticeReactionSystem, dprob, aggregator,args...;
31+
combinatoric_ratelaws = get_combinatoric_ratelaws(reactionsystem(lrs)),
32+
name = nameof(reactionsystem(lrs)), kwargs...)
3133
# Error checks.
3234
if !isnothing(dprob.f.sys)
3335
throw(ArgumentError("Unexpected `DiscreteProblem` passed into `JumpProblem`. Was a `LatticeReactionSystem` used as input to the initial `DiscreteProblem`?"))
@@ -40,12 +42,13 @@ function JumpProcesses.JumpProblem(lrs::LatticeReactionSystem, dprob, aggregator
4042
# The non-spatial DiscreteProblem have a u0 matrix with entries for all combinations of species and vertexes.
4143
hopping_constants = make_hopping_constants(dprob, lrs)
4244
sma_jumps = make_spatial_majumps(dprob, lrs)
43-
non_spat_dprob = DiscreteProblem(reshape(dprob.u0, num_species(lrs), num_verts(lrs)), dprob.tspan, first.(dprob.p[1]))
45+
non_spat_dprob = DiscreteProblem(reshape(dprob.u0, num_species(lrs), num_verts(lrs)),
46+
dprob.tspan, first.(dprob.p[1]))
4447

4548
# Creates and returns a spatial JumpProblem (masked lattices are not supported by these).
4649
spatial_system = has_masked_lattice(lrs) ? get_lattice_graph(lrs) : lattice(lrs)
47-
return JumpProblem(non_spat_dprob, aggregator, sma_jumps;
48-
hopping_constants, spatial_system , name, kwargs...)
50+
return JumpProblem(non_spat_dprob, aggregator, sma_jumps;
51+
hopping_constants, spatial_system, name, kwargs...)
4952
end
5053

5154
# Creates the hopping constants from a discrete problem and a lattice reaction system.
@@ -59,18 +62,17 @@ function make_hopping_constants(dprob::DiscreteProblem, lrs::LatticeReactionSyst
5962
# Creates an array (of the same size as the hopping constant array) containing all edges.
6063
# First the array is a NxM matrix (number of species x number of vertices). Each element is a
6164
# vector containing all edges leading out from that vertex (sorted by destination index).
62-
edge_array = [Pair{Int64,Int64}[] for _1 in 1:num_species(lrs), _2 in 1:num_verts(lrs)]
65+
edge_array = [Pair{Int64, Int64}[] for _1 in 1:num_species(lrs), _2 in 1:num_verts(lrs)]
6366
for e in edge_iterator(lrs), s_idx in 1:num_species(lrs)
6467
push!(edge_array[s_idx, e[1]], e)
6568
end
6669
foreach(e_vec -> sort!(e_vec; by = e -> e[2]), edge_array)
6770

6871
# Creates the hopping constants array. It has the same shape as the edge array, but each
6972
# element is that species transportation rate along that edge
70-
hopping_constants = [
71-
[Catalyst.get_edge_value(all_diff_rates[s_idx], e) for e in edge_array[s_idx, src_idx]]
72-
for s_idx in 1:num_species(lrs), src_idx in 1:num_verts(lrs)
73-
]
73+
hopping_constants = [[Catalyst.get_edge_value(all_diff_rates[s_idx], e)
74+
for e in edge_array[s_idx, src_idx]]
75+
for s_idx in 1:num_species(lrs), src_idx in 1:num_verts(lrs)]
7476
return hopping_constants
7577
end
7678

@@ -79,16 +81,17 @@ end
7981
# Not sure if there is any form of performance improvement from that though. Likely not the case.
8082
function make_spatial_majumps(dprob, lrs::LatticeReactionSystem)
8183
# Creates a vector, storing which reactions have spatial components.
82-
is_spatials = [has_spatial_vertex_component(rx.rate, dprob.p)
83-
for rx in reactions(reactionsystem(lrs))]
84+
is_spatials = [has_spatial_vertex_component(rx.rate, dprob.p)
85+
for rx in reactions(reactionsystem(lrs))]
8486

8587
# Creates templates for the rates (uniform and spatial) and the stoichiometries.
8688
# We cannot fetch reactant_stoich and net_stoich from a (non-spatial) MassActionJump.
8789
# The reason is that we need to re-order the reactions so that uniform appears first, and spatial next.
88-
u_rates = Vector{Float64}(undef, length(reactions(reactionsystem(lrs))) - count(is_spatials))
90+
num_rxs = length(reactions(reactionsystem(lrs)))
91+
u_rates = Vector{Float64}(undef, num_rxs - count(is_spatials))
8992
s_rates = Matrix{Float64}(undef, count(is_spatials), num_verts(lrs))
90-
reactant_stoich = Vector{Vector{Pair{Int64, Int64}}}(undef, length(reactions(reactionsystem(lrs))))
91-
net_stoich = Vector{Vector{Pair{Int64, Int64}}}(undef, length(reactions(reactionsystem(lrs))))
93+
reactant_stoich = Vector{Vector{Pair{Int64, Int64}}}(undef, num_rxs)
94+
net_stoich = Vector{Vector{Pair{Int64, Int64}}}(undef, num_rxs)
9295

9396
# Loops through reactions with non-spatial rates, computes their rates and stoichiometries.
9497
cur_rx = 1
@@ -101,9 +104,10 @@ function make_spatial_majumps(dprob, lrs::LatticeReactionSystem)
101104
cur_rx += 1
102105
end
103106
# Loops through reactions with spatial rates, computes their rates and stoichiometries.
104-
for (is_spat, rx) in zip(is_spatials, reactions(reactionsystem(lrs)))
107+
for (is_spat, rx) in zip(is_spatials, reactions(reactionsystem(lrs)))
105108
is_spat || continue
106-
s_rates[cur_rx - length(u_rates), :] .= compute_vertex_value(rx.rate, lrs; ps = dprob.p)
109+
s_rates[cur_rx - length(u_rates), :] .= compute_vertex_value(rx.rate, lrs;
110+
ps = dprob.p)
107111
substoich_map = Pair.(rx.substrates, rx.substoich)
108112
reactant_stoich[cur_rx] = int_map(substoich_map, reactionsystem(lrs))
109113
net_stoich[cur_rx] = int_map(rx.netstoich, reactionsystem(lrs))

0 commit comments

Comments
 (0)