diff --git a/OpenNest.Core/CNC/Thermal/PlateHeatSimulation.cs b/OpenNest.Core/CNC/Thermal/PlateHeatSimulation.cs index 388e8706..6c961c0e 100644 --- a/OpenNest.Core/CNC/Thermal/PlateHeatSimulation.cs +++ b/OpenNest.Core/CNC/Thermal/PlateHeatSimulation.cs @@ -26,6 +26,10 @@ namespace OpenNest.CNC.Thermal private const int MaxCells = 400_000; private const int MaxSamplesPerStep = 10_000; + // Cell updates for a whole run when the cell size is automatic: about a quarter second. + private const double WorkBudget = 20_000_000; + private const int MaxHalvings = 3; + // Explicit diffusion is stable below a quarter on a square grid; the cap keeps timing // fine enough that playback shows heat arrive with the torch. private const double StabilityFactor = 0.2; @@ -62,7 +66,9 @@ namespace OpenNest.CNC.Thermal Parameters = parameters; var bounds = plate.BoundingBox(includeParts: false); - var cell = parameters.EffectiveCellSize; + var cell = parameters.CellSize > 0 + ? parameters.CellSize + : AutomaticCellSize(bounds, simulation.TotalTime.TotalSeconds, parameters); while (CellCount(bounds, cell) > MaxCells) cell *= 1.25; CellSize = cell; @@ -114,7 +120,10 @@ namespace OpenNest.CNC.Thermal public double OriginX { get; } public double OriginY { get; } - /// Cell edge in the plate's units (larger than asked for on very large sheets). + /// + /// Cell edge in the plate's units: the one asked for (larger on very large sheets), or one + /// picked for the plate (). + /// public double CellSize { get; } /// How far the model has run. @@ -191,6 +200,32 @@ namespace OpenNest.CNC.Thermal return best; } + // The finest cell, from the coarsest default halved down to the finest, whose whole run + // (every cell, every time step) stays within the work budget. Halving the cell costs up + // to sixteen times the work: four times the cells and, once diffusion limits the step, + // four times the steps. Going back in time replays from the start, so the budget keeps a + // replay to a fraction of a second; large sheets stay at the coarsest. + internal static double AutomaticCellSize(Box bounds, double seconds, ThermalParameters parameters) + { + var coarsest = parameters.Units == Units.Millimeters ? 12 : 0.5; + var cell = coarsest; + for (var halvings = 0; halvings < MaxHalvings; halvings++) + { + var finer = cell / 2; + if (RunWork(bounds, seconds, finer, parameters) > WorkBudget) + break; + cell = finer; + } + return cell; + } + + private static double RunWork(Box bounds, double seconds, double cell, ThermalParameters parameters) + { + var dx = cell * parameters.MetersPerUnit; + var step = System.Math.Min(MaxStepSeconds, StabilityFactor * dx * dx / parameters.Material.Diffusivity); + return CellCount(bounds, cell) * System.Math.Ceiling(System.Math.Max(seconds, step) / step); + } + private static long CellCount(Box bounds, double cell) => (long)System.Math.Max(1, System.Math.Ceiling(bounds.Length / cell)) * (long)System.Math.Max(1, System.Math.Ceiling(bounds.Width / cell)); diff --git a/OpenNest.Core/CNC/Thermal/ThermalParameters.cs b/OpenNest.Core/CNC/Thermal/ThermalParameters.cs index 73ec26ca..c670c88a 100644 --- a/OpenNest.Core/CNC/Thermal/ThermalParameters.cs +++ b/OpenNest.Core/CNC/Thermal/ThermalParameters.cs @@ -33,7 +33,11 @@ namespace OpenNest.CNC.Thermal /// Heat loss to the air from both faces, in W/m²·K (still air is about 10-25). public double SurfaceCoefficient { get; init; } = 20; - /// Grid cell edge in ; zero picks 0.5 in (12 mm). + /// + /// Grid cell edge in . Zero picks one for the plate: 0.5 in (12 mm), + /// halved down to 1/16 in (1.5 mm) while the whole run stays quick to replay, so small + /// plates and short cuts get finer cells. + /// public double CellSize { get; init; } /// @@ -61,9 +65,6 @@ namespace OpenNest.CNC.Thermal 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 33b8289c..be0a2bb6 100644 --- a/OpenNest.Tests/CNC/PlateHeatSimulationTests.cs +++ b/OpenNest.Tests/CNC/PlateHeatSimulationTests.cs @@ -123,6 +123,35 @@ public class PlateHeatSimulationTests Assert.Equal(System.Math.Exp(-rate * 120), TotalHeat(heat, p) / atEnd, 3); } + private static PlateHeatSimulation Automatic(Plate plate) => + new(plate, CutSimulation.Build(plate, Cutting), new ThermalParameters + { + Material = ThermalMaterial.MildSteel, + Thickness = 0.25, + Units = Units.Inches, + }); + + [Fact] + public void SmallPlateGetsFinerCells() + { + var plate = new Plate(6, 16); + plate.Parts.Add(new Part(new Drawing("part", Frame(4, 2)), new Vector(1, 1))); + + Assert.Equal(0.5 / 8, Automatic(plate).CellSize); + } + + [Fact] + public void LargePlateWithALongCutKeepsHalfInchCells() + { + var plate = new Plate(60, 120); + var drawing = new Drawing("part", Square(25)); + for (var x = 0; x < 2; x++) + for (var y = 0; y < 4; y++) + plate.Parts.Add(new Part(drawing, new Vector(2 + x * 28, 2 + y * 28))); + + Assert.Equal(0.5, Automatic(plate).CellSize); + } + [Fact] public void HeatSpreadsFromTheCut() { diff --git a/docs/plate-heat-map.md b/docs/plate-heat-map.md index 3ac27a36..9245513c 100644 --- a/docs/plate-heat-map.md +++ b/docs/plate-heat-map.md @@ -7,8 +7,14 @@ a parked [heat-aware sequencer](#sequencing) uses a cheaper estimate of the same ## Model -- A grid over the plate's bounds, 0.5 in (12 mm) cells by default, made coarser on sheets that - would need more than 400,000 cells. Each cell holds its temperature rise above ambient. +- A grid over the plate's bounds. Unless `CellSize` is set, cells start at 0.5 in (12 mm) and + halve, down to 1/16 in (1.5 mm), while the whole run (cells times time steps) stays within + 20 million cell updates, about a quarter second to replay. Halving costs up to sixteen times + the work, since diffusion shortens the time step with the cell's area, so small plates and short + cuts get fine cells and full sheets keep 0.5 in. Any size is made coarser on sheets that would + need more than 400,000 cells. Each cell holds its temperature rise above ambient. Finer cells + hold the torch's heat in less metal, so cells along the cut read hotter; compare cut orders at + one cell size. - Heat in: while the torch is on (cuts and pierces), `AbsorbedPower` watts go into the cell under 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