41 #include "AbstractCellBasedSimulation.hpp"
42 #include "CellBasedEventHandler.hpp"
43 #include "LogFile.hpp"
44 #include "Version.hpp"
45 #include "ExecutableSupport.hpp"
49 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
51 bool deleteCellPopulationInDestructor,
55 mrCellPopulation(rCellPopulation),
56 mDeleteCellPopulationInDestructor(deleteCellPopulationInDestructor),
57 mInitialiseCells(initialiseCells),
59 mUpdateCellPopulation(true),
61 mSimulationOutputDirectory(mOutputDirectory),
64 mOutputDivisionLocations(false),
65 mOutputCellVelocities(false),
66 mSamplingTimestepMultiple(1),
67 mpCellBasedPdeHandler(NULL)
78 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
81 if (mDeleteCellPopulationInDestructor)
83 delete &mrCellPopulation;
84 delete mpCellBasedPdeHandler;
88 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
91 mpCellBasedPdeHandler = pCellBasedPdeHandler;
94 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
97 return mpCellBasedPdeHandler;
100 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
108 unsigned num_births_this_step = 0;
112 cell_iter != mrCellPopulation.End();
116 double cell_age = cell_iter->GetAge();
119 if (cell_iter->ReadyToDivide())
122 if (mrCellPopulation.IsRoomToDivide(*cell_iter))
125 CellPtr p_new_cell = cell_iter->Divide();
128 c_vector<double, SPACE_DIM> new_location = CalculateCellDivisionVector(*cell_iter);
147 if (mOutputDivisionLocations)
149 c_vector<double, SPACE_DIM> cell_location = mrCellPopulation.GetLocationOfCellCentre(*cell_iter);
152 for (
unsigned i=0; i<SPACE_DIM; i++)
154 *mpDivisionLocationFile << cell_location[i] <<
"\t";
156 *mpDivisionLocationFile <<
"\t" << cell_age <<
"\n";
160 mrCellPopulation.AddCell(p_new_cell, new_location, *cell_iter);
163 num_births_this_step++;
168 return num_births_this_step;
171 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
174 unsigned num_deaths_this_step = 0;
181 killer_iter != mCellKillers.end();
184 (*killer_iter)->CheckAndLabelCellsForApoptosisOrDeath();
187 num_deaths_this_step += mrCellPopulation.RemoveDeadCells();
189 return num_deaths_this_step;
192 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
199 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
205 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
211 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
217 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
224 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
227 mOutputDirectory = outputDirectory;
228 mSimulationOutputDirectory = mOutputDirectory;
231 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
234 return mOutputDirectory;
237 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
240 assert(samplingTimestepMultiple > 0);
241 mSamplingTimestepMultiple = samplingTimestepMultiple;
244 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
247 return mrCellPopulation;
250 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
253 return mrCellPopulation;
256 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
259 mUpdateCellPopulation = updateCellPopulation;
262 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
265 return mUpdateCellPopulation;
268 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
274 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
277 mCellKillers.push_back(pCellKiller);
280 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
283 mCellKillers.clear();
286 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
289 mSimulationModifiers.push_back(pSimulationModifier);
292 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
295 return &mSimulationModifiers;
298 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
301 std::vector<double> location;
302 for (
unsigned i=0; i<SPACE_DIM; i++)
304 location.push_back(mrCellPopulation.GetNode(rNodeIndex)->rGetLocation()[i]);
309 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
317 double current_time = p_simulation_time->
GetTime();
323 EXCEPTION(
"SetEndTime has not yet been called.");
333 unsigned num_time_steps = (
unsigned) ((mEndTime-current_time)/mDt+0.5);
334 if (current_time > 0)
342 EXCEPTION(
"End time and number of timesteps already setup. You should not use SimulationTime::SetEndTimeAndNumberOfTimeSteps in cell-based tests.");
350 if (mOutputDirectory ==
"")
355 double time_now = p_simulation_time->
GetTime();
356 std::ostringstream time_string;
357 time_string << time_now;
359 std::string results_directory = mOutputDirectory +
"/results_from_time_" + time_string.str();
360 mSimulationOutputDirectory = results_directory;
367 mrCellPopulation.OpenWritersFiles(output_file_handler);
369 if (mOutputDivisionLocations)
371 mpDivisionLocationFile = output_file_handler.
OpenOutputFile(
"divisions.dat");
373 if (mOutputCellVelocities)
375 OutputFileHandler output_file_handler2(this->mSimulationOutputDirectory+
"/",
false);
376 mpCellVelocitiesFile = output_file_handler2.
OpenOutputFile(
"cellvelocities.dat");
381 mpVizSetupFile = output_file_handler.
OpenOutputFile(
"results.vizsetup");
385 if (mpCellBasedPdeHandler != NULL)
387 mpCellBasedPdeHandler->OpenResultsFiles(this->mSimulationOutputDirectory);
390 *this->mpVizSetupFile <<
"PDE \n";
399 mpCellBasedPdeHandler->SolvePdeAndWriteResultsToFile(this->mSamplingTimestepMultiple);
407 iter != mSimulationModifiers.end();
410 (*iter)->SetupSolve(this->mrCellPopulation,this->mSimulationOutputDirectory);
417 LOG(1,
"Setting up cells...");
419 cell_iter != mrCellPopulation.End();
426 cell_iter->ReadyToDivide();
431 WriteVisualizerSetupFile();
435 *mpVizSetupFile << std::flush;
438 mrCellPopulation.WriteResultsToFiles(results_directory+
"/");
440 OutputSimulationSetup();
444 while (!( p_simulation_time->
IsFinished() || StoppingEventHasOccurred() ) )
446 LOG(1,
"--TIME = " << p_simulation_time->
GetTime() <<
"\n");
449 UpdateCellPopulation();
453 bool at_sampling_timestep = (p_time->
GetTimeStepsElapsed()%this->mSamplingTimestepMultiple == 0);
462 std::map<CellPtr, c_vector<double, SPACE_DIM> > old_cell_locations;
463 if (mOutputCellVelocities && at_sampling_timestep)
466 cell_iter != mrCellPopulation.End();
469 old_cell_locations[*cell_iter] = mrCellPopulation.GetLocationOfCellCentre(*cell_iter);
474 UpdateCellLocationsAndTopology();
477 if (mOutputCellVelocities && at_sampling_timestep)
480 *mpCellVelocitiesFile << p_time->
GetTime() + mDt<<
"\t";
483 cell_iter != mrCellPopulation.End();
486 unsigned index = mrCellPopulation.GetLocationIndexUsingCell(*cell_iter);
487 const c_vector<double,SPACE_DIM>& position = mrCellPopulation.GetLocationOfCellCentre(*cell_iter);
489 c_vector<double, SPACE_DIM> velocity;
490 velocity = (position - old_cell_locations[*cell_iter])/mDt;
492 *mpCellVelocitiesFile << index <<
" ";
493 for (
unsigned i=0; i<SPACE_DIM; i++)
495 *mpCellVelocitiesFile << position[i] <<
" ";
498 for (
unsigned i=0; i<SPACE_DIM; i++)
500 *mpCellVelocitiesFile << velocity[i] <<
" ";
503 *mpCellVelocitiesFile <<
"\n";
507 mrCellPopulation.UpdateCellProcessLocation();
513 if (mpCellBasedPdeHandler != NULL)
516 mpCellBasedPdeHandler->SolvePdeAndWriteResultsToFile(this->mSamplingTimestepMultiple);
523 iter != mSimulationModifiers.end();
526 (*iter)->UpdateAtEndOfTimeStep(this->mrCellPopulation);
534 mrCellPopulation.WriteResultsToFiles(results_directory+
"/");
538 iter != mSimulationModifiers.end();
541 (*iter)->UpdateAtEndOfOutputTimeStep(this->mrCellPopulation);
547 LOG(1,
"--END TIME = " << p_simulation_time->
GetTime() <<
"\n");
554 UpdateCellPopulation();
557 if (mpCellBasedPdeHandler != NULL)
559 mpCellBasedPdeHandler->CloseResultsFiles();
565 iter != mSimulationModifiers.end();
568 (*iter)->UpdateAtEndOfSolve(this->mrCellPopulation);
574 mrCellPopulation.CloseWritersFiles();
576 if (mOutputDivisionLocations)
578 mpDivisionLocationFile->close();
580 if (mOutputCellVelocities)
582 mpCellVelocitiesFile->close();
587 *mpVizSetupFile <<
"Complete\n";
588 mpVizSetupFile->close();
595 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
601 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
606 unsigned deaths_this_step = DoCellRemoval();
607 mNumDeaths += deaths_this_step;
608 LOG(1,
"\tNum deaths = " << mNumDeaths <<
"\n");
613 unsigned births_this_step = DoCellBirth();
614 mNumBirths += births_this_step;
615 LOG(1,
"\tNum births = " << mNumBirths <<
"\n");
619 bool births_or_death_occurred = ((births_this_step>0) || (deaths_this_step>0));
623 if (mUpdateCellPopulation)
625 LOG(1,
"\tUpdating cell population...");
626 mrCellPopulation.Update(births_or_death_occurred);
629 else if (births_or_death_occurred)
631 EXCEPTION(
"CellPopulation has had births or deaths but mUpdateCellPopulation is set to false, please set it to true.");
636 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
639 return mOutputDivisionLocations;
642 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
645 mOutputDivisionLocations = outputDivisionLocations;
648 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
651 return mOutputCellVelocities;
654 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
657 mOutputCellVelocities = outputCellVelocities;
660 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
663 OutputFileHandler output_file_handler(this->mSimulationOutputDirectory +
"/",
false);
672 out_stream build_info_file = output_file_handler.
OpenOutputFile(
"build.info");
673 std::string build_info;
675 *build_info_file << build_info;
676 build_info_file->close();
679 out_stream parameter_file = output_file_handler.
OpenOutputFile(
"results.parameters");
682 std::string simulation_type = GetIdentifier();
684 *parameter_file <<
"<Chaste>\n";
685 *parameter_file <<
"\n\t<" << simulation_type <<
">\n";
686 OutputSimulationParameters(parameter_file);
687 *parameter_file <<
"\t</" << simulation_type <<
">\n";
688 *parameter_file <<
"\n";
691 mrCellPopulation.OutputCellPopulationInfo(parameter_file);
694 *parameter_file <<
"\n\t<CellKillers>\n";
696 iter != mCellKillers.end();
700 (*iter)->OutputCellKillerInfo(parameter_file);
702 *parameter_file <<
"\t</CellKillers>\n";
705 *parameter_file <<
"\n\t<SimulationModifiers>\n";
707 iter != mSimulationModifiers.end();
711 (*iter)->OutputSimulationModifierInfo(parameter_file);
713 *parameter_file <<
"\t</SimulationModifiers>\n";
716 OutputAdditionalSimulationSetup(parameter_file);
718 *parameter_file <<
"\n</Chaste>\n";
719 parameter_file->close();
723 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
726 if (mpCellBasedPdeHandler != NULL)
728 mpCellBasedPdeHandler->OutputParameters(rParamsFile);
731 *rParamsFile <<
"\t\t<Dt>" << mDt <<
"</Dt>\n";
732 *rParamsFile <<
"\t\t<EndTime>" << mEndTime <<
"</EndTime>\n";
733 *rParamsFile <<
"\t\t<SamplingTimestepMultiple>" << mSamplingTimestepMultiple <<
"</SamplingTimestepMultiple>\n";
734 *rParamsFile <<
"\t\t<OutputDivisionLocations>" << mOutputDivisionLocations <<
"</OutputDivisionLocations>\n";
735 *rParamsFile <<
"\t\t<OutputCellVelocities>" << mOutputCellVelocities <<
"</OutputCellVelocities>\n";
CellBasedPdeHandler< SPACE_DIM > * GetCellBasedPdeHandler()
virtual unsigned DoCellBirth()
virtual ~AbstractCellBasedSimulation()
virtual bool StoppingEventHasOccurred()
void AddCellKiller(boost::shared_ptr< AbstractCellKiller< SPACE_DIM > > pCellKiller)
bool IsEndTimeAndNumberOfTimeStepsSetUp() const
#define EXCEPTION(message)
static void BeginEvent(unsigned event)
static SimulationTime * Instance()
AbstractCellBasedSimulation(AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > &rCellPopulation, bool deleteCellPopulationInDestructor=false, bool initialiseCells=true)
bool GetOutputDivisionLocations()
void OutputSimulationSetup()
AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > & rGetCellPopulation()
unsigned GetTimeStepsElapsed() const
void SetOutputCellVelocities(bool outputCellVelocities)
std::string GetOutputDirectoryFullPath() const
void SetNoBirth(bool noBirth)
std::string GetOutputDirectory()
const double DOUBLE_UNSET
bool GetOutputCellVelocities()
void SetUpdateCellPopulationRule(bool updateCellPopulation)
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
void SetEndTime(double endTime)
bool GetUpdateCellPopulationRule()
void SetOutputDivisionLocations(bool outputDivisionLocations)
virtual void OutputSimulationParameters(out_stream &rParamsFile)=0
void RemoveAllCellKillers()
virtual void UpdateCellPopulation()
void SetOutputDirectory(std::string outputDirectory)
void SetEndTimeAndNumberOfTimeSteps(double endTime, unsigned totalTimeStepsInSimulation)
static RandomNumberGenerator * Instance()
std::vector< double > GetNodeLocation(const unsigned &rNodeIndex)
void AddSimulationModifier(boost::shared_ptr< AbstractCellBasedSimulationModifier< ELEMENT_DIM, SPACE_DIM > > pSimulationModifier)
static void GetBuildInfo(std::string &rInfo)
static void EndEvent(unsigned event)
void IncrementTimeOneStep()
static void WriteMachineInfoFile(std::string fileBaseName)
std::vector< boost::shared_ptr< AbstractCellBasedSimulationModifier< ELEMENT_DIM, SPACE_DIM > > > * GetSimulationModifiers()
void ResetEndTimeAndNumberOfTimeSteps(const double &rEndTime, const unsigned &rNumberOfTimeStepsInThisRun)
static void SetOutputDirectory(const std::string &rOutputDirectory)
void SetCellBasedPdeHandler(CellBasedPdeHandler< SPACE_DIM > *pCellBasedPdeHandler)
void SetSamplingTimestepMultiple(unsigned samplingTimestepMultiple)
AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > & mrCellPopulation