Commit 8b8f7af6 authored by fmg005's avatar fmg005

Ability to log command duration

parent 552894c3
......@@ -2,30 +2,50 @@
CLA::LOGGER::LOGGER() {
m_logstream.open(prosim_logs_dir+"prosim_"+current_time(false)+".log"); // timestamped logs
m_logstream[0].open(prosim_logs_dir+"prosim_"+current_time(false)+".log"); // timestamped logs
m_logstream[1].open(prosim_logs_dir+"duration_"+current_time(false)+".log"); // timestamped logs
m_logstream[1] <<"start time,"<<"end time,"<<"duration(s)\n";
}
CLA::LOGGER::LOGGER(const string& filename): m_filename(move(filename)) {
m_logstream.open(prosim_logs_dir+m_filename, ios::out|ios::binary);
m_logstream[0].open(prosim_logs_dir+m_filename, ios::out|ios::binary);
m_logstream[1].open(prosim_logs_dir+"duration_"+m_filename, ios::out|ios::binary);
m_logstream[1] <<"start time,"<<"end time,"<<"duration(s)\n";
}
string CLA::LOGGER::current_time(bool display_tz){
time_t now = time(0);
struct tm* timeinfo;
char buffer[50];
string CLA::LOGGER::current_time(bool display_timezone) {
time_t now = time(0);
struct tm* timeinfo;
char buffer[50];
timeinfo = localtime(&now);
timeinfo = localtime(&now);
if (display_timezone)
strftime(buffer, 50, "%Y-%M-%d.%X-%Z", timeinfo);
else
strftime(buffer, 50, "%Y-%M-%d.%X", timeinfo);
if (display_tz)
strftime(buffer, 50, "%Y-%M-%d.%X-%Z", timeinfo);
else
strftime(buffer, 50, "%Y-%M-%d.%X", timeinfo);
return buffer;
}
string CLA::LOGGER::format_chrono_time(time_t t) {
char buff[20];
strftime(buff, 20, "%X", localtime(&t));
return buff;
}
string CLA::LOGGER::format_chrono_time(system_clock::time_point t) {
char buff[20];
auto in_time = system_clock::to_time_t(t);
strftime(buff, 20, "%X", localtime(&in_time));
return buff;
}
return buffer;
void CLA::LOGGER::log_duration(system_clock::time_point start_time, system_clock::time_point end_time, time_t duration_time) {
m_logstream[1]<<format_chrono_time(start_time)<<","<<format_chrono_time(end_time)<<","<<static_cast<float>(duration_time)/1000<<"\n";
}
string CLA::LOGGER::set_level(LEVEL level) {
switch(level) {
switch(level) {
case DEBUG:
level_name = "DEBUG";
break;
......@@ -41,10 +61,13 @@ string CLA::LOGGER::set_level(LEVEL level) {
case FATAL:
level_name = "FATAL";
break;
}
return level_name;
}
return level_name;
}
CLA::LOGGER::~LOGGER() {
m_logstream.close();
for (int i = 0; i < 2; i++) {
m_logstream[i].close();
}
ss.clear();
}
#include "HardwareSimulator.h"
HardwareSimulator::HardwareSimulator(serial::Serial*
my_serial, CLA::LOGGER* logger):m_serial(my_serial), m_logger(logger) {
HardwareSimulator::HardwareSimulator(serial::Serial* my_serial, CLA::LOGGER*
logger):m_serial(my_serial), m_logger(logger) {
}
HardwareSimulator::HardwareSimulator(serial::Serial*
my_serial, CLA::LOGGER* logger, CLA::Environment* env):m_serial(my_serial), m_logger(logger), m_env(env) {
HardwareSimulator::HardwareSimulator(serial::Serial* my_serial, CLA::LOGGER*
logger, CLA::Environment* env):m_serial(my_serial), m_logger(logger), m_env(env) {
}
......@@ -18,27 +18,29 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
*/
m_start = system_clock::now();
engine->AdvanceModelTime(advance_time, TimeUnit::s);
m_end = system_clock::now();
//m_end = system_clock::now();
m_command_start = system_clock::now();
// m_command_start = system_clock::now();
systolic_pressure = engine->GetCardiovascularSystem()->GetSystolicArterialPressure(PressureUnit::mmHg);
diastolic_pressure = engine->GetCardiovascularSystem()->GetDiastolicArterialPressure(PressureUnit::mmHg);
heart_rate = engine->GetCardiovascularSystem()->GetHeartRate(FrequencyUnit::Per_min);
/* send patient data to monitor */
SetIBP(1, systolic_pressure, diastolic_pressure);
SetHeartRate(heart_rate);
m_command_end = system_clock::now();
m_end = system_clock::now();
/* start time for the next execution */
duration<double> m_next_start(real_time); // seconds
m_real_time = system_clock::to_time_t(m_start) + m_next_start.count();
m_advance_duration_ms = duration_cast<milliseconds>(m_end - m_start).count();
m_command_duration_ms = duration_cast<milliseconds>(m_command_end - m_command_start).count();
m_duration_ms = duration_cast<milliseconds>(m_end - m_start).count();
//m_command_duration_ms = duration_cast<milliseconds>(m_command_end - m_command_start).count();
m_real_time_duration_ms = duration_cast<milliseconds>(m_next_start).count();
m_logger->log_duration(m_start, m_end, m_duration_ms);
/* checking if simulation will take longer than allotted 'time' */
if (m_advance_duration_ms + m_command_duration_ms > m_real_time_duration_ms)
if (m_duration_ms > m_real_time_duration_ms)
m_logger->error("Simulation too slow");
/* wait until start time of next execution is reached i.e., time:s of real-time */
......
......@@ -41,7 +41,7 @@ int main() {
string f_path = prosim_config_dir+"scenario.cfg";
const char* filepath = f_path.c_str();
Simulation("Cynthia", cf, filepath);
Simulation("Hassan", cf, filepath);
}
void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg, const char* filepath )
......@@ -66,6 +66,8 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg, c
SEHemorrhage hemorrhageLeg;
double initialMAP = 70.0;
double currentMAP;
bool STOP_HEMORRHAGE = false;
bool REAL_TIME_START = false;
CLA::Environment sim_env;
Global_LoadConfig(pe, cfg, filepath, &sim_env);
......@@ -84,35 +86,37 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg, c
HardwareSimulator prosim(&myserial, &logger, &sim_env);
prosim.LoadConfig(filepath);
bool STOP = false; // Execute stop hemmorrhage only once; without this it be executed for each timestep
bool DEVICES_START = false;
prosim.SetRemoteMode();
// stop when the set time is reached
while (sim_env.time_index <= sim_env.simulation_timesteps) {
// event_hemorrage_start
if(sim_env.time_index == sim_env.simulation_injury_start_timestep) {
cout<< "The time_index "<<sim_env.time_index;
cout<< "The time_index "<<sim_env.time_index;
// Hemorrhage Starts - instantiate a hemorrhage action and have the engine process it
cout<<"\nHemorrhage Started at: "<<pe->GetSimulationTime(TimeUnit::s)<<endl;
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
//the rate of hemorrhage
hemorrhageLeg.GetRate().SetValue(350,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
STOP = true;
STOP_HEMORRHAGE = true;
}
currentMAP = pe->GetCardiovascularSystem()->GetMeanArterialPressure(PressureUnit::mmHg);
if( currentMAP <= initialMAP && STOP) {
if( currentMAP <= initialMAP && STOP_HEMORRHAGE) {
cout<<"\nHemorrhage Stoppd at: "<<pe->GetSimulationTime(TimeUnit::s)<<endl;
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
hemorrhageLeg.GetRate().SetValue(0,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
STOP = false;
STOP_HEMORRHAGE = false;
REAL_TIME_START = true; /* Begin real time simulation */
}
/* We both track data and advance engine inside the update method */
/* We both track data and advance engine inside the update method */
prosim.update(pe);
/*
if (REAL_TIME_START) {
prosim.update(pe);
}*/
sim_env.time_index++;
}// End while looop
}// End Simulation function
......
......@@ -3,82 +3,74 @@
#define CLALOGGER_H
#include <string>
#include <time.h> /* time_t, time, struct tm, localtime, strftime*/
#include <ctime> /* time_t, time, struct tm, localtime, strftime*/
#include <fstream>
#include <utility>
#include <sstream>
#include <iostream>
#include <chrono>
#include "configure_prosim_paths.h"
using namespace std;
using namespace std::chrono;
namespace CLA {
class LOGGER {
private:
string level_name;
const string m_filename;
ofstream m_logstream;
ostringstream ss;
const string m_filename;
ofstream m_logstream[2];
ostringstream ss;
enum LEVEL {DEBUG, INFO, WARNING, ERROR, FATAL};
string current_time(bool);
string current_time(bool);
string format_chrono_time(time_t);
string format_chrono_time(system_clock::time_point);
string set_level(LEVEL);
public:
LOGGER();
LOGGER();
LOGGER(const string&);
void log_duration(system_clock::time_point, system_clock::time_point, time_t);
~LOGGER();
template <typename T>
void debug(const T& t) {
ss << t;
m_logstream<<current_time(true)<<","<<set_level(LOGGER::DEBUG)<<","+ss.str()+"\n";
ss.str("");
ss.clear();
}
m_logstream[0]<<current_time(true)<<","<<set_level(LOGGER::DEBUG)<<","+ss.str()+"\n";
ss.str("");
}
template <typename T>
void info(const T& t) {
ss << t;
m_logstream<<current_time(true)<<","<<set_level(LOGGER::INFO)<<","+ss.str()+"\n";
cout<<current_time(false)<<" ("<<set_level(LOGGER::INFO)<<") "+ss.str()<<endl;
ss.str("");
ss.clear();
m_logstream[0]<<current_time(true)<<","<<set_level(LOGGER::INFO)<<","+ss.str()+"\n";
cout<<current_time(false)<<" ("<<set_level(LOGGER::INFO)<<") "+ss.str()<<endl;
ss.str("");
}
template <typename T>
void warning(const T& t) {
ss << t;
m_logstream<<current_time(true)<<","<<set_level(LOGGER::WARNING)<<","+ss.str()+"\n";
m_logstream[0]<<current_time(true)<<","<<set_level(LOGGER::WARNING)<<","+ss.str()+"\n";
cout<<current_time(false)<<" ("<<set_level(LOGGER::WARNING)<<") "+ss.str()<<endl;
ss.str("");
ss.clear();
cout<<current_time(false)<<" ("<<set_level(LOGGER::WARNING)<<") "+ss.str()<<endl;
ss.str("");
}
template <typename T>
void error(const T& t) {
ss << t;
m_logstream<<current_time(true)<<","<<set_level(LOGGER::ERROR)<<","+ss.str()+"\n";
ss.str("");
ss.clear();
m_logstream[0]<<current_time(true)<<","<<set_level(LOGGER::ERROR)<<","+ss.str()+"\n";
ss.str("");
}
template <typename T>
void fatal(const T& t) {
ss << t;
m_logstream<<current_time(true)<<","<<set_level(LOGGER::FATAL)<<","+ss.str()+"\n";
ss.str("");
ss.clear();
m_logstream[0]<<current_time(true)<<","<<set_level(LOGGER::FATAL)<<","+ss.str()+"\n";
ss.str("");
}
};
}
#endif
......@@ -43,12 +43,12 @@ private:
/* chrono */
system_clock::time_point m_start;
system_clock::time_point m_end;
system_clock::time_point m_command_start;
system_clock::time_point m_command_end;
//system_clock::time_point m_command_start;
//system_clock::time_point m_command_end;
time_t m_delta;
time_t m_real_time;
time_t m_advance_duration_ms;
time_t m_command_duration_ms;
time_t m_duration_ms;
//time_t m_command_duration_ms;
time_t m_real_time_duration_ms;
public:
......
# Configuration file
pulse = {
advance_time = 1.0; # Simulation engine advance time -> seconds
advance_time = 2.0; # Simulation engine advance time -> seconds
}
prosim = {
real_time = 1.0; # Real time -> seconds
real_time = 2.0; # Real time -> seconds
}
simulation = {
time = {
run = 180.0; # Time -> Seconds How long should simulation run
injury_start = 60.0; # When should injury be introduced to patient
run = 30.0; # Time -> Seconds How long should simulation run
injury_start = 10.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