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:
google-labs-jules[bot]
2025-08-20 08:30:53 +00:00
parent 55ef5ce5aa
commit ede2dee772
30 changed files with 299 additions and 84 deletions
+18 -4
View File
@@ -1,4 +1,7 @@
#include "MedicalLib/Kidneys.h"
#include "MedicalLib/Patient.h"
#include "MedicalLib/Heart.h"
#include "MedicalLib/Bladder.h"
#include <random>
#include <algorithm>
#include <sstream>
@@ -28,7 +31,7 @@ Kidneys::Kidneys(int id)
}
}
void Kidneys::update(double deltaTime_s) {
void Kidneys::update(Patient& patient, double deltaTime_s) {
// Recalculate total capacity based on nephron health
totalFiltrationCapacity = 0.0;
for (const auto& nephron : nephrons) {
@@ -38,14 +41,25 @@ void Kidneys::update(double deltaTime_s) {
}
totalFiltrationCapacity /= nephrons.size();
// GFR is dependent on overall health
const double baseline_gfr = 125.0 * totalFiltrationCapacity;
// GFR is dependent on blood pressure from the heart
double perfusionPressure = 90.0; // Assume normal MAP if heart is not present
if (const Heart* heart = getOrgan<Heart>(patient)) {
perfusionPressure = heart->getAorticPressure();
}
double pressureModifier = std::clamp(perfusionPressure / 90.0, 0.5, 1.2);
const double baseline_gfr = 125.0 * totalFiltrationCapacity * pressureModifier;
gfr_mL_per_min += 0.1 * (baseline_gfr - gfr_mL_per_min) * deltaTime_s + getFluctuation(0.5);
// Urine output is related to GFR but also hydration status (not modeled yet)
// Urine output is related to GFR
urineOutput_mL_per_s = gfr_mL_per_min / 60.0 * 0.01; // Simplified relationship
urineOutput_mL_per_s += getFluctuation(0.001);
// Pass urine to the bladder
if (Bladder* bladder = getOrgan<Bladder>(patient)) {
bladder->addUrine(urineOutput_mL_per_s * deltaTime_s);
}
// Simulate electrolyte balance
bloodSodium_mEq_per_L += getFluctuation(0.05);
bloodPotassium_mEq_per_L += getFluctuation(0.01);