Skip to content

Fix uncompilable code for event assignments using expressions - #3416

Merged
dweindl merged 3 commits into
AMICI-dev:mainfrom
dweindl:fix_event_assignment_expressions
Oct 19, 2026
Merged

dweindl merged 3 commits into
AMICI-dev:mainfrom
dweindl:fix_event_assignment_expressions

Conversation

@dweindl

@dweindl dweindl commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #3412.

Event assignments that reference expressions (w), e.g., targets of SBML assignment rules (y := 3 * x, E1: at time > 1: v = v + y), produced uncompilable code (deltax.cpp: 'y_' was not declared in this scope), since deltax is evaluated without access to w.

Event.get_state_update now takes a function that replaces expressions by their (recursively expanded) definitions, which is applied before substituting x by x_old. This reuses the substitution #3388 added for noise formulas, moved to DEModel._get_expression_substituter. Since the fix is in DEModel, it also applies to the JAX backend's _delta_x for expressions that only depend on states, parameters and conservation law totals. JAX's _delta_x doesn't get t and the Heaviside variables, so event assignments that depend on time or piecewise, directly or via expressions, still fail there, as on main (#3423).

Event assignments using expressions that contain splines still don't compile (deltax.cpp: 'spl_0_' was not declared in this scope), as on main, since only w and dwdx get the spline values (#3424).

The new test test_event_assignment_with_expressions uses a chain of assignment rules depending on a state and a parameter. It checks the simulated values against the analytical solution, and forward and adjoint sensitivities against finite differences. On main, model compilation fails.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.11%. Comparing base (589bb74) to head (fff8213).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #3416   +/-   ##
=======================================
  Coverage   80.10%   80.11%           
=======================================
  Files         320      320           
  Lines       22699    22705    +6     
  Branches     1536     1535    -1     
=======================================
+ Hits        18183    18189    +6     
  Misses       4508     4508           
  Partials        8        8           
Flag Coverage Δ
cpp 74.22% <100.00%> (+<0.01%) ⬆️
cpp_python 35.09% <90.90%> (+0.01%) ⬆️
petab 48.47% <90.90%> (+0.01%) ⬆️
petab_sciml 16.96% <72.72%> (+0.01%) ⬆️
petab_sciml_benchmarks 15.54% <72.72%> (+0.01%) ⬆️
python 72.40% <100.00%> (+<0.01%) ⬆️
sbmlsuite 51.02% <ø> (ø)

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

Files with missing lines Coverage Δ
python/sdist/amici/_symbolic/de_model.py 93.88% <100.00%> (+0.02%) ⬆️
...ython/sdist/amici/_symbolic/de_model_components.py 96.62% <100.00%> (+0.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dweindl
dweindl force-pushed the fix_event_assignment_expressions branch from 7359cee to bde2a7e Compare October 19, 2026 06:58
dweindl and others added 2 commits October 19, 2026 10:08
Event assignments that reference expressions (`w`), e.g., targets of
SBML assignment rules, resulted in uncompilable `deltax` code, as
`deltax` is evaluated without access to `w`. Replace expressions by
their definitions before substituting `x` by `x_old`, reusing the
substitution for noise formulas from AMICI-dev#3388. This also fixes the JAX
backend's `_delta_x`.

Fixes AMICI-dev#3412.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The noise generated by `ExpData(rdata, ..., seed)` differs between
platforms, which made the check for non-zero sensitivities fail on macOS
and Windows. Use a fixed offset instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl
dweindl force-pushed the fix_event_assignment_expressions branch from bde2a7e to 2035fd2 Compare October 19, 2026 08:09
Avoids merge conflicts with other pull requests adding tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dweindl
dweindl marked this pull request as ready for review October 19, 2026 12:44
@dweindl
dweindl requested a review from a team as a code owner October 19, 2026 12:44
@dweindl dweindl self-assigned this Oct 19, 2026
@dweindl dweindl added code generation Related to model code generation events related to handing discrete events labels Oct 19, 2026
@dweindl
dweindl added this pull request to the merge queue Oct 19, 2026
Merged via the queue into AMICI-dev:main with commit 0179275 Oct 19, 2026
36 checks passed
@dweindl
dweindl deleted the fix_event_assignment_expressions branch October 19, 2026 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code generation Related to model code generation events related to handing discrete events

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated code does not compile if an event assignment uses an assignment rule variable

2 participants