Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sensitivities for models with events / extend and cleanup event tests #2084

Merged
merged 13 commits into from
May 16, 2023

Conversation

dweindl
Copy link
Member

@dweindl dweindl commented May 11, 2023

Fixes incorrect sensitivities for models with events with state-dependent trigger functions.

Add three new event test cases and do some cleanup.

Closes #2086

Three new event test cases and some cleanup.

To reduce changes in #1539

Co-authored-by: Paul Stapor <paul.stapor@helmholtz-muenchen.de>
@dweindl dweindl requested a review from a team as a code owner May 11, 2023 19:32
@dweindl dweindl self-assigned this May 11, 2023
@codecov
Copy link

codecov bot commented May 11, 2023

Codecov Report

Merging #2084 (ef6cf15) into develop (438812d) will not change coverage.
The diff coverage is 0.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #2084   +/-   ##
========================================
  Coverage    49.44%   49.44%           
========================================
  Files           32       32           
  Lines         5497     5497           
========================================
  Hits          2718     2718           
  Misses        2779     2779           
Flag Coverage Δ
petab 49.44% <0.00%> (ø)

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

Impacted Files Coverage Δ
python/sdist/amici/de_export.py 78.41% <0.00%> (ø)

@dweindl
Copy link
Member Author

dweindl commented May 12, 2023

There seems to be some issue with forward sensitivities for python/tests/test_events.py::test_models[piecewise_plus_event_simple_case].

Only for d_x1/d_alpha, rest is okay.
image

@dweindl dweindl marked this pull request as draft May 12, 2023 06:56
Copy link
Member

@FFroehlich FFroehlich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇

@FFroehlich
Copy link
Member

FFroehlich commented May 12, 2023

There seems to be some issue with forward sensitivities for python/tests/test_events.py::test_models[piecewise_plus_event_simple_case].

Only for d_x1/d_alpha, rest is okay. image

Oi! This is bad, will try to have a look next week!

@stephanmg
Copy link
Collaborator

There seems to be some issue with forward sensitivities for python/tests/test_events.py::test_models[piecewise_plus_event_simple_case].
Only for d_x1/d_alpha, rest is okay. image

Oi! This is bad, will try to have a look next week!

True. Will also have a look, we should then sync up perhaps.

def model_definition_piecewise_plus_event_semi_complicated():
"""Test model for boolean operations in a piecewise condition, discrete
events and a non-vanishing quadrature for the adjoint state.
"""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently fails finite differences check.

Passes with

diff --git a/python/tests/util.py b/python/tests/util.py
index d5907da29..e85160a98 100644
--- a/python/tests/util.py
+++ b/python/tests/util.py
@@ -154,4 +154,4 @@ def check_trajectories_with_forward_sensitivities(
     solver.setRelativeToleranceFSA(1e-13)
     rdata = runAmiciSimulation(amici_model, solver=solver)
     _check_close(rdata["x"], result_expected_x, field="x", rtol=1e-10, atol=1e-12)
-    _check_close(rdata["sx"], result_expected_sx, field="sx", rtol=1e-10, atol=1e-9)
+    _check_close(rdata["sx"], result_expected_sx, field="sx", rtol=1e-07, atol=1e-9)

Seems a bit loose, but then again, we are comparing to FD...

@dweindl dweindl marked this pull request as ready for review May 16, 2023 06:44
@@ -2024,7 +2024,7 @@ def _compute_equation(self, name: str) -> None:
# This part only works if we use self.eq('xdot')
# instead of self.sym('xdot'). Not immediately clear
# why that is.
tmp_dxdp += smart_multiply(self.eq("xdot"), self.sym("stau").T)
tmp_dxdp += smart_multiply(self.eq("xdot_old"), self.sym("stau").T)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does seem plausible. Would argue though that self.sym("xdot_old") is more intuitive (should be the same, no?).

But the fact that this makes a difference does point to a more conceptual problem. If xdot needs to be evaluated before the event (which kinda makes sense), the same should also apply to ddeltaxdt, ddeltaxdx and ddeltaxdp. Probably best to add to derived state, add respective methods to model and compute these quantities pre-event. Probably worthwhile to come up with a testcase where this applies beforehand. Should be straightforward to do, use current testcase and change deltax such that ddeltaxdt, ddeltaxdx and ddeltaxdp have a dependency on x, i.e., are different pre- and post-event.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would argue though that self.sym("xdot_old") is more intuitive (should be the same, no?).

Yes, I already changed that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into that. I will merge this PR, as it clearly improves things already, and keep your suggestions for another one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: Some of these cases are already covered:

python/tests/test_events.py::test_models[events_plus_heavisides]
ddeltaxdp [Matrix([
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]), Matrix([
[0, 0, 0, 0, 0, 0, 0, 0,   0],
[0, 0, 0, 0, 0, 0, 0, 0,   0],
[0, 0, 0, 0, 0, 0, 0, 0, 1/3]]), Matrix([
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]), Matrix([
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]), Matrix([
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]])]
ddeltaxdx [Matrix([
[0, 0, -1/2],
[0, 0,    0],
[0, 0,    0]]), Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]), Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]), Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]), Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])]


python/tests/test_events.py::test_models[piecewise_plus_event_simple_case]
ddeltaxdp [Matrix([[0, 0, 1, 0]]), Matrix([[0, 0, 0, 0]]), Matrix([[0, 0, 0, 0]]), Matrix([[0, 0, 0, 0]])]
ddeltaxdx [Matrix([[-1]]), Matrix([[0]]), Matrix([[0]]), Matrix([[0]])]

python/tests/test_events.py::test_models[piecewise_plus_event_semi_complicated]
ddeltaxdp [Matrix([
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), Matrix([
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), Matrix([
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), Matrix([
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]), Matrix([
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])]
ddeltaxdx [Matrix([
[-1, 0],
[ 0, 0]]), Matrix([
[0, 1],
[0, 0]]), Matrix([
[0, 0],
[0, 0]]), Matrix([
[0, 0],
[0, 0]]), Matrix([
[0, 0],
[0, 0]])]

Only non-zero ddeltaxdt seems to be missing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, they are non-zero, but not dependent on x. Noted.

@dweindl dweindl changed the title Extend / cleanup event tests Fix sensitivities for models with events / extend and cleanup event tests May 16, 2023
@dweindl dweindl disabled auto-merge May 16, 2023 12:16
@dweindl dweindl merged commit a5398dd into develop May 16, 2023
@dweindl dweindl deleted the event_test branch May 16, 2023 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants