|
| 1 | +### Lattice Reaction Network Structure ### |
| 2 | +# Describes a spatial reaction network over a graph. |
| 3 | +# Adding the "<: MT.AbstractTimeDependentSystem" part messes up show, disabling me from creating LRSs. |
| 4 | +struct LatticeReactionSystem{S,T} # <: MT.AbstractTimeDependentSystem |
| 5 | + # Input values. |
| 6 | + """The reaction system within each compartment.""" |
| 7 | + rs::ReactionSystem{S} |
| 8 | + """The spatial reactions defined between individual nodes.""" |
| 9 | + spatial_reactions::Vector{T} |
| 10 | + """The graph on which the lattice is defined.""" |
| 11 | + lattice::SimpleDiGraph{Int64} |
| 12 | + |
| 13 | + # Derived values. |
| 14 | + """The number of compartments.""" |
| 15 | + num_verts::Int64 |
| 16 | + """The number of edges.""" |
| 17 | + num_edges::Int64 |
| 18 | + """The number of species.""" |
| 19 | + num_species::Int64 |
| 20 | + """Whenever the initial input was a digraph.""" |
| 21 | + init_digraph::Bool |
| 22 | + """Species that may move spatially.""" |
| 23 | + spat_species::Vector{BasicSymbolic{Real}} |
| 24 | + """ |
| 25 | + All parameters related to the lattice reaction system |
| 26 | + (both with spatial and non-spatial effects). |
| 27 | + """ |
| 28 | + parameters::Vector{BasicSymbolic{Real}} |
| 29 | + """ |
| 30 | + Parameters which values are tied to vertexes (adjacencies), |
| 31 | + e.g. (possibly) have a unique value at each vertex of the system. |
| 32 | + """ |
| 33 | + vertex_parameters::Vector{BasicSymbolic{Real}} |
| 34 | + """ |
| 35 | + Parameters which values are tied to edges (adjacencies), |
| 36 | + e.g. (possibly) have a unique value at each edge of the system. |
| 37 | + """ |
| 38 | + edge_parameters::Vector{BasicSymbolic{Real}} |
| 39 | + |
| 40 | + function LatticeReactionSystem(rs::ReactionSystem{S}, spatial_reactions::Vector{T}, |
| 41 | + lattice::DiGraph; init_digraph = true) where {S, T} |
| 42 | + # There probably some better way to ascertain that T has that type. Not sure how. |
| 43 | + if !(T <: AbstractSpatialReaction) |
| 44 | + error("The second argument must be a vector of AbstractSpatialReaction subtypes.") |
| 45 | + end |
| 46 | + |
| 47 | + if isempty(spatial_reactions) |
| 48 | + spat_species = Vector{BasicSymbolic{Real}}[] |
| 49 | + else |
| 50 | + spat_species = unique(reduce(vcat, [spatial_species(sr) for sr in spatial_reactions])) |
| 51 | + end |
| 52 | + num_species = length(unique([species(rs); spat_species])) |
| 53 | + rs_edge_parameters = filter(isedgeparameter, parameters(rs)) |
| 54 | + if isempty(spatial_reactions) |
| 55 | + srs_edge_parameters = Vector{BasicSymbolic{Real}}[] |
| 56 | + else |
| 57 | + srs_edge_parameters = setdiff(reduce(vcat, [parameters(sr) for sr in spatial_reactions]), parameters(rs)) |
| 58 | + end |
| 59 | + edge_parameters = unique([rs_edge_parameters; srs_edge_parameters]) |
| 60 | + vertex_parameters = filter(!isedgeparameter, parameters(rs)) |
| 61 | + # Ensures the parameter order begins similarly to in the non-spatial ReactionSystem. |
| 62 | + ps = [parameters(rs); setdiff([edge_parameters; vertex_parameters], parameters(rs))] |
| 63 | + |
| 64 | + foreach(sr -> check_spatial_reaction_validity(rs, sr; edge_parameters=edge_parameters), spatial_reactions) |
| 65 | + return new{S,T}(rs, spatial_reactions, lattice, nv(lattice), ne(lattice), num_species, |
| 66 | + init_digraph, spat_species, ps, vertex_parameters, edge_parameters) |
| 67 | + end |
| 68 | +end |
| 69 | +function LatticeReactionSystem(rs, srs, lat::SimpleGraph) |
| 70 | + return LatticeReactionSystem(rs, srs, DiGraph(lat); init_digraph = false) |
| 71 | +end |
| 72 | + |
| 73 | +### Lattice ReactionSystem Getters ### |
| 74 | + |
| 75 | +# Get all species. |
| 76 | +species(lrs::LatticeReactionSystem) = unique([species(lrs.rs); lrs.spat_species]) |
| 77 | +# Get all species that may be transported. |
| 78 | +spatial_species(lrs::LatticeReactionSystem) = lrs.spat_species |
| 79 | + |
| 80 | +# Get all parameters. |
| 81 | +ModelingToolkit.parameters(lrs::LatticeReactionSystem) = lrs.parameters |
| 82 | +# Get all parameters which values are tied to vertexes (compartments). |
| 83 | +vertex_parameters(lrs::LatticeReactionSystem) = lrs.vertex_parameters |
| 84 | +# Get all parameters which values are tied to edges (adjacencies). |
| 85 | +edge_parameters(lrs::LatticeReactionSystem) = lrs.edge_parameters |
| 86 | + |
| 87 | +# Gets the lrs name (same as rs name). |
| 88 | +ModelingToolkit.nameof(lrs::LatticeReactionSystem) = nameof(lrs.rs) |
| 89 | + |
| 90 | +# Checks if a lattice reaction system is a pure (linear) transport reaction system. |
| 91 | +is_transport_system(lrs::LatticeReactionSystem) = all(sr -> sr isa TransportReaction, lrs.spatial_reactions) |
0 commit comments