From ab5329b6dfa1927928408c1c649f9ac194b5230b Mon Sep 17 00:00:00 2001 From: Bill Hlavacek Date: Thu, 3 Dec 2026 19:19:05 -0700 Subject: [PATCH 1/3] steady_state: ask a waiting "residual" march's inputs where it stalls (#2074) A march under criterion="residual" that waits for masked inputs asks them by a Newton step only at doubling counts of the steps the kept species pass. One ended as stalled, by an integrator failure, or out of steps between two asks was not asked where it stopped, unlike the default criterion's march (#1001). It is now. The ask schedule is unchanged: asking at every step costs one dense factorization a step (260 s for 0.73 s at 500 unknowns) and converges the issue's ring-down only at the one step of 8,762 that passes. Refs #2074 --- changelog.d/2074.fixed.md | 1 + .../tests/test_steady_state_masked_inputs.py | 55 +++++++++++++++++++ src/steady_state.cpp | 16 +++++- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 changelog.d/2074.fixed.md diff --git a/changelog.d/2074.fixed.md b/changelog.d/2074.fixed.md new file mode 100644 index 000000000..bf9a7f1ad --- /dev/null +++ b/changelog.d/2074.fixed.md @@ -0,0 +1 @@ +- **`steady_state(mask=..., criterion="residual")` by integration came back unconverged where its march stalled within tolerance between two checks of the masked inputs ([#2074](https://github.com/lanl/bngsim/issues/2074)).** A march that waits for masked species the kept ones read checks those inputs by a Newton step only at the 1st, 2nd, 4th, ... step the kept species pass, and one called stalled (or ended by the integrator) between two such steps was not checked where it stopped. With the inputs at rest and a kept species coming within its tolerance at the 1,385th such step, a march stalled at the 2,005th came back unconverged 2e-9 from its steady state. It is now checked where it stops, as the default criterion's march is. The schedule of the checks is unchanged. diff --git a/python/tests/test_steady_state_masked_inputs.py b/python/tests/test_steady_state_masked_inputs.py index 0c90bc4cc..2c8304285 100644 --- a/python/tests/test_steady_state_masked_inputs.py +++ b/python/tests/test_steady_state_masked_inputs.py @@ -1017,6 +1017,61 @@ def test_a_network_that_does_not_settle_is_given_up_on_as_with_no_mask(): assert ss.n_steps < 10_000 +# I creeps to c = 1e12 at lam = 1e-10 from 1e-10 above it: its rate, 1e-8, is +# over tol and stays there, so its residual does not fall. X' = ks I/c - kd X +# reads I, X* = ks/kd = 2 from 1e-6 above it, its rate (1e-10) under tol from +# the start. U and V ring on at w and nothing reads them; they set the steps. +STALL_BETWEEN_ASKS = """begin parameters + 1 lam 1.0e-10 + 2 ks 1.0e-4 + 3 kd 5.0e-5 + 4 w 1.0e-3 + 5 c 1.0e12 +end parameters +begin functions + 1 fi() -lam*(Itot-c) + 2 fx() ks*Itot/c + 3 fu() -w*(Vtot-1) + 4 fv() w*(Utot-1) +end functions +begin species + 1 I() 1.0000000001e12 + 2 X() 2.000002 + 3 U() 2 + 4 V() 1 +end species +begin reactions + 1 0 1 fi + 2 0 2 fx + 3 2 0 kd + 4 0 3 fu + 5 0 4 fv +end reactions +begin groups + 1 Itot 1 + 2 Utot 3 + 3 Vtot 4 +end groups +""" + + +def test_a_march_that_waits_for_inputs_is_asked_where_it_stalls(tmp_path): + # Issue #2074. X passes from the first step and the inputs are asked at + # the 1st, 2nd, 4th, ... step it passes: the Newton step moves X by more + # than its tolerance up to the 1,385th (X 1e-8 above X*), and by less + # from there. I's residual does not fall, so the march is called stalled + # at the 2,005th, between the asks at the 1,024th and the 2,048th, and + # came back unconverged at X 2e-9 above X*. Asked where it stalled, as + # the default criterion's march is, it is converged there. + ss = _sim(tmp_path, STALL_BETWEEN_ASKS).steady_state( + mask=["X()"], method="integration", criterion="residual", max_time=1e12 + ) + assert ss.converged + assert 1_024 < ss.n_steps < 2_048 + assert ss.concentrations[0] == pytest.approx(1e12, rel=REL) + assert ss.concentrations[1] == pytest.approx(2.0, rel=REL) + + # Three pools as in _ring_beside_pools (kr = 7e4), X' = ks g S/(S + 1) - kd X # with g the mean of C/(K + C) over the pools, and E + S -> E with E at 0 and # only decaying: S stays at 3, and the rows of E and S in the Jacobian are diff --git a/src/steady_state.cpp b/src/steady_state.cpp index 754db6f71..65009f4e9 100644 --- a/src/steady_state.cpp +++ b/src/steady_state.cpp @@ -1571,6 +1571,8 @@ class SteadyStateMarcher { double best_inputs_resid = std::numeric_limits::infinity(); long inputs_steps = 0; long inputs_check_at = 1; + // Whether the inputs were asked at the state the march stops at. + bool inputs_asked = false; while (t_ < t_stop) { // `tout` is NOT the budget (issue #235). In CV_ONE_STEP CVODE returns @@ -1613,8 +1615,9 @@ class SteadyStateMarcher { // (ss_inputs_within_tolerance). Asked at the first such step and // then at each doubling of the count, as the criterion's step is // below: the test factors a Jacobian. - if (waits_for_inputs_ && resid < tol && !reached && - (++inputs_steps >= inputs_check_at || flag == CV_TSTOP_RETURN)) { + inputs_asked = waits_for_inputs_ && resid < tol && !reached && + (++inputs_steps >= inputs_check_at || flag == CV_TSTOP_RETURN); + if (inputs_asked) { inputs_check_at = 2 * inputs_steps; reached = ss_inputs_within_tolerance(model_, rhs_, opts_, sub_, y_data); } @@ -1760,6 +1763,7 @@ class SteadyStateMarcher { std::copy(dropped_at_.begin(), dropped_at_.end(), y_data); *residual_out = dropped_residual_; asked = false; + inputs_asked = false; if (!reinit_here()) { integrator_failed_ = march_failed_ = true; } @@ -1772,6 +1776,14 @@ class SteadyStateMarcher { if (by_distance_ && !asked) { converged = stops_at(y_data, *residual_out); } + // So is a march that waits for masked inputs (issue #2074), whose + // inputs are asked at the same doubling counts. With the inputs + // at rest and a kept species coming within its tolerance between + // the 1,024th and 2,048th steps it passed, a march called stalled + // at the 2,005th came back unconverged 2e-9 from its steady state. + if (waits_for_inputs_ && !inputs_asked && *residual_out < tol) { + converged = ss_inputs_let_a_stop(model_, rhs_, opts_, sub_, y_data, tol); + } // Stopped at a state the residual test refuses, the march returns // the last state it passed, asked there, where it was not, whether // the step takes it (issue #1001). The step only says where to From 072368b0c9fa47b9a5039531f5a8656182bf3e56 Mon Sep 17 00:00:00 2001 From: Bill Hlavacek Date: Thu, 3 Dec 2026 20:49:25 -0700 Subject: [PATCH 2/3] Review fixes for #2074; give the sharded-compile deadline test room Wording of the stall-ask comment, the changelog and the new test's comment (the steps counted are those at which the kept species pass and the inputs do not; the offsets are relative). The march's end ask cannot fire on a run that is taken on, which marches with tol = 0, so "out of steps" in the previous commit's message describes nothing reachable. test_a_sharded_compile_leaves_no_compiler_and_no_debris[unsupervised] failed in a loaded pre-push run: writing the 6,000 species' code took the whole 6 s deadline, so no compiler had started and cc.pids did not exist. 15 s still stops it mid-compile (the stand-in compiler sleeps 120 s). Refs #2074 --- changelog.d/2074.fixed.md | 2 +- python/tests/test_cli_deadline.py | 8 ++++++-- python/tests/test_steady_state_masked_inputs.py | 17 +++++++++-------- src/steady_state.cpp | 10 ++++++---- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/changelog.d/2074.fixed.md b/changelog.d/2074.fixed.md index bf9a7f1ad..786b84aa1 100644 --- a/changelog.d/2074.fixed.md +++ b/changelog.d/2074.fixed.md @@ -1 +1 @@ -- **`steady_state(mask=..., criterion="residual")` by integration came back unconverged where its march stalled within tolerance between two checks of the masked inputs ([#2074](https://github.com/lanl/bngsim/issues/2074)).** A march that waits for masked species the kept ones read checks those inputs by a Newton step only at the 1st, 2nd, 4th, ... step the kept species pass, and one called stalled (or ended by the integrator) between two such steps was not checked where it stopped. With the inputs at rest and a kept species coming within its tolerance at the 1,385th such step, a march stalled at the 2,005th came back unconverged 2e-9 from its steady state. It is now checked where it stops, as the default criterion's march is. The schedule of the checks is unchanged. +- **`steady_state(mask=..., criterion="residual")` by integration came back unconverged where its march stalled within tolerance between two checks of the masked inputs ([#2074](https://github.com/lanl/bngsim/issues/2074)).** A march that waits for masked species the kept ones read checks those inputs by a Newton step only at the 1st, 2nd, 4th, ... step at which the kept species pass and the inputs do not, and one called stalled (or ended by the integrator) between two such steps was not checked where it stopped. With the inputs at rest and a kept species coming within its tolerance at the 1,385th such step, a march stalled at the 2,005th came back unconverged, 2e-9 (relative) from its steady state. It is now checked where it stops, as the default criterion's march is. The schedule of the checks is unchanged. diff --git a/python/tests/test_cli_deadline.py b/python/tests/test_cli_deadline.py index 273996b58..b2a92e1b3 100644 --- a/python/tests/test_cli_deadline.py +++ b/python/tests/test_cli_deadline.py @@ -340,9 +340,13 @@ def test_a_sharded_compile_leaves_no_compiler_and_no_debris(tmp_path, supervised env = {"CC": str(cc), "BNGSIM_CODEGEN_CACHE_DIR": str(cache), "BNGSIM_CODEGEN_JOBS": "4"} net = _chain_net(tmp_path / "chain.net") run, elapsed = _bngsim( - "run", net, "--t-end", "1", "--deadline", "6", "--json", script=script, env=env + "run", net, "--t-end", "1", "--deadline", "15", "--json", script=script, env=env ) - _deadline_error(run, 6.0, elapsed, "compile") + # 15 s, not 6: in a loaded pre-push run (6 workers, 27k tests) writing the + # 6,000 species' code took the whole of 6 s, the deadline fell in the + # compile phase before any compiler started, and there was no cc.pids to + # read. The compiler sleeps 120 s, so the stop is still mid-compile. + _deadline_error(run, 15.0, elapsed, "compile") time.sleep(0.5) # a unit a pool thread would start after the stop started = [int(p) for p in pids.read_text().split()] assert started and all(_gone(p) for p in started), started diff --git a/python/tests/test_steady_state_masked_inputs.py b/python/tests/test_steady_state_masked_inputs.py index 2c8304285..83c917998 100644 --- a/python/tests/test_steady_state_masked_inputs.py +++ b/python/tests/test_steady_state_masked_inputs.py @@ -1017,9 +1017,9 @@ def test_a_network_that_does_not_settle_is_given_up_on_as_with_no_mask(): assert ss.n_steps < 10_000 -# I creeps to c = 1e12 at lam = 1e-10 from 1e-10 above it: its rate, 1e-8, is +# I creeps to c = 1e12 at lam = 1e-10 from 1e-10 (relative) above it: its rate, 1e-8, is # over tol and stays there, so its residual does not fall. X' = ks I/c - kd X -# reads I, X* = ks/kd = 2 from 1e-6 above it, its rate (1e-10) under tol from +# reads I, X* = ks/kd = 2 from 2e-6 (1e-6 relative) above it, its rate (1e-10) under tol from # the start. U and V ring on at w and nothing reads them; they set the steps. STALL_BETWEEN_ASKS = """begin parameters 1 lam 1.0e-10 @@ -1057,12 +1057,13 @@ def test_a_network_that_does_not_settle_is_given_up_on_as_with_no_mask(): def test_a_march_that_waits_for_inputs_is_asked_where_it_stalls(tmp_path): # Issue #2074. X passes from the first step and the inputs are asked at - # the 1st, 2nd, 4th, ... step it passes: the Newton step moves X by more - # than its tolerance up to the 1,385th (X 1e-8 above X*), and by less - # from there. I's residual does not fall, so the march is called stalled - # at the 2,005th, between the asks at the 1,024th and the 2,048th, and - # came back unconverged at X 2e-9 above X*. Asked where it stalled, as - # the default criterion's march is, it is converged there. + # the 1st, 2nd, 4th, ... step at which it passes and I does not: the + # Newton step moves X by more than its tolerance up to the 1,385th (X + # 1e-8 relative above X*), and by less from there. I's residual does not + # fall, so the march is called stalled at the 2,005th, between the asks + # at the 1,024th and the 2,048th, and came back unconverged at X 2e-9 + # relative above X*. Asked where it stalled, as the default criterion's + # march is, it is converged there. ss = _sim(tmp_path, STALL_BETWEEN_ASKS).steady_state( mask=["X()"], method="integration", criterion="residual", max_time=1e12 ) diff --git a/src/steady_state.cpp b/src/steady_state.cpp index 65009f4e9..640f127f6 100644 --- a/src/steady_state.cpp +++ b/src/steady_state.cpp @@ -1777,10 +1777,12 @@ class SteadyStateMarcher { converged = stops_at(y_data, *residual_out); } // So is a march that waits for masked inputs (issue #2074), whose - // inputs are asked at the same doubling counts. With the inputs - // at rest and a kept species coming within its tolerance between - // the 1,024th and 2,048th steps it passed, a march called stalled - // at the 2,005th came back unconverged 2e-9 from its steady state. + // inputs are asked only at the 1st, 2nd, 4th, ... step at which + // the kept species pass and the inputs do not (inputs_steps). With + // the inputs at rest and a kept species coming within its + // tolerance between the 1,024th and 2,048th such step, a march + // called stalled at the 2,005th came back unconverged, X 2e-9 + // (relative) from its steady state. if (waits_for_inputs_ && !inputs_asked && *residual_out < tol) { converged = ss_inputs_let_a_stop(model_, rhs_, opts_, sub_, y_data, tol); } From 533f5ce033748fcb914554fb18a4bbe866024914 Mon Sep 17 00:00:00 2001 From: Bill Hlavacek Date: Thu, 3 Dec 2026 20:52:51 -0700 Subject: [PATCH 3/3] test: give the slow-compile deadline test the same headroom (#2074 review) Refs #2074 --- python/tests/test_cli_deadline.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/tests/test_cli_deadline.py b/python/tests/test_cli_deadline.py index b2a92e1b3..607a882cc 100644 --- a/python/tests/test_cli_deadline.py +++ b/python/tests/test_cli_deadline.py @@ -178,8 +178,10 @@ def test_a_slow_compile_is_stopped_and_the_next_command_compiles(tmp_path): tmp.mkdir() env = {"CC": str(cc), "BNGSIM_CODEGEN_CACHE_DIR": str(cache), "TMPDIR": str(tmp)} args = ("run", DECAY, "--t-end", "1", "--sensitivity-params", "k", "--json") - run, elapsed = _bngsim(*args, "--deadline", "6", env=env) - error = _deadline_error(run, 6.0, elapsed, "compile") + # 15 s, as the sharded test below: on a loaded machine the stop can fall + # before the compiler has started. + run, elapsed = _bngsim(*args, "--deadline", "15", env=env) + error = _deadline_error(run, 15.0, elapsed, "compile") assert "compiling the model's code" in error["message"] # The stop is not a failed compile the run falls back from (round 3). assert "codegen failed" not in run.stderr