00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
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