From a621d641d5fcc7c07b68a0754c2ccac60c7a2976 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Tue, 10 Nov 2026 02:45:46 -0500 Subject: [PATCH 1/2] Fix Float32/BigFloat priority-table setup (issue #674) Aggregator constructors computed the table minimum rate as a Float64 (2.0^minexponent), forcing priority-group bounds to Float64 when T was Float32 or BigFloat. Compute the power of two in T instead. The PriorityTable constructor also collapsed to zero groups when the minimum rate was below eps(T) (Float32): priortogid maps values at or below eps to group 1, giving numgroups = 0 and maxpriority = 0, which made padtable! spin forever on 0 * 2. Clamp the table to its three base groups so maxpriority stays positive. CCNRM's PriorityTimeTable update received times of a wider type than the table's (t + randexp(rng)/rate is Float64 when T is Float32). Convert old and new times to the table's type before the TimeGrouper call. Co-Authored-By: Chris Rackauckas Co-Authored-By: Muse Agent-Harness: Muse CLI Code Agent-Model: muse-ember-1.0 Agent-Session: local job 674-muse (no session URL) --- src/aggregators/directcr.jl | 2 +- src/aggregators/prioritytable.jl | 9 +++++++++ src/aggregators/rssacr.jl | 2 +- src/spatial/directcrdirect.jl | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/aggregators/directcr.jl b/src/aggregators/directcr.jl index 8bbb9fc6e..3e8b8626e 100644 --- a/src/aggregators/directcr.jl +++ b/src/aggregators/directcr.jl @@ -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 diff --git a/src/aggregators/prioritytable.jl b/src/aggregators/prioritytable.jl index 620a062c9..3af08e06c 100644 --- a/src/aggregators/prioritytable.jl +++ b/src/aggregators/prioritytable.jl @@ -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}}}() @@ -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. diff --git a/src/aggregators/rssacr.jl b/src/aggregators/rssacr.jl index f54f22ad4..b685b9fcc 100644 --- a/src/aggregators/rssacr.jl +++ b/src/aggregators/rssacr.jl @@ -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 diff --git a/src/spatial/directcrdirect.jl b/src/spatial/directcrdirect.jl index bc15eb8fc..7c6677642 100644 --- a/src/spatial/directcrdirect.jl +++ b/src/spatial/directcrdirect.jl @@ -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 From f490e93005777980dec2514a0e377a5831919298 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Tue, 10 Nov 2026 02:45:46 -0500 Subject: [PATCH 2/2] Add Float32/BigFloat priority-table regression tests (issue #674) Cover Float32 PriorityTable and PriorityTimeTable behavior, the aggregator-style table construction (maxpriority below eps) that hung pre-fix, full Float32 solves for Direct/DirectCR/RSSACR/CCNRM, and BigFloat-tspan solves for DirectCR/RSSACR/CCNRM. Co-Authored-By: Chris Rackauckas Co-Authored-By: Muse Agent-Harness: Muse CLI Code Agent-Model: muse-ember-1.0 Agent-Session: local job 674-muse (no session URL) --- test/table_test.jl | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/test/table_test.jl b/test/table_test.jl index 1cf01f4c1..278d4792b 100644 --- a/test/table_test.jl +++ b/test/table_test.jl @@ -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