#include #include #include #include #include #include #include "serial/serial.h" #include "HardwareSimulator.h" #include "CLA_Logger.h" #include "SimulationEngine.h" #include "Timer.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; int main() { CLA::Timer timer; auto cf = std::make_shared(); string f_path = prosim_config_dir+"scenario.cfg"; const char* filepath = f_path.c_str(); Simulation("Cynthia", cf, filepath); } void Simulation(const string patient_name, const std::shared_ptr& cfg, const char* filepath ) { // Create the engine and load the patient std::unique_ptr pe = CreatePulseEngine(prosim_results_dir+"SimulationEngine_"+patient_name+".log"); pe->GetLogger()->Info("CLA_Prosim 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 pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("SystolicArterialPressure", PressureUnit::mmHg); pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("DiastolicArterialPressure", PressureUnit::mmHg); pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("MeanArterialPressure", PressureUnit::mmHg); pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("HeartRate", FrequencyUnit::Per_min); pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("RespirationRate", FrequencyUnit::Per_min); pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("OxygenSaturation"); pe->GetEngineTracker()->GetDataRequestManager().SetResultsFilename(prosim_results_dir+"PulseSimEngine_"+patient_name+".txt"); SEHemorrhage hemorrhageLeg; double initialMAP = 70.0; double currentMAP; CLA::Environment sim_env; Global_LoadConfig(pe, cfg, filepath, &sim_env); /*serial instance arguments*/ std::string port = "/dev/ttyACM0"; // arbitrary port uint32_t baudrate = 115200; serial::Timeout timeout = serial::Timeout(); serial::bytesize_t bytesize = serial::eightbits; serial::parity_t parity = serial::parity_none; serial::stopbits_t stopbits = serial::stopbits_one; serial::flowcontrol_t flowcontrol = serial::flowcontrol_hardware; /*end serial instance arguments*/ serial::Serial myserial(port, baudrate, timeout, bytesize, parity, stopbits, flowcontrol); CLA::LOGGER logger; /* for timestamped logs */ 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 "<GetSimulationTime(TimeUnit::s)<ProcessAction(hemorrhageLeg); STOP = true; } currentMAP = pe->GetCardiovascularSystem()->GetMeanArterialPressure(PressureUnit::mmHg); if( currentMAP <= initialMAP && STOP) { cout<<"\nHemorrhage Stoppd at: "<GetSimulationTime(TimeUnit::s)<ProcessAction(hemorrhageLeg); STOP = false; } /* We both track data and advance engine inside the update method */ prosim.update(pe); sim_env.time_index++; }// End while looop }// End Simulation function void Global_LoadConfig(std::unique_ptr& engine, const std::shared_ptr& cf, const char* file_path, CLA::Environment* env) { try { cf->readFile(file_path); // Read configration file } catch(const FileIOException &fioex) { std::cerr << "I/O error while reading file." << std::endl; } catch(const ParseException &pex) { std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << std::endl; } env->simulation_time = cf->lookup("simulation.time.run"); env->simulation_injury_start = cf->lookup("simulation.time.injury_start"); env->simulation_injury_stop = cf->lookup("simulation.time.injury_stop"); env->time_index = 0; // initialize the time index env->engine_timestep = cf->lookup("pulse.advance_time"); 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; }// End Global_LoadConfig