Add SheetDef and TissueDef Thickness and Sheet with validation

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-25 22:21:46 +01:00
commit 56a58bc975
2 changed files with 57 additions and 4 deletions

View file

@ -15,15 +15,28 @@ namespace Lodur
/// <summary>What an effect's curve is read at: its affliction's severity or age (in its clock's seconds), or the organism's blood loss.</summary>
public enum EffectDriver { Severity, Age, BloodLoss }
/// <summary>
/// The material of a layer simulated as a particle sheet. <paramref name="Spacing"/> is the particle spacing in m. The compliances are
/// XPBD compliances in m/N: along the sheet's edges (also its joint stitches) and to the bone. <paramref name="TensileStrength"/> is
/// ultimate stress in Pa; the sheet builder derives an edge's break force as TensileStrength x Thickness x Spacing.
/// </summary>
public readonly record struct SheetDef(double Spacing, double EdgeCompliance, double AttachCompliance, double TensileStrength);
/// <summary>
/// One tissue layer of a part. <paramref name="Consciousness"/>, <paramref name="BloodPumping"/> and <paramref name="Motor"/> are
/// function weights, a closed set with one reader each: a capacity is the weighted mean integrity of the tissues that carry its weight.
/// A Vital tissue at integrity 0 kills.
/// A Vital tissue at integrity 0 kills. A stack is shells (<see cref="Thickness"/> above 0, in m) outside-in, then cores (Thickness 0:
/// bone, organs, a muscle with no sheet); the cores fill what the shells leave of the part's shape. <see cref="Sheet"/> is the
/// material of a shell simulated as a particle sheet; null means it is not.
/// </summary>
public readonly record struct TissueDef(
string Label, double Hp, double Vascularity, bool Vital = false, double Consciousness = 0.0, double BloodPumping = 0.0, double Motor = 0.0);
string Label, double Hp, double Vascularity, bool Vital = false, double Consciousness = 0.0, double BloodPumping = 0.0, double Motor = 0.0)
{
public double Thickness { get; init; }
public SheetDef? Sheet { get; init; }
}
/// <summary>A part's tissue stack, outside-in, named by the body part's label and side.</summary>
/// <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>
public readonly record struct PartTissuesDef(string PartLabel, Side Side, IReadOnlyList<TissueDef> Tissues);
/// <summary>The blood: nominal volume in m^3, the lost fraction that kills, and the regrowth per slow second as a fraction of nominal.</summary>
@ -65,7 +78,7 @@ namespace Lodur
/// <summary>
/// Physiology as content: tissue stacks per body part, the afflictions that can be bound, the organism-wide effects of blood
/// loss, the blood and the size of the affliction table. Validated at construction; resolved against a <see cref="BodyPlan"/> by <see cref="Physiology"/>.
/// loss, the blood and the size of the affliction table. Validated at construction; resolved against a <see cref="BodyPlan"/> by <see cref="Resolve"/>, the one (label, side) join, which <see cref="Physiology"/> and the sheet builder both call.
/// </summary>
public sealed class PhysiologyPlan
{
@ -122,6 +135,7 @@ namespace Lodur
foreach (var p in Parts)
{
var seen = new HashSet<string>();
var core = false;
foreach (var t in p.Tissues)
{
var where = $"tissue '{t.Label}' of part '{p.PartLabel}' {p.Side}";
@ -133,6 +147,21 @@ namespace Lodur
{
throw new ArgumentException($"{where}: a function weight is not finite and at least 0.");
}
if (!(IsFinite(t.Thickness) && t.Thickness >= 0.0)) throw new ArgumentException($"{where}: thickness is not finite and at least 0.");
if (t.Thickness == 0.0) core = true;
else if (core) throw new ArgumentException($"{where}: a shell follows a core.");
if (t.Sheet is SheetDef sh)
{
if (!(t.Thickness > 0.0)) throw new ArgumentException($"{where}: a sheet needs a thickness above 0.");
if (!(IsFinite(sh.Spacing) && sh.Spacing > 0.0)) throw new ArgumentException($"{where}: sheet spacing is not finite and above 0.");
if (!(IsFinite(sh.EdgeCompliance) && sh.EdgeCompliance >= 0.0 && IsFinite(sh.AttachCompliance) && sh.AttachCompliance >= 0.0))
{
throw new ArgumentException($"{where}: a sheet compliance is not finite and at least 0.");
}
if (!(IsFinite(sh.TensileStrength) && sh.TensileStrength > 0.0)) throw new ArgumentException($"{where}: sheet tensile strength is not finite and above 0.");
}
}
}

View file

@ -59,6 +59,20 @@ public sealed class PhysiologyPlanTests
Refuses(() => Plan(new[] { t with { BloodPumping = double.NaN } }));
Refuses(() => Plan(new[] { t with { Motor = double.PositiveInfinity } }));
var sheet = new SheetDef(0.1, 1e-4, 1e-3, 1e6);
var shell = t with { Label = "shell", Thickness = 0.01, Sheet = sheet };
Refuses(() => Plan(new[] { t with { Thickness = -1 } }));
Refuses(() => Plan(new[] { t with { Thickness = double.NaN } }));
Refuses(() => Plan(new[] { t with { Sheet = sheet } })); // a sheet on Thickness 0
Refuses(() => Plan(new[] { shell with { Sheet = sheet with { Spacing = 0 } } }));
Refuses(() => Plan(new[] { shell with { Sheet = sheet with { Spacing = double.NaN } } }));
Refuses(() => Plan(new[] { shell with { Sheet = sheet with { EdgeCompliance = -1 } } }));
Refuses(() => Plan(new[] { shell with { Sheet = sheet with { EdgeCompliance = double.NaN } } }));
Refuses(() => Plan(new[] { shell with { Sheet = sheet with { AttachCompliance = -1 } } }));
Refuses(() => Plan(new[] { shell with { Sheet = sheet with { TensileStrength = 0 } } }));
Refuses(() => Plan(new[] { shell with { Sheet = sheet with { TensileStrength = double.NaN } } }));
Refuses(() => Plan(new[] { t, shell })); // a shell after a core
var a = new AfflictionDef("a", PhysiologyClock.Acute, 0, true, Array.Empty<Effect>());
Refuses(() => Plan(afflictions: new[] { a, a })); // duplicate affliction label
Refuses(() => Plan(afflictions: new[] { a with { Label = "" } }));
@ -81,6 +95,16 @@ public sealed class PhysiologyPlanTests
Refuses(() => Plan(slots: 0));
}
[Fact]
public void A_positional_tissue_has_no_thickness_and_no_sheet_and_zero_compliance_is_accepted()
{
var t = new TissueDef("t", 10, 1, Consciousness: 1, Motor: 1);
Assert.Equal(0.0, t.Thickness);
Assert.Null(t.Sheet);
Plan(new[] { t });
Plan(new[] { t with { Label = "s", Thickness = 0.01, Sheet = new SheetDef(0.1, 0, 0, 1) }, t });
}
[Fact]
public void An_affliction_may_use_every_driver_and_the_boundary_values_of_the_rules_are_accepted()
{