Skip to content

Gillespie timedep#1334

Merged
luciansmith merged 6 commits into
developfrom
gillespie-timedep
Jul 3, 2026
Merged

Gillespie timedep#1334
luciansmith merged 6 commits into
developfrom
gillespie-timedep

Conversation

@luciansmith

Copy link
Copy Markdown

Slightly altered fix for #1318.

wshlavacek and others added 6 commits June 6, 2026 20:44
…y on time.

Fixes #1318. The direct method samples the waiting time from a propensity it
treats as constant until the next reaction, which is only correct when the rate
laws are time-homogeneous. When a rate depends explicitly on time (directly, or
through a time-dependent assignment or rate rule), the SSA mean drifts off the
ODE: a rate that is zero at t=0 froze the first reporting interval, and coarse
output intervals trailed the true mean by about one interval.

Now we check once, on the first step, whether any rate varies with time (the
detection adds no random draws, so time-homogeneous models keep the original code
path and produce bit-identical results). When a rate does depend on time, the
waiting time tau satisfies integral over [t, t+tau] of s(u) du = -log(r1), where
s is the total propensity re-evaluated as time advances; we accumulate that
integral with an adaptive trapezoidal sub-step and select the reaction using the
propensities at the firing time. This is the direct-method form of the
integrated-propensity representation in D. F. Anderson, J. Chem. Phys. 127,
214107 (2007). The representation is exact; this implementation realizes it up to
the quadrature error of the sub-step, so it is exact in the limit of small steps
and converges as the tolerance is tightened, not bit-exact at a finite tolerance.

Adds regression test TimeDependentPropensityMatchesODE, an immigration-death
process with a time-dependent immigration rate and a closed-form ODE mean. The
time-homogeneous path is byte-for-byte identical to before, and the stochastic
test suite is unaffected (its rate laws are all time-homogeneous; the CSymbolTime
models use time only in event triggers).

The time-dependence check is a value-based heuristic; inspecting the rate-law
expression trees for the time symbol would be more robust, which is noted in the
code.
…rules

The timeDependentRates comment claimed the fix handles time entering
through a rate rule. It does not: Gillespie freezes the state vector
between reaction events and never integrates a rate-rule variable, so
setTime() alone does not advance it and it is neither detected nor
tracked by the propensity integral. Drop the rate-rule claim and state
the limitation explicitly. Comment-only; no behavior change.
Same fix as the header comment, in the duplicate that sat in the .cpp.
A rate-rule variable is part of the frozen state, so it does not vary
across the detection probes; time dependence entering through a rate
rule is not detected (and not handled) here. Comment-only.
* Actually check for the 'time' csymbol instead of integrating.
* Make the comments less voluminous.
* Update dependencies, since VS updated on github.
The latest MSVC seems to only use <random>, not <tr1/random>.  Or something like that.
@luciansmith luciansmith merged commit 926191d into develop Jul 3, 2026
9 checks passed
@luciansmith luciansmith deleted the gillespie-timedep branch July 3, 2026 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants