Controller.h
1/* Distributed under the Apache License, Version 2.0.
2 See accompanying NOTICE file for details.*/
3
4#pragma once
5
6#include "engine/CommonDefs.h"
7#include "engine/PulseConfiguration.h"
8#include "engine/common/controller/BlackBoxManager.h"
9#include "engine/common/controller/CircuitManager.h"
10#include "engine/common/controller/CompartmentManager.h"
11#include "engine/common/controller/SubstanceManager.h"
12
13// CDM
14class DataTrack;
15class SEActionManager;
17class SEDataRequested;
18class SEEngineTracker;
19#include "cdm/engine/SEEventManager.h"
20#include "cdm/engine/SEEngineStabilization.h"
21#include "cdm/properties/SEScalarTime.h"
22
23namespace pulse
24{
25 class StabilizationController;
26 class Model;
27
28 class EnvironmentModel;
29
30 class BloodChemistryModel;
31 class CardiovascularModel;
32 class EndocrineModel;
33 class EnergyModel;
34 class GastrointestinalModel;
35 class HepaticModel;
36 class NervousModel;
37 class RenalModel;
38 class RespiratoryModel;
39 class DrugModel;
40 class TissueModel;
41
42 class AnesthesiaMachineModel;
43 class BagValveMaskModel;
44 class ECMOModel;
45 class ElectroCardioGramModel;
46 class InhalerModel;
47 class MechanicalVentilatorModel;
48
49 class SaturationCalculator;
50
51 enum class EngineState
52 {
53 NotReady = 0,
54 Initialization,
55 InitialStabilization,
56 AtInitialStableState,
57 SecondaryStabilization,
58 AtSecondaryStableState,
59 Active,
60 Fatal
61 };
62
63 // Keep enums in sync with appropriate proto file !!
64 enum class eAirwayMode
65 {
66 Free = 0,
67 AnesthesiaMachine,
68 BagValveMask,
69 Inhaler,
70 MechanicalVentilation,// Action
71 MechanicalVentilator, // Equipment
72 NasalCannula,
73 NonRebreatherMask,
74 SimpleMask
75 };
76 extern PULSE_DECL const std::string& eAirwayMode_Name(eAirwayMode m);
77
78 class PULSE_DECL Data : public Loggable
79 {
80 friend class Engine;
81 protected:// Create in an Engine
82 Data(Logger* logger = nullptr);
83 public:
84 virtual ~Data();
85
86 virtual std::string GetDataRoot() const { return m_DataDir; }
87 virtual void SetDataRoot(const std::string& dir) { m_DataDir = dir; }
88
89 virtual void AdvanceCallback(double time_s);
90
91 virtual EngineState GetState() const { return m_State; }
92
93 virtual SEEngineTracker& GetEngineTracker() const;
94 virtual DataTrack& GetDataTrack() const;
95 virtual SaturationCalculator& GetSaturationCalculator() const;
96
97 virtual SubstanceManager& GetSubstances() const;
98
99 virtual const SEPatient& GetInitialPatient() const;
100 virtual SEPatient& GetCurrentPatient() const;
101
102 virtual bool HasBloodChemistry() const;
103 virtual SEBloodChemistrySystem& GetBloodChemistry() const;
104 virtual bool HasCardiovascular() const;
105 virtual SECardiovascularSystem& GetCardiovascular() const;
106 virtual bool HasDrugs() const;
107 virtual SEDrugSystem& GetDrugs() const;
108 virtual bool HasEndocrine() const;
109 virtual SEEndocrineSystem& GetEndocrine() const;
110 virtual bool HasEnergy() const;
111 virtual SEEnergySystem& GetEnergy() const;
112 virtual bool HasGastrointestinal() const;
113 virtual SEGastrointestinalSystem& GetGastrointestinal() const;
114 virtual bool HasHepatic() const;
115 virtual SEHepaticSystem& GetHepatic() const;
116 virtual bool HasNervous() const;
117 virtual SENervousSystem& GetNervous() const;
118 virtual bool HasRenal() const;
119 virtual SERenalSystem& GetRenal() const;
120 virtual bool HasRespiratory() const;
121 virtual SERespiratorySystem& GetRespiratory() const;
122 virtual bool HasTissue() const;
123 virtual SETissueSystem& GetTissue() const;
124
125 virtual bool HasEnvironment() const;
126 virtual SEEnvironment& GetEnvironment() const;
127 virtual bool HasAnesthesiaMachine() const;
128 virtual SEAnesthesiaMachine& GetAnesthesiaMachine() const;
129 virtual bool HasBagValveMask() const;
130 virtual SEBagValveMask& GetBagValveMask() const;
131 virtual bool HasECG() const;
132 virtual SEElectroCardioGram& GetECG() const;
133 virtual bool HasECMO() const;
134 virtual SEECMO& GetECMO() const;
135 virtual bool HasInhaler() const;
136 virtual SEInhaler& GetInhaler() const;
137 virtual bool HasMechanicalVentilator() const;
138 virtual SEMechanicalVentilator& GetMechanicalVentilator() const;
139
140 virtual SEActionManager& GetActions() const;
141
142 virtual SEConditionManager& GetConditions() const;
143
144 virtual SEEventManager& GetEvents() const;
145
146 virtual CircuitManager& GetCircuits() const;
147
148 virtual CompartmentManager& GetCompartments() const;
149
150 virtual BlackBoxManager& GetBlackBoxes() const;
151
152 virtual const PulseConfiguration& GetConfiguration() const;
153
154 virtual double GetTimeStep_s() const;
155 virtual const SEScalarTime& GetTimeStep() const;
156 virtual const SEScalarTime& GetEngineTime() const;
157 virtual const SEScalarTime& GetSimulationTime() const;
158 virtual const SEScalarTime& GetStabilizationTime() const;
159
160 virtual bool IsAirwayModeSupported(eAirwayMode /*mode*/) { return true; }
161 virtual eAirwayMode GetAirwayMode() const { return m_AirwayMode; }
162 virtual void SetAirwayMode(eAirwayMode mode);
163
164 virtual eSwitch GetIntubation() const { return m_Intubation; }
165
166 virtual void SetAdvanceHandler(SEAdvanceHandler* handler) { m_AdvanceHandler = handler; }
167
168 virtual const SEScalarProperties& GetOverrides() const;
169
170 std::stringstream m_ss;
171 protected:
172 virtual void SetupTracker();
173
174 EngineState m_State;
175 SEEngineTracker* m_EngineTrack;
176 SEDataRequested* m_DataRequested;
177 eEngineInitializationState m_EngineInitializationState;
178
179 SEScalarTime m_CurrentTime;
180 SEScalarTime m_SimulationTime;
181 SEScalarTime m_StabilizationTime;
182 double m_SpareAdvanceTime_s;
183 eAirwayMode m_AirwayMode;
184 eSwitch m_Intubation;
185
186 PulseConfiguration* m_Config = nullptr;
187 SaturationCalculator* m_SaturationCalculator = nullptr;
188
189 SubstanceManager* m_Substances = nullptr;
190
191 SEActionManager* m_Actions = nullptr;
192 SEConditionManager* m_Conditions = nullptr;
193 CircuitManager* m_Circuits = nullptr;
194 CompartmentManager* m_Compartments = nullptr;
195 BlackBoxManager* m_BlackBoxes = nullptr;
196
197 EnvironmentModel* m_EnvironmentModel = nullptr;
198
199 BloodChemistryModel* m_BloodChemistryModel = nullptr;
200 CardiovascularModel* m_CardiovascularModel = nullptr;
201 EndocrineModel* m_EndocrineModel = nullptr;
202 EnergyModel* m_EnergyModel = nullptr;
203 GastrointestinalModel* m_GastrointestinalModel = nullptr;
204 HepaticModel* m_HepaticModel = nullptr;
205 NervousModel* m_NervousModel = nullptr;
206 RenalModel* m_RenalModel = nullptr;
207 RespiratoryModel* m_RespiratoryModel = nullptr;
208 DrugModel* m_DrugModel = nullptr;
209 TissueModel* m_TissueModel = nullptr;
210
211 AnesthesiaMachineModel* m_AnesthesiaMachineModel = nullptr;
212 BagValveMaskModel* m_BagValveMaskModel = nullptr;
213 ECMOModel* m_ECMOModel = nullptr;
214 ElectroCardioGramModel* m_ElectroCardioGramModel = nullptr;
215 InhalerModel* m_InhalerModel = nullptr;
216 MechanicalVentilatorModel* m_MechanicalVentilatorModel = nullptr;
217
218 SEPatient* m_InitialPatient = nullptr;
219 SEPatient* m_CurrentPatient = nullptr;
220
221 SEEventManager* m_EventManager = nullptr;
222 LoggerForward* m_LogForward = nullptr;
223
224 SEAdvanceHandler* m_AdvanceHandler = nullptr;
225
226 std::string m_DataDir;
227
228 SEScalarProperties m_ScalarOverrides;
229 protected:
230 std::vector<pulse::Model*> m_Models;
231 };
232
236 class PULSE_DECL Controller : public Data
237 {
238 friend class Engine;
239 friend class EngineTest;
240 friend class PBState;//friend the serialization class
241 protected:// Create via Engine
242 Controller(Logger* logger = nullptr);
243 public:
244 virtual ~Controller();
245
246 virtual Data& GetData() { return (*this); }
247 virtual const Data& GetData() const { return (*this); }
248
249 virtual bool SerializeFromFile(const std::string& file);
250 virtual bool SerializeToFile(const std::string& file) const;
251
252 virtual bool SerializeFromString(const std::string& state, eSerializationFormat m);
253 virtual bool SerializeToString(std::string& state, eSerializationFormat m) const;
254
255 virtual bool InitializeEngine(const std::string& patient_configuration, eSerializationFormat m);
256 virtual bool InitializeEngine(const SEPatientConfiguration& patient_configuration);
257 virtual bool IsReady() const;
258 virtual eEngineInitializationState GetInitializationState() const;
259
260 virtual void Clear();
261
262 virtual bool SetConfigurationOverride(const SEEngineConfiguration* config);
263
264 virtual void SetSimulationTime(const SEScalarTime& time);
265
266 virtual bool AdvanceModelTime();
267 virtual bool AdvanceModelTime(double time, const TimeUnit& unit);
268 virtual bool ProcessAction(const SEAction& action);
269
270 virtual bool GetPatientAssessment(SEPatientAssessment& assessment) const = 0;
271
272 virtual bool CreateCircuitsAndCompartments();
273
274 virtual void CheckIntubation();
275 protected:
276 virtual std::string GetTypeName() const = 0;
277 virtual void LogBuildInfo() const;
278 // Setup Circuit/Compartments for systems
279
280 // Default/Optimal Cardiovascular
281 virtual void SetupCardiovascular();
282 virtual void SetupRenal();
283 virtual void SetupTissue();
284 virtual void SetupCerebrospinalFluid();
285
286 // Optional Expanded Cardiovascular Circuit Setup Methods
287 virtual void SetupExpandedCardiovascular();
288 virtual void SetupExpandedCardiovascularRenal();
289 virtual void SetupExpandedCardiovascularTissue();
290 virtual void SetupExpandedCardiovascularCerebrospinalFluid();
291
292 // Default/Optimal Gastrointestinal
293 virtual void SetupGastrointestinal();
294
295 // Default/Optimal Respiratory
296 virtual void SetupRespiratory();
297
298 // Optional Expanded Respiratory and Cardiovascular Circuit Setup Methods
299 virtual void SetupExpandedPulmonaryRespiratory();
300 virtual void SetupExpandedPulmonaryCardiovascular();
301
302 // Default/Optimal Equipment
303 virtual void SetupAnesthesiaMachine();
304 virtual void SetupBagValveMask();
305 virtual void SetupECMO();
306 virtual void SetupInhaler();
307 virtual void SetupMechanicalVentilation();
308 virtual void SetupMechanicalVentilator();
309 virtual void SetupNasalCannula();
310 virtual void SetupSimpleMask();
311 virtual void SetupNonRebreatherMask();
312
313 // Default/Optimal Energy
314 virtual void SetupExternalTemperature();
315 virtual void SetupInternalTemperature();
316
317 virtual bool OverrideCircuits();
318 virtual bool ModifyCircuits(const SEScalarProperties& modifiers);
319
320 virtual bool Initialize(const SEPatient& patient);
321 virtual bool Stabilize(const SEPatientConfiguration& patient_configuration);
322
323 // Allocate all the models this engine supports
324 virtual void Allocate();
325 virtual bool SetupPatient(const SEPatient& patient) = 0;
326 // Based on what modles are used, setup order for the following
327 virtual void InitializeModels();
328 // Notify Models that steady state has been achieved
329 virtual void AtSteadyState(EngineState state);
330 virtual void PreProcess();
331 virtual void Process();
332 virtual void PostProcess();
333
334 PulseConfiguration const*m_ConfigOverride = nullptr;
335 StabilizationController *m_Stabilizer = nullptr;
336 };
337
338 class PULSE_DECL StabilizationController : public SEEngineStabilization::Controller
339 {
340 public:
341 StabilizationController(pulse::Controller& pc) : _pc(pc) {}
342 virtual ~StabilizationController() = default;
343
344 bool AdvanceTime() override { return _pc.AdvanceModelTime(); }
345 SEEngineTracker* GetEngineTracker() override
346 {
347 return &_pc.GetData().GetEngineTracker();
348 }
349 double GetTimeStep(const TimeUnit& unit) override
350 {
351 return _pc.GetData().GetTimeStep().GetValue(unit);
352 }
353 double GetSimulationTime(const TimeUnit& unit) override
354 {
355 return _pc.GetData().GetSimulationTime().GetValue(unit);
356 }
357
358 protected:
359 pulse::Controller& _pc;
360 };
361
362 class FatalListner : public LoggerForward
363 {
364 public:
365 FatalListner(SEEventManager& mgr, SEScalarTime& ct) : m_Events(mgr), m_CurrentTime(ct) {};
366 ~FatalListner() = default;
367
368 void ForwardDebug(const std::string& /*msg*/) override { }
369 void ForwardInfo(const std::string& /*msg*/) override { }
370 void ForwardWarning(const std::string& /*msg*/) override { }
371 void ForwardError(const std::string& /*msg*/) override { }
372 void ForwardFatal(const std::string& /*msg*/) override
373 {
374 m_Events.SetEvent(eEvent::IrreversibleState, true, m_CurrentTime);
375 throw IrreversibleStateException(); // Caught in Common::AdvanceModelTime, so we do not do anything more in the engine
376 }
377
378 protected:
379 SEEventManager& m_Events;
380 SEScalarTime& m_CurrentTime;
381 };
382END_NAMESPACE
Definition: DataTrack.h:17
Definition: Logger.h:23
Definition: Logger.h:60
Definition: Logger.h:71
Pulse specific configuration parameters for all systems/equipment
Definition: PulseConfiguration.h:20
Definition: SEAction.h:14
Definition: SEActionManager.h:12
Definition: SEAdvanceHandler.h:8
Definition: SEAnesthesiaMachine.h:24
Definition: SEBagValveMask.h:14
Definition: SEBloodChemistrySystem.h:10
Definition: SECardiovascularSystem.h:24
Definition: SEConditionManager.h:26
Definition: SEDataRequested.h:11
Definition: SEDrugSystem.h:9
Definition: SEECMO.h:10
Definition: SEElectroCardioGram.h:9
Definition: SEEndocrineSystem.h:8
Definition: SEEnergySystem.h:8
Definition: SEEngineConfiguration.h:8
Definition: SEEngineStabilization.h:18
Definition: SEEngineTracker.h:66
double GetValue(const SEDataRequest &dr) const
Definition: SEEngineTracker.cpp:131
Definition: SEEnvironment.h:13
Definition: SEEventManager.h:117
Definition: SEGastrointestinalSystem.h:9
Definition: SEHepaticSystem.h:8
Definition: SEInhaler.h:12
Definition: SEMechanicalVentilator.h:10
Definition: SENervousSystem.h:9
Data formed at a level of a clinicians report. This is high level data, such as a mean or generalized...
Definition: SEPatientAssessment.h:22
Definition: SEPatientConfiguration.h:11
Definition: SEPatient.h:13
Definition: SERenalSystem.h:8
Definition: SERespiratorySystem.h:25
Definition: SEScalarTime.h:28
Definition: SETissueSystem.h:8
Definition: SEScalarTime.h:8
Definition: Logger.h:14
const std::string & eAirwayMode_Name(eAirwayMode m)
Definition: PBEnums.cpp:12
Definition: PulseEngine.h:23

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.