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 if (version >= 1u)
00141 {
00142 archive & mHasAnalyticJacobian;
00143 }
00144
00145
00146 const std::vector<double> state_vars = MakeStdVec(mStateVariables);
00147 archive & state_vars;
00148 const std::vector<double> params = MakeStdVec(mParameters);
00149 archive & params;
00150 archive & rGetParameterNames();
00151
00152 archive & mLastSolutionTime;
00153 archive & mForceReset;
00154 archive & mForceMinimalReset;
00155 archive & mRelTol;
00156 archive & mAbsTol;
00157 archive & mMaxSteps;
00158 archive & mLastInternalStepSize;
00159
00160
00161
00162
00163
00164
00165
00166
00167 }
00174 template<class Archive>
00175 void load(Archive & archive, const unsigned int version)
00176 {
00177 archive & mNumberOfStateVariables;
00178 archive & mUseAnalyticJacobian;
00179
00180
00181 mHasAnalyticJacobian = mUseAnalyticJacobian;
00182 if (version >= 1u)
00183 {
00184 archive & mHasAnalyticJacobian;
00185 }
00186
00187 std::vector<double> state_vars;
00188 archive & state_vars;
00189 CopyFromStdVector(state_vars,mStateVariables);
00190
00191 std::vector<double> parameters;
00192 archive & parameters;
00193
00194 std::vector<std::string> param_names;
00195 archive & param_names;
00196 archive & mLastSolutionTime;
00197 archive & mForceReset;
00198 archive & mForceMinimalReset;
00199 archive & mRelTol;
00200 archive & mAbsTol;
00201 archive & mMaxSteps;
00202 archive & mLastInternalStepSize;
00203
00204
00205
00206
00207
00208 CheckParametersOnLoad(parameters,param_names);
00209 }
00210 BOOST_SERIALIZATION_SPLIT_MEMBER()
00211
00212
00219 void SetupCvode(N_Vector initialConditions,
00220 realtype tStart,
00221 realtype maxDt);
00222
00227 void RecordStoppingPoint(double stopTime);
00228
00230 void FreeCvodeMemory();
00231
00238 void CvodeError(int flag, const char * msg);
00239
00241 N_Vector mLastSolutionState;
00242
00244 double mLastSolutionTime;
00245
00247 bool mForceReset;
00248
00250 bool mForceMinimalReset;
00251
00252 protected:
00253
00255 bool mHasAnalyticJacobian;
00256
00258 bool mUseAnalyticJacobian;
00259
00261 double mRelTol;
00262
00264 double mAbsTol;
00265
00267 void* mpCvodeMem;
00268
00273 long int mMaxSteps;
00274
00276 double mLastInternalStepSize;
00277
00282 void Init();
00283
00284 public:
00285
00291 AbstractCvodeSystem(unsigned numberOfStateVariables);
00292
00296 virtual ~AbstractCvodeSystem();
00297
00305 virtual void EvaluateYDerivatives(realtype time,
00306 const N_Vector y,
00307 N_Vector ydot)=0;
00308
00309
00326 virtual void EvaluateAnalyticJacobian(long int N, realtype time, N_Vector y, N_Vector ydot,
00327 CHASTE_CVODE_DENSE_MATRIX jacobian,
00328 N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
00329 {
00330 EXCEPTION("No analytic Jacobian has been defined for this system.");
00331 }
00332
00343 void SetForceReset(bool autoReset);
00344
00353 void SetMinimalReset(bool minimalReset);
00354
00365 void ResetSolver();
00366
00386 OdeSolution Solve(realtype tStart,
00387 realtype tEnd,
00388 realtype maxDt,
00389 realtype tSamp);
00390
00406 void Solve(realtype tStart,
00407 realtype tEnd,
00408 realtype maxDt);
00409
00416 void SetMaxSteps(long int numSteps);
00417
00422 long int GetMaxSteps();
00423
00431 void SetTolerances(double relTol=1e-5, double absTol=1e-7);
00432
00436 double GetRelativeTolerance();
00437
00441 double GetAbsoluteTolerance();
00442
00446 double GetLastStepSize();
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00465 bool GetUseAnalyticJacobian() const;
00466
00470 bool HasAnalyticJacobian() const;
00471
00478 void ForceUseOfNumericalJacobian(bool useNumericalJacobian = true);
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 };
00498
00499 CLASS_IS_ABSTRACT(AbstractCvodeSystem)
00500 BOOST_CLASS_VERSION(AbstractCvodeSystem, 1u)
00501
00502 #endif //_ABSTRACTCVODESYSTEM_HPP_
00503 #endif // CHASTE_CVODE
00504
00505