Files
OpenNest/OpenNest.Tests/CNC/PlateHeatSimulationTests.cs
T
ajandClaude Opus 5.5 1403517184 feat(simulator): estimate plate heat buildup during a cut
Add PlateHeatSimulation, a grid model driven by the cut simulation's
timed moves: cutting and piercing add absorbed power under the torch,
heat conducts within each piece and is lost from both faces. Finished
cutouts drop their slug and its heat; a finished outside contour cuts
the part free of the skeleton so no heat crosses the kerf.

The Cut Simulator gains a Heat layer (Now / Peak so far) with the
absorbed power as an input, colored against the 99.5th percentile so
single-cell pierce spikes don't wash out area buildup. Temperatures are
relative estimates for comparing cut orders; see docs/plate-heat-map.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 22:47:18 -05:00

195 lines
6.4 KiB
C#

using OpenNest.CNC;
using OpenNest.CNC.Thermal;
using OpenNest.Geometry;
namespace OpenNest.Tests.CNC;
public class PlateHeatSimulationTests
{
private static readonly CutParameters Cutting = new()
{
Feedrate = 100, // in/min
RapidTravelRate = 600,
PierceTime = TimeSpan.FromSeconds(1),
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,
};
// Joules per kelvin held by one cell of the grid above.
private static double CellCapacity(ThermalParameters p)
{
var dx = p.CellSize * 0.0254;
return p.Material.Density * p.Material.SpecificHeat * p.Thickness * 0.0254 * dx * dx;
}
// Counterclockwise: a cutout under the CI Fiber rule. Clockwise: a part's outside.
private static Program Square(double size, bool clockwise)
{
var program = new Program();
program.Codes.Add(new RapidMove(0, 0));
if (clockwise)
{
program.Codes.Add(new LinearMove(0, size));
program.Codes.Add(new LinearMove(size, size));
program.Codes.Add(new LinearMove(size, 0));
}
else
{
program.Codes.Add(new LinearMove(size, 0));
program.Codes.Add(new LinearMove(size, size));
program.Codes.Add(new LinearMove(0, size));
}
program.Codes.Add(new LinearMove(0, 0));
return program;
}
private static Program Line(double length)
{
var program = new Program();
program.Codes.Add(new RapidMove(0, 0));
program.Codes.Add(new LinearMove(length, 0));
return program;
}
private static (Plate Plate, CutSimulation Simulation) Cut(Program program, Vector location)
{
var plate = new Plate(48, 96);
plate.Parts.Add(new Part(new Drawing("part", program), location));
return (plate, CutSimulation.Build(plate, Cutting));
}
private static double TorchOnSeconds(CutSimulation simulation) => simulation.Steps
.Where(s => s.Kind is CutSimulationStepKind.Cut or CutSimulationStepKind.Pierce)
.Sum(s => s.Duration.TotalSeconds);
private static double TotalHeat(PlateHeatSimulation heat, ThermalParameters p)
{
var sum = 0.0;
foreach (var t in heat.Temperatures)
sum += t;
return sum * CellCapacity(p);
}
[Fact]
public void WithoutSurfaceLossAllAbsorbedEnergyStaysInTheSheet()
{
var (plate, simulation) = Cut(Line(20), new Vector(10, 10));
var p = Thermal();
var heat = new PlateHeatSimulation(plate, simulation, p);
heat.RunToEnd(TimeSpan.FromSeconds(30));
var expected = p.AbsorbedPower * TorchOnSeconds(simulation);
Assert.Equal(expected, TotalHeat(heat, p), expected * 1e-4);
}
[Fact]
public void SurfaceLossDecaysHeatExponentially()
{
var (plate, simulation) = Cut(Line(20), new Vector(10, 10));
var p = Thermal(surfaceCoefficient: 20);
var heat = new PlateHeatSimulation(plate, simulation, p);
heat.RunToEnd();
var atEnd = TotalHeat(heat, p);
heat.RunToEnd(TimeSpan.FromSeconds(120));
var thickness = p.Thickness * 0.0254;
var rate = 2 * p.SurfaceCoefficient / (p.Material.Density * p.Material.SpecificHeat * thickness);
Assert.Equal(System.Math.Exp(-rate * 120), TotalHeat(heat, p) / atEnd, 3);
}
[Fact]
public void HeatSpreadsFromTheCut()
{
var (plate, simulation) = Cut(Line(4), new Vector(20, 20));
var heat = new PlateHeatSimulation(plate, simulation, Thermal());
heat.RunToEnd();
var near = heat.CellAt(new Vector(22, 20.1));
var away = heat.CellAt(new Vector(22, 23.1));
Assert.True(heat.Temperatures[near] > 0);
var awayAtEnd = heat.Temperatures[away];
heat.RunToEnd(TimeSpan.FromSeconds(60));
Assert.True(heat.Temperatures[away] > awayAtEnd);
Assert.True(heat.Temperatures[near] < heat.Peaks[near]);
Assert.True(heat.Peaks[near] > heat.Peaks[away]);
}
[Fact]
public void CutoutSlugFallsAwayWithItsHeat()
{
var (plate, simulation) = Cut(Square(6, clockwise: false), new Vector(10, 10));
var heat = new PlateHeatSimulation(plate, simulation, Thermal());
heat.RunToEnd();
var inside = heat.CellAt(new Vector(13, 13));
Assert.True(heat.IsRemoved(inside));
Assert.Equal(0f, heat.Temperatures[inside]);
Assert.False(heat.IsRemoved(heat.CellAt(new Vector(5, 5))));
}
[Fact]
public void CutFreePartKeepsItsHeatAcrossTheKerf()
{
var (plate, simulation) = Cut(Square(6, clockwise: true), new Vector(10, 10));
var heat = new PlateHeatSimulation(plate, simulation, Thermal());
heat.RunToEnd();
double PartHeat()
{
var sum = 0.0;
for (var i = 0; i < heat.Temperatures.Length; i++)
{
var c = heat.CellCenter(i);
if (c.X > 10 && c.X < 16 && c.Y > 10 && c.Y < 16)
sum += heat.Temperatures[i];
}
return sum;
}
var before = PartHeat();
heat.RunToEnd(TimeSpan.FromSeconds(120));
Assert.True(before > 0);
Assert.False(heat.IsRemoved(heat.CellAt(new Vector(13, 13))));
Assert.Equal(before, PartHeat(), before * 1e-4);
}
[Fact]
public void GoingBackInTimeReplaysTheSameState()
{
var (plate, simulation) = Cut(Square(6, clockwise: false), new Vector(10, 10));
var heat = new PlateHeatSimulation(plate, simulation, Thermal(20));
var middle = TimeSpan.FromTicks(simulation.TotalTime.Ticks / 2);
heat.AdvanceTo(middle);
var first = heat.Temperatures.ToArray();
heat.RunToEnd();
heat.AdvanceTo(middle);
Assert.Equal(first, heat.Temperatures.ToArray());
}
[Theory]
[InlineData("304 Stainless", "Stainless steel")]
[InlineData("SS", "Stainless steel")]
[InlineData("Aluminum 5052", "Aluminum")]
[InlineData("A36", "Mild steel")]
[InlineData("", "Mild steel")]
public void MaterialNamesPickAPreset(string name, string expected) =>
Assert.Equal(expected, ThermalMaterial.ForName(name).Name);
}