Chaste  Release::2018.1
AbstractCellPopulation.hpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 
58 #include "AbstractMesh.hpp"
59 #include "TetrahedralMesh.hpp"
60 #include "CellPropertyRegistry.hpp"
61 #include "Identifiable.hpp"
62 #include "AbstractCellPopulationCountWriter.hpp"
63 #include "AbstractCellPopulationWriter.hpp"
64 #include "AbstractCellWriter.hpp"
65 
66 // Forward declaration prevents circular include chain
67 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM> class AbstractCellBasedSimulation;
68 
74 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM=ELEMENT_DIM>
76 {
77 private:
78 
80  friend class boost::serialization::access;
81 
88  template<class Archive>
89  void serialize(Archive & archive, const unsigned int version)
90  {
91  archive & mCells;
92  archive & mLocationCellMap;
93  archive & mCellLocationMap;
94  archive & mpCellPropertyRegistry;
96  archive & mCellWriters;
97  archive & mCellPopulationWriters;
99  }
100 
109  void OpenRoundRobinWritersFilesForAppend(OutputFileHandler& rOutputFileHandler);
110 
119 
120 protected:
121 
123  std::map<unsigned, std::set<CellPtr> > mLocationCellMap;
124 
126  std::map<Cell*, unsigned> mCellLocationMap;
127 
130 
132  std::list<CellPtr> mCells;
133 
135  c_vector<double, SPACE_DIM> mCentroid;
136 
138  out_stream mpVtkMetaFile;
139 
141  boost::shared_ptr<CellPropertyRegistry> mpCellPropertyRegistry;
142 
145 
147  std::vector<boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > > mCellWriters;
148 
150  std::vector<boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > > mCellPopulationWriters;
151 
153  std::vector<boost::shared_ptr<AbstractCellPopulationCountWriter<ELEMENT_DIM, SPACE_DIM> > > mCellPopulationCountWriters;
154 
161  virtual void Validate()=0;
162 
171  virtual void WriteVtkResultsToFile(const std::string& rDirectory)=0;
172 
179 
186  virtual void AcceptCellWritersAcrossPopulation();
187 
188 public:
189 
202  std::vector<CellPtr>& rCells,
203  const std::vector<unsigned> locationIndices=std::vector<unsigned>());
204 
208  virtual ~AbstractCellPopulation();
209 
213  void InitialiseCells();
214 
220  void SetDataOnAllCells(const std::string& rDataName, double dataValue);
221 
226 
235 
246  virtual bool IsPdeNodeAssociatedWithNonApoptoticCell(unsigned pdeNodeIndex);
247 
264  virtual double GetCellDataItemAtPdeNode(unsigned pdeNodeIndex,
265  std::string& rVariableName,
266  bool dirichletBoundaryConditionApplies=false,
267  double dirichletBoundaryValue=0.0)=0;
268 
272  std::list<CellPtr>& rGetCells();
273 
280  virtual unsigned GetNumNodes()=0;
281 
291  virtual c_vector<double, SPACE_DIM> GetLocationOfCellCentre(CellPtr pCell)=0;
292 
302  virtual Node<SPACE_DIM>* GetNode(unsigned index)=0;
303 
313  virtual void SetNode(unsigned nodeIndex, ChastePoint<SPACE_DIM>& rNewLocation)=0;
314 
325  virtual bool IsCellAssociatedWithADeletedLocation(CellPtr pCell)=0;
326 
333  virtual void WriteDataToVisualizerSetupFile(out_stream& pVizSetupFile);
334 
346  virtual CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell=CellPtr())=0;
347 
358  virtual double GetDefaultTimeStep()=0;
359 
360  class Iterator; // Forward declaration; see below
361 
370  virtual unsigned RemoveDeadCells()=0;
371 
381  virtual void Update(bool hasHadBirthsOrDeaths=true)=0;
382 
392  std::vector<unsigned> GetCellMutationStateCount();
393 
403  std::vector<unsigned> GetCellProliferativeTypeCount();
404 
415  std::vector<unsigned> GetCellCyclePhaseCount();
416 
423  unsigned GetNumRealCells();
424 
431  unsigned GetNumAllCells();
432 
438 
445  std::set<unsigned> GetCellAncestors();
446 
457  virtual CellPtr GetCellUsingLocationIndex(unsigned index);
458 
468  std::set<CellPtr> GetCellsUsingLocationIndex(unsigned index);
469 
477  virtual bool IsCellAttachedToLocationIndex(unsigned index);
478 
489  void SetCellUsingLocationIndex(unsigned index, CellPtr pCell);
490 
497  virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell);
498 
505  virtual void RemoveCellUsingLocationIndex(unsigned index, CellPtr pCell);
506 
514  void MoveCellInLocationMap(CellPtr pCell, unsigned old_index, unsigned new_index);
515 
525  unsigned GetLocationIndexUsingCell(CellPtr pCell);
526 
530  boost::shared_ptr<CellPropertyRegistry> GetCellPropertyRegistry();
531 
537 
547  virtual double GetWidth(const unsigned& rDimension)=0;
548 
556  virtual double GetVolumeOfCell(CellPtr pCell)=0;
557 
567  virtual std::set<unsigned> GetNeighbouringNodeIndices(unsigned index)=0;
568 
578  virtual std::set<unsigned> GetNeighbouringLocationIndices(CellPtr pCell)=0;
579 
583  c_vector<double, SPACE_DIM> GetCentroidOfCellPopulation();
584 
588  virtual void UpdateCellProcessLocation();
589 
603  virtual void OpenWritersFiles(OutputFileHandler& rOutputFileHandler);
604 
611  void CloseWritersFiles();
612 
618  virtual void WriteResultsToFiles(const std::string& rDirectory);
619 
628  virtual void AcceptPopulationWriter(boost::shared_ptr<AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> > pPopulationWriter)=0;
629 
638  virtual void AcceptPopulationCountWriter(boost::shared_ptr<AbstractCellPopulationCountWriter<ELEMENT_DIM, SPACE_DIM> > pPopulationCountWriter)=0;
639 
649  virtual void AcceptCellWriter(boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > pCellWriter, CellPtr pCell)=0;
650 
656  void OutputCellPopulationInfo(out_stream& rParamsFile);
657 
666  virtual void OutputCellPopulationParameters(out_stream& rParamsFile)=0;
667 
683 
688 
695  template<template <unsigned, unsigned> class T>
697  {
698  mCellPopulationWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
699  }
700 
707  template<template <unsigned, unsigned> class T>
709  {
710  mCellWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
711  }
712 
719  template<template <unsigned, unsigned> class T>
721  {
722  mCellPopulationCountWriters.push_back(boost::shared_ptr< T<ELEMENT_DIM, SPACE_DIM> >(new T<ELEMENT_DIM, SPACE_DIM> ));
723  }
724 
735  {
736  mCellPopulationWriters.push_back(pPopulationWriter);
737  }
738 
748  void AddCellWriter(boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > pCellWriter)
749  {
750  mCellWriters.push_back(pCellWriter);
751  }
752 
763  {
764  mCellPopulationCountWriters.push_back(pCellPopulationCountWriter);
765  }
766 
772  template<template <unsigned, unsigned> class T>
773  bool HasWriter() const
774  {
775  typedef AbstractCellPopulationWriter<ELEMENT_DIM, SPACE_DIM> population_writer_t;
776  BOOST_FOREACH(boost::shared_ptr<population_writer_t> p_pop_writer, mCellPopulationWriters)
777  {
778  if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_pop_writer.get()))
779  {
780  return true;
781  }
782  }
783  typedef AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> cell_writer_t;
784  BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
785  {
786  if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_cell_writer.get()))
787  {
788  return true;
789  }
790  }
792  BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
793  {
794  if (dynamic_cast<T<ELEMENT_DIM, SPACE_DIM>* >(p_count_writer.get()))
795  {
796  return true;
797  }
798  }
799  return false;
800  }
801 
807  void SetOutputResultsForChasteVisualizer(bool outputResultsForChasteVisualizer);
808 
813  c_vector<double,SPACE_DIM> GetSizeOfCellPopulation();
814 
821  virtual bool IsRoomToDivide(CellPtr pCell);
822 
830  std::pair<unsigned,unsigned> CreateOrderedPair(unsigned index1, unsigned index2);
831 
838  class Iterator
839  {
840  public:
841 
846  inline CellPtr operator*();
847 
853  inline CellPtr operator->();
854 
861  inline bool operator!=(const typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator& rOther);
862 
867  inline Iterator& operator++();
868 
875  Iterator(AbstractCellPopulation& rCellPopulation, std::list<CellPtr>::iterator cellIter);
876 
880  virtual ~Iterator()
881  {}
882 
883  private:
884 
892  virtual inline bool IsRealCell();
893 
898  inline bool IsAtEnd();
899 
902 
904  std::list<CellPtr>::iterator mCellIter;
905  };
906 
910  Iterator Begin();
911 
915  Iterator End();
916 };
917 
919 
920 // Iterator class implementation - most methods are inlined //
923 
924 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
925 CellPtr AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator::operator*()
926 {
927  assert(!IsAtEnd());
928  return *mCellIter;
929 }
930 
931 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
933 {
934  assert(!IsAtEnd());
935  return *mCellIter;
936 }
937 
938 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
940 {
941  return mCellIter != rOther.mCellIter;
942 }
943 
944 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
946 {
947  do
948  {
949  ++mCellIter;
950  }
951  while (!IsAtEnd() && !IsRealCell());
952 
953  return (*this);
954 }
955 
956 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
958 {
959  return !( mrCellPopulation.IsCellAssociatedWithADeletedLocation(*mCellIter) || (*this)->IsDead() );
960 }
961 
962 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
964 {
965  return mCellIter == mrCellPopulation.rGetCells().end();
966 }
967 
968 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
970  : mrCellPopulation(rCellPopulation),
971  mCellIter(cellIter)
972 {
973  // The cell population can now return empty if it only has ghost nodes
974  if (mrCellPopulation.rGetCells().empty())
975  {
977  }
978  else
979  {
980  // Make sure we start at a real cell
981  if (mCellIter == mrCellPopulation.rGetCells().begin() && !IsRealCell())
982  {
983  ++(*this);
984  }
985  }
986 }
987 
988 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
990 {
991  return Iterator(*this, this->mCells.begin());
992 }
993 
994 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
996 {
997  return Iterator(*this, this->mCells.end());
998 }
999 
1000 #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
virtual void SimulationSetupHook(AbstractCellBasedSimulation< ELEMENT_DIM, SPACE_DIM > *pSimulation)
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
virtual 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 CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell=CellPtr())=0
virtual double GetCellDataItemAtPdeNode(unsigned pdeNodeIndex, std::string &rVariableName, bool dirichletBoundaryConditionApplies=false, double dirichletBoundaryValue=0.0)=0
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()
void OpenRoundRobinWritersFilesForAppend(OutputFileHandler &rOutputFileHandler)
virtual void Update(bool hasHadBirthsOrDeaths=true)=0
virtual bool IsPdeNodeAssociatedWithNonApoptoticCell(unsigned pdeNodeIndex)
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 void WriteDataToVisualizerSetupFile(out_stream &pVizSetupFile)
virtual double GetVolumeOfCell(CellPtr pCell)=0
virtual double GetDefaultTimeStep()=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 TetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * GetTetrahedralMeshForPdeModifier()=0
virtual void WriteVtkResultsToFile(const std::string &rDirectory)=0
virtual unsigned GetNumNodes()=0
std::vector< unsigned > GetCellMutationStateCount()
std::set< unsigned > GetCellAncestors()