WebGPU simulation of a liquid quenching to form crystals
Every post so far has simulated a thousand or two atoms, because that is about what you can afford when every atom is compared with every other one. Here there are nineteen thousand, ten times more, and a few things tha…
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
Every post so far has simulated a thousand or two atoms, because that is about what you can afford when every atom is compared with every other one. Here there are nineteen thousand, ten times more, and a few things that were invisible at the smaller scale appear.
Heat the box until it is a liquid, then quench it: cool it so suddenly that the atoms have no time to arrange themselves in an orderly way. It does not freeze into one crystal. Crystals nucleate in many places at once, and each one has its own orientation: the same hexagonal lattice, but turned to a different angle, like tiles laid in different directions. Where two of them meet there is a grain boundary, a seam where the lattice changes direction and the atoms do not fit neatly on either side.
The colour shows the orientation. Every atom looks at the directions of the bonds to its neighbours, which in a crystal all line up with the lattice. A hexagonal lattice looks the same after a turn of 60 degrees, so the angle only matters between 0 and 60, and that range is mapped round the colour wheel. Every grain is its own hue, and the atoms in the boundaries, where the angle is not well defined, fade to grey.
Over time the boundaries move: small grains shrink, big ones grow, and the pattern coarsens. This is the structure of nearly every metal or ceramic you have touched, and it takes thousands of atoms before it shows at all.
Getting there took two changes to the force calculation, which is where the time goes:
Cell lists. An atom only feels neighbours within 2.5 σ, but the all-pairs loop still visits every atom to find them. Instead, divide the box into cells 2.5 σ wide, sort the atoms into cells, and look only in an atom’s own cell and the eight around it. That is about 50 candidates instead of 19,000, so the work grows with the number of atoms, N, instead of N^2. Building the sort on a GPU takes four small passes: count the atoms in each cell with atomicAdd, turn the counts into start offsets with a prefix sum, and scatter each atom’s index into its slot.
Tiling in workgroup memory. In the all-pairs loop every thread in a workgroup reads the same neighbour’s position from slow memory, one atom at a time. A tiled loop has the 64 threads each fetch one position into fast var<workgroup> memory, wait at a barrier, and then all read the whole tile from there. It is the standard trick for an N-body loop, and it is in the benchmark below.
Press Benchmark to time all three kernels on your GPU at several sizes (it pauses the simulation while it runs). Moving a slider does not restart anything.
viewof temperature = { const ui = await import(new URL("../lib/ui.js", document.baseURI).href); return ui.slider({ label: "Temperature", min: 0.1, max: 1.5, step: 0.05, value: 1.3, format: (v) => v.toFixed(2), }); }
viewof colouring = { const ui = await import(new URL("../lib/ui.js", document.baseURI).href); return ui.segmented({ label: "Colour by", options: [ ["orientation", "angle"], // the lattice direction of each grain ["speed", "speed"], ], value: "orientation", }); }
Show code { // Shared WebGPU boilerplate: setup, buffers and the animation loop (lib/gpu.js) const gpu = await import(new URL("../lib/gpu.js", document.baseURI).href); const md = await import(new URL("../lib/md.js", document.baseURI).href); const ui = await import(new URL("../lib/ui.js", document.baseURI).href);
// The controls are read live from their input elements (referencing viewof x
// gives the element, not the value), so moving them does not restart anything.
const temperatureInput = viewof temperature;
const colourInput = viewof colouring;
const { canvas, device, context, format } = await gpu.setup({ width: 640, height: 360, resolution: "auto", });
const dt = 0.005; const stepsPerFrame = 16; const friction = 0.5; // thermostat coupling: how fast velocities are refreshed const decay = Math.exp(-friction * dt); // per-step velocity damping const cutoff = 2.5; // force range in sigma, which is also the cell size
// ---------------------------------------------------------------- the kernels // One shader module holds every kernel, and the sizes live in a uniform, so the // same pipelines run the main simulation and the benchmark's different sizes. const module = device.createShaderModule({ code: ` const DT = ${dt}; const RC2 = ${cutoff * cutoff}; // force cutoff, squared const SHELL2 = 1.96; // first neighbour shell, 1.4 sigma, squared const DECAY = ${decay}; // thermostat: velocity kept each step const TAU = 6.2831853;
struct Sim { box: vec2f, // box size grid: vec2u, // cells across and up count: u32, // atoms cells: u32, // grid.x * grid.y noise: f32, // thermostat kick size pad: f32, }
@group(0) @binding(0) var<storage, read_write> positions: array<vec2f>; @group(0) @binding(1) var<storage, read_write> velocities: array<vec2f>; @group(0) @binding(2) var<storage, read_write> accelerations: array<vec2f>; @group(0) @binding(3) var<storage, read_write> rng: array<u32>; @group(0) @binding(4) var<uniform> sim: Sim; // (order, orientation) per atom: |psi6| and the angle of its bonds, 0 to 1 @group(0) @binding(5) var<storage, read_write> orders: array<vec2f>; // Cell lists: first the count in each cell, then (after the scan) the index of // the first sorted atom in each cell, with one extra entry for the total @group(0) @binding(6) var<storage, read_write> cellStart: array<atomic<u32>>; @group(0) @binding(7) var<storage, read_write> cellInfo: array<vec2u>; // cell, rank @group(0) @binding(8) var<storage, read_write> sorted: array<u32>; // atom ids
${md.rngWGSL}
// ---- shared pair maths fn minImage(d: vec2f) -> vec2f { return d - sim.box * round(d / sim.box); }
struct Pair { a: vec2f, bond: vec2f, shell: f32 }
// Force on an atom from a neighbour at separation d (and its contribution to // the bond-orientational order, see the previous post) fn pair(d: vec2f) -> Pair { var out: Pair; let r2 = dot(d, d); if (r2 < RC2) { let s2 = 1.0 / r2; // (sigma / r)^2 let s6 = s2 * s2 * s2; out.a = d * (24.0 * s2 * s6 * (2.0 * s6 - 1.0)); } if (r2 < SHELL2) { let theta = atan2(d.y, d.x); out.bond = vec2f(cos(6.0 * theta), sin(6.0 * theta)); out.shell = 1.0; } return out; }
fn finish(i: u32, a: vec2f, bond: vec2f, shell: f32) { accelerations[i] = a; // An atom needs a real neighbourhood to count as ordered if (shell >= 4.0) { orders[i] = vec2f(length(bond) / shell, atan2(bond.y, bond.x) / TAU + 0.5); } else { orders[i] = vec2f(0.0, 0.5); } }
// ---- 1. all pairs: every atom against every other @compute @workgroup_size(64) fn forcesAll(@builtin(global_invocation_id) id: vec3u) { let i = id.x; if (i >= sim.count) { return; } let here = positions[i]; var a = vec2f(0.0); var bond = vec2f(0.0); var shell = 0.0; for (var j = 0u; j < sim.count; j++) { if (j == i) { continue; } let p = pair(minImage(here - positions[j])); a += p.a; bond += p.bond; shell += p.shell; } finish(i, a, bond, shell); }
// ---- 2. all pairs, tiled: the same sum, but each group of 64 threads loads // 64 positions into workgroup memory once and reads them from there var<workgroup> tile: array<vec2f, 64>;
@compute @workgroup_size(64) fn forcesTiled( @builtin(global_invocation_id) id: vec3u, @builtin(local_invocation_id) local: vec3u, ) { let i = id.x; let alive = i < sim.count; // Threads past the end still have to take part in the loads and barriers let here = positions[min(i, sim.count - 1u)]; var a = vec2f(0.0); var bond = vec2f(0.0); var shell = 0.0;
let tiles = (sim.count + 63u) / 64u; for (var t = 0u; t < tiles; t++) { tile[local.x] = positions[min(t * 64u + local.x, sim.count - 1u)]; workgroupBarrier(); // everyone's load is done before anyone reads the tile let n = min(64u, sim.count - t * 64u); for (var k = 0u; k < n; k++) { let j = t * 64u + k; if (j != i) { let p = pair(minImage(here - tile[k])); a += p.a; bond += p.bond; shell += p.shell; } } workgroupBarrier(); // everyone is done reading before the tile is reused } if (alive) { finish(i, a, bond, shell); } }
// ---- 3. cell lists: build the grid, then look only in nearby cells fn cellOf(p: vec2f) -> u32 { let size = sim.box / vec2f(sim.grid); let c = min(vec2u(max(p, vec2f(0.0)) / size), sim.grid - vec2u(1u)); return c.y * sim.grid.x + c.x; }
@compute @workgroup_size(64) fn clearCells(@builtin(global_invocation_id) id: vec3u) { if (id.x <= sim.cells) { atomicStore(&cellStart[id.x], 0u); } }
// Count the atoms in each cell. The value atomicAdd returns is the atom's // rank within its cell: no two atoms in a cell get the same one. @compute @workgroup_size(64) fn assign(@builtin(global_invocation_id) id: vec3u) { let i = id.x; if (i >= sim.count) { return; } let c = cellOf(positions[i]); cellInfo[i] = vec2u(c, atomicAdd(&cellStart[c], 1u)); }
// Exclusive prefix sum of the counts, in place: cellStart[c] becomes the number // of atoms in all earlier cells. One thread; there are only a few thousand cells. @compute @workgroup_size(1) fn scan() { var sum = 0u; for (var c = 0u; c < sim.cells; c++) { let n = atomicLoad(&cellStart[c]); atomicStore(&cellStart[c], sum); sum += n; } atomicStore(&cellStart[sim.cells], sum); }
// Put each atom's index at its cell's start plus its rank @compute @workgroup_size(64) fn scatter(@builtin(global_invocation_id) id: vec3u) { let i = id.x; if (i >= sim.count) { return; } sorted[atomicLoad(&cellStart[cellInfo[i].x]) + cellInfo[i].y] = i; }
@compute @workgroup_size(64) fn forcesCells(@builtin(global_invocation_id) id: vec3u) { let i = id.x; if (i >= sim.count) { return; } let here = positions[i]; let cell = cellInfo[i].x; let cx = i32(cell % sim.grid.x); let cy = i32(cell / sim.grid.x); let gx = i32(sim.grid.x); let gy = i32(sim.grid.y);
var a = vec2f(0.0); var bond = vec2f(0.0); var shell = 0.0; for (var oy = -1; oy <= 1; oy++) { for (var ox = -1; ox <= 1; ox++) { // The neighbouring cell, wrapping round the periodic box let nx = (cx + ox + gx) % gx; let ny = (cy + oy + gy) % gy; let c = u32(ny) * sim.grid.x + u32(nx); let end = atomicLoad(&cellStart[c + 1u]); for (var k = atomicLoad(&cellStart[c]); k < end; k++) { let j = sorted[k]; if (j == i) { continue; } let p = pair(minImage(here - positions[j])); a += p.a; bond += p.bond; shell += p.shell; } } } finish(i, a, bond, shell); }
// ---- the rest of a step @compute @workgroup_size(64) fn integrate(@builtin(global_invocation_id) id: vec3u) { let i = id.x; if (i >= sim.count) { return; }
// Leapfrog: kick the velocity... var v = velocities[i] + accelerations[i] * DT;
// ...thermostat: slow it a little and add a random kick... var state = rng[i]; v = v * DECAY + sim.noise * gaussian2(&state); rng[i] = state;
// ...then drift the position, wrapping round the periodic box var p = positions[i] + v * DT; p -= sim.box * floor(p / sim.box);
velocities[i] = v; positions[i] = p; } `, });
// One bind group layout for every kernel, so one bind group serves them all const storage = { type: "storage" }; const layoutEntries = [0, 1, 2, 3, 5, 6, 7, 8].map((binding) => ({ binding, visibility: GPUShaderStage.COMPUTE, buffer: storage, })); layoutEntries.push({ binding: 4, visibility: GPUShaderStage.COMPUTE, buffer: { type: "uniform" }, }); const bindGroupLayout = device.createBindGroupLayout({ entries: layoutEntries }); const pipelineLayout = device.createPipelineLayout({ bindGroupLayouts: [bindGroupLayout] }); const pipelines = Object.fromEntries( [ "forcesAll", "forcesTiled", "forcesCells", "clearCells", "assign", "scan", "scatter", "integrate", ].map((entryPoint) => [ entryPoint, device.createComputePipeline({ layout: pipelineLayout, compute: { module, entryPoint }, }), ]) );
// One random-number-generator seed per atom. crypto.getRandomValues refuses more // than 64 KB at a tim
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。