Skip to content
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

Merged
merged 121 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
665a199
Begin tests
TorkelE May 4, 2023
21f3f44
Reupload everything
TorkelE May 4, 2023
b2c7a3b
add graph imports
TorkelE May 4, 2023
eeb7eb8
Start preparing tests
TorkelE May 4, 2023
9ba2721
test updates
TorkelE May 5, 2023
1fb738a
update tests
TorkelE May 5, 2023
93b6926
test fix
TorkelE May 5, 2023
4593a3a
update
TorkelE Jun 28, 2023
283b46a
test update
TorkelE Jun 29, 2023
c07c2af
formating
TorkelE Jun 29, 2023
a2bb7cf
change time requirements
TorkelE Jun 29, 2023
659a189
use SIR test
TorkelE Jun 29, 2023
97d2cf0
update
TorkelE Jun 29, 2023
ebb2d88
update
TorkelE Jun 30, 2023
e0d68f1
More tests
TorkelE Jun 30, 2023
a334a34
test improvement
TorkelE Jul 2, 2023
7f7bc88
add a test
TorkelE Jul 2, 2023
585d4b4
update and more tests
TorkelE Jul 2, 2023
4af6384
format
TorkelE Jul 2, 2023
1b8cf80
internal test update
TorkelE Jul 2, 2023
4b66b59
fix
TorkelE Jul 2, 2023
1b8f09f
updaye
TorkelE Jul 3, 2023
147c0d8
add test and fixes
TorkelE Jul 3, 2023
fbaa488
format
TorkelE Jul 3, 2023
107858f
remove old file
TorkelE Jul 4, 2023
f284bc6
Comment out timing tests
TorkelE Jul 6, 2023
5f627f3
Enable symbolics in diffusion reactions
TorkelE Jul 6, 2023
151c80a
format
TorkelE Jul 6, 2023
22826d5
small fix
TorkelE Jul 6, 2023
405f28c
remove BenchmarkTools test dependency
TorkelE Jul 13, 2023
6393a79
use Num's everywhere
TorkelE Aug 1, 2023
024134c
update
TorkelE Aug 1, 2023
8728a19
use sym properly
TorkelE Aug 2, 2023
bcf4efe
add test
TorkelE Aug 2, 2023
1fef525
another update
TorkelE Aug 2, 2023
e40e04c
update
TorkelE Aug 2, 2023
24586ba
Internal revamp and spatial jump support
TorkelE Aug 12, 2023
25abf21
Add spatial tests.
TorkelE Aug 12, 2023
e64e129
format
TorkelE Aug 12, 2023
52bb6e4
format
TorkelE Aug 12, 2023
5fb99be
test fix
TorkelE Aug 12, 2023
abc4f4e
Remove Jump Stuff
TorkelE Aug 13, 2023
a474b1b
fix
TorkelE Aug 13, 2023
bf0395a
fix
TorkelE Aug 13, 2023
06cc919
test update
TorkelE Aug 13, 2023
7537bbe
use functors
TorkelE Sep 3, 2023
aaf6c7f
functor update
TorkelE Sep 3, 2023
8e221d9
revamp test and start SplitApplyCombine removal
TorkelE Sep 11, 2023
32dd7e1
fix broadcasting issue
TorkelE Sep 11, 2023
d97c4a1
reorder files.
TorkelE Sep 11, 2023
9d034df
Move spatial reactions to separate file
TorkelE Sep 11, 2023
1115e72
wip
TorkelE Sep 11, 2023
35f0aeb
updates
TorkelE Sep 12, 2023
01769cf
Finish LatticeReactionSystem revamp
TorkelE Sep 12, 2023
f19c4c1
redo test networks
TorkelE Sep 12, 2023
f3ad063
LatticeReactionSystem revamp
TorkelE Sep 12, 2023
264965a
test updates
TorkelE Sep 13, 2023
0ee1a53
More tests
TorkelE Sep 13, 2023
186d09b
Remove SplitApplyCombine dependency
TorkelE Sep 13, 2023
e2d555f
More tests
TorkelE Sep 13, 2023
0b75daf
fix
TorkelE Sep 13, 2023
d851fb6
Additional tests
TorkelE Sep 15, 2023
4647bf2
Update performance benchmarks (not run, by saved in repo)
TorkelE Sep 15, 2023
cf73270
Split of utility function and handle un-directed graph input properly
TorkelE Sep 17, 2023
591a9f5
Test fix
TorkelE Sep 17, 2023
acd760f
fix
TorkelE Sep 17, 2023
f0235d3
update history file.
TorkelE Sep 17, 2023
2e3a1cb
Update src/spatial_reaction_systems/spatial_reactions.jl
TorkelE Nov 3, 2023
ae8c7b6
Update src/spatial_reaction_systems/lattice_reaction_systems.jl
TorkelE Nov 3, 2023
d0644ce
Update src/Catalyst.jl
TorkelE Nov 3, 2023
cbbd07d
Update src/spatial_reaction_systems/spatial_reactions.jl
TorkelE Nov 3, 2023
1dd205c
Update src/spatial_reaction_systems/spatial_reactions.jl
TorkelE Nov 3, 2023
59da638
updates
TorkelE Nov 3, 2023
3a21f44
up
TorkelE Nov 3, 2023
fc48925
update
TorkelE Nov 4, 2023
63af0ed
remove internal type ascretain
TorkelE Nov 4, 2023
f9d2714
update
TorkelE Nov 4, 2023
c302fd5
test update
TorkelE Nov 4, 2023
1dd9658
small fix
TorkelE Nov 4, 2023
828a920
test changes
TorkelE Nov 4, 2023
aa33707
update
TorkelE Nov 4, 2023
0051a81
updates
TorkelE Nov 4, 2023
2bf70c6
Test with manually computed Jacobian.
TorkelE Nov 4, 2023
17ba553
update
TorkelE Nov 4, 2023
ea8a0f6
≈ instead if ==
TorkelE Nov 4, 2023
2258e30
Update HISTORY.md
TorkelE Nov 13, 2023
49f0560
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 13, 2023
f087775
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 13, 2023
fb15625
history file update
TorkelE Nov 15, 2023
f41ddef
ups
TorkelE Nov 15, 2023
7e7c3dd
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 15, 2023
31ea398
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 15, 2023
f712fc8
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 15, 2023
0392139
ups
TorkelE Nov 15, 2023
b93bcb1
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 15, 2023
5fe0ab2
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 15, 2023
2b5b6bf
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 15, 2023
d5d8873
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 15, 2023
0795917
up
TorkelE Nov 15, 2023
520bf89
fix
TorkelE Nov 15, 2023
9ab6437
fix
TorkelE Nov 15, 2023
2e3e2d4
ifx
TorkelE Nov 16, 2023
fc47179
update
TorkelE Nov 16, 2023
617d89f
Error for species in TR rate added and tested
TorkelE Nov 17, 2023
b3b229b
Update src/spatial_reaction_systems/spatial_reactions.jl
TorkelE Nov 20, 2023
89f7780
Update test/spatial_reaction_systems/lattice_reaction_systems_ODEs.jl
TorkelE Nov 20, 2023
4903bbe
Update test/spatial_reaction_systems/lattice_reaction_systems_ODEs.jl
TorkelE Nov 20, 2023
5397007
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 20, 2023
be39cdd
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 20, 2023
499a748
Update test/spatial_reaction_systems/lattice_reaction_systems.jl
TorkelE Nov 20, 2023
e058f47
Update test/spatial_reaction_systems/lattice_reaction_systems.jl
TorkelE Nov 20, 2023
121f2c2
Auto stash before merge of "lattice_reaction_system_may_2023" and "or…
TorkelE Nov 20, 2023
26634d3
Update src/spatial_reaction_systems/spatial_ODE_systems.jl
TorkelE Nov 20, 2023
2a81fca
up
TorkelE Nov 21, 2023
4341962
reduce more lines.
TorkelE Nov 21, 2023
621d664
use generated rate law function
TorkelE Nov 21, 2023
efa91d0
Update HISTORY.md
TorkelE Nov 21, 2023
1f9386d
up
TorkelE Nov 21, 2023
820efb0
use SteadyStateDiffEq 2 syntax
TorkelE Nov 21, 2023
9f6c1af
update remaining tests for SteadyStateDiffEq v2
TorkelE Nov 22, 2023
5e220f7
remove old SteadyStateDiffEq file (with similar test, but old syntax)
TorkelE Nov 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
# Breaking updates and feature summaries across releases

## Catalyst unreleased (master branch)
- Simulation of spatial ODEs now supported. For full details, please see https://github.com/SciML/Catalyst.jl/pull/644 and upcoming documentation. Note that these methods are currently considered alpha, with the interface and approach changing even in non-breaking Catalyst releases.
- LatticeReactionSystem structure represents a spatial reaction network:
```julia
rn = @reaction_network begin
(p,d), 0 <--> X
end
tr = @transport_reaction D X
lattice = Graphs.grid([5, 5])
lrs = LatticeReactionSystem(rn, [tr], lattice)
```
- Here, if a `u0` or `p` vector is given with scalar values:
```julia
u0 = [:X => 1.0]
p = [:p => 1.0, :d => 0.5, :D => 0.1]
```
this value will be used across the entire system. If their values are instead vectors, different values are used across the spatial system. Here
Comment on lines +16 to +19
Copy link
Member

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.

Copy link
Member Author

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;

  • A variable has uniform initial condition across the grid (or potentially random with inform distribution).
  • A variable is 0 in most of the grid vertexes, but has a value in others.

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.

```julia
X0 = zeros(25)
X0[1] = 1.0
u0 = [:X => X0]
```
X's value will be `1.0` in the first vertex, but `0.0` in the remaining one (the system have 25 vertexes in total). SInce th parameters `p` and `d` are part of the non-spatial reaction network, their values are tied to vertexes. However, if the `D` parameter (which governs diffusion between vertexes) is given several values, these will instead correspond to the specific edges (and transportation along those edges.)


## Catalyst 13.5
- Added a CatalystHomotopyContinuationExtension extension, which exports the `hc_steady_state` function if HomotopyContinuation is exported. `hc_steady_state` finds the steady states of a reactin system using the homotopy continuation method. This feature is only available for julia versions 1.9+. Example:
- Added a CatalystHomotopyContinuationExtension extension, which exports the `hc_steady_state` function if HomotopyContinuation is exported. `hc_steady_state` finds the steady states of a reaction system using the homotopy continuation method. This feature is only available for julia versions 1.9+. Example:
TorkelE marked this conversation as resolved.
Show resolved Hide resolved
```julia
wilhelm_2009_model = @reaction_network begin
k1, Y --> 2X
Expand Down
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a compat entry too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I just put = "0.5.12"?

Copy link
Member

Choose a reason for hiding this comment

The 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"
Expand All @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions src/Catalyst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const MT = ModelingToolkit
using Unitful
@reexport using ModelingToolkit
using Symbolics

using RuntimeGeneratedFunctions
RuntimeGeneratedFunctions.init(@__MODULE__)

import Symbolics: BasicSymbolic
import SymbolicUtils
using ModelingToolkit: Symbolic, value, istree, get_states, get_ps, get_iv, get_systems,
Expand All @@ -33,6 +37,7 @@ import ModelingToolkit: check_variables,

import Base: (==), hash, size, getindex, setindex, isless, Sort.defalg, length, show
import MacroTools, Graphs
import Graphs: DiGraph, SimpleGraph, SimpleDiGraph, vertices, edges, add_vertices!, nv, ne
import DataStructures: OrderedDict, OrderedSet
import Parameters: @with_kw_noshow

Expand Down Expand Up @@ -107,4 +112,22 @@ export balance_reaction
function hc_steady_states end
export hc_steady_states

### Spatial Reaction Networks ###

# spatial reactions
include("spatial_reaction_systems/spatial_reactions.jl")
export TransportReaction, TransportReactions, @transport_reaction
export isedgeparameter

# lattice reaction systems
include("spatial_reaction_systems/lattice_reaction_systems.jl")
export LatticeReactionSystem
export spatial_species, vertex_parameters, edge_parameters

# variosu utility functions
include("spatial_reaction_systems/utility.jl")

# spatial lattice ode systems.
include("spatial_reaction_systems/spatial_ODE_systems.jl")

end # module
91 changes: 91 additions & 0 deletions src/spatial_reaction_systems/lattice_reaction_systems.jl
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.")

Check warning on line 44 in src/spatial_reaction_systems/lattice_reaction_systems.jl

View check run for this annotation

Codecov / codecov/patch

src/spatial_reaction_systems/lattice_reaction_systems.jl#L44

Added line #L44 was not covered by tests
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)
Loading
Loading