AbstractCvodeSystem.hpp

00001 /*
00002 
00003 Copyright (c) 2005-2014, 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 #include <sundials/sundials_dense.h> /* definitions DlsMat DENSE_ELEM */
00062 
00063 // CVODE changed their dense matrix type...
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         // Despite the fact that 3 of these variables actually live in our base class,
00135         // we still archive them here to maintain backwards compatibility,
00136         // this doesn't hurt
00137         archive & mNumberOfStateVariables;
00138         archive & mUseAnalyticJacobian;
00139 
00140         // Convert from N_Vector to std::vector for serialization
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         // We don't bother archiving CVODE's internal data, because it is missing then we'll just
00156         // get a new solver being initialised after a save/load.
00157 
00158 
00159         // This is always set up by subclass constructors, and is essentially
00160         // 'static' data, so shouldn't go in the archive.
00161         //archive &mpSystemInfo;
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         // We don't bother archiving CVODE's internal data, because it is missing then we'll just
00193         // get a new solver being initialised after a save/load.
00194 
00195         // Do some checking on the parameters
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     /* NB This needs making into a doxygen comment if you bring the method back in.
00435      *
00436      * An alternative approach to stopping events; currently only useful with CVODE.
00437      * CVODE can search for roots (zeros) of this function while solving the ODE system,
00438      * and home in on them to find sign transitions to high precision.
00439      *
00440      * The default implementation here fakes a root function using CalculateStoppingEvent.
00441      *
00442      * @param time  the current time
00443      * @param rY  the current values of the state variables
00444      */
00445 //    virtual double CalculateRootFunction(double time, const std::vector<double>& rY);
00446 
00450     bool GetUseAnalyticJacobian();
00451 
00458     void ForceUseOfNumericalJacobian(bool useNumericalJacobian = true);
00459 
00460 // The following method may be useful to identify problems with the Analytic Jacobians, if anything goes wrong,
00461 // but #1795 seems to have got these working OK, so commented out for now.
00462 
00463 //    /*
00464 //     * Compare the calculated analytic jacobian to a numerical approximation, and throw if it looks silly.
00465 //     *
00466 //     * @param time  the current time
00467 //     * @param y  the current state variables
00468 //     * @param jacobian  the analytic jacobian matrix
00469 //     * @param tmp1  working memory of the correct size provided by CVODE for temporary calculations
00470 //     * @param tmp2  working memory of the correct size provided by CVODE for temporary calculations
00471 //     * @param tmp3  working memory of the correct size provided by CVODE for temporary calculations
00472 //     */
00473 //    void CheckAnalyticJacobian(realtype time, N_Vector y, N_Vector ydot,
00474 //                               CHASTE_CVODE_DENSE_MATRIX jacobian,
00475 //                               N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
00476 
00477 };
00478 
00479 CLASS_IS_ABSTRACT(AbstractCvodeSystem)
00480 
00481 #endif //_ABSTRACTCVODESYSTEM_HPP_
00482 #endif // CHASTE_CVODE
00483 
00484 

Generated by  doxygen 1.6.2