-
Notifications
You must be signed in to change notification settings - Fork 31
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
Conversation
Three new event test cases and some cleanup. To reduce changes in #1539 Co-authored-by: Paul Stapor <paul.stapor@helmholtz-muenchen.de>
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## develop #2084 +/- ##
========================================
Coverage 49.44% 49.44%
========================================
Files 32 32
Lines 5497 5497
========================================
Hits 2718 2718
Misses 2779 2779
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙇
This reverts commit 4381ac3.
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. | ||
""" |
There was a problem hiding this comment.
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...
python/sdist/amici/de_export.py
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Fixes incorrect sensitivities for models with events with state-dependent trigger functions.
Add three new event test cases and do some cleanup.
Closes #2086