Feat: Implement interconnected organ simulation
This commit refactors the organ simulation to enable dynamic interactions between organs, replacing the previous "faked" or hardcoded connections.
Key changes include:
- Major Refactoring: Changed the `Organ::update` method signature to `update(Patient& patient, double deltaTime_s)`, allowing organs to access the shared patient state and other organs. This was propagated to all organ classes.
- Blood Chemistry Model: Introduced a central `Blood` struct in the `Patient` model to track shared resources like oxygen, CO2, glucose, and toxins.
- Organ System Interconnections:
- Lungs & Brain: Lungs now perform gas exchange affecting the blood. The brain consumes O2, produces CO2, and its GCS is affected by hypoxia/hypercapnia.
- Liver-Gallbladder: Gallbladder now receives bile directly from the liver's production rate.
- Digestive System: Stomach passes chyme to the intestines, which absorb glucose into the blood. The pancreas responds to blood glucose changes.
- Renal System: Kidneys' GFR is now influenced by the heart's aortic pressure, and they produce urine that fills the bladder directly.
- Cardiovascular & Neurological: The heart rate responds to hypoxia, and the brain uses live aortic pressure from the heart.
- Comprehensive Test Scenario: Updated the main example to include a 60-second simulation with a meal and a lung injury event to verify the new interconnected system.
This creates a more realistic and scalable physiological simulation framework where organ behaviors are emergent from their interactions.
This commit is contained in:
+44
-6
@@ -8,31 +8,69 @@
|
||||
#include "MedicalLib/Patient.h"
|
||||
#include "MedicalLib/Organ.h"
|
||||
#include "MedicalLib/Heart.h"
|
||||
#include "MedicalLib/Stomach.h"
|
||||
#include "MedicalLib/Lungs.h"
|
||||
|
||||
int main() {
|
||||
// Initialize a new patient with a 12-lead heart
|
||||
Patient patient = initializePatient(1, 12);
|
||||
std::cout << "Patient created with ID: " << patient.patientId << std::endl;
|
||||
|
||||
// Introduce a toxin load to the blood for the liver to clear
|
||||
patient.blood.toxins_au = 100.0;
|
||||
std::cout << "Initial toxin load of 100.0 a.u. introduced.\n" << std::endl;
|
||||
|
||||
// Simulate time passing and print a live summary
|
||||
const double simulationTime_s = 10.0;
|
||||
const double deltaTime_s = 0.05; // 20 Hz update rate for display
|
||||
const double simulationTime_s = 60.0; // Run for a longer time to see effects
|
||||
const double deltaTime_s = 0.1;
|
||||
const int numSteps = static_cast<int>(simulationTime_s / deltaTime_s);
|
||||
|
||||
std::cout << "\n--- Simulating " << simulationTime_s << " seconds of heart activity... ---" << std::endl;
|
||||
// Get pointers to organs we want to interact with
|
||||
Stomach* stomach = getOrgan<Stomach>(patient);
|
||||
if(stomach) {
|
||||
stomach->addSubstance(300.0); // Simulate eating a meal
|
||||
std::cout << "A 300mL meal has been consumed." << std::endl;
|
||||
}
|
||||
Lungs* lungs = getOrgan<Lungs>(patient);
|
||||
|
||||
|
||||
std::cout << "\n--- Simulating " << simulationTime_s << " seconds... ---" << std::endl;
|
||||
|
||||
for (int i = 0; i < numSteps; ++i) {
|
||||
double currentTime = i * deltaTime_s;
|
||||
// Clear console on systems that support ANSI escape codes
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
std::cout << "\033[2J\033[1;1H";
|
||||
#endif
|
||||
|
||||
// --- Event scripting ---
|
||||
if (std::abs(currentTime - 20.0) < deltaTime_s/2.0) {
|
||||
if (lungs) {
|
||||
std::cout << "\n*** LUNG INJURY EVENT ***\n" << std::endl;
|
||||
lungs->inflictDamage(0.8); // 80% damage
|
||||
}
|
||||
}
|
||||
|
||||
updatePatient(patient, deltaTime_s);
|
||||
|
||||
std::cout << "Time: " << i * deltaTime_s << "s / " << simulationTime_s << "s\n" << std::endl;
|
||||
std::cout << getOrganSummary(patient, "Heart") << std::endl;
|
||||
std::cout << "Time: " << currentTime << "s / " << simulationTime_s << "s\n" << std::endl;
|
||||
std::cout << "--- Blood Chemistry ---\n"
|
||||
<< "SpO2: " << patient.blood.oxygenSaturation << " %\n"
|
||||
<< "PaCO2: " << patient.blood.co2PartialPressure_mmHg << " mmHg\n"
|
||||
<< "Glucose: " << patient.blood.glucose_mg_per_dL << " mg/dL\n"
|
||||
<< "Toxins: " << patient.blood.toxins_au << " a.u.\n\n";
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
std::cout << getOrganSummary(patient, "Heart") << std::endl;
|
||||
std::cout << getOrganSummary(patient, "Lungs") << std::endl;
|
||||
std::cout << getOrganSummary(patient, "Brain") << std::endl;
|
||||
std::cout << getOrganSummary(patient, "Stomach") << std::endl;
|
||||
std::cout << getOrganSummary(patient, "Intestines") << std::endl;
|
||||
std::cout << getOrganSummary(patient, "Pancreas") << std::endl;
|
||||
std::cout << getOrganSummary(patient, "Kidneys") << std::endl;
|
||||
std::cout << getOrganSummary(patient, "Bladder") << std::endl;
|
||||
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
std::cout << "\n--- Simulation Complete. Final State: ---\n" << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user