Skip to content

Fix SBML event triggers gated by conditions that only change at events - #3587

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

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

Conversation

@dweindl

@dweindl dweindl commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Sub-conditions of SBML event triggers that only depend on quantities that are constant during integration (parameters, constant compartments, species changed by neither reactions nor rate rules, and expressions depending only on those) are now evaluated exactly via Piecewise((1, cond), (-1, True)) terms, instead of becoming part of the root function.

This fixes:

Trigger times are still computed explicitly, without root-finding, where possible: solve_minmax_trigger_for_t treats the exact conditions as gates in both Min and Max. In addition, contains_periodic_subexpression no longer treats time-independent functions as periodic. Previously, that blocked explicit trigger times for triggers like time >= exp(p).

Not covered: conditions in piecewise expressions, which go through _parse_heaviside_trigger, and ==/!= on quantities that change during integration (#2912).

Fixes #3586. Relates to #2912, #3508.

🤖 Generated with Claude Code

Sub-conditions of SBML event triggers that only depend on symbols that
are constant during integration (parameters, constant compartments,
species not changed by reactions or rate rules, and expressions over
those) are now evaluated exactly via `Piecewise((1, cond), (-1, True))`
leaves instead of becoming part of the `Min`/`Max` root function.

Previously, e.g., `(x > c) && (u == 0)` resulted in the root function
`Min(u, -u, x - c)`, which touches zero without changing sign, so the
event was only detected at the end of the next integration step
(AMICI-dev#3586). Strict comparisons at the threshold were treated as true
(AMICI-dev#3508).

`solve_minmax_trigger_for_t` handles these leaves as gates, so such
triggers keep explicit trigger times where possible.
`contains_periodic_subexpression` no longer treats time-independent
functions as periodic, which prevented explicit trigger times for
triggers like `time >= exp(p)`.

Fixes AMICI-dev#3586. Relates to AMICI-dev#2912 and AMICI-dev#3508.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl dweindl added SBML SBML import related events related to handing discrete events labels Nov 17, 2026
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.29730% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.77%. Comparing base (7cb3c09) to head (a1d5e86).

Files with missing lines Patch % Lines
python/sdist/amici/importers/sbml/__init__.py 96.36% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3587      +/-   ##
==========================================
+ Coverage   80.75%   80.77%   +0.01%     
==========================================
  Files         331      331              
  Lines       23699    23757      +58     
  Branches     1674     1674              
==========================================
+ Hits        19139    19190      +51     
- Misses       4554     4561       +7     
  Partials        6        6              
Flag Coverage Δ
cpp 75.37% <97.29%> (+0.04%) ⬆️
cpp_python 34.80% <12.16%> (-0.06%) ⬇️
petab 47.24% <71.62%> (+0.07%) ⬆️
petab_sciml 16.48% <8.10%> (-0.03%) ⬇️
petab_sciml_benchmarks 15.09% <8.10%> (-0.02%) ⬇️
python 73.07% <97.29%> (+0.06%) ⬆️
sbmlsuite 50.07% <69.09%> (+0.35%) ⬆️

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

Files with missing lines Coverage Δ
...ython/sdist/amici/_symbolic/de_model_components.py 96.68% <100.00%> (+0.01%) ⬆️
python/sdist/amici/_symbolic/sympy_utils.py 97.70% <100.00%> (+0.20%) ⬆️
python/sdist/amici/importers/utils.py 91.34% <100.00%> (+0.69%) ⬆️
python/sdist/amici/importers/sbml/__init__.py 94.76% <96.36%> (+0.11%) ⬆️

... and 1 file with indirect coverage changes

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

@dweindl dweindl self-assigned this Nov 17, 2026
@dweindl
dweindl marked this pull request as ready for review November 17, 2026 10:18
@dweindl
dweindl requested a review from a team as a code owner November 17, 2026 10:18

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

events related to handing discrete events SBML SBML import related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Event with trigger (x > c) && (p == 0) runs at the next output time instead of when x crosses c

1 participant