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)
506 *mpVtkMetaFile <<
" </Collection>\n";
507 *mpVtkMetaFile <<
"</VTKFile>\n";
508 mpVtkMetaFile->close();
511template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
515 *mpVtkMetaFile <<
"<?xml version=\"1.0\"?>\n";
516 *mpVtkMetaFile <<
"<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
517 *mpVtkMetaFile <<
" <Collection>\n";
519 if (mOutputResultsForChasteVisualizer)
521 if (!HasWriter<NodeLocationWriter>())
523 AddPopulationWriter<NodeLocationWriter>();
525 if (!HasWriter<BoundaryNodeWriter>())
527 AddPopulationWriter<BoundaryNodeWriter>();
529 if (!HasWriter<CellProliferativeTypesWriter>())
531 AddCellWriter<CellProliferativeTypesWriter>();
533 if (!HasWriter<LegacyCellProliferativeTypesWriter>())
535 AddCellWriter<LegacyCellProliferativeTypesWriter>();
541 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
548 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
551 p_pop_writer->WriteHeader(
this);
556 for (boost::shared_ptr<count_writer_t> p_count_writer : mCellPopulationCountWriters)
559 p_count_writer->WriteHeader(
this);
564 for (boost::shared_ptr<event_writer_t> p_event_writer : mCellPopulationEventWriters)
567 p_event_writer->WriteHeader(
this);
571template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
576 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
580 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
582 p_pop_writer->OpenOutputFileForAppend(rOutputFileHandler);
586template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
593 if (!(mCellWriters.empty() && mCellPopulationWriters.empty() && mCellPopulationCountWriters.empty()))
596 SetDefaultCellMutationStateAndProliferativeTypeOrdering();
600 OpenRoundRobinWritersFilesForAppend(output_file_handler);
605 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
607 p_cell_writer->WriteTimeStamp();
609 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
611 p_pop_writer->WriteTimeStamp();
616 pop_writer_iter != mCellPopulationWriters.end();
619 AcceptPopulationWriter(*pop_writer_iter);
622 AcceptCellWritersAcrossPopulation();
627 for (boost::shared_ptr<cell_writer_t> p_cell_writer : mCellWriters)
629 p_cell_writer->WriteNewline();
631 for (boost::shared_ptr<pop_writer_t> p_pop_writer : mCellPopulationWriters)
633 p_pop_writer->WriteNewline();
636 CloseRoundRobinWritersFiles();
646 for (boost::shared_ptr<count_writer_t> p_count_writer : mCellPopulationCountWriters)
649 p_count_writer->WriteTimeStamp();
653 count_writer_iter != mCellPopulationCountWriters.end();
656 AcceptPopulationCountWriter(*count_writer_iter);
662 for (boost::shared_ptr<count_writer_t> p_count_writer : mCellPopulationCountWriters)
664 p_count_writer->WriteNewline();
665 p_count_writer->CloseFile();
676 for (boost::shared_ptr<event_writer_t> p_event_writer : mCellPopulationEventWriters)
682 event_writer_iter != mCellPopulationEventWriters.end();
685 AcceptPopulationEventWriter(*event_writer_iter);
691 for (boost::shared_ptr<event_writer_t> p_event_writer : mCellPopulationEventWriters)
693 p_event_writer->CloseFile();
701 WriteVtkResultsToFile(rDirectory);
705template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
709 cell_iter != this->End();
713 cell_writer_iter != mCellWriters.end();
716 AcceptCellWriter(*cell_writer_iter, *cell_iter);
721template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
724 std::string cell_population_type = GetIdentifier();
726 *rParamsFile <<
"\t<" << cell_population_type <<
">\n";
727 OutputCellPopulationParameters(rParamsFile);
728 *rParamsFile <<
"\t</" << cell_population_type <<
">\n";
729 *rParamsFile <<
"\n";
730 *rParamsFile <<
"\t<CellCycleModels>\n";
738 std::set<std::string> unique_cell_cycle_models;
739 std::vector<CellPtr> first_cell_with_unique_CCM;
741 cell_iter != this->End();
744 std::string identifier = cell_iter->GetCellCycleModel()->GetIdentifier();
745 if (unique_cell_cycle_models.count(identifier) == 0)
747 unique_cell_cycle_models.insert(identifier);
748 first_cell_with_unique_CCM.push_back((*cell_iter));
753 for (
unsigned i=0; i<first_cell_with_unique_CCM.size(); i++)
756 first_cell_with_unique_CCM[i]->GetCellCycleModel()->OutputCellCycleModelInfo(rParamsFile);
758 *rParamsFile <<
"\t</CellCycleModels>\n";
760 *rParamsFile <<
"\n";
761 *rParamsFile <<
"\t<SrnModels>\n";
769 std::set<std::string> unique_srn_models;
770 std::vector<CellPtr> first_cell_with_unique_SRN;
772 cell_iter != this->End();
775 std::string identifier = cell_iter->GetSrnModel()->GetIdentifier();
776 if (unique_srn_models.count(identifier) == 0)
778 unique_srn_models.insert(identifier);
779 first_cell_with_unique_SRN.push_back((*cell_iter));
784 for (
unsigned i=0; i<first_cell_with_unique_SRN.size(); i++)
787 first_cell_with_unique_SRN[i]->GetSrnModel()->OutputSrnModelInfo(rParamsFile);
790 *rParamsFile <<
"\t</SrnModels>\n";
793template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
796 *rParamsFile <<
"\t\t<OutputResultsForChasteVisualizer>" << mOutputResultsForChasteVisualizer <<
"</OutputResultsForChasteVisualizer>\n";
799template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
804template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
807 return mOutputResultsForChasteVisualizer;
810template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
813 mOutputResultsForChasteVisualizer = outputResultsForChasteVisualizer;
816template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
819 return mDivisionsInformation;
821template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
824 mDivisionsInformation.push_back(divisionInformation);
827template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
830 mDivisionsInformation.clear();
833template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
836 return mRemovalsInformation;
839template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
842 mRemovalsInformation.push_back(removalInformation);
845template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
848 mRemovalsInformation.clear();
851template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
855 c_vector<double, SPACE_DIM> cell_location = GetLocationOfCellCentre(pCell);
856 if (HasWriter<CellRemovalLocationsWriter>())
858 std::stringstream removal_info;
860 for (
unsigned i = 0; i < SPACE_DIM; i++)
862 removal_info << cell_location[i] <<
"\t";
864 removal_info <<
"\t" << pCell->GetAge() <<
"\t" << pCell->GetCellId() <<
"\t" << killerInfo <<
"\t";
866 AddRemovalInformation(removal_info.str());
870template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
874 GenerateRemovalInformation(pCell, killerInfo);
880template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
884 GenerateRemovalInformation(pCell, killerInfo);
887 pCell->StartApoptosis();
890template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
896template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
900 c_vector<double,SPACE_DIM> centre = GetCentroidOfCellPopulation();
903 c_vector<double,SPACE_DIM> max_distance_from_centre = zero_vector<double>(SPACE_DIM);
905 cell_iter != this->End();
908 c_vector<double,SPACE_DIM> cell_location = GetLocationOfCellCentre(*cell_iter);
911 c_vector<double,SPACE_DIM> displacement;
912 displacement = centre - cell_location;
914 for (
unsigned i=0; i<SPACE_DIM; i++)
916 if (displacement[i] > max_distance_from_centre[i])
918 max_distance_from_centre[i] = displacement[i];
923 return max_distance_from_centre;
926template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
929 assert(index1 != index2);
931 std::pair<unsigned, unsigned> ordered_pair;
934 ordered_pair.first = index1;
935 ordered_pair.second = index2;
939 ordered_pair.first = index2;
940 ordered_pair.second = index1;
945template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
948 bool non_apoptotic_cell_present =
false;
950 if (IsCellAttachedToLocationIndex(pdeNodeIndex))
952 non_apoptotic_cell_present = !(GetCellUsingLocationIndex(pdeNodeIndex)->template HasCellProperty<ApoptoticCellProperty>());
955 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()