Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/aggregators/directcr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function DirectCRJumpAggregation(nj::Int, njt::T, et::T, crs::Vector{T}, sr::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
9 changes: 9 additions & 0 deletions src/aggregators/prioritytable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,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 @@ -525,6 +529,11 @@ end
function update!(ptt::PriorityTimeTable{T, F}, pid, oldtime, newtime) where {T, F}
(; times, timegrouper, maxtime, pidtogroup, groups) = ptt

# widening arithmetic (e.g. `randexp`) can pass times of a wider type than the
# table's; convert so the timegrouper sees the stored value's type
newtime = convert(T, newtime)
oldtime = convert(T, oldtime)

times[pid] = newtime
if oldtime >= maxtime
# If a reaction comes back into the time window, insert it.
Expand Down
2 changes: 1 addition & 1 deletion src/aggregators/rssacr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function RSSACRJumpAggregation(nj::Int, njt::F, et::F, crs::Vector{F}, sum_rate:
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
2 changes: 1 addition & 1 deletion src/spatial/directcrdirect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function DirectCRDirectJumpAggregation(nj::SpatialJump{J}, njt::T, et::T, rx_rat
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
73 changes: 73 additions & 0 deletions test/table_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,76 @@ DJ.rebuild!(ptt, mintime, timestep)
@test ptt.groups[18].numpids == 1
@test ptt.groups[21].numpids == 1
@test ptt.pidtogroup[1] == (0, 0)

##### Float32 tests (issue #674)
minpriority32 = Float32(2.0)^exponent(Float32(1e-12))
maxpriority32 = Float32(2.0)^exponent(Float32(1e12))
priorities32 = [Float32(1e-13), 0.99f0 * minpriority32, minpriority32, Float32(1.01e-4),
Float32(1e-4), 5.0f0, 0.0f0, Float32(1e10)]
mingid32 = exponent(minpriority32)
ptog32 = priority -> DJ.priortogid(priority, mingid32)
pt32 = DJ.PriorityTable(ptog32, priorities32, minpriority32, maxpriority32)
@test eltype(priorities32) == Float32
@test typeof(pt32.minpriority) == Float32
@test typeof(pt32.maxpriority) == Float32
@test eltype(pt32.groups[1].maxpriority) == Float32

# insert a priority far above the initial maxpriority (table must grow, not hang)
push!(priorities32, Float32(1e20))
DJ.insert!(pt32, length(priorities32), priorities32[end])
@test pt32.groups[end].pids[end] == length(priorities32)

# update across groups
DJ.update!(pt32, 5, priorities32[5], 8.0f0)
priorities32[5] = 8.0f0

# sample returns valid ids and groupsum tracks the priorities
rng32 = StableRNG(12345)
samp = DJ.sample(pt32, priorities32, rng32)
@test 1 <= samp <= length(priorities32)
DJ.resum!(pt32, priorities32)
@test isapprox(DJ.groupsum(pt32), sum(Float64.(priorities32)); rtol = 1e-6)

# table built the way DirectCR builds it (maxpriority = 2*minpriority below
# eps(Float32)): this hung pre-fix, as the constructor collapsed to zero groups and
# zero maxpriority, spinning `padtable!` forever on `0 * 2`
crmin = Float32(2)^exponent(Float32(1e-12))
crtog = priority -> DJ.priortogid(priority, exponent(crmin))
crpt = DJ.PriorityTable(crtog, zeros(Float32, 1), crmin, 2 * crmin)
@test DJ.numgroups(crpt) == 3
@test typeof(crpt.maxpriority) == Float32
@test crpt.maxpriority > 0
DJ.insert!(crpt, 1, 10.0f0)
@test DJ.groupsum(crpt) == 10.0f0
DJ.insert!(crpt, 2, 0.0f0)
@test DJ.sample(crpt, Float32[10.0f0, 0.0f0], rng32) == 1

# PriorityTimeTable with Float32 times; update with a wider (Float64) time, as
# `t + randexp(rng) / rate` produces in CCNRM
times32 = Float32[2.0, 8.0, 13.0, 15.0, 74.0]
ptt32 = DJ.PriorityTimeTable(times32, 0.0f0, 1.5f0 / 16)
@test DJ.getfirst(ptt32) == (1, 2.0f0)
DJ.update!(ptt32, 1, times32[1], 20.0) # Float64 newtime
@test ptt32.times[1] == 20.0f0
@test DJ.getfirst(ptt32) == (2, 8.0f0)
DJ.update!(ptt32, 1, 20.0f0, 70.0) # Float64 newtime outside the window
@test ptt32.pidtogroup[1] == (0, 0)

# full solves with Float32 rates and tspan (issue #674 reproducer)
majump32 = MassActionJump([10.0f0, 1.0f0], [Pair{Int, Int}[], [1 => 1]], [
[1 => 1], [1 => -1]])
dprob32 = DiscreteProblem([0], (0.0f0, 10.0f0))
for agg in (Direct(), DirectCR(), RSSACR(), CCNRM())
sol = solve(JumpProblem(dprob32, agg, majump32), SSAStepper())
@test eltype(sol.t) == Float32
@test sol.u[end][1] >= 0
end

# BigFloat tspan works for the priority-table aggregators (issue #674)
majumpbig = MassActionJump([10.0, 1.0], [Pair{Int, Int}[], [1 => 1]], [[1 => 1], [1 => -1]])
dprobbig = DiscreteProblem([0], (BigFloat(0), BigFloat(10)))
for agg in (DirectCR(), RSSACR(), CCNRM())
sol = solve(JumpProblem(dprobbig, agg, majumpbig), SSAStepper())
@test sol.u[end][1] >= 0
@test eltype(sol.t) == BigFloat
end
Loading