Chaste  Release::3.4
AbstractCellPopulation.hpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef ABSTRACTCELLPOPULATION_HPP_
37 #define ABSTRACTCELLPOPULATION_HPP_
38 
39 #include "Cell.hpp"
40 #include "OutputFileHandler.hpp"
41 
42 #include <list>
43 #include <map>
44 #include <vector>
45 #include <boost/shared_ptr.hpp>
46 
47 #include "ChasteSerialization.hpp"
48 #include "ClassIsAbstract.hpp"
49 
50 #include <boost/serialization/vector.hpp>
51 #include <boost/serialization/list.hpp>
52 #include <boost/serialization/map.hpp>
53 #include <boost/serialization/set.hpp>
54 #include <boost/serialization/shared_ptr.hpp>
55 
56 #include <boost/foreach.hpp>
57 #include <boost/utility/enable_if.hpp>
58 #include <boost/type_traits/is_base_of.hpp>
59 
60 #include "AbstractMesh.hpp"
61 #include "CellPropertyRegistry.hpp"
62 #include "Identifiable.hpp"
63 #include "AbstractCellPopulationCountWriter.hpp"
64 #include "AbstractCellPopulationWriter.hpp"
65 #include "AbstractCellWriter.hpp"
66 
67 // These #includes are needed for SetDefaultCellMutationStateAndProliferativeTypeOrdering()
68 #include "WildTypeCellMutationState.hpp"
69 #include "ApcOneHitCellMutationState.hpp"
70 #include "ApcTwoHitCellMutationState.hpp"
71 #include "BetaCateninOneHitCellMutationState.hpp"
72 #include "DefaultCellProliferativeType.hpp"
73 #include "StemCellProliferativeType.hpp"
74 #include "TransitCellProliferativeType.hpp"
75 #include "DifferentiatedCellProliferativeType.hpp"
76 
82 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM=ELEMENT_DIM>
84 {
85 private:
86 
88  friend class boost::serialization::access;
89 
96  template<class Archive>
97  void serialize(Archive & archive, const unsigned int version)
98  {
99  archive & mCells;
100  archive & mLocationCellMap;
101  archive & mCellLocationMap;
102  archive & mpCellPropertyRegistry;
104  archive & mCellWriters;
105  archive & mCellPopulationWriters;
106  archive & mCellPopulationCountWriters;
107  }
108 
117  void OpenRoundRobinWritersFilesForAppend(OutputFileHandler& rOutputFileHandler);
118 
127 
128 protected:
129 
131  std::map<unsigned, std::set<CellPtr> > mLocationCellMap;
132 
134  std::map<Cell*, unsigned> mCellLocationMap;
135 
138 
140  std::list<CellPtr> mCells;
141 
143  c_vector<double, SPACE_DIM> mCentroid;
144 
146  out_stream mpVtkMetaFile;
147 
149  boost::shared_ptr<CellPropertyRegistry> mpCellPropertyRegistry;
150 
153 
155  std::vector<boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > > mCellWriters;
156 
158  std::vector<boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > > mCellPopulationWriters;
159 
161  std::vector<boost::shared_ptr<AbstractCellPopulationCountWriter<ELEMENT_DIM, SPACE_DIM> > > mCellPopulationCountWriters;
162 
169  virtual void Validate()=0;
170 
179  virtual void WriteVtkResultsToFile(const std::string& rDirectory)=0;
180 
187 
194  virtual void AcceptCellWritersAcrossPopulation();
195 
196 public:
197 
210  std::vector<CellPtr>& rCells,
211  const std::vector<unsigned> locationIndices=std::vector<unsigned>());
212 
216  virtual ~AbstractCellPopulation();
217 
221  void InitialiseCells();
222 
228  void SetDataOnAllCells(const std::string& rDataName, double dataValue);
229 
234 
238  std::list<CellPtr>& rGetCells();
239 
246  virtual unsigned GetNumNodes()=0;
247 
257  virtual c_vector<double, SPACE_DIM> GetLocationOfCellCentre(CellPtr pCell)=0;
258 
268  virtual Node<SPACE_DIM>* GetNode(unsigned index)=0;
269 
279  virtual void SetNode(unsigned nodeIndex, ChastePoint<SPACE_DIM>& rNewLocation)=0;
280 
291  virtual bool IsCellAssociatedWithADeletedLocation(CellPtr pCell)=0;
292 
308  virtual CellPtr AddCell(CellPtr pNewCell,
309  const c_vector<double,SPACE_DIM>& rCellDivisionVector,
310  CellPtr pParentCell=CellPtr())=0;
311 
312  class Iterator; // Forward declaration; see below
313 
322  virtual unsigned RemoveDeadCells()=0;
323 
330  virtual void Update(bool hasHadBirthsOrDeaths=true)=0;
331 
341  std::vector<unsigned> GetCellMutationStateCount();
342 
352  std::vector<unsigned> GetCellProliferativeTypeCount();
353 
364  std::vector<unsigned> GetCellCyclePhaseCount();
365 
372  unsigned GetNumRealCells();
373 
380  unsigned GetNumAllCells();
381 
387 
394  std::set<unsigned> GetCellAncestors();
395 
406  virtual CellPtr GetCellUsingLocationIndex(unsigned index);
407 
417  std::set<CellPtr> GetCellsUsingLocationIndex(unsigned index);
418 
426  bool IsCellAttachedToLocationIndex(unsigned index);
427 
438  void SetCellUsingLocationIndex(unsigned index, CellPtr pCell);
439 
446  virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell);
447 
454  virtual void RemoveCellUsingLocationIndex(unsigned index, CellPtr pCell);
455 
463  void MoveCellInLocationMap(CellPtr pCell, unsigned old_index, unsigned new_index);
464 
474  unsigned GetLocationIndexUsingCell(CellPtr pCell);
475 
479  boost::shared_ptr<CellPropertyRegistry> GetCellPropertyRegistry();
480 
486 
496  virtual double GetWidth(const unsigned& rDimension)=0;
497 
505  virtual double GetVolumeOfCell(CellPtr pCell)=0;
506 
516  virtual std::set<unsigned> GetNeighbouringNodeIndices(unsigned index)=0;
517 
527  virtual std::set<unsigned> GetNeighbouringLocationIndices(CellPtr pCell)=0;
528 
532  c_vector<double, SPACE_DIM> GetCentroidOfCellPopulation();
533 
537  virtual void UpdateCellProcessLocation();
538 
552  virtual void OpenWritersFiles(OutputFileHandler& rOutputFileHandler);
553 
560  void CloseWritersFiles();
561 
567  virtual void WriteResultsToFiles(const std::string& rDirectory);
568 
575  virtual void AcceptPopulationWriter(boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > pPopulationWriter)=0;
576 
583  virtual void AcceptPopulationCountWriter(boost::shared_ptr<AbstractCellPopulationCountWriter<ELEMENT_DIM, SPACE_DIM> > pPopulationCountWriter)=0;
584 
592  virtual void AcceptCellWriter(boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > pCellWriter, CellPtr pCell)=0;
593 
599  void OutputCellPopulationInfo(out_stream& rParamsFile);
600 
609  virtual void OutputCellPopulationParameters(out_stream& rParamsFile)=0;
610 
615 
622  template<template <unsigned, unsigned> class T>
624  {
625  mCellPopulationWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
626  }
627 
634  template<template <unsigned, unsigned> class T>
636  {
637  mCellWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
638  }
639 
646  template<template <unsigned, unsigned> class T>
648  {
649  mCellPopulationCountWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
650  }
651 
662  {
663  mCellPopulationWriters.push_back(pPopulationWriter);
664  }
665 
675  void AddCellWriter(boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > pCellWriter)
676  {
677  mCellWriters.push_back(pCellWriter);
678  }
679 
690  {
691  mCellPopulationCountWriters.push_back(pCellPopulationCountWriter);
692  }
693 
699  template<template <unsigned, unsigned> class T>
700  bool HasWriter() const
701  {
702  typedef AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> population_writer_t;
703  BOOST_FOREACH(boost::shared_ptr<population_writer_t> p_pop_writer, mCellPopulationWriters)
704  {
705  if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_pop_writer.get()))
706  {
707  return true;
708  }
709  }
710  typedef AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> cell_writer_t;
711  BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
712  {
713  if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_cell_writer.get()))
714  {
715  return true;
716  }
717  }
719  BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
720  {
721  if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_count_writer.get()))
722  {
723  return true;
724  }
725  }
726  return false;
727  }
728 
734  void SetOutputResultsForChasteVisualizer(bool outputResultsForChasteVisualizer);
735 
740  c_vector<double,SPACE_DIM> GetSizeOfCellPopulation();
741 
748  virtual bool IsRoomToDivide(CellPtr pCell);
749 
757  std::pair<unsigned,unsigned> CreateOrderedPair(unsigned index1, unsigned index2);
758 
765  class Iterator
766  {
767  public:
768 
773  inline CellPtr operator*();
774 
780  inline CellPtr operator->();
781 
788  inline bool operator!=(const typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& rOther);
789 
794  inline Iterator& operator++();
795 
802  Iterator(AbstractCellPopulation& rCellPopulation, std::list<CellPtr>::iterator cellIter);
803 
807  virtual ~Iterator()
808  {}
809 
810  private:
811 
819  virtual inline bool IsRealCell();
820 
825  inline bool IsAtEnd();
826 
829 
831  std::list<CellPtr>::iterator mCellIter;
832  };
833 
837  Iterator Begin();
838 
842  Iterator End();
843 };
844 
846 
847 // Iterator class implementation - most methods are inlined //
850 
851 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
852 CellPtr AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator*()
853 {
854  assert(!IsAtEnd());
855  return *mCellIter;
856 }
857 
858 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
860 {
861  assert(!IsAtEnd());
862  return *mCellIter;
863 }
864 
865 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
867 {
868  return mCellIter != rOther.mCellIter;
869 }
870 
871 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
873 {
874  do
875  {
876  ++mCellIter;
877  }
878  while (!IsAtEnd() && !IsRealCell());
879 
880  return (*this);
881 }
882 
883 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
885 {
886  return !( mrCellPopulation.IsCellAssociatedWithADeletedLocation(*mCellIter) || (*this)->IsDead() );
887 }
888 
889 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
891 {
892  return mCellIter == mrCellPopulation.rGetCells().end();
893 }
894 
895 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
897  : mrCellPopulation(rCellPopulation),
898  mCellIter(cellIter)
899 {
900  // The cell population can now return empty if it only has ghost nodes
901  if (mrCellPopulation.rGetCells().empty())
902  {
904  }
905  else
906  {
907  // Make sure we start at a real cell
908  if (mCellIter == mrCellPopulation.rGetCells().begin() && !IsRealCell())
909  {
910  ++(*this);
911  }
912  }
913 }
914 
915 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
917 {
918  return Iterator(*this, this->mCells.begin());
919 }
920 
921 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
923 {
924  return Iterator(*this, this->mCells.end());
925 }
926 
927 #endif /*ABSTRACTCELLPOPULATION_HPP_*/
virtual Node< SPACE_DIM > * GetNode(unsigned index)=0
bool operator!=(const typename AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM >::Iterator &rOther)
virtual void AcceptPopulationWriter(boost::shared_ptr< AbstractCellPopulationWriter< ELEMENT_DIM, SPACE_DIM > > pPopulationWriter)=0
c_vector< double, SPACE_DIM > GetCentroidOfCellPopulation()
std::vector< boost::shared_ptr< AbstractCellPopulationWriter< ELEMENT_DIM, SPACE_DIM > > > mCellPopulationWriters
virtual CellPtr GetCellUsingLocationIndex(unsigned index)
void SetDefaultCellMutationStateAndProliferativeTypeOrdering()
Definition: Node.hpp:58
unsigned GetLocationIndexUsingCell(CellPtr pCell)
#define TEMPLATED_CLASS_IS_ABSTRACT_1_UNSIGNED(T)
std::vector< boost::shared_ptr< AbstractCellWriter< ELEMENT_DIM, SPACE_DIM > > > mCellWriters
void AddPopulationWriter(boost::shared_ptr< AbstractCellPopulationWriter< ELEMENT_DIM, SPACE_DIM > > pPopulationWriter)
void SetDataOnAllCells(const std::string &rDataName, double dataValue)
boost::shared_ptr< CellPropertyRegistry > mpCellPropertyRegistry
virtual void RemoveCellUsingLocationIndex(unsigned index, CellPtr pCell)
c_vector< double, SPACE_DIM > mCentroid
virtual void OutputCellPopulationParameters(out_stream &rParamsFile)=0
std::vector< boost::shared_ptr< AbstractCellPopulationCountWriter< ELEMENT_DIM, SPACE_DIM > > > mCellPopulationCountWriters
virtual double GetWidth(const unsigned &rDimension)=0
virtual bool IsCellAssociatedWithADeletedLocation(CellPtr pCell)=0
std::set< CellPtr > GetCellsUsingLocationIndex(unsigned index)
virtual std::set< unsigned > GetNeighbouringLocationIndices(CellPtr pCell)=0
bool IsCellAttachedToLocationIndex(unsigned index)
virtual unsigned RemoveDeadCells()=0
virtual void WriteResultsToFiles(const std::string &rDirectory)
void SetOutputResultsForChasteVisualizer(bool outputResultsForChasteVisualizer)
void MoveCellInLocationMap(CellPtr pCell, unsigned old_index, unsigned new_index)
std::list< CellPtr > & rGetCells()
virtual void Validate()=0
std::vector< unsigned > GetCellCyclePhaseCount()
virtual void AcceptPopulationCountWriter(boost::shared_ptr< AbstractCellPopulationCountWriter< ELEMENT_DIM, SPACE_DIM > > pPopulationCountWriter)=0
c_vector< double, SPACE_DIM > GetSizeOfCellPopulation()
virtual void AcceptCellWritersAcrossPopulation()
std::pair< unsigned, unsigned > CreateOrderedPair(unsigned index1, unsigned index2)
Iterator(AbstractCellPopulation &rCellPopulation, std::list< CellPtr >::iterator cellIter)
std::map< unsigned, std::set< CellPtr > > mLocationCellMap
void SetCellUsingLocationIndex(unsigned index, CellPtr pCell)
std::list< CellPtr >::iterator mCellIter
std::map< Cell *, unsigned > mCellLocationMap
virtual std::set< unsigned > GetNeighbouringNodeIndices(unsigned index)=0
virtual void AcceptCellWriter(boost::shared_ptr< AbstractCellWriter< ELEMENT_DIM, SPACE_DIM > > pCellWriter, CellPtr pCell)=0
boost::shared_ptr< CellPropertyRegistry > GetCellPropertyRegistry()
virtual CellPtr AddCell(CellPtr pNewCell, const c_vector< double, SPACE_DIM > &rCellDivisionVector, CellPtr pParentCell=CellPtr())=0
void OpenRoundRobinWritersFilesForAppend(OutputFileHandler &rOutputFileHandler)
virtual void Update(bool hasHadBirthsOrDeaths=true)=0
virtual c_vector< double, SPACE_DIM > GetLocationOfCellCentre(CellPtr pCell)=0
virtual void OpenWritersFiles(OutputFileHandler &rOutputFileHandler)
void AddCellWriter(boost::shared_ptr< AbstractCellWriter< ELEMENT_DIM, SPACE_DIM > > pCellWriter)
void OutputCellPopulationInfo(out_stream &rParamsFile)
virtual bool IsRoomToDivide(CellPtr pCell)
AbstractMesh< ELEMENT_DIM, SPACE_DIM > & rGetMesh()
virtual double GetVolumeOfCell(CellPtr pCell)=0
void serialize(Archive &archive, const unsigned int version)
AbstractCellPopulation(AbstractMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
void AddCellPopulationCountWriter(boost::shared_ptr< AbstractCellPopulationCountWriter< ELEMENT_DIM, SPACE_DIM > > pCellPopulationCountWriter)
virtual void SetNode(unsigned nodeIndex, ChastePoint< SPACE_DIM > &rNewLocation)=0
virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell)
std::vector< unsigned > GetCellProliferativeTypeCount()
AbstractMesh< ELEMENT_DIM, SPACE_DIM > & mrMesh
virtual void WriteVtkResultsToFile(const std::string &rDirectory)=0
virtual unsigned GetNumNodes()=0
std::vector< unsigned > GetCellMutationStateCount()
std::set< unsigned > GetCellAncestors()