From 67a143f39622e27d2e1ba50d166c2b48857e1788 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 7 Feb 2013 23:40:31 +0000 Subject: [PATCH] Refactored CVodesIntegrator to reduce redundancy when setting options --- include/cantera/numerics/CVodesIntegrator.h | 10 ++++-- src/numerics/CVodesIntegrator.cpp | 39 +++++---------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/include/cantera/numerics/CVodesIntegrator.h b/include/cantera/numerics/CVodesIntegrator.h index 68d6b3773..6c3b27cf4 100644 --- a/include/cantera/numerics/CVodesIntegrator.h +++ b/include/cantera/numerics/CVodesIntegrator.h @@ -6,13 +6,13 @@ #ifndef CT_CVODESWRAPPER_H #define CT_CVODESWRAPPER_H -#ifdef HAS_SUNDIALS - #include "cantera/numerics/Integrator.h" #include "cantera/numerics/FuncEval.h" #include "cantera/base/ctexceptions.h" #include "cantera/base/ct_defs.h" +#ifdef HAS_SUNDIALS + #if SUNDIALS_VERSION == 22 #include "nvector_serial.h" #else @@ -85,6 +85,12 @@ public: //! responsible for integrator failures or unexpected small timesteps. virtual std::string getErrorInfo(int N); +protected: + + //! Applies user-specified options to the underlying CVODES solver. Called + //! during integrator initialization or reinitialization. + void applyOptions(); + private: void sensInit(double t0, FuncEval& func); diff --git a/src/numerics/CVodesIntegrator.cpp b/src/numerics/CVodesIntegrator.cpp index c11fcf1f3..0731054b9 100644 --- a/src/numerics/CVodesIntegrator.cpp +++ b/src/numerics/CVodesIntegrator.cpp @@ -374,22 +374,6 @@ void CVodesIntegrator::initialize(double t0, FuncEval& func) } #endif - if (m_type == DENSE + NOJAC) { - long int N = m_neq; - CVDense(m_cvode_mem, N); - } else if (m_type == DIAG) { - CVDiag(m_cvode_mem); - } else if (m_type == GMRES) { - CVSpgmr(m_cvode_mem, PREC_NONE, 0); - } else if (m_type == BAND + NOJAC) { - long int N = m_neq; - long int nu = m_mupper; - long int nl = m_mlower; - CVBand(m_cvode_mem, N, nu, nl); - } else { - throw CVodesErr("unsupported option"); - } - // pass a pointer to func in m_data delete m_fdata; m_fdata = new FuncData(&func, func.nparams()); @@ -411,17 +395,7 @@ void CVodesIntegrator::initialize(double t0, FuncEval& func) flag = CVodeSetSensParams(m_cvode_mem, DATA_PTR(m_fdata->m_pars), NULL, NULL); } - - // set options - if (m_maxord > 0) { - flag = CVodeSetMaxOrd(m_cvode_mem, m_maxord); - } - if (m_maxsteps > 0) { - flag = CVodeSetMaxNumSteps(m_cvode_mem, m_maxsteps); - } - if (m_hmax > 0) { - flag = CVodeSetMaxStep(m_cvode_mem, m_hmax); - } + applyOptions(); } @@ -459,24 +433,27 @@ void CVodesIntegrator::reinitialize(double t0, FuncEval& func) } #endif + applyOptions(); +} + +void CVodesIntegrator::applyOptions() +{ if (m_type == DENSE + NOJAC) { long int N = m_neq; CVDense(m_cvode_mem, N); } else if (m_type == DIAG) { CVDiag(m_cvode_mem); + } else if (m_type == GMRES) { + CVSpgmr(m_cvode_mem, PREC_NONE, 0); } else if (m_type == BAND + NOJAC) { long int N = m_neq; long int nu = m_mupper; long int nl = m_mlower; CVBand(m_cvode_mem, N, nu, nl); - } else if (m_type == GMRES) { - CVSpgmr(m_cvode_mem, PREC_NONE, 0); } else { throw CVodesErr("unsupported option"); } - - // set options if (m_maxord > 0) { CVodeSetMaxOrd(m_cvode_mem, m_maxord); }