Skip to content

Fix time conditions at their threshold at the initial time - #3565

Open
dweindl wants to merge 1 commit into
AMICI-dev:mainfrom
dweindl:explicit_roots_at_t0
Open

dweindl wants to merge 1 commit into
AMICI-dev:mainfrom
dweindl:explicit_roots_at_t0

Conversation

@dweindl

@dweindl dweindl commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Fix piecewise and trigger conditions on time whose threshold is the initial time, e.g., piecewise(2, time <= 0, 0) from t0 = 0 or time <= 1 from t0 = 1, which kept their value at t0 for the whole simulation. The state cases of #3515 were already fixed by #3490.

Conditions on time are handled at their explicit trigger times, which are only processed when they are reached after the current time (get_next_trigger_timepoint uses upper_bound), so a trigger time at t0 was never processed. Now, as for the root functions tracked by the solver, handle_initial_events decides the Heaviside variables of trigger times within the root-finding tolerance of t0 by the direction of the root function, and marks these trigger times as handled, so they are not flipped back when reached within roundoff after t0. For that, the initialization of t_last_trigger_ moved from run()/run_steady_state() to handle_initial_events.

This also covers trigger times that coincide with t0 only up to roundoff. The Heaviside variable at t0 is initialized from the sign of the computed root function, while the trigger time comes from the solved expression, and both may disagree. E.g., for k * time >= 1 with t0 = 1 / k = 1 / 49, k * t0 - 1 is slightly negative, but the trigger time is not after t0, so the condition stayed false for the whole simulation. Within the tolerance and noise level, the final side is now decided by the slope of the root function alone. Whether the event triggers is then decided, as for a root function that is zero at t0, against the Heaviside variable at the end of the previous simulation period (Model::get_h_old), so an event that already triggered at t0 during presimulation or preequilibration does not trigger again.

Known limitation, as for the state cases: values evaluated exactly at t0 already use the value after the switch, e.g., an observable piecewise(2, time <= 0, 0) is 0 at t = 0.

For trigger times within roundoff after t0, the switch now also happens silently at t0, without recording a discontinuity. Therefore, the sensitivity with respect to such a trigger time is the one for a trigger time <= t0 (no jump contribution), consistent with #3490 for root functions tracked by the solver.

Fixes #3515.

🤖 Generated with Claude Code

@dweindl dweindl added c++ events related to handing discrete events piecewise Related to piecewise functions / Heaviside switches labels Nov 15, 2026
@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.29730% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.68%. Comparing base (68c5495) to head (6019e1d).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
src/forwardproblem.cpp 96.96% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3565      +/-   ##
==========================================
+ Coverage   80.51%   80.68%   +0.17%     
==========================================
  Files         331      331              
  Lines       23688    24449     +761     
  Branches     1691     1681      -10     
==========================================
+ Hits        19072    19727     +655     
- Misses       4610     4716     +106     
  Partials        6        6              
Flag Coverage Δ
cpp 75.31% <97.29%> (+0.20%) ⬆️
cpp_python 34.84% <40.54%> (+0.08%) ⬆️
petab 47.12% <51.35%> (+0.17%) ⬆️
petab_sciml 16.48% <0.00%> (+0.06%) ⬆️
petab_sciml_benchmarks 15.09% <0.00%> (+0.07%) ⬆️
python 72.95% <78.37%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
include/amici/model.h 82.00% <100.00%> (+1.14%) ⬆️
src/model.cpp 87.09% <100.00%> (+<0.01%) ⬆️
src/forwardproblem.cpp 93.70% <96.96%> (+0.11%) ⬆️

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

A `piecewise` or trigger condition on time whose trigger time is the
initial time, e.g., `piecewise(2, time <= 0, 0)` simulated from `t0 = 0`
or `time <= 1` from `t0 = 1`, kept its value at `t0` for the whole
simulation. Trigger times are only processed when they are reached after
the current time, so a trigger time at `t0` was never processed.

As for the root functions tracked by the solver (AMICI-dev#3490), the direction of
the root function now decides the Heaviside variable after `t0` for trigger
times within the root-finding tolerance of `t0`. These trigger times are
marked as handled, so that they are not processed again when reached.

This also covers trigger times that coincide with `t0` only up to
roundoff. The Heaviside variable at `t0` is initialized from the sign of
the computed root function, while the trigger time is computed from the
solved expression, and both may disagree. E.g., for `k * time >= 1` with
`t0 = 1 / k = 1 / 49`, `k * t0 - 1` evaluates to slightly below zero,
while the trigger time is not after `t0`, so the condition stayed false.
Conversely, a decreasing root function that is slightly negative at `t0`,
while the trigger time is after `t0`, was flipped to true when the trigger
time was reached. Within the tolerance and noise level, the final side is
now decided by the slope only. As for a root function that is zero at
`t0`, the event then triggers if its Heaviside variable was 0 at the end
of the previous simulation period, or, for the first one, if its initial
value is false. For that, the model keeps the Heaviside variables passed
to `Model::reinit_events`. The same criterion is used for Heaviside
variables that change in turn.

Fixes AMICI-dev#3515.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl
dweindl force-pushed the explicit_roots_at_t0 branch from 85496eb to 6019e1d Compare November 16, 2026 12:31
@dweindl dweindl self-assigned this Nov 16, 2026
@dweindl
dweindl marked this pull request as ready for review November 18, 2026 10:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ events related to handing discrete events piecewise Related to piecewise functions / Heaviside switches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A piecewise condition exactly at its threshold at t0 keeps its initial value for the whole simulation

1 participant