diff --git a/changelog.d/2074.fixed.md b/changelog.d/2074.fixed.md new file mode 100644 index 000000000..786b84aa1 --- /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 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..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 @@ -340,9 +342,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 0c90bc4cc..83c917998 100644 --- a/python/tests/test_steady_state_masked_inputs.py +++ b/python/tests/test_steady_state_masked_inputs.py @@ -1017,6 +1017,62 @@ 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 (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 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 + 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 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 + ) + 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..640f127f6 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,16 @@ 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 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); + } // 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