AbstractOdeSystem.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #ifndef _ABSTRACTODESYSTEM_HPP_
00030 #define _ABSTRACTODESYSTEM_HPP_
00031 
00032 #include <vector>
00033 #include <string>
00034 #include <algorithm>
00035 
00036 
00037 #include "ChasteSerialization.hpp"
00038 #include <boost/serialization/split_member.hpp>
00039 #include <boost/serialization/vector.hpp>
00040 #include <boost/serialization/version.hpp>
00041 #include "ClassIsAbstract.hpp"
00042 
00043 #include "AbstractParameterisedSystem.hpp"
00044 #include "Exception.hpp"
00045 
00080 class AbstractOdeSystem : public AbstractParameterisedSystem<std::vector<double> >
00081 {
00082     friend class TestAbstractOdeSystem;
00083 
00084 private:
00085 
00086 
00087     friend class boost::serialization::access;
00094     template<class Archive>
00095     void save(Archive & archive, const unsigned int version) const
00096     {
00097         // Despite the fact that 3 of these variables actually live in our base class,
00098         // we still archive them here to maintain backwards compatibility.  Since the
00099         // N_Vector version of AbstractParameterisedSystem doesn't get checkpointed yet,
00100         // this doesn't hurt.
00101         archive & mNumberOfStateVariables;
00102         archive & mUseAnalyticJacobian;
00103         archive & mStateVariables;
00104         archive & mParameters;
00105 
00106         if (version > 0)
00107         {
00108             archive & rGetParameterNames();
00109         }
00110 
00111         // This is always set up by subclass constructors, and is essentially
00112         // 'static' data, so shouldn't go in the archive.
00113         //archive &mpSystemInfo;
00114     }
00121     template<class Archive>
00122     void load(Archive & archive, const unsigned int version)
00123     {
00124         archive & mNumberOfStateVariables;
00125         archive & mUseAnalyticJacobian;
00126         archive & mStateVariables;
00127         std::vector<double> parameters;
00128         archive & parameters;
00129 
00130         if (version > 0)
00131         {
00132             std::vector<std::string> param_names;
00133             archive & param_names;
00134 
00135             if (mParameters.size() != rGetParameterNames().size())
00136             {
00137                 // Subclass constructor didn't give default values, so we need the archive to provide them all
00138                 if (param_names.size() != rGetParameterNames().size())
00139                 {
00140                     EXCEPTION("Number of ODE parameters in archive does not match number in class.");
00141                 }
00142                 mParameters.resize(rGetParameterNames().size());
00143             }
00144 
00145             // Check whether the archive specifies parameters that don't appear in this class,
00146             // and create a map from archive index to local index
00147             std::vector<unsigned> index_map(param_names.size());
00148             for (unsigned i=0; i<param_names.size(); ++i)
00149             {
00150                 index_map[i] = find(rGetParameterNames().begin(), rGetParameterNames().end(), param_names[i])
00151                                - rGetParameterNames().begin();
00152                 if (index_map[i] == rGetParameterNames().size())
00153                 {
00154                     EXCEPTION("Archive specifies a parameter '" + param_names[i] + "' which does not appear in this class.");
00155                 }
00156             }
00157 
00158             for (unsigned i=0; i<param_names.size(); ++i)
00159             {
00160                 mParameters[index_map[i]] = parameters[i];
00161             }
00162         }
00163         else
00164         {
00165             mParameters = parameters;
00166         }
00167     }
00168     BOOST_SERIALIZATION_SPLIT_MEMBER()
00169 
00170 protected:
00171 
00173     bool mUseAnalyticJacobian;
00174 
00175 public:
00176 
00182     AbstractOdeSystem(unsigned numberOfStateVariables);
00183 
00187     virtual ~AbstractOdeSystem();
00188 
00196     virtual void EvaluateYDerivatives(double time, const std::vector<double>& rY,
00197                                       std::vector<double>& rDY)=0;
00198 
00210     virtual bool CalculateStoppingEvent(double time, const std::vector<double>& rY);
00211 
00222     virtual double CalculateRootFunction(double time, const std::vector<double>& rY);
00223 
00229     bool GetUseAnalyticJacobian();
00230 
00236     const std::vector<double>& rGetConstStateVariables() const;
00237 };
00238 
00239 CLASS_IS_ABSTRACT(AbstractOdeSystem)
00240 BOOST_CLASS_VERSION(AbstractOdeSystem, 1u)
00241 
00242 #endif //_ABSTRACTODESYSTEM_HPP_
Generated on Thu Dec 22 13:00:16 2011 for Chaste by  doxygen 1.6.3