36 #include <boost/bind.hpp>
37 #include <boost/mem_fn.hpp>
42 #include "AbstractCellPopulation.hpp"
43 #include "AbstractOdeBasedCellCycleModel.hpp"
49 #include "BoundaryNodeWriter.hpp"
50 #include "CellProliferativeTypesWriter.hpp"
53 #include "CellMutationStatesCountWriter.hpp"
54 #include "CellProliferativePhasesCountWriter.hpp"
55 #include "CellProliferativeTypesCountWriter.hpp"
56 #include "NodeLocationWriter.hpp"
58 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
60 std::vector<CellPtr>& rCells,
61 const std::vector<unsigned> locationIndices)
63 mCells(rCells.begin(), rCells.end()),
64 mCentroid(zero_vector<
double>(SPACE_DIM)),
66 mOutputResultsForChasteVisualizer(true)
73 std::vector<CellPtr>().swap(rCells);
76 if (!locationIndices.empty())
78 if (
mCells.size() != locationIndices.size())
80 EXCEPTION(
"There is not a one-one correspondence between cells and location indices");
88 std::list<CellPtr>::iterator it =
mCells.begin();
89 for (
unsigned i=0; it !=
mCells.end(); ++it, ++i)
96 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
102 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
107 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
111 cell_iter!=this->End();
114 cell_iter->InitialiseCellCycleModel();
115 cell_iter->InitialiseSrnModel();
119 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
123 cell_iter!=this->End();
126 cell_iter->GetCellData()->SetItem(rDataName, dataValue);
130 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
136 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
142 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
145 unsigned counter = 0;
147 cell_iter!=this->End();
155 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
158 return mCells.size();
161 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
167 cell_iter->SetAncestor(p_cell_ancestor);
171 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
174 std::set<unsigned> remaining_ancestors;
177 remaining_ancestors.insert(cell_iter->GetAncestor());
179 return remaining_ancestors;
182 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
185 std::vector<unsigned> mutation_state_count;
186 const std::vector<boost::shared_ptr<AbstractCellProperty> >& r_cell_properties
187 = mpCellPropertyRegistry->rGetAllCellProperties();
190 for (
unsigned i=0; i<r_cell_properties.size(); i++)
192 if (r_cell_properties[i]->IsSubType<AbstractCellMutationState>())
194 mutation_state_count.push_back(r_cell_properties[i]->GetCellCount());
202 unsigned local_size = mutation_state_count.size();
203 unsigned global_size;
205 assert(local_size == global_size);
207 std::vector<unsigned> mutation_counts(global_size);
208 MPI_Allreduce(&mutation_state_count[0], &mutation_counts[0], mutation_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
210 mutation_state_count = mutation_counts;
213 return mutation_state_count;
216 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
219 std::vector<unsigned> proliferative_type_count;
220 const std::vector<boost::shared_ptr<AbstractCellProperty> >& r_cell_properties
221 = mpCellPropertyRegistry->rGetAllCellProperties();
224 for (
unsigned i=0; i<r_cell_properties.size(); i++)
226 if (r_cell_properties[i]->IsSubType<AbstractCellProliferativeType>())
228 proliferative_type_count.push_back(r_cell_properties[i]->GetCellCount());
236 unsigned local_size = proliferative_type_count.size();
237 unsigned global_size;
240 assert(local_size == global_size);
242 std::vector<unsigned> total_types_counts(global_size);
243 MPI_Allreduce(&proliferative_type_count[0], &total_types_counts[0], total_types_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
245 proliferative_type_count = total_types_counts;
248 return proliferative_type_count;
251 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
254 std::vector<unsigned> cell_cycle_phase_count(5);
255 for (
unsigned i=0; i<5; i++)
257 cell_cycle_phase_count[i] = 0;
261 cell_iter != this->End();
264 switch ((*cell_iter)->GetCellCycleModel()->GetCurrentCellCyclePhase())
267 cell_cycle_phase_count[0]++;
270 cell_cycle_phase_count[1]++;
273 cell_cycle_phase_count[2]++;
276 cell_cycle_phase_count[3]++;
279 cell_cycle_phase_count[4]++;
289 std::vector<unsigned> phase_counts(cell_cycle_phase_count.size(), 0u);
290 MPI_Allreduce(&cell_cycle_phase_count[0], &phase_counts[0], phase_counts.size(), MPI_UNSIGNED, MPI_SUM,
PetscTools::GetWorld());
292 cell_cycle_phase_count = phase_counts;
295 return cell_cycle_phase_count;
298 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
302 std::set<CellPtr> cells = mLocationCellMap[index];
305 if (cells.size() == 1)
307 return *(cells.begin());
311 EXCEPTION(
"Location index input argument does not correspond to a Cell");
315 EXCEPTION(
"Multiple cells are attached to a single location index.");
319 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
323 return mLocationCellMap[index];
326 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
330 std::set<CellPtr> cells = mLocationCellMap[index];
333 return !(cells.empty());
336 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
340 mLocationCellMap[index].clear();
341 mCellLocationMap.erase(pCell.get());
344 mLocationCellMap[index].insert(pCell);
347 mCellLocationMap[pCell.get()] = index;
350 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
353 mLocationCellMap[index].insert(pCell);
354 mCellLocationMap[pCell.get()] = index;
357 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
360 std::set<CellPtr>::iterator cell_iter = mLocationCellMap[index].find(pCell);
362 if (cell_iter == mLocationCellMap[index].end())
364 EXCEPTION(
"Tried to remove a cell which is not attached to the given location index");
368 mLocationCellMap[index].erase(cell_iter);
369 mCellLocationMap.erase(pCell.get());
373 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
377 RemoveCellUsingLocationIndex(old_index, pCell);
380 AddCellUsingLocationIndex(new_index, pCell);
383 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
387 assert(this->mCellLocationMap.find(pCell.get()) != this->mCellLocationMap.end());
389 return mCellLocationMap[pCell.get()];
392 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
395 return mpCellPropertyRegistry;
398 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
401 boost::shared_ptr<CellPropertyRegistry> p_registry = GetCellPropertyRegistry();
402 if (!p_registry->HasOrderingBeenSpecified())
404 std::vector<boost::shared_ptr<AbstractCellProperty> > mutations_and_proliferative_types;
414 p_registry->SpecifyOrdering(mutations_and_proliferative_types);
418 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
421 mCentroid = zero_vector<double>(SPACE_DIM);
423 cell_iter != this->End();
426 mCentroid += GetLocationOfCellCentre(*cell_iter);
428 mCentroid /= this->GetNumRealCells();
433 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
438 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
442 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
448 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
454 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
458 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
464 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
470 *mpVtkMetaFile <<
" </Collection>\n";
471 *mpVtkMetaFile <<
"</VTKFile>\n";
472 mpVtkMetaFile->close();
476 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
481 *mpVtkMetaFile <<
"<?xml version=\"1.0\"?>\n";
482 *mpVtkMetaFile <<
"<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
483 *mpVtkMetaFile <<
" <Collection>\n";
486 if (mOutputResultsForChasteVisualizer)
488 if (!HasWriter<NodeLocationWriter>())
490 AddPopulationWriter<NodeLocationWriter>();
492 if (!HasWriter<BoundaryNodeWriter>())
494 AddPopulationWriter<BoundaryNodeWriter>();
496 if (!HasWriter<CellProliferativeTypesWriter>())
498 AddCellWriter<CellProliferativeTypesWriter>();
504 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
511 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
514 p_pop_writer->WriteHeader(
this);
519 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
522 p_count_writer->WriteHeader(
this);
526 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
531 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
535 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
537 p_pop_writer->OpenOutputFileForAppend(rOutputFileHandler);
541 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
548 if (!(mCellWriters.empty() && mCellPopulationWriters.empty() && mCellPopulationCountWriters.empty()))
551 SetDefaultCellMutationStateAndProliferativeTypeOrdering();
555 OpenRoundRobinWritersFilesForAppend(output_file_handler);
560 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
562 p_cell_writer->WriteTimeStamp();
564 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
566 p_pop_writer->WriteTimeStamp();
571 pop_writer_iter != mCellPopulationWriters.end();
574 AcceptPopulationWriter(*pop_writer_iter);
577 AcceptCellWritersAcrossPopulation();
582 BOOST_FOREACH(boost::shared_ptr<cell_writer_t> p_cell_writer, mCellWriters)
584 p_cell_writer->WriteNewline();
586 BOOST_FOREACH(boost::shared_ptr<pop_writer_t> p_pop_writer, mCellPopulationWriters)
588 p_pop_writer->WriteNewline();
591 CloseRoundRobinWritersFiles();
601 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
604 p_count_writer->WriteTimeStamp();
608 count_writer_iter != mCellPopulationCountWriters.end();
611 AcceptPopulationCountWriter(*count_writer_iter);
617 BOOST_FOREACH(boost::shared_ptr<count_writer_t> p_count_writer, mCellPopulationCountWriters)
619 p_count_writer->WriteNewline();
620 p_count_writer->CloseFile();
628 WriteVtkResultsToFile(rDirectory);
632 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
636 cell_iter != this->End();
640 cell_writer_iter != mCellWriters.end();
643 AcceptCellWriter(*cell_writer_iter, *cell_iter);
648 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
651 std::string cell_population_type = GetIdentifier();
653 *rParamsFile <<
"\t<" << cell_population_type <<
">\n";
654 OutputCellPopulationParameters(rParamsFile);
655 *rParamsFile <<
"\t</" << cell_population_type <<
">\n";
656 *rParamsFile <<
"\n";
657 *rParamsFile <<
"\t<CellCycleModels>\n";
665 std::set<std::string> unique_cell_cycle_models;
666 std::vector<CellPtr> first_cell_with_unique_CCM;
668 cell_iter != this->End();
671 std::string identifier = cell_iter->GetCellCycleModel()->GetIdentifier();
672 if (unique_cell_cycle_models.count(identifier) == 0)
674 unique_cell_cycle_models.insert(identifier);
675 first_cell_with_unique_CCM.push_back((*cell_iter));
680 for (
unsigned i=0; i<first_cell_with_unique_CCM.size(); i++)
683 first_cell_with_unique_CCM[i]->GetCellCycleModel()->OutputCellCycleModelInfo(rParamsFile);
685 *rParamsFile <<
"\t</CellCycleModels>\n";
687 *rParamsFile <<
"\n";
688 *rParamsFile <<
"\t<SrnModels>\n";
696 std::set<std::string> unique_srn_models;
697 std::vector<CellPtr> first_cell_with_unique_SRN;
699 cell_iter != this->End();
702 std::string identifier = cell_iter->GetSrnModel()->GetIdentifier();
703 if (unique_srn_models.count(identifier) == 0)
705 unique_srn_models.insert(identifier);
706 first_cell_with_unique_SRN.push_back((*cell_iter));
711 for (
unsigned i=0; i<first_cell_with_unique_SRN.size(); i++)
714 first_cell_with_unique_SRN[i]->GetSrnModel()->OutputSrnModelInfo(rParamsFile);
717 *rParamsFile <<
"\t</SrnModels>\n";
720 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
723 *rParamsFile <<
"\t\t<OutputResultsForChasteVisualizer>" << mOutputResultsForChasteVisualizer <<
"</OutputResultsForChasteVisualizer>\n";
726 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
729 return mOutputResultsForChasteVisualizer;
732 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
735 mOutputResultsForChasteVisualizer = outputResultsForChasteVisualizer;
738 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
744 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
748 c_vector<double,SPACE_DIM> centre = GetCentroidOfCellPopulation();
751 c_vector<double,SPACE_DIM> max_distance_from_centre = zero_vector<double>(SPACE_DIM);
753 cell_iter != this->End();
756 c_vector<double,SPACE_DIM> cell_location = GetLocationOfCellCentre(*cell_iter);
759 c_vector<double,SPACE_DIM> displacement;
760 displacement = centre - cell_location;
762 for (
unsigned i=0; i<SPACE_DIM; i++)
764 if (displacement[i] > max_distance_from_centre[i])
766 max_distance_from_centre[i] = displacement[i];
771 return max_distance_from_centre;
774 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
777 assert(index1 != index2);
779 std::pair<unsigned, unsigned> ordered_pair;
782 ordered_pair.first = index1;
783 ordered_pair.second = index2;
787 ordered_pair.first = index2;
788 ordered_pair.second = index1;
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()
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 void OpenWritersFiles(OutputFileHandler &rOutputFileHandler)
std::list< CellPtr > mCells
void OutputCellPopulationInfo(out_stream &rParamsFile)
virtual bool IsRoomToDivide(CellPtr pCell)
AbstractMesh< ELEMENT_DIM, SPACE_DIM > & rGetMesh()
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()