-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spatial Reaction Network Implementation #644
Changes from all commits
665a199
21f3f44
b2c7a3b
eeb7eb8
9ba2721
1fb738a
93b6926
4593a3a
283b46a
c07c2af
a2bb7cf
659a189
97d2cf0
ebb2d88
e0d68f1
a334a34
7f7bc88
585d4b4
4af6384
1b8cf80
4b66b59
1b8f09f
147c0d8
fbaa488
107858f
f284bc6
5f627f3
151c80a
22826d5
405f28c
6393a79
024134c
8728a19
bcf4efe
1fef525
e40e04c
24586ba
25abf21
e64e129
52bb6e4
5fb99be
abc4f4e
a474b1b
bf0395a
06cc919
7537bbe
aaf6c7f
8e221d9
32dd7e1
d97c4a1
9d034df
1115e72
35f0aeb
01769cf
f19c4c1
f3ad063
264965a
0ee1a53
186d09b
e2d555f
0b75daf
d851fb6
4647bf2
cf73270
591a9f5
acd760f
f0235d3
2e3a1cb
ae8c7b6
d0644ce
cbbd07d
1dd205c
59da638
3a21f44
fc48925
63af0ed
f9d2714
c302fd5
1dd9658
828a920
aa33707
0051a81
2bf70c6
17ba553
ea8a0f6
2258e30
49f0560
f087775
fb15625
f41ddef
7e7c3dd
31ea398
f712fc8
0392139
b93bcb1
5fe0ab2
2b5b6bf
d5d8873
0795917
520bf89
9ab6437
2e3e2d4
fc47179
617d89f
b3b229b
89f7780
4903bbe
5397007
be39cdd
499a748
e058f47
121f2c2
26634d3
2a81fca
4341962
621d664
efa91d0
1f9386d
820efb0
9f6c1af
5e220f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" | |
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a" | ||
Reexport = "189a3867-3050-52da-a836-e630ba90ab69" | ||
Requires = "ae029012-a4dd-5104-9daa-d747884805df" | ||
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a compat entry too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I just put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MTK uses RuntimeGeneratedFunctions = "0.5.9" so maybe go with that? |
||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b" | ||
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" | ||
|
@@ -40,6 +41,7 @@ ModelingToolkit = "8.66" | |
Parameters = "0.12" | ||
Reexport = "0.2, 1.0" | ||
Requires = "1.0" | ||
RuntimeGeneratedFunctions = "0.5.12" | ||
SymbolicUtils = "1.0.3" | ||
Symbolics = "5.0.3" | ||
Unitful = "1.12.4" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
### Lattice Reaction Network Structure ### | ||
# Describes a spatial reaction network over a graph. | ||
# Adding the "<: MT.AbstractTimeDependentSystem" part messes up show, disabling me from creating LRSs. | ||
struct LatticeReactionSystem{S,T} # <: MT.AbstractTimeDependentSystem | ||
# Input values. | ||
"""The reaction system within each compartment.""" | ||
rs::ReactionSystem{S} | ||
"""The spatial reactions defined between individual nodes.""" | ||
spatial_reactions::Vector{T} | ||
"""The graph on which the lattice is defined.""" | ||
lattice::SimpleDiGraph{Int64} | ||
|
||
# Derived values. | ||
"""The number of compartments.""" | ||
num_verts::Int64 | ||
"""The number of edges.""" | ||
num_edges::Int64 | ||
"""The number of species.""" | ||
num_species::Int64 | ||
"""Whenever the initial input was a digraph.""" | ||
init_digraph::Bool | ||
"""Species that may move spatially.""" | ||
spat_species::Vector{BasicSymbolic{Real}} | ||
""" | ||
All parameters related to the lattice reaction system | ||
(both with spatial and non-spatial effects). | ||
""" | ||
parameters::Vector{BasicSymbolic{Real}} | ||
""" | ||
Parameters which values are tied to vertexes (adjacencies), | ||
e.g. (possibly) have a unique value at each vertex of the system. | ||
""" | ||
vertex_parameters::Vector{BasicSymbolic{Real}} | ||
""" | ||
Parameters which values are tied to edges (adjacencies), | ||
e.g. (possibly) have a unique value at each edge of the system. | ||
""" | ||
edge_parameters::Vector{BasicSymbolic{Real}} | ||
|
||
function LatticeReactionSystem(rs::ReactionSystem{S}, spatial_reactions::Vector{T}, | ||
lattice::DiGraph; init_digraph = true) where {S, T} | ||
# There probably some better way to ascertain that T has that type. Not sure how. | ||
if !(T <: AbstractSpatialReaction) | ||
error("The second argument must be a vector of AbstractSpatialReaction subtypes.") | ||
end | ||
|
||
if isempty(spatial_reactions) | ||
spat_species = Vector{BasicSymbolic{Real}}[] | ||
else | ||
spat_species = unique(reduce(vcat, [spatial_species(sr) for sr in spatial_reactions])) | ||
end | ||
num_species = length(unique([species(rs); spat_species])) | ||
rs_edge_parameters = filter(isedgeparameter, parameters(rs)) | ||
if isempty(spatial_reactions) | ||
srs_edge_parameters = Vector{BasicSymbolic{Real}}[] | ||
else | ||
srs_edge_parameters = setdiff(reduce(vcat, [parameters(sr) for sr in spatial_reactions]), parameters(rs)) | ||
end | ||
edge_parameters = unique([rs_edge_parameters; srs_edge_parameters]) | ||
vertex_parameters = filter(!isedgeparameter, parameters(rs)) | ||
# Ensures the parameter order begins similarly to in the non-spatial ReactionSystem. | ||
ps = [parameters(rs); setdiff([edge_parameters; vertex_parameters], parameters(rs))] | ||
|
||
foreach(sr -> check_spatial_reaction_validity(rs, sr; edge_parameters=edge_parameters), spatial_reactions) | ||
return new{S,T}(rs, spatial_reactions, lattice, nv(lattice), ne(lattice), num_species, | ||
init_digraph, spat_species, ps, vertex_parameters, edge_parameters) | ||
end | ||
end | ||
function LatticeReactionSystem(rs, srs, lat::SimpleGraph) | ||
return LatticeReactionSystem(rs, srs, DiGraph(lat); init_digraph = false) | ||
end | ||
|
||
### Lattice ReactionSystem Getters ### | ||
|
||
# Get all species. | ||
species(lrs::LatticeReactionSystem) = unique([species(lrs.rs); lrs.spat_species]) | ||
TorkelE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Get all species that may be transported. | ||
spatial_species(lrs::LatticeReactionSystem) = lrs.spat_species | ||
|
||
# Get all parameters. | ||
ModelingToolkit.parameters(lrs::LatticeReactionSystem) = lrs.parameters | ||
# Get all parameters which values are tied to vertexes (compartments). | ||
vertex_parameters(lrs::LatticeReactionSystem) = lrs.vertex_parameters | ||
# Get all parameters which values are tied to edges (adjacencies). | ||
edge_parameters(lrs::LatticeReactionSystem) = lrs.edge_parameters | ||
|
||
# Gets the lrs name (same as rs name). | ||
ModelingToolkit.nameof(lrs::LatticeReactionSystem) = nameof(lrs.rs) | ||
|
||
# Checks if a lattice reaction system is a pure (linear) transport reaction system. | ||
is_transport_system(lrs::LatticeReactionSystem) = all(sr -> sr isa TransportReaction, lrs.spatial_reactions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should consider just making users pass initial conditions with the correct shape for the grid. The reason being that we probably want to support mixed models that have both spatial variables and well-mixed variables ultimately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think stuff like
u0 = [:X => 1.0]
is good. I think in most cases that I am familiar with you either have;Again, I am primarily thinking of stuff like plans where each vertex is a cell. At some point we should have support for well-mixed + not well mixed stuff though (but I think the cases where this is interesting is also the one where we have a continuous spatial domain with automatic discretization).
Let's keep this in mind for the future how we want to do it, but I def. think that the simple interface for defining a spatially uniform variable should stay.