From e0fbeec982c8a3389a3c7c300f4524f38e09f5d2 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Tue, 12 Dec 2023 21:45:33 +0100 Subject: [PATCH] Fix piecewise/Heavisides handling * Substitute non-time-dependent Heavisides * Substitute inside the expanded expression, otherwise not all substitution targets are found Closes #2231 --- python/sdist/amici/de_export.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/python/sdist/amici/de_export.py b/python/sdist/amici/de_export.py index ac857b366c..2943b000d3 100644 --- a/python/sdist/amici/de_export.py +++ b/python/sdist/amici/de_export.py @@ -2757,19 +2757,22 @@ def _process_heavisides( for tmp_old in tmp_roots_old: # we want unique identifiers for the roots tmp_new = self._get_unique_root(tmp_old, roots) + heavisides.append((sp.Heaviside(tmp_old), tmp_new)) + # `tmp_new` is None if the root is not time-dependent. if tmp_new is None: continue # For Heavisides, we need to add the negative function as well self._get_unique_root(sp.sympify(-tmp_old), roots) - heavisides.append((sp.Heaviside(tmp_old), tmp_new)) if heavisides: # only apply subs if necessary for heaviside_sympy, heaviside_amici in heavisides: - dxdt = dxdt.subs(heaviside_sympy, heaviside_amici) + dt_expanded = dt_expanded.subs( + heaviside_sympy, heaviside_amici + ) - return dxdt + return dt_expanded class DEExporter: