Skip to content

Commit

Permalink
timegroup fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vyudu committed Oct 16, 2024
1 parent 8d3d33d commit 4045437
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/aggregators/ccnrm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# algorithm with optimal binning, Journal of Chemical Physics 143, 074108
# (2015). doi: 10.1063/1.4928635.

const BINWIDTH_OVER_AVGTIME = 16

mutable struct CCNRMJumpAggregation{T, S, F1, F2, RNG, DEPGR, PT} <:
AbstractSSAJumpAggregator{T, S, F1, F2, RNG}
next_jump::Int
Expand Down Expand Up @@ -157,8 +155,8 @@ function initialize_rates_and_times!(p::CCNRMJumpAggregation, u, params, t)
end

# Build the priority time table with the times and bin width.
binwidth = BINWIDTH_OVER_AVGTIME / sum(cur_rates)
timestep = sum(cur_rates)
p.ptt.times = pttdata
rebuild!(p.ptt, t, binwidth)
rebuild!(p.ptt, t, timestep)
nothing
end
19 changes: 11 additions & 8 deletions src/aggregators/prioritytable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ mutable struct PriorityTimeTable{T, F <: Int}
minbin::F
steps::F # TODO: For adaptive rebuilding.
maxtime::T
binwidthconst::F
numbinsconst::F
end

# Construct the time table with the default optimal bin width and number of bins.
Expand All @@ -356,14 +358,15 @@ function PriorityTimeTable(
groups = Vector{PriorityGroup{ptype, Vector{pidtype}}}()
pidtogroup = Vector{Tuple{Int, Int}}(undef, length(times))

ttgdata = TimeGrouper{ptype}(mintime, timestep)
ttgdata = TimeGrouper{ptype}(mintime, binwidth)
# Create the groups, [t_min, t_min + τ), [t_min + τ, t_min + 2τ)...
for i in 1:numbins
push!(groups, PriorityGroup{pidtype}(mintime + i * timestep))
end

ptt = PriorityTimeTable(
groups, pidtogroup, times, ttgdata, zero(pidtype), zero(pidtype), maxtime)
groups, pidtogroup, times, ttgdata, zero(pidtype),
zero(pidtype), maxtime, binwidthconst, numbinsconst)
# Insert priority ids into the groups
for (pid, time) in enumerate(times)
if time > maxtime
Expand All @@ -381,13 +384,13 @@ end
# Rebuild the table when there are no more reaction times within the current
# time window.
function rebuild!(ptt::PriorityTimeTable{T, F}, mintime, timestep) where {T, F}
@unpack pidtogroup, groups, times, timegrouper = ptt
@unpack pidtogroup, groups, times, timegrouper, binwidthconst, numbinsconst = ptt
fill!(pidtogroup, (zero(F), zero(F)))

numbins = length(groups)
ptt.maxtime = mintime + numbins * timestep
timegrouper.mintime = mintime
timegrouper.timestep = timestep
binwidth = binwidthconst * timestep
ptt.maxtime = mintime + numbins * binwidth
timegrouper = TimeGrouper(mintime, binwidth)

groupmaxtime = mintime
for group in groups
Expand All @@ -404,7 +407,7 @@ function rebuild!(ptt::PriorityTimeTable{T, F}, mintime, timestep) where {T, F}
ptt.minbin = findfirst(g -> g.numpids > (0), groups)
ptt.minbin === nothing && (ptt.minbin = 0)
ptt.steps = 0

return nothing
end

Expand Down Expand Up @@ -440,7 +443,7 @@ function insert!(ptt::PriorityTimeTable, pid, time)
gid = timegrouper(time)
@inbounds pididx = insert!(groups[gid], pid)
@inbounds pidtogroup[pid] = (gid, pididx)

return nothing
end

Expand Down

0 comments on commit 4045437

Please sign in to comment.