A switch late in a long ODE run is taken where the doubles are coarse: dX/dsigma 1.1% off over 2^40, 69% over 2^46 and 0 over 2^50, from 0, with no error

Open
#1,836 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start by reading the switch crossing and CVODE restart handling; the issue says this intersects the crossing work planned in PR #1781. Run the supplied reproduction script across the listed spans and compare the final X and sensitivity values with the exact values in the issue. Done means late switches retain accurate values and sensitivities without CVODE's lost-step warning reaching only stderr.

Written by the indexing model from the issue text.

Description

bug correctness concern

A switch late in a long ODE run is taken where the run's doubles are coarse, and the answer comes back wrong with no error. This happens on main, for a run from 0.

Model. A counter C' = 1 starts at 0.5, and X' = k (1 - X) once C >= sigma. With sigma = span - 2, the crossing is 2.5 before the end. The exact values are X(end) = 1 - e^-5 = 0.993262053 and dX(end)/dsigma = -k e^-5 = -0.013475894.

Results, rtol=1e-10, atol=1e-12, with rows at 0 and span, on bngsim 0.17.2 (main's install, 2026-11-20):

span 2^20: X(end) 0.993262053 (want 0.993262053)  dX/dsigma -0.013475894 (want -0.013475894, 1.7e-08 off)
span 2^30: X(end) 0.993261975 (want 0.993262053)  dX/dsigma -0.013476051 (want -0.013475894, 1.2e-05 off)
span 2^40: X(end) 0.993188470 (want 0.993262053)  dX/dsigma -0.013623060 (want -0.013475894, 1.1e-02 off)
span 2^46: X(end) 0.997905367 (want 0.993262053)  dX/dsigma -0.004189267 (want -0.013475894, 6.9e-01 off)
span 2^50: X(end) 1.000000000 (want 0.993262053)  dX/dsigma -0.000000000 (want -0.013475894, 1.0e+00 off)

Why. After the switch's stop, CVODE restarts with small steps. At t ≈ 2^40 the doubles are 2^-12 apart, and at 2^46 they are 2^-6 apart, so steps shorter than that spacing round. Over 2^50 the jump is lost entirely. CVODE says so on stderr ("t + h = t on the next step"), and nothing reaches the caller (#1837).

Scope. This applies to every long run with a late switch, from 0 or from far out.

  • PR lanl/bngsim#1753 integrates a far run of a model that reads no clock on its own clock, counted from its start. Its long runs inherit the same limitation, and its guide section says so.
  • The PR first counted runs below 0 from their end. That got late switches right but got the start wrong: dX/dsigma was -0.685 for -0.164 over 2^46 to -1e17.
  • No single clock origin is fine at both ends of a long run.

Possible fix. For a model that reads no clock, re-zero the clock at each switch's restart. Integrate each leg between stops on its own clock, carrying the state and the sensitivities across. Each restart then takes its small steps where the doubles are fine. This touches the crossing handling that lanl/bngsim#1781 plans to rebuild.

Repro script
"""A switch late in a long ODE run is taken where the doubles are coarse (bngsim main, a run from 0).

C counts time from 0.5 (C' = 1); X' = k (1 - X) once C >= sigma. sigma = span - 2,
so the crossing is 2.5 before the end, and X(end) = 1 - exp(-5),
dX(end)/dsigma = -k exp(-5).
"""
import math, sys, tempfile, warnings
from pathlib import Path
import numpy as np
import bngsim
warnings.simplefilter("ignore")
NET = """begin parameters
    1 c0     0.5
    2 sigma  %r
    3 k      2
    4 rc     1
end parameters
begin functions
    1 rate_X() if(t>=sigma,k*(1-x),0)
end functions
begin species
    1 C() c0
    2 X() 0
end species
begin reactions
    1 0 1 rc
    2 0 2 rate_X
end reactions
begin groups
    1 t 1
    2 x 2
end groups
"""
want_x, want_s = 1 - math.exp(-5.0), -2 * math.exp(-5.0)
p = Path(tempfile.mkdtemp()) / "late.net"
out = []
for e in (20, 30, 40, 46, 50):
    span = 2.0**e
    p.write_text(NET % (span - 2.0,))
    sim = bngsim.Simulator(bngsim.Model.from_net(str(p)), "ode", sensitivity_params=["sigma"])
    r = sim.run(sample_times=[0.0, span], rtol=1e-10, atol=1e-12)
    x = float(np.asarray(r.species)[-1, 1]); s = float(np.asarray(r.sensitivities)[-1, 1, 0])
    out.append(f"span 2^{e}: X(end) {x:.9f} (want {want_x:.9f})  dX/dsigma {s:.9f} (want {want_s:.9f}, {abs(s / want_s - 1):.1e} off)")
open(sys.argv[1], "w").write("\n".join(out) + "\n")

Run as python late_switch.py out.txt.

Dominant language
Python
Stars
3
Forks
6
Avg merge
1d 1h
Merged PRs (30d)
295

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lanl/bngsim

All issues in lanl/bngsim

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.