From 0fdbe24aaf6d74f8a082782b32c9d6dd7488cd5e Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sat, 23 Feb 2019 17:31:37 -0500 Subject: [PATCH] Provide error message after failure to create large Sundials matrix If Sundials tries to create an excessively large matrix, it returns a null pointer. To avoid a subsequent segfault, throw an exception which makes the cause of the error clear. WIP: Better error message for too-large Sundials matrix TEMP: fixup for cvodes error messages --- src/numerics/CVodesIntegrator.cpp | 9 +++++++++ src/numerics/IDA_Solver.cpp | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/numerics/CVodesIntegrator.cpp b/src/numerics/CVodesIntegrator.cpp index 98b2acbf7..d1930aae6 100644 --- a/src/numerics/CVodesIntegrator.cpp +++ b/src/numerics/CVodesIntegrator.cpp @@ -369,6 +369,10 @@ void CVodesIntegrator::applyOptions() SUNLinSolFree((SUNLinearSolver) m_linsol); SUNMatDestroy((SUNMatrix) m_linsol_matrix); m_linsol_matrix = SUNDenseMatrix(N, N); + if (m_linsol_matrix == nullptr) { + throw CanteraError("CVodesIntegrator::applyOptions", + "Unable to create SUNDenseMatrix of size {0} x {0}", N); + } #if CT_SUNDIALS_USE_LAPACK m_linsol = SUNLapackDense(m_y, (SUNMatrix) m_linsol_matrix); #else @@ -400,6 +404,11 @@ void CVodesIntegrator::applyOptions() SUNLinSolFree((SUNLinearSolver) m_linsol); SUNMatDestroy((SUNMatrix) m_linsol_matrix); m_linsol_matrix = SUNBandMatrix(N, nu, nl, nu+nl); + if (m_linsol_matrix == nullptr) { + throw CanteraError("CVodesIntegrator::applyOptions", + "Unable to create SUNBandMatrix of size {} with bandwidths " + "{} and {}", N, nu, nl); + } #if CT_SUNDIALS_USE_LAPACK m_linsol = SUNLapackBand(m_y, (SUNMatrix) m_linsol_matrix); #else diff --git a/src/numerics/IDA_Solver.cpp b/src/numerics/IDA_Solver.cpp index a5b88101e..0f071ddfd 100644 --- a/src/numerics/IDA_Solver.cpp +++ b/src/numerics/IDA_Solver.cpp @@ -422,6 +422,10 @@ void IDA_Solver::init(doublereal t0) SUNLinSolFree((SUNLinearSolver) m_linsol); SUNMatDestroy((SUNMatrix) m_linsol_matrix); m_linsol_matrix = SUNDenseMatrix(N, N); + if (m_linsol_matrix == nullptr) { + throw CanteraError("IDA_Solver::init", + "Unable to create SUNDenseMatrix of size {0} x {0}", N); + } #if CT_SUNDIALS_USE_LAPACK m_linsol = SUNLapackDense(m_y, (SUNMatrix) m_linsol_matrix); #else @@ -443,6 +447,11 @@ void IDA_Solver::init(doublereal t0) SUNLinSolFree((SUNLinearSolver) m_linsol); SUNMatDestroy((SUNMatrix) m_linsol_matrix); m_linsol_matrix = SUNBandMatrix(N, nu, nl, nu+nl); + if (m_linsol_matrix == nullptr) { + throw CanteraError("IDA_Solver::init", + "Unable to create SUNBandMatrix of size {} with bandwidths " + "{} and {}", N, nu, nl); + } #if CT_SUNDIALS_USE_LAPACK m_linsol = SUNLapackBand(m_y, (SUNMatrix) m_linsol_matrix); #else