diff --git a/src/Lodur.Core/Physiology.cs b/src/Lodur.Core/Physiology.cs
index c7e62c7..d196d20 100644
--- a/src/Lodur.Core/Physiology.cs
+++ b/src/Lodur.Core/Physiology.cs
@@ -406,7 +406,8 @@ namespace Lodur
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;
+ // Coughed is capped at removed: for a cough that is not a power of two removed x cough / total can exceed removed by an ulp, and Drained (removed - Coughed + spill) would read a negative, which no consumer can take.
+ _coughed = total > 0.0 ? Math.Min(removed, 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)));
diff --git a/src/Lodur.Core/PhysiologyPlan.cs b/src/Lodur.Core/PhysiologyPlan.cs
index a59e6e4..9fd23d2 100644
--- a/src/Lodur.Core/PhysiologyPlan.cs
+++ b/src/Lodur.Core/PhysiologyPlan.cs
@@ -56,16 +56,14 @@ namespace Lodur
{
///
/// The layers a wound of (m, finite and above 0) at (the part's frame) reaches, written into as stack indices in stack order; returns the count.
- /// Shells (Thickness above 0) outside-in: shell i is reached iff depth exceeds the summed Thickness of the shells before it. With S the shells' total and B the summed Thickness of the shells listed before a core:
- /// an unsited core is reached iff depth exceeds S; a sited one iff depth exceeds B and depth exceeds the distance from point to the Site's centre less its radius (B is S for a site listed after every shell). Strict: a depth equal to a boundary does not reach past it. Allocates nothing.
+ /// Stack order is depth order: with B the summed Thickness of the shells listed before a tissue, a shell is reached iff depth exceeds B, and a core iff depth exceeds B and (it has no Site, or depth exceeds the distance from point to the Site's centre less its radius).
+ /// The validator refuses a shell after an unsited core, so an unsited core's B is every shell's total. Strict: a depth equal to a boundary does not reach past it. Allocates nothing.
///
public int Reach((float X, float Y, float Z) point, double depth, Span layers)
{
if (!(double.IsFinite(depth) && depth > 0.0)) throw new ArgumentException("depth is not finite and above 0.");
if (!(float.IsFinite(point.X) && float.IsFinite(point.Y) && float.IsFinite(point.Z))) throw new ArgumentException("the point is not finite.");
if (layers.Length < Tissues.Count) throw new ArgumentException("layers is shorter than the stack.");
- var shells = 0.0;
- for (var i = 0; i < Tissues.Count; i++) shells += Tissues[i].Thickness;
var n = 0;
var before = 0.0;
for (var i = 0; i < Tissues.Count; i++)
@@ -78,11 +76,9 @@ namespace Lodur
}
else
{
- if (t.Site is not TissueSite site)
- {
- if (depth > shells) layers[n++] = i;
- }
- else if (depth > before)
+ if (!(depth > before)) continue;
+ if (t.Site is not TissueSite site) layers[n++] = i;
+ else
{
double dx = point.X - site.Centre.X, dy = point.Y - site.Centre.Y, dz = point.Z - site.Centre.Z;
if (depth > Math.Sqrt(dx * dx + dy * dy + dz * dz) - site.Radius) layers[n++] = i;
diff --git a/tests/Lodur.Core.Tests/AirwayBleedTests.cs b/tests/Lodur.Core.Tests/AirwayBleedTests.cs
index 4899cae..c83cc48 100644
--- a/tests/Lodur.Core.Tests/AirwayBleedTests.cs
+++ b/tests/Lodur.Core.Tests/AirwayBleedTests.cs
@@ -121,6 +121,35 @@ public sealed class AirwayBleedTests
}
}
+ [Fact]
+ public void Every_airway_flow_is_at_least_zero_at_any_cough_rate()
+ {
+ // Drained was removed - Coughed + spill, with Coughed = removed x cough / total: for a cough that is not a power of two the product can exceed removed by an ulp and Drained read -5e-23 (BDS refuses a negative drain).
+ // Fill the airway, then clear it with drain on and off, and read every flow every step.
+ var humanoid = PhysiologyPlans.Humanoid().Airway;
+ var rates = new List { 0.3, 0.36, 0.24, 2.0, humanoid.CoughPerSecond };
+ for (var k = 1; k <= 300; k++) rates.Add(0.01 * k);
+ foreach (var rate in rates)
+ {
+ foreach (var drop in new[] { 0.0, 0.5 * humanoid.DrainDrop, humanoid.DrainDrop })
+ {
+ var air = humanoid with { CoughPerSecond = rate };
+ var p = new Physiology(Body3(), Plan3(airway: air), 20.0);
+ var flood = new AirwaySample(100.0 * air.HoldAt, 0.0, drop);
+ var clear = new AirwaySample(0.0, 0.0, drop);
+ for (var i = 0; i < 1500; i++)
+ {
+ p.Step(Dt, new PhysiologyInput(0.0, 0.0, false, i < 100 ? flood : clear));
+ var at = $"cough {rate} drop {drop} step {i}";
+ Assert.True(p.Coughed >= 0.0, $"{at}: Coughed {p.Coughed:R}");
+ Assert.True(p.Drained >= 0.0, $"{at}: Drained {p.Drained:R}");
+ Assert.True(p.AirwayBleed >= 0.0 && p.DrawnLive >= 0.0 && p.DrawnPooled >= 0.0, $"{at}: a flow is negative");
+ Assert.True(p.AirwayLiquid >= 0.0, $"{at}: liquid {p.AirwayLiquid:R}");
+ }
+ }
+ }
+ }
+
[Fact]
public void A_breath_draws_only_into_the_room_left()
{
diff --git a/tests/Lodur.Core.Tests/AirwayPhysiologyTests.cs b/tests/Lodur.Core.Tests/AirwayPhysiologyTests.cs
index f9d9df1..66d9023 100644
--- a/tests/Lodur.Core.Tests/AirwayPhysiologyTests.cs
+++ b/tests/Lodur.Core.Tests/AirwayPhysiologyTests.cs
@@ -275,22 +275,25 @@ public sealed class AirwayPhysiologyTests
public void An_awake_Fella_coughs_his_airway_clear()
{
// Placeholders deciding this: Cough (0.3 per second, a continuous airway bleed must beat it: ruling bds-drown-in-own-blood-first) and every bound below; Draw only fills the half airway first and decides no outcome; Drain is 0 face up and decides nothing.
- // Half the airway, awake, face up, nothing at the openings, 20 s. Readings (deterministic): shipped liquid 1.9e-7 m^3 (half the airway down to 0.25%, quarter at 2.4 s), a cough at half the rate 3.86e-6 (quarter at 4.9 s), no cough 7.8e-5.
- // Liquid bound 8.6e-7 = sqrt(1.9e-7 x 3.86e-6), separation 20 (ruling deterministic-bound-margin); the half-rate cough is claimed. Coughed total (fraction of capacity): shipped 0.515, no cough 0 (one unit 0.01,
- // midpoint 0.07): the bound stays at 0.4, tighter than that midpoint, 1.29 under the shipped reading; a half-rate cough (0.493) is not claimed by this metric, the liquid bound claims it.
+ // Half the airway, awake, face up, nothing at the openings, 20 s. Readings (deterministic, ruling deterministic-bound-margin): the time for the liquid to fall to a quarter of the capacity (half of what it held)
+ // shipped 2.425 s, a cough at half the rate 4.867 s, cough 2.0 0.417 s; the liquid left after 20 s shipped 1.9e-7 m^3, half-rate 3.864e-6, no cough 7.8e-5; the total coughed (fraction of the capacity) shipped 0.515, no cough 0.
+ // Time bound (1.005, 3.436) = sqrt(2.425 x 0.417) and sqrt(2.425 x 4.867), separation 5.8 and 2.0: it claims cough 2.0 and the half-rate cough (a cough of 0.36 or 0.24 is not claimed). Liquid bound 8.6e-7 = sqrt(1.9e-7 x 3.864e-6),
+ // separation 20: it claims the half-rate and the missing cough. Coughed bound 0.0718 = sqrt(0.515 x 0.01), the zero reading's mutant counted as one unit of 0.01 (ruling deterministic-bound-zero-reading), separation 7.2.
var p = Breather(20.0);
var flood = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(100.0 * HoldAt, 0.0, 0.0));
while (p.AirwayLiquid < 0.5 * Capacity) p.Step(Dt, flood);
var clear = new PhysiologyInput(0.0, 0.0, false, new AirwaySample(0.0, 0.0, 0.0));
- double coughed = 0.0;
+ double coughed = 0.0, quarter = -1.0;
for (long i = 0; i < (long)(20.0 / Dt); i++)
{
p.Step(Dt, clear);
coughed += p.Coughed;
+ if (quarter < 0.0 && p.AirwayLiquid < 0.25 * Capacity) quarter = (i + 1) * Dt;
}
+ Assert.True(quarter > 1.005 && quarter < 3.436, $"quarter at {quarter} s");
Assert.True(p.AirwayLiquid < 8.6e-7, $"airway liquid {p.AirwayLiquid}");
- Assert.True(coughed > 0.4 * Capacity, $"coughed {coughed}");
+ Assert.True(coughed > 0.0718 * Capacity, $"coughed {coughed}");
Assert.Equal(0.0, p.Drained);
}
diff --git a/tests/Lodur.Core.Tests/VesselTests.cs b/tests/Lodur.Core.Tests/VesselTests.cs
index f60b42d..74e4155 100644
--- a/tests/Lodur.Core.Tests/VesselTests.cs
+++ b/tests/Lodur.Core.Tests/VesselTests.cs
@@ -33,9 +33,9 @@ public sealed class VesselTests
},
new GoalSpec
{
- Id = "clutch", Priority = 1.0, InvoluntarySeconds = double.PositiveInfinity,
+ Id = "clutch", Priority = 3.0, InvoluntarySeconds = double.PositiveInfinity,
When = new[] { new Condition(DriveQuantity.AirHunger, ConditionOp.AtLeast, 0.3) },
- Behaviour = new PoseBehaviour { Keys = new[] { Identity() }, FlinchPose = Identity(), Hold = new HoldParameters { HoldStrength = 1f, FlinchGain = 0.0, StrainShake = 0.0 } },
+ Behaviour = new PoseBehaviour { Keys = new[] { Identity() }, FlinchPose = Identity(), Hold = new HoldParameters { HoldStrength = 0.8f, FlinchGain = 0.0, StrainShake = 0.0 } },
},
},
};
@@ -73,10 +73,10 @@ public sealed class VesselTests
return new Band(p.Cause, deadAt, outAt, oxygen, loss, maxOcc, fullAt, names);
}
- // Shipped readings (deterministic, Release): either carotid and either femoral die of BloodLoss at 29.73 s, out at 16.82 s with brain oxygen 0.995 and no occlusion; the front of the throat
- // fills the airway at 1.29 s, is out at 83.46 s with brain oxygen 0.371 and blood loss 0.450 and dies of Consciousness at 167.3 s. Mutants measured the same way: VesselV 1.0 dies at 79.35 s;
+ // The clutch fixture is the spec's probe setup: hold 0.8, priority 3. Shipped readings (deterministic, Release): either carotid and either femoral die of BloodLoss at 29.73 s, out at 16.82 s with brain oxygen 0.995 and no occlusion;
+ // the front of the throat fills the airway at 1.29 s, is out at 95.56 s with brain oxygen 0.376 and blood loss 0.4937 and dies of Consciousness at 184.8 s. Mutants measured the same way: VesselV 1.0 dies at 79.35 s;
// a carotid with no site (reached at the front of the throat too) at 16.15 s; cough 2.0 fills the airway at 129.95 s, is out at 127.7 s with brain oxygen 0.994 and blood loss 0.6000 and dies of BloodLoss at 380.9 s;
- // larynx V 1.0 is out at 86.61 s with blood loss 0.6001 (brain oxygen 0.376: the hypoxia bound does not claim it).
+ // larynx V 1.0 fills at 0.85 s, is out at 86.61 s with brain oxygen 0.435 and blood loss 0.6001, dies of BloodLoss at 191.0 s. Every bound is the geometric midpoint of shipped and its least claimed mutant (ruling deterministic-bound-margin).
[Theory]
[InlineData("Head", Side.Center, 0.0778f, -0.0778f, 0f, "CarotidLeft")]
@@ -85,12 +85,11 @@ public sealed class VesselTests
[InlineData("Thigh", Side.Right, 0.0495f, 0.12f, 0.0495f, "Femoral")]
public void A_vessel_kills_in_about_30_seconds_of_blood_loss_and_does_not_drown(string part, Side side, float x, float y, float z, string vessel)
{
- // Death in [20, 40] s: shipped 29.73 (ruled about 30). VesselV 1.0 reads 79.35 (2.0 x the upper bound), an unsited carotid 16.15 (1.24 x under the lower bound); the shipped reading is 1.35 and 1.49 inside them.
- // The geometric midpoints (48.6 and 21.9 s) are looser; the ruled band is kept.
+ // Death in (21.9, 48.6) s: shipped 29.73 (ruling bds-bleed-by-vessels, about 30 s, sets the target, not a band); the bounds are the midpoints sqrt(29.73 x 16.15) for the unsited carotid and sqrt(29.73 x 79.35) for VesselV 1.0, separation 1.84 and 2.67.
var r = Cut(part, side, (x, y, z), 0.02);
Assert.Contains(vessel, r.Layers);
Assert.Equal(DeathCause.BloodLoss, r.Cause);
- Assert.InRange(r.DeadAt, 20.0, 40.0);
+ Assert.InRange(r.DeadAt, 21.9, 48.6);
Assert.Equal(0.0, r.MaxOcclusion); // no airway tissue reached: exactly zero
}
@@ -99,13 +98,13 @@ public sealed class VesselTests
{
var r = Cut("Head", Side.Center, (0f, -0.0778f, 0.0778f), 0.02);
Assert.Equal(new[] { "Skin", "Fat", "Larynx" }, r.Layers); // no carotid at the front
- // Occlusion full within 5 s: shipped 1.29 s (3.9 x inside), cough 2.0 reads 129.95 s (26 x outside).
- Assert.InRange(r.OcclusionFullAt, 0.0, 5.0);
+ // Occlusion full within 12.9 s = sqrt(1.29 x 129.95): shipped 1.29 s, cough 2.0 129.95 s, separation 10.
+ Assert.InRange(r.OcclusionFullAt, 0.0, 12.9);
Assert.True(r.OutAt > 0.0, "he never loses consciousness");
- // Brain oxygen at consciousness 0.1: shipped 0.371, cough 2.0 0.994; bound sqrt(0.371 x 0.994) = 0.607 (separation 2.7).
- Assert.True(r.BrainOxygenOut < 0.607, $"brain oxygen {r.BrainOxygenOut}");
- // Blood loss then: shipped 0.4501, cough 2.0 and larynx V 1.0 0.6000; bound sqrt(0.4501 x 0.6000) = 0.5197 (separation 1.33, ruling deterministic-bound-margin).
- Assert.True(r.BloodLossOut < 0.5197, $"blood loss {r.BloodLossOut}");
+ // Brain oxygen at consciousness 0.1: shipped 0.376, cough 2.0 0.994; bound sqrt(0.376 x 0.994) = 0.611 (separation 2.6).
+ Assert.True(r.BrainOxygenOut < 0.611, $"brain oxygen {r.BrainOxygenOut}");
+ // Blood loss then: shipped 0.4937, cough 2.0 0.60004 and larynx V 1.0 0.60008; bound sqrt(0.4937 x 0.60004) = 0.5443 (separation 1.2155).
+ Assert.True(r.BloodLossOut < 0.5443, $"blood loss {r.BloodLossOut}");
Assert.Equal(DeathCause.Consciousness, r.Cause);
}
}
diff --git a/tests/Lodur.Tests/AirwayTests.cs b/tests/Lodur.Tests/AirwayTests.cs
index 60b8729..129d9e8 100644
--- a/tests/Lodur.Tests/AirwayTests.cs
+++ b/tests/Lodur.Tests/AirwayTests.cs
@@ -421,6 +421,8 @@ public sealed class AirwayTests
var flipDown = Sweep(down, 40f, -40f, -0.05f, Nose);
// The hold is the margin itself (ruling deterministic-bound-margin, readings at 0.05 degree steps): margin 5 mm (shipped) holds Mouth to +10.40 and Nose to -26.35 degrees; 1 mm holds +5.95 and -21.90, 10 mm holds +15.99 and -31.95.
// Mouth bound (7.87, 12.9) = sqrt(10.40 x 5.95) and sqrt(10.40 x 15.99), separation 1.75 and 1.54; Nose bound (-29.0, -24.0) = sqrt(26.35 x 31.95) and sqrt(26.35 x 21.90), separation 1.21 and 1.20.
+ // Why the Nose separation is 1.20: the Nose reading is 20.4 degrees plus 1.12 per mm of margin, so no margin pair a 5 mm shipped value is held against separates it by more; the Mouth bound (1.75, 1.54) is the pin, and this one claims the same mutants
+ // (a margin of 2.5 mm or less, 8 mm or more; 3 to 7 mm pass both) on a second axis, 0.05 degree steps, identical on a MinGW and an MSVC native build.
Assert.True(flipUp > 7.87f && flipUp < 12.9f, $"held Mouth to {flipUp} degrees");
Assert.True(flipDown < -24.0f && flipDown > -29.0f, $"held Nose to {flipDown} degrees");
}