diff --git a/src/Lodur.Core/Physiology.cs b/src/Lodur.Core/Physiology.cs
index 7ccdc68..c7e62c7 100644
--- a/src/Lodur.Core/Physiology.cs
+++ b/src/Lodur.Core/Physiology.cs
@@ -77,6 +77,8 @@ namespace Lodur
private double _urge;
private double _coughed;
private double _drained;
+ private double _drawnLive;
+ private double _drawnPooled;
private bool _atAirway;
private long _steps;
private bool _bound;
@@ -210,6 +212,12 @@ namespace Lodur
/// The liquid in world m^3 the last step drained out of the airway (by gravity through the mouth), including what a full airway spilled.
public double Drained => _drained;
+ /// The liquid in world m^3 the last step drew into the airway from the live liquid at the openings, after the room-left clip; 0 for a held breath, and after a Step on a dead physiology.
+ public double DrawnLive => _drawnLive;
+
+ /// The liquid in world m^3 the last step drew into the airway from the pooled liquid at the openings, after the room-left clip; 0 for a held breath, and after a Step on a dead physiology.
+ public double DrawnPooled => _drawnPooled;
+
/// The tissue's remaining fraction, 0..1, as of the last step.
public double Integrity(int part, int layer) => _integrity[Flat(part, layer)];
@@ -305,7 +313,7 @@ namespace Lodur
///
/// Advances by dt seconds of the caller's clock: afflictions age and change severity (healed ones free their slot), integrity,
/// capacities, bleeding and regrowth follow, then the airway and the gases on the Acute clock, the motor ceilings, and death is judged once. Bleeding is computed before the airway.
- /// A dead physiology does not change, except that its flows (Bleed, AirwayBleed, Coughed, Drained) read 0 after a Step on it. A non-finite input, or one outside [0, 1], is refused before anything changes.
+ /// A dead physiology does not change, except that its flows (Bleed, AirwayBleed, Coughed, Drained, DrawnLive, DrawnPooled) read 0 after a Step on it. A non-finite input, or one outside [0, 1], is refused before anything changes.
///
public void Step(double dt, in PhysiologyInput input = default)
{
@@ -326,6 +334,8 @@ namespace Lodur
_airwayBleed = 0.0;
_coughed = 0.0;
_drained = 0.0;
+ _drawnLive = 0.0;
+ _drawnPooled = 0.0;
return;
}
@@ -379,7 +389,7 @@ namespace Lodur
// 4b. The airway, a quantity (not particles): liquid at the openings is drawn in by a free breath, never by a held one, and only into the room left; blood bled into it enters at the
// blood gain whether or not the breath is held; a cough clears it by this step's consciousness, and it drains by gravity through the mouth. What a full airway cannot hold of the bled blood
- // spills through the mouth. What a step removes is reported as Coughed and Drained; occlusion takes its share of the ventilation below.
+ // spills through the mouth. What a step removes is reported as Coughed and Drained, and what a breath drew as DrawnLive and DrawnPooled; occlusion takes its share of the ventilation below.
var held = input.BreathHeld;
var aw = _airway;
var at = aw.LiveWeight * air.Live + aw.PooledWeight * air.Pooled;
@@ -398,6 +408,10 @@ namespace Lodur
var spill = Math.Max(0.0, _airwayLiquid + bled - removed - aw.Capacity);
_coughed = total > 0.0 ? removed * cough / total : 0.0;
_drained = removed - _coughed + spill;
+ // What the breath really drew: the draw clipped to the room left after what the airway held, bled in and lost (a full airway with nothing removed leaves exactly 0), split by the sample's weighted shares.
+ var drawn = Math.Max(0.0, Math.Min(drawRaw, aw.Capacity - (_airwayLiquid + bled - removed)));
+ _drawnLive = at > 0.0 ? drawn * (aw.LiveWeight * air.Live / at) : 0.0;
+ _drawnPooled = at > 0.0 ? drawn * (aw.PooledWeight * air.Pooled / at) : 0.0;
_airwayLiquid = Math.Min(aw.Capacity, after);
_occlusion = Clamp01(_airwayLiquid / aw.Capacity);
diff --git a/tests/Lodur.Core.Tests/AirwayBleedTests.cs b/tests/Lodur.Core.Tests/AirwayBleedTests.cs
index 577252b..e527af0 100644
--- a/tests/Lodur.Core.Tests/AirwayBleedTests.cs
+++ b/tests/Lodur.Core.Tests/AirwayBleedTests.cs
@@ -135,11 +135,92 @@ public sealed class AirwayBleedTests
Assert.Equal(Quiet.Capacity, pp.AirwayLiquid);
Assert.Equal(0.0, pp.Drained);
Assert.Equal(0.0, pp.Coughed);
+ Assert.Equal(0.0, pp.DrawnLive);
+ Assert.Equal(0.0, pp.DrawnPooled);
}
Assert.Equal(0.0, p.AirwayBleed);
}
+ [Fact]
+ public void A_breath_reports_what_it_drew_split_by_the_airways_weights()
+ {
+ var p = new Physiology(Body3(), Plan3(airway: Quiet), 20.0);
+ p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, new AirwaySample(2e-5, 6e-5, 0.0)));
+ Near(Dt * 2e-5, p.DrawnLive);
+ Near(Dt * 6e-5, p.DrawnPooled);
+
+ var half = new Physiology(Body3(), Plan3(airway: Quiet with { PooledWeight = 0.5 }), 20.0);
+ half.Step(Dt, new PhysiologyInput(0.0, 0.0, false, new AirwaySample(2e-5, 6e-5, 0.0)));
+ Near(Dt * 2e-5, half.DrawnLive);
+ Near(Dt * 3e-5, half.DrawnPooled);
+ }
+
+ [Fact]
+ public void A_nearly_full_airway_draws_only_the_room_left()
+ {
+ var p = new Physiology(Body3(), Plan3(airway: Quiet), 20.0);
+ var wet = new AirwaySample(1e-3, 3e-3, 0.0);
+ for (var i = 0; i < 100 && p.AirwayLiquid < Quiet.Capacity; i++)
+ {
+ var room = Quiet.Capacity - p.AirwayLiquid;
+ p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, wet));
+ if (Dt * 4e-3 > room)
+ {
+ Near(room, p.DrawnLive + p.DrawnPooled);
+ Near(0.25, p.DrawnLive / (p.DrawnLive + p.DrawnPooled));
+ Assert.Equal(Quiet.Capacity, p.AirwayLiquid);
+ }
+ }
+
+ Assert.Equal(Quiet.Capacity, p.AirwayLiquid);
+ }
+
+ [Fact]
+ public void A_held_breath_draws_nothing_while_the_airway_bleeds()
+ {
+ var p = Bleeding(1.0, 10.0);
+ p.Step(Dt, new PhysiologyInput(0.0, 0.0, true, new AirwaySample(1e-4, 1e-4, 0.0)));
+ Assert.Equal(0.0, p.DrawnLive);
+ Assert.Equal(0.0, p.DrawnPooled);
+ Near(Dt * 10.0 * Rate, p.AirwayLiquid);
+ }
+
+ [Fact]
+ public void The_airways_liquid_is_conserved_with_the_draw()
+ {
+ var p = Bleeding(0.5, 10.0, InertAirway, new BloodDef(1.0, 0.5, 0.0));
+ var rng = new Random(7);
+ var drew = 0.0;
+ for (var i = 0; i < 2000; i++)
+ {
+ var before = p.AirwayLiquid;
+ var sample = new AirwaySample(rng.NextDouble() * 1e-4, rng.NextDouble() * 1e-4, rng.NextDouble() * InertAirway.DrainDrop);
+ p.Step(Dt, new PhysiologyInput(0.0, 0.0, rng.NextDouble() < 0.2, sample));
+ Assert.True(p.DrawnLive >= 0.0 && p.DrawnPooled >= 0.0);
+ var expected = Dt * 10.0 * p.AirwayBleed + p.DrawnLive + p.DrawnPooled - p.Coughed - p.Drained;
+ Assert.InRange(Math.Abs(p.AirwayLiquid - before - expected), 0.0, 1e-18);
+ drew += p.DrawnLive + p.DrawnPooled;
+ }
+
+ Assert.True(drew > 0.0);
+ }
+
+ [Fact]
+ public void The_dying_step_reports_its_draw_and_the_next_step_none()
+ {
+ var p = Bleeding(0.5, 0.1, InertAirway);
+ var pooled = new AirwaySample(0.0, 1e-6, InertAirway.DrainDrop);
+ for (var i = 0; i < 100000 && !p.Dead; i++) p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, pooled));
+ Assert.True(p.Dead);
+ Assert.True(p.DrawnPooled > 0.0);
+ Assert.Equal(0.0, p.DrawnLive);
+
+ p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, pooled));
+ Assert.Equal(0.0, p.DrawnLive);
+ Assert.Equal(0.0, p.DrawnPooled);
+ }
+
[Fact]
public void The_dying_step_reports_its_flows_and_the_next_step_none()
{