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"
52 #include "CellMutationStatesCountWriter.hpp"
53 #include "CellProliferativePhasesCountWriter.hpp"
54 #include "CellProliferativeTypesCountWriter.hpp"
55 #include "NodeLocationWriter.hpp"
58 #include "WildTypeCellMutationState.hpp"
59 #include "ApcOneHitCellMutationState.hpp"
60 #include "ApcTwoHitCellMutationState.hpp"
61 #include "BetaCateninOneHitCellMutationState.hpp"
62 #include "DefaultCellProliferativeType.hpp"
63 #include "StemCellProliferativeType.hpp"
64 #include "TransitCellProliferativeType.hpp"
65 #include "DifferentiatedCellProliferativeType.hpp"
67 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
69 std::vector<CellPtr>& rCells,
70 const std::vector<unsigned> locationIndices)
72 mCells(rCells.begin(), rCells.end()),
73 mCentroid(zero_vector<
double>(SPACE_DIM)),
75 mOutputResultsForChasteVisualizer(true)
82 std::vector<CellPtr>().swap(rCells);
85 if (!locationIndices.empty())
87 if (
mCells.size() != locationIndices.size())
89 EXCEPTION(
"There is not a one-one correspondence between cells and location indices");
97 std::list<CellPtr>::iterator it =
mCells.begin();
98 for (
unsigned i=0; it !=
mCells.end(); ++it, ++i)
105 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
111 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
116 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
120 cell_iter!=this->End();
123 cell_iter->InitialiseCellCycleModel();
124 cell_iter->InitialiseSrnModel();
128 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
132 cell_iter!=this->End();
135 cell_iter->GetCellData()->SetItem(rDataName, dataValue);
139 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
145 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
151 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
154 unsigned counter = 0;
156 cell_iter!=this->End();
164 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
167 return mCells.size();
170 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
176 cell_iter->SetAncestor(p_cell_ancestor);
180 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
183 std::set<unsigned> remaining_ancestors;
186 remaining_ancestors.insert(cell_iter->GetAncestor());
188 return remaining_ancestors;
191 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
194 std::vector<unsigned> mutation_state_count;
195 const std::vector<boost::shared_ptr<AbstractCellProperty> >& r_cell_properties
196 = 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[0], &mutation_counts[0], mutation_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
219 mutation_state_count = mutation_counts;
222 return mutation_state_count;
225 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
228 std::vector<unsigned> proliferative_type_count;
229 const std::vector<boost::shared_ptr<AbstractCellProperty> >& r_cell_properties
230 = mpCellPropertyRegistry->rGetAllCellProperties();
233 for (
unsigned i=0; i<r_cell_properties.size(); i++)
235 if (r_cell_properties[i]->IsSubType<AbstractCellProliferativeType>())
237 proliferative_type_count.push_back(r_cell_properties[i]->GetCellCount());
245 unsigned local_size = proliferative_type_count.size();
246 unsigned global_size;
249 assert(local_size == global_size);
251 std::vector<unsigned> total_types_counts(global_size);
252 MPI_Allreduce(&proliferative_type_count[0], &total_types_counts[0], total_types_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
254 proliferative_type_count = total_types_counts;
257 return proliferative_type_count;
260 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
263 std::vector<unsigned> cell_cycle_phase_count(5);
264 for (
unsigned i=0; i<5; i++)
266 cell_cycle_phase_count[i] = 0;
273 if (GetNumAllCells() > 0u)
275 if (dynamic_cast<AbstractPhaseBasedCellCycleModel*>((*(this->Begin()))->GetCellCycleModel()) ==
nullptr)
277 EXCEPTION(
"You are trying to record the cell cycle phase of cells with a non phase based cell cycle model.");
281 cell_iter != this->End();
284 switch (static_cast<AbstractPhaseBasedCellCycleModel*>((*cell_iter)->GetCellCycleModel())->GetCurrentCellCyclePhase())
287 cell_cycle_phase_count[0]++;
290 cell_cycle_phase_count[1]++;
293 cell_cycle_phase_count[2]++;
296 cell_cycle_phase_count[3]++;
299 cell_cycle_phase_count[4]++;
310 std::vector<unsigned> phase_counts(cell_cycle_phase_count.size(), 0u);
311 MPI_Allreduce(&cell_cycle_phase_count[0], &phase_counts[0], phase_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
313 cell_cycle_phase_count = phase_counts;
316 return cell_cycle_phase_count;
319 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
323 std::set<CellPtr> cells = mLocationCellMap[index];
326 if (cells.size() == 1)
328 return *(cells.begin());
332 EXCEPTION(
"Location index input argument does not correspond to a Cell");
336 EXCEPTION(
"Multiple cells are attached to a single location index.");
340 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
344 return mLocationCellMap[index];
347 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
351 std::set<CellPtr> cells = mLocationCellMap[index];
354 return !(cells.empty());
357 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
362 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
366 mLocationCellMap[index].clear();
367 mCellLocationMap.erase(pCell.get());
370 mLocationCellMap[index].insert(pCell);
373 mCellLocationMap[pCell.get()] = index;
376 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
379 mLocationCellMap[index].insert(pCell);
380 mCellLocationMap[pCell.get()] = index;
383 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
386 std::set<CellPtr>::iterator cell_iter = mLocationCellMap[index].find(pCell);
388 if (cell_iter == mLocationCellMap[index].end())
390 EXCEPTION(
"Tried to remove a cell which is not attached to the given location index");
394 mLocationCellMap[index].erase(cell_iter);
395 mCellLocationMap.erase(pCell.get());
399 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
403 RemoveCellUsingLocationIndex(old_index, pCell);
406 AddCellUsingLocationIndex(new_index, pCell);
409 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
413 assert(this->mCellLocationMap.find(pCell.get()) != this->mCellLocationMap.end());
415 return mCellLocationMap[pCell.get()];
418 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
421 return mpCellPropertyRegistry;
424 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
427 boost::shared_ptr<CellPropertyRegistry> p_registry = GetCellPropertyRegistry();
428 if (!p_registry->HasOrderingBeenSpecified())
430 std::vector<boost::shared_ptr<AbstractCellProperty> > mutations_and_proliferative_types;
441 p_registry->SpecifyOrdering(mutations_and_proliferative_types);
445 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
448 mCentroid = zero_vector<double>(SPACE_DIM);
450 cell_iter != this->End();
453 mCentroid += GetLocationOfCellCentre(*cell_iter);
455 mCentroid /= this->GetNumRealCells();
460 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
465 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
469 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
475 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
481 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
485 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
491 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
497 *mpVtkMetaFile <<
" </Collection>\n";
498 *mpVtkMetaFile <<
"</VTKFile>\n";
499 mpVtkMetaFile->close();
503 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
508 *mpVtkMetaFile <<
"<?xml version=\"1.0\"?>\n";
509 *mpVtkMetaFile <<
"<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
510 *mpVtkMetaFile <<
" <Collection>\n";
513 if (mOutputResultsForChasteVisualizer)
515 if (!HasWriter<NodeLocationWriter>())
517 AddPopulationWriter<NodeLocationWriter>();
519 if (!HasWriter<BoundaryNodeWriter>())
521 AddPopulationWriter<BoundaryNodeWriter>();
523 if (!HasWriter<CellProliferativeTypesWriter>())
525 AddCellWriter<CellProliferativeTypesWriter>();
531 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
538 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
541 p_pop_writer->WriteHeader(
this);
546 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
549 p_count_writer->WriteHeader(
this);
553 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
558 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
562 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
564 p_pop_writer->OpenOutputFileForAppend(rOutputFileHandler);
568 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
575 if (!(mCellWriters.empty() && mCellPopulationWriters.empty() && mCellPopulationCountWriters.empty()))
578 SetDefaultCellMutationStateAndProliferativeTypeOrdering();
582 OpenRoundRobinWritersFilesForAppend(output_file_handler);
587 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
589 p_cell_writer->WriteTimeStamp();
591 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
593 p_pop_writer->WriteTimeStamp();
598 pop_writer_iter != mCellPopulationWriters.end();
601 AcceptPopulationWriter(*pop_writer_iter);
604 AcceptCellWritersAcrossPopulation();
609 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
611 p_cell_writer->WriteNewline();
613 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
615 p_pop_writer->WriteNewline();
618 CloseRoundRobinWritersFiles();
628 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
631 p_count_writer->WriteTimeStamp();
635 count_writer_iter != mCellPopulationCountWriters.end();
638 AcceptPopulationCountWriter(*count_writer_iter);
644 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
646 p_count_writer->WriteNewline();
647 p_count_writer->CloseFile();
655 WriteVtkResultsToFile(rDirectory);
659 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
663 cell_iter != this->End();
667 cell_writer_iter != mCellWriters.end();
670 AcceptCellWriter(*cell_writer_iter, *cell_iter);
675 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
678 std::string cell_population_type = GetIdentifier();
680 *rParamsFile <<
"\t<" << cell_population_type <<
">\n";
681 OutputCellPopulationParameters(rParamsFile);
682 *rParamsFile <<
"\t</" << cell_population_type <<
">\n";
683 *rParamsFile <<
"\n";
684 *rParamsFile <<
"\t<CellCycleModels>\n";
692 std::set<std::string> unique_cell_cycle_models;
693 std::vector<CellPtr> first_cell_with_unique_CCM;
695 cell_iter != this->End();
698 std::string identifier = cell_iter->GetCellCycleModel()->GetIdentifier();
699 if (unique_cell_cycle_models.count(identifier) == 0)
701 unique_cell_cycle_models.insert(identifier);
702 first_cell_with_unique_CCM.push_back((*cell_iter));
707 for (
unsigned i=0; i<first_cell_with_unique_CCM.size(); i++)
710 first_cell_with_unique_CCM[i]->GetCellCycleModel()->OutputCellCycleModelInfo(rParamsFile);
712 *rParamsFile <<
"\t</CellCycleModels>\n";
714 *rParamsFile <<
"\n";
715 *rParamsFile <<
"\t<SrnModels>\n";
723 std::set<std::string> unique_srn_models;
724 std::vector<CellPtr> first_cell_with_unique_SRN;
726 cell_iter != this->End();
729 std::string identifier = cell_iter->GetSrnModel()->GetIdentifier();
730 if (unique_srn_models.count(identifier) == 0)
732 unique_srn_models.insert(identifier);
733 first_cell_with_unique_SRN.push_back((*cell_iter));
738 for (
unsigned i=0; i<first_cell_with_unique_SRN.size(); i++)
741 first_cell_with_unique_SRN[i]->GetSrnModel()->OutputSrnModelInfo(rParamsFile);
744 *rParamsFile <<
"\t</SrnModels>\n";
747 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
750 *rParamsFile <<
"\t\t<OutputResultsForChasteVisualizer>" << mOutputResultsForChasteVisualizer <<
"</OutputResultsForChasteVisualizer>\n";
753 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
758 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
761 return mOutputResultsForChasteVisualizer;
764 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
767 mOutputResultsForChasteVisualizer = outputResultsForChasteVisualizer;
770 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
776 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
780 c_vector<double,SPACE_DIM> centre = GetCentroidOfCellPopulation();
783 c_vector<double,SPACE_DIM> max_distance_from_centre = zero_vector<double>(SPACE_DIM);
785 cell_iter != this->End();
788 c_vector<double,SPACE_DIM> cell_location = GetLocationOfCellCentre(*cell_iter);
791 c_vector<double,SPACE_DIM> displacement;
792 displacement = centre - cell_location;
794 for (
unsigned i=0; i<SPACE_DIM; i++)
796 if (displacement[i] > max_distance_from_centre[i])
798 max_distance_from_centre[i] = displacement[i];
803 return max_distance_from_centre;
806 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
809 assert(index1 != index2);
811 std::pair<unsigned, unsigned> ordered_pair;
814 ordered_pair.first = index1;
815 ordered_pair.second = index2;
819 ordered_pair.first = index2;
820 ordered_pair.second = index1;
825 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
828 bool non_apoptotic_cell_present =
false;
830 if (IsCellAttachedToLocationIndex(pdeNodeIndex))
832 non_apoptotic_cell_present = !(GetCellUsingLocationIndex(pdeNodeIndex)->template HasCellProperty<ApoptoticCellProperty>());
835 return non_apoptotic_cell_present;
virtual void SimulationSetupHook(AbstractCellBasedSimulation< ELEMENT_DIM, SPACE_DIM > *pSimulation)
c_vector< double, SPACE_DIM > GetCentroidOfCellPopulation()
void OpenOutputFileForAppend(OutputFileHandler &rOutputFileHandler)
virtual CellPtr GetCellUsingLocationIndex(unsigned index)
void SetDefaultCellMutationStateAndProliferativeTypeOrdering()
unsigned GetLocationIndexUsingCell(CellPtr pCell)
virtual void UpdateCellProcessLocation()
void SetDataOnAllCells(const std::string &rDataName, double dataValue)
boost::shared_ptr< CellPropertyRegistry > mpCellPropertyRegistry
virtual void RemoveCellUsingLocationIndex(unsigned index, CellPtr pCell)
virtual void OutputCellPopulationParameters(out_stream &rParamsFile)=0
#define EXCEPTION(message)
void SetCellAncestorsToLocationIndices()
std::set< CellPtr > GetCellsUsingLocationIndex(unsigned index)
unsigned GetNumRealCells()
virtual bool IsCellAttachedToLocationIndex(unsigned index)
unsigned GetNumAllCells()
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()
std::vector< unsigned > GetCellCyclePhaseCount()
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
c_vector< double, SPACE_DIM > GetSizeOfCellPopulation()
virtual void AcceptCellWritersAcrossPopulation()
std::pair< unsigned, unsigned > CreateOrderedPair(unsigned index1, unsigned index2)
std::map< unsigned, std::set< CellPtr > > mLocationCellMap
void SetCellUsingLocationIndex(unsigned index, CellPtr pCell)
std::map< Cell *, unsigned > mCellLocationMap
boost::shared_ptr< CellPropertyRegistry > GetCellPropertyRegistry()
void OpenRoundRobinWritersFilesForAppend(OutputFileHandler &rOutputFileHandler)
virtual bool IsPdeNodeAssociatedWithNonApoptoticCell(unsigned pdeNodeIndex)
virtual void OpenWritersFiles(OutputFileHandler &rOutputFileHandler)
std::list< CellPtr > mCells
void OutputCellPopulationInfo(out_stream &rParamsFile)
virtual bool IsRoomToDivide(CellPtr pCell)
AbstractMesh< ELEMENT_DIM, SPACE_DIM > & rGetMesh()
virtual void WriteDataToVisualizerSetupFile(out_stream &pVizSetupFile)
bool GetOutputResultsForChasteVisualizer()
virtual void OpenOutputFile(OutputFileHandler &rOutputFileHandler)
AbstractCellPopulation(AbstractMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell)
std::vector< unsigned > GetCellProliferativeTypeCount()
virtual ~AbstractCellPopulation()
#define MAKE_PTR_ARGS(TYPE, NAME, ARGS)
void CloseRoundRobinWritersFiles()
std::vector< unsigned > GetCellMutationStateCount()
std::set< unsigned > GetCellAncestors()