Files
medicallib/src/Bladder.cpp
T
google-labs-jules[bot] ede2dee772 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.
2025-08-20 08:30:53 +00:00

81 lines
2.7 KiB
C++

#include "MedicalLib/Bladder.h"
#include "MedicalLib/Patient.h"
#include <random>
#include <algorithm>
#include <sstream>
#include <iomanip>
Bladder::Bladder(int id)
: Organ(id, "Bladder"),
currentState(MicturitionState::FILLING),
currentVolume_mL(50.0),
pressure_cmH2O(5.0),
internalSphincterClosed(true) {}
void Bladder::update(Patient& patient, double deltaTime_s) {
// Simple pressure model: pressure increases with volume
pressure_cmH2O = (currentVolume_mL / capacity_mL) * 60.0;
// State machine
switch (currentState) {
case MicturitionState::FILLING:
if (currentVolume_mL > capacity_mL * 0.8 || pressure_cmH2O > pressureThreshold_cmH2O) {
currentState = MicturitionState::FULL;
}
break;
case MicturitionState::FULL:
// For demo, automatically start voiding after 10 seconds in FULL state
static double timeInFullState = 0.0;
timeInFullState += deltaTime_s;
if (timeInFullState > 10.0) {
currentState = MicturitionState::VOIDING;
internalSphincterClosed = false;
timeInFullState = 0.0;
}
break;
case MicturitionState::VOIDING:
double voidingRate_ml_per_s = 15.0;
currentVolume_mL -= voidingRate_ml_per_s * deltaTime_s;
if (currentVolume_mL <= 0) {
currentVolume_mL = 0;
currentState = MicturitionState::FILLING;
internalSphincterClosed = true;
}
break;
}
}
void Bladder::addUrine(double amount_ml) {
if (currentState != MicturitionState::VOIDING) {
currentVolume_mL += amount_ml;
currentVolume_mL = std::clamp(currentVolume_mL, 0.0, capacity_mL);
}
}
std::string Bladder::stateToString(MicturitionState state) const {
switch (state) {
case MicturitionState::FILLING: return "Filling";
case MicturitionState::FULL: return "Full";
case MicturitionState::VOIDING: return "Voiding";
default: return "Unknown";
}
}
std::string Bladder::getSummary() const {
std::stringstream ss;
ss.precision(1);
ss << std::fixed;
ss << "--- Bladder Summary ---\n"
<< "State: " << stateToString(currentState) << "\n"
<< "Volume: " << getVolume() << " / " << capacity_mL << " mL\n"
<< "Pressure: " << getPressure() << " cmH2O\n";
return ss.str();
}
// --- Getters Implementation ---
double Bladder::getVolume() const { return currentVolume_mL; }
double Bladder::getPressure() const { return pressure_cmH2O; }
MicturitionState Bladder::getCurrentState() const { return currentState; }