Repository navigation
Stop conditions end the run where they hold (#2018) - #2148
Merged
Merged
Conversation
…n output (#2018) A strict condition whose root CVODE landed on an output time exactly was not met there, and the warm path asked CVODE for the same output again, which returned the same root, for ever; the cold path skipped it and ended at the next output. A root on the output is now the output's row, and the time a few roundings past it stands in for the row up to the next output. New tests in test_stop_condition_during_the_run.py (34 of 53 fail on main; the rest are controls). Docs, CLI help, the add_stop_condition docstring and the stubs say where a run ends.
#2018) - A run whose rows a state-switch re-take can record again (an ODE run of a .net model with a state switch) asks its conditions of the result alone: a stop on a row the re-take later rewrote returned Y = 9.4e-134 where the run holds 0.0063. - A run integrated on the time since a start far from zero drops the stop roots that read the time and asks those conditions at its rows: the root there was quantized and CVODE failed with CV_ILL_INPUT at 1e12. - The engine decides a comparison it can compute as numpy does (no power of a column) without calling Python at each row; Python is asked where one may hold. A condition never met cost 2 us a row, 0.2 s over 1e5 rows. - Constants that read no column are folded as Python computes them. - The SSA run's clock write-back drops a branch that could not change it. - Tests for each, and for the rows before the first and after the last sample time, PSA integrals and rate-rule targets at a stop, a far-start stochastic clock, and the safety net on run_batch and run_replicates.
…t holds (#2018) Two screen mutants left every result array right: a NaN read as not met (inf >= inf), and a folded constant reckoned in doubles (2**53 + 1 - 2**53). The engine then ran past the stop and the result was cut after the run, leaving the clock at the end of the span. The test now holds the clock at the stop and asks that Python saw the row.
…2018) A stop root's g of exactly 0 now reads as the smallest number on the side the condition is on at equality, so CVODE sees g change sign where the condition does and returns the root where it already holds. The probe past the root is gone. A conserved total compared with its own value no longer parks g at 0, which CVODE refused to go on from (CV_ILL_INPUT). A crossing at the instant an event's trigger rises is not taken as the stop: the row there holds the state after the event. The condition is asked at the next row recorded, as on main. A crossing on the last sample of a longer span belongs to no reported row and is not a stop. run_batch and run_replicates take a row's early end for a stop only where the engine was given conditions to ask, as run() does. Docs: a callable beside an expression is asked of the result up to the stop; conditions that only rounding decides; a crossing at an event. Run-time messages cite no issue numbers.
…ety net (#2018) run_batch and run_replicates take any short row for a stop, as before: a row ends short only on a stop or a steady state (a timeout raises), so a short row no condition holds at is a bug, and the check says so rather than returning the row.
…2018) A run far from zero keeps its own clock, and a stop root lies past the row before it by about its root tolerance. Moved back to the caller's clock it can round onto that row's time: from 1e9, A = t crossing 5 on the sample at +5 returned a second row at 1e9 + 5, and a condition met just past the start returned two rows at the start with the clock left there. Such a root is now declined, and the condition is asked at the next sample, the row main stopped at. Tests for that, and for a strict condition on its threshold at the start, which now ends the run within the root tolerance of the start. The user guide says both, and that a delayed event is not looked ahead to. The changelog no longer says a condition only rounding decides gives main's run. Two stale comments; a run-time message no longer cites the issue.
MSVC failed on the stop check with an internal compiler error (C1001) at the std::any_of whose generic lambda read a thread_local declared in the check's own lambda; every Windows leg stopped there. The screen loop is now a plain function holding its own thread_local stack, and asks the screens in the same order, stopping at the first that may hold. The least normal double at a stop root's side is written (std::numeric_limits<double>::min)(), safe from a min macro.
Collaborator
Author
|
The macos-15 legs of python-tests have had no runner for over an hour, so I ran them here on an arm64 Mac (macOS 27.2) in their place, the way the workflow runs them. Head 047ba77, which is this branch merged with main at 49c1979. Built with
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2018.
The bug
A stop condition was asked of the result only after the run had gone to its end time. Two things followed.
A' = A²from A = 1 passes 10 at t = 0.9 and blows up at t = 1. Withadd_stop_condition("Atot > 10")over (0, 2), main raisedSimulationErrorat t = 1. The branch returnsStopConditionMetat t = 0.8999992701816443 (91 rows).A' = -AtoAtot < 1e-3over (0, 1e6): main took 189 steps and reported the stop at the sample t = 7.0. The branch takes 108 steps and stops at the crossing, t = 6.90777.Atot > 1e4over (0, 30) ran to its timeout on main. On the branch it ends at t ≈ 7.The fix
lhs < rhs(or<=,>,>=), or a bare number that is met where > 0, is also a CVODE root function. The run then ends at the crossing, not at the next sample.current_timeat its time. On main it was left at the end of the span.Simulator._stoppedasks the result rows after the run. If the engine stopped but no condition holds at any row the result reports, the run raises instead of returning a run cut short.run_batch,run_replicatesandparameter_scancut rows the same way.Still asked of the whole result after the run, as on main:
.netrate laws that switch on the state).A condition that never holds gives the same run as main, bit for bit. The exceptions, both under Left alone: a comparison only rounding decides, which the branch can find met between samples, and a threshold met at the instant an event fires.
Review
Found while building the branch, fixed in it:
.netrate law that switches on the state re-takes the step that crossed the switch, and records the rows inside it again. A condition that held only on the first reading (Y = 9.4e-134, later 0.0063) stopped the run on a row the result then rewrote. Such runs now ask their conditions after the run, as main did.Review 1 (whole branch), three defects, all fixed:
sample_times=[1, 5]over (0, 10) witht > 5: the run ended on a row at 5.000000000000018 the caller never asked for, in place of the 5.0 row. Main ran it whole. Cause: an exact zero at a root was asked just past it, and that probe stood in for an unreported row.at (A > 5): A = 0with stopA > 5: the stop root ended the row, the row's read-ahead then fired the event, and the recorded row no longer held the condition. Main returned the whole run.Tot < 10, Tot = 10 conserved: g sat at 0 to rounding, and CVODE refused to go on ("root found at and very near t").>: not met;>=: met). CVODE then sees g change sign exactly where the condition does, and returns a root where it already holds. g is never parked at 0, and there is no probe past the root. A stop at a root is not taken where an event's trigger rises there: the row would hold the state after the event, so the condition is asked at the next row recorded, as on main.kernel.pycomment that still said the whole interval is integrated was corrected.test_messages_carry_no_issue_referencesrefuses. Reworded.Review 2 (the fixes after review 1): F1, F2 and F3 rerun on the new build, all fixed. An event fuzz of 6,336 event × condition × sample-grid cases (1,584 more with sensitivities) found no new error, no stop later than main's, and no stop on a row the condition does not hold at. Sensitivities at the last row agree with main to 9e-14. 435 never-met runs are bit-identical. Batch and scan rows equal the single runs. It found:
A_tot > 5crosses on the sample at +5, and returned a second row at 1e9 + 5 with A = 5.000000000000057. A condition met just past the start returned two rows at the start, and the clock did not move. The run integrates on the time since its start, and a root one root tolerance past the row rounds onto the row's time on the caller's clock. Fixed: such a root is declined, and the condition is asked at the next sample (+6, +1), the row main stopped at. The same holds throughrun_batchandparameter_scan.Ctot > 0with C made from 0 stops at t = 7.6e-25; main stopped at the sample at t = 1. That is the crossing. It is now tested, and the user guide says so.Tot < 10with Tot conserved at 10, over (0, 2000) with 11 samples: the branch stops at t = 5.3e-5, where Tot = 9.999999999999998. Main ran whole, and on a grid of 2,001 samples it stops too (t = 7). The row the branch stops on does hold the condition. The changelog said a condition that never holds gives main's run; it now names this case, and the user guide says to compare with a margin.CI (Windows): every Windows leg failed to build with an MSVC internal compiler error (C1001) at the screen in
make_stop_check: a generic lambda passed tostd::any_ofread athread_localdeclared inside the check's own lambda. The screen loop is now a plain function that owns itsthread_localstack and asks the screens in the same order. A stop root's least normal double is written(std::numeric_limits<double>::min)(), so a Windowsminmacro cannot touch it. The stop-condition test files pass unchanged (853 tests).Left alone
at (A < 5): A = 20with stopA < 5), CVODE locates the event from the stop root's return rather than from the step. The event time moves within CVODE's root tolerance: the species differ from main by up to 1.8e-13 absolute on the tests' two models, and by up to 2.6e-12 absolute (1.9e-12 relative) over review 2's fuzz. Where an event freezes the compared quantity on its threshold (at (A > 5 - 1e-13): k = 0with stopA > 5), the side rounding leaves it on decides the stop. In 34 such fuzz cases main stopped (A = 5.000000000000063) and the branch did not (4.999999999999904). An event that does not coincide with a crossing, and a condition that never crosses, give main's run bit for bit.-> Aat rate 1 withat 1 after (time > 4): A = 0and stopA > 5: the trigger is located up to a root tolerance late, so A passes 5 about 1e-13 before the reset. The branch stops there (t = 5.000000000000044, A = 5.000000000000043). Main ran whole, because none of its samples lands in that window. A sample there would record the same state: main also records a row just before a delayed execution with the state before it. Whether such a run stops is decided at the root tolerance (10 cases in review 2's fuzz). The user guide says so.Tot > 10stopped at t = 0.5 on main). The branch also asks between samples, so it can stop earlier. Documented; no tolerance band.run_batchrebuilds the condition's name map for each row (in_run_stopswalks every species). The whole stop overhead of a 200-row batch was about 50 us per row.Ctot > 2thenAtot.nope > 2): every condition is asked at each row, so the broken one raisesUsageErrorat t = 0. Main stopped on the first at t = 3..netruns that re-take rows at a state switch, conditions on remapped species, and conditions on the time in a run started far from zero (asked at the samples).Tests
python/tests/test_stop_condition_during_the_run.py, 154 tests: 114 fail on main. The 40 that pass on main are controls, and their docstrings say so (11 test functions).Mutation checks
Each mutant was switched on by an environment variable in a separate copy of the package; the source was then restored and the rebuilt extension checked byte-identical.
set_stop_roots, the warm path's loop guard). Ten are caught; two needed new tests (a trigger already true at the crossing; a root on an output the check declines, through the engine's own options). Two remain:>=for>; compared with the start alone; compared with the row's own time; compared on the run's clock). All five are caught. The last two decline every far root, and the existing far-start crossing test catches them.Corpus A/B
824 models (224
.netand 600 SBML from the parity corpora), each run plain and with two never-met conditions (time < -1, andX > 1e300for the model's first observable X), on main and on the branch. ODE (0, 10), 21 points: 809 of 809 runs bit-identical to main, both ways. SSA (0, 1), 11 points, seed 7: 223 of 223 bit-identical; two runs hit the 60 s timeout on both builds, and their messages differ only in the elapsed time they print. The A/B ran on the code before review 2. The one engine change since then acts only when a stop root is found in a run started far from zero, and none of these runs has either.Cost
EGFR (Blinov 2006
.net), ODE over (0, 100) with 100,001 rows, 555 steps; best of 3, two runs each agreeing to 2 ms:X > 1e300)With no condition, nothing changes. A condition never met costs about 0.6 us per row more than main's check after the run. That cost is CVODE evaluating the stop root at each output it returns to; the per-row check itself costs no more than main's, because the engine's screen keeps Python out of it. A condition met early costs the short run (see The bug).
Local runs
On the head 047ba77 (the branch merged with main at 49c1979), macOS arm64, built as CI's macos-15 leg builds it (MIR on, KLU required; HAS_KLU, HAS_MIR, HAS_LAPACK_DENSE and HAS_NFSIM true),
BNGSIM_SKIP_AUDIT=strict pytest -p no:cacheprovider python/tests -n auto --dist worksteal:ci/test_file_counts.py compare, rc 0).These stand in for the macos-15 legs while GitHub has no macOS runner for them (see the PR comment).