AbstractCvodeSystem.hpp
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
00030
00031
00032
00033
00034
00035
00036 #ifdef CHASTE_CVODE
00037 #ifndef _ABSTRACTCVODESYSTEM_HPP_
00038 #define _ABSTRACTCVODESYSTEM_HPP_
00039
00040 #include <vector>
00041 #include <string>
00042 #include <algorithm>
00043
00044
00045 #include "UblasVectorInclude.hpp"
00046
00047
00048 #include "OdeSolution.hpp"
00049 #include "AbstractParameterisedSystem.hpp"
00050 #include "Exception.hpp"
00051 #include "VectorHelperFunctions.hpp"
00052
00053
00054 #include "ChasteSerialization.hpp"
00055 #include <boost/serialization/split_member.hpp>
00056 #include <boost/serialization/vector.hpp>
00057 #include "ClassIsAbstract.hpp"
00058
00059
00060 #include <nvector/nvector_serial.h>
00061 #include <sundials/sundials_dense.h>
00062
00063
00064 #if CHASTE_SUNDIALS_VERSION >= 20400
00065 #define CHASTE_CVODE_DENSE_MATRIX DlsMat
00066 #else
00067 #define CHASTE_CVODE_DENSE_MATRIX DenseMat
00068 #endif
00069
00119 class AbstractCvodeSystem : public AbstractParameterisedSystem<N_Vector>
00120 {
00121 private:
00122 friend class TestAbstractCvodeSystem;
00123
00124 friend class boost::serialization::access;
00131 template<class Archive>
00132 void save(Archive & archive, const unsigned int version) const
00133 {
00134
00135
00136
00137 archive & mNumberOfStateVariables;
00138 archive & mUseAnalyticJacobian;
00139
00140
00141 const std::vector<double> state_vars = MakeStdVec(mStateVariables);
00142 archive & state_vars;
00143 const std::vector<double> params = MakeStdVec(mParameters);
00144 archive & params;
00145 archive & rGetParameterNames();
00146
00147 archive & mLastSolutionTime;
00148 archive & mForceReset;
00149 archive & mForceMinimalReset;
00150 archive & mRelTol;
00151 archive & mAbsTol;
00152 archive & mMaxSteps;
00153 archive & mLastInternalStepSize;
00154
00155
00156
00157
00158
00159
00160
00161
00162 }
00169 template<class Archive>
00170 void load(Archive & archive, const unsigned int version)
00171 {
00172 archive & mNumberOfStateVariables;
00173 archive & mUseAnalyticJacobian;
00174
00175 std::vector<double> state_vars;
00176 archive & state_vars;
00177 CopyFromStdVector(state_vars,mStateVariables);
00178
00179 std::vector<double> parameters;
00180 archive & parameters;
00181
00182 std::vector<std::string> param_names;
00183 archive & param_names;
00184 archive & mLastSolutionTime;
00185 archive & mForceReset;
00186 archive & mForceMinimalReset;
00187 archive & mRelTol;
00188 archive & mAbsTol;
00189 archive & mMaxSteps;
00190 archive & mLastInternalStepSize;
00191
00192
00193
00194
00195
00196 CheckParametersOnLoad(parameters,param_names);
00197 }
00198 BOOST_SERIALIZATION_SPLIT_MEMBER()
00199
00200
00207 void SetupCvode(N_Vector initialConditions,
00208 realtype tStart,
00209 realtype maxDt);
00210
00215 void RecordStoppingPoint(double stopTime);
00216
00218 void FreeCvodeMemory();
00219
00226 void CvodeError(int flag, const char * msg);
00227
00229 N_Vector mLastSolutionState;
00230
00232 double mLastSolutionTime;
00233
00235 bool mForceReset;
00236
00238 bool mForceMinimalReset;
00239
00240 protected:
00241
00243 bool mUseAnalyticJacobian;
00244
00246 double mRelTol;
00247
00249 double mAbsTol;
00250
00252 void* mpCvodeMem;
00253
00258 long int mMaxSteps;
00259
00261 double mLastInternalStepSize;
00262
00267 void Init();
00268
00269 public:
00270
00276 AbstractCvodeSystem(unsigned numberOfStateVariables);
00277
00281 virtual ~AbstractCvodeSystem();
00282
00290 virtual void EvaluateYDerivatives(realtype time,
00291 const N_Vector y,
00292 N_Vector ydot)=0;
00293
00294
00311 virtual void EvaluateAnalyticJacobian(long int N, realtype time, N_Vector y, N_Vector ydot,
00312 CHASTE_CVODE_DENSE_MATRIX jacobian,
00313 N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
00314 {
00315 EXCEPTION("No analytic Jacobian has been defined for this system.");
00316 }
00317
00328 void SetForceReset(bool autoReset);
00329
00338 void SetMinimalReset(bool minimalReset);
00339
00350 void ResetSolver();
00351
00371 OdeSolution Solve(realtype tStart,
00372 realtype tEnd,
00373 realtype maxDt,
00374 realtype tSamp);
00375
00391 void Solve(realtype tStart,
00392 realtype tEnd,
00393 realtype maxDt);
00394
00401 void SetMaxSteps(long int numSteps);
00402
00407 long int GetMaxSteps();
00408
00416 void SetTolerances(double relTol=1e-5, double absTol=1e-7);
00417
00421 double GetRelativeTolerance();
00422
00426 double GetAbsoluteTolerance();
00427
00431 double GetLastStepSize();
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00450 bool GetUseAnalyticJacobian();
00451
00458 void ForceUseOfNumericalJacobian(bool useNumericalJacobian = true);
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 };
00478
00479 CLASS_IS_ABSTRACT(AbstractCvodeSystem)
00480
00481 #endif //_ABSTRACTCVODESYSTEM_HPP_
00482 #endif // CHASTE_CVODE
00483
00484