Airway tissue bleeds into the airway at the caller's blood gain; a full airway spills through the mouth
TissueDef.AirwayShare splits each wound's bleed into Bleed (surface) and AirwayBleed (airway). Physiology takes bloodGain (default 1). Bleeding runs before the airway; a breath draws only into the room left; the dying step reports its flows and the next step zeroes them. Larynx and Trachea tissues added; lungs share 0.5. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
47605e1e68
commit
1fe6606fa8
5 changed files with 262 additions and 37 deletions
|
|
@ -36,7 +36,7 @@ namespace Lodur
|
|||
/// The body's physiology: tissue integrity, bound afflictions, blood and its oxygen, the capacities they fold into, the motor ceiling and death.
|
||||
/// Harm enters only through <see cref="Afflict"/>; time through <see cref="Step"/>. Integrity is derived from the severities of the
|
||||
/// Injures afflictions bound to a tissue (part HP is never stored), and capacities, bleed and the motor ceiling are recomputed each step;
|
||||
/// nobody writes them; the gases are integrated by Step and nobody writes those either. Content owns every label and number (<see cref="PhysiologyPlan"/>). Everything is allocated in the constructor.
|
||||
/// nobody writes them. A bleed leaves the circulation once, through the part's surface (<see cref="Bleed"/>) or into the airway (<see cref="AirwayBleed"/>), split by each tissue's <see cref="TissueDef.AirwayShare"/>; the gases are integrated by Step and nobody writes those either. Content owns every label and number (<see cref="PhysiologyPlan"/>). Everything is allocated in the constructor.
|
||||
/// </summary>
|
||||
public sealed class Physiology
|
||||
{
|
||||
|
|
@ -61,7 +61,9 @@ namespace Lodur
|
|||
private readonly int[] _slotTissue; // flattened
|
||||
private readonly double[] _slotSeverity;
|
||||
private readonly double[] _slotAge;
|
||||
private readonly double[] _bleed; // per part, m^3/s
|
||||
private readonly double[] _bleed; // per part, m^3/s, through the surface
|
||||
private readonly double _bloodGain;
|
||||
private double _airwayBleed; // m^3/s into the airway
|
||||
private readonly double[] _motor; // per joint
|
||||
private double _volume;
|
||||
private double _consciousness;
|
||||
|
|
@ -79,9 +81,12 @@ namespace Lodur
|
|||
private long _steps;
|
||||
private bool _bound;
|
||||
|
||||
public Physiology(BodyPlan body, PhysiologyPlan plan, double slowScale)
|
||||
/// <param name="bloodGain">The world's liquid volume per m^3 of blood, owned by the caller: blood bled into the airway enters it at this multiple of its real volume. Finite and above 0.</param>
|
||||
public Physiology(BodyPlan body, PhysiologyPlan plan, double slowScale, double bloodGain = 1.0)
|
||||
{
|
||||
if (!(slowScale > 0.0 && !double.IsInfinity(slowScale))) throw new ArgumentException("the slow scale must be finite and above 0.");
|
||||
if (!(bloodGain > 0.0 && !double.IsInfinity(bloodGain))) throw new ArgumentException("the blood gain must be finite and above 0.");
|
||||
_bloodGain = bloodGain;
|
||||
_body = body;
|
||||
_slowScale = slowScale;
|
||||
_nominal = plan.Blood.NominalVolume;
|
||||
|
|
@ -199,17 +204,20 @@ namespace Lodur
|
|||
/// <summary>True when the liquid at the airway the last step was sampled was at least the airway's HoldAt: the Actor holds the breath while it is.</summary>
|
||||
public bool LiquidAtAirway => _atAirway;
|
||||
|
||||
/// <summary>The liquid in m^3 the last step coughed out of the airway.</summary>
|
||||
/// <summary>The liquid in world m^3 the last step coughed out of the airway.</summary>
|
||||
public double Coughed => _coughed;
|
||||
|
||||
/// <summary>The liquid in m^3 the last step drained out of the airway (by gravity through the mouth).</summary>
|
||||
/// <summary>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.</summary>
|
||||
public double Drained => _drained;
|
||||
|
||||
/// <summary>The tissue's remaining fraction, 0..1, as of the last step.</summary>
|
||||
public double Integrity(int part, int layer) => _integrity[Flat(part, layer)];
|
||||
|
||||
/// <summary>The part's blood loss in m^3/s as of the last step; 0 once dead.</summary>
|
||||
public double Bleed(int part) => Dead ? 0.0 : _bleed[part];
|
||||
/// <summary>The blood lost through the part's surface in real m^3/s over the last Step that ran, the dying step included; 0 after a Step on a dead physiology.</summary>
|
||||
public double Bleed(int part) => _bleed[part];
|
||||
|
||||
/// <summary>The blood bled into the airway in real m^3/s over the last Step that ran (same lifetime as <see cref="Bleed"/>); it enters the airway at the blood gain.</summary>
|
||||
public double AirwayBleed => _airwayBleed;
|
||||
|
||||
/// <summary>The ceiling on a joint's muscle strength, 0..1: its capacity times consciousness; 0 once dead.</summary>
|
||||
public double MotorCeiling(int joint) => Dead ? 0.0 : _motor[joint];
|
||||
|
|
@ -296,8 +304,8 @@ namespace Lodur
|
|||
|
||||
/// <summary>
|
||||
/// 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 gases on the Acute clock, the motor ceilings, and death is judged once.
|
||||
/// A dead physiology does not change. A non-finite input, or one outside [0, 1], is refused before anything changes.
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public void Step(double dt, in PhysiologyInput input = default)
|
||||
{
|
||||
|
|
@ -312,7 +320,15 @@ namespace Lodur
|
|||
throw new ArgumentException("the airway sample's liquid must be finite and not negative and its drop finite and not negative.");
|
||||
}
|
||||
|
||||
if (Dead) return;
|
||||
if (Dead)
|
||||
{
|
||||
Array.Clear(_bleed, 0, _bleed.Length);
|
||||
_airwayBleed = 0.0;
|
||||
_coughed = 0.0;
|
||||
_drained = 0.0;
|
||||
return;
|
||||
}
|
||||
|
||||
var slowDt = dt * _slowScale;
|
||||
|
||||
// 1. Each live affliction on its own clock.
|
||||
|
|
@ -342,8 +358,28 @@ namespace Lodur
|
|||
_pumping = Capacity(EffectTarget.BloodPumping);
|
||||
_breathing = Capacity(EffectTarget.Breathing);
|
||||
|
||||
// 3b. The airway, a quantity (not particles): liquid at the openings is drawn in by a free breath, never by a held one; a cough clears it by this step's
|
||||
// consciousness, and it drains by gravity through the mouth. What a step removes is reported as Coughed and Drained; occlusion takes its share of the ventilation below.
|
||||
// 4. Bleeding: each affliction's bleed effects, through its tissue's vascularity and the pumping; the tissue's airway share enters the airway, the rest leaves through the part's surface.
|
||||
Array.Clear(_bleed, 0, _bleed.Length);
|
||||
var totalBleed = 0.0;
|
||||
var internalBleed = 0.0;
|
||||
for (var s = 0; s < _slotDef.Length; s++)
|
||||
{
|
||||
if (_slotDef[s] < 0) continue;
|
||||
double offset = 0.0, factor = 1.0;
|
||||
Fold(_defs[_slotDef[s]].Effects, EffectTarget.Bleed, _slotSeverity[s], _slotAge[s], ref offset, ref factor);
|
||||
var tissue = _tissues[_slotTissue[s]];
|
||||
var rate = Math.Max(0.0, offset * factor) * tissue.Vascularity * _pumping;
|
||||
_bleed[_slotPart[s]] += rate * (1.0 - tissue.AirwayShare);
|
||||
internalBleed += rate * tissue.AirwayShare;
|
||||
totalBleed += rate;
|
||||
}
|
||||
|
||||
_airwayBleed = internalBleed;
|
||||
_volume -= totalBleed * dt;
|
||||
|
||||
// 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.
|
||||
var held = input.BreathHeld;
|
||||
var aw = _airway;
|
||||
var at = aw.LiveWeight * air.Live + aw.PooledWeight * air.Pooled;
|
||||
|
|
@ -352,29 +388,19 @@ namespace Lodur
|
|||
var cough = aw.CoughPerSecond * Effect.EvalCurve(aw.CoughByConsciousness, _consciousness);
|
||||
var drain = aw.DrainPerSecond * Clamp01(air.Drop / aw.DrainDrop);
|
||||
var total = cough + drain;
|
||||
var avail = _airwayLiquid + dt * inflow;
|
||||
var bled = dt * _bloodGain * _airwayBleed;
|
||||
var drawRaw = dt * inflow;
|
||||
var avail = _airwayLiquid + bled + drawRaw;
|
||||
var want = dt * total * _airwayLiquid;
|
||||
var removed = want < avail ? want : avail;
|
||||
var after = avail - removed;
|
||||
// Only bled blood overflows: a breath draws into the room left, so the spill is what the airway held plus what bled in, less what left, above capacity (after - capacity - drawRaw, ordered so a full airway with no bleed spills exactly 0).
|
||||
var spill = Math.Max(0.0, _airwayLiquid + bled - removed - aw.Capacity);
|
||||
_coughed = total > 0.0 ? removed * cough / total : 0.0;
|
||||
_drained = removed - _coughed;
|
||||
_airwayLiquid = Math.Min(aw.Capacity, avail - removed);
|
||||
_drained = removed - _coughed + spill;
|
||||
_airwayLiquid = Math.Min(aw.Capacity, after);
|
||||
_occlusion = Clamp01(_airwayLiquid / aw.Capacity);
|
||||
|
||||
// 4. Bleeding: each affliction's bleed effects, through its tissue's vascularity and the pumping.
|
||||
Array.Clear(_bleed, 0, _bleed.Length);
|
||||
var totalBleed = 0.0;
|
||||
for (var s = 0; s < _slotDef.Length; s++)
|
||||
{
|
||||
if (_slotDef[s] < 0) continue;
|
||||
double offset = 0.0, factor = 1.0;
|
||||
Fold(_defs[_slotDef[s]].Effects, EffectTarget.Bleed, _slotSeverity[s], _slotAge[s], ref offset, ref factor);
|
||||
var rate = Math.Max(0.0, offset * factor) * _tissues[_slotTissue[s]].Vascularity * _pumping;
|
||||
_bleed[_slotPart[s]] += rate;
|
||||
totalBleed += rate;
|
||||
}
|
||||
|
||||
_volume -= totalBleed * dt;
|
||||
|
||||
// 5. Regrowth, on the slow clock, only while nothing bleeds.
|
||||
if (totalBleed == 0.0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -40,6 +40,9 @@ namespace Lodur
|
|||
{
|
||||
public double Thickness { get; init; }
|
||||
public SheetDef? Sheet { get; init; }
|
||||
|
||||
/// <summary>The fraction, 0..1, of a wound's bleed in this tissue that enters the airway instead of leaving through the part's surface (the larynx, trachea and lungs' mucosa); 0 for every other tissue.</summary>
|
||||
public double AirwayShare { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>The one tissue stack of a part, outside-in, owned by <see cref="PhysiologyPlan.Parts"/> and joined to a <see cref="BodyPlan"/> only through <see cref="PhysiologyPlan.Resolve"/>.</summary>
|
||||
|
|
@ -68,7 +71,8 @@ namespace Lodur
|
|||
/// and the Actor holds the breath. A free breath draws <paramref name="DrawPerSecond"/> x Breathing x (liquid at the airway) per second into it; a cough
|
||||
/// removes <paramref name="CoughPerSecond"/> x <paramref name="CoughByConsciousness"/>(consciousness) x contents per second; and it drains at
|
||||
/// <paramref name="DrainPerSecond"/> x clamp01(drop / <paramref name="DrainDrop"/>) x contents, drop being how far (m) the lowest opening's rim lies below the deepest airway sump along
|
||||
/// gravity: <paramref name="DrainDrop"/> m of drop drains at the full rate.
|
||||
/// gravity: <paramref name="DrainDrop"/> m of drop drains at the full rate. The airway holds liquid in the world's units (what the openings sample); blood bled into it
|
||||
/// (<see cref="TissueDef.AirwayShare"/>) enters at the physiology's blood gain; a breath draws only into the room left, and what bleeds into a full airway spills through the mouth and is reported as drained.
|
||||
/// </summary>
|
||||
public readonly record struct AirwayDef(
|
||||
double Capacity, double DrawPerSecond, double LiveWeight, double PooledWeight, double HoldAt, double CoughPerSecond,
|
||||
|
|
@ -221,6 +225,7 @@ namespace Lodur
|
|||
if (!seen.Add(t.Label)) throw new ArgumentException($"{where}: the label is not unique within the part.");
|
||||
if (!(IsFinite(t.Hp) && t.Hp > 0.0)) throw new ArgumentException($"{where}: Hp is not finite and above 0.");
|
||||
if (!(IsFinite(t.Vascularity) && t.Vascularity >= 0.0)) throw new ArgumentException($"{where}: vascularity is not finite and at least 0.");
|
||||
if (!(IsFinite(t.AirwayShare) && t.AirwayShare >= 0.0 && t.AirwayShare <= 1.0)) throw new ArgumentException($"{where}: the airway share is not finite and in [0, 1].");
|
||||
if (!(IsFinite(t.Consciousness) && t.Consciousness >= 0.0 && IsFinite(t.BloodPumping) && t.BloodPumping >= 0.0 && IsFinite(t.Motor) && t.Motor >= 0.0 && IsFinite(t.Breathing) && t.Breathing >= 0.0))
|
||||
{
|
||||
throw new ArgumentException($"{where}: a function weight is not finite and at least 0.");
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ namespace Lodur
|
|||
// Vascularity is the blood a wound in the tissue loses relative to muscle (play-tuned placeholders): skin and bone are thin, organs rich.
|
||||
private const double SkinV = 0.2, MuscleV = 1.0, BoneV = 0.3, BrainV = 1.0, HeartV = 4.0, LungV = 2.0, LiverV = 3.0, KidneyV = 2.0, GutV = 1.0;
|
||||
|
||||
// Airway tissues: mucosa as perfused as muscle (placeholders). AirwayShare is the fraction of a wound's bleed that enters the airway: all of the larynx's and trachea's, half a lung's (half coughed up, half stays in the chest).
|
||||
private const double LarynxV = 1.0, TracheaV = 1.0, MucosaShare = 1.0, LungShare = 0.5;
|
||||
|
||||
// Fat is thickness in the stack, not particles; its vascularity is a placeholder.
|
||||
private const double FatV = 0.1;
|
||||
|
||||
|
|
@ -43,10 +46,13 @@ namespace Lodur
|
|||
Stack("Torso", Side.Center,
|
||||
Skin(10), Fat(10), MuscleSheet(40), Bone("Ribcage", 25),
|
||||
new TissueDef("Heart", 15, HeartV, BloodPumping: 1.0),
|
||||
new TissueDef("LungLeft", 15, LungV, Breathing: 1.0), new TissueDef("LungRight", 15, LungV, Breathing: 1.0),
|
||||
new TissueDef("Liver", 20, LiverV), new TissueDef("KidneyLeft", 15, KidneyV), new TissueDef("KidneyRight", 15, KidneyV));
|
||||
new TissueDef("LungLeft", 15, LungV, Breathing: 1.0) { AirwayShare = LungShare }, new TissueDef("LungRight", 15, LungV, Breathing: 1.0) { AirwayShare = LungShare },
|
||||
new TissueDef("Liver", 20, LiverV), new TissueDef("KidneyLeft", 15, KidneyV), new TissueDef("KidneyRight", 15, KidneyV),
|
||||
// The trachea sits at Torso's top in the 19-part plan, the larynx in Head's lower region; where a cut reaches them is follow-up lodur-wound-reach.
|
||||
new TissueDef("Trachea", 10, TracheaV) { AirwayShare = MucosaShare });
|
||||
// The neck's muscle carries the Neck joint's motor weight: the head has to be a joint child with a muscle (spec discrepancy, report deviation).
|
||||
Stack("Head", Side.Center, Skin(5), Fat(5), Muscle(25), Bone("Skull", 25), new TissueDef("Brain", 10, BrainV, Vital: true, Consciousness: 1.0));
|
||||
Stack("Head", Side.Center, Skin(5), Fat(5), Muscle(25), Bone("Skull", 25), new TissueDef("Brain", 10, BrainV, Vital: true, Consciousness: 1.0),
|
||||
new TissueDef("Larynx", 10, LarynxV) { AirwayShare = MucosaShare });
|
||||
foreach (var side in new[] { Side.Left, Side.Right })
|
||||
{
|
||||
Stack("UpperArm", side, Skin(8), Fat(8), MuscleSheet(30), Bone("Humerus", 25));
|
||||
|
|
|
|||
185
tests/Lodur.Core.Tests/AirwayBleedTests.cs
Normal file
185
tests/Lodur.Core.Tests/AirwayBleedTests.cs
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
using Lodur;
|
||||
using static Lodur.Core.Tests.PhysioFixtures;
|
||||
|
||||
namespace Lodur.Core.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// A wound in an airway tissue bleeds into the airway: the bleed splits at the tissue by its AirwayShare, the airway share enters at the caller's blood gain, and a full airway spills
|
||||
/// through the mouth (idea bds-slice-map.gain-at-circulation-boundary; rulings bds-drown-in-own-blood-first, bds-blood-gain-simulated, bds-airway-cough-hold-drain).
|
||||
/// The rule tests run on Body3 / Plan3 with a root muscle given an AirwayShare and a Bleeder (1e-3 m^3/s at severity 1, vascularity 1) on it.
|
||||
/// </summary>
|
||||
public sealed class AirwayBleedTests
|
||||
{
|
||||
private const double Dt = 0.01;
|
||||
private const double Rate = 1e-3;
|
||||
|
||||
/// <summary>A cough-free, drain-free airway with a breath that draws the liquid at the openings and holds at 1e-3 of it.</summary>
|
||||
private static AirwayDef Quiet => new(150e-6, 1.0, 1.0, 1.0, 1e-3, 0.0, new[] { (0.0, 0.0), (1.0, 0.0) }, 0.0, 0.05);
|
||||
|
||||
private static void Near(double expected, double actual) => Assert.InRange(Math.Abs(expected - actual), 0.0, 1e-12 * Math.Abs(expected));
|
||||
|
||||
private static Physiology Bleeding(double share, double gain, AirwayDef? airway = null, BloodDef? blood = null)
|
||||
{
|
||||
var root = new[]
|
||||
{
|
||||
new TissueDef("brain", 10, 1, Consciousness: 1),
|
||||
new TissueDef("heart", 10, 4, true, BloodPumping: 1),
|
||||
new TissueDef("muscle", 10, 1, Motor: 1) { AirwayShare = share },
|
||||
};
|
||||
var p = new Physiology(Body3(), Plan3(root: root, airway: airway ?? Quiet, blood: blood), 20.0, gain);
|
||||
Assert.True(p.Afflict(Af("Bleeder"), 0, RootMuscle, 1));
|
||||
return p;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.25)]
|
||||
[InlineData(0.0)]
|
||||
[InlineData(1.0)]
|
||||
public void A_tissues_bleed_splits_by_its_airway_share_and_the_blood_falls_by_both(double share)
|
||||
{
|
||||
var p = Bleeding(share, 1.0);
|
||||
p.Step(Dt);
|
||||
Assert.Equal(Rate * (1.0 - share), p.Bleed(0), 15);
|
||||
Assert.Equal(Rate * share, p.AirwayBleed, 15);
|
||||
Assert.Equal(0.0, p.Bleed(1));
|
||||
Assert.Equal(0.0, p.Bleed(2));
|
||||
var drop = 0.005 - p.BloodVolume;
|
||||
Assert.Equal(Dt * (p.Bleed(0) + p.Bleed(1) + p.Bleed(2) + p.AirwayBleed), drop, 15);
|
||||
Assert.Equal(Dt * Rate, drop, 15);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1.0, false)]
|
||||
[InlineData(10.0, false)]
|
||||
[InlineData(1.0, true)]
|
||||
[InlineData(10.0, true)]
|
||||
public void Blood_enters_the_airway_at_the_gain_held_breath_or_not(double gain, bool held)
|
||||
{
|
||||
var p = Bleeding(1.0, gain);
|
||||
p.Step(Dt, new PhysiologyInput(0.0, 0.0, held));
|
||||
Near(Dt * gain * p.AirwayBleed, p.AirwayLiquid);
|
||||
Near(Dt * gain * Rate, p.AirwayLiquid);
|
||||
Assert.Equal(0.0, p.Coughed);
|
||||
Assert.Equal(0.0, p.Drained);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_full_airway_spills_what_bleeds_into_it_through_the_mouth()
|
||||
{
|
||||
// Share 1, gain 10, a drain the sample opens (Drop at DrainDrop): fill, then 600 more steps.
|
||||
var air = Quiet with { DrainPerSecond = 0.1 };
|
||||
var p = Bleeding(1.0, 10.0, air, new BloodDef(1.0, 0.5, 0.0));
|
||||
var drop = new AirwaySample(0.0, 0.0, air.DrainDrop);
|
||||
double bledIn = 0.0, out_ = 0.0;
|
||||
var steps = 0;
|
||||
for (; p.AirwayLiquid < air.Capacity && steps < 100000; steps++) Step();
|
||||
Assert.Equal(air.Capacity, p.AirwayLiquid);
|
||||
for (var i = 0; i < 600; i++) Step();
|
||||
Assert.Equal(air.Capacity, p.AirwayLiquid);
|
||||
Assert.True(out_ > 0.0);
|
||||
Near(10.0 * bledIn, out_ + p.AirwayLiquid);
|
||||
|
||||
void Step()
|
||||
{
|
||||
p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, drop));
|
||||
bledIn += p.AirwayBleed * Dt;
|
||||
out_ += p.Coughed + p.Drained;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_breath_draws_only_into_the_room_left()
|
||||
{
|
||||
var p = Make(); // no wound: nothing bleeds
|
||||
var pp = new Physiology(Body3(), Plan3(airway: Quiet), 20.0);
|
||||
var wet = new AirwaySample(1e-3, 0.0, 0.0);
|
||||
for (var i = 0; i < 100 && pp.AirwayLiquid < Quiet.Capacity; i++) pp.Step(Dt, new PhysiologyInput(0.0, 0.0, false, wet));
|
||||
Assert.Equal(Quiet.Capacity, pp.AirwayLiquid);
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
pp.Step(Dt, new PhysiologyInput(0.0, 0.0, false, wet));
|
||||
Assert.Equal(Quiet.Capacity, pp.AirwayLiquid);
|
||||
Assert.Equal(0.0, pp.Drained);
|
||||
Assert.Equal(0.0, pp.Coughed);
|
||||
}
|
||||
|
||||
Assert.Equal(0.0, p.AirwayBleed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_dying_step_reports_its_flows_and_the_next_step_none()
|
||||
{
|
||||
var p = Bleeding(0.5, 1.0, InertAirway); // cough 2/s and a drain, so Coughed and Drained read above 0 while it bleeds into the airway
|
||||
var drop = new AirwaySample(0.0, 0.0, InertAirway.DrainDrop);
|
||||
double lost = 0.0;
|
||||
for (var i = 0; i < 100000 && !p.Dead; i++)
|
||||
{
|
||||
p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, drop));
|
||||
lost += Dt * (p.Bleed(0) + p.Bleed(1) + p.Bleed(2) + p.AirwayBleed);
|
||||
}
|
||||
|
||||
Assert.True(p.Dead);
|
||||
Assert.Equal(DeathCause.BloodLoss, p.Cause);
|
||||
Assert.True(p.Bleed(0) > 0.0 && p.AirwayBleed > 0.0);
|
||||
Near(0.005 - p.BloodVolume, lost);
|
||||
Assert.True(p.Coughed > 0.0 && p.Drained > 0.0);
|
||||
|
||||
p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, drop));
|
||||
Assert.Equal(0.0, p.Bleed(0));
|
||||
Assert.Equal(0.0, p.AirwayBleed);
|
||||
Assert.Equal(0.0, p.Coughed);
|
||||
Assert.Equal(0.0, p.Drained);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_cut_larynx_fills_the_humanoids_airway_at_the_gain()
|
||||
{
|
||||
var body = BodyPlans.Humanoid();
|
||||
var plan = PhysiologyPlans.Humanoid();
|
||||
var head = body.PartIndex("Head");
|
||||
var cut = plan.AfflictionIndex("Cut");
|
||||
|
||||
double Seconds(double gain)
|
||||
{
|
||||
var p = new Physiology(body, plan, 20.0, gain);
|
||||
Assert.True(p.Afflict(cut, head, p.TissueIndex(head, "Larynx"), 60));
|
||||
var input = new PhysiologyInput(0.5, 0.5);
|
||||
for (var i = 0; i < 3.05 * 120; i++)
|
||||
{
|
||||
p.Step(1.0 / 120.0, input);
|
||||
if (p.Occlusion >= 0.99) return (i + 1) / 120.0;
|
||||
}
|
||||
|
||||
return double.PositiveInfinity;
|
||||
}
|
||||
|
||||
Assert.True(Seconds(10.0) <= 3.05);
|
||||
Assert.True(double.IsPositiveInfinity(Seconds(1.0))); // at gain 1 it takes 31 s (probe): a dropped gain misses the bound
|
||||
|
||||
// Larynx and Trachea are cores with share 1, the lungs 0.5, every other tissue 0.
|
||||
foreach (var part in plan.Parts)
|
||||
{
|
||||
foreach (var t in part.Tissues)
|
||||
{
|
||||
if (t.Label is "Larynx" or "Trachea") { Assert.Equal(0.0, t.Thickness); Assert.Equal(1.0, t.AirwayShare); }
|
||||
else if (t.Label is "LungLeft" or "LungRight") Assert.Equal(0.5, t.AirwayShare);
|
||||
else Assert.Equal(0.0, t.AirwayShare);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Shares_and_gains_the_physiology_cannot_run_on_are_refused()
|
||||
{
|
||||
foreach (var bad in new[] { -0.1, 1.1, double.NaN, double.PositiveInfinity })
|
||||
{
|
||||
var root = new[] { new TissueDef("brain", 10, 1, Consciousness: 1) { AirwayShare = bad } };
|
||||
Assert.Throws<ArgumentException>(() => Plan3(root: root));
|
||||
}
|
||||
|
||||
foreach (var bad in new[] { 0.0, -1.0, double.PositiveInfinity, double.NaN })
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Physiology(Body3(), Plan3(), 20.0, bad));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -424,7 +424,7 @@ public sealed class PhysiologyTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void A_dead_body_reads_no_bleed_whatever_it_was_bleeding()
|
||||
public void A_dead_body_reads_the_dying_steps_bleed_and_then_none()
|
||||
{
|
||||
var p = Make();
|
||||
Afflict(p, "Bleeder", 1, 0, 1);
|
||||
|
|
@ -432,7 +432,9 @@ public sealed class PhysiologyTests
|
|||
p.Step(0.01);
|
||||
Assert.True(p.Dead);
|
||||
Assert.Equal(DeathCause.Consciousness, p.Cause);
|
||||
Assert.Equal(0.0, p.Bleed(1));
|
||||
Assert.Equal(1e-3, p.Bleed(1), 15); // the dying step's own bleed: the last Step that ran
|
||||
p.Step(0.01);
|
||||
Assert.Equal(0.0, p.Bleed(1)); // a Step on the dead zeroes the flows
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -576,7 +578,7 @@ public sealed class PhysiologyTests
|
|||
Assert.Equal(0, p.DiedAtStep);
|
||||
Assert.Equal(0.0, p.Consciousness);
|
||||
Assert.Equal(0.0, p.BloodPumping);
|
||||
Assert.Equal(0.0, p.Bleed(1));
|
||||
Assert.Equal(0.0, p.Bleed(1)); // the heart is the cause here: pumping 0 on the dying step, so nothing bleeds
|
||||
Assert.Equal(0.0, p.MotorCeiling(0));
|
||||
Assert.Equal(0.0, p.MotorCeiling(1));
|
||||
|
||||
|
|
@ -586,6 +588,7 @@ public sealed class PhysiologyTests
|
|||
for (var s = 0; s < p.SlotCount; s++) { severity[s] = p.SlotSeverity(s); age[s] = p.SlotAge(s); }
|
||||
var integrity = p.Integrity(0, Heart);
|
||||
for (var i = 0; i < 100; i++) p.Step(1.0);
|
||||
Assert.Equal(0.0, p.Bleed(1)); // one Step on the dead zeroes the flows
|
||||
Assert.Equal(volume, p.BloodVolume);
|
||||
Assert.Equal(0, p.DiedAtStep);
|
||||
Assert.Equal(DeathCause.VitalTissue, p.Cause);
|
||||
|
|
|
|||
Loading…
Reference in a new issue