diff --git a/python/sdist/amici/sbml_import.py b/python/sdist/amici/sbml_import.py index be3b007aee..eeb8087563 100644 --- a/python/sdist/amici/sbml_import.py +++ b/python/sdist/amici/sbml_import.py @@ -3132,13 +3132,18 @@ def _parse_event_trigger(trigger: sp.Expr) -> sp.Expr: Recursively translates a boolean trigger function into a real valued root function - :param trigger: - :return: real valued root function expression + :param trigger: The Boolean trigger expression. + The event triggers when this expression changes from False to True. + :return: real-valued root function expression """ # Events can be defined without trigger, i.e., the event will never fire. # In this case, set a dummy trigger: if trigger is None or trigger is sp.false: + return sp.Float(-1.0) + + if trigger is sp.true: return sp.Float(1.0) + if trigger.is_Relational: root = trigger.args[0] - trigger.args[1] _check_unsupported_functions_sbml(root, "sympy.Expression") diff --git a/python/tests/test_sbml_import.py b/python/tests/test_sbml_import.py index 3277b233c0..8d2546f3a5 100644 --- a/python/tests/test_sbml_import.py +++ b/python/tests/test_sbml_import.py @@ -17,6 +17,7 @@ from amici import import_model_module from amici.testing import TemporaryDirectoryWinSafe as TemporaryDirectory from conftest import MODEL_STEADYSTATE_SCALED_XML +import sympy as sp def simple_sbml_model(): @@ -41,6 +42,22 @@ def simple_sbml_model(): return document, model +def test_event_trigger_to_root_function(): + """Test that root functions for event triggers are generated correctly.""" + from amici.sbml_import import _parse_event_trigger as to_trig + + a, b = sp.symbols("a b") + + assert to_trig(None) == sp.Float(-1) + assert to_trig(sp.false) == sp.Float(-1) + assert to_trig(sp.true) == sp.Float(1) + + assert to_trig(a > b) == a - b + assert to_trig(a >= b) == a - b + assert to_trig(a < b) == b - a + assert to_trig(a <= b) == b - a + + def test_sbml2amici_no_observables(tempdir): """Test model generation works for model without observables""" sbml_doc, sbml_model = simple_sbml_model()