CvodeAdaptor.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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 "ChasteSerialization.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 
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->GetUseAnalyticJacobian().
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:
00131     friend class boost::serialization::access;
00138     template<class Archive>
00139     void serialize(Archive & archive, const unsigned int version)
00140     {
00141         archive & boost::serialization::base_object<AbstractIvpOdeSolver>(*this);
00142         archive & mRelTol;
00143         archive & mAbsTol;
00144         // All other member variables given values on each call.
00145     }
00146 
00148     void* mpCvodeMem;
00149 
00151     N_Vector mInitialValues;
00152 
00154     CvodeData mData;
00155 
00157     double mRelTol;
00158 
00160     double mAbsTol;
00161 
00163     double mLastInternalStepSize;
00164 
00169     long int mMaxSteps;
00170 
00172     bool mCheckForRoots;
00173 
00174 protected:
00175 
00179     void SetupCvode(AbstractOdeSystem* pOdeSystem,
00180                     std::vector<double>& rInitialY,
00181                     double startTime, double maxStep);
00182 
00186     void FreeCvodeMemory();
00187 
00194     void CvodeError(int flag, const char * msg);
00195 
00196 public:
00197 
00205     CvodeAdaptor(double relTol=1e-4, double absTol=1e-6);
00206 
00214     void SetTolerances(double relTol=1e-4, double absTol=1e-6);
00215 
00219     double GetRelativeTolerance();
00220 
00224     double GetAbsoluteTolerance();
00225 
00229     double GetLastStepSize();
00230 
00243     OdeSolution Solve(AbstractOdeSystem* pOdeSystem,
00244                       std::vector<double>& rYValues,
00245                       double startTime,
00246                       double endTime,
00247                       double maxStep,
00248                       double timeSampling);
00249 
00260     void Solve(AbstractOdeSystem* pOdeSystem,
00261                std::vector<double>& rYValues,
00262                double startTime,
00263                double endTime,
00264                double maxStep);
00265 
00271     void CheckForStoppingEvents();
00272 
00277     void SetMaxSteps(long int numSteps);
00278 
00283     long int GetMaxSteps();
00284 
00285 };
00286 
00287 #include "SerializationExportWrapper.hpp"
00288 CHASTE_CLASS_EXPORT(CvodeAdaptor);
00289 
00290 #endif // _CVODEADAPTOR_HPP_
00291 #endif // CHASTE_CVODE

Generated by  doxygen 1.6.2