-
-
Notifications
You must be signed in to change notification settings - Fork 35
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 constant rate jump #343
base: master
Are you sure you want to change the base?
Changes from all commits
aa9edad
49a6b49
964afaf
443dca3
eddb7bf
d0115c5
3dc9596
666d2cc
f0997cd
0614fd9
9034abe
ea26ce2
6f1ca89
865aa69
5c97a63
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 | ||||
---|---|---|---|---|---|---|
|
@@ -39,6 +39,9 @@ | |||||
|
||||||
# a dependency graph is needed | ||||||
if dep_graph === nothing | ||||||
if length(rx_rates.cr_jumps) != 0 | ||||||
error("Provide a dependency graph to use DirectCRDirect with constant rate jumps.") | ||||||
end | ||||||
dg = make_dependency_graph(num_specs, rx_rates.ma_jumps) | ||||||
else | ||||||
dg = dep_graph | ||||||
|
@@ -54,6 +57,9 @@ | |||||
end | ||||||
|
||||||
if jumptovars_map === nothing | ||||||
if length(rx_rates.cr_jumps) != 0 | ||||||
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.
Suggested change
|
||||||
error("Provide a jump-to-species dependency graph to use DirectCRDirect with constant rate jumps.") | ||||||
end | ||||||
jtov_map = jump_to_vars_map(rx_rates.ma_jumps) | ||||||
else | ||||||
jtov_map = jumptovars_map | ||||||
|
@@ -94,7 +100,7 @@ | |||||
|
||||||
next_jump = SpatialJump{Int}(typemax(Int), typemax(Int), typemax(Int)) #a placeholder | ||||||
next_jump_time = typemax(typeof(end_time)) | ||||||
rx_rates = RxRates(num_sites(spatial_system), majumps) | ||||||
rx_rates = RxRates(num_sites(spatial_system), majumps, constant_jumps) | ||||||
hop_rates = HopRates(hopping_constants, spatial_system) | ||||||
site_rates = zeros(typeof(end_time), num_sites(spatial_system)) | ||||||
|
||||||
|
@@ -199,4 +205,4 @@ | |||||
|
||||||
number of constant rate jumps | ||||||
""" | ||||||
num_constant_rate_jumps(aggregator::DirectCRDirectJumpAggregation) = 0 | ||||||
num_constant_rate_jumps(aggregator::DirectCRDirectJumpAggregation) = length(aggregator.rx_rates.cr_jumps) | ||||||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,9 @@ | |
|
||
# a dependency graph is needed | ||
if dep_graph === nothing | ||
if length(rx_rates.cr_jumps) != 0 | ||
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. same as above |
||
error("Provide a dependency graph to use NSM with constant rate jumps.") | ||
end | ||
dg = make_dependency_graph(num_specs, rx_rates.ma_jumps) | ||
else | ||
dg = dep_graph | ||
|
@@ -47,6 +50,9 @@ | |
end | ||
|
||
if jumptovars_map === nothing | ||
if length(rx_rates.cr_jumps) != 0 | ||
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. same as above |
||
error("Provide a jump-to-species dependency graph to use NSM with constant rate jumps.") | ||
end | ||
jtov_map = jump_to_vars_map(rx_rates.ma_jumps) | ||
else | ||
jtov_map = jumptovars_map | ||
|
@@ -83,7 +89,7 @@ | |
|
||
next_jump = SpatialJump{Int}(typemax(Int), typemax(Int), typemax(Int)) #a placeholder | ||
next_jump_time = typemax(typeof(end_time)) | ||
rx_rates = RxRates(num_sites(spatial_system), majumps) | ||
rx_rates = RxRates(num_sites(spatial_system), majumps, constant_jumps) | ||
hop_rates = HopRates(hopping_constants, spatial_system) | ||
|
||
NSMJumpAggregation(next_jump, next_jump_time, end_time, rx_rates, hop_rates, | ||
|
@@ -187,4 +193,4 @@ | |
|
||
number of constant rate jumps | ||
""" | ||
num_constant_rate_jumps(aggregator::NSMJumpAggregation) = 0 | ||
num_constant_rate_jumps(aggregator::NSMJumpAggregation) = length(aggregator.rx_rates.cr_jumps) | ||
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,9 +1,10 @@ | ||||||
""" | ||||||
A file with structs and functions for sampling reactions and updating reaction rates in spatial SSAs | ||||||
A file with structs and functions for sampling reactions and updating reaction rates in spatial SSAs. | ||||||
Massaction jumps go first in the indexing, then constant rate jumps. | ||||||
""" | ||||||
|
||||||
### spatial rx rates ### | ||||||
struct RxRates{F, M} | ||||||
struct RxRates{F, M, C} | ||||||
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. For future consideration, do you know if storing the jumps in |
||||||
"rx_rates[i,j] is rate of reaction i at site j" | ||||||
rates::Matrix{F} | ||||||
|
||||||
|
@@ -12,20 +13,25 @@ struct RxRates{F, M} | |||||
|
||||||
"AbstractMassActionJump" | ||||||
ma_jumps::M | ||||||
|
||||||
"indexable collection of ConstantRateJump" | ||||||
cr_jumps::C | ||||||
end | ||||||
|
||||||
""" | ||||||
RxRates(num_sites::Int, ma_jumps::M) where {M} | ||||||
RxRates(num_sites::Int, ma_jumps::M, cr_jumps::C) where {M, C} | ||||||
|
||||||
initializes RxRates with zero rates | ||||||
""" | ||||||
function RxRates(num_sites::Int, ma_jumps::M) where {M} | ||||||
numrxjumps = get_num_majumps(ma_jumps) | ||||||
function RxRates(num_sites::Int, ma_jumps::M, cr_jumps::C) where {M, C} | ||||||
numrxjumps = get_num_majumps(ma_jumps) + length(cr_jumps) | ||||||
rates = zeros(Float64, numrxjumps, num_sites) | ||||||
RxRates{Float64, M}(rates, vec(sum(rates, dims = 1)), ma_jumps) | ||||||
RxRates{Float64, M, C}(rates, vec(sum(rates, dims = 1)), ma_jumps, cr_jumps) | ||||||
end | ||||||
RxRates(num_sites::Int, ma_jumps::M) where {M<:AbstractMassActionJump} = RxRates(num_sites, ma_jumps, ConstantRateJump[]) | ||||||
RxRates(num_sites::Int, cr_jumps::C) where {C} = RxRates(num_sites, SpatialMassActionJump(), cr_jumps) | ||||||
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.
Suggested change
I'd specify a type for |
||||||
|
||||||
num_rxs(rx_rates::RxRates) = get_num_majumps(rx_rates.ma_jumps) | ||||||
num_rxs(rx_rates::RxRates) = get_num_majumps(rx_rates.ma_jumps) + length(rx_rates.cr_jumps) | ||||||
|
||||||
""" | ||||||
reset!(rx_rates::RxRates) | ||||||
|
@@ -48,16 +54,21 @@ function total_site_rx_rate(rx_rates::RxRates, site) | |||||
end | ||||||
|
||||||
""" | ||||||
update_rx_rates!(rx_rates, rxs, u, site) | ||||||
update_rx_rates!(rx_rates, rxs, integrator, site) | ||||||
|
||||||
update rates of all reactions in rxs at site | ||||||
""" | ||||||
function update_rx_rates!(rx_rates::RxRates, rxs, u::AbstractMatrix, integrator, | ||||||
site) | ||||||
ma_jumps = rx_rates.ma_jumps | ||||||
@inbounds for rx in rxs | ||||||
rate = eval_massaction_rate(u, rx, ma_jumps, site) | ||||||
set_rx_rate_at_site!(rx_rates, site, rx, rate) | ||||||
if is_massaction(rx_rates, rx) | ||||||
rate = eval_massaction_rate(u, rx, ma_jumps, site) | ||||||
set_rx_rate_at_site!(rx_rates, site, rx, rate) | ||||||
else | ||||||
cr_jump = rx_rates.cr_jumps[rx - get_num_majumps(ma_jumps)] | ||||||
set_rx_rate_at_site!(rx_rates, site, rx, cr_jump.rate(u, integrator.p, integrator.t, site)) | ||||||
Comment on lines
+69
to
+70
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 isn't type stable is it? That is why we split the ConstantRateJump rates and affects in the non-spatial solvers and wrap them inside |
||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
|
@@ -77,6 +88,16 @@ function sample_rx_at_site(rx_rates::RxRates, site, rng) | |||||
rand(rng) * total_site_rx_rate(rx_rates, site)) | ||||||
end | ||||||
|
||||||
function execute_rx_at_site!(integrator, rx_rates::RxRates, rx, site) | ||||||
if is_massaction(rx_rates, rx) | ||||||
@inbounds executerx!((@view integrator.u[:, site]), rx, | ||||||
rx_rates.ma_jumps) | ||||||
else | ||||||
cr_jump = rx_rates.cr_jumps[rx - get_num_majumps(rx_rates.ma_jumps)] | ||||||
cr_jump.affect!(integrator, site) | ||||||
Comment on lines
+96
to
+97
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 isn't type stable is it? That is why we split the ConstantRateJump rates and affects in the non-spatial solvers and wrap them inside FunctionWrappers. |
||||||
end | ||||||
end | ||||||
|
||||||
# helper functions | ||||||
function set_rx_rate_at_site!(rx_rates::RxRates, site, rx, rate) | ||||||
@inbounds old_rate = rx_rates.rates[rx, site] | ||||||
|
@@ -90,5 +111,10 @@ function Base.show(io::IO, ::MIME"text/plain", rx_rates::RxRates) | |||||
println(io, "RxRates with $num_rxs reactions and $num_sites sites") | ||||||
end | ||||||
|
||||||
"Return true if jump is a massaction jump." | ||||||
function is_massaction(rx_rates::RxRates, rx) | ||||||
rx <= get_num_majumps(rx_rates.ma_jumps) | ||||||
end | ||||||
|
||||||
eval_massaction_rate(u, rx, ma_jumps::M, site) where {M <: SpatialMassActionJump} = evalrxrate(u, rx, ma_jumps, site) | ||||||
eval_massaction_rate(u, rx, ma_jumps::M, site) where {M <: MassActionJump} = evalrxrate((@view u[:, site]), rx, ma_jumps) |
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.