From 84a3eb93a532ace76953e384586b33647cff7ae8 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Wed, 10 Jul 2024 17:05:22 +0200 Subject: [PATCH] fix --- python/tests/test_swig_interface.py | 1 + src/amici.cpp | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/python/tests/test_swig_interface.py b/python/tests/test_swig_interface.py index 2aaff7ea4e..f61b28ea4d 100644 --- a/python/tests/test_swig_interface.py +++ b/python/tests/test_swig_interface.py @@ -572,6 +572,7 @@ def test_python_exceptions(sbml_example_presimulation_module): rdata = amici.runAmiciSimulation(model, solver) assert rdata.status == amici.AMICI_FIRST_RHSFUNC_ERR + edata = amici.ExpData(1, 1, 1, [1]) rdatas = amici.runAmiciSimulations( model, solver, [edata, edata], failfast=True, num_threads=1 ) diff --git a/src/amici.cpp b/src/amici.cpp index c74c88e3a5..9fbc472774 100644 --- a/src/amici.cpp +++ b/src/amici.cpp @@ -282,17 +282,28 @@ std::vector> runAmiciSimulations( #pragma omp parallel for num_threads(num_threads) #endif for (int i = 0; i < (int)edatas.size(); ++i) { - auto mySolver = std::unique_ptr(solver.clone()); - auto myModel = std::unique_ptr(model.clone()); - - /* if we fail we need to write empty return datas for the python - interface */ - if (skipThrough) { - ConditionContext conditionContext(myModel.get(), edatas[i]); + // must catch exceptions in parallel section to avoid termination + try { + auto mySolver = std::unique_ptr(solver.clone()); + auto myModel = std::unique_ptr(model.clone()); + + /* if we fail we need to write empty return datas for the python + interface */ + if (skipThrough) { + ConditionContext conditionContext(myModel.get(), edatas[i]); + results[i] + = std::unique_ptr(new ReturnData(solver, model) + ); + } else { + results[i] = runAmiciSimulation(*mySolver, edatas[i], *myModel); + } + } catch (std::exception const& ex) { results[i] = std::unique_ptr(new ReturnData(solver, model)); - } else { - results[i] = runAmiciSimulation(*mySolver, edatas[i], *myModel); + results[i]->status = AMICI_ERROR; + results[i]->messages.push_back( + LogItem(LogSeverity::error, "OTHER", ex.what()) + ); } skipThrough |= failfast && results[i]->status < 0;