39#include "AbstractCellPopulation.hpp"
40#include "AbstractPhaseBasedCellCycleModel.hpp"
42#include "CellAncestor.hpp"
43#include "ApoptoticCellProperty.hpp"
46#include "BoundaryNodeWriter.hpp"
47#include "CellProliferativeTypesWriter.hpp"
48#include "LegacyCellProliferativeTypesWriter.hpp"
49#include "CellRemovalLocationsWriter.hpp"
52#include "NodeLocationWriter.hpp"
55#include "WildTypeCellMutationState.hpp"
56#include "ApcOneHitCellMutationState.hpp"
57#include "ApcTwoHitCellMutationState.hpp"
58#include "BetaCateninOneHitCellMutationState.hpp"
59#include "DefaultCellProliferativeType.hpp"
60#include "StemCellProliferativeType.hpp"
61#include "TransitCellProliferativeType.hpp"
62#include "DifferentiatedCellProliferativeType.hpp"
64template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
66 std::vector<CellPtr>& rCells,
67 const std::vector<unsigned> locationIndices)
69 mCells(rCells.begin(), rCells.end()),
70 mCentroid(zero_vector<
double>(SPACE_DIM)),
72 mOutputResultsForChasteVisualizer(true)
79 std::vector<CellPtr>().swap(rCells);
82 if (!locationIndices.empty())
84 if (
mCells.size() != locationIndices.size())
86 EXCEPTION(
"There is not a one-one correspondence between cells and location indices");
94 std::list<CellPtr>::iterator it =
mCells.begin();
95 for (
unsigned i=0; it !=
mCells.end(); ++it, ++i)
106template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
112template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
117template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
121 cell_iter!=this->End();
124 cell_iter->InitialiseCellCycleModel();
125 cell_iter->InitialiseSrnModel();
129template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
133 cell_iter!=this->End();
136 cell_iter->GetCellData()->SetItem(rDataName, dataValue);
140template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
146template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
152template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
155 unsigned counter = 0;
157 cell_iter!=this->End();
165template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
168 return mCells.size();
171template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
177 cell_iter->SetAncestor(p_cell_ancestor);
181template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
184 std::set<unsigned> remaining_ancestors;
187 remaining_ancestors.insert(cell_iter->GetAncestor());
189 return remaining_ancestors;
192template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
195 std::vector<unsigned> mutation_state_count;
196 const auto& r_cell_properties = mpCellPropertyRegistry->rGetAllCellProperties();
199 for (
unsigned i=0; i<r_cell_properties.size(); i++)
201 if (r_cell_properties[i]->IsSubType<AbstractCellMutationState>())
203 mutation_state_count.push_back(r_cell_properties[i]->GetCellCount());
211 unsigned local_size = mutation_state_count.size();
212 unsigned global_size;
214 assert(local_size == global_size);
216 std::vector<unsigned> mutation_counts(global_size);
217 MPI_Allreduce(mutation_state_count.data(), mutation_counts.data(), mutation_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
219 mutation_state_count = mutation_counts;
222 return mutation_state_count;
225template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
228 std::vector<unsigned> proliferative_type_count;
229 const auto& r_cell_properties = mpCellPropertyRegistry->rGetAllCellProperties();
232 for (
unsigned i=0; i<r_cell_properties.size(); i++)
234 if (r_cell_properties[i]->IsSubType<AbstractCellProliferativeType>())
236 proliferative_type_count.push_back(r_cell_properties[i]->GetCellCount());
244 unsigned local_size = proliferative_type_count.size();
245 unsigned global_size;
248 assert(local_size == global_size);
250 std::vector<unsigned> total_types_counts(global_size);
251 MPI_Allreduce(proliferative_type_count.data(), total_types_counts.data(), total_types_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
253 proliferative_type_count = total_types_counts;
256 return proliferative_type_count;
259template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
262 std::vector<unsigned> cell_cycle_phase_count(5);
263 for (
unsigned i=0; i<5; i++)
265 cell_cycle_phase_count[i] = 0;
272 if (GetNumAllCells() > 0u)
276 EXCEPTION(
"You are trying to record the cell cycle phase of cells with a non phase based cell cycle model.");
280 cell_iter != this->End();
286 cell_cycle_phase_count[0]++;
289 cell_cycle_phase_count[1]++;
292 cell_cycle_phase_count[2]++;
295 cell_cycle_phase_count[3]++;
298 cell_cycle_phase_count[4]++;
309 std::vector<unsigned> phase_counts(cell_cycle_phase_count.size(), 0u);
310 MPI_Allreduce(cell_cycle_phase_count.data(), phase_counts.data(), phase_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
312 cell_cycle_phase_count = phase_counts;
315 return cell_cycle_phase_count;
318template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
322 std::set<CellPtr> cells = mLocationCellMap[index];
325 if (cells.size() == 1)
327 return *(cells.begin());
331 EXCEPTION(
"Location index input argument does not correspond to a Cell");
335 EXCEPTION(
"Multiple cells are attached to a single location index.");
339template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
343 return mLocationCellMap[index];
346template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
350 std::set<CellPtr> cells = mLocationCellMap[index];
353 return !(cells.empty());
356template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
361template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
365 mLocationCellMap[index].clear();
366 mCellLocationMap.erase(pCell.get());
369 mLocationCellMap[index].insert(pCell);
372 mCellLocationMap[pCell.get()] = index;
375template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
378 mLocationCellMap[index].insert(pCell);
379 mCellLocationMap[pCell.get()] = index;
382template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
385 std::set<CellPtr>::iterator cell_iter = mLocationCellMap[index].find(pCell);
387 if (cell_iter == mLocationCellMap[index].end())
389 EXCEPTION(
"Tried to remove a cell which is not attached to the given location index");
393 mLocationCellMap[index].erase(cell_iter);
394 mCellLocationMap.erase(pCell.get());
398template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
402 RemoveCellUsingLocationIndex(old_index, pCell);
405 AddCellUsingLocationIndex(new_index, pCell);
408template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
412 assert(this->mCellLocationMap.find(pCell.get()) != this->mCellLocationMap.end());
414 return mCellLocationMap[pCell.get()];
417template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
420 return mpCellPropertyRegistry;
423template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
426 boost::shared_ptr<CellPropertyRegistry> p_registry = GetCellPropertyRegistry();
427 if (!p_registry->HasOrderingBeenSpecified())
429 std::vector<boost::shared_ptr<AbstractCellProperty> > mutations_and_proliferative_types;
440 p_registry->SpecifyOrdering(mutations_and_proliferative_types);
448template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
451 return std::set<std::pair<unsigned, unsigned>>();
455template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
458 mCentroid = zero_vector<double>(SPACE_DIM);
460 cell_iter != this->End();
463 mCentroid += GetLocationOfCellCentre(*cell_iter);
465 mCentroid /= this->GetNumRealCells();
470template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
475template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
479 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
485 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
491template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
495 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
501 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
507 *mpVtkMetaFile <<
" </Collection>\n";
508 *mpVtkMetaFile <<
"</VTKFile>\n";
509 mpVtkMetaFile->close();
513template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
518 *mpVtkMetaFile <<
"<?xml version=\"1.0\"?>\n";
519 *mpVtkMetaFile <<
"<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
520 *mpVtkMetaFile <<
" <Collection>\n";
523 if (mOutputResultsForChasteVisualizer)
525 if (!HasWriter<NodeLocationWriter>())
527 AddPopulationWriter<NodeLocationWriter>();
529 if (!HasWriter<BoundaryNodeWriter>())
531 AddPopulationWriter<BoundaryNodeWriter>();
533 if (!HasWriter<CellProliferativeTypesWriter>())
535 AddCellWriter<CellProliferativeTypesWriter>();
537 if (!HasWriter<LegacyCellProliferativeTypesWriter>())
539 AddCellWriter<LegacyCellProliferativeTypesWriter>();
545 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
552 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
555 p_pop_writer->WriteHeader(
this);
560 for (boost::shared_ptr<count_writer_t> p_count_writer : mCellPopulationCountWriters)
563 p_count_writer->WriteHeader(
this);
568 for (boost::shared_ptr<event_writer_t> p_event_writer : mCellPopulationEventWriters)
571 p_event_writer->WriteHeader(
this);
575template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
580 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
584 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
586 p_pop_writer->OpenOutputFileForAppend(rOutputFileHandler);
590template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
597 if (!(mCellWriters.empty() && mCellPopulationWriters.empty() && mCellPopulationCountWriters.empty()))
600 SetDefaultCellMutationStateAndProliferativeTypeOrdering();
604 OpenRoundRobinWritersFilesForAppend(output_file_handler);
609 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
611 p_cell_writer->WriteTimeStamp();
613 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
615 p_pop_writer->WriteTimeStamp();
620 pop_writer_iter != mCellPopulationWriters.end();
623 AcceptPopulationWriter(*pop_writer_iter);
626 AcceptCellWritersAcrossPopulation();
631 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
633 p_cell_writer->WriteNewline();
635 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
637 p_pop_writer->WriteNewline();
640 CloseRoundRobinWritersFiles();
650 for (boost::shared_ptr<count_writer_t> p_count_writer : mCellPopulationCountWriters)
653 p_count_writer->WriteTimeStamp();
657 count_writer_iter != mCellPopulationCountWriters.end();
660 AcceptPopulationCountWriter(*count_writer_iter);
666 for (boost::shared_ptr<count_writer_t> p_count_writer : mCellPopulationCountWriters)
668 p_count_writer->WriteNewline();
669 p_count_writer->CloseFile();
680 for (boost::shared_ptr<event_writer_t> p_event_writer : mCellPopulationEventWriters)
686 event_writer_iter != mCellPopulationEventWriters.end();
689 AcceptPopulationEventWriter(*event_writer_iter);
695 for (boost::shared_ptr<event_writer_t> p_event_writer : mCellPopulationEventWriters)
697 p_event_writer->CloseFile();
705 WriteVtkResultsToFile(rDirectory);
709template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
713 cell_iter != this->End();
717 cell_writer_iter != mCellWriters.end();
720 AcceptCellWriter(*cell_writer_iter, *cell_iter);
725template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
728 std::string cell_population_type = GetIdentifier();
730 *rParamsFile <<
"\t<" << cell_population_type <<
">\n";
731 OutputCellPopulationParameters(rParamsFile);
732 *rParamsFile <<
"\t</" << cell_population_type <<
">\n";
733 *rParamsFile <<
"\n";
734 *rParamsFile <<
"\t<CellCycleModels>\n";
742 std::set<std::string> unique_cell_cycle_models;
743 std::vector<CellPtr> first_cell_with_unique_CCM;
745 cell_iter != this->End();
748 std::string identifier = cell_iter->GetCellCycleModel()->GetIdentifier();
749 if (unique_cell_cycle_models.count(identifier) == 0)
751 unique_cell_cycle_models.insert(identifier);
752 first_cell_with_unique_CCM.push_back((*cell_iter));
757 for (
unsigned i=0; i<first_cell_with_unique_CCM.size(); i++)
760 first_cell_with_unique_CCM[i]->GetCellCycleModel()->OutputCellCycleModelInfo(rParamsFile);
762 *rParamsFile <<
"\t</CellCycleModels>\n";
764 *rParamsFile <<
"\n";
765 *rParamsFile <<
"\t<SrnModels>\n";
773 std::set<std::string> unique_srn_models;
774 std::vector<CellPtr> first_cell_with_unique_SRN;
776 cell_iter != this->End();
779 std::string identifier = cell_iter->GetSrnModel()->GetIdentifier();
780 if (unique_srn_models.count(identifier) == 0)
782 unique_srn_models.insert(identifier);
783 first_cell_with_unique_SRN.push_back((*cell_iter));
788 for (
unsigned i=0; i<first_cell_with_unique_SRN.size(); i++)
791 first_cell_with_unique_SRN[i]->GetSrnModel()->OutputSrnModelInfo(rParamsFile);
794 *rParamsFile <<
"\t</SrnModels>\n";
797template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
800 *rParamsFile <<
"\t\t<OutputResultsForChasteVisualizer>" << mOutputResultsForChasteVisualizer <<
"</OutputResultsForChasteVisualizer>\n";
803template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
808template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
811 return mOutputResultsForChasteVisualizer;
814template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
817 mOutputResultsForChasteVisualizer = outputResultsForChasteVisualizer;
820template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
823 return mDivisionsInformation;
825template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
828 mDivisionsInformation.push_back(divisionInformation);
831template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
834 mDivisionsInformation.clear();
837template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
840 return mRemovalsInformation;
843template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
846 mRemovalsInformation.push_back(removalInformation);
849template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
852 mRemovalsInformation.clear();
855template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
859 c_vector<double, SPACE_DIM> cell_location = GetLocationOfCellCentre(pCell);
860 if (HasWriter<CellRemovalLocationsWriter>())
862 std::stringstream removal_info;
864 for (
unsigned i = 0; i < SPACE_DIM; i++)
866 removal_info << cell_location[i] <<
"\t";
868 removal_info <<
"\t" << pCell->GetAge() <<
"\t" << pCell->GetCellId() <<
"\t" << killerInfo <<
"\t";
870 AddRemovalInformation(removal_info.str());
874template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
878 GenerateRemovalInformation(pCell, killerInfo);
884template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
888 GenerateRemovalInformation(pCell, killerInfo);
891 pCell->StartApoptosis();
894template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
900template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
904 c_vector<double,SPACE_DIM> centre = GetCentroidOfCellPopulation();
907 c_vector<double,SPACE_DIM> max_distance_from_centre = zero_vector<double>(SPACE_DIM);
909 cell_iter != this->End();
912 c_vector<double,SPACE_DIM> cell_location = GetLocationOfCellCentre(*cell_iter);
915 c_vector<double,SPACE_DIM> displacement;
916 displacement = centre - cell_location;
918 for (
unsigned i=0; i<SPACE_DIM; i++)
920 if (displacement[i] > max_distance_from_centre[i])
922 max_distance_from_centre[i] = displacement[i];
927 return max_distance_from_centre;
930template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
933 assert(index1 != index2);
935 std::pair<unsigned, unsigned> ordered_pair;
938 ordered_pair.first = index1;
939 ordered_pair.second = index2;
943 ordered_pair.first = index2;
944 ordered_pair.second = index1;
949template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
952 bool non_apoptotic_cell_present =
false;
954 if (IsCellAttachedToLocationIndex(pdeNodeIndex))
956 non_apoptotic_cell_present = !(GetCellUsingLocationIndex(pdeNodeIndex)->template HasCellProperty<ApoptoticCellProperty>());
959 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()