Commit 5c25f4bf authored by fmg005's avatar fmg005

Reimplemented realtime method :)

parent 1fdc5324
......@@ -4,28 +4,35 @@ using namespace CLA;
Realtime::Realtime(std::unique_ptr<PhysiologyEngine>& engine, CLA::LOGGER*
logger): m_engine(engine), m_logger(logger) {
m_cycles = 0;
}
void Realtime::AdvanceModelTime(double time) {
m_cycles = static_cast<uint>(time * N);
/*
synchronize simulation time to real clock
i.e., 1s of Simulation = 1s of real-time
*/
m_start = clock();
m_engine->AdvanceModelTime(1.0, TimeUnit::s);
m_end = clock();
for(int i = 0; i < m_cycles; i++) {
m_engine->AdvanceModelTime(time, TimeUnit::s);
}
}
/* start time for the next execution */
m_real_time = m_start + (m_next_start * time);
void Realtime::LoadConfig(const char* file_path) {
try {
m_cfg.readFile(file_path); // Read configration file
}
catch(const FileIOException &fioex) {
m_logger->error("I/O error while reading file.");
}
catch(const ParseException &pex) {
m_logger->error("Error parsing configuration file.");
}
N = m_cfg.lookup("realtime.time_steps");
/* check if simuation takes longer than expected */
if ((m_end - m_start) > m_real_time)
m_logger->error("Simulation too slow");
/* wait until start time of next execution is reached i.e., 1s of real-time */
while(true) {
m_delta = m_real_time - clock();
if (m_delta <= 0)
break;
m_delta_secs = long(((double)m_delta)/CLOCKS_PER_SEC);
std::this_thread::sleep_for(std::chrono::seconds(m_delta_secs));
}
/* track data */
m_engine->GetEngineTracker()->TrackData(m_engine->GetSimulationTime(TimeUnit::s));
}
Realtime::~Realtime() {
......
......@@ -62,7 +62,7 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg, c
pe->GetEngineTracker()->GetDataRequestManager().SetResultsFilename(prosim_results_dir+"PulseSimEngine_"+patient_name+".txt");
SEHemorrhage hemorrhageLeg;
double initialMAP = 75.0;
double initialMAP = 76.0;
double currentMAP;
CLA::Environment sim_env;
Global_LoadConfig(pe, cfg, filepath, &sim_env);
......@@ -82,14 +82,12 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg, c
HardwareSimulator prosim(&myserial, &logger, &sim_env);
prosim.LoadConfig(filepath);
CLA::Realtime rt(pe, &logger);
rt.LoadConfig(filepath);
bool STOP = false; // Execute stop hemmorrhage only once; without this it be executed for each timestep
bool DEVICES_START = false;
prosim.SetRemoteMode();
CLA::Realtime rt(pe, &logger);
prosim.SetRemoteMode();
// stop when the set time is reached
while (sim_env.time_index <= sim_env.simulation_timesteps) {
// event_hemorrage_start
......@@ -122,10 +120,8 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg, c
// track patient data
//pe->GetEngineTracker()->TrackData(pe->GetSimulationTime(TimeUnit::s));
//pe->AdvanceModelTime();
rt.AdvanceModelTime(1.0);
rt.AdvanceModelTime(1.0);//Advance Engine at 1s in real time
sim_env.time_index++;
pe->GetEngineTracker()->TrackData((double)sim_env.time_index);
}// End while looop
}// End Simulation function
......
#ifndef REALTIME_H
#define REALTIME_H
#ifndef REALTIME2_H
#define REALTIME2_H
#include <time.h>
#include <string>
#include <chrono>
#include <thread>
#include <libconfig.h++>
#include "CLA_Logger.h"
#include "PulsePhysiologyEngine.h"
......@@ -10,21 +13,22 @@
#include "properties/SEScalarTime.h"
using namespace libconfig;
using namespace std;
namespace CLA {
class Realtime {
private:
double N; //system clock: 1s->600 sim time steps or 12 sim secs
std::unique_ptr<PhysiologyEngine>& m_engine;
uint m_cycles;
Config m_cfg;
CLA::LOGGER* m_logger;
Config m_cfg;
clock_t m_real_time, real_start, m_delta, m_start, m_end;
clock_t m_next_start = CLOCKS_PER_SEC;
long m_delta_secs;
public:
Realtime(std::unique_ptr<PhysiologyEngine>&, CLA::LOGGER*);
~Realtime();
void AdvanceModelTime(double);
void LoadConfig(const char*);
};
}
......
# Configuration file
prosim = {
delay_5_sec = 5.0; # Prosim delay-->arbitrary value
}
realtime = {
time_steps = 12.0; # How many simulation seconds result into 1s of real time
}
simulation = {
time = {
run = 120.0; # Time -> Seconds # How long should simulation run
run = 60.0; # Time -> Seconds # How long should simulation run
injury_start = 5.0; # When should injury be introduced to patient
injury_stop = 700.0; # When should injury be stopped
}
};
}
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