AbstractCvodeSystem.hpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, 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         if (version >= 1u)
00141         {
00142             archive & mHasAnalyticJacobian;
00143         }
00144 
00145         // Convert from N_Vector to std::vector for serialization
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         // We don't bother archiving CVODE's internal data, because it is missing then we'll just
00161         // get a new solver being initialised after a save/load.
00162 
00163 
00164         // This is always set up by subclass constructors, and is essentially
00165         // 'static' data, so shouldn't go in the archive.
00166         //archive &mpSystemInfo;
00167     }
00174     template<class Archive>
00175     void load(Archive & archive, const unsigned int version)
00176     {
00177         archive & mNumberOfStateVariables;
00178         archive & mUseAnalyticJacobian;
00179 
00180         // This is pretty much what the code was saying before.
00181         mHasAnalyticJacobian = mUseAnalyticJacobian;
00182         if (version >= 1u)
00183         {   // Overwrite if it has been archived though
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         // We don't bother archiving CVODE's internal data, because it is missing then we'll just
00205         // get a new solver being initialised after a save/load.
00206 
00207         // Do some checking on the parameters
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     /* NB This needs making into a doxygen comment if you bring the method back in.
00450      *
00451      * An alternative approach to stopping events; currently only useful with CVODE.
00452      * CVODE can search for roots (zeros) of this function while solving the ODE system,
00453      * and home in on them to find sign transitions to high precision.
00454      *
00455      * The default implementation here fakes a root function using CalculateStoppingEvent.
00456      *
00457      * @param time  the current time
00458      * @param rY  the current values of the state variables
00459      */
00460 //    virtual double CalculateRootFunction(double time, const std::vector<double>& rY);
00461 
00465     bool GetUseAnalyticJacobian() const;
00466 
00470     bool HasAnalyticJacobian() const;
00471 
00478     void ForceUseOfNumericalJacobian(bool useNumericalJacobian = true);
00479 
00480 // The following method may be useful to identify problems with the Analytic Jacobians, if anything goes wrong,
00481 // but #1795 seems to have got these working OK, so commented out for now.
00482 
00483 //    /*
00484 //     * Compare the calculated analytic jacobian to a numerical approximation, and throw if it looks silly.
00485 //     *
00486 //     * @param time  the current time
00487 //     * @param y  the current state variables
00488 //     * @param jacobian  the analytic jacobian matrix
00489 //     * @param tmp1  working memory of the correct size provided by CVODE for temporary calculations
00490 //     * @param tmp2  working memory of the correct size provided by CVODE for temporary calculations
00491 //     * @param tmp3  working memory of the correct size provided by CVODE for temporary calculations
00492 //     */
00493 //    void CheckAnalyticJacobian(realtype time, N_Vector y, N_Vector ydot,
00494 //                               CHASTE_CVODE_DENSE_MATRIX jacobian,
00495 //                               N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
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 

Generated by  doxygen 1.6.2