From bb5a81f5d6224ab58c1185ae4efe09516de3f163 Mon Sep 17 00:00:00 2001 From: band-a-prend Date: Thu, 25 Jul 2019 00:06:45 +0300 Subject: [PATCH 1/3] Simple fix for Sundials 3.2 compatibility The Sundials 3.1 and 3.2 are compatible with each other so this patch just allows to pass check for the installed Sundials 3.2 --- SConstruct | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index 206d09c6cd..ca5b23c51a 100644 --- a/SConstruct +++ b/SConstruct @@ -1091,7 +1091,7 @@ if env['system_sundials'] == 'y': # Ignore the minor version, e.g. 2.4.x -> 2.4 env['sundials_version'] = '.'.join(sundials_version.split('.')[:2]) - if env['sundials_version'] not in ('2.4','2.5','2.6','2.7','3.0','3.1'): + if env['sundials_version'] not in ('2.4','2.5','2.6','2.7','3.0','3.1','3.2'): print("""ERROR: Sundials version %r is not supported.""" % env['sundials_version']) sys.exit(1) print("""INFO: Using system installation of Sundials version %s.""" % sundials_version) From 5dbdbb97d802c7d4ea66615e381b0093142b1a44 Mon Sep 17 00:00:00 2001 From: band-a-prend Date: Thu, 25 Jul 2019 00:22:34 +0300 Subject: [PATCH 2/3] Fix building Cantera against Sundials 4.x library The changelog of Sundials 4.0.0 states: "With the introduction of SUNNonlinSol modules, the input parameter iter to CVodeCreate has been removed along with the function CVodeSetIterType and the constants CV_NEWTON and CV_FUNCTIONAL. Similarly, the ITMETH parameter has been removed from the Fortran interface function FCVMALLOC. Instead of specifying the nonlinear iteration type when creating the CVODE(S) memory structure, CVODE(S) uses the SUNNONLINSOL_NEWTON module implementation of a Newton iteration by default." so the appropreate conditional changes are added to control the code execution via CT_SUNDIALS_VERSION preprocessor variable to omit the parameters of Sundials solver that are no longer required. --- SConstruct | 30 ++++++++++------- include/cantera/numerics/CVodesIntegrator.h | 4 ++- include/cantera/numerics/Integrator.h | 10 +++--- src/kinetics/ImplicitSurfChem.cpp | 4 ++- src/numerics/CVodesIntegrator.cpp | 36 ++++++++++++++------- src/numerics/IDA_Solver.cpp | 6 +++- src/zeroD/ReactorNet.cpp | 4 ++- 7 files changed, 62 insertions(+), 32 deletions(-) diff --git a/SConstruct b/SConstruct index ca5b23c51a..b953c03626 100644 --- a/SConstruct +++ b/SConstruct @@ -1038,23 +1038,29 @@ env['has_demangle'] = conf.CheckDeclaration("boost::core::demangle", import SCons.Conftest, SCons.SConf context = SCons.SConf.CheckContext(conf) -ret = SCons.Conftest.CheckLib(context, - ['sundials_cvodes'], - header='#include "cvodes/cvodes.h"', - language='C++', - call='CVodeCreate(CV_BDF, CV_NEWTON);', - autoadd=False, - extra_libs=env['blas_lapack_libs']) -if ret: + +# Check initially for Sundials<=3.2 and then for Sundials>=4.0 +for cvode_call in ['CVodeCreate(CV_BDF, CV_NEWTON);','CVodeCreate(CV_BDF);']: + ret = SCons.Conftest.CheckLib(context, + ['sundials_cvodes'], + header='#include "cvodes/cvodes.h"', + language='C++', + call=cvode_call, + autoadd=False, + extra_libs=env['blas_lapack_libs']) # CheckLib returns False to indicate success + if not ret: + if env['system_sundials'] == 'default': + env['system_sundials'] = 'y' + break + +# Execute if the cycle ends without 'break' +else: if env['system_sundials'] == 'default': env['system_sundials'] = 'n' elif env['system_sundials'] == 'y': config_error('Expected system installation of Sundials, but it could ' 'not be found.') -elif env['system_sundials'] == 'default': - env['system_sundials'] = 'y' - # Checkout Sundials submodule if needed if (env['system_sundials'] == 'n' and @@ -1091,7 +1097,7 @@ if env['system_sundials'] == 'y': # Ignore the minor version, e.g. 2.4.x -> 2.4 env['sundials_version'] = '.'.join(sundials_version.split('.')[:2]) - if env['sundials_version'] not in ('2.4','2.5','2.6','2.7','3.0','3.1','3.2'): + if env['sundials_version'] not in ('2.4','2.5','2.6','2.7','3.0','3.1','3.2','4.0','4.1'): print("""ERROR: Sundials version %r is not supported.""" % env['sundials_version']) sys.exit(1) print("""INFO: Using system installation of Sundials version %s.""" % sundials_version) diff --git a/include/cantera/numerics/CVodesIntegrator.h b/include/cantera/numerics/CVodesIntegrator.h index 1332336f01..8bd39bf97f 100644 --- a/include/cantera/numerics/CVodesIntegrator.h +++ b/include/cantera/numerics/CVodesIntegrator.h @@ -51,7 +51,9 @@ class CVodesIntegrator : public Integrator m_maxord = n; } virtual void setMethod(MethodType t); - virtual void setIterator(IterType t); + #if CT_SUNDIALS_VERSION < 40 + virtual void setIterator(IterType t); + #endif virtual void setMaxStepSize(double hmax); virtual void setMinStepSize(double hmin); virtual void setMaxSteps(int nmax); diff --git a/include/cantera/numerics/Integrator.h b/include/cantera/numerics/Integrator.h index 272468ae9b..25884f1225 100644 --- a/include/cantera/numerics/Integrator.h +++ b/include/cantera/numerics/Integrator.h @@ -175,10 +175,12 @@ class Integrator warn("setMethodType"); } - //! Set the linear iterator. - virtual void setIterator(IterType t) { - warn("setInterator"); - } + #if CT_SUNDIALS_VERSION < 40 + //! Set the linear iterator. + virtual void setIterator(IterType t) { + warn("setInterator"); + } + #endif //! Set the maximum step size virtual void setMaxStepSize(double hmax) { diff --git a/src/kinetics/ImplicitSurfChem.cpp b/src/kinetics/ImplicitSurfChem.cpp index 771a34330c..c969ed6c06 100644 --- a/src/kinetics/ImplicitSurfChem.cpp +++ b/src/kinetics/ImplicitSurfChem.cpp @@ -84,7 +84,9 @@ ImplicitSurfChem::ImplicitSurfChem( // numerically, and use a Newton linear iterator m_integ->setMethod(BDF_Method); m_integ->setProblemType(DENSE + NOJAC); - m_integ->setIterator(Newton_Iter); + #if CT_SUNDIALS_VERSION < 40 + m_integ->setIterator(Newton_Iter); + #endif m_work.resize(ntmax); } diff --git a/src/numerics/CVodesIntegrator.cpp b/src/numerics/CVodesIntegrator.cpp index 806c67dd45..982e101451 100644 --- a/src/numerics/CVodesIntegrator.cpp +++ b/src/numerics/CVodesIntegrator.cpp @@ -89,7 +89,9 @@ CVodesIntegrator::CVodesIntegrator() : m_type(DENSE+NOJAC), m_itol(CV_SS), m_method(CV_BDF), - m_iter(CV_NEWTON), + #if CT_SUNDIALS_VERSION < 40 + m_iter(CV_NEWTON), + #endif m_maxord(0), m_reltol(1.e-9), m_abstols(1.e-15), @@ -227,16 +229,18 @@ void CVodesIntegrator::setMaxErrTestFails(int n) } } -void CVodesIntegrator::setIterator(IterType t) -{ - if (t == Newton_Iter) { - m_iter = CV_NEWTON; - } else if (t == Functional_Iter) { - m_iter = CV_FUNCTIONAL; - } else { - throw CanteraError("CVodesIntegrator::setIterator", "unknown iterator"); +#if CT_SUNDIALS_VERSION < 40 + void CVodesIntegrator::setIterator(IterType t) + { + if (t == Newton_Iter) { + m_iter = CV_NEWTON; + } else if (t == Functional_Iter) { + m_iter = CV_FUNCTIONAL; + } else { + throw CanteraError("CVodesIntegrator::setIterator", "unknown iterator"); + } } -} +#endif void CVodesIntegrator::sensInit(double t0, FuncEval& func) { @@ -298,7 +302,11 @@ void CVodesIntegrator::initialize(double t0, FuncEval& func) //! Specify the method and the iteration type. Cantera Defaults: //! CV_BDF - Use BDF methods //! CV_NEWTON - use Newton's method - m_cvode_mem = CVodeCreate(m_method, m_iter); + #if CT_SUNDIALS_VERSION < 40 + m_cvode_mem = CVodeCreate(m_method, m_iter); + #else + m_cvode_mem = CVodeCreate(m_method); + #endif if (!m_cvode_mem) { throw CanteraError("CVodesIntegrator::initialize", "CVodeCreate failed."); @@ -412,7 +420,11 @@ void CVodesIntegrator::applyOptions() #if CT_SUNDIALS_VERSION >= 30 SUNLinSolFree((SUNLinearSolver) m_linsol); SUNMatDestroy((SUNMatrix) m_linsol_matrix); - m_linsol_matrix = SUNBandMatrix(N, nu, nl, nu+nl); + #if CT_SUNDIALS_VERSION < 40 + m_linsol_matrix = SUNBandMatrix(N, nu, nl, nu+nl); + #else + m_linsol_matrix = SUNBandMatrix(N, nu, nl); + #endif if (m_linsol_matrix == nullptr) { throw CanteraError("CVodesIntegrator::applyOptions", "Unable to create SUNBandMatrix of size {} with bandwidths " diff --git a/src/numerics/IDA_Solver.cpp b/src/numerics/IDA_Solver.cpp index 0f071ddfd2..f91354e455 100644 --- a/src/numerics/IDA_Solver.cpp +++ b/src/numerics/IDA_Solver.cpp @@ -446,7 +446,11 @@ void IDA_Solver::init(doublereal t0) #if CT_SUNDIALS_VERSION >= 30 SUNLinSolFree((SUNLinearSolver) m_linsol); SUNMatDestroy((SUNMatrix) m_linsol_matrix); - m_linsol_matrix = SUNBandMatrix(N, nu, nl, nu+nl); + #if CT_SUNDIALS_VERSION < 40 + m_linsol_matrix = SUNBandMatrix(N, nu, nl, nu+nl); + #else + m_linsol_matrix = SUNBandMatrix(N, nu, nl); + #endif if (m_linsol_matrix == nullptr) { throw CanteraError("IDA_Solver::init", "Unable to create SUNBandMatrix of size {} with bandwidths " diff --git a/src/zeroD/ReactorNet.cpp b/src/zeroD/ReactorNet.cpp index 389da71123..482d8c66ed 100644 --- a/src/zeroD/ReactorNet.cpp +++ b/src/zeroD/ReactorNet.cpp @@ -28,7 +28,9 @@ ReactorNet::ReactorNet() : // numerically, and use a Newton linear iterator m_integ->setMethod(BDF_Method); m_integ->setProblemType(DENSE + NOJAC); - m_integ->setIterator(Newton_Iter); + #if CT_SUNDIALS_VERSION < 40 + m_integ->setIterator(Newton_Iter); + #endif } void ReactorNet::setInitialTime(double time) From 32d8fe49ec8756ef46587405d60c1c489279a1c3 Mon Sep 17 00:00:00 2001 From: band-a-prend Date: Sat, 3 Aug 2019 12:46:59 +0300 Subject: [PATCH 3/3] Remove m_iter variable and deprecate setIterator function Remove m_iter variable and deprecate setIterator function because only Newton (CV_NEWTON) iteration method is used. --- include/cantera/numerics/CVodesIntegrator.h | 3 --- include/cantera/numerics/Integrator.h | 11 +++++------ src/kinetics/ImplicitSurfChem.cpp | 3 --- src/numerics/CVodesIntegrator.cpp | 18 +----------------- src/zeroD/ReactorNet.cpp | 3 --- 5 files changed, 6 insertions(+), 32 deletions(-) diff --git a/include/cantera/numerics/CVodesIntegrator.h b/include/cantera/numerics/CVodesIntegrator.h index 8bd39bf97f..e17d042c88 100644 --- a/include/cantera/numerics/CVodesIntegrator.h +++ b/include/cantera/numerics/CVodesIntegrator.h @@ -51,9 +51,6 @@ class CVodesIntegrator : public Integrator m_maxord = n; } virtual void setMethod(MethodType t); - #if CT_SUNDIALS_VERSION < 40 - virtual void setIterator(IterType t); - #endif virtual void setMaxStepSize(double hmax); virtual void setMinStepSize(double hmin); virtual void setMaxSteps(int nmax); diff --git a/include/cantera/numerics/Integrator.h b/include/cantera/numerics/Integrator.h index 25884f1225..58d073b29c 100644 --- a/include/cantera/numerics/Integrator.h +++ b/include/cantera/numerics/Integrator.h @@ -175,12 +175,11 @@ class Integrator warn("setMethodType"); } - #if CT_SUNDIALS_VERSION < 40 - //! Set the linear iterator. - virtual void setIterator(IterType t) { - warn("setInterator"); - } - #endif + //! Set the linear iterator. + //! @deprecated Unused. To be removed after Cantera 2.5. + virtual void setIterator(IterType t) { + warn_deprecated("Integrator::setIterator", "To be removed after Cantera 2.5."); + } //! Set the maximum step size virtual void setMaxStepSize(double hmax) { diff --git a/src/kinetics/ImplicitSurfChem.cpp b/src/kinetics/ImplicitSurfChem.cpp index c969ed6c06..798d1a7e85 100644 --- a/src/kinetics/ImplicitSurfChem.cpp +++ b/src/kinetics/ImplicitSurfChem.cpp @@ -84,9 +84,6 @@ ImplicitSurfChem::ImplicitSurfChem( // numerically, and use a Newton linear iterator m_integ->setMethod(BDF_Method); m_integ->setProblemType(DENSE + NOJAC); - #if CT_SUNDIALS_VERSION < 40 - m_integ->setIterator(Newton_Iter); - #endif m_work.resize(ntmax); } diff --git a/src/numerics/CVodesIntegrator.cpp b/src/numerics/CVodesIntegrator.cpp index 982e101451..92ee3312cc 100644 --- a/src/numerics/CVodesIntegrator.cpp +++ b/src/numerics/CVodesIntegrator.cpp @@ -89,9 +89,6 @@ CVodesIntegrator::CVodesIntegrator() : m_type(DENSE+NOJAC), m_itol(CV_SS), m_method(CV_BDF), - #if CT_SUNDIALS_VERSION < 40 - m_iter(CV_NEWTON), - #endif m_maxord(0), m_reltol(1.e-9), m_abstols(1.e-15), @@ -229,19 +226,6 @@ void CVodesIntegrator::setMaxErrTestFails(int n) } } -#if CT_SUNDIALS_VERSION < 40 - void CVodesIntegrator::setIterator(IterType t) - { - if (t == Newton_Iter) { - m_iter = CV_NEWTON; - } else if (t == Functional_Iter) { - m_iter = CV_FUNCTIONAL; - } else { - throw CanteraError("CVodesIntegrator::setIterator", "unknown iterator"); - } - } -#endif - void CVodesIntegrator::sensInit(double t0, FuncEval& func) { m_np = func.nparams(); @@ -303,7 +287,7 @@ void CVodesIntegrator::initialize(double t0, FuncEval& func) //! CV_BDF - Use BDF methods //! CV_NEWTON - use Newton's method #if CT_SUNDIALS_VERSION < 40 - m_cvode_mem = CVodeCreate(m_method, m_iter); + m_cvode_mem = CVodeCreate(m_method, CV_NEWTON); #else m_cvode_mem = CVodeCreate(m_method); #endif diff --git a/src/zeroD/ReactorNet.cpp b/src/zeroD/ReactorNet.cpp index 482d8c66ed..3ca6972964 100644 --- a/src/zeroD/ReactorNet.cpp +++ b/src/zeroD/ReactorNet.cpp @@ -28,9 +28,6 @@ ReactorNet::ReactorNet() : // numerically, and use a Newton linear iterator m_integ->setMethod(BDF_Method); m_integ->setProblemType(DENSE + NOJAC); - #if CT_SUNDIALS_VERSION < 40 - m_integ->setIterator(Newton_Iter); - #endif } void ReactorNet::setInitialTime(double time)