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

Fix method ambiguities in spatial massaction jump #392

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 1 addition & 5 deletions src/spatial/flatten.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ function flatten(ma_jump, prob::DiscreteProblem, spatial_system, hopping_constan
rx_rates = ma_jump.scaled_rates
elseif isa(ma_jump, SpatialMassActionJump)
num_nodes = num_sites(spatial_system)
if isnothing(ma_jump.uniform_rates) && isnothing(ma_jump.spatial_rates)
rx_rates = zeros(0, num_nodes)
elseif isnothing(ma_jump.uniform_rates)
rx_rates = ma_jump.spatial_rates
elseif isnothing(ma_jump.spatial_rates)
if isempty(ma_jump.spatial_rates)
rx_rates = reshape(repeat(ma_jump.uniform_rates, num_nodes),
length(ma_jump.uniform_rates), num_nodes)
else
Expand Down
90 changes: 19 additions & 71 deletions src/spatial/spatial_massaction_jump.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const AVecOrNothing = Union{AbstractVector, Nothing}
const AMatOrNothing = Union{AbstractMatrix, Nothing}

struct SpatialMassActionJump{A <: AVecOrNothing, B <: AMatOrNothing, S, U, V} <:
struct SpatialMassActionJump{A <: AbstractVector, B <: AbstractMatrix, S, U, V} <:
AbstractMassActionJump
uniform_rates::A # reactions that are uniform in space
spatial_rates::B # reactions whose rate depends on the site
Expand All @@ -16,25 +13,24 @@ struct SpatialMassActionJump{A <: AVecOrNothing, B <: AMatOrNothing, S, U, V} <:
reactant_stoch::S, net_stoch::U,
param_mapper::V, scale_rates::Bool,
useiszero::Bool,
nocopy::Bool) where {A <: AVecOrNothing,
B <: AMatOrNothing,
nocopy::Bool) where {A <: AbstractVector,
B <: AbstractMatrix,
S, U, V}
uniform_rates = (nocopy || isnothing(uniform_rates)) ? uniform_rates :
copy(uniform_rates)
spatial_rates = (nocopy || isnothing(spatial_rates)) ? spatial_rates :
copy(spatial_rates)
uniform_rates = nocopy ? uniform_rates : copy(uniform_rates)
spatial_rates = nocopy ? spatial_rates : copy(spatial_rates)

reactant_stoch = nocopy ? reactant_stoch : copy(reactant_stoch)
for i in eachindex(reactant_stoch)
if useiszero && (length(reactant_stoch[i]) == 1) &&
iszero(reactant_stoch[i][1][1])
reactant_stoch[i] = typeof(reactant_stoch[i])()
end
end
num_unif_rates = isnothing(uniform_rates) ? 0 : length(uniform_rates)
num_unif_rates = length(uniform_rates)
if scale_rates && num_unif_rates > 0
scalerates!(uniform_rates, reactant_stoch)
end
if scale_rates && !isnothing(spatial_rates) && !isempty(spatial_rates)
if scale_rates && !isempty(spatial_rates)
scalerates!(spatial_rates, reactant_stoch[(num_unif_rates + 1):end])
end
new(uniform_rates, spatial_rates, reactant_stoch, net_stoch, param_mapper)
Expand All @@ -43,83 +39,35 @@ end

################ Constructors ##################

function SpatialMassActionJump(urates::A, srates::B, rs::S, ns::U, pmapper::V;
function SpatialMassActionJump(urates::A, srates::B, rs, ns, pmapper = nothing;
scale_rates = true, useiszero = true,
nocopy = false) where {A <: AVecOrNothing,
B <: AMatOrNothing, S, U, V}
SpatialMassActionJump{A, B, S, U, V}(urates, srates, rs, ns, pmapper, scale_rates,
nocopy = false) where {A <: AbstractVector,
B <: AbstractMatrix}
SpatialMassActionJump{A, B, typeof(rs), typeof(ns), typeof(pmapper)}(urates, srates, rs, ns, pmapper, scale_rates,
useiszero, nocopy)
end
function SpatialMassActionJump(urates::A, srates::B, rs, ns; scale_rates = true,
useiszero = true,
nocopy = false) where {A <: AVecOrNothing,
B <: AMatOrNothing}
SpatialMassActionJump(urates, srates, rs, ns, nothing; scale_rates = scale_rates,
useiszero = useiszero, nocopy = nocopy)
end

function SpatialMassActionJump(srates::B, rs, ns, pmapper; scale_rates = true,
useiszero = true,
nocopy = false) where {B <: AMatOrNothing}
SpatialMassActionJump(nothing, srates, rs, ns, pmapper; scale_rates = scale_rates,
useiszero = useiszero, nocopy = nocopy)
end
function SpatialMassActionJump(srates::B, rs, ns; scale_rates = true, useiszero = true,
nocopy = false) where {B <: AMatOrNothing}
SpatialMassActionJump(nothing, srates, rs, ns, nothing; scale_rates = scale_rates,
useiszero = useiszero, nocopy = nocopy)
function SpatialMassActionJump(srates::AbstractMatrix, rs, ns, pmapper = nothing; kwargs...)
SpatialMassActionJump(Vector{eltype(srates)}(undef, 0), srates, rs, ns, pmapper; kwargs...)
end

function SpatialMassActionJump(urates::A, rs, ns, pmapper; scale_rates = true,
useiszero = true,
nocopy = false) where {A <: AVecOrNothing}
SpatialMassActionJump(urates, nothing, rs, ns, pmapper; scale_rates = scale_rates,
useiszero = useiszero, nocopy = nocopy)
end
function SpatialMassActionJump(urates::A, rs, ns; scale_rates = true, useiszero = true,
nocopy = false) where {A <: AVecOrNothing}
SpatialMassActionJump(urates, nothing, rs, ns, nothing; scale_rates = scale_rates,
useiszero = useiszero, nocopy = nocopy)
function SpatialMassActionJump(urates::AbstractVector, rs::AbstractVector, ns, pmapper = nothing; kwargs...)
SpatialMassActionJump(urates, Matrix{eltype(urates)}(undef, 0, 0), rs, ns, pmapper; kwargs...)
end

function SpatialMassActionJump(ma_jumps::MassActionJump{T, S, U, V}; scale_rates = true,
useiszero = true, nocopy = false) where {T, S, U, V}
function SpatialMassActionJump(ma_jumps::M; kwargs...) where {M <: MassActionJump}
SpatialMassActionJump(ma_jumps.scaled_rates, ma_jumps.reactant_stoch,
ma_jumps.net_stoch, ma_jumps.param_mapper;
scale_rates = scale_rates, useiszero = useiszero, nocopy = nocopy)
kwargs...)
end

##############################################

function get_num_majumps(smaj::SpatialMassActionJump{Nothing, Nothing, S, U, V}) where
{S, U, V}
0
end
function get_num_majumps(smaj::SpatialMassActionJump{Nothing, B, S, U, V}) where
{B, S, U, V}
size(smaj.spatial_rates, 1)
end
function get_num_majumps(smaj::SpatialMassActionJump{A, Nothing, S, U, V}) where
{A, S, U, V}
length(smaj.uniform_rates)
end
function get_num_majumps(smaj::SpatialMassActionJump{A, B, S, U, V}) where
{A <: AbstractVector, B <: AbstractMatrix, S, U, V}
length(smaj.uniform_rates) + size(smaj.spatial_rates, 1)
end
using_params(smaj::SpatialMassActionJump) = false

function rate_at_site(rx, site,
smaj::SpatialMassActionJump{Nothing, B, S, U, V}) where {B, S, U, V}
smaj.spatial_rates[rx, site]
end
function rate_at_site(rx, site,
smaj::SpatialMassActionJump{A, Nothing, S, U, V}) where {A, S, U, V}
smaj.uniform_rates[rx]
end
function rate_at_site(rx, site,
smaj::SpatialMassActionJump{A, B, S, U, V}) where
{A <: AbstractVector, B <: AbstractMatrix, S, U, V}
smaj::SpatialMassActionJump)
num_unif_rxs = length(smaj.uniform_rates)
rx <= num_unif_rxs ? smaj.uniform_rates[rx] :
smaj.spatial_rates[rx - num_unif_rxs, site]
Expand Down
Loading