mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-05 07:09:25 -05:00
Split only cutting time into short steps, raise the automatic resolution, add an optional ILGPU accelerator with CPU fallback, and estimate heat off the UI thread. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
105 lines
4.6 KiB
C#
105 lines
4.6 KiB
C#
using System;
|
|
using ILGPU;
|
|
using ILGPU.Runtime;
|
|
using OpenNest.CNC.Thermal;
|
|
using Floats = ILGPU.Runtime.ArrayView1D<float, ILGPU.Stride1D.Dense>;
|
|
using Ints = ILGPU.Runtime.ArrayView1D<int, ILGPU.Stride1D.Dense>;
|
|
using Samples = ILGPU.Runtime.ArrayView1D<OpenNest.CNC.Thermal.HeatSample, ILGPU.Stride1D.Dense>;
|
|
|
|
namespace OpenNest.Gpu;
|
|
|
|
/// <summary>The views one diffusion step reads and writes.</summary>
|
|
internal readonly struct StepViews(Floats current, Floats next, Floats peak, Floats cellKeep, Ints links)
|
|
{
|
|
public readonly Floats Current = current;
|
|
public readonly Floats Next = next;
|
|
public readonly Floats Peak = peak;
|
|
public readonly Floats CellKeep = cellKeep;
|
|
public readonly Ints Links = links;
|
|
}
|
|
|
|
/// <summary>The device programs, compiled once. Each mirrors the CPU code it is named after.</summary>
|
|
internal sealed class HeatKernels
|
|
{
|
|
public HeatKernels(Accelerator accelerator)
|
|
{
|
|
Diffuse = accelerator.LoadAutoGroupedKernel<Index1D, StepViews, int, float, float, int>(DiffuseCell);
|
|
Add = accelerator.LoadAutoGroupedKernel<Index1D, Floats, Ints, Samples, SampleRange>(AddSamples);
|
|
Change = accelerator.LoadAutoGroupedKernel<Index1D, Floats, Ints, Ints, Ints, Ints, Ints>(ChangeCell);
|
|
}
|
|
|
|
public Action<AcceleratorStream, Index1D, StepViews, int, float, float, int> Diffuse { get; }
|
|
public Action<AcceleratorStream, Index1D, Floats, Ints, Samples, SampleRange> Add { get; }
|
|
public Action<AcceleratorStream, Index1D, Floats, Ints, Ints, Ints, Ints, Ints> Change { get; }
|
|
|
|
// PlateHeatSimulation.DiffuseCells. A missing link reads the cell itself, so its term is zero
|
|
// and the index stays in the grid.
|
|
private static void DiffuseCell(Index1D index, StepViews views, int columns, float share, float keep, int perCell)
|
|
{
|
|
int i = index;
|
|
var links = views.Links[i];
|
|
var t = views.Current[i];
|
|
var left = views.Current[(links & 1) != 0 ? i - 1 : i];
|
|
var right = views.Current[(links & 2) != 0 ? i + 1 : i];
|
|
var below = views.Current[(links & 4) != 0 ? i - columns : i];
|
|
var above = views.Current[(links & 8) != 0 ? i + columns : i];
|
|
var flow = (left - t) + (right - t) + (below - t) + (above - t);
|
|
var value = (t + share * flow) * (perCell != 0 ? views.CellKeep[i] : keep);
|
|
if (value < PlateHeatSimulation.Negligible)
|
|
value = 0;
|
|
views.Next[i] = value;
|
|
if (value > views.Peak[i])
|
|
views.Peak[i] = value;
|
|
}
|
|
|
|
// PlateHeatSimulation.Add, for the samples of one step. One thread takes them in order, since
|
|
// neighboring samples share cells; a step has only a few.
|
|
private static void AddSamples(Index1D one, Floats temperature, Ints region, Samples samples, SampleRange range)
|
|
{
|
|
for (var i = range.From; i < range.To; i++)
|
|
{
|
|
var sample = samples[i];
|
|
var piece = region[sample.Cell];
|
|
if (piece == -1)
|
|
continue;
|
|
|
|
var columns = range.Columns;
|
|
var left = sample.Left;
|
|
var right = left + 1 < columns ? left + 1 : columns - 1;
|
|
var lower = sample.Lower * columns;
|
|
var upper = (sample.Lower + 1 < range.Rows ? sample.Lower + 1 : range.Rows - 1) * columns;
|
|
var x = sample.X;
|
|
var y = sample.Y;
|
|
var lowerLeft = region[lower + left] == piece ? (1 - x) * (1 - y) : 0f;
|
|
var lowerRight = region[lower + right] == piece ? x * (1 - y) : 0f;
|
|
var upperLeft = region[upper + left] == piece ? (1 - x) * y : 0f;
|
|
var upperRight = region[upper + right] == piece ? x * y : 0f;
|
|
|
|
var rise = sample.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;
|
|
}
|
|
}
|
|
|
|
// New piece and links for a listed cell, and no heat where material fell away.
|
|
private static void ChangeCell(Index1D k, Floats temperature, Ints region, Ints links,
|
|
Ints cells, Ints newRegion, Ints newLinks)
|
|
{
|
|
var cell = cells[k];
|
|
region[cell] = newRegion[k];
|
|
links[cell] = newLinks[k];
|
|
if (newRegion[k] == -1)
|
|
temperature[cell] = 0;
|
|
}
|
|
}
|
|
|
|
internal readonly struct SampleRange(int from, int to, int columns, int rows)
|
|
{
|
|
public readonly int From = from;
|
|
public readonly int To = to;
|
|
public readonly int Columns = columns;
|
|
public readonly int Rows = rows;
|
|
}
|