feat(simulator): pick finer heat cells for small plates

The heat map's 0.5 in cells were too coarse to show heat trapped in
small parts such as washer rings. Unless a cell size is set, cells now
halve from 0.5 in down to 1/16 in while a full replay stays within 20
million cell updates (about a quarter second), since scrubbing back
replays from the start. Full sheets with long cuts keep 0.5 in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-12-01 13:09:13 -05:00
1 parent d4dfde5126
commit 71196866d7
4 files changed
+79 -8

No files matched your search

@@ -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; }
/// <summary>Cell edge in the plate's units (larger than asked for on very large sheets).</summary>
/// <summary>
/// Cell edge in the plate's units: the one asked for (larger on very large sheets), or one
/// picked for the plate (<see cref="ThermalParameters.CellSize"/>).
/// </summary>
public double CellSize { get; }
/// <summary>How far the model has run.</summary>
@@ -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));
@@ -33,7 +33,11 @@ namespace OpenNest.CNC.Thermal
/// <summary>Heat loss to the air from both faces, in W/m²·K (still air is about 10-25).</summary>
public double SurfaceCoefficient { get; init; } = 20;
/// <summary>Grid cell edge in <see cref="Units"/>; zero picks 0.5 in (12 mm).</summary>
/// <summary>
/// Grid cell edge in <see cref="Units"/>. 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.
/// </summary>
public double CellSize { get; init; }
/// <summary>
@@ -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;
}
@@ -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()
{
+8 -2
View File
@@ -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