AbstractCellPopulation.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 #ifndef ABSTRACTCELLPOPULATION_HPP_
00037 #define ABSTRACTCELLPOPULATION_HPP_
00038 
00039 #include "Cell.hpp"
00040 #include "OutputFileHandler.hpp"
00041 
00042 #include <list>
00043 #include <map>
00044 #include <vector>
00045 #include <boost/shared_ptr.hpp>
00046 
00047 #include "ChasteSerialization.hpp"
00048 #include "ClassIsAbstract.hpp"
00049 
00050 #include <boost/serialization/vector.hpp>
00051 #include <boost/serialization/list.hpp>
00052 #include <boost/serialization/map.hpp>
00053 #include <boost/serialization/set.hpp>
00054 #include <boost/serialization/shared_ptr.hpp>
00055 
00056 #include <boost/foreach.hpp>
00057 #include <boost/utility/enable_if.hpp>
00058 #include <boost/type_traits/is_base_of.hpp>
00059 
00060 #include "AbstractMesh.hpp"
00061 #include "CellPropertyRegistry.hpp"
00062 #include "Identifiable.hpp"
00063 #include "AbstractCellPopulationCountWriter.hpp"
00064 #include "AbstractCellPopulationWriter.hpp"
00065 #include "AbstractCellWriter.hpp"
00066 
00067 // These #includes are needed for SetDefaultCellMutationStateAndProliferativeTypeOrdering()
00068 #include "WildTypeCellMutationState.hpp"
00069 #include "ApcOneHitCellMutationState.hpp"
00070 #include "ApcTwoHitCellMutationState.hpp"
00071 #include "BetaCateninOneHitCellMutationState.hpp"
00072 #include "DefaultCellProliferativeType.hpp"
00073 #include "StemCellProliferativeType.hpp"
00074 #include "TransitCellProliferativeType.hpp"
00075 #include "DifferentiatedCellProliferativeType.hpp"
00076 
00082 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM=ELEMENT_DIM>
00083 class AbstractCellPopulation : public Identifiable
00084 {
00085 private:
00086 
00088     friend class boost::serialization::access;
00089 
00096     template<class Archive>
00097     void serialize(Archive & archive, const unsigned int version)
00098     {
00099         archive & mCells;
00100         archive & mLocationCellMap;
00101         archive & mCellLocationMap;
00102         archive & mpCellPropertyRegistry;
00103         archive & mOutputResultsForChasteVisualizer;
00104         archive & mCellWriters;
00105         archive & mCellPopulationWriters;
00106         archive & mCellPopulationCountWriters;
00107     }
00108 
00117     void OpenRoundRobinWritersFilesForAppend(OutputFileHandler& rOutputFileHandler);
00118 
00126     void CloseRoundRobinWritersFiles();
00127 
00128 protected:
00129 
00131     std::map<unsigned, std::set<CellPtr> > mLocationCellMap;
00132 
00134     std::map<Cell*, unsigned> mCellLocationMap;
00135 
00137     AbstractMesh<ELEMENT_DIM, SPACE_DIM>& mrMesh;
00138 
00140     std::list<CellPtr> mCells;
00141 
00143     c_vector<double, SPACE_DIM> mCentroid;
00144 
00146     out_stream mpVtkMetaFile;
00147 
00149     boost::shared_ptr<CellPropertyRegistry> mpCellPropertyRegistry;
00150 
00152     bool mOutputResultsForChasteVisualizer;
00153 
00155     std::vector<boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > > mCellWriters;
00156 
00158     std::vector<boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > > mCellPopulationWriters;
00159 
00161     std::vector<boost::shared_ptr<AbstractCellPopulationCountWriter<ELEMENT_DIM, SPACE_DIM> > > mCellPopulationCountWriters;
00162 
00169     virtual void Validate()=0;
00170 
00179     virtual void WriteVtkResultsToFile(const std::string& rDirectory)=0;
00180 
00186     AbstractCellPopulation(AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rMesh);
00187 
00188 public:
00189 
00201     AbstractCellPopulation( AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
00202                             std::vector<CellPtr>& rCells,
00203                             const std::vector<unsigned> locationIndices=std::vector<unsigned>());
00204 
00208     virtual ~AbstractCellPopulation();
00209 
00213     void InitialiseCells();
00214 
00220     void SetDataOnAllCells(const std::string& dataName, double dataValue);
00221 
00225     AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rGetMesh();
00226 
00230     std::list<CellPtr>& rGetCells();
00231 
00238     virtual unsigned GetNumNodes()=0;
00239 
00249     virtual c_vector<double, SPACE_DIM> GetLocationOfCellCentre(CellPtr pCell)=0;
00250 
00260     virtual Node<SPACE_DIM>* GetNode(unsigned index)=0;
00261 
00271     virtual void SetNode(unsigned nodeIndex, ChastePoint<SPACE_DIM>& rNewLocation)=0;
00272 
00283     virtual bool IsCellAssociatedWithADeletedLocation(CellPtr pCell)=0;
00284 
00300     virtual CellPtr AddCell(CellPtr pNewCell,
00301                             const c_vector<double,SPACE_DIM>& rCellDivisionVector,
00302                             CellPtr pParentCell=CellPtr())=0;
00303 
00304     class Iterator; // Forward declaration; see below
00305 
00314     virtual unsigned RemoveDeadCells()=0;
00315 
00322     virtual void Update(bool hasHadBirthsOrDeaths=true)=0;
00323 
00333     std::vector<unsigned> GetCellMutationStateCount();
00334 
00344     std::vector<unsigned> GetCellProliferativeTypeCount();
00345 
00356     std::vector<unsigned> GetCellCyclePhaseCount();
00357 
00364     unsigned GetNumRealCells();
00365 
00372     unsigned GetNumAllCells();
00373 
00378     void SetCellAncestorsToLocationIndices();
00379 
00386     std::set<unsigned> GetCellAncestors();
00387 
00398     virtual CellPtr GetCellUsingLocationIndex(unsigned index);
00399 
00409     std::set<CellPtr> GetCellsUsingLocationIndex(unsigned index);
00410 
00418     bool IsCellAttachedToLocationIndex(unsigned index);
00419 
00430     void SetCellUsingLocationIndex(unsigned index, CellPtr pCell);
00431 
00438     virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell);
00439 
00446     virtual void RemoveCellUsingLocationIndex(unsigned index, CellPtr pCell);
00447 
00455     void MoveCellInLocationMap(CellPtr pCell, unsigned old_index, unsigned new_index);
00456 
00466     unsigned GetLocationIndexUsingCell(CellPtr pCell);
00467 
00471     boost::shared_ptr<CellPropertyRegistry> GetCellPropertyRegistry();
00472 
00477     void SetDefaultCellMutationStateAndProliferativeTypeOrdering();
00478 
00488     virtual double GetWidth(const unsigned& rDimension)=0;
00489 
00497     virtual double GetVolumeOfCell(CellPtr pCell)=0;
00498 
00508     virtual std::set<unsigned> GetNeighbouringNodeIndices(unsigned index)=0;
00509 
00519     virtual std::set<unsigned> GetNeighbouringLocationIndices(CellPtr pCell)=0;
00520 
00524     c_vector<double, SPACE_DIM> GetCentroidOfCellPopulation();
00525 
00529     virtual void UpdateCellProcessLocation();
00530 
00544     virtual void OpenWritersFiles(OutputFileHandler& rOutputFileHandler);
00545 
00552     void CloseWritersFiles();
00553 
00559     virtual void WriteResultsToFiles(const std::string& rDirectory);
00560 
00567     virtual void AcceptPopulationWriter(boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > pPopulationWriter)=0;
00568 
00575     virtual void AcceptPopulationCountWriter(boost::shared_ptr<AbstractCellPopulationCountWriter<ELEMENT_DIM, SPACE_DIM> > pPopulationCountWriter)=0;
00576 
00584     virtual void AcceptCellWriter(boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > pCellWriter, CellPtr pCell)=0;
00585 
00591     void OutputCellPopulationInfo(out_stream& rParamsFile);
00592 
00601     virtual void OutputCellPopulationParameters(out_stream& rParamsFile)=0;
00602 
00606     bool GetOutputResultsForChasteVisualizer();
00607 
00614     template<template <unsigned, unsigned> class T>
00615     void AddPopulationWriter()
00616     {
00617         mCellPopulationWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
00618     }
00619 
00626     template<template <unsigned, unsigned> class T>
00627     void AddCellWriter()
00628     {
00629         mCellWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
00630     }
00631 
00638     template<template <unsigned, unsigned> class T>
00639     void AddCellPopulationCountWriter()
00640     {
00641         mCellPopulationCountWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
00642     }
00643 
00653     void AddPopulationWriter(boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > pPopulationWriter)
00654     {
00655         mCellPopulationWriters.push_back(pPopulationWriter);
00656     }
00657 
00667     void AddCellWriter(boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > pCellWriter)
00668     {
00669         mCellWriters.push_back(pCellWriter);
00670     }
00671 
00681     void AddCellPopulationCountWriter(boost::shared_ptr<AbstractCellPopulationCountWriter<ELEMENT_DIM, SPACE_DIM> > pCellPopulationCountWriter)
00682     {
00683         mCellPopulationCountWriters.push_back(pCellPopulationCountWriter);
00684     }
00685 
00691     template<template <unsigned, unsigned> class T>
00692     bool HasWriter() const
00693     {
00694         typedef AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> population_writer_t;
00695         BOOST_FOREACH(boost::shared_ptr<population_writer_t> p_pop_writer, mCellPopulationWriters)
00696         {
00697             if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_pop_writer.get()))
00698             {
00699                 return true;
00700             }
00701         }
00702         typedef AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> cell_writer_t;
00703         BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
00704         {
00705             if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_cell_writer.get()))
00706             {
00707                 return true;
00708             }
00709         }
00710         typedef AbstractCellPopulationCountWriter<ELEMENT_DIM, SPACE_DIM> count_writer_t;
00711         BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
00712         {
00713             if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_count_writer.get()))
00714             {
00715                 return true;
00716             }
00717         }
00718         return false;
00719     }
00720 
00726     void SetOutputResultsForChasteVisualizer(bool outputResultsForChasteVisualizer);
00727 
00732     c_vector<double,SPACE_DIM> GetSizeOfCellPopulation();
00733 
00740     virtual bool IsRoomToDivide(CellPtr pCell);
00741 
00749     std::pair<unsigned,unsigned> CreateOrderedPair(unsigned index1, unsigned index2);
00750 
00757     class Iterator
00758     {
00759     public:
00760 
00765         inline CellPtr operator*();
00766 
00772         inline CellPtr operator->();
00773 
00780         inline bool operator!=(const typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& rOther);
00781 
00786         inline Iterator& operator++();
00787 
00794         Iterator(AbstractCellPopulation& rCellPopulation, std::list<CellPtr>::iterator cellIter);
00795 
00799         virtual ~Iterator()
00800         {}
00801 
00802     private:
00803 
00811         virtual inline bool IsRealCell();
00812 
00817         inline bool IsAtEnd();
00818 
00820         AbstractCellPopulation& mrCellPopulation;
00821 
00823         std::list<CellPtr>::iterator mCellIter;
00824     };
00825 
00829     Iterator Begin();
00830 
00834     Iterator End();
00835 };
00836 
00837 TEMPLATED_CLASS_IS_ABSTRACT_1_UNSIGNED(AbstractCellPopulation)
00838 
00839 
00840 //         Iterator class implementation - most methods are inlined         //
00842 
00843 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00844 CellPtr AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator*()
00845 {
00846     assert(!IsAtEnd());
00847     return *mCellIter;
00848 }
00849 
00850 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00851 CellPtr AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator->()
00852 {
00853     assert(!IsAtEnd());
00854     return *mCellIter;
00855 }
00856 
00857 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00858 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator!=(const typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& rOther)
00859 {
00860     return mCellIter != rOther.mCellIter;
00861 }
00862 
00863 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00864 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator++()
00865 {
00866     do
00867     {
00868         ++mCellIter;
00869     }
00870     while (!IsAtEnd() && !IsRealCell());
00871 
00872     return (*this);
00873 }
00874 
00875 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00876 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::IsRealCell()
00877 {
00878     return !( mrCellPopulation.IsCellAssociatedWithADeletedLocation(*mCellIter) || (*this)->IsDead() );
00879 }
00880 
00881 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00882 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::IsAtEnd()
00883 {
00884     return mCellIter == mrCellPopulation.rGetCells().end();
00885 }
00886 
00887 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00888 AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::Iterator(AbstractCellPopulation& rCellPopulation, std::list<CellPtr>::iterator cellIter)
00889     : mrCellPopulation(rCellPopulation),
00890       mCellIter(cellIter)
00891 {
00892     // The cell population can now return empty if it only has ghost nodes
00893     if (mrCellPopulation.rGetCells().empty())
00894     {
00895         mCellIter = mrCellPopulation.rGetCells().end();
00896     }
00897     else
00898     {
00899         // Make sure we start at a real cell
00900         if (mCellIter == mrCellPopulation.rGetCells().begin() && !IsRealCell())
00901         {
00902             ++(*this);
00903         }
00904     }
00905 }
00906 
00907 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00908 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Begin()
00909 {
00910     return Iterator(*this, this->mCells.begin());
00911 }
00912 
00913 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00914 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::End()
00915 {
00916     return Iterator(*this, this->mCells.end());
00917 }
00918 
00919 #endif /*ABSTRACTCELLPOPULATION_HPP_*/

Generated by  doxygen 1.6.2