41 #include "AbstractCellBasedSimulation.hpp"
42 #include "CellBasedEventHandler.hpp"
43 #include "LogFile.hpp"
44 #include "ExecutableSupport.hpp"
45 #include "AbstractPdeModifier.hpp"
47 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
49 bool deleteCellPopulationInDestructor,
53 mrCellPopulation(rCellPopulation),
54 mDeleteCellPopulationInDestructor(deleteCellPopulationInDestructor),
55 mInitialiseCells(initialiseCells),
57 mUpdateCellPopulation(true),
59 mSimulationOutputDirectory(mOutputDirectory),
62 mOutputDivisionLocations(false),
63 mOutputCellVelocities(false),
64 mSamplingTimestepMultiple(1)
81 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
84 if (mDeleteCellPopulationInDestructor)
86 delete &mrCellPopulation;
90 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
98 unsigned num_births_this_step = 0;
102 cell_iter != mrCellPopulation.End();
106 double cell_age = cell_iter->GetAge();
109 if (cell_iter->ReadyToDivide())
112 if (mrCellPopulation.IsRoomToDivide(*cell_iter))
115 unsigned parent_cell_id = cell_iter->GetCellId();
118 CellPtr p_new_cell = cell_iter->Divide();
138 if (mOutputDivisionLocations)
140 c_vector<double, SPACE_DIM> cell_location = mrCellPopulation.GetLocationOfCellCentre(*cell_iter);
143 for (
unsigned i=0; i<SPACE_DIM; i++)
145 *mpDivisionLocationFile << cell_location[i] <<
"\t";
147 *mpDivisionLocationFile <<
"\t" << cell_age <<
"\t" << parent_cell_id <<
"\t" << cell_iter->GetCellId() <<
"\t" << p_new_cell->GetCellId() <<
"\n";
151 mrCellPopulation.AddCell(p_new_cell, *cell_iter);
154 num_births_this_step++;
159 return num_births_this_step;
162 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
165 unsigned num_deaths_this_step = 0;
172 killer_iter != mCellKillers.end();
175 (*killer_iter)->CheckAndLabelCellsForApoptosisOrDeath();
178 num_deaths_this_step += mrCellPopulation.RemoveDeadCells();
180 return num_deaths_this_step;
183 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
190 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
196 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
202 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
208 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
215 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
218 mOutputDirectory = outputDirectory;
219 mSimulationOutputDirectory = mOutputDirectory;
222 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
225 return mOutputDirectory;
228 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
231 assert(samplingTimestepMultiple > 0);
232 mSamplingTimestepMultiple = samplingTimestepMultiple;
235 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
238 return mrCellPopulation;
241 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
244 return mrCellPopulation;
247 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
250 mUpdateCellPopulation = updateCellPopulation;
253 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
256 return mUpdateCellPopulation;
259 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
265 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
268 mCellKillers.push_back(pCellKiller);
271 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
274 mCellKillers.clear();
277 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
280 mSimulationModifiers.push_back(pSimulationModifier);
283 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
286 return &mSimulationModifiers;
289 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
292 std::vector<double> location;
293 for (
unsigned i=0; i<SPACE_DIM; i++)
295 location.push_back(mrCellPopulation.GetNode(rNodeIndex)->rGetLocation()[i]);
300 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
308 double current_time = p_simulation_time->
GetTime();
314 EXCEPTION(
"SetEndTime has not yet been called.");
324 unsigned num_time_steps = (
unsigned) ((mEndTime-current_time)/mDt+0.5);
325 if (current_time > 0)
333 EXCEPTION(
"End time and number of timesteps already setup. You should not use SimulationTime::SetEndTimeAndNumberOfTimeSteps in cell-based tests.");
341 if (mOutputDirectory ==
"")
346 double time_now = p_simulation_time->
GetTime();
347 std::ostringstream time_string;
348 time_string << time_now;
350 std::string results_directory = mOutputDirectory +
"/results_from_time_" + time_string.str();
351 mSimulationOutputDirectory = results_directory;
358 mrCellPopulation.OpenWritersFiles(output_file_handler);
360 if (mOutputDivisionLocations)
362 mpDivisionLocationFile = output_file_handler.
OpenOutputFile(
"divisions.dat");
364 if (mOutputCellVelocities)
366 OutputFileHandler output_file_handler2(this->mSimulationOutputDirectory+
"/",
false);
367 mpCellVelocitiesFile = output_file_handler2.
OpenOutputFile(
"cellvelocities.dat");
372 mpVizSetupFile = output_file_handler.
OpenOutputFile(
"results.vizsetup");
375 iter != mSimulationModifiers.end();
380 *this->mpVizSetupFile <<
"PDE \n";
385 this->mrCellPopulation.SimulationSetupHook(
this);
391 iter != mSimulationModifiers.end();
394 (*iter)->SetupSolve(this->mrCellPopulation,this->mSimulationOutputDirectory);
401 LOG(1,
"Setting up cells...");
403 cell_iter != mrCellPopulation.End();
410 cell_iter->ReadyToDivide();
415 WriteVisualizerSetupFile();
419 *mpVizSetupFile << std::flush;
422 mrCellPopulation.WriteResultsToFiles(results_directory+
"/");
424 OutputSimulationSetup();
428 while (!( p_simulation_time->
IsFinished() || StoppingEventHasOccurred() ) )
430 LOG(1,
"--TIME = " << p_simulation_time->
GetTime() <<
"\n");
433 UpdateCellPopulation();
437 bool at_sampling_timestep = (p_time->
GetTimeStepsElapsed()%this->mSamplingTimestepMultiple == 0);
446 std::map<CellPtr, c_vector<double, SPACE_DIM> > old_cell_locations;
447 if (mOutputCellVelocities && at_sampling_timestep)
450 cell_iter != mrCellPopulation.End();
453 old_cell_locations[*cell_iter] = mrCellPopulation.GetLocationOfCellCentre(*cell_iter);
458 UpdateCellLocationsAndTopology();
461 if (mOutputCellVelocities && at_sampling_timestep)
464 *mpCellVelocitiesFile << p_time->
GetTime() + mDt<<
"\t";
467 cell_iter != mrCellPopulation.End();
470 unsigned index = mrCellPopulation.GetLocationIndexUsingCell(*cell_iter);
471 const c_vector<double,SPACE_DIM>& position = mrCellPopulation.GetLocationOfCellCentre(*cell_iter);
473 c_vector<double, SPACE_DIM> velocity;
474 velocity = (position - old_cell_locations[*cell_iter])/mDt;
476 *mpCellVelocitiesFile << index <<
" ";
477 for (
unsigned i=0; i<SPACE_DIM; i++)
479 *mpCellVelocitiesFile << position[i] <<
" ";
482 for (
unsigned i=0; i<SPACE_DIM; i++)
484 *mpCellVelocitiesFile << velocity[i] <<
" ";
487 *mpCellVelocitiesFile <<
"\n";
491 mrCellPopulation.UpdateCellProcessLocation();
499 iter != mSimulationModifiers.end();
502 (*iter)->UpdateAtEndOfTimeStep(this->mrCellPopulation);
510 mrCellPopulation.WriteResultsToFiles(results_directory+
"/");
514 iter != mSimulationModifiers.end();
517 (*iter)->UpdateAtEndOfOutputTimeStep(this->mrCellPopulation);
523 LOG(1,
"--END TIME = " << p_simulation_time->
GetTime() <<
"\n");
530 UpdateCellPopulation();
535 iter != mSimulationModifiers.end();
538 (*iter)->UpdateAtEndOfSolve(this->mrCellPopulation);
544 mrCellPopulation.CloseWritersFiles();
546 if (mOutputDivisionLocations)
548 mpDivisionLocationFile->close();
550 if (mOutputCellVelocities)
552 mpCellVelocitiesFile->close();
557 *mpVizSetupFile <<
"Complete\n";
558 mpVizSetupFile->close();
565 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
571 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
576 unsigned deaths_this_step = DoCellRemoval();
577 mNumDeaths += deaths_this_step;
578 LOG(1,
"\tNum deaths = " << mNumDeaths <<
"\n");
583 unsigned births_this_step = DoCellBirth();
584 mNumBirths += births_this_step;
585 LOG(1,
"\tNum births = " << mNumBirths <<
"\n");
589 bool births_or_death_occurred = ((births_this_step>0) || (deaths_this_step>0));
593 if (mUpdateCellPopulation)
595 LOG(1,
"\tUpdating cell population...");
596 mrCellPopulation.Update(births_or_death_occurred);
599 else if (births_or_death_occurred)
601 EXCEPTION(
"CellPopulation has had births or deaths but mUpdateCellPopulation is set to false, please set it to true.");
606 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
609 return mOutputDivisionLocations;
612 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
615 mOutputDivisionLocations = outputDivisionLocations;
618 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
621 return mOutputCellVelocities;
624 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
627 mOutputCellVelocities = outputCellVelocities;
630 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
633 OutputFileHandler output_file_handler(this->mSimulationOutputDirectory +
"/",
false);
642 out_stream build_info_file = output_file_handler.
OpenOutputFile(
"build.info");
643 std::string build_info;
645 *build_info_file << build_info;
646 build_info_file->close();
649 out_stream parameter_file = output_file_handler.
OpenOutputFile(
"results.parameters");
652 std::string simulation_type = GetIdentifier();
654 *parameter_file <<
"<Chaste>\n";
655 *parameter_file <<
"\n\t<" << simulation_type <<
">\n";
656 OutputSimulationParameters(parameter_file);
657 *parameter_file <<
"\t</" << simulation_type <<
">\n";
658 *parameter_file <<
"\n";
661 mrCellPopulation.OutputCellPopulationInfo(parameter_file);
664 *parameter_file <<
"\n\t<CellKillers>\n";
666 iter != mCellKillers.end();
670 (*iter)->OutputCellKillerInfo(parameter_file);
672 *parameter_file <<
"\t</CellKillers>\n";
675 *parameter_file <<
"\n\t<SimulationModifiers>\n";
677 iter != mSimulationModifiers.end();
681 (*iter)->OutputSimulationModifierInfo(parameter_file);
683 *parameter_file <<
"\t</SimulationModifiers>\n";
686 OutputAdditionalSimulationSetup(parameter_file);
688 *parameter_file <<
"\n</Chaste>\n";
689 parameter_file->close();
693 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
696 *rParamsFile <<
"\t\t<Dt>" << mDt <<
"</Dt>\n";
697 *rParamsFile <<
"\t\t<EndTime>" << mEndTime <<
"</EndTime>\n";
698 *rParamsFile <<
"\t\t<SamplingTimestepMultiple>" << mSamplingTimestepMultiple <<
"</SamplingTimestepMultiple>\n";
699 *rParamsFile <<
"\t\t<OutputDivisionLocations>" << mOutputDivisionLocations <<
"</OutputDivisionLocations>\n";
700 *rParamsFile <<
"\t\t<OutputCellVelocities>" << mOutputCellVelocities <<
"</OutputCellVelocities>\n";
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)
virtual double GetDefaultTimeStep()=0
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 SetSamplingTimestepMultiple(unsigned samplingTimestepMultiple)
AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > & mrCellPopulation