CvodeAdaptor.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #ifdef CHASTE_CVODE
00030 #ifndef _CVODEADAPTOR_HPP_
00031 #define _CVODEADAPTOR_HPP_
00032 
00033 #include <vector>
00034 
00035 #include "AbstractOdeSystem.hpp"
00036 #include "AbstractIvpOdeSolver.hpp"
00037 #include "OdeSolution.hpp"
00038 
00039 // CVODE headers
00040 #include <cvode/cvode.h>
00041 #include <nvector/nvector_serial.h>
00042 #include <sundials/sundials_nvector.h>
00043 #include <cvode/cvode_dense.h>
00044 
00061 int CvodeRhsAdaptor(realtype t, N_Vector y, N_Vector ydot, void *pData);
00062 
00083 int CvodeRootAdaptor(realtype t, N_Vector y, realtype *pGOut, void *pData);
00084 
00085 // /**
00086 //  * Jacobian computation adaptor function.
00087 //  *
00088 //  * If solving an AbstractOdeSystemWithAnalyticJacobian, this function
00089 //  * can be used to allow CVODE to compute the Jacobian analytically.
00090 //  *
00091 //  * Note to self: can test using pSystem->GetUseAnalytic().
00092 //  */
00093 // int CvodeDenseJacobianAdaptor(long int numberOfStateVariables, DenseMat J,
00094 //                               realtype t, N_Vector y, N_Vector fy,
00095 //                               void *pData,
00096 //                               N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
00097 
00104 void CvodeErrorHandler(int errorCode, const char *module, const char *function,
00105                        char *message, void *pData);
00106 
00107 
00108 typedef struct CvodeData_ {
00109     std::vector<realtype>* pY;
00110     AbstractOdeSystem* pSystem;
00111 } CvodeData;
00112 
00127 class CvodeAdaptor : public AbstractIvpOdeSolver
00128 {
00129 private:
00130     void* mpCvodeMem;
00131     N_Vector mInitialValues;
00132     CvodeData mData;
00133     double mRelTol;
00134     double mAbsTol;
00135     double mLastInternalStepSize;
00136     long int mMaxSteps;
00137     bool mCheckForRoots;
00138 protected:
00142     void SetupCvode(AbstractOdeSystem* pOdeSystem,
00143                     std::vector<double>& rInitialY,
00144                     double startTime, double maxStep);
00145 
00149     void FreeCvodeMemory();
00150 
00157     void CvodeError(int flag, const char * msg);
00158 
00159 public:
00165     CvodeAdaptor(double relTol=1e-4, double absTol=1e-6)
00166         : AbstractIvpOdeSolver(),
00167           mpCvodeMem(NULL), mInitialValues(NULL),
00168           mRelTol(relTol), mAbsTol(absTol),
00169           mLastInternalStepSize(-0.0),
00170           mMaxSteps(0),
00171           mCheckForRoots(false)
00172     {
00173     }
00174     
00179     void SetTolerances(double relTol=1e-4, double absTol=1e-6)
00180     {
00181         mRelTol = relTol;
00182         mAbsTol = absTol;
00183     }
00184     double GetRelativeTolerance()
00185     {
00186         return mRelTol;
00187     }
00188     double GetAbsoluteTolerance()
00189     {
00190         return mAbsTol;
00191     }
00192     
00196     double GetLastStepSize()
00197     {
00198         return mLastInternalStepSize;
00199     }
00200 
00213     OdeSolution Solve(AbstractOdeSystem* pOdeSystem,
00214                       std::vector<double>& rYValues,
00215                       double startTime,
00216                       double endTime,
00217                       double maxStep,
00218                       double timeSampling);
00219 
00230     void Solve(AbstractOdeSystem* pOdeSystem,
00231                std::vector<double>& rYValues,
00232                double startTime,
00233                double endTime,
00234                double maxStep);
00235                
00241     void CheckForStoppingEvents()
00242     {
00243         mCheckForRoots = true;
00244     }
00245     
00250     void SetMaxSteps(long int numSteps)
00251     {
00252         mMaxSteps = numSteps;
00253     }
00254     long int GetMaxSteps()
00255     {
00256         return mMaxSteps;
00257     }
00258 };
00259 
00260 #endif // _CVODEADAPTOR_HPP_
00261 #endif // CHASTE_CVODE

Generated on Wed Mar 18 12:51:56 2009 for Chaste by  doxygen 1.5.5