Files
medicallib/src/Pancreas.cpp
T
google-labs-jules[bot] fb0962ff95 feat: Implement major physiological feedback loops
This commit re-implements several critical physiological feedback loops that were lost, enhancing the realism of the simulation.

The following systems have been added:

1.  **Full Digestive Loop:**
    - The Gallbladder now has a `releaseBile` method, triggered by chyme in the intestines.
    - The Pancreas has a `releaseEnzymes` method, also triggered by chyme.
    - The Intestines' digestion logic has been updated to be more effective when bile and enzymes are present.

2.  **Autonomic Nervous System Control:**
    - The Brain now monitors blood gas (O2/CO2) and blood pressure levels.
    - It dynamically adjusts the respiration rate of the Lungs via a new `setRespirationRate` method in response to blood gas changes.
    - It controls the heart rate via a new `setHeartRate` method in response to blood pressure changes, simulating the baroreceptor reflex.
    - The previous hardcoded rate control logic in the Lungs and Heart has been removed.

3.  **Kidney Blood Pressure Regulation (RAAS):**
    - A simplified Renin-Angiotensin-Aldosterone System has been implemented.
    - The Kidneys now secrete renin in response to low blood pressure.
    - The Liver produces a constant supply of angiotensinogen.
    - A new `angiotensin_au` value in the Blood struct is calculated in the main patient update loop.
    - This hormone now acts as a vasoconstrictor, directly affecting the blood pressure calculation in the Heart.

These changes significantly increase the complexity and fidelity of the medical simulation by modeling the interconnectedness of the major organ systems.
2025-08-20 08:55:54 +00:00

88 lines
3.3 KiB
C++

#include "MedicalLib/Pancreas.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);
}
Pancreas::Pancreas(int id)
: Organ(id, "Pancreas"),
insulinSecretion_units_per_hr(1.0),
glucagonSecretion_ng_per_hr(50.0),
amylaseSecretion_U_per_L(80.0),
lipaseSecretion_U_per_L(40.0),
enzymeReleaseRate_ml_per_s(0.5) {}
void Pancreas::update(Patient& patient, double deltaTime_s) {
// Hormone secretion is driven by blood glucose.
const double glucose = patient.blood.glucose_mg_per_dL;
const double highGlucoseThreshold = 120.0;
const double lowGlucoseThreshold = 80.0;
// Insulin response
if (glucose > highGlucoseThreshold) {
insulinSecretion_units_per_hr += (glucose - highGlucoseThreshold) * 0.1 * deltaTime_s;
} else {
insulinSecretion_units_per_hr -= 0.5 * deltaTime_s;
}
// Glucagon response
if (glucose < lowGlucoseThreshold) {
glucagonSecretion_ng_per_hr += (lowGlucoseThreshold - glucose) * 0.2 * deltaTime_s;
} else {
glucagonSecretion_ng_per_hr -= 1.0 * deltaTime_s;
}
// Enzyme secretion would be driven by food in the duodenum (not yet modeled).
// For now, we just simulate minor fluctuations around a baseline.
amylaseSecretion_U_per_L += getFluctuation(0.2);
lipaseSecretion_U_per_L += getFluctuation(0.2);
// Clamp to healthy/possible ranges
insulinSecretion_units_per_hr = std::clamp(insulinSecretion_units_per_hr, 0.5, 10.0);
glucagonSecretion_ng_per_hr = std::clamp(glucagonSecretion_ng_per_hr, 20.0, 100.0);
amylaseSecretion_U_per_L = std::clamp(amylaseSecretion_U_per_L, 60.0, 100.0);
lipaseSecretion_U_per_L = std::clamp(lipaseSecretion_U_per_L, 20.0, 60.0);
}
DigestiveEnzymes Pancreas::releaseEnzymes(double deltaTime_s) {
DigestiveEnzymes enzymes;
enzymes.volume_mL = enzymeReleaseRate_ml_per_s * deltaTime_s;
enzymes.amylase_U_per_L = getAmylaseSecretion();
enzymes.lipase_U_per_L = getLipaseSecretion();
// When stimulated, enzyme production should ramp up
amylaseSecretion_U_per_L += 2.0 * deltaTime_s;
lipaseSecretion_U_per_L += 2.0 * deltaTime_s;
return enzymes;
}
std::string Pancreas::getSummary() const {
std::stringstream ss;
ss.precision(1);
ss << std::fixed;
ss << "--- Pancreas Summary ---\n"
<< "--- Endocrine Function ---\n"
<< "Insulin Secretion: " << getInsulinSecretion() << " units/hr\n"
<< "Glucagon Secretion: " << getGlucagonSecretion() << " ng/hr\n"
<< "--- Exocrine Function ---\n"
<< "Amylase Secretion: " << getAmylaseSecretion() << " U/L\n"
<< "Lipase Secretion: " << getLipaseSecretion() << " U/L\n";
return ss.str();
}
// --- Getters Implementation ---
double Pancreas::getInsulinSecretion() const { return insulinSecretion_units_per_hr; }
double Pancreas::getGlucagonSecretion() const { return glucagonSecretion_ng_per_hr; }
double Pancreas::getAmylaseSecretion() const { return amylaseSecretion_U_per_L; }
double Pancreas::getLipaseSecretion() const { return lipaseSecretion_U_per_L; }