Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
CvodeAdaptor Class Reference

#include <CvodeAdaptor.hpp>

+ Inheritance diagram for CvodeAdaptor:
+ Collaboration diagram for CvodeAdaptor:

Public Member Functions

 CvodeAdaptor (double relTol=1e-4, double absTol=1e-6)
 
 ~CvodeAdaptor ()
 
void SetTolerances (double relTol=1e-4, double absTol=1e-6)
 
double GetRelativeTolerance ()
 
double GetAbsoluteTolerance ()
 
double GetLastStepSize ()
 
void SetForceReset (bool autoReset)
 
void SetMinimalReset (bool minimalReset)
 
void ResetSolver ()
 
OdeSolution Solve (AbstractOdeSystem *pOdeSystem, std::vector< double > &rYValues, double startTime, double endTime, double maxStep, double timeSampling)
 
void Solve (AbstractOdeSystem *pOdeSystem, std::vector< double > &rYValues, double startTime, double endTime, double maxStep)
 
void CheckForStoppingEvents ()
 
void SetMaxSteps (long int numSteps)
 
long int GetMaxSteps ()
 
- Public Member Functions inherited from AbstractIvpOdeSolver
virtual void SolveAndUpdateStateVariable (AbstractOdeSystem *pAbstractOdeSystem, double startTime, double endTime, double timeStep)
 
bool StoppingEventOccurred ()
 
double GetStoppingTime ()
 
 AbstractIvpOdeSolver ()
 
virtual ~AbstractIvpOdeSolver ()
 
- Public Member Functions inherited from Identifiable
virtual ~Identifiable ()
 
std::string GetIdentifier () const
 

Protected Member Functions

void SetupCvode (AbstractOdeSystem *pOdeSystem, std::vector< double > &rInitialY, double startTime, double maxStep)
 
void FreeCvodeMemory ()
 
void CvodeError (int flag, const char *msg)
 

Private Member Functions

template<class Archive >
void serialize (Archive &archive, const unsigned int version)
 
void RecordStoppingPoint (double stopTime, N_Vector yEnd)
 

Private Attributes

void * mpCvodeMem
 
CvodeData mData
 
double mRelTol
 
double mAbsTol
 
double mLastInternalStepSize
 
long int mMaxSteps
 
bool mCheckForRoots
 
N_Vector mLastSolutionState
 
double mLastSolutionTime
 
bool mForceReset
 
bool mForceMinimalReset
 

Friends

class boost::serialization::access
 

Additional Inherited Members

- Protected Attributes inherited from AbstractIvpOdeSolver
bool mStoppingEventOccurred
 
double mStoppingTime
 

Detailed Description

The CVODE adaptor ODE solver class. This class is used as a solver for Chaste standard AbstractOdeSystems that have a native vector type of std::vector<double> and so can be used to apply CVODE to systems that could also be solved with the other solvers in this folder.

N.B. If you know you are always going to want to use CVODE to solve your system, then it will be faster to write your system so it uses N_Vectors natively as an AbstractCvodeSystem. AbstractCvodeSystems have an in-built CVODE solver, and therefore don't need to use this class. For Chaste developers - this means there is some duplication of code between this class and AbstractCvodeSystem as both set up and use CVODE to solve systems. If you change one you should probably change both!

This class assumes that it will be solving stiff systems, so uses BDF/Newton.

The timeStep parameters of the abstract class are here used to specify *maximum* steps, since the solver is adaptive.

Repeated calls to Solve will no longer set up and delete CVODE memory, unless the following method is called:

SetForceReset(true) - reset each time Solve() is called

default behaviour - reset if state variables change, or we ask to solve from a different time than the last solve call finished.

SetMinimalReset(true) - ignore changes in state vars and just reset if the time is inconsistent.

Definition at line 105 of file CvodeAdaptor.hpp.

Constructor & Destructor Documentation

◆ CvodeAdaptor()

CvodeAdaptor::CvodeAdaptor ( double  relTol = 1e-4,
double  absTol = 1e-6 
)

Default constructor. Can optionally set relative and absolute tolerances.

Parameters
relTolthe relative tolerance for the solver
absTolthe absolute tolerance for the solver

Definition at line 564 of file CvodeAdaptor.cpp.

◆ ~CvodeAdaptor()

CvodeAdaptor::~CvodeAdaptor ( )

Destructor. Frees memory.

Definition at line 588 of file CvodeAdaptor.cpp.

References DeleteVector(), FreeCvodeMemory(), and mLastSolutionState.

Member Function Documentation

◆ CheckForStoppingEvents()

void CvodeAdaptor::CheckForStoppingEvents ( )

Make the solver check for stopping events using CVODE's rootfinding functionality.

By default we do not check.

Definition at line 629 of file CvodeAdaptor.cpp.

References mCheckForRoots.

◆ CvodeError()

void CvodeAdaptor::CvodeError ( int  flag,
const char *  msg 
)
protected

Report an error from CVODE.

This will (probably) never be called, since we supply an error handler function which throws an exception.

Parameters
flagerror flag
msgerror message

Definition at line 425 of file CvodeAdaptor.cpp.

References EXCEPTION.

Referenced by Solve(), and Solve().

◆ FreeCvodeMemory()

void CvodeAdaptor::FreeCvodeMemory ( )
protected

Free CVODE memory after solving a system of ODEs.

Definition at line 375 of file CvodeAdaptor.cpp.

References mpCvodeMem.

Referenced by ~CvodeAdaptor(), Solve(), and Solve().

◆ GetAbsoluteTolerance()

double CvodeAdaptor::GetAbsoluteTolerance ( )
Returns
the absolute tolerance.

Definition at line 619 of file CvodeAdaptor.cpp.

References mAbsTol.

◆ GetLastStepSize()

double CvodeAdaptor::GetLastStepSize ( )
Returns
the last step size used internally by CVODE in the last Solve call

Definition at line 624 of file CvodeAdaptor.cpp.

References mLastInternalStepSize.

◆ GetMaxSteps()

long int CvodeAdaptor::GetMaxSteps ( )
Returns
the maximum number of steps to be taken by the solver in its attempt to reach the next output time.

Definition at line 639 of file CvodeAdaptor.cpp.

References mMaxSteps.

◆ GetRelativeTolerance()

double CvodeAdaptor::GetRelativeTolerance ( )
Returns
the relative tolerance.

Definition at line 614 of file CvodeAdaptor.cpp.

References mRelTol.

◆ RecordStoppingPoint()

void CvodeAdaptor::RecordStoppingPoint ( double  stopTime,
N_Vector  yEnd 
)
private

Record where the last solve got to so we know whether to re-initialise.

Parameters
stopTimethe finishing time
yEndthe state variables at this time

Definition at line 594 of file CvodeAdaptor.cpp.

References CreateVectorIfEmpty(), GetVectorComponent(), GetVectorSize(), mForceReset, mLastSolutionState, mLastSolutionTime, and SetVectorComponent().

Referenced by Solve(), and Solve().

◆ ResetSolver()

void CvodeAdaptor::ResetSolver ( )

Successive calls to Solve will attempt to intelligently determine whether to re-initialise the internal CVODE solver, or whether we are simply extending the previous solution forward in time. This mechanism compares the state vector to its previous value, and the start time to the end of the last solve, which captures most cases where re-initialisation is required. However, changes to the RHS function can also require this, and cannot be automatically detected. In such cases users must call this function to force re-initialisation.

Definition at line 419 of file CvodeAdaptor.cpp.

References DeleteVector(), and mLastSolutionState.

Referenced by SetForceReset().

◆ serialize()

template<class Archive >
void CvodeAdaptor::serialize ( Archive &  archive,
const unsigned int  version 
)
inlineprivate

Archive, never used directly - boost uses this.

Parameters
archivethe archive
versionthe current version of this class

Definition at line 117 of file CvodeAdaptor.hpp.

References mAbsTol, mCheckForRoots, mLastInternalStepSize, mMaxSteps, and mRelTol.

◆ SetForceReset()

void CvodeAdaptor::SetForceReset ( bool  autoReset)

Set whether to automatically re-initialise CVODE on every call to Solve, or whether to attempt to guess when re-initialisation is needed. See also ResetSolver.

Parameters
autoResetwhether to reset on every Solve

Definition at line 400 of file CvodeAdaptor.cpp.

References mForceReset, ResetSolver(), and SetMinimalReset().

Referenced by SetMinimalReset().

◆ SetMaxSteps()

void CvodeAdaptor::SetMaxSteps ( long int  numSteps)

Change the maximum number of steps to be taken by the solver in its attempt to reach the next output time. Default is 500.

Parameters
numStepsnew maximum number of steps

Definition at line 634 of file CvodeAdaptor.cpp.

References mMaxSteps.

◆ SetMinimalReset()

void CvodeAdaptor::SetMinimalReset ( bool  minimalReset)

Set whether to reduce the checking done when guessing when re-initialisation is needed, so it ignores changes in the state variables. If call with true argument, will call SetForceReset(false).

Parameters
minimalResetwhether to avoid checking for changes in state variables

Definition at line 410 of file CvodeAdaptor.cpp.

References mForceMinimalReset, and SetForceReset().

Referenced by SetForceReset().

◆ SetTolerances()

void CvodeAdaptor::SetTolerances ( double  relTol = 1e-4,
double  absTol = 1e-6 
)

Set relative and absolute tolerances; both scalars. If no parameters are given, tolerances will be reset to default values.

Parameters
relTolthe relative tolerance for the solver
absTolthe absolute tolerance for the solver

Definition at line 608 of file CvodeAdaptor.cpp.

References mAbsTol, and mRelTol.

◆ SetupCvode()

void CvodeAdaptor::SetupCvode ( AbstractOdeSystem pOdeSystem,
std::vector< double > &  rInitialY,
double  startTime,
double  maxStep 
)
protected

Set up the CVODE data structures needed to solve the given system.

Parameters
pOdeSystemthe ODE system being solved
rInitialYinitial conditions vector
startTimewhen to simulate from
maxStepmaximum time step

Initial conditions for the ODE solver.

Definition at line 191 of file CvodeAdaptor.cpp.

References DeleteVector(), EXCEPTION, AbstractUntemplatedParameterisedSystem::GetNumberOfStateVariables(), GetVectorComponent(), GetVectorSize(), mAbsTol, mCheckForRoots, mData, mForceMinimalReset, mForceReset, mLastSolutionState, mLastSolutionTime, mMaxSteps, mpCvodeMem, mRelTol, CvodeData_::pSystem, CvodeData_::pY, and CompareDoubles::WithinAnyTolerance().

Referenced by Solve(), and Solve().

◆ Solve() [1/2]

void CvodeAdaptor::Solve ( AbstractOdeSystem pOdeSystem,
std::vector< double > &  rYValues,
double  startTime,
double  endTime,
double  maxStep 
)
virtual

Solve the given ODE system, storing the final result in rYValues.

Parameters
pOdeSystemthe ODE system to solve
rYValuesthe initial state variable values; will be filled in with the final values on return
startTimethe time to start solving at
endTimethe time to solve to
maxStepthe maximum time step to be taken by the adaptive solver

Implements AbstractIvpOdeSolver.

Definition at line 509 of file CvodeAdaptor.cpp.

References AbstractOdeSystem::CalculateStoppingEvent(), CvodeError(), DeleteVector(), EXCEPTION, FreeCvodeMemory(), mCheckForRoots, mLastInternalStepSize, mpCvodeMem, AbstractIvpOdeSolver::mStoppingEventOccurred, AbstractIvpOdeSolver::mStoppingTime, RecordStoppingPoint(), SetupCvode(), and UNUSED_OPT.

◆ Solve() [2/2]

OdeSolution CvodeAdaptor::Solve ( AbstractOdeSystem pOdeSystem,
std::vector< double > &  rYValues,
double  startTime,
double  endTime,
double  maxStep,
double  timeSampling 
)
virtual

Solve the given ODE system, returning the solution at sampling intervals.

Parameters
pOdeSystemthe ODE system to solve
rYValuesthe initial state variable values (note: this vector will also be used as working memory)
startTimethe time to start solving at
endTimethe time to solve to
maxStepthe maximum time step to be taken by the adaptive solver
timeSamplingthe interval at which to sample the solution
Returns
the solution

Implements AbstractIvpOdeSolver.

Definition at line 436 of file CvodeAdaptor.cpp.

References TimeStepper::AdvanceOneTimeStep(), AbstractOdeSystem::CalculateStoppingEvent(), CvodeError(), DeleteVector(), TimeStepper::EstimateTimeSteps(), EXCEPTION, FreeCvodeMemory(), TimeStepper::GetNextTime(), AbstractUntemplatedParameterisedSystem::GetSystemInformation(), TimeStepper::GetTotalTimeStepsTaken(), TimeStepper::IsTimeAtEnd(), mCheckForRoots, mLastInternalStepSize, mLastSolutionTime, mpCvodeMem, AbstractIvpOdeSolver::mStoppingEventOccurred, AbstractIvpOdeSolver::mStoppingTime, RecordStoppingPoint(), OdeSolution::rGetSolutions(), OdeSolution::rGetTimes(), OdeSolution::SetNumberOfTimeSteps(), OdeSolution::SetOdeSystemInformation(), SetupCvode(), and UNUSED_OPT.

Friends And Related Symbol Documentation

◆ boost::serialization::access

friend class boost::serialization::access
friend

Needed for serialization.

Definition at line 109 of file CvodeAdaptor.hpp.

Member Data Documentation

◆ mAbsTol

double CvodeAdaptor::mAbsTol
private

Absolute tolerance for the ODE solver.

Definition at line 138 of file CvodeAdaptor.hpp.

Referenced by GetAbsoluteTolerance(), serialize(), SetTolerances(), and SetupCvode().

◆ mCheckForRoots

bool CvodeAdaptor::mCheckForRoots
private

Whether to check for stopping events.

Definition at line 150 of file CvodeAdaptor.hpp.

Referenced by CheckForStoppingEvents(), serialize(), SetupCvode(), Solve(), and Solve().

◆ mData

CvodeData CvodeAdaptor::mData
private

The CVODE data structure.

Definition at line 132 of file CvodeAdaptor.hpp.

Referenced by SetupCvode().

◆ mForceMinimalReset

bool CvodeAdaptor::mForceMinimalReset
private

Whether to ignore changes in the state variables when deciding whether to reset.

Definition at line 162 of file CvodeAdaptor.hpp.

Referenced by SetMinimalReset(), and SetupCvode().

◆ mForceReset

bool CvodeAdaptor::mForceReset
private

Whether to automatically reset CVODE on each Solve call.

Definition at line 159 of file CvodeAdaptor.hpp.

Referenced by RecordStoppingPoint(), SetForceReset(), and SetupCvode().

◆ mLastInternalStepSize

double CvodeAdaptor::mLastInternalStepSize
private

The size of the previous timestep.

Definition at line 141 of file CvodeAdaptor.hpp.

Referenced by GetLastStepSize(), serialize(), Solve(), and Solve().

◆ mLastSolutionState

N_Vector CvodeAdaptor::mLastSolutionState
private

Remember where the last solve got to so we know whether to re-initialise.

Definition at line 153 of file CvodeAdaptor.hpp.

Referenced by ~CvodeAdaptor(), RecordStoppingPoint(), ResetSolver(), and SetupCvode().

◆ mLastSolutionTime

double CvodeAdaptor::mLastSolutionTime
private

Remember where the last solve got to so we know whether to re-initialise.

Definition at line 156 of file CvodeAdaptor.hpp.

Referenced by RecordStoppingPoint(), SetupCvode(), and Solve().

◆ mMaxSteps

long int CvodeAdaptor::mMaxSteps
private

The maximum number of steps to be taken by the solver in its attempt to reach the next output time.

Definition at line 147 of file CvodeAdaptor.hpp.

Referenced by GetMaxSteps(), serialize(), SetMaxSteps(), and SetupCvode().

◆ mpCvodeMem

void* CvodeAdaptor::mpCvodeMem
private

Pointer to the CVODE memory block.

Definition at line 129 of file CvodeAdaptor.hpp.

Referenced by FreeCvodeMemory(), SetupCvode(), Solve(), and Solve().

◆ mRelTol

double CvodeAdaptor::mRelTol
private

Relative tolerance for the ODE solver.

Definition at line 135 of file CvodeAdaptor.hpp.

Referenced by GetRelativeTolerance(), serialize(), SetTolerances(), and SetupCvode().


The documentation for this class was generated from the following files: