From 8253a97d413cbbcfec335fb50fd1355107804e9e Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Fri, 18 Oct 2024 11:12:39 +0200 Subject: [PATCH] Fix capturing SUNDIALS warnings after 7.1.1 upgrade (#2551) Disable SUNDIALS's warning-handling through `SUNLogger`. Instead pipe everything both errors and warnings through the provided`SUNErrHandlerFn`. For details, see #2550. Fixes #2550. This also corrects the error message formatting - file path and line numbers for debug builds, but omitted in release builds - which was inverted before. --- ThirdParty/sundials/src/cvodes/cvodes.c | 3 +++ ThirdParty/sundials/src/idas/idas.c | 3 +++ include/amici/defines.h | 1 + src/amici.cpp | 1 + src/solver.cpp | 2 +- src/solver_cvodes.cpp | 4 ++++ src/solver_idas.cpp | 4 ++++ 7 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ThirdParty/sundials/src/cvodes/cvodes.c b/ThirdParty/sundials/src/cvodes/cvodes.c index 55399d7349..228911bee3 100644 --- a/ThirdParty/sundials/src/cvodes/cvodes.c +++ b/ThirdParty/sundials/src/cvodes/cvodes.c @@ -9930,6 +9930,8 @@ void cvProcessError(CVodeMem cv_mem, int error_code, int line, const char* func, break; } +/* AMICI: https://github.com/AMICI-dev/AMICI/issues/2550 */ +#ifdef AMICI_SUNLOGGER_WARNINGS if (error_code == CV_WARNING) { #if SUNDIALS_LOGGING_LEVEL >= SUNDIALS_LOGGING_WARNING @@ -9940,6 +9942,7 @@ void cvProcessError(CVodeMem cv_mem, int error_code, int line, const char* func, #endif break; } +#endif /* Call the SUNDIALS main error handler */ SUNHandleErrWithMsg(line, func, file, msg, error_code, cv_mem->cv_sunctx); diff --git a/ThirdParty/sundials/src/idas/idas.c b/ThirdParty/sundials/src/idas/idas.c index 3174660c12..1f3c3b5935 100644 --- a/ThirdParty/sundials/src/idas/idas.c +++ b/ThirdParty/sundials/src/idas/idas.c @@ -8779,6 +8779,8 @@ void IDAProcessError(IDAMem IDA_mem, int error_code, int line, const char* func, break; } +/* AMICI: https://github.com/AMICI-dev/AMICI/issues/2550 */ +#ifdef AMICI_SUNLOGGER_WARNINGS if (error_code == IDA_WARNING) { #if SUNDIALS_LOGGING_LEVEL >= SUNDIALS_LOGGING_WARNING @@ -8789,6 +8791,7 @@ void IDAProcessError(IDAMem IDA_mem, int error_code, int line, const char* func, #endif break; } +#endif /* Call the SUNDIALS main error handler */ SUNHandleErrWithMsg(line, func, file, msg, error_code, IDA_mem->ida_sunctx); diff --git a/include/amici/defines.h b/include/amici/defines.h index 949c7fffc1..1c6741f8d2 100644 --- a/include/amici/defines.h +++ b/include/amici/defines.h @@ -60,6 +60,7 @@ constexpr double pi = M_PI; // // NOTE: When adding / removing / renaming return codes, // please update simulation_status_to_str_map in amici.h +constexpr int AMICI_WARNING= 99; constexpr int AMICI_RECOVERABLE_ERROR= 1; constexpr int AMICI_UNRECOVERABLE_ERROR= -10; constexpr int AMICI_TOO_MUCH_WORK= -1; diff --git a/src/amici.cpp b/src/amici.cpp index 3c795f0d7e..bba344358f 100644 --- a/src/amici.cpp +++ b/src/amici.cpp @@ -50,6 +50,7 @@ std::map simulation_status_to_str_map = { {AMICI_NOT_RUN, "AMICI_NOT_RUN"}, {AMICI_LSETUP_FAIL, "AMICI_LSETUP_FAIL"}, {AMICI_FIRST_QRHSFUNC_ERR, "AMICI_FIRST_QRHSFUNC_ERR"}, + {AMICI_WARNING, "AMICI_WARNING"}, }; std::unique_ptr runAmiciSimulation( diff --git a/src/solver.cpp b/src/solver.cpp index 7dc3980c5c..118af4c8d7 100644 --- a/src/solver.cpp +++ b/src/solver.cpp @@ -27,7 +27,7 @@ void wrapErrHandlerFn( std::is_same::value, "Must update format string" ); // for debug builds, include full file path and line numbers -#ifdef NDEBUG +#ifndef NDEBUG snprintf(msg_buffer, BUF_SIZE, "%s:%d: %s (%d)", file, line, msg, err_code); #else snprintf(msg_buffer, BUF_SIZE, "%s", msg); diff --git a/src/solver_cvodes.cpp b/src/solver_cvodes.cpp index 53bfd32613..72fcfa78ed 100644 --- a/src/solver_cvodes.cpp +++ b/src/solver_cvodes.cpp @@ -90,6 +90,10 @@ static_assert( amici::AMICI_FIRST_QRHSFUNC_ERR == CV_FIRST_QRHSFUNC_ERR, "AMICI_FIRST_QRHSFUNC_ERR != CV_FIRST_QRHSFUNC_ERR" ); +static_assert( + amici::AMICI_WARNING == CV_WARNING, + "AMICI_WARNING != CV_WARNING" +); /* * The following static members are callback function to CVODES. diff --git a/src/solver_idas.cpp b/src/solver_idas.cpp index c50968d9a4..8e64edb92e 100644 --- a/src/solver_idas.cpp +++ b/src/solver_idas.cpp @@ -70,6 +70,10 @@ static_assert( amici::AMICI_IDAS_CONSTR_FAIL == IDA_CONSTR_FAIL, "AMICI_IDAS_CONSTR_FAIL != IDA_CONSTR_FAIL" ); +static_assert( + amici::AMICI_WARNING == IDA_WARNING, + "AMICI_WARNING != IDA_WARNING" +); /* * The following static members are callback function to IDAS.