36#include <boost/bind.hpp>
41#include "AbstractCellPopulation.hpp"
42#include "AbstractPhaseBasedCellCycleModel.hpp"
44#include "CellAncestor.hpp"
45#include "ApoptoticCellProperty.hpp"
48#include "BoundaryNodeWriter.hpp"
49#include "CellProliferativeTypesWriter.hpp"
50#include "LegacyCellProliferativeTypesWriter.hpp"
51#include "CellRemovalLocationsWriter.hpp"
54#include "CellMutationStatesCountWriter.hpp"
55#include "CellProliferativePhasesCountWriter.hpp"
56#include "CellProliferativeTypesCountWriter.hpp"
57#include "NodeLocationWriter.hpp"
60#include "WildTypeCellMutationState.hpp"
61#include "ApcOneHitCellMutationState.hpp"
62#include "ApcTwoHitCellMutationState.hpp"
63#include "BetaCateninOneHitCellMutationState.hpp"
64#include "DefaultCellProliferativeType.hpp"
65#include "StemCellProliferativeType.hpp"
66#include "TransitCellProliferativeType.hpp"
67#include "DifferentiatedCellProliferativeType.hpp"
69template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
71 std::vector<CellPtr>& rCells,
72 const std::vector<unsigned> locationIndices)
74 mCells(rCells.begin(), rCells.end()),
75 mCentroid(zero_vector<
double>(SPACE_DIM)),
77 mOutputResultsForChasteVisualizer(true)
84 std::vector<CellPtr>().swap(rCells);
87 if (!locationIndices.empty())
89 if (
mCells.size() != locationIndices.size())
91 EXCEPTION(
"There is not a one-one correspondence between cells and location indices");
99 std::list<CellPtr>::iterator it =
mCells.begin();
100 for (
unsigned i=0; it !=
mCells.end(); ++it, ++i)
111template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
117template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
122template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
126 cell_iter!=this->End();
129 cell_iter->InitialiseCellCycleModel();
130 cell_iter->InitialiseSrnModel();
134template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
138 cell_iter!=this->End();
141 cell_iter->GetCellData()->SetItem(rDataName, dataValue);
145template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
151template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
157template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
160 unsigned counter = 0;
162 cell_iter!=this->End();
170template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
173 return mCells.size();
176template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
182 cell_iter->SetAncestor(p_cell_ancestor);
186template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
189 std::set<unsigned> remaining_ancestors;
192 remaining_ancestors.insert(cell_iter->GetAncestor());
194 return remaining_ancestors;
197template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
200 std::vector<unsigned> mutation_state_count;
201 const std::vector<boost::shared_ptr<AbstractCellProperty> >& r_cell_properties
202 = mpCellPropertyRegistry->rGetAllCellProperties();
205 for (
unsigned i=0; i<r_cell_properties.size(); i++)
207 if (r_cell_properties[i]->IsSubType<AbstractCellMutationState>())
209 mutation_state_count.push_back(r_cell_properties[i]->GetCellCount());
217 unsigned local_size = mutation_state_count.size();
218 unsigned global_size;
220 assert(local_size == global_size);
222 std::vector<unsigned> mutation_counts(global_size);
223 MPI_Allreduce(&mutation_state_count[0], &mutation_counts[0], mutation_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
225 mutation_state_count = mutation_counts;
228 return mutation_state_count;
231template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
234 std::vector<unsigned> proliferative_type_count;
235 const std::vector<boost::shared_ptr<AbstractCellProperty> >& r_cell_properties
236 = mpCellPropertyRegistry->rGetAllCellProperties();
239 for (
unsigned i=0; i<r_cell_properties.size(); i++)
241 if (r_cell_properties[i]->IsSubType<AbstractCellProliferativeType>())
243 proliferative_type_count.push_back(r_cell_properties[i]->GetCellCount());
251 unsigned local_size = proliferative_type_count.size();
252 unsigned global_size;
255 assert(local_size == global_size);
257 std::vector<unsigned> total_types_counts(global_size);
258 MPI_Allreduce(&proliferative_type_count[0], &total_types_counts[0], total_types_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
260 proliferative_type_count = total_types_counts;
263 return proliferative_type_count;
266template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
269 std::vector<unsigned> cell_cycle_phase_count(5);
270 for (
unsigned i=0; i<5; i++)
272 cell_cycle_phase_count[i] = 0;
279 if (GetNumAllCells() > 0u)
283 EXCEPTION(
"You are trying to record the cell cycle phase of cells with a non phase based cell cycle model.");
287 cell_iter != this->End();
293 cell_cycle_phase_count[0]++;
296 cell_cycle_phase_count[1]++;
299 cell_cycle_phase_count[2]++;
302 cell_cycle_phase_count[3]++;
305 cell_cycle_phase_count[4]++;
316 std::vector<unsigned> phase_counts(cell_cycle_phase_count.size(), 0u);
317 MPI_Allreduce(&cell_cycle_phase_count[0], &phase_counts[0], phase_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
319 cell_cycle_phase_count = phase_counts;
322 return cell_cycle_phase_count;
325template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
329 std::set<CellPtr> cells = mLocationCellMap[index];
332 if (cells.size() == 1)
334 return *(cells.begin());
338 EXCEPTION(
"Location index input argument does not correspond to a Cell");
342 EXCEPTION(
"Multiple cells are attached to a single location index.");
346template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
350 return mLocationCellMap[index];
353template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
357 std::set<CellPtr> cells = mLocationCellMap[index];
360 return !(cells.empty());
363template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
368template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
372 mLocationCellMap[index].clear();
373 mCellLocationMap.erase(pCell.get());
376 mLocationCellMap[index].insert(pCell);
379 mCellLocationMap[pCell.get()] = index;
382template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
385 mLocationCellMap[index].insert(pCell);
386 mCellLocationMap[pCell.get()] = index;
389template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
392 std::set<CellPtr>::iterator cell_iter = mLocationCellMap[index].find(pCell);
394 if (cell_iter == mLocationCellMap[index].end())
396 EXCEPTION(
"Tried to remove a cell which is not attached to the given location index");
400 mLocationCellMap[index].erase(cell_iter);
401 mCellLocationMap.erase(pCell.get());
405template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
409 RemoveCellUsingLocationIndex(old_index, pCell);
412 AddCellUsingLocationIndex(new_index, pCell);
415template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
419 assert(this->mCellLocationMap.find(pCell.get()) != this->mCellLocationMap.end());
421 return mCellLocationMap[pCell.get()];
424template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
427 return mpCellPropertyRegistry;
430template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
433 boost::shared_ptr<CellPropertyRegistry> p_registry = GetCellPropertyRegistry();
434 if (!p_registry->HasOrderingBeenSpecified())
436 std::vector<boost::shared_ptr<AbstractCellProperty> > mutations_and_proliferative_types;
447 p_registry->SpecifyOrdering(mutations_and_proliferative_types);
455template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
458 return std::set<std::pair<unsigned, unsigned>>();
462template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
465 mCentroid = zero_vector<double>(SPACE_DIM);
467 cell_iter != this->End();
470 mCentroid += GetLocationOfCellCentre(*cell_iter);
472 mCentroid /= this->GetNumRealCells();
477template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
482template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
486 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
492 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
498template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
502 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
508 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
514 *mpVtkMetaFile <<
" </Collection>\n";
515 *mpVtkMetaFile <<
"</VTKFile>\n";
516 mpVtkMetaFile->close();
520template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
525 *mpVtkMetaFile <<
"<?xml version=\"1.0\"?>\n";
526 *mpVtkMetaFile <<
"<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
527 *mpVtkMetaFile <<
" <Collection>\n";
530 if (mOutputResultsForChasteVisualizer)
532 if (!HasWriter<NodeLocationWriter>())
534 AddPopulationWriter<NodeLocationWriter>();
536 if (!HasWriter<BoundaryNodeWriter>())
538 AddPopulationWriter<BoundaryNodeWriter>();
540 if (!HasWriter<CellProliferativeTypesWriter>())
542 AddCellWriter<CellProliferativeTypesWriter>();
544 if (!HasWriter<LegacyCellProliferativeTypesWriter>())
546 AddCellWriter<LegacyCellProliferativeTypesWriter>();
552 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
559 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
562 p_pop_writer->WriteHeader(
this);
567 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
570 p_count_writer->WriteHeader(
this);
575 BOOST_FOREACH(boost::shared_ptr<event_writer_t> p_event_writer, mCellPopulationEventWriters)
578 p_event_writer->WriteHeader(
this);
582template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
587 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
591 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
593 p_pop_writer->OpenOutputFileForAppend(rOutputFileHandler);
597template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
604 if (!(mCellWriters.empty() && mCellPopulationWriters.empty() && mCellPopulationCountWriters.empty()))
607 SetDefaultCellMutationStateAndProliferativeTypeOrdering();
611 OpenRoundRobinWritersFilesForAppend(output_file_handler);
616 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
618 p_cell_writer->WriteTimeStamp();
620 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
622 p_pop_writer->WriteTimeStamp();
627 pop_writer_iter != mCellPopulationWriters.end();
630 AcceptPopulationWriter(*pop_writer_iter);
633 AcceptCellWritersAcrossPopulation();
638 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
640 p_cell_writer->WriteNewline();
642 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
644 p_pop_writer->WriteNewline();
647 CloseRoundRobinWritersFiles();
657 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
660 p_count_writer->WriteTimeStamp();
664 count_writer_iter != mCellPopulationCountWriters.end();
667 AcceptPopulationCountWriter(*count_writer_iter);
673 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
675 p_count_writer->WriteNewline();
676 p_count_writer->CloseFile();
687 BOOST_FOREACH(boost::shared_ptr<event_writer_t> p_event_writer, mCellPopulationEventWriters)
693 event_writer_iter != mCellPopulationEventWriters.end();
696 AcceptPopulationEventWriter(*event_writer_iter);
702 BOOST_FOREACH(boost::shared_ptr<event_writer_t> p_event_writer, mCellPopulationEventWriters)
704 p_event_writer->CloseFile();
712 WriteVtkResultsToFile(rDirectory);
716template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
720 cell_iter != this->End();
724 cell_writer_iter != mCellWriters.end();
727 AcceptCellWriter(*cell_writer_iter, *cell_iter);
732template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
735 std::string cell_population_type = GetIdentifier();
737 *rParamsFile <<
"\t<" << cell_population_type <<
">\n";
738 OutputCellPopulationParameters(rParamsFile);
739 *rParamsFile <<
"\t</" << cell_population_type <<
">\n";
740 *rParamsFile <<
"\n";
741 *rParamsFile <<
"\t<CellCycleModels>\n";
749 std::set<std::string> unique_cell_cycle_models;
750 std::vector<CellPtr> first_cell_with_unique_CCM;
752 cell_iter != this->End();
755 std::string identifier = cell_iter->GetCellCycleModel()->GetIdentifier();
756 if (unique_cell_cycle_models.count(identifier) == 0)
758 unique_cell_cycle_models.insert(identifier);
759 first_cell_with_unique_CCM.push_back((*cell_iter));
764 for (
unsigned i=0; i<first_cell_with_unique_CCM.size(); i++)
767 first_cell_with_unique_CCM[i]->GetCellCycleModel()->OutputCellCycleModelInfo(rParamsFile);
769 *rParamsFile <<
"\t</CellCycleModels>\n";
771 *rParamsFile <<
"\n";
772 *rParamsFile <<
"\t<SrnModels>\n";
780 std::set<std::string> unique_srn_models;
781 std::vector<CellPtr> first_cell_with_unique_SRN;
783 cell_iter != this->End();
786 std::string identifier = cell_iter->GetSrnModel()->GetIdentifier();
787 if (unique_srn_models.count(identifier) == 0)
789 unique_srn_models.insert(identifier);
790 first_cell_with_unique_SRN.push_back((*cell_iter));
795 for (
unsigned i=0; i<first_cell_with_unique_SRN.size(); i++)
798 first_cell_with_unique_SRN[i]->GetSrnModel()->OutputSrnModelInfo(rParamsFile);
801 *rParamsFile <<
"\t</SrnModels>\n";
804template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
807 *rParamsFile <<
"\t\t<OutputResultsForChasteVisualizer>" << mOutputResultsForChasteVisualizer <<
"</OutputResultsForChasteVisualizer>\n";
810template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
815template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
818 return mOutputResultsForChasteVisualizer;
821template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
824 mOutputResultsForChasteVisualizer = outputResultsForChasteVisualizer;
827template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
830 return mDivisionsInformation;
832template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
835 mDivisionsInformation.push_back(divisionInformation);
838template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
841 mDivisionsInformation.clear();
844template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
847 return mRemovalsInformation;
850template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
853 mRemovalsInformation.push_back(removalInformation);
856template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
859 mRemovalsInformation.clear();
862template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
866 c_vector<double, SPACE_DIM> cell_location = GetLocationOfCellCentre(pCell);
867 if (HasWriter<CellRemovalLocationsWriter>())
869 std::stringstream removal_info;
871 for (
unsigned i = 0; i < SPACE_DIM; i++)
873 removal_info << cell_location[i] <<
"\t";
875 removal_info <<
"\t" << pCell->GetAge() <<
"\t" << pCell->GetCellId() <<
"\t" << killerInfo <<
"\t";
877 AddRemovalInformation(removal_info.str());
881template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
885 GenerateRemovalInformation(pCell, killerInfo);
891template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
895 GenerateRemovalInformation(pCell, killerInfo);
898 pCell->StartApoptosis();
901template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
907template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
911 c_vector<double,SPACE_DIM> centre = GetCentroidOfCellPopulation();
914 c_vector<double,SPACE_DIM> max_distance_from_centre = zero_vector<double>(SPACE_DIM);
916 cell_iter != this->End();
919 c_vector<double,SPACE_DIM> cell_location = GetLocationOfCellCentre(*cell_iter);
922 c_vector<double,SPACE_DIM> displacement;
923 displacement = centre - cell_location;
925 for (
unsigned i=0; i<SPACE_DIM; i++)
927 if (displacement[i] > max_distance_from_centre[i])
929 max_distance_from_centre[i] = displacement[i];
934 return max_distance_from_centre;
937template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
940 assert(index1 != index2);
942 std::pair<unsigned, unsigned> ordered_pair;
945 ordered_pair.first = index1;
946 ordered_pair.second = index2;
950 ordered_pair.first = index2;
951 ordered_pair.second = index1;
956template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
959 bool non_apoptotic_cell_present =
false;
961 if (IsCellAttachedToLocationIndex(pdeNodeIndex))
963 non_apoptotic_cell_present = !(GetCellUsingLocationIndex(pdeNodeIndex)->template HasCellProperty<ApoptoticCellProperty>());
966 return non_apoptotic_cell_present;
#define EXCEPTION(message)
#define MAKE_PTR_ARGS(TYPE, NAME, ARGS)
virtual void OpenOutputFile(OutputFileHandler &rOutputFileHandler)
void OpenOutputFileForAppend(OutputFileHandler &rOutputFileHandler)
boost::shared_ptr< CellPropertyRegistry > mpCellPropertyRegistry
void ClearDivisionsInformation()
std::vector< std::string > GetDivisionsInformation()
void OpenRoundRobinWritersFilesForAppend(OutputFileHandler &rOutputFileHandler)
void SetCellUsingLocationIndex(unsigned index, CellPtr pCell)
std::vector< std::string > GetRemovalsInformation()
virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell)
std::vector< unsigned > GetCellMutationStateCount()
std::list< CellPtr > & rGetCells()
void SetDataOnAllCells(const std::string &rDataName, double dataValue)
virtual void OpenWritersFiles(OutputFileHandler &rOutputFileHandler)
virtual bool IsCellAttachedToLocationIndex(unsigned index)
virtual bool IsRoomToDivide(CellPtr pCell)
std::list< CellPtr > mCells
virtual bool IsPdeNodeAssociatedWithNonApoptoticCell(unsigned pdeNodeIndex)
std::map< unsigned, std::set< CellPtr > > mLocationCellMap
unsigned GetLocationIndexUsingCell(CellPtr pCell)
void SetOutputResultsForChasteVisualizer(bool outputResultsForChasteVisualizer)
virtual void WriteResultsToFiles(const std::string &rDirectory)
std::set< CellPtr > GetCellsUsingLocationIndex(unsigned index)
void ClearRemovalsInformation()
std::set< unsigned > GetCellAncestors()
virtual void OutputCellPopulationParameters(out_stream &rParamsFile)=0
virtual std::set< std::pair< unsigned, unsigned > > GetNeighbouringEdgeIndices(CellPtr pCell, unsigned pEdgeIndex)
virtual void SimulationSetupHook(AbstractCellBasedSimulation< ELEMENT_DIM, SPACE_DIM > *pSimulation)
boost::shared_ptr< CellPropertyRegistry > GetCellPropertyRegistry()
virtual void WriteDataToVisualizerSetupFile(out_stream &pVizSetupFile)
virtual void CloseWritersFiles()
void GenerateRemovalInformation(CellPtr pCell, std::string killerInfo)
unsigned GetNumAllCells()
unsigned GetNumRealCells()
void CloseRoundRobinWritersFiles()
std::map< Cell *, unsigned > mCellLocationMap
void SetCellAncestorsToLocationIndices()
bool GetOutputResultsForChasteVisualizer()
void OutputCellPopulationInfo(out_stream &rParamsFile)
virtual CellPtr GetCellUsingLocationIndex(unsigned index)
std::vector< unsigned > GetCellCyclePhaseCount()
AbstractCellPopulation(AbstractMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
AbstractMesh< ELEMENT_DIM, SPACE_DIM > & rGetMesh()
void StartApoptosisOnCell(CellPtr pCell, std::string killerInfo)
c_vector< double, SPACE_DIM > GetCentroidOfCellPopulation()
virtual ~AbstractCellPopulation()
void KillCell(CellPtr pCell, std::string killerInfo)
virtual void UpdateCellProcessLocation()
std::vector< unsigned > GetCellProliferativeTypeCount()
void AddRemovalInformation(std::string removalInformation)
void SetDefaultCellMutationStateAndProliferativeTypeOrdering()
std::pair< unsigned, unsigned > CreateOrderedPair(unsigned index1, unsigned index2)
void MoveCellInLocationMap(CellPtr pCell, unsigned old_index, unsigned new_index)
c_vector< double, SPACE_DIM > GetSizeOfCellPopulation()
void AddDivisionInformation(std::string divisionInformation)
virtual void AcceptCellWritersAcrossPopulation()
virtual void RemoveCellUsingLocationIndex(unsigned index, CellPtr pCell)
CellCyclePhase GetCurrentCellCyclePhase() const
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
static SimulationTime * Instance()