AbstractCellPopulation.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 #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/utility/enable_if.hpp>
00057 #include <boost/type_traits/is_base_of.hpp>
00058 
00059 #include "Node.hpp"
00060 #include "CellPropertyRegistry.hpp"
00061 #include "Identifiable.hpp"
00062 
00063 // Needed here to avoid serialization errors (on Boost<1.37)
00064 #include "WildTypeCellMutationState.hpp"
00065 #include "ApcOneHitCellMutationState.hpp"
00066 #include "ApcTwoHitCellMutationState.hpp"
00067 #include "BetaCateninOneHitCellMutationState.hpp"
00068 #include "DefaultCellProliferativeType.hpp"
00069 #include "StemCellProliferativeType.hpp"
00070 #include "TransitCellProliferativeType.hpp"
00071 #include "DifferentiatedCellProliferativeType.hpp"
00072 #include "ApoptoticCellProperty.hpp"
00073 #include "CellLabel.hpp"
00074 #include "CellData.hpp"
00075 #include "AbstractMesh.hpp"
00076 #include "AbstractCellPopulationWriter.hpp"
00077 #include "AbstractCellWriter.hpp"
00078 
00084 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM=ELEMENT_DIM>
00085 class AbstractCellPopulation : public Identifiable
00086 {
00087 private:
00088 
00090     friend class boost::serialization::access;
00091 
00098     template<class Archive>
00099     void serialize(Archive & archive, const unsigned int version)
00100     {
00101         archive & mCells;
00102         archive & mLocationCellMap;
00103         archive & mCellLocationMap;
00104         archive & mCellCyclePhaseCount;
00105         archive & mpCellPropertyRegistry;
00106         archive & mOutputResultsForChasteVisualizer;
00107         archive & mCellWriters;
00108         archive & mCellPopulationWriters;
00109     }
00110 
00111 protected:
00112 
00114     std::map<unsigned, std::set<CellPtr> > mLocationCellMap;
00115 
00117     std::map<Cell*, unsigned> mCellLocationMap;
00118 
00120     AbstractMesh<ELEMENT_DIM, SPACE_DIM>& mrMesh;
00121 
00123     std::list<CellPtr> mCells;
00124 
00126     std::vector<unsigned> mCellCyclePhaseCount;
00127 
00129     std::vector<unsigned> mCellProliferativeTypesCount;
00130 
00132     std::vector<unsigned> mCellMutationStateCount;
00133 
00135     c_vector<double, SPACE_DIM> mCentroid;
00136 
00138     out_stream mpVtkMetaFile;
00139 
00141     boost::shared_ptr<CellPropertyRegistry> mpCellPropertyRegistry;
00142 
00144     bool mOutputResultsForChasteVisualizer;
00145 
00147     std::set<boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > > mCellWriters;
00148 
00150     std::set<boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > > mCellPopulationWriters;
00151 
00158     virtual void Validate()=0;
00159 
00168     virtual void WriteVtkResultsToFile(const std::string& rDirectory)=0;
00169 
00175     AbstractCellPopulation(AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rMesh);
00176 
00177 public:
00178 
00190     AbstractCellPopulation( AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
00191                             std::vector<CellPtr>& rCells,
00192                             const std::vector<unsigned> locationIndices=std::vector<unsigned>());
00193 
00197     virtual ~AbstractCellPopulation();
00198 
00202     void InitialiseCells();
00203 
00209     void SetDataOnAllCells(const std::string& dataName, double dataValue);
00210 
00214     AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rGetMesh();
00215 
00219     std::list<CellPtr>& rGetCells();
00220 
00227     virtual unsigned GetNumNodes()=0;
00228 
00238     virtual c_vector<double, SPACE_DIM> GetLocationOfCellCentre(CellPtr pCell)=0;
00239 
00249     virtual Node<SPACE_DIM>* GetNode(unsigned index)=0;
00250 
00260     virtual void SetNode(unsigned nodeIndex, ChastePoint<SPACE_DIM>& rNewLocation)=0;
00261 
00272     virtual bool IsCellAssociatedWithADeletedLocation(CellPtr pCell)=0;
00273 
00289     virtual CellPtr AddCell(CellPtr pNewCell,
00290                             const c_vector<double,SPACE_DIM>& rCellDivisionVector,
00291                             CellPtr pParentCell=CellPtr())=0;
00292 
00293     class Iterator; // Forward declaration; see below
00294 
00303     virtual unsigned RemoveDeadCells()=0;
00304 
00311     virtual void Update(bool hasHadBirthsOrDeaths=true)=0;
00312 
00322     std::vector<unsigned> GetCellMutationStateCount();
00323 
00333     std::vector<unsigned> GetCellProliferativeTypeCount();
00334 
00345     std::vector<unsigned> GetCellCyclePhaseCount();
00346 
00353     unsigned GetNumRealCells();
00354 
00361     unsigned GetNumAllCells();
00362 
00367     void SetCellAncestorsToLocationIndices();
00368 
00375     std::set<unsigned> GetCellAncestors();
00376 
00387     virtual CellPtr GetCellUsingLocationIndex(unsigned index);
00388 
00398     std::set<CellPtr> GetCellsUsingLocationIndex(unsigned index);
00399 
00407     bool IsCellAttachedToLocationIndex(unsigned index);
00408 
00419     void SetCellUsingLocationIndex(unsigned index, CellPtr pCell);
00420 
00427     virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell);
00428 
00435     virtual void RemoveCellUsingLocationIndex(unsigned index, CellPtr pCell);
00436 
00444     void MoveCellInLocationMap(CellPtr pCell, unsigned old_index, unsigned new_index);
00445 
00455     unsigned GetLocationIndexUsingCell(CellPtr pCell);
00456 
00460     boost::shared_ptr<CellPropertyRegistry> GetCellPropertyRegistry();
00461 
00466     void SetDefaultCellMutationStateAndProliferativeTypeOrdering();
00467 
00477     virtual double GetWidth(const unsigned& rDimension)=0;
00478 
00486     virtual double GetVolumeOfCell(CellPtr pCell)=0;
00487 
00497     virtual std::set<unsigned> GetNeighbouringNodeIndices(unsigned index)=0;
00498 
00502     c_vector<double, SPACE_DIM> GetCentroidOfCellPopulation();
00503 
00507     virtual void UpdateCellProcessLocation();
00508 
00514     virtual void OpenWritersFiles(const std::string& rDirectory);
00515 
00521     void OpenWritersFilesForAppend(const std::string& rDirectory);
00522 
00528     virtual void WriteResultsToFiles(const std::string& rDirectory);
00529 
00536     virtual void AcceptPopulationWriter(boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > pPopulationWriter)=0;
00537 
00545     virtual void AcceptCellWriter(boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > pCellWriter, CellPtr pCell)=0;
00546 
00550     virtual void GenerateCellResults();
00551 
00555     virtual void CloseOutputFiles();
00556 
00562     void OutputCellPopulationInfo(out_stream& rParamsFile);
00563 
00572     virtual void OutputCellPopulationParameters(out_stream& rParamsFile)=0;
00573 
00577     bool GetOutputResultsForChasteVisualizer();
00578 
00585     template<template <unsigned, unsigned> class T>
00586     void AddPopulationWriter()
00587     {
00588         mCellPopulationWriters.insert(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
00589     }
00590 
00597     template<template <unsigned, unsigned> class T>
00598     void AddCellWriter()
00599     {
00600         mCellWriters.insert(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
00601     }
00602 
00608     template<template <unsigned, unsigned> class T>
00609     bool HasWriter() const
00610     {
00611         for (typename std::set<boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > >::iterator pop_writer_iter = mCellPopulationWriters.begin();
00612              pop_writer_iter != mCellPopulationWriters.end();
00613              ++pop_writer_iter)
00614         {
00615             if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(pop_writer_iter->get()))
00616             {
00617                 return true;
00618             }
00619         }
00620         for (typename std::set<boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > >::iterator cell_writer = mCellWriters.begin();
00621              cell_writer != mCellWriters.end();
00622              ++cell_writer)
00623         {
00624             if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(cell_writer->get()))
00625             {
00626                 return true;
00627             }
00628         }
00629         return false;
00630     }
00631 
00637     void SetOutputResultsForChasteVisualizer(bool outputResultsForChasteVisualizer);
00638 
00643     c_vector<double,SPACE_DIM> GetSizeOfCellPopulation();
00644 
00651     virtual bool IsRoomToDivide(CellPtr pCell);
00652 
00660     std::pair<unsigned,unsigned> CreateOrderedPair(unsigned index1, unsigned index2);
00661 
00668     class Iterator
00669     {
00670     public:
00671 
00676         inline CellPtr operator*();
00677 
00683         inline CellPtr operator->();
00684 
00691         inline bool operator!=(const typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& rOther);
00692 
00697         inline Iterator& operator++();
00698 
00705         Iterator(AbstractCellPopulation& rCellPopulation, std::list<CellPtr>::iterator cellIter);
00706 
00710         virtual ~Iterator()
00711         {}
00712 
00713     private:
00714 
00722         virtual inline bool IsRealCell();
00723 
00728         inline bool IsAtEnd();
00729 
00731         AbstractCellPopulation& mrCellPopulation;
00732 
00734         std::list<CellPtr>::iterator mCellIter;
00735     };
00736 
00740     Iterator Begin();
00741 
00745     Iterator End();
00746 };
00747 
00748 TEMPLATED_CLASS_IS_ABSTRACT_1_UNSIGNED(AbstractCellPopulation)
00749 
00750 
00751 //         Iterator class implementation - most methods are inlined         //
00753 
00754 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00755 CellPtr AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator*()
00756 {
00757     assert(!IsAtEnd());
00758     return *mCellIter;
00759 }
00760 
00761 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00762 CellPtr AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator->()
00763 {
00764     assert(!IsAtEnd());
00765     return *mCellIter;
00766 }
00767 
00768 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00769 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator!=(const typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& rOther)
00770 {
00771     return mCellIter != rOther.mCellIter;
00772 }
00773 
00774 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00775 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator++()
00776 {
00777     do
00778     {
00779         ++mCellIter;
00780     }
00781     while (!IsAtEnd() && !IsRealCell());
00782 
00783     return (*this);
00784 }
00785 
00786 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00787 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::IsRealCell()
00788 {
00789     return !( mrCellPopulation.IsCellAssociatedWithADeletedLocation(*mCellIter) || (*this)->IsDead() );
00790 }
00791 
00792 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00793 bool AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::IsAtEnd()
00794 {
00795     return mCellIter == mrCellPopulation.rGetCells().end();
00796 }
00797 
00798 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00799 AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::Iterator(AbstractCellPopulation& rCellPopulation, std::list<CellPtr>::iterator cellIter)
00800     : mrCellPopulation(rCellPopulation),
00801       mCellIter(cellIter)
00802 {
00803     // The cell population can now return empty if it only has ghost nodes
00804     if (mrCellPopulation.rGetCells().empty())
00805     {
00806         mCellIter = mrCellPopulation.rGetCells().end();
00807     }
00808     else
00809     {
00810         // Make sure we start at a real cell
00811         if (mCellIter == mrCellPopulation.rGetCells().begin() && !IsRealCell())
00812         {
00813             ++(*this);
00814         }
00815     }
00816 }
00817 
00818 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00819 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Begin()
00820 {
00821     return Iterator(*this, this->mCells.begin());
00822 }
00823 
00824 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00825 typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::End()
00826 {
00827     return Iterator(*this, this->mCells.end());
00828 }
00829 
00830 #endif /*ABSTRACTCELLPOPULATION_HPP_*/

Generated by  doxygen 1.6.2