Commit f4e725b5 authored by Ulrich Kemloh's avatar Ulrich Kemloh

Refactoring the Simulation class to allow more control e.g. Pausing and

resuming. Usefull when JuPedSim is controlled from an external program.
parent 782e1904
......@@ -273,6 +273,15 @@ bool Simulation::InitArgs(const ArgumentParser& args)
return true;
}
int Simulation::RunStandardSimulation(double maxSimTime)
{
RunHeader(_nPeds);
double t=RunBody(maxSimTime);
RunFooter();
return (int)t;
}
//TODO: use RunHeader, RunBody and RunFooter
int Simulation::RunSimulation(double maxSimTime)
{
int frameNr = 1; // Frame Number
......@@ -516,12 +525,111 @@ void Simulation::PrintStatistics()
Log->Write("\n");
}
void Simulation::RunHeader(long nPed)
{
// writing the header
if(nPed==-1) nPed=_nPeds;
_iod->WriteHeader(nPed, _fps, _building.get(), _seed);
_iod->WriteGeometry(_building.get());
_iod->WriteFrame(0, _building.get());
//first initialisation needed by the linked-cells
UpdateRoutesAndLocations();
ProcessAgentsQueue();
}
const AgentsSourcesManager& Simulation::GetAgentSrcManager()
int Simulation::RunBody(double maxSimTime)
{
return _agentSrcManager;
//needed to control the execution time PART 1
//in the case you want to run in no faster than realtime
//time_t starttime, endtime;
//time(&starttime);
//frame number. This function can be called many times,
static int frameNr = 1; // Frame Number
int writeInterval = (int) ((1. / _fps) / _deltaT + 0.5);
writeInterval = (writeInterval <= 0) ? 1 : writeInterval; // mustn't be <= 0
//take the current time from the pedestrian
double t=Pedestrian::GetGlobalTime();
//process the queue for incoming pedestrians
//important since the number of peds is used
//to break the main simulation loop
ProcessAgentsQueue();
_nPeds = _building->GetAllPedestrians().size();
// main program loop
while ( (_nPeds || !_agentSrcManager.IsCompleted() ) && t < maxSimTime)
{
t = 0 + (frameNr - 1) * _deltaT;
//process the queue for incoming pedestrians
ProcessAgentsQueue();
// update the positions
_operationalModel->ComputeNextTimeStep(t, _deltaT, _building.get());
//update the routes and locations
UpdateRoutesAndLocations();
//update the events
//_em->Update_Events(t);
_em->ProcessEvent();
//other updates
//someone might have left the building
_nPeds = _building->GetAllPedestrians().size();
//update the linked cells
_building->UpdateGrid();
// update the global time
Pedestrian::SetGlobalTime(t);
// write the trajectories
if (0 == frameNr % writeInterval) {
_iod->WriteFrame(frameNr / writeInterval, _building.get());
}
// needed to control the execution time PART 2
// time(&endtime);
// double timeToWait=t-difftime(endtime, starttime);
// clock_t goal = timeToWait*1000 + clock();
// while (goal > clock());
++frameNr;
}
return (int) t;
}
void Simulation::RunFooter()
{
// writing the footer
_iod->WriteFooter();
//temporary work around since the total number of frame is only available at the end of the simulation.
if (_argsParser.GetFileFormat() == FORMAT_XML_BIN)
{
delete _iod;
_iod = NULL;
//reopen the file and write the missing information
// char tmp[CLENGTH];
// int f= frameNr / writeInterval ;
// sprintf(tmp,"<frameCount>%07d</frameCount>",f);
// string frameCount (tmp);
//char replace[CLENGTH];
// open the file and replace the 8th line
//sprintf(replace, "sed -i '9s/.*/ %d /' %s", frameNr / writeInterval,
// _argsParser.GetTrajectoriesFile().c_str());
//int result = system(replace);
//Log->Write("INFO:\t Updating the framenumber exits with code [%d]", result);
}
}
void Simulation::ProcessAgentsQueue()
{
......@@ -539,3 +647,8 @@ void Simulation::ProcessAgentsQueue()
_hybridSimManager->ProcessOutgoingAgent();
}
}
const AgentsSourcesManager& Simulation::GetAgentSrcManager()
{
return _agentSrcManager;
}
......@@ -75,12 +75,13 @@ private:
std::unique_ptr<Building> _building;
/// Force model to use
std::shared_ptr<OperationalModel> _operationalModel;
/// differential equation solver
/// Manage all route choices algorithms
std::shared_ptr<RoutingEngine> _routingEngine;
/// differential equation solver
ODESolver* _solver;
/// writing the trajectories to file
IODispatcher* _iod;
///new: EventManager
/// EventManager
EventManager* _em;
/// argument parser
ArgumentParser _argsParser;
......@@ -91,7 +92,6 @@ private:
std::shared_ptr<HybridSimulationManager>_hybridSimManager=nullptr;
public:
/**
* Constructor
*/
......@@ -119,24 +119,33 @@ public:
int RunSimulation(double maxSimTime);
/**
* Updathe route of the pedestrians and reassign rooms, in the case a room change happens
* Update the route of the pedestrians and reassign rooms, in the case a room change happens
*/
void UpdateRoutesAndLocations();
/**
* Set the ProfilingFlag
* Perform some initialisation for the simulation.
* such as writing the headers for the trajectories.
* @param the maximal number of pedestrian
*/
void SetProfileFlag(bool flag);
void RunHeader(long nPed=-1);
/**
* Get the ProfileFlag
* Run the main part of the simulation
*/
bool GetProfileFlag();
int RunBody(double maxSimTime);
/**
* Get the HPCFlag
* Perform some finalization like writing the
* footers for the trajectories.
*/
void RunFooter();
/**
* Run a standard simulation
* @return the total simulated/evacuation time
*/
int GetHPCFlag();
int RunStandardSimulation(double maxSimTime);
/**
* print some statistics about the simulation
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment