diff --git a/OpenNest.Core/CNC/Thermal/PlateHeatSimulation.cs b/OpenNest.Core/CNC/Thermal/PlateHeatSimulation.cs index 137884c6..388e8706 100644 --- a/OpenNest.Core/CNC/Thermal/PlateHeatSimulation.cs +++ b/OpenNest.Core/CNC/Thermal/PlateHeatSimulation.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using OpenNest.Geometry; +using OpenNest.Support; namespace OpenNest.CNC.Thermal { @@ -8,7 +9,8 @@ namespace OpenNest.CNC.Thermal /// Estimates how heat builds up and spreads in the sheet while a /// plays: a grid of cells over the plate, each holding its temperature rise above ambient in /// kelvin. Cutting and piercing add the absorbed power along the torch's path, heat conducts - /// to neighboring cells of the same piece and is lost to the air from both faces. + /// to neighboring cells of the same piece and is lost to the air from both faces and, given a + /// , into the slats or grid points under each cell. /// /// The pieces change as contours close: a cutout's slug falls away with its heat, and a /// part's perimeter separates the part from the skeleton so no heat crosses the kerf. That is @@ -37,6 +39,11 @@ namespace OpenNest.CNC.Thermal private readonly float[] peak; private readonly double diffusionRate; private readonly double lossRate; + private readonly float[] contactFraction; + private readonly double contactRate; + private readonly float[] fullStepKeep; + private readonly float[] partialStepKeep; + private double partialStepSeconds = -1; private readonly double maxStep; private int depositCursor; private int eventCursor; @@ -79,6 +86,19 @@ namespace OpenNest.CNC.Thermal lossRate = 2 * parameters.SurfaceCoefficient / (volumetricHeat * thickness); maxStep = System.Math.Min(MaxStepSeconds, StabilityFactor / diffusionRate); + if (parameters.SupportBed != null && parameters.ContactCoefficient > 0) + { + contactFraction = ContactFractions(parameters.SupportBed, plate.Quadrant, + parameters.EffectiveGridContactSize); + if (contactFraction != null) + { + // Through the bottom face only. + contactRate = parameters.ContactCoefficient / (volumetricHeat * thickness); + fullStepKeep = ContactKeep(maxStep, new float[count]); + partialStepKeep = new float[count]; + } + } + BuildDeposits(parameters.AbsorbedPower / (volumetricHeat * thickness * dx * dx)); BuildPieceEvents(); Reset(); @@ -106,6 +126,15 @@ namespace OpenNest.CNC.Thermal /// The highest rise each cell has reached so far. public ReadOnlySpan Peaks => peak; + /// True when heat is also lost into the support bed (). + public bool UsesSupportBed => contactFraction != null; + + /// + /// The share of each cell's area that rests on a slat or grid point, 0 to 1, row by row + /// from the bottom; empty when is false. + /// + public ReadOnlySpan SupportFractions => contactFraction; + /// True for a cell whose material fell away as a slug. public bool IsRemoved(int index) => region[index] == Removed; @@ -186,6 +215,9 @@ namespace OpenNest.CNC.Thermal { var share = (float)(diffusionRate * seconds); var keep = (float)System.Math.Exp(-lossRate * seconds); + var cellKeep = contactFraction == null ? null + : seconds == maxStep ? fullStepKeep + : PartialStepKeep(seconds); var columns = Columns; var current = temperature; for (var row = 0; row < Rows; row++) @@ -212,7 +244,7 @@ namespace OpenNest.CNC.Thermal if (row < Rows - 1 && region[i + columns] == piece) flow += current[i + columns] - t; - var value = (t + share * flow) * keep; + var value = (t + share * flow) * (cellKeep == null ? keep : cellKeep[i]); next[i] = value; if (value > peak[i]) peak[i] = value; @@ -221,6 +253,88 @@ namespace OpenNest.CNC.Thermal (temperature, next) = (next, temperature); } + // Each cell's decay over one step: the air from both faces plus the bed under it. An + // exponential factor stays stable however strong the contact. Most steps are full + // length; playback ends each frame on a shorter one, kept until its length changes. + private float[] PartialStepKeep(double seconds) + { + if (seconds != partialStepSeconds) + { + ContactKeep(seconds, partialStepKeep); + partialStepSeconds = seconds; + } + return partialStepKeep; + } + + private float[] ContactKeep(double seconds, float[] keep) + { + for (var i = 0; i < keep.Length; i++) + keep[i] = (float)System.Math.Exp(-(lossRate + contactRate * contactFraction[i]) * seconds); + return keep; + } + + // The share of every cell over the bed: the overlap of each slat's top, or each grid + // point's square pad, with the cell, so slats narrower or wider than a cell both count. + // Null for a bed this model does not know. + private float[] ContactFractions(SupportBed bed, int quadrant, double padSize) + { + var area = new double[Columns * Rows]; + var extent = new Box(OriginX, OriginY, Columns * CellSize, Rows * CellSize); + switch (bed) + { + case SupportSlats slats: + foreach (var slat in slats.SlatsIn(extent, quadrant, int.MaxValue)) + AddOverlap(area, slat.Left, slat.Bottom, slat.Right, slat.Top, 0, Rows - 1); + break; + case SupportGrid grid: + // One row of cells at a time, so a fine grid on a large sheet never lists + // every point at once. + var half = padSize / 2; + for (var row = 0; row < Rows; row++) + { + var strip = new Box(extent.Left - half, OriginY + row * CellSize - half, + extent.Length + padSize, CellSize + padSize); + foreach (var point in grid.PointsIn(strip, quadrant, int.MaxValue)) + AddOverlap(area, point.X - half, point.Y - half, point.X + half, point.Y + half, row, row); + } + break; + default: + return null; + } + + var cellArea = CellSize * CellSize; + var fractions = new float[area.Length]; + for (var i = 0; i < area.Length; i++) + fractions[i] = (float)System.Math.Min(1, area[i] / cellArea); + return fractions; + } + + // Adds the area of the rectangle over each cell in rows firstRow to lastRow. + private void AddOverlap(double[] area, double left, double bottom, double right, double top, + int firstRow, int lastRow) + { + if (!(right > left) || !(top > bottom)) + return; + var columnStart = System.Math.Max(0, (int)System.Math.Floor((left - OriginX) / CellSize)); + var columnEnd = System.Math.Min(Columns - 1, (int)System.Math.Floor((right - OriginX) / CellSize)); + var rowStart = System.Math.Max(firstRow, (int)System.Math.Floor((bottom - OriginY) / CellSize)); + var rowEnd = System.Math.Min(lastRow, (int)System.Math.Floor((top - OriginY) / CellSize)); + for (var row = rowStart; row <= rowEnd; row++) + { + var cellBottom = OriginY + row * CellSize; + var height = System.Math.Min(top, cellBottom + CellSize) - System.Math.Max(bottom, cellBottom); + if (height <= 0) + continue; + for (var column = columnStart; column <= columnEnd; column++) + { + var cellLeft = OriginX + column * CellSize; + var width = System.Math.Min(right, cellLeft + CellSize) - System.Math.Max(left, cellLeft); + if (width > 0) + area[row * Columns + column] += width * height; + } + } + } + private void Apply(PieceEvent piece) { // Only skeleton cells change: a part cut free earlier (one nested in this cutout) diff --git a/OpenNest.Core/CNC/Thermal/ThermalParameters.cs b/OpenNest.Core/CNC/Thermal/ThermalParameters.cs index 6037f668..73ec26ca 100644 --- a/OpenNest.Core/CNC/Thermal/ThermalParameters.cs +++ b/OpenNest.Core/CNC/Thermal/ThermalParameters.cs @@ -1,3 +1,5 @@ +using OpenNest.Support; + namespace OpenNest.CNC.Thermal { /// Inputs to . Lengths are in the plate's units. @@ -34,9 +36,35 @@ namespace OpenNest.CNC.Thermal /// Grid cell edge in ; zero picks 0.5 in (12 mm). public double CellSize { get; init; } + /// + /// The bed the sheet rests on (usually the plate's SupportBed), in + /// ; null loses heat to the air alone. The sheet loses heat through its + /// bottom face into the bed wherever it lies over a slat or a grid point's pad. + /// + public SupportBed SupportBed { get; init; } + + /// + /// Heat loss from the sheet into the bed where they touch, in W/m²·K through the bottom + /// face. Contact between a sheet and slats is poor (dry, rough, often slagged, a few + /// hundred W/m²·K), and the slats warm up themselves, which the model does not track, so + /// the default of 200 is an effective value below a cold contact's. An estimate to + /// calibrate. + /// + public double ContactCoefficient { get; init; } = 200; + + /// + /// The square pad each point of a SupportGrid touches the sheet with, edge in + /// ; zero picks 0.125 in (3 mm). A grid point has no area of its own, + /// so this is an estimate of the worn tip it rests on. + /// + public double GridContactSize { get; init; } + internal double MetersPerUnit => Units == Units.Millimeters ? 0.001 : 0.0254; internal double EffectiveCellSize => CellSize > 0 ? CellSize : Units == Units.Millimeters ? 12 : 0.5; + + internal double EffectiveGridContactSize => + GridContactSize > 0 ? GridContactSize : Units == Units.Millimeters ? 3 : 0.125; } } diff --git a/OpenNest.Tests/CNC/PlateHeatSimulationTests.cs b/OpenNest.Tests/CNC/PlateHeatSimulationTests.cs index 851ec515..228fcb35 100644 --- a/OpenNest.Tests/CNC/PlateHeatSimulationTests.cs +++ b/OpenNest.Tests/CNC/PlateHeatSimulationTests.cs @@ -1,6 +1,7 @@ using OpenNest.CNC; using OpenNest.CNC.Thermal; using OpenNest.Geometry; +using OpenNest.Support; namespace OpenNest.Tests.CNC; @@ -14,15 +15,18 @@ public class PlateHeatSimulationTests Units = Units.Inches, }; - private static ThermalParameters Thermal(double surfaceCoefficient = 0) => new() - { - Material = ThermalMaterial.MildSteel, - Thickness = 0.25, - Units = Units.Inches, - AbsorbedPower = 1000, - SurfaceCoefficient = surfaceCoefficient, - CellSize = 0.5, - }; + private static ThermalParameters Thermal(double surfaceCoefficient = 0, SupportBed? bed = null, + double contactCoefficient = 200) => new() + { + Material = ThermalMaterial.MildSteel, + Thickness = 0.25, + Units = Units.Inches, + AbsorbedPower = 1000, + SurfaceCoefficient = surfaceCoefficient, + CellSize = 0.5, + SupportBed = bed, + ContactCoefficient = contactCoefficient, + }; // Joules per kelvin held by one cell of the grid above. private static double CellCapacity(ThermalParameters p) @@ -183,6 +187,112 @@ public class PlateHeatSimulationTests Assert.Equal(first, heat.Temperatures.ToArray()); } + // Slats along X, one every 4 in, 1 in wide, centered at 2, 6, 10... in from the origin. + private static readonly SupportSlats Slats = new(SlatAxis.X, pitch: 4, thickness: 1, offset: 2); + + [Fact] + public void CellOverASlatCoolsFasterThanOneBetweenSlats() + { + // The same cut on a slat's center line and midway between two slats. + double Retained(double y) + { + var (plate, simulation) = Cut(Line(4), new Vector(20, y)); + var bare = new PlateHeatSimulation(plate, simulation, Thermal(20)); + var onSlats = new PlateHeatSimulation(plate, simulation, Thermal(20, Slats)); + bare.RunToEnd(TimeSpan.FromSeconds(60)); + onSlats.RunToEnd(TimeSpan.FromSeconds(60)); + var cell = onSlats.CellAt(new Vector(22, y)); + return onSlats.Temperatures[cell] / bare.Temperatures[cell]; + } + + var over = Retained(10.25); + var between = Retained(12.25); + + Assert.True(over < between, $"over a slat {over}, between slats {between}"); + Assert.True(between < 1); + } + + [Fact] + public void WithoutABedTheModelIsUnchanged() + { + var (plate, simulation) = Cut(Square(6, clockwise: true), new Vector(10, 10)); + var before = new PlateHeatSimulation(plate, simulation, Thermal(20)); + before.RunToEnd(TimeSpan.FromSeconds(60)); + + // The plate's own bed is used only when passed in. + plate.SupportBed = Slats; + var after = new PlateHeatSimulation(plate, simulation, Thermal(20)); + after.RunToEnd(TimeSpan.FromSeconds(60)); + var noContact = new PlateHeatSimulation(plate, simulation, Thermal(20, Slats, contactCoefficient: 0)); + noContact.RunToEnd(TimeSpan.FromSeconds(60)); + + Assert.False(after.UsesSupportBed); + Assert.True(after.SupportFractions.IsEmpty); + Assert.Equal(before.Temperatures.ToArray(), after.Temperatures.ToArray()); + Assert.Equal(before.Peaks.ToArray(), after.Peaks.ToArray()); + Assert.Equal(before.Temperatures.ToArray(), noContact.Temperatures.ToArray()); + } + + [Fact] + public void FullSlatContactDecaysHeatThroughBothPaths() + { + // Slats as wide as their pitch cover the whole sheet. + var (plate, simulation) = Cut(Line(20), new Vector(10, 10)); + var p = Thermal(20, new SupportSlats(SlatAxis.Y, pitch: 1, thickness: 1, offset: 0.5), contactCoefficient: 300); + var heat = new PlateHeatSimulation(plate, simulation, p); + Assert.All(heat.SupportFractions.ToArray(), f => Assert.Equal(1f, f, 5)); + + heat.RunToEnd(); + var atEnd = TotalHeat(heat, p); + heat.RunToEnd(TimeSpan.FromSeconds(60)); + + var thickness = p.Thickness * 0.0254; + var capacity = p.Material.Density * p.Material.SpecificHeat * thickness; + var rate = (2 * p.SurfaceCoefficient + p.ContactCoefficient) / capacity; + Assert.Equal(System.Math.Exp(-rate * 60), TotalHeat(heat, p) / atEnd, 3); + } + + [Fact] + public void SlatFractionsFollowTheSlatsOverlapNotTheCells() + { + // 0.75 in slats centered on 2, 6, 10...: a half-inch cell from 9.5 to 10 is 3/4 on one. + var (plate, simulation) = Cut(Line(4), new Vector(20, 20)); + var heat = new PlateHeatSimulation(plate, simulation, + Thermal(bed: new SupportSlats(SlatAxis.X, pitch: 4, thickness: 0.75, offset: 2))); + + Assert.True(heat.UsesSupportBed); + Assert.Equal(0.75f, heat.SupportFractions[heat.CellAt(new Vector(30, 9.75))], 5); + Assert.Equal(0.75f, heat.SupportFractions[heat.CellAt(new Vector(30, 10.25))], 5); + Assert.Equal(0f, heat.SupportFractions[heat.CellAt(new Vector(30, 12.25))]); + } + + [Theory] + [InlineData(1, 1, 1)] + [InlineData(2, -1, 1)] + [InlineData(3, -1, -1)] + [InlineData(4, 1, -1)] + public void SlatsAndGridPointsMirrorWithThePlateQuadrant(int quadrant, int signX, int signY) + { + var plate = new Plate(48, 96) { Quadrant = quadrant }; + var simulation = CutSimulation.Build(plate, Cutting); + + // Slats along X sit at Y = ±2, ±6...; slats along Y at X = ±2, ±6.... + var alongX = new PlateHeatSimulation(plate, simulation, Thermal(bed: Slats)); + Assert.Equal(1f, alongX.SupportFractions[alongX.CellAt(new Vector(signX * 30.25, signY * 2.25))]); + Assert.Equal(0f, alongX.SupportFractions[alongX.CellAt(new Vector(signX * 30.25, signY * 4.25))]); + + var alongY = new PlateHeatSimulation(plate, simulation, + Thermal(bed: new SupportSlats(SlatAxis.Y, pitch: 4, thickness: 1, offset: 2))); + Assert.Equal(1f, alongY.SupportFractions[alongY.CellAt(new Vector(signX * 6.25, signY * 30.25))]); + Assert.Equal(0f, alongY.SupportFractions[alongY.CellAt(new Vector(signX * 4.25, signY * 30.25))]); + + // Grid points at (±1.25, ±1.25) + 3 in steps, each on a 1/8 in pad in the middle of a cell. + var grid = new PlateHeatSimulation(plate, simulation, Thermal(bed: new SupportGrid(3, 3, 1.25, 1.25))); + var pad = 0.125 * 0.125 / (0.5 * 0.5); + Assert.Equal((float)pad, grid.SupportFractions[grid.CellAt(new Vector(signX * 4.25, signY * 7.25))], 5); + Assert.Equal(0f, grid.SupportFractions[grid.CellAt(new Vector(signX * 4.75, signY * 7.25))]); + } + [Theory] [InlineData("304 Stainless", "Stainless steel")] [InlineData("SS", "Stainless steel")] diff --git a/OpenNest/Forms/CutSimulatorForm.cs b/OpenNest/Forms/CutSimulatorForm.cs index 75de10cf..63460af7 100644 --- a/OpenNest/Forms/CutSimulatorForm.cs +++ b/OpenNest/Forms/CutSimulatorForm.cs @@ -7,6 +7,7 @@ using OpenNest.CNC.Thermal; using OpenNest.Controls; using OpenNest.Desktop; using OpenNest.Desktop.Interaction; +using OpenNest.Support; using NumericUpDown = OpenNest.Controls.NumericUpDown; namespace OpenNest.Forms @@ -293,6 +294,7 @@ namespace OpenNest.Forms Thickness = nest.Thickness, Units = nest.Units, AbsorbedPower = (double)heatPowerBox.Value, + SupportBed = plate.SupportBed, }); heat.RunToEnd(); // Pierces leave single very hot cells; scaling to them would wash out the areas @@ -307,7 +309,9 @@ namespace OpenNest.Forms var scale = peaks.Count == 0 ? 1f : peaks[(int)(0.995 * (peaks.Count - 1))]; heat.Reset(); overlay.LoadHeat(heat, scale); - heatScaleText = $"{material.Name}; red is {scale:0} °C above ambient"; + var cooling = !heat.UsesSupportBed ? "cooled by air only (no support bed)" + : plate.SupportBed is SupportSlats ? "cooled by air and slats" : "cooled by air and the support grid"; + heatScaleText = $"{material.Name}, {cooling}; red is {scale:0} °C above ambient"; UpdateHeatLabel(); } diff --git a/docs/plate-heat-map.md b/docs/plate-heat-map.md index 79b0ffe9..cb6b5bd3 100644 --- a/docs/plate-heat-map.md +++ b/docs/plate-heat-map.md @@ -13,7 +13,21 @@ a parked [heat-aware sequencer](#sequencing) uses a cheaper estimate of the same the torch, in proportion to the time spent there. Scribes add nothing. - Conduction: explicit finite differences between the four neighbors of the same piece, with the time step kept below the stability limit and at most 1 s. Sheet edges are insulated. -- Loss: convection from both faces at `SurfaceCoefficient` (W/m²·K). +- Loss: convection from both faces at `SurfaceCoefficient` (W/m²·K), plus, when + `ThermalParameters.SupportBed` is given, contact with the bed through the bottom face at + `ContactCoefficient` (W/m²·K) times each cell's share of area over the bed. The share is the + overlap of the cell with each slat's top (`SupportSlats.SlatsIn`) or with a square pad + `GridContactSize` wide (0.125 in / 3 mm by default) around each grid point + (`SupportGrid.PointsIn`), placed for the plate's quadrant as the beds mirror themselves; it + is not snapped to cells, so slats narrower or wider than a cell count their true area. The + nominal bed is used, without registration shifts. Each cell decays by an exponential factor + per step, which stays stable however strong the contact. A part cut free still rests on the + bed and keeps losing heat to it; a dropped slug has no heat left. Without a bed (or with a zero + `ContactCoefficient`) the model is exactly the air-only model. +- Contact default: 200 W/m²·K. A sheet resting dry on slats, often over slag, conducts poorly + (a few hundred W/m²·K when the slat is cold), and the slats warm up under a long cut. The slats + are not given a temperature of their own; the default is a lower effective value standing in + for that. An estimate to calibrate, like `AbsorbedPower`. - Pieces: when a closed contour finishes, a cutout (an interior contour by the CI Fiber rule the timeline already uses) removes its slug's cells and their heat; a part's outside contour separates its cells from the skeleton, so no heat crosses the kerf. Only skeleton cells change, @@ -22,7 +36,8 @@ a parked [heat-aware sequencer](#sequencing) uses a cheaper estimate of the same - Material from the nest's material name and grade (`ThermalMaterial.ForName`: stainless, aluminum, otherwise mild steel); thickness from the nest. -Not modeled: radiation, heat into the slats, pierce power differing from cutting power, the +Not modeled: radiation, the slats' own temperature (they are an infinite sink at ambient behind +an effective coefficient), less air loss from the bottom face where slats cover it, pierce power differing from cutting power, the heat carried off by the melt beyond `AbsorbedPower`, kerf width and warping. The absorbed power (1000 W by default) is an uncalibrated guess, so the temperatures are relative; use them to compare cut orders of one plate, not as real values. @@ -31,7 +46,9 @@ compare cut orders of one plate, not as real values. The simulator colors the field against the 99.5th percentile of the whole cut's peaks: a pierce puts its energy into a single cell, and scaling to that cell would hide the areas where heat -builds up. Cells whose slug fell away are transparent. +builds up. Cells whose slug fell away are transparent. The simulator passes the plate's support +bed, and its heat label says whether the sheet is cooled by air only or by air and the slats +(or support grid). ## Sequencing