Skip to content

Commit 4f371e6

Browse files
authored
Merge pull request #1133 from SciML/lrs_fix_for_new_MTK
Update spatial models for new MTK release
2 parents 2cd8c75 + 3441992 commit 4f371e6

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

src/spatial_reaction_systems/lattice_reaction_systems.jl

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const GridLattice{N, T} = Union{Array{Bool, N}, CartesianGridRej{N, T}}
88
"""
99
$(TYPEDEF)
1010
11-
A representation of a spatial system of chemical reactions on a discrete (lattice) space.
11+
A representation of a spatial system of chemical reactions on a discrete (lattice) space.
1212
1313
# Fields
1414
$(FIELDS)
@@ -20,7 +20,7 @@ Arguments:
2020
to which the non-spatial model is expanded.
2121
2222
Keyword Arguments:
23-
- `diagonal_connections = false`: Only relevant for Cartesian and masked lattices. If `true`,
23+
- `diagonal_connections = false`: Only relevant for Cartesian and masked lattices. If `true`,
2424
diagonally adjacent compartments are considered adjacent, and spatial reactions in between these
2525
are possible.
2626
@@ -82,12 +82,12 @@ struct LatticeReactionSystem{Q, R, S, T} <: MT.AbstractTimeDependentSystem
8282
"""
8383
parameters::Vector{Any}
8484
"""
85-
Parameters which values are tied to vertices,
85+
Parameters which values are tied to vertices,
8686
e.g. that possibly could have unique values at each vertex of the system.
8787
"""
8888
vertex_parameters::Vector{Any}
8989
"""
90-
Parameters whose values are tied to edges (adjacencies),
90+
Parameters whose values are tied to edges (adjacencies),
9191
e.g. that possibly could have unique values at each edge of the system.
9292
"""
9393
edge_parameters::Vector{Any}
@@ -108,23 +108,23 @@ struct LatticeReactionSystem{Q, R, S, T} <: MT.AbstractTimeDependentSystem
108108
if !iscomplete(rs)
109109
throw(ArgumentError("A non-complete `ReactionSystem` was used as input, this is not permitted."))
110110
end
111-
if !isempty(MT.get_systems(rs))
112-
throw(ArgumentError("A non-flattened (hierarchical) `ReactionSystem` was used as input. `LatticeReactionSystem`s can only be based on non-hierarchical `ReactionSystem`s."))
113-
end
114111
if length(reactions(rs)) != length(equations(rs))
115112
throw(ArgumentError("The `ReactionSystem` used as input contain equations (in addition to reactions). This is not permitted."))
116113
end
117114
if length(species(rs)) != length(unknowns(rs))
118115
throw(ArgumentError("The `ReactionSystem` used as input contain variable unknowns (in addition to species unknowns). This is not permitted (the input `ReactionSystem` must contain species unknowns only)."))
119116
end
120117
if !isempty(MT.continuous_events(rs)) || !isempty(MT.discrete_events(rs))
121-
throw(ArgumentError("The `ReactionSystem` used as input to `LatticeReactionSystem contain events. These will be ignored in any simulations based on the created `LatticeReactionSystem`."))
118+
throw(ArgumentError("The `ReactionSystem` used as input to `LatticeReactionSystem` contain events. These will be ignored in any simulations based on the created `LatticeReactionSystem`."))
119+
end
120+
if !isnothing(MT.get_parent(rs)) && !isempty(MT.get_systems(MT.get_parent(rs)))
121+
@warn "The `ReactionSystem` used as input to `LatticeReactionSystem` was originally created as a hierarchical model. While this won't necessarily result in errors, it has not been well-tested, and is not recommended."
122122
end
123123
if !isempty(observed(rs))
124-
@warn "The `ReactionSystem` used as input to `LatticeReactionSystem contain observables. It will not be possible to access these from the created `LatticeReactionSystem`."
124+
@warn "The `ReactionSystem` used as input to `LatticeReactionSystem` contain observables. It will not be possible to access these from the created `LatticeReactionSystem`."
125125
end
126126

127-
# Computes the species which are parts of spatial reactions. Also counts the total number of
127+
# Computes the species which are parts of spatial reactions. Also counts the total number of
128128
# species types.
129129
if isempty(spatial_reactions)
130130
spat_species = Vector{BasicSymbolic{Real}}[]
@@ -171,7 +171,7 @@ function LatticeReactionSystem(rs, srs, lattice::SimpleGraph)
171171
LatticeReactionSystem(rs, srs, DiGraph(lattice))
172172
end
173173

174-
# Creates a LatticeReactionSystem from a CartesianGrid lattice (cartesian grid) or a Boolean Array
174+
# Creates a LatticeReactionSystem from a CartesianGrid lattice (cartesian grid) or a Boolean Array
175175
# lattice (masked grid). These two are quite similar, so much code can be reused in a single interface.
176176
function LatticeReactionSystem(rs, srs, lattice::GridLattice{N, T};
177177
diagonal_connections = false) where {N, T}
@@ -226,7 +226,7 @@ function count_edges(grid::CartesianGridRej{N, T};
226226
return num_edges
227227
end
228228

229-
# Counts and edges on a masked grid. Does so by looping through all the vertices of the grid,
229+
# Counts and edges on a masked grid. Does so by looping through all the vertices of the grid,
230230
# finding their neighbours, and updating the edge count accordingly.
231231
function count_edges(grid::Array{Bool, N}; diagonal_connections = false) where {N}
232232
g_size = grid_size(grid)
@@ -239,7 +239,7 @@ function count_edges(grid::Array{Bool, N}; diagonal_connections = false) where {
239239
return num_edges
240240
end
241241

242-
# For a (1d, 2d, or 3d) (Cartesian or masked) grid, returns a vector and an array, permitting the
242+
# For a (1d, 2d, or 3d) (Cartesian or masked) grid, returns a vector and an array, permitting the
243243
# conversion between a vertex's flat (scalar) and grid indices. E.g. for a 2d grid, if grid point (3,2)
244244
# corresponds to the fifth vertex, then `flat_to_grid_idx[5] = (3,2)` and `grid_to_flat_idx[3,2] = 5`.
245245
function get_index_converters(grid::GridLattice{N, T}, num_verts) where {N, T}
@@ -335,7 +335,7 @@ num_verts(lrs::LatticeReactionSystem) = getfield(lrs, :num_verts)
335335
"""
336336
num_edges(lrs::LatticeReactionSystem)
337337
338-
Returns the number of edges (i.e. connections between vertices) in the lattice stored in a
338+
Returns the number of edges (i.e. connections between vertices) in the lattice stored in a
339339
`LatticeReactionSystem`.
340340
"""
341341
num_edges(lrs::LatticeReactionSystem) = getfield(lrs, :num_edges)
@@ -392,7 +392,7 @@ end
392392
"""
393393
has_cartesian_lattice(lrs::LatticeReactionSystem)
394394
395-
Returns `true` if `lrs` was created using a cartesian grid lattice (e.g. created via `CartesianGrid(5,5)`).
395+
Returns `true` if `lrs` was created using a cartesian grid lattice (e.g. created via `CartesianGrid(5,5)`).
396396
Otherwise, returns `false`.
397397
"""
398398
has_cartesian_lattice(lrs::LatticeReactionSystem) = lattice(lrs) isa
@@ -401,7 +401,7 @@ has_cartesian_lattice(lrs::LatticeReactionSystem) = lattice(lrs) isa
401401
"""
402402
has_masked_lattice(lrs::LatticeReactionSystem)
403403
404-
Returns `true` if `lrs` was created using a masked grid lattice (e.g. created via `[true true; true false]`).
404+
Returns `true` if `lrs` was created using a masked grid lattice (e.g. created via `[true true; true false]`).
405405
Otherwise, returns `false`.
406406
"""
407407
has_masked_lattice(lrs::LatticeReactionSystem) = lattice(lrs) isa Array{Bool, N} where {N}
@@ -418,15 +418,15 @@ end
418418
"""
419419
has_graph_lattice(lrs::LatticeReactionSystem)
420420
421-
Returns `true` if `lrs` was created using a graph grid lattice (e.g. created via `path_graph(5)`).
421+
Returns `true` if `lrs` was created using a graph grid lattice (e.g. created via `path_graph(5)`).
422422
Otherwise, returns `false`.
423423
"""
424424
has_graph_lattice(lrs::LatticeReactionSystem) = lattice(lrs) isa SimpleDiGraph
425425

426426
"""
427427
grid_size(lrs::LatticeReactionSystem)
428428
429-
Returns the size of `lrs`'s lattice (only if it is a cartesian or masked grid lattice).
429+
Returns the size of `lrs`'s lattice (only if it is a cartesian or masked grid lattice).
430430
E.g. for a lattice `CartesianGrid(4,6)`, `(4,6)` is returned.
431431
"""
432432
grid_size(lrs::LatticeReactionSystem) = grid_size(lattice(lrs))
@@ -439,7 +439,7 @@ end
439439
"""
440440
grid_dims(lrs::LatticeReactionSystem)
441441
442-
Returns the number of dimensions of `lrs`'s lattice (only if it is a cartesian or masked grid lattice).
442+
Returns the number of dimensions of `lrs`'s lattice (only if it is a cartesian or masked grid lattice).
443443
The output is either `1`, `2`, or `3`.
444444
"""
445445
grid_dims(lrs::LatticeReactionSystem) = grid_dims(lattice(lrs))
@@ -506,19 +506,19 @@ end
506506
"""
507507
make_edge_p_values(lrs::LatticeReactionSystem, make_edge_p_value::Function)
508508
509-
Generates edge parameter values for a lattice reaction system. Only work for (Cartesian or masked)
510-
grid lattices (without diagonal adjacencies).
509+
Generates edge parameter values for a lattice reaction system. Only work for (Cartesian or masked)
510+
grid lattices (without diagonal adjacencies).
511511
512512
Input:
513513
- `lrs`: The lattice reaction system for which values should be generated.
514514
- `make_edge_p_value`: a function describing a rule for generating the edge parameter values.
515515
516516
Output:
517-
- `ep_vals`: A sparse matrix of size (num_verts,num_verts) (where num_verts is the number of
518-
vertices in `lrs`). Here, `eps[i,j]` is filled only if there is an edge going from vertex i to
517+
- `ep_vals`: A sparse matrix of size (num_verts,num_verts) (where num_verts is the number of
518+
vertices in `lrs`). Here, `eps[i,j]` is filled only if there is an edge going from vertex i to
519519
vertex j. The value of `eps[i,j]` is determined by `make_edge_p_value`.
520520
521-
Here, `make_edge_p_value` should take two arguments, `src_vert` and `dst_vert`, which correspond to
521+
Here, `make_edge_p_value` should take two arguments, `src_vert` and `dst_vert`, which correspond to
522522
the grid indices of an edge's source and destination vertices, respectively. It outputs a single value,
523523
which is the value assigned to that edge.
524524
@@ -568,34 +568,34 @@ function make_edge_p_values(lrs::LatticeReactionSystem, make_edge_p_value::Funct
568568
end
569569

570570
"""
571-
make_directed_edge_values(lrs::LatticeReactionSystem, x_vals::Tuple{T,T}, y_vals::Tuple{T,T} = (undef,undef),
571+
make_directed_edge_values(lrs::LatticeReactionSystem, x_vals::Tuple{T,T}, y_vals::Tuple{T,T} = (undef,undef),
572572
z_vals::Tuple{T,T} = (undef,undef)) where {T}
573573
574-
Generates edge parameter values for a lattice reaction system. Only work for (Cartesian or masked)
575-
grid lattices (without diagonal adjacencies). Each dimension (x, and possibly y and z), and
574+
Generates edge parameter values for a lattice reaction system. Only work for (Cartesian or masked)
575+
grid lattices (without diagonal adjacencies). Each dimension (x, and possibly y and z), and
576576
direction has assigned its own constant edge parameter value.
577577
578578
Input:
579579
- `lrs`: The lattice reaction system for which values should be generated.
580-
- `x_vals::Tuple{T,T}`: The values in the increasing (from a lower x index to a higher x index)
580+
- `x_vals::Tuple{T,T}`: The values in the increasing (from a lower x index to a higher x index)
581581
and decreasing (from a higher x index to a lower x index) direction along the x dimension.
582-
- `y_vals::Tuple{T,T}`: The values in the increasing and decreasing direction along the y dimension.
582+
- `y_vals::Tuple{T,T}`: The values in the increasing and decreasing direction along the y dimension.
583583
Should only be used for 2 and 3-dimensional grids.
584-
- `z_vals::Tuple{T,T}`: The values in the increasing and decreasing direction along the z dimension.
584+
- `z_vals::Tuple{T,T}`: The values in the increasing and decreasing direction along the z dimension.
585585
Should only be used for 3-dimensional grids.
586586
587587
Output:
588-
- `ep_vals`: A sparse matrix of size (num_verts,num_verts) (where num_verts is the number of
589-
vertices in `lrs`). Here, `eps[i,j]` is filled only if there is an edge going from vertex i to
590-
vertex j. The value of `eps[i,j]` is determined by the `x_vals`, `y_vals`, and `z_vals` Tuples,
588+
- `ep_vals`: A sparse matrix of size (num_verts,num_verts) (where num_verts is the number of
589+
vertices in `lrs`). Here, `eps[i,j]` is filled only if there is an edge going from vertex i to
590+
vertex j. The value of `eps[i,j]` is determined by the `x_vals`, `y_vals`, and `z_vals` Tuples,
591591
and vertices i and j's relative position in the grid.
592592
593-
It should be noted that two adjacent vertices will always be different in exactly a single dimension
593+
It should be noted that two adjacent vertices will always be different in exactly a single dimension
594594
(x, y, or z). The corresponding tuple determines which value is assigned.
595595
596596
Example:
597597
In the following example, we wish to have diffusion in the x dimension, but a constant flow from
598-
low y values to high y values (so not transportation from high to low y). We achieve it in the
598+
low y values to high y values (so not transportation from high to low y). We achieve it in the
599599
following manner:
600600
```julia
601601
using Catalyst

test/spatial_modelling/lattice_reaction_systems.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ end
248248
@test_logs (:warn, r"The `ReactionSystem` used as input to `LatticeReactionSystem contain observables. It *") match_mode=:any LatticeReactionSystem(rs4, [tr], short_path)
249249
end
250250

251-
# Tests for hierarchical input system.
251+
# Tests for hierarchical input system (should yield a warning).
252252
let
253253
t = default_t()
254254
@parameters d D
@@ -257,7 +257,7 @@ let
257257
@named rs1 = ReactionSystem(rxs, t)
258258
@named rs2 = ReactionSystem(rxs, t; systems = [rs1])
259259
rs2 = complete(rs2)
260-
@test_throws ArgumentError LatticeReactionSystem(rs2, [TransportReaction(D, X)], CartesianGrid((2,2)))
260+
@test_logs (:warn, r"The `ReactionSystem` used as input to `LatticeReactionSystem` was originally created as a hierarchical model. While *") match_mode=:any LatticeReactionSystem(rs2, [TransportReaction(D, X)], CartesianGrid((2,2)))
261261
end
262262

263263
# Tests for non-complete input `ReactionSystem`.

0 commit comments

Comments
 (0)