run_diffrax steps over a narrow Gaussian pulse of the clock: X(100) = 0 for 100 at every rtol, in 7 steps, with no warning (it places none of the #1286 smooth-pulse stops Simulator.run uses)

Open Beginner friendly
#1,795 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Read python/bngsim/_diffrax_solver.py around _switch_times (lines 384–452) and compare it with how Simulator.run uses smooth_pulse_stops in _simulator.py around lines 2445–2452. First inspect python/bngsim/_smooth_pulses.py around line 181 to see how pulse stop times are produced. Done means run_diffrax includes those stops and the supplied reproduction returns X(100) close to 100.

Written by the indexing model from the issue text.

Description

bug correctness concern

run_diffrax steps right over a narrow Gaussian pulse of the clock. The model makes X at kx/(s*sqrt(pi))*exp(-((time()-c)/s)^2), a pulse of area kx = 100 centred at t = 50, so X(100) = 100 exactly.

  • Simulator.run: 100.00003. It has placed smooth-pulse stops since #1286.
  • run_diffrax: 0.0, in 7 steps, at rtol 1e-6, 1e-8 and 1e-10, for pulse widths s = 0.05 and s = 0.5. No warning.

Found by checking a lead of #1532 (sibling of #902). Reproduced on main 5d7a5318 (editable build, macOS arm64, Python 3.13), with jax 0.9.0.1 and diffrax 0.7.2.

Repro

"""run_diffrax and a narrow Gaussian pulse of the clock. Needs the jax extra (jax, diffrax).
0 -> X at kx/(s*sqrt(pi))*exp(-((time()-c)/s)^2): the pulse's area is kx, so X(100) = kx = 100 exactly."""
import contextlib, io, os, tempfile, warnings
import numpy as np
import bngsim
from bngsim._diffrax_solver import run_diffrax

NET = """begin parameters
    1 kx 100
    2 c  50
    3 s  {s!r}
end parameters
begin functions
    1 f() kx/(s*sqrt(_pi))*exp(-((time()-c)/s)^2)
end functions
begin species
    1 X() 0
end species
begin reactions
    1 0 1 f #_FUNC
end reactions
begin groups
    1 Xobs 1
end groups
"""
def model(s):
    p = os.path.join(tempfile.mkdtemp(), "m.net")
    with open(p, "w") as fh:
        fh.write(NET.format(s=s))
    with contextlib.redirect_stderr(io.StringIO()):
        return bngsim.Model.from_net(p)
for s in (0.05, 0.5):
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        r = bngsim.Simulator(model(s), method="ode").run(t_span=(0, 100), n_points=3)
        print(f"s={s}: Simulator.run X(100) = {float(np.asarray(r.species)[-1, 0]):.6f}")
        for rtol in (1e-6, 1e-8, 1e-10):
            d = run_diffrax(model(s), t_start=0.0, t_end=100.0, n_points=3, rtol=rtol, atol=rtol)
            print(f"  run_diffrax rtol={rtol:g}: X(100) = {float(np.asarray(d['species'])[-1, 0]):.6f}  n_steps={d['n_steps']}")
    print(f"  exact 100; warnings={len(w)}")

Output

s=0.05: Simulator.run X(100) = 100.000027
  run_diffrax rtol=1e-06: X(100) = 0.000000  n_steps=7
  run_diffrax rtol=1e-08: X(100) = 0.000000  n_steps=7
  run_diffrax rtol=1e-10: X(100) = 0.000000  n_steps=7
  exact 100; warnings=0
s=0.5: Simulator.run X(100) = 100.000018
  run_diffrax rtol=1e-06: X(100) = 0.000000  n_steps=7
  run_diffrax rtol=1e-08: X(100) = 0.000000  n_steps=7
  run_diffrax rtol=1e-10: X(100) = 0.000000  n_steps=7
  exact 100; warnings=0

Where

_switch_times (python/bngsim/_diffrax_solver.py:384-452) places stops only from time_discontinuity_conditions() through crossings_with_periodic. It never calls smooth_pulse_stops (python/bngsim/_smooth_pulses.py:181), which Simulator.run calls (_simulator.py ~2445-2452). A Gaussian has no condition, so the diffrax path gets no stop at all.

Fix direction: add the smooth_pulse_stops times to the diffrax stop set the same way Simulator.run does.

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.