fix(thermal): stop heat map stripes along straight cuts

A step added all its heat at once while the torch crossed several cells,
and each sample went wholly into one cell, so peaks varied by up to a
quarter along a straight cut. Share each sample between the surrounding
cells of the same piece, sample the path four times per cell, and limit
a step to one cell of torch travel.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Sonnet 5.5 committed 2026-12-02 17:28:42 -05:00
1 parent 4cacddc059
commit 1839e6641b
3 files changed
+119 -17

No files matched your search

@@ -31,7 +31,8 @@ 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 tenth of a second.
// Cell updates for a whole run, counting diffusion-limited steps, when the cell size is
// automatic. The torch travel limit below can multiply the actual steps a few times.
private const double WorkBudget = 60_000_000;
private const int MaxHalvings = 3;
@@ -40,6 +41,16 @@ namespace OpenNest.CNC.Thermal
private const double StabilityFactor = 0.2;
private const double MaxStepSeconds = 1.0;
// A step adds all its heat at once, so one in which the torch crosses several cells
// lays it down in lumps that show as stripes along a straight cut. A step is therefore
// no longer than the torch takes to cross this many cells at its fastest, though never
// shortened below the floor (a very high feed over fine cells).
private const double MaxTravelCells = 1.0;
private const double MinStepSeconds = 0.005;
// Samples of the torch's path per cell; fewer leave some cells with more than others.
private const int SamplesPerCell = 4;
// Checkpoints come every this many cell updates of a run (about a millisecond), so going
// back costs at most that much stepping, and share at most this much memory (temperature
// and peak per cell); a long run on a large grid spaces them further apart.
@@ -126,6 +137,9 @@ namespace OpenNest.CNC.Thermal
diffusionRate = material.Diffusivity / (dx * dx);
lossRate = 2 * parameters.SurfaceCoefficient / (volumetricHeat * thickness);
maxStep = System.Math.Min(MaxStepSeconds, StabilityFactor / diffusionRate);
var fastest = FastestCut(simulation);
if (fastest > 0)
maxStep = System.Math.Min(maxStep, System.Math.Max(MinStepSeconds, MaxTravelCells * cell / fastest));
if (parameters.SupportBed != null && parameters.ContactCoefficient > 0)
{
@@ -268,11 +282,11 @@ namespace OpenNest.CNC.Thermal
}
// 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
// (every cell, every diffusion-limited 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. The budget keeps the first run, which also sets the color scale, to
// about a tenth of a second (going back resumes from a checkpoint); large sheets stay at
// the coarsest.
// four times the steps. The budget keeps the first run, which also sets the color scale,
// short (going back resumes from a checkpoint); large sheets stay at the coarsest. The
// torch travel limit is left out so that feed does not change the picture's resolution.
internal static double AutomaticCellSize(Box bounds, double seconds, ThermalParameters parameters)
{
var coarsest = parameters.Units == Units.Millimeters ? 12 : 0.5;
@@ -303,9 +317,7 @@ namespace OpenNest.CNC.Thermal
var end = time + seconds;
while (depositCursor < deposits.Count && deposits[depositCursor].Time <= end)
{
var deposit = deposits[depositCursor++];
if (region[deposit.Cell] != Removed)
temperature[deposit.Cell] += deposit.Rise;
Add(deposits[depositCursor++]);
}
while (eventCursor < events.Count && events[eventCursor].Time <= end)
Apply(events[eventCursor]);
@@ -314,6 +326,34 @@ namespace OpenNest.CNC.Thermal
time = end;
}
// Shares a sample between the four cells whose centers surround the torch, by how close
// it is to each, so heat moves smoothly across the grid with the torch. Only cells of
// the piece under the torch take a share, which keeps heat from crossing a kerf.
private void Add(Deposit deposit)
{
var piece = region[deposit.Cell];
if (piece == Removed)
return;
var left = deposit.Left;
var right = System.Math.Min(left + 1, Columns - 1);
var lower = deposit.Lower * Columns;
var upper = System.Math.Min(deposit.Lower + 1, Rows - 1) * Columns;
var lowerLeft = Share(lower + left, piece, (1 - deposit.X) * (1 - deposit.Y));
var lowerRight = Share(lower + right, piece, deposit.X * (1 - deposit.Y));
var upperLeft = Share(upper + left, piece, (1 - deposit.X) * deposit.Y);
var upperRight = Share(upper + right, piece, deposit.X * deposit.Y);
// The cell under the torch always has a share, so the total is never zero.
var rise = deposit.Rise / (lowerLeft + lowerRight + upperLeft + upperRight);
temperature[lower + left] += rise * lowerLeft;
temperature[lower + right] += rise * lowerRight;
temperature[upper + left] += rise * upperLeft;
temperature[upper + right] += rise * upperRight;
}
private float Share(int cell, int piece, float weight) => region[cell] == piece ? weight : 0f;
// One explicit step: each cell takes share times the difference to each neighbor of its
// own piece, then decays. A link weight is 1 between cells of one piece and 0 otherwise
// (and for every link of a removed cell, which holds no heat), so the sum runs without
@@ -626,7 +666,7 @@ namespace OpenNest.CNC.Thermal
// Heat goes in where the torch is, in proportion to the time it spends there.
private void BuildDeposits(double risePerSecond)
{
var spacing = CellSize / 2;
var spacing = CellSize / SamplesPerCell;
foreach (var step in Simulation.Steps)
{
var seconds = step.Duration.TotalSeconds;
@@ -657,8 +697,28 @@ namespace OpenNest.CNC.Thermal
private void AddDeposit(double at, Vector point, double rise)
{
var cell = CellAt(point);
if (cell >= 0)
deposits.Add(new Deposit(at, cell, (float)rise));
if (cell < 0)
return;
// Beyond the outermost cell centers the edge cell takes both shares.
var x = System.Math.Clamp((point.X - OriginX) / CellSize - 0.5, 0, Columns - 1);
var y = System.Math.Clamp((point.Y - OriginY) / CellSize - 0.5, 0, Rows - 1);
var left = (int)x;
var lower = (int)y;
deposits.Add(new Deposit(at, cell, left, lower, (float)(x - left), (float)(y - lower), (float)rise));
}
// The highest average speed of any cutting move, in plate units per second.
private static double FastestCut(CutSimulation simulation)
{
var fastest = 0.0;
foreach (var step in simulation.Steps)
{
var seconds = step.Duration.TotalSeconds;
if (step.Kind == CutSimulationStepKind.Cut && seconds > 0)
fastest = System.Math.Max(fastest, step.Length / seconds);
}
return fastest;
}
// Each closed contour changes the pieces when it finishes: a cutout drops its slug, an
@@ -757,7 +817,10 @@ namespace OpenNest.CNC.Thermal
return inside;
}
private readonly record struct Deposit(double Time, int Cell, float Rise);
// A sample of the torch's heat: the cell it is over, and its position between cell
// centers (X and Y, 0 to 1, from column Left and row Lower).
private readonly record struct Deposit(double Time, int Cell, int Left, int Lower,
float X, float Y, float Rise);
private sealed record Checkpoint(double Time, double PreviousTime, int StraightSteps,
int DepositCursor, int EventCursor, float[] Temperature, float[] Peak);
@@ -171,6 +171,39 @@ public class PlateHeatSimulationTests
Assert.True(heat.Peaks[near] > heat.Peaks[away]);
}
// Feeds that cross a whole and a fractional number of cells per diffusion-limited step.
[Theory]
[InlineData(47)]
[InlineData(97)]
[InlineData(283)]
public void AStraightCutPeaksEvenlyAlongItsLength(double feedrate)
{
var plate = new Plate(48, 96);
plate.Parts.Add(new Part(new Drawing("part", Line(23.7)), new Vector(10.03, 10.31)));
var simulation = CutSimulation.Build(plate, new CutParameters
{
Feedrate = feedrate,
RapidTravelRate = 600,
Units = Units.Inches,
});
var heat = new PlateHeatSimulation(plate, simulation, new ThermalParameters
{
Material = ThermalMaterial.MildSteel,
Thickness = 0.135,
Units = Units.Inches,
CellSize = 0.25,
});
heat.RunToEnd(TimeSpan.FromSeconds(10));
// The middle of the cut, clear of the warm-up and the end.
var peaks = new List<float>();
for (var x = 18.125; x < 28; x += 0.25)
peaks.Add(heat.Peaks[heat.CellAt(new Vector(x, 10.31))]);
var ripple = (peaks.Max() - peaks.Min()) / peaks.Average();
Assert.True(ripple < 0.1, $"peaks vary {ripple:P0} along the cut");
}
[Fact]
public void CutoutSlugFallsAwayWithItsHeat()
{
+11 -5
View File
@@ -8,17 +8,23 @@ a removed heat-aware sequencer used a cheaper estimate of the same physics ([Seq
## Model
- 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
60 million cell updates, about a tenth of a second. Halving costs up to sixteen times
halve, down to 1/16 in (1.5 mm), while the whole run (cells times diffusion-limited time steps)
stays within 60 million cell updates. 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.
- Heat in: while the torch is on (cuts and pierces), `AbsorbedPower` watts go into the sheet at
the torch, in proportion to the time spent there. The path is sampled four times per cell, and
each sample is shared between the four cells whose centers surround it, by closeness, among
those in the same piece as the cell under the torch. 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. A rise
time step kept below the stability limit and at most 1 s, and no longer than the torch takes to
cross one cell at its fastest cutting move (but not below 5 ms for that). A step adds its heat
all at once, so a longer one shows stripes along a straight cut. This limit does not enter the
choice of cell size, so a fast feed can take a few times longer to run (about 0.3 s in the
worst case measured). Sheet edges are insulated. A rise
below 1e-30 °C is set to zero: the front of the spreading heat would otherwise pass through
subnormal floats, which make each step several times slower. It changes no cell above about
1e-18 °C.