Skip to content
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
21 changes: 21 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Breaking updates and feature summaries across releases

## Unreleased
- Jump times are now drawn in the numeric type of the time, without its units (the type
of `one(t)`), in every SSA aggregator and in `VR_FRM`, `VR_Direct` and `VR_DirectFW`.
`Float32` times are no longer computed in `Float64`, which also lets `CCNRM` solve
problems with `Float32` times (it failed with a `MethodError`), and `BigFloat` times
are drawn at `BigFloat` precision. `Float64` results are unchanged; seeded results can
change for `Float32` and `BigFloat` problems.
- `DirectCR`, `RSSACR` and the spatial `DirectCRDirect` can now build problems with
`Float32` or `BigFloat` rates and times (they failed with a `MethodError`). `RDirect`
still can't build problems with `BigFloat` rates.
- `NSM` and `DirectCRDirect` now compute reaction rates in the type of the time, as the
other aggregators do, instead of `Float64`: a `BigFloat` rate below `floatmin(Float64)`
rounded to zero, so its reaction never fired. Seeded results can change for spatial
problems with `Float32` times.
- `DirectCR`, `RSSACR` and `DirectCRDirect` no longer hang on a rate of at least
`2^exponent(floatmax(T))` for rates of type `T` (e.g. `2.0^1023`), or on a tiny
`minrate` keyword (e.g. `1e-50` with `Float32` rates, or `floatmin(BigFloat)`). `minrate`
is now raised to at least `eps(T)`, since rates at or below `eps(T)` already share the
tables' first group; for `Float32` rates this raises the default from `2^-40` to
`eps(Float32)`.

## 10.2.0
- New `isolated_copy(jprob)` returns a `JumpProblem` with its own jump state, for
concurrent solves and for integrators that are alive at the same time. It copies only
Expand Down
6 changes: 3 additions & 3 deletions src/aggregators/ccnrm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function update_dependent_rates!(p::CCNRMJumpAggregation, dep_rxs, executed_jump
end
else
if cur_rates[rx] > zero(eltype(cur_rates))
update!(ptt, rx, oldtime, t + randexp(rng) / cur_rates[rx])
update!(ptt, rx, oldtime, t + typed_randexp(rng, t) / cur_rates[rx])
else
update!(ptt, rx, oldtime, floatmax(typeof(t)))
end
Expand All @@ -143,15 +143,15 @@ function initialize_rates_and_times!(p::CCNRMJumpAggregation, u, params, t, rng)
maj_rates = p.maj_rates
@inbounds for i in 1:get_num_majumps(majumps)
cur_rates[i] = evalrxrate(u, i, majumps, maj_rates)
pttdata[i] = t + randexp(rng) / cur_rates[i]
pttdata[i] = t + typed_randexp(rng, t) / cur_rates[i]
end

# Initialize next-reaction times for the constant rates
rates = p.rates
idx = get_num_majumps(majumps) + 1
@inbounds for rate in rates
cur_rates[idx] = rate(u, params, t)
pttdata[idx] = t + randexp(rng) / cur_rates[idx]
pttdata[idx] = t + typed_randexp(rng, t) / cur_rates[idx]
idx += 1
end

Expand Down
6 changes: 3 additions & 3 deletions src/aggregators/coevolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function accept_next_jump!(p::CoevolveJumpAggregation, integrator, u, params, t)
# it was longer than the rateinterval when it was proposed
if lrate == typemax(t)
urate = get_urate(p, uidx, u, params, t)
s = urate == zero(t) ? typemax(t) : randexp(rng) / urate
s = urate == zero(t) ? typemax(t) : typed_randexp(rng, t) / urate
elseif lrate < urate
# when the lower and upper bound are the same, then v < 1 = lrate / urate = urate / urate
v = rand(rng) * urate
Expand All @@ -238,7 +238,7 @@ function accept_next_jump!(p::CoevolveJumpAggregation, integrator, u, params, t)
rate = get_rate(p, lidx, u, params, t)
if v > rate
urate = get_urate(p, uidx, u, params, t)
s = urate == zero(t) ? typemax(t) : randexp(rng) / urate
s = urate == zero(t) ? typemax(t) : typed_randexp(rng, t) / urate
end
end
end
Expand Down Expand Up @@ -305,7 +305,7 @@ function next_time(p::CoevolveJumpAggregation, u, params, t, i, rng)
return next_candidate_time!(p, u, params, t, s, lidx), urate
end
end
s = urate == zero(t) ? typemax(t) : randexp(rng) / urate
s = urate == zero(t) ? typemax(t) : typed_randexp(rng, t) / urate
return next_candidate_time!(p, u, params, t, s, lidx), urate
end

Expand Down
4 changes: 2 additions & 2 deletions src/aggregators/direct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function time_to_next_jump(p::DirectJumpAggregation{T, S, F1}, u, params,
end

@inbounds sum_rate = cur_rates[end]
sum_rate, randexp(rng) / sum_rate
sum_rate, typed_randexp(rng, t) / sum_rate
end

# Unrolled at compile time: recursive splatting over `rates...` is only inferred for
Expand Down Expand Up @@ -147,5 +147,5 @@ function time_to_next_jump(p::DirectJumpAggregation{T, S, F1}, u, params,
end

@inbounds sum_rate = cur_rates[end]
sum_rate, randexp(rng) / sum_rate
sum_rate, typed_randexp(rng, t) / sum_rate
end
9 changes: 6 additions & 3 deletions src/aggregators/directcr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ function DirectCRJumpAggregation(nj::Int, njt::T, et::T, crs::Vector{T}, sr::T,
maxrate = convert(T, Inf),
kwargs...) where {T, S, F1, F2}

# mapping from jump rate to group id
# mapping from jump rate to group id, in the rates' type. Rates at or below eps(T) all go
# in the table's first group, so a smaller minrate only adds empty groups, one per power
# of two (about 10^9 for floatmin(BigFloat)): it is raised to eps(T)
minrate = max(convert(T, minrate), eps(T))
minexponent = exponent(minrate)

# use the largest power of two that is <= the passed in minrate
minrate = 2.0^minexponent
minrate = T(2)^minexponent
ratetogroup = rate -> priortogid(rate, minexponent)

# construct an empty initial priority table -- we'll reset this in init
Expand Down Expand Up @@ -118,7 +121,7 @@ end
nomorejumps!(p, p.sum_rate) && return nothing

rng = get_rng(integrator)
p.next_jump_time = t + randexp(rng) / p.sum_rate
p.next_jump_time = t + typed_randexp(rng, t) / p.sum_rate

if p.next_jump_time < p.end_time
p.next_jump = sample(p.rt, p.cur_rates, rng)
Expand Down
6 changes: 3 additions & 3 deletions src/aggregators/frm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function next_ma_jump(p::FRMJumpAggregation, u, params, t, rng)
maj_rates = p.maj_rates
@inbounds for i in 1:get_num_majumps(majumps)
p.cur_rates[i] = evalrxrate(u, i, majumps, maj_rates)
dt = randexp(rng) / p.cur_rates[i]
dt = typed_randexp(rng, t) / p.cur_rates[i]
if dt < ttnj
ttnj = dt
nextrx = i
Expand All @@ -112,7 +112,7 @@ function next_constant_rate_jump(p::FRMJumpAggregation{T, S, F1, F2}, u, params,
idx = get_num_majumps(p.ma_jumps) + 1
fill_cur_rates(u, params, t, p.cur_rates, idx, p.rates)
@inbounds for i in idx:length(p.cur_rates)
dt = randexp(rng) / p.cur_rates[i]
dt = typed_randexp(rng, t) / p.cur_rates[i]
if dt < ttnj
ttnj = dt
nextrx = i
Expand All @@ -131,7 +131,7 @@ function next_constant_rate_jump(p::FRMJumpAggregation{T, S, F1}, u, params,
idx = get_num_majumps(p.ma_jumps) + 1
@inbounds for i in 1:length(p.rates)
p.cur_rates[idx] = p.rates[i](u, params, t)
dt = randexp(rng) / p.cur_rates[idx]
dt = typed_randexp(rng, t) / p.cur_rates[idx]
if dt < ttnj
ttnj = dt
nextrx = idx
Expand Down
6 changes: 3 additions & 3 deletions src/aggregators/nrm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function update_dependent_rates!(p::NRMJumpAggregation, dep_rxs, executed_jump,
end
else
if cur_rates[rx] > zero(eltype(cur_rates))
update!(p.pq, rx, t + randexp(rng) / cur_rates[rx])
update!(p.pq, rx, t + typed_randexp(rng, t) / cur_rates[rx])
else
update!(p.pq, rx, typemax(t))
end
Expand All @@ -123,15 +123,15 @@ function fill_rates_and_get_times!(p::NRMJumpAggregation, u, params, t, rng)
maj_rates = p.maj_rates
@inbounds for i in 1:get_num_majumps(majumps)
cur_rates[i] = evalrxrate(u, i, majumps, maj_rates)
pqdata[i] = t + randexp(rng) / cur_rates[i]
pqdata[i] = t + typed_randexp(rng, t) / cur_rates[i]
end

# constant rates
rates = p.rates
idx = get_num_majumps(majumps) + 1
@inbounds for rate in rates
cur_rates[idx] = rate(u, params, t)
pqdata[idx] = t + randexp(rng) / cur_rates[idx]
pqdata[idx] = t + typed_randexp(rng, t) / cur_rates[idx]
idx += 1
end

Expand Down
12 changes: 11 additions & 1 deletion src/aggregators/prioritytable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ function PriorityTable(priortogid::Function, priorities::AbstractVector, minprio
maxpriority)
numgroups = priortogid(maxpriority)
numgroups -= one(typeof(numgroups))
# `priortogid` maps priorities at or below `eps` to group 1, so when minpriority
# itself is below `eps` (e.g. Float32, where 2^-40 < eps(Float32)) this can give
# fewer than the three base groups {0}, (0,minpriority), [minpriority,2*minpriority).
numgroups = max(numgroups, 3)
pidtype = typeof(numgroups)
ptype = eltype(priorities)
groups = Vector{PriorityGroup{ptype, Vector{pidtype}}}()
Expand Down Expand Up @@ -174,6 +178,12 @@ end
pt.gsum
end

# The rejection bound of the group of priorities below `maxpriority`. For the group of the
# largest finite priorities, `maxpriority` overflows to Inf, against which `sample` would
# never accept, so the bound is capped at `floatmax`. The table's `maxpriority` is not capped:
# it reaches Inf, so the table stops growing.
@inline rejection_bound(maxpriority) = min(maxpriority, floatmax(typeof(maxpriority)))

"""
Adds extra groups to the table to accommodate a new maxpriority.
"""
Expand All @@ -183,7 +193,7 @@ function padtable!(pt::PriorityTable, pid, priority)

while priority >= maxpriority
maxpriority *= 2
push!(groups, PriorityGroup{pidtype}(maxpriority))
push!(groups, PriorityGroup{pidtype}(rejection_bound(maxpriority)))
push!(gsums, zero(eltype(gsums)))
end
pt.maxpriority = maxpriority
Expand Down
2 changes: 1 addition & 1 deletion src/aggregators/rdirect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function generate_jumps!(p::RDirectJumpAggregation, integrator, u, params, t)

p.counter = counter
p.next_jump = rx
p.next_jump_time = t + randexp(rng) / sum_rate
p.next_jump_time = t + typed_randexp(rng, t) / sum_rate
nothing
end

Expand Down
4 changes: 2 additions & 2 deletions src/aggregators/rssa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function generate_jumps!(p::RSSAJumpAggregation, integrator, u, params, t)
jidx = linear_search(cur_rate_high, r)
# the search finds no jump only if the sum exceeds the sum of the rate bounds
iszero(jidx) && ((sum_rate, jidx) = resum_and_search!(p, rng))
rerl += randexp(rng)
rerl += typed_randexp(rng, t)
nrejected = 0
@inbounds while rejectrx(ma_jumps, num_majumps, rates, cur_rate_high,
cur_rate_low, rng, u, jidx, params, t, p.maj_rates)
Expand All @@ -129,7 +129,7 @@ function generate_jumps!(p::RSSAJumpAggregation, integrator, u, params, t)
r = rand(rng) * sum_rate
jidx = linear_search(cur_rate_high, r)
iszero(jidx) && ((sum_rate, jidx) = resum_and_search!(p, rng))
rerl += randexp(rng)
rerl += typed_randexp(rng, t)
end
p.next_jump = jidx

Expand Down
11 changes: 7 additions & 4 deletions src/aggregators/rssacr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ function RSSACRJumpAggregation(nj::Int, njt::F, et::F, crs::Vector{F}, sum_rate:
ulow = similar(u)
uhigh = similar(u)

# mapping from jump rate to group id
# mapping from jump rate to group id, in the rates' type. Rates at or below eps(F) all go
# in the table's first group, so a smaller minrate only adds empty groups, one per power
# of two (about 10^9 for floatmin(BigFloat)): it is raised to eps(F)
minrate = max(convert(F, minrate), eps(F))
minexponent = exponent(minrate)

# use the largest power of two that is <= the passed in minrate
minrate = 2.0^minexponent
minrate = F(2)^minexponent
ratetogroup = rate -> priortogid(rate, minexponent)

# construct an empty initial priority table -- we'll reset this in init
Expand Down Expand Up @@ -140,7 +143,7 @@ function generate_jumps!(p::RSSACRJumpAggregation, integrator, u, params, t)
jidx = sample(rt, cur_rate_high, rng)
# sampling finds no jump only if the sums exceed the sum of the rate bounds
iszero(jidx) && ((sum_rate, jidx) = resum_and_sample!(p, rng))
rerl += randexp(rng)
rerl += typed_randexp(rng, t)
nrejected = 0
while rejectrx(ma_jumps, num_majumps, rates, cur_rate_high, cur_rate_low, rng, u, jidx,
params, t, p.maj_rates)
Expand All @@ -154,7 +157,7 @@ function generate_jumps!(p::RSSACRJumpAggregation, integrator, u, params, t)
# sample candidate reaction
jidx = sample(rt, cur_rate_high, rng)
iszero(jidx) && ((sum_rate, jidx) = resum_and_sample!(p, rng))
rerl += randexp(rng)
rerl += typed_randexp(rng, t)
end
p.next_jump = jidx

Expand Down
2 changes: 1 addition & 1 deletion src/aggregators/sortingdirect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function generate_jumps!(p::SortingDirectJumpAggregation, integrator, u, params,
nomorejumps!(p, p.sum_rate) && return nothing

rng = get_rng(integrator)
p.next_jump_time = t + randexp(rng) / p.sum_rate
p.next_jump_time = t + typed_randexp(rng, t) / p.sum_rate

# search for next jump
if p.next_jump_time < p.end_time
Expand Down
9 changes: 9 additions & 0 deletions src/aggregators/ssajump.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ function DiscreteCallback(c::AbstractSSAJumpAggregator)
DiscreteCallback(c, c, initialize = c, save_positions = c.save_positions)
end

# An exponential random variate in the numeric type of the time `t` without its units
# (`one(t)`), so that `t + typed_randexp(rng, t) / rate` keeps the type of `t`. Floating
# point types are drawn in that type; other numbers, such as dual numbers, convert a `Float64`
# draw. Floating point times skip `one(t)`, which allocates for `BigFloat`.
@inline typed_randexp(rng, t::AbstractFloat) = randexp(rng, typeof(t))
@inline typed_randexp(rng, t) = _typed_randexp(rng, typeof(one(t)))
@inline _typed_randexp(rng, ::Type{T}) where {T <: AbstractFloat} = randexp(rng, T)
@inline _typed_randexp(rng, ::Type{T}) where {T} = convert(T, randexp(rng))

########### The following routines are templates for all SSAs ###########
########### Generally they should not need to be overloaded. ###########

Expand Down
11 changes: 7 additions & 4 deletions src/spatial/directcrdirect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ function DirectCRDirectJumpAggregation(nj::SpatialJump{J}, njt::T, et::T, rx_rat
num_specs, minrate = convert(T, MINJUMPRATE),
dependencies, kwargs...) where {J, T, RX, HOP, SS}

# mapping from jump rate to group id
# mapping from jump rate to group id, in the rates' type. Rates at or below eps(T) all go
# in the table's first group, so a smaller minrate only adds empty groups, one per power
# of two (about 10^9 for floatmin(BigFloat)): it is raised to eps(T)
minrate = max(convert(T, minrate), eps(T))
minexponent = exponent(minrate)

# use the largest power of two that is <= the passed in minrate
minrate = 2.0^minexponent
minrate = T(2)^minexponent
ratetogroup = rate -> priortogid(rate, minexponent)

# construct an empty initial priority table -- we'll reset this in init
Expand Down Expand Up @@ -71,7 +74,7 @@ function aggregate(aggregator::DirectCRDirect, starting_state, p, t, end_time,

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, typeof(end_time))
hop_rates = HopRates(hopping_constants, spatial_system)
site_rates = zeros(typeof(end_time), num_sites(spatial_system))

Expand All @@ -91,7 +94,7 @@ end
# calculate the next jump / jump time
function generate_jumps!(p::DirectCRDirectJumpAggregation, integrator, params, u, t)
rng = get_rng(integrator)
p.next_jump_time = t + randexp(rng) / p.rt.gsum
p.next_jump_time = t + typed_randexp(rng, t) / p.rt.gsum
p.next_jump_time >= p.end_time && return nothing
site = sample(p.rt, p.site_rates, rng)
p.next_jump = sample_jump_direct(p, site, rng)
Expand Down
7 changes: 4 additions & 3 deletions src/spatial/nsm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function aggregate(aggregator::NSM, starting_state, p, t, end_time, constant_jum

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, typeof(end_time))
hop_rates = HopRates(hopping_constants, spatial_system)

NSMJumpAggregation(next_jump, next_jump_time, end_time, rx_rates, hop_rates,
Expand Down Expand Up @@ -117,7 +117,8 @@ function fill_rates_and_get_times!(aggregation::NSMJumpAggregation, integrator,
@inbounds for site in 1:num_nodes
update_rx_rates!(rx_rates, rxs, integrator, site)
update_hop_rates!(hop_rates, species, u, site, spatial_system)
pqdata[site] = t + randexp(rng) / (total_site_rate(rx_rates, hop_rates, site))
pqdata[site] = t +
typed_randexp(rng, t) / (total_site_rate(rx_rates, hop_rates, site))
end

aggregation.pq = MutableBinaryMinHeap(pqdata)
Expand Down Expand Up @@ -155,7 +156,7 @@ update the time of site in the priority queue
function update_site_time!(p::NSMJumpAggregation, site, t, rng)
site_rate = (total_site_rate(p.rx_rates, p.hop_rates, site))
if site_rate > zero(typeof(site_rate))
update!(p.pq, site, t + randexp(rng) / site_rate)
update!(p.pq, site, t + typed_randexp(rng, t) / site_rate)
else
update!(p.pq, site, typemax(t))
end
Expand Down
12 changes: 6 additions & 6 deletions src/spatial/reaction_rates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ end
shared_fields(::Type{<:RxRates}) = (:ma_jumps,)

"""
RxRates(num_sites::Int, ma_jumps::M) where {M}
RxRates(num_sites::Int, ma_jumps::M, ::Type{F} = Float64) where {M, F}

initializes RxRates with zero rates
initializes RxRates with zero rates of type `F`
"""
function RxRates(num_sites::Int, ma_jumps::M) where {M}
function RxRates(num_sites::Int, ma_jumps::M, ::Type{F} = Float64) where {M, F}
numrxjumps = get_num_majumps(ma_jumps)
rates = zeros(Float64, numrxjumps, num_sites)
maj_rates = Vector{Float64}(undef, numrxjumps)
RxRates{Float64, M}(rates, vec(sum(rates, dims = 1)), ma_jumps, maj_rates)
rates = zeros(F, numrxjumps, num_sites)
maj_rates = Vector{F}(undef, numrxjumps)
RxRates{F, M}(rates, vec(sum(rates, dims = 1)), ma_jumps, maj_rates)
end

num_rxs(rx_rates::RxRates) = get_num_majumps(rx_rates.ma_jumps)
Expand Down
Loading
Loading