Commit c223f5d6 authored by fmg005's avatar fmg005
Browse files

cleaned up simEngine files

parent c0a44c9f
......@@ -25,22 +25,22 @@ configure_file(
)
# set your simulation source directory
set(SIM_SRC_PATH simsrc/mcps2018/algo1)
set(SIM_SRC_DIR simsrc/mcps2018/algo1)
# *** include your source files
# list each file individually
set(MY_SOURCE_FILES
set(CLASIM_SOURCE_FILES
${SIM_SRC_PATH}/SimEngine.cpp
${SIM_SRC_PATH}/Monitor.cpp
${SIM_SRC_PATH}/Controller.cpp
${SIM_SRC_PATH}/Pump.cpp
${SIM_SRC_PATH}/SimulationLogger.cpp
${SIM_SRC_PATH}/MedicalDevice.cpp
${SIM_SRC_DIR}/SimEngine.cpp
${SIM_SRC_DIR}/Monitor.cpp
${SIM_SRC_DIR}/Controller.cpp
${SIM_SRC_DIR}/Pump.cpp
${SIM_SRC_DIR}/SimulationLogger.cpp
${SIM_SRC_DIR}/MedicalDevice.cpp
)
add_executable(clasim ${MY_SOURCE_FILES})
add_executable(clasim ${CLASIM_SOURCE_FILES})
#include directories
target_include_directories(clasim PRIVATE ${Pulse_INCLUDE_DIRS})
......
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <map>
#include <libconfig.h>
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
#include "Pump.h"
#include "Monitor.h"
#include "Controller.h"
......@@ -15,296 +7,258 @@
#include "SimEngine.h"
// Include the various types you will be using in your code
#include "scenario/SEDataRequestManager.h"
#include "patient/actions/SEHemorrhage.h"
#include "patient/actions/SESubstanceCompoundInfusion.h"
#include "system/physiology/SEBloodChemistrySystem.h"
#include "system/physiology/SECardiovascularSystem.h"
#include "system/physiology/SEEnergySystem.h"
#include "system/physiology/SERespiratorySystem.h"
#include "substance/SESubstanceManager.h"
#include "substance/SESubstanceCompound.h"
#include "properties/SEScalar0To1.h"
#include "properties/SEScalarFrequency.h"
#include "properties/SEScalarMass.h"
#include "properties/SEScalarMassPerVolume.h"
#include "properties/SEScalarPressure.h"
#include "properties/SEScalarTemperature.h"
#include "properties/SEScalarTime.h"
#include "properties/SEScalarVolume.h"
#include "properties/SEScalarVolumePerTime.h"
#include "engine/SEEngineTracker.h"
#include "compartment/SECompartmentManager.h"
//--------------------------------------------------------------------------------------------------
/// \brief
/// Usage for applying a Hemorrhage insult to the patient
///
/// \details
/// Refer to the SEHemorrhage class
/// Refer to the SESubstanceManager class
/// Refer to the SESubstanceIVFluids class for applying an IV to the patient
//--------------------------------------------------------------------------------------------------
using namespace libconfig;
int main(int argc, char * argv[]){
clock_t t1, t2, sim_run_time;
vector<string> patients_list;
auto cf = std::make_shared<Config>();
float sim_time_secs, sim_time_mins;
string f_path = clasim_config_dir+"scenario.cfg";
const char* filepath = f_path.c_str();
try
{
cf->readFile(filepath);
}
catch(const FileIOException &fioex) {
std::cerr << "I/O error while reading file: " << filepath << std::endl;
//return(EXIT_FAILURE);
}
catch(const ParseException &pex) {
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
<< " - " << pex.getError() << std::endl;
//return(EXIT_FAILURE);
}
patients_list = get_global_patients(cf);
for(auto patient: patients_list) {
// start timer
t1 = clock();
//Simulation input : Name of patient
Simulation(patient, cf);
//stop timer
t2 = clock();
sim_run_time = t2 - t1;
sim_time_secs = ((float)sim_run_time)/CLOCKS_PER_SEC;
sim_time_mins = sim_time_secs/60.0;
cout << "\nThe Simulation for " << patient;
cout << " took "<< sim_time_secs <<" seconds ";
cout <<"or " << sim_time_mins <<" minutes"<<endl;
}
}
void Simulation(const string patient_name, const std::shared_ptr<Config>& cf)
{
// Create the engine and load the patient
std::unique_ptr<PhysiologyEngine> pe =
CreatePulseEngine(clasim_results_dir+"SimulationEngine_"+patient_name+".log");
pe->GetLogger()->Info("CMD Simulation");
//if (!pe->LoadStateFile("./states/StandardMale@0s.pba"))
if (!pe->LoadStateFile(pulse_state_dir+patient_name+"@0s.pba"))
{
pe->GetLogger()->Error("Could not load state, check the error");
return;
}
int main() {
clock_t t1, t2, sim_run_time;
vector<string> patients_list;
auto cf = std::make_shared<Config>();// LibConfig instance
float sim_time_secs, sim_time_mins;
string f_path = clasim_config_dir+"scenario.cfg";
const char* filepath = f_path.c_str();
// Create data requests for each value that should be written to the output log as the engine is executing
// Physiology System Names are defined on the System Objects
// defined in the Physiology.xsd file
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("HeartRate", FrequencyUnit::Per_min);
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("BloodVolume", VolumeUnit::mL);
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("MeanArterialPressure", PressureUnit::mmHg);
pe->GetEngineTracker()->GetDataRequestManager().SetResultsFilename(clasim_results_dir+"PulseSimEngine_"+patient_name+".txt");
try
{
cf->readFile(filepath);
}
//instatiate Hemorrhage Action
SEHemorrhage hemorrhageLeg;
const double initialMAP = 70.0;
double currentMAP;
catch(const FileIOException &fioex) {
std::cerr << "I/O error while reading file: "<< filepath <<std::endl;
}
//instatiate enviroment object
CMD::Environment sim_env;
PhysiologyData data;
catch(const ParseException &pex) {
std::cerr << "Parse error at " << pex.getFile() << ":"<< pex.getLine()
<< " - " << pex.getError() << std::endl;
}
// load simulation environment
Global_LoadConfig(pe, cf, &sim_env);
patients_list = get_global_patients(cf);
// List of pumps
map<string, Pump*> pumps;
for(auto patient: patients_list) {
// start timer
t1 = clock();
//Iintialize Pump
Pump pump_saline(&sim_env, &data, "Saline");
pump_saline.LoadConfig(pe, cf);
// Simulation input : Name of patient, Config object
Simulation(patient, cf);
Pump pump_bpdrug(&sim_env, &data, "Norepinephrine");
pump_bpdrug.LoadConfig(pe, cf);
// stop timer
t2 = clock();
pumps.insert(make_pair("saline", &pump_saline));
pumps.insert(make_pair("bpdrug", &pump_bpdrug));
sim_run_time = t2 - t1;
sim_time_secs = ((double)sim_run_time)/CLOCKS_PER_SEC;
sim_time_mins = sim_time_secs/60.0;
//Initialize Controller
Controller controller(&sim_env, &data, pumps);
controller.LoadConfig(pe, cf);
cout << "\nThe Simulation for " << patient;
cout << " took "<< sim_time_secs <<" seconds ";
cout <<"or " << sim_time_mins <<" minutes"<<endl;
}
//Initialize Monitor
Monitor monitor(&sim_env, &data);
// Call this member function before 'update member function'
// to load new rate value from the config file
monitor.LoadConfig(pe, cf);
//logger
sim_env.logger =
std::make_shared<SimulationLogger>(clasim_results_dir+"SimulationEngineLog_"+patient_name+".txt");
bool STOP = false; // Execute stop hemmorrhage only once; without this it be executed for each timestep
bool DEVICES_START = false;
// stop when the set time is reached
while (sim_env.time_index <= sim_env.simulation_timesteps) {
sim_env.sim_currentTime = pe->GetSimulationTime(TimeUnit::s);
//cout << "sim time "<< sim_env.sim_currentTime <<endl;
// event_hemorrage_start
if(sim_env.time_index == sim_env.simulation_injury_start_timestep ){
// Hemorrhage Starts - instantiate a hemorrhage action and have the engine process it
cout << "\nHemorrhage Started at: " << pe->GetSimulationTime(TimeUnit::s) << endl;
//the location of the hemorrhage
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
//the rate of hemorrhage
hemorrhageLeg.GetRate().SetValue(150,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
STOP = true;
}
currentMAP = pe->GetCardiovascularSystem()->GetMeanArterialPressure(PressureUnit::mmHg);
}
if( currentMAP <= initialMAP && STOP){
void Simulation(const string patient_name, const std::shared_ptr<Config>& cf) {
// Create the engine and load the patient
std::unique_ptr<PhysiologyEngine> pe = CreatePulseEngine(clasim_results_dir+
"SimulationEngine_"+patient_name+".log");
pe->GetLogger()->Info("CLA Simulation");
if (!pe->LoadStateFile(pulse_state_dir+patient_name+"@0s.pba"))
{
pe->GetLogger()->Error("Could not load state, check the error");
return;
}
// Create data requests for each value that should be written to the output log as
// the engine is executing
// Physiology System Names are defined on the System Objects
// defined in the Physiology.xsd file
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest(
"HeartRate", FrequencyUnit::Per_min);
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest(
"BloodVolume", VolumeUnit::mL);
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest(
"MeanArterialPressure", PressureUnit::mmHg);
// Store requested data in a file
pe->GetEngineTracker()->GetDataRequestManager().SetResultsFilename(
clasim_results_dir+"PulseSimEngine_"+patient_name+".txt");
// Hemorrhage Action
SEHemorrhage hemorrhageLeg;
const double initialMAP = 70.0;
double currentMAP;
double hemorrhageRate;
// Enviroment object
CMD::Environment sim_env;
PhysiologyData data;
// Load simulation environment/variables
Global_LoadConfig(pe, cf, &sim_env);
// Hash map for the different pumps
map<string, Pump*> pumps;
// Intialize Saline Pump
Pump pump_saline(&sim_env, &data, "Saline");
pump_saline.LoadConfig(pe, cf);
// Initialize Norepinephrine Pump
Pump pump_bpdrug(&sim_env, &data, "Norepinephrine");
pump_bpdrug.LoadConfig(pe, cf);
pumps.insert(make_pair("saline", &pump_saline));
pumps.insert(make_pair("bpdrug", &pump_bpdrug));
// Initialize Controller
Controller controller(&sim_env, &data, pumps);
controller.LoadConfig(pe, cf);
// Initialize Monitor
Monitor monitor(&sim_env, &data);
monitor.LoadConfig(pe, cf);
// logger
sim_env.logger = std::make_shared<SimulationLogger>(clasim_results_dir+
"SimulationEngineLog_"+patient_name+".txt");
// Execute stop hemmorrhage only once; without this it'd be executed for each
// timestep
bool STOP = false;
bool DEVICES_START = false;
// Stop when the set time is reached
while (sim_env.time_index <= sim_env.simulation_timesteps) {
sim_env.sim_currentTime = pe->GetSimulationTime(TimeUnit::s);
// event_hemorrage_start
if(sim_env.time_index == sim_env.simulation_injury_start_timestep) {
// Hemorrhage Starts
hemorrhageRate = 150;
cout << "\nHemorrhage Started at: "<<pe->GetSimulationTime(TimeUnit::s)<<
endl;
// location of the hemorrhage
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
// rate of hemorrhage
hemorrhageLeg.GetRate().SetValue(hemorrhageRate,
VolumePerTimeUnit::mL_Per_min);
// Engine processes this action
pe->ProcessAction(hemorrhageLeg);
STOP = true;
}
cout << "\nHemorrhage Stoppd at: " << pe->GetSimulationTime(TimeUnit::s) << endl;
// polling MAP
currentMAP = pe->GetCardiovascularSystem()->GetMeanArterialPressure(
PressureUnit::mmHg);
//the location of the hemorrhage
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
// Stop hemorrhage
hemorrhageLeg.GetRate().SetValue(0,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
if(currentMAP <= initialMAP && STOP) {
STOP = false;
// Hemorrhage Stops
hemorrhageRate = 0;
cout<<"\nHemorrhage Stoppd at: "<< pe->GetSimulationTime(TimeUnit::s)<<
endl;
// instruct devices to start
DEVICES_START = true;
// location of the hemorrhage
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
hemorrhageLeg.GetRate().SetValue(hemorrhageRate,
VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
cout << "START DEVICES" << endl;
}
STOP = false;
if (DEVICES_START) {
// instruct devices to start
DEVICES_START = true;
monitor.update(pe);
controller.update(pe);
pumps["saline"]->update(pe);
pumps["bpdrug"]->update(pe);
cout << "START DEVICES" << endl;
}
}
if (DEVICES_START) {
/*
if( sim_env.time_index == sim_env.simulation_injury_stop_timestep ){
monitor.update(pe);
controller.update(pe);
pumps["saline"]->update(pe);
pumps["bpdrug"]->update(pe);
cout << "\nHemorrhage Stoppd at: " << pe->GetSimulationTime(TimeUnit::s) << endl;
}
//the location of the hemorrhage
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
// Stop hemorrhage
hemorrhageLeg.GetRate().SetValue(0,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
}
*/
// track patient data
pe->GetEngineTracker()->TrackData(pe->GetSimulationTime(TimeUnit::s));
// Advance engine for each time step
pe->AdvanceModelTime();
// Track patient data
pe->GetEngineTracker()->TrackData(pe->GetSimulationTime(TimeUnit::s));
// Advance engine for each time step
pe->AdvanceModelTime();
// increment environment time index whenever engine advances
sim_env.time_index++;
// increment environment time index whenever engine advances
sim_env.time_index++;
}// End while loop
}// End while looop
}// End Simulation function
void Global_LoadConfig(std::unique_ptr<PhysiologyEngine>& engine, const std::shared_ptr<Config>& cf, CMD::Environment* env) {
/* get the simulation run time */
try
{
env->simulation_time = cf->lookup("simulation.time.run");
}
catch(const SettingNotFoundException) {
cout << "Setting Not Found: simulation.time.run" << endl;
}
/* get the injury start time */
try
{
env->simulation_injury_start = cf->lookup("simulation.time.injury_start");
}
catch(const SettingNotFoundException) {
cout << "Setting Not Found: simulation.time.injury_start" << endl;
}
/* get the injury stop time */
try
{
env->simulation_injury_stop = cf->lookup("simulation.time.injury_stop");
}
catch(const SettingNotFoundException) {
cout << "Setting Not Found: simulation.time.injury_stop" << endl;
}
env->time_index = 0; // initialize the time index
env->engine_timestep = engine->GetTimeStep(TimeUnit::s);
env->simulation_timesteps = env->simulation_time / env->engine_timestep;
env->simulation_injury_start_timestep = env->simulation_injury_start / env->engine_timestep;
env->simulation_injury_stop_timestep = env->simulation_injury_stop / env->engine_timestep;
env->vector_index = 0;
env->sim_currentTime = engine->GetSimulationTime(TimeUnit::s);
//env->logger = std::make_shared<SimulationLogger>("SimulatoinEngineLog.txt");
}// End Global_LoadConfig
/* get the simulation run time */
try
{
env->simulation_time = cf->lookup("simulation.time.run");
}
catch(const SettingNotFoundException) {
cout << "Setting Not Found: simulation.time.run" << endl;
}
/* get the injury start time */
try
{
env->simulation_injury_start = cf->lookup("simulation.time.injury_start");
}
catch(const SettingNotFoundException) {
cout << "Setting Not Found: simulation.time.injury_start" << endl;
}
/* get the injury stop time */
try
{
env->simulation_injury_stop = cf->lookup("simulation.time.injury_stop");
}
catch(const SettingNotFoundException) {
cout << "Setting Not Found: simulation.time.injury_stop" << endl;
}
env->time_index = 0; // initialize the time index
env->engine_timestep = engine->GetTimeStep(TimeUnit::s);
env->simulation_timesteps = env->simulation_time / env->engine_timestep;
env->simulation_injury_start_timestep = env->simulation_injury_start /
env->engine_timestep;
env->simulation_injury_stop_timestep = env->simulation_injury_stop /
env->engine_timestep;
env->vector_index = 0;
env->sim_currentTime = engine->GetSimulationTime(TimeUnit::s);
vector<string> get_global_patients(const std::shared_ptr<Config>& cf){
vector<string> patients;
}// End Global_LoadConfig
try{
vector<string> get_global_patients(const std::shared_ptr<Config>& cf) {
Setting& names = cf->lookup("simulation.patients.names");
vector<string> patients;
// Check if patient list is empty
if (names.getLength() < 1) cout <<"Name List is empty"<<endl;
try{
Setting& names = cf->lookup("simulation.patients.names");
// Check if patient list is empty
if (names.getLength() < 1) cout <<"Name List is empty"<<endl;
for (Setting& name : names) {
//use strlen to check if entered string isn't empty
for (Setting& name : names) {
patients.push_back(string((const char*)name));
}
patients.push_back(string((const char*)name));
//env->patient_names.push_back(string(p_name));
}
}
catch(const SettingNotFoundException) {
cout << "Setting Not Found: simulation.patients.names" << endl;
}
}
catch(const SettingNotFoundException) {
cout << "Setting Not Found: simulation.patients.names" << endl;
}
return patients;
return patients;
}
}// End get_global_patients
#ifndef SIMENGINE_H
#define SIMENGINE_H
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <libconfig.h>
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
#include "configure_clasim_paths.h"
// Include the various types you will be using in your code
#include "scenario/SEDataRequestManager.h"
#include "patient/actions/SEHemorrhage.h"
#include "patient/actions/SESubstanceCompoundInfusion.h"
#include "system/physiology/SEBloodChemistrySystem.h"
#include "system/physiology/SECardiovascularSystem.h"
#include "system/physiology/SEEnergySystem.h"
#include "system/physiology/SERespiratorySystem.h"
#include "substance/SESubstanceManager.h"
#include "substance/SESubstanceCompound.h"
#include "properties/SEScalar0To1.h"
#include "properties/SEScalarFrequency.h"
#include "properties/SEScalarMass.h"
#include "properties/SEScalarMassPerVolume.h"
#include "properties/SEScalarPressure.h"
#include "properties/SEScalarTemperature.h"
#include "properties/SEScalarTime.h"
#include "properties/SEScalarVolume.h"
#include "properties/SEScalarVolumePerTime.h"
#include "engine/SEEngineTracker.h"
#include "compartment/SECompartmentManager.h"
using namespace libconfig;
using namespace std;
void Simulation(const std::string, const std::shared_ptr<Config>& cf);
void Global_LoadConfig(std::unique_ptr<PhysiologyEngine>&, const
......
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