Chaste Release::3.1
AbstractCvodeSystem.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // This is only needed to prevent compilation errors on PETSc 2.2/Boost 1.33.1 combo
00045 #include "UblasVectorInclude.hpp"
00046 
00047 // Chaste includes
00048 #include "OdeSolution.hpp"
00049 #include "AbstractParameterisedSystem.hpp"
00050 #include "Exception.hpp"
00051 #include "VectorHelperFunctions.hpp"
00052 
00053 // Serialiazation
00054 #include "ChasteSerialization.hpp"
00055 #include <boost/serialization/split_member.hpp>
00056 #include <boost/serialization/vector.hpp>
00057 #include "ClassIsAbstract.hpp"
00058 
00059 // CVODE headers
00060 #include <nvector/nvector_serial.h>
00061 
00101 class AbstractCvodeSystem : public AbstractParameterisedSystem<N_Vector>
00102 {
00103 private:
00104 
00105     friend class boost::serialization::access;
00112     template<class Archive>
00113     void save(Archive & archive, const unsigned int version) const
00114     {
00115         // Despite the fact that 3 of these variables actually live in our base class,
00116         // we still archive them here to maintain backwards compatibility,
00117         // this doesn't hurt
00118         archive & mNumberOfStateVariables;
00119         archive & mUseAnalyticJacobian;
00120 
00121         // Convert from N_Vector to std::vector for serialization
00122         const std::vector<double> state_vars = MakeStdVec(mStateVariables);
00123         archive & state_vars;
00124         const std::vector<double> params = MakeStdVec(mParameters);
00125         archive & params;
00126         archive & rGetParameterNames();
00127 
00128         archive & mLastSolutionTime;
00129         archive & mAutoReset;
00130         archive & mForceMinimalReset;
00131         archive & mRelTol;
00132         archive & mAbsTol;
00133         archive & mMaxSteps;
00134         archive & mLastInternalStepSize;
00135 
00136         // We don't bother archiving CVODE's internal data, because it is missing then we'll just
00137         // get a new solver being initialised after a save/load.
00138 
00139 
00140         // This is always set up by subclass constructors, and is essentially
00141         // 'static' data, so shouldn't go in the archive.
00142         //archive &mpSystemInfo;
00143     }
00150     template<class Archive>
00151     void load(Archive & archive, const unsigned int version)
00152     {
00153         archive & mNumberOfStateVariables;
00154         archive & mUseAnalyticJacobian;
00155 
00156         std::vector<double> state_vars;
00157         archive & state_vars;
00158         CopyFromStdVector(state_vars,mStateVariables);
00159 
00160         std::vector<double> parameters;
00161         archive & parameters;
00162 
00163         std::vector<std::string> param_names;
00164         archive & param_names;
00165         archive & mLastSolutionTime;
00166         archive & mAutoReset;
00167         archive & mForceMinimalReset;
00168         archive & mRelTol;
00169         archive & mAbsTol;
00170         archive & mMaxSteps;
00171         archive & mLastInternalStepSize;
00172 
00173         // We don't bother archiving CVODE's internal data, because it is missing then we'll just
00174         // get a new solver being initialised after a save/load.
00175 
00176         // Do some checking on the parameters
00177         CheckParametersOnLoad(parameters,param_names);
00178     }
00179     BOOST_SERIALIZATION_SPLIT_MEMBER()
00180 
00181     
00188     void SetupCvode(N_Vector initialConditions,
00189                     realtype tStart,
00190                     realtype maxDt);
00191 
00196     void RecordStoppingPoint(double stopTime);
00197 
00199     void FreeCvodeMemory();
00200 
00207     void CvodeError(int flag, const char * msg);
00208 
00210     N_Vector mLastSolutionState;
00211 
00213     double mLastSolutionTime;
00214 
00216     bool mAutoReset;
00217 
00219     bool mForceMinimalReset;
00220 
00221 protected:
00222 
00224     bool mUseAnalyticJacobian;
00225 
00227     double mRelTol;
00228 
00230     double mAbsTol;
00231 
00233     void* mpCvodeMem;
00234 
00239     long int mMaxSteps;
00240 
00242     double mLastInternalStepSize;
00243 
00248     void Init();
00249 
00250 public:
00251 
00257     AbstractCvodeSystem(unsigned numberOfStateVariables);
00258 
00262     virtual ~AbstractCvodeSystem();
00263 
00271     virtual void EvaluateYDerivatives(realtype time,
00272                                       N_Vector y,
00273                                       N_Vector ydot)=0;
00274 
00284     void SetAutoReset(bool autoReset);
00285 
00293     void SetMinimalReset(bool minimalReset);
00294 
00305     void ResetSolver();
00306 
00325     OdeSolution Solve(realtype tStart,
00326                       realtype tEnd,
00327                       realtype maxDt,
00328                       realtype tSamp);
00329 
00345     void Solve(realtype tStart,
00346                realtype tEnd,
00347                realtype maxDt);
00348 
00355     void SetMaxSteps(long int numSteps);
00356 
00361     long int GetMaxSteps();
00362 
00370     void SetTolerances(double relTol=1e-5, double absTol=1e-7);
00371 
00375     double GetRelativeTolerance();
00376 
00380     double GetAbsoluteTolerance();
00381 
00385     double GetLastStepSize();
00386 
00387 
00388 //    /**
00389 //     * An alternative approach to stopping events; currently only useful with CVODE.
00390 //     * CVODE can search for roots (zeros) of this function while solving the ODE system,
00391 //     * and home in on them to find sign transitions to high precision.
00392 //     *
00393 //     * The default implementation here fakes a root function using CalculateStoppingEvent.
00394 //     *
00395 //     * @param time  the current time
00396 //     * @param rY  the current values of the state variables
00397 //     */
00398 //    virtual double CalculateRootFunction(double time, const std::vector<double>& rY);
00399 //
00400 //    /**
00401 //     * Get whether an analytic Jacobian is used.
00402 //     *
00403 //     * @return mUseAnalyticJacobian
00404 //     */
00405 //    bool GetUseAnalyticJacobian();
00406 
00407 };
00408 
00409 CLASS_IS_ABSTRACT(AbstractCvodeSystem)
00410 
00411 #endif //_ABSTRACTCVODESYSTEM_HPP_
00412 #endif // CHASTE_CVODE
00413 
00414