Skip to content

tolerance table: an event's assigned value is differenced over kAssignedDifferenceStep, and a ratchet keeps the table whole (#2073, #2045) - #2151

Merged
wshlavacek merged 4 commits into
mainfrom
chore/2073-2045-tolerance-ratchet
Dec 4, 2026

Conversation

@wshlavacek

Copy link
Copy Markdown
Collaborator

Closes #2073. Closes #2045. Both are phase 1 steps of #1781 (1.6 and 1.3).

#2073: an event's assigned value is differenced over kAssignedDifferenceStep

In apply_event_sensitivity_jump (src/cvode_simulator.cpp), the three difference steps of an event's assigned value were written as the literal 1e-6: the step for a species the value reads, for a parameter, and for the fire time. They now read kAssignedDifferenceStep, which is 1e-6. Before this, that constant was read only by the smooth test's rounding cap, so the constant named the step but did not set it. The two 1e-6 * fabs at the trigger's own ∂g/∂x and ∂g/∂p (lines 9463 and 9547) are outside the issue and were left as they are.

No result changes. cvode_simulator.cpp was compiled at origin/main and at the branch with the build's own flags (taken from ninja -t commands, Apple clang, x86-64), and cmp finds the two object files identical.

The pin was measured, not read. I moved the constant and ran the two event test files:

kAssignedDifferenceStep tests that fail
2e-7 10, including the sawtooth cases [a-species-1] and [a-parameter-1]
5e-7 7, including test_event_assignment_derivative_step.py::test_a_smooth_value_that_curves_inside_every_wide_step
1e-6 (shipped) 0
2e-6 2 (test_a_difference_that_rounds_by_more_than_it_resolves_is_refused[0.37] and [2.0])
1e-5 5

So the constant is pinned both ways, to (5e-7, 2e-6), within a factor of two each way.

docs/development/tolerance-table.md:

#2045: a ratchet keeps the tolerance table whole

python/tests/test_tolerance_table_ratchet.py runs in every PR's suite (python-tests.yml has no paths filter). It reads every constexpr constant named k… in the integrator's sources: src/cvode_simulator.cpp, src/crossing_record.hpp and include/bngsim/nonneg_clamp.hpp. It fails on:

  • such a constant with no row, or with a number of rows that differs from its declarations (kGolden is declared twice, kMaxNamed five times);
  • a row naming a test that does not exist, or a name that is not a test's (file.py::Class::test, ::test in the file named before it, tests/file.cpp::test);
  • a row that says pinned or reached only and names no test;
  • a row whose constant is gone and that is not marked removed. A removed constant keeps its row as `kName` (removed in PR #N);
  • a constant whose row does not say pinned, unless it is one of NOT_YET_PINNED. That list holds the 45 that had no pinning test when the ratchet landed, and it only shrinks: a new constant lands with a test that pins it;
  • a file under src/ or include/ that declares such a constant and is neither one of the integrator's sources nor listed in OUTSIDE_THE_INTEGRATOR with its reason. Phase 3 builds its engine in new files, and a new file is not a way around the table;
  • a row it cannot read.

Wider than the issue's letter. #2045 asks for the constexpr doubles. The table counts every constexpr … k… declaration (107 at 1627a15, 72 of them doubles), and phase 3's new numbers include counts (D6's K), so the ratchet counts every type. Every one of those constants already had its row.

Its own tests show each failure on a doctored copy of the tree or the table: a constant added without a row, in eleven forms (a new double, an array, const before or after the type, a second declarator, a third kGolden, an int, a std::size_t in braces, a std::array, an attribute with a typedef, k2Pi). Also:

  • a constant in a new engine file, and a new file outside both lists;
  • a misspelled test, a missing file, a method named as a function, a missing C++ test, a helper named as a test;
  • a removed constant with its row unmarked, then marked, then marked while it is still there;
  • one of kGolden's two declarations removed;
  • a kMaxNamed declaration gone;
  • a reached-only new constant, and a pinned row that names no test;
  • NOT_YET_PINNED growing;
  • a row naming the wrong file;
  • four unreadable rows and two path-prefixed references.

Comments, strings (prefixed and raw included), digit separators, constexpr functions, if constexpr and constexpr lambdas are shown not to count. The size and file-size ratchets are phase 4's (#2068) and are not touched.

What I changed in the table so a test can read it

  • Wrong test paths fixed (a class prefix was missing):
    • test_event_sensitivity.py::TestStateDependentTrigger::…;
    • TestABystanderDoesNotHideAJump:: in the rows of kStateSwitchAgreeRoundoff (twice), kStateSwitchLandingSteps, kStateSwitchRememberSteps and kStateSwitchRootSlack;
    • ::TestWhatIsMergedIsOneCrossingTime::test_coincident_crossings_that_move_apart_are_refused;
    • ::TestTheRealModels::test_the_floor_does_not_clear_a_structural_failure.
  • "The test above" replaced by the test's name in the rows of kFarStartFloor, kAtPole, kEventJumpUlps, kSensFloorTauFraction, kStateSwitchNudgeTries, kStateSwitchResolveRefinements, kGolden (21823), ladder_first and ladder_ratio.
  • Forms a reader could not follow, rewritten:
    • kTermEps names tests/test_crossing_record.cpp::test_… and ::test_…;
    • BNGSIM_SENS_PBAR lists its four test_pbar_unit_* tests by name;
    • BNGSIM_NO_WARM_CVODE names test_warm_cvode.py::TestWarmColdParity.
  • "How to read a row" now explains the ::Class::name and C++ forms, and gains a Removed bullet.
  • "Keeping it current" describes the ratchet.
  • "Named constants outside the count" now says that the integrator's dense and colored Jacobians read kFdJumpRatio, kFdJumpNoise and kFdJumpParked from fd_jacobian.hpp, which have no rows. Whether they should is tolerance table: the integrator's difference-quotient Jacobians read three tuning constants of fd_jacobian.hpp with no rows #2150, filed during this work.
  • British spellings fixed: judgement, neighbours, neighbourhood, behaviour (twice), cancelled, cancelling.

Review

By AI models, two independent reviews of the whole branch.

  1. /code-review (Claude Opus 5.5) at high effort, one round, at e150a82. Five findings, all fixed in 012debc, each with a test that fails without its fix:

  2. Fable 5.2 at medium effort, one round, at e150a82. It was given the diff, the two issues and CONTRIBUTING's review prompt, not my conclusions. Three findings:

    • A new non-double constexpr k… (an int count, a std::size_t, a std::array) passed with no row. The docstring also wrongly said the table does not count them. Fixed in 2dc17f8: the ratchet counts every type. 13 counts without a pinning test join NOT_YET_PINNED, and nine files of non-integrator constants join OUTSIDE_THE_INTEGRATOR, each with its reason.
    • The kGolden removal above, fixed in 012debc.
    • The path-prefixed test above, fixed in 012debc. The form with no :: (python/tests/test_x.py) was fixed in 2dc17f8.

    Its notes, also fixed in 2dc17f8:

    • a constexpr double function followed by a k… name could be read as a constant;
    • constexpr double const kX, constexpr [[maybe_unused]] double kX, constexpr sunrealtype kX and k2Pi passed with no row;
    • a named "test" could be a helper. A name must now begin with test or Test.

Commits after the last round of either review: 012debc (the five /code-review fixes, plus two of Fable's) and 2dc17f8 (counting every type, the head reader, and requiring a named test to be a test). Each change in them comes with a test that fails without it.

Left alone

  • A row deleted together with its constant leaves nothing to compare, so the ratchet cannot see it. That stays with review.
  • Parametrized ids ([b=0.1]) are not checked, only the test they belong to.
  • Forms not counted: a tuning number written as a const, a macro or a bare literal, or as a direct-initialized constexpr double kX(1.0), which reads as a function. None of these is in the table's count either. One const (kMinAtolS) has a row by hand.
  • The three jump-mend constants of fd_jacobian.hpp have no rows. This is a scope decision, filed as tolerance table: the integrator's difference-quotient Jacobians read three tuning constants of fd_jacobian.hpp with no rows #2150.
  • Row line numbers are at 1627a15, as the table says, and are not checked: checking them would fail every PR that moves a line of the file.

Testing

  • Named in #1781 phase 1.6: make the three difference steps of an event's assigned value read kAssignedDifferenceStep #2073: test_event_assignment_derivative_step.py, test_event_value_time_step_sensitivity.py, and the source-census tests that read cvode_simulator.cpp as text. Those are test_jacobian_discontinuous_fallback, test_sensitivity_refusals_during_a_run, test_messages_carry_no_issue_references, test_windows_klu_ci_coverage, test_ci_source_pairing, test_changelog_fragments, test_codegen_jacobian, test_model_codegen_sensitivity, test_rhs_nonneg_clamp, test_sbml_event_chatter_biomd711, test_sbml_event_triggered_events, test_solver_stats_sens_counters and test_expression_parser_thread_safety. All 405 tests passed on the rebuilt branch.
  • test_tolerance_table_ratchet.py: 35 passed in about 7 s. test_ci_run_list_coverage.py and test_skip_audit.py pass with it.
  • pre-commit run on the changed files, ci/changelog.py check, and sphinx -W on the docs all pass.
  • The pre-push suite (ci/pre_push_pytest.py) passed at 2dc17f8. The SSH connection to GitHub was reset during that run, so the same commit, with the tree unchanged, was pushed again with --no-verify.

benchmarks/perf_ab.py (origin/main vs HEAD)

Run on an idle Mac mini (macOS 15.7, x86-64, 6 CPUs), 5 repeats plus a warmup, 3 warm runs each, wheels built from origin/main (4d590ea) and HEAD (2dc17f8). The 28 models of benchmarks/_dev/suite_ode.json range from 2 to 3,744 species.

workload geometric-mean warm_sec ratio geometric-mean cold_sec ratio solver work
ode 0.991 0.990 same on all 28 models
codegen 0.988 1.000 same on all 28 models

No significant slowdown: no model is flagged. That is expected, since the object file is byte-identical.

…nceStep (#2073)

The three central-difference steps of an event's assigned value, in a
species the value reads, in a parameter and in the time, were written as
the literal 1e-6. kAssignedDifferenceStep, declared for them, was read
only by the smooth test's rounding cap, which is worked out from that
step, so a change to the constant would have moved the cap and not the
steps. The steps read the constant now.

No value changes. With the build's flags, src/cvode_simulator.cpp
compiles to the same object file as on origin/main, byte for byte
(Apple clang, x86-64).

The tolerance table's row for the constant now describes the step it
sets, and its pin is measured rather than read. Moved to 5e-7, seven
tests of the two event-assignment files fail; at 2e-6, two
(test_a_difference_that_rounds_by_more_than_it_resolves_is_refused at
both); at 2e-7, ten; at 1e-5, five. So it is pinned both ways within a
factor of two, and the counts under "What the table shows" move by one.
python/tests/test_tolerance_table_ratchet.py reads every constexpr
double named k... in the integrator's sources (cvode_simulator.cpp, the
new engine's crossing_record.hpp, and nonneg_clamp.hpp, whose constant
the table lists) and fails, in every PR's suite, on:

- such a constant with no row in docs/development/tolerance-table.md,
  or with a number of rows other than its declarations;
- a row that names a test that does not exist, or that says **pinned**
  or **reached only** and names none in its own cell;
- a row whose constant is gone and that is not marked removed, and a
  row marked removed whose constant is still there;
- a constant whose row does not say **pinned**, unless it is one of
  NOT_YET_PINNED, the 32 that had no pinning test when this landed;
- a file under src/ or include/ that declares such a constant and is
  neither one of the integrator's sources nor listed, with why, in
  OUTSIDE_THE_INTEGRATOR;
- a row it cannot read.

Its own tests show each failure, starting with a constant added without
a row.

The table did not read cleanly, so it is fixed rather than the reader
loosened. Seven rows named eight tests by a path that does not exist,
most of them a method of a class written as a top-level test; nine said
**pinned** or **reached only** and named their test only by pointing at
another row ("the test above") or describing it; the kTermEps row, the BNGSIM_SENS_PBAR row and the
BNGSIM_NO_WARM_CVODE row named tests in forms a reader cannot resolve
(a bare name, a glob, a bare class). How to read a row now says how a
test is named and how a removed constant's row is marked, and "Keeping
it current" says what the ratchet checks.

The table also said kClampNoiseAtols was the one constant the file
reads from a header. The integrator's two difference-quotient
Jacobians also read three of fd_jacobian.hpp's (the jump mend); the
table now says so, and #2150 asks whether they should have rows. British
spellings in the table are made American.
A row marked removed is an error only when no live row of its name is
left, so one of kGolden's two declarations can go with its row marked
removed. A test named with a path before its file is reported rather
than passed over, prefixed literals are blanked, a row may omit its
closing pipe, and the source tree is found by bngsim_source_root().
…2045)

The table counts every constexpr named k..., the counts and caps among
them (107 declarations, 72 doubles), and phase 3's numbers include
counts, so a new constexpr int or std::array with no row now fails as a
double does. A declaration's head is read to its first = { [ , ; or (,
so a constexpr function, a constructor and if constexpr are not read as
constants. Thirteen counts without a pinning test join NOT_YET_PINNED,
nine files of non-integrator constants join OUTSIDE_THE_INTEGRATOR, and
a named test must be one: a name beginning with test or Test.
@wshlavacek
wshlavacek merged commit 49c1979 into main Dec 4, 2026
27 checks passed
@wshlavacek
wshlavacek deleted the chore/2073-2045-tolerance-ratchet branch December 4, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant