Files
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

58 lines
2.0 KiB
C++

#include "MedicalLib/Spleen.h"
#include "MedicalLib/Patient.h"
#include <random>
#include <algorithm>
#include <sstream>
#include <iomanip>
// Helper function for random fluctuations
static double getFluctuation(double stddev) {
static std::random_device rd;
static std::mt19937 gen(rd());
std::normal_distribution<> d(0, stddev);
return d(gen);
}
Spleen::Spleen(int id) : Organ(id, "Spleen") {
// Initialize pulp components
redPulp = {1.0, 0.5};
whitePulp = {1500.0, 500.0};
}
void Spleen::update(Patient& patient, double deltaTime_s) {
// In a real model, these values would change in response to infection or disease.
// For now, we just simulate minor fluctuations around a healthy baseline.
// Red pulp fluctuations
redPulp.filtrationRate += getFluctuation(0.01);
redPulp.rbcBreakdownRate += getFluctuation(0.005);
// White pulp fluctuations
whitePulp.lymphocyteCount += getFluctuation(1.0);
whitePulp.macrophageCount += getFluctuation(0.5);
// Clamp to healthy ranges
redPulp.filtrationRate = std::clamp(redPulp.filtrationRate, 0.9, 1.1);
redPulp.rbcBreakdownRate = std::clamp(redPulp.rbcBreakdownRate, 0.45, 0.55);
whitePulp.lymphocyteCount = std::clamp(whitePulp.lymphocyteCount, 1400.0, 1600.0);
whitePulp.macrophageCount = std::clamp(whitePulp.macrophageCount, 450.0, 550.0);
}
std::string Spleen::getSummary() const {
std::stringstream ss;
ss.precision(1);
ss << std::fixed;
ss << "--- Spleen Summary ---\n"
<< "--- Red Pulp ---\n"
<< "Filtration Rate: " << redPulp.filtrationRate << "\n"
<< "RBC Breakdown Rate: " << getRbcBreakdownRate() << "\n"
<< "--- White Pulp ---\n"
<< "Lymphocyte Count: " << getLymphocyteCount() << " million\n"
<< "Macrophage Count: " << whitePulp.macrophageCount << " million\n";
return ss.str();
}
// --- Getters Implementation ---
double Spleen::getRbcBreakdownRate() const { return redPulp.rbcBreakdownRate; }
double Spleen::getLymphocyteCount() const { return whitePulp.lymphocyteCount; }