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 <boost/serialization/access.hpp>
00034 #include <boost/serialization/base_object.hpp>
00035 
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 
00045 // Needs to be included last
00046 #include <boost/serialization/export.hpp>
00047 
00064 int CvodeRhsAdaptor(realtype t, N_Vector y, N_Vector ydot, void *pData);
00065 
00086 int CvodeRootAdaptor(realtype t, N_Vector y, realtype *pGOut, void *pData);
00087 
00088 // /**
00089 //  * Jacobian computation adaptor function.
00090 //  *
00091 //  * If solving an AbstractOdeSystemWithAnalyticJacobian, this function
00092 //  * can be used to allow CVODE to compute the Jacobian analytically.
00093 //  *
00094 //  * Note to self: can test using pSystem->GetUseAnalyticJacobian().
00095 //  */
00096 // int CvodeDenseJacobianAdaptor(long int numberOfStateVariables, DenseMat J,
00097 //                               realtype t, N_Vector y, N_Vector fy,
00098 //                               void *pData,
00099 //                               N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
00100 
00107 void CvodeErrorHandler(int errorCode, const char *module, const char *function,
00108                        char *message, void *pData);
00109 
00110 
00111 typedef struct CvodeData_ {
00112     std::vector<realtype>* pY;
00113     AbstractOdeSystem* pSystem;
00114 } CvodeData;
00115 
00130 class CvodeAdaptor : public AbstractIvpOdeSolver
00131 {
00132 private:
00134     friend class boost::serialization::access;
00141     template<class Archive>
00142     void serialize(Archive & archive, const unsigned int version)
00143     {
00144         // This calls serialize on the base class - all member variables instantiated on construction or temporary.
00145         archive & boost::serialization::base_object<AbstractIvpOdeSolver>(*this);
00146         archive & mRelTol;
00147         archive & mAbsTol;
00148         // All other member variables given values on each call.
00149     }
00150 
00152     void *mpCvodeMem;
00153 
00155     N_Vector mInitialValues;
00156 
00158     CvodeData mData;
00159 
00161     double mRelTol;
00162 
00164     double mAbsTol;
00165 
00167     double mLastInternalStepSize;
00168 
00173     long int mMaxSteps;
00174 
00176     bool mCheckForRoots;
00177 
00178 protected:
00179 
00183     void SetupCvode(AbstractOdeSystem* pOdeSystem,
00184                     std::vector<double>& rInitialY,
00185                     double startTime, double maxStep);
00186 
00190     void FreeCvodeMemory();
00191 
00198     void CvodeError(int flag, const char * msg);
00199 
00200 public:
00201 
00209     CvodeAdaptor(double relTol=1e-4, double absTol=1e-6);
00210 
00218     void SetTolerances(double relTol=1e-4, double absTol=1e-6);
00219 
00223     double GetRelativeTolerance();
00224 
00228     double GetAbsoluteTolerance();
00229 
00233     double GetLastStepSize();
00234 
00247     OdeSolution Solve(AbstractOdeSystem* pOdeSystem,
00248                       std::vector<double>& rYValues,
00249                       double startTime,
00250                       double endTime,
00251                       double maxStep,
00252                       double timeSampling);
00253 
00264     void Solve(AbstractOdeSystem* pOdeSystem,
00265                std::vector<double>& rYValues,
00266                double startTime,
00267                double endTime,
00268                double maxStep);
00269 
00275     void CheckForStoppingEvents();
00276 
00281     void SetMaxSteps(long int numSteps);
00282 
00287     long int GetMaxSteps();
00288 
00289 };
00290 
00291 BOOST_CLASS_EXPORT(CvodeAdaptor);
00292 
00293 #endif // _CVODEADAPTOR_HPP_
00294 #endif // CHASTE_CVODE

Generated on Tue Aug 4 16:10:24 2009 for Chaste by  doxygen 1.5.5