Water Tank

A water-flow experiment in a tank with a low barrier. This is a rough demo, not a validated fluid model.

Published

No video preview yet

Source

use "@hyle/maths";
use "@hyle/linalg";
use "@hyle/stats";

// Wave Basin: D3Q19 free-surface lattice Boltzmann water.
// Maintained directly as ordinary Hyle; validate stencil changes with the LBM tests.
// Source: Thuerey (2007), chapter 4, https://www10.cs.fau.de/publications/dissertations/Diss_2007-Thuerey.pdf
// BGK + Guo gravity, atmospheric reconstruction and conservative interface motion.
// Lattice units: dx=dt=rho_air=1, g=0.0008, tau=0.6, nu=(tau-0.5)/3.
// kind: 0 gas, 1 interface, 2 liquid. Model names record painted defaults only.
// mass is actual liquid; rho is pressure-bearing density, not volume fraction.
// mass + reserve is conserved; reserve carries signed conversion roundoff/excess.
// populations[0]..populations[18] store the D3Q19 populations. Other fields are phase scratch, also included in bakes.
// The finite +/-1e6 guards serve compiler interval checking, not stabilization.
// No surface tension, gas dynamics, calibrated SI units or smooth surface mesh.
// Ordered D3Q19 populations share one fixed-size value contract.
type Populations = [Float; 19];
// Spatial vectors keep axis components together without imposing vector operators.
struct Vector3 { x: Float; y: Float; z: Float; }
fn lengthSquared(value: Vector3) -> Float {
	let components = [value.x, value.y, value.z];
	return linalg::dot(components, components);
}
property FreeSurfaceWater {
	mass: Float = 0.0;
	reserve: Float = 0.0;
	// Velocity in lattice cells per step, recovered from the populations.
	velocity: Vector3 = Vector3 { x: 0.0, y: 0.0, z: 0.0 };
	share: Float = 0.0;
	carried: Float = 0.0;
	// Central differences of liquid fill, pointing toward the wet side.
	surfaceGradient: Vector3 = Vector3 { x: 0.0, y: 0.0, z: 0.0 };
	targetMass: Float = 0.0;
	populations: Populations = [0.3333333333333333, 0.027777777777777776, 0.027777777777777776, 0.05555555555555555, 0.027777777777777776, 0.027777777777777776, 0.027777777777777776, 0.05555555555555555, 0.027777777777777776, 0.05555555555555555, 0.05555555555555555, 0.027777777777777776, 0.05555555555555555, 0.027777777777777776, 0.027777777777777776, 0.027777777777777776, 0.05555555555555555, 0.027777777777777776, 0.027777777777777776];
	rho: Float [0.000001, 4.0] = 1.0;
	gravity: Float [0.0, 0.001] = 0.0008;
	tau: Float [0.6, 2.0] = 0.6;
	kind: Float [0.0, 2.0] = 0.0;
	nextKind: Float [0.0, 2.0] = 0.0;
	rank: Float [0.0, 2.0] = 0.0;
	wet: Float [0.0, 1.0] = 0.0;
	fluid: Float [0.0, 1.0] = 0.0;
	interface: Float [0.0, 1.0] = 0.0;
	gas: Float [0.0, 1.0] = 0.0;
	fill: Float [0.0, 1.0] = 0.0;
	filling: Float [0.0, 1.0] = 0.0;
	emptying: Float [0.0, 1.0] = 0.0;
	recipient: Float [0.0, 1.0] = 0.0;
	unstable: Float [0.0, 1.0] = 0.0;
}
property TankGlass { thickness: Float = 1.0; }
property TankStone { hardness: Float = 1.0; }
model Glass : TankGlass;
model LbmMedium : FreeSurfaceWater;
model Stone : TankStone;
model Water : FreeSurfaceWater { mass = 1.0; kind = 2.0; fill = 1.0; wet = 1.0; fluid = 1.0; };
neighborhood D3Q19 {
	D1 = [-1, -1, 0];
	D2 = [-1, 0, -1];
	D3 = [-1, 0, 0];
	D4 = [-1, 0, 1];
	D5 = [-1, 1, 0];
	D6 = [0, -1, -1];
	D7 = [0, -1, 0];
	D8 = [0, -1, 1];
	D9 = [0, 0, -1];
	D10 = [0, 0, 1];
	D11 = [0, 1, -1];
	D12 = [0, 1, 0];
	D13 = [0, 1, 1];
	D14 = [1, -1, 0];
	D15 = [1, 0, -1];
	D16 = [1, 0, 0];
	D17 = [1, 0, 1];
	D18 = [1, 1, 0];
}
world { dimensions = 3; neighborhood = D3Q19; }

// D3Q19 quadrature weights. Keep the original binary64 coefficients exactly.
const restWeight: Float = 0.3333333333333333;
const axisWeight: Float = 0.05555555555555555;
const diagonalWeight: Float = 0.027777777777777776;

// Second-order equilibrium and Guo forcing; cu is the lattice direction dot velocity.
// Pure scalar functions expand identically for CPU and GPU execution.
fn equilibrium(weight: Float, density: Float, cu: Float, speedSquared: Float) -> Float {
	return weight * density * (1.0 + 3.0 * cu + 4.5 * cu * cu - 1.5 * speedSquared);
}
fn gravityForce(weight: Float, omega: Float, density: Float, gravity: Float, uz: Float, cu: Float, directionZ: Float) -> Float {
	return weight * (1.0 - omega * 0.5) * density * (-gravity) * (3.0 * (directionZ - uz) + 9.0 * cu * directionZ);
}

behavior RecoverMoments for FreeSurfaceWater {
	update when self.kind > 0.0 {
		// Moments use the D3Q19 direction ordering declared above.
		let density = stats::sum(self.populations);
		set self.rho = maths::clip(density, 0.000001, 4.0);
		set self.velocity.x = maths::clip(linalg::dot(self.populations, [0.0, -1.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0]) / maths::maximum(density, 0.000001), -1000000.0, 1000000.0);
		set self.velocity.y = maths::clip(linalg::dot(self.populations, [0.0, -1.0, 0.0, 0.0, 0.0, 1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0, -1.0, 0.0, 0.0, 0.0, 1.0]) / maths::maximum(density, 0.000001), -1000000.0, 1000000.0);
		set self.velocity.z = maths::clip(linalg::dot(self.populations, [0.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, -1.0, 1.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0]) / maths::maximum(density, 0.000001) - self.gravity * 0.5, -1000000.0, 1000000.0);
	}
}
behavior CollideD3Q19 for FreeSurfaceWater {
	update when self.kind > 0.0 {
		let omega = 1.0 / self.tau;
		let cu0 = 0.0;
		let equilibrium0 = equilibrium(restWeight, self.rho, 0.0, lengthSquared(self.velocity));
		let force0 = gravityForce(restWeight, omega, self.rho, self.gravity, self.velocity.z, cu0, 0.0);
		let cu1 = (-self.velocity.x) + (-self.velocity.y);
		let equilibrium1 = equilibrium(diagonalWeight, self.rho, (-self.velocity.x) + (-self.velocity.y), lengthSquared(self.velocity));
		let force1 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu1, 0.0);
		let cu2 = (-self.velocity.x) + (-self.velocity.z);
		let equilibrium2 = equilibrium(diagonalWeight, self.rho, (-self.velocity.x) + (-self.velocity.z), lengthSquared(self.velocity));
		let force2 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu2, -1.0);
		let cu3 = (-self.velocity.x);
		let equilibrium3 = equilibrium(axisWeight, self.rho, (-self.velocity.x), lengthSquared(self.velocity));
		let force3 = gravityForce(axisWeight, omega, self.rho, self.gravity, self.velocity.z, cu3, 0.0);
		let cu4 = (-self.velocity.x) + self.velocity.z;
		let equilibrium4 = equilibrium(diagonalWeight, self.rho, (-self.velocity.x) + self.velocity.z, lengthSquared(self.velocity));
		let force4 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu4, 1.0);
		let cu5 = (-self.velocity.x) + self.velocity.y;
		let equilibrium5 = equilibrium(diagonalWeight, self.rho, (-self.velocity.x) + self.velocity.y, lengthSquared(self.velocity));
		let force5 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu5, 0.0);
		let cu6 = (-self.velocity.y) + (-self.velocity.z);
		let equilibrium6 = equilibrium(diagonalWeight, self.rho, (-self.velocity.y) + (-self.velocity.z), lengthSquared(self.velocity));
		let force6 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu6, -1.0);
		let cu7 = (-self.velocity.y);
		let equilibrium7 = equilibrium(axisWeight, self.rho, (-self.velocity.y), lengthSquared(self.velocity));
		let force7 = gravityForce(axisWeight, omega, self.rho, self.gravity, self.velocity.z, cu7, 0.0);
		let cu8 = (-self.velocity.y) + self.velocity.z;
		let equilibrium8 = equilibrium(diagonalWeight, self.rho, (-self.velocity.y) + self.velocity.z, lengthSquared(self.velocity));
		let force8 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu8, 1.0);
		let cu9 = (-self.velocity.z);
		let equilibrium9 = equilibrium(axisWeight, self.rho, (-self.velocity.z), lengthSquared(self.velocity));
		let force9 = gravityForce(axisWeight, omega, self.rho, self.gravity, self.velocity.z, cu9, -1.0);
		let cu10 = self.velocity.z;
		let equilibrium10 = equilibrium(axisWeight, self.rho, self.velocity.z, lengthSquared(self.velocity));
		let force10 = gravityForce(axisWeight, omega, self.rho, self.gravity, self.velocity.z, cu10, 1.0);
		let cu11 = self.velocity.y + (-self.velocity.z);
		let equilibrium11 = equilibrium(diagonalWeight, self.rho, self.velocity.y + (-self.velocity.z), lengthSquared(self.velocity));
		let force11 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu11, -1.0);
		let cu12 = self.velocity.y;
		let equilibrium12 = equilibrium(axisWeight, self.rho, self.velocity.y, lengthSquared(self.velocity));
		let force12 = gravityForce(axisWeight, omega, self.rho, self.gravity, self.velocity.z, cu12, 0.0);
		let cu13 = self.velocity.y + self.velocity.z;
		let equilibrium13 = equilibrium(diagonalWeight, self.rho, self.velocity.y + self.velocity.z, lengthSquared(self.velocity));
		let force13 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu13, 1.0);
		let cu14 = self.velocity.x + (-self.velocity.y);
		let equilibrium14 = equilibrium(diagonalWeight, self.rho, self.velocity.x + (-self.velocity.y), lengthSquared(self.velocity));
		let force14 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu14, 0.0);
		let cu15 = self.velocity.x + (-self.velocity.z);
		let equilibrium15 = equilibrium(diagonalWeight, self.rho, self.velocity.x + (-self.velocity.z), lengthSquared(self.velocity));
		let force15 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu15, -1.0);
		let cu16 = self.velocity.x;
		let equilibrium16 = equilibrium(axisWeight, self.rho, self.velocity.x, lengthSquared(self.velocity));
		let force16 = gravityForce(axisWeight, omega, self.rho, self.gravity, self.velocity.z, cu16, 0.0);
		let cu17 = self.velocity.x + self.velocity.z;
		let equilibrium17 = equilibrium(diagonalWeight, self.rho, self.velocity.x + self.velocity.z, lengthSquared(self.velocity));
		let force17 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu17, 1.0);
		let cu18 = self.velocity.x + self.velocity.y;
		let equilibrium18 = equilibrium(diagonalWeight, self.rho, self.velocity.x + self.velocity.y, lengthSquared(self.velocity));
		let force18 = gravityForce(diagonalWeight, omega, self.rho, self.gravity, self.velocity.z, cu18, 0.0);
		set self.populations[0] = maths::clip(self.populations[0] + omega * (equilibrium0 - self.populations[0]) + force0, -1000000.0, 1000000.0);
		set self.populations[1] = maths::clip(self.populations[1] + omega * (equilibrium1 - self.populations[1]) + force1, -1000000.0, 1000000.0);
		set self.populations[2] = maths::clip(self.populations[2] + omega * (equilibrium2 - self.populations[2]) + force2, -1000000.0, 1000000.0);
		set self.populations[3] = maths::clip(self.populations[3] + omega * (equilibrium3 - self.populations[3]) + force3, -1000000.0, 1000000.0);
		set self.populations[4] = maths::clip(self.populations[4] + omega * (equilibrium4 - self.populations[4]) + force4, -1000000.0, 1000000.0);
		set self.populations[5] = maths::clip(self.populations[5] + omega * (equilibrium5 - self.populations[5]) + force5, -1000000.0, 1000000.0);
		set self.populations[6] = maths::clip(self.populations[6] + omega * (equilibrium6 - self.populations[6]) + force6, -1000000.0, 1000000.0);
		set self.populations[7] = maths::clip(self.populations[7] + omega * (equilibrium7 - self.populations[7]) + force7, -1000000.0, 1000000.0);
		set self.populations[8] = maths::clip(self.populations[8] + omega * (equilibrium8 - self.populations[8]) + force8, -1000000.0, 1000000.0);
		set self.populations[9] = maths::clip(self.populations[9] + omega * (equilibrium9 - self.populations[9]) + force9, -1000000.0, 1000000.0);
		set self.populations[10] = maths::clip(self.populations[10] + omega * (equilibrium10 - self.populations[10]) + force10, -1000000.0, 1000000.0);
		set self.populations[11] = maths::clip(self.populations[11] + omega * (equilibrium11 - self.populations[11]) + force11, -1000000.0, 1000000.0);
		set self.populations[12] = maths::clip(self.populations[12] + omega * (equilibrium12 - self.populations[12]) + force12, -1000000.0, 1000000.0);
		set self.populations[13] = maths::clip(self.populations[13] + omega * (equilibrium13 - self.populations[13]) + force13, -1000000.0, 1000000.0);
		set self.populations[14] = maths::clip(self.populations[14] + omega * (equilibrium14 - self.populations[14]) + force14, -1000000.0, 1000000.0);
		set self.populations[15] = maths::clip(self.populations[15] + omega * (equilibrium15 - self.populations[15]) + force15, -1000000.0, 1000000.0);
		set self.populations[16] = maths::clip(self.populations[16] + omega * (equilibrium16 - self.populations[16]) + force16, -1000000.0, 1000000.0);
		set self.populations[17] = maths::clip(self.populations[17] + omega * (equilibrium17 - self.populations[17]) + force17, -1000000.0, 1000000.0);
		set self.populations[18] = maths::clip(self.populations[18] + omega * (equilibrium18 - self.populations[18]) + force18, -1000000.0, 1000000.0);
	}
}
behavior StreamBulkLiquid for FreeSurfaceWater
{
	update when self.kind == 2.0 {
		let incoming1 = (neighbors[FreeSurfaceWater using D3Q19, D18].populations[1] else self.populations[18]);
		let incoming2 = (neighbors[FreeSurfaceWater using D3Q19, D17].populations[2] else self.populations[17]);
		let incoming3 = (neighbors[FreeSurfaceWater using D3Q19, D16].populations[3] else self.populations[16]);
		let incoming4 = (neighbors[FreeSurfaceWater using D3Q19, D15].populations[4] else self.populations[15]);
		let incoming5 = (neighbors[FreeSurfaceWater using D3Q19, D14].populations[5] else self.populations[14]);
		let incoming6 = (neighbors[FreeSurfaceWater using D3Q19, D13].populations[6] else self.populations[13]);
		let incoming7 = (neighbors[FreeSurfaceWater using D3Q19, D12].populations[7] else self.populations[12]);
		let incoming8 = (neighbors[FreeSurfaceWater using D3Q19, D11].populations[8] else self.populations[11]);
		let incoming9 = (neighbors[FreeSurfaceWater using D3Q19, D10].populations[9] else self.populations[10]);
		let incoming10 = (neighbors[FreeSurfaceWater using D3Q19, D9].populations[10] else self.populations[9]);
		let incoming11 = (neighbors[FreeSurfaceWater using D3Q19, D8].populations[11] else self.populations[8]);
		let incoming12 = (neighbors[FreeSurfaceWater using D3Q19, D7].populations[12] else self.populations[7]);
		let incoming13 = (neighbors[FreeSurfaceWater using D3Q19, D6].populations[13] else self.populations[6]);
		let incoming14 = (neighbors[FreeSurfaceWater using D3Q19, D5].populations[14] else self.populations[5]);
		let incoming15 = (neighbors[FreeSurfaceWater using D3Q19, D4].populations[15] else self.populations[4]);
		let incoming16 = (neighbors[FreeSurfaceWater using D3Q19, D3].populations[16] else self.populations[3]);
		let incoming17 = (neighbors[FreeSurfaceWater using D3Q19, D2].populations[17] else self.populations[2]);
		let incoming18 = (neighbors[FreeSurfaceWater using D3Q19, D1].populations[18] else self.populations[1]);
		set self.populations[1] = maths::clip(incoming1, -1000000.0, 1000000.0);
		set self.populations[2] = maths::clip(incoming2, -1000000.0, 1000000.0);
		set self.populations[3] = maths::clip(incoming3, -1000000.0, 1000000.0);
		set self.populations[4] = maths::clip(incoming4, -1000000.0, 1000000.0);
		set self.populations[5] = maths::clip(incoming5, -1000000.0, 1000000.0);
		set self.populations[6] = maths::clip(incoming6, -1000000.0, 1000000.0);
		set self.populations[7] = maths::clip(incoming7, -1000000.0, 1000000.0);
		set self.populations[8] = maths::clip(incoming8, -1000000.0, 1000000.0);
		set self.populations[9] = maths::clip(incoming9, -1000000.0, 1000000.0);
		set self.populations[10] = maths::clip(incoming10, -1000000.0, 1000000.0);
		set self.populations[11] = maths::clip(incoming11, -1000000.0, 1000000.0);
		set self.populations[12] = maths::clip(incoming12, -1000000.0, 1000000.0);
		set self.populations[13] = maths::clip(incoming13, -1000000.0, 1000000.0);
		set self.populations[14] = maths::clip(incoming14, -1000000.0, 1000000.0);
		set self.populations[15] = maths::clip(incoming15, -1000000.0, 1000000.0);
		set self.populations[16] = maths::clip(incoming16, -1000000.0, 1000000.0);
		set self.populations[17] = maths::clip(incoming17, -1000000.0, 1000000.0);
		set self.populations[18] = maths::clip(incoming18, -1000000.0, 1000000.0);
		set self.mass = maths::clip(self.mass + (incoming1 - self.populations[18]) + (incoming2 - self.populations[17]) + (incoming3 - self.populations[16]) + (incoming4 - self.populations[15]) + (incoming5 - self.populations[14]) + (incoming6 - self.populations[13]) + (incoming7 - self.populations[12]) + (incoming8 - self.populations[11]) + (incoming9 - self.populations[10]) + (incoming10 - self.populations[9]) + (incoming11 - self.populations[8]) + (incoming12 - self.populations[7]) + (incoming13 - self.populations[6]) + (incoming14 - self.populations[5]) + (incoming15 - self.populations[4]) + (incoming16 - self.populations[3]) + (incoming17 - self.populations[2]) + (incoming18 - self.populations[1]), -1000000.0, 1000000.0);
	}
}
behavior StreamFreeSurface for FreeSurfaceWater
{
	update when self.kind == 1.0 {
		let type1 = (neighbors[FreeSurfaceWater using D3Q19, D18].kind else 3.0);
		let wall1 = maths::maximum(type1 - 2.0, 0.0);
		let gas1 = maths::maximum(1.0 - type1, 0.0);
		let wet1 = maths::minimum(type1, 1.0) - wall1;
		let fluid1 = maths::maximum(0.0, 1.0 - maths::abs(type1 - 2.0));
		let interface1 = maths::maximum(0.0, 1.0 - maths::abs(type1 - 1.0));
		let incoming1 = (neighbors[FreeSurfaceWater using D3Q19, D18].populations[1] else 0.0);
		let reconstructed1 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x) + (-self.velocity.y)) + 4.5 * ((-self.velocity.x) + (-self.velocity.y)) * ((-self.velocity.x) + (-self.velocity.y)) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.x + self.velocity.y) + 4.5 * (self.velocity.x + self.velocity.y) * (self.velocity.x + self.velocity.y) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[18];
		let inward1 = (-self.surfaceGradient.x) + (-self.surfaceGradient.y);
		let reconstruct1 = wet1 * maths::clip(inward1 * 1000000000000.0, 0.0, 1.0);
		let interfacePair1 = self.interface * interface1;
		let incomingMask1 = 1.0 - interfacePair1 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D18].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask1 = 1.0 - interfacePair1 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D18].rank else 0.0), 0.0, 1.0);
		let area1 = wet1 * maths::maximum(self.fluid, fluid1) + self.interface * interface1 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D18].fill else 0.0));
		let transfer1 = area1 * (incomingMask1 * incoming1 - outgoingMask1 * self.populations[18]);
		let type2 = (neighbors[FreeSurfaceWater using D3Q19, D17].kind else 3.0);
		let wall2 = maths::maximum(type2 - 2.0, 0.0);
		let gas2 = maths::maximum(1.0 - type2, 0.0);
		let wet2 = maths::minimum(type2, 1.0) - wall2;
		let fluid2 = maths::maximum(0.0, 1.0 - maths::abs(type2 - 2.0));
		let interface2 = maths::maximum(0.0, 1.0 - maths::abs(type2 - 1.0));
		let incoming2 = (neighbors[FreeSurfaceWater using D3Q19, D17].populations[2] else 0.0);
		let reconstructed2 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x) + (-self.velocity.z)) + 4.5 * ((-self.velocity.x) + (-self.velocity.z)) * ((-self.velocity.x) + (-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.x + self.velocity.z) + 4.5 * (self.velocity.x + self.velocity.z) * (self.velocity.x + self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[17];
		let inward2 = (-self.surfaceGradient.x) + (-self.surfaceGradient.z);
		let reconstruct2 = wet2 * maths::clip(inward2 * 1000000000000.0, 0.0, 1.0);
		let interfacePair2 = self.interface * interface2;
		let incomingMask2 = 1.0 - interfacePair2 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D17].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask2 = 1.0 - interfacePair2 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D17].rank else 0.0), 0.0, 1.0);
		let area2 = wet2 * maths::maximum(self.fluid, fluid2) + self.interface * interface2 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D17].fill else 0.0));
		let transfer2 = area2 * (incomingMask2 * incoming2 - outgoingMask2 * self.populations[17]);
		let type3 = (neighbors[FreeSurfaceWater using D3Q19, D16].kind else 3.0);
		let wall3 = maths::maximum(type3 - 2.0, 0.0);
		let gas3 = maths::maximum(1.0 - type3, 0.0);
		let wet3 = maths::minimum(type3, 1.0) - wall3;
		let fluid3 = maths::maximum(0.0, 1.0 - maths::abs(type3 - 2.0));
		let interface3 = maths::maximum(0.0, 1.0 - maths::abs(type3 - 1.0));
		let incoming3 = (neighbors[FreeSurfaceWater using D3Q19, D16].populations[3] else 0.0);
		let reconstructed3 = (0.05555555555555555 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x)) + 4.5 * ((-self.velocity.x)) * ((-self.velocity.x)) - 1.5 * (lengthSquared(self.velocity)))) + (0.05555555555555555 * 1.0 * (1.0 + 3.0 * (self.velocity.x) + 4.5 * (self.velocity.x) * (self.velocity.x) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[16];
		let inward3 = (-self.surfaceGradient.x);
		let reconstruct3 = wet3 * maths::clip(inward3 * 1000000000000.0, 0.0, 1.0);
		let interfacePair3 = self.interface * interface3;
		let incomingMask3 = 1.0 - interfacePair3 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D16].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask3 = 1.0 - interfacePair3 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D16].rank else 0.0), 0.0, 1.0);
		let area3 = wet3 * maths::maximum(self.fluid, fluid3) + self.interface * interface3 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D16].fill else 0.0));
		let transfer3 = area3 * (incomingMask3 * incoming3 - outgoingMask3 * self.populations[16]);
		let type4 = (neighbors[FreeSurfaceWater using D3Q19, D15].kind else 3.0);
		let wall4 = maths::maximum(type4 - 2.0, 0.0);
		let gas4 = maths::maximum(1.0 - type4, 0.0);
		let wet4 = maths::minimum(type4, 1.0) - wall4;
		let fluid4 = maths::maximum(0.0, 1.0 - maths::abs(type4 - 2.0));
		let interface4 = maths::maximum(0.0, 1.0 - maths::abs(type4 - 1.0));
		let incoming4 = (neighbors[FreeSurfaceWater using D3Q19, D15].populations[4] else 0.0);
		let reconstructed4 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x) + self.velocity.z) + 4.5 * ((-self.velocity.x) + self.velocity.z) * ((-self.velocity.x) + self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.x + (-self.velocity.z)) + 4.5 * (self.velocity.x + (-self.velocity.z)) * (self.velocity.x + (-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[15];
		let inward4 = (-self.surfaceGradient.x) + self.surfaceGradient.z;
		let reconstruct4 = wet4 * maths::clip(inward4 * 1000000000000.0, 0.0, 1.0);
		let interfacePair4 = self.interface * interface4;
		let incomingMask4 = 1.0 - interfacePair4 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D15].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask4 = 1.0 - interfacePair4 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D15].rank else 0.0), 0.0, 1.0);
		let area4 = wet4 * maths::maximum(self.fluid, fluid4) + self.interface * interface4 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D15].fill else 0.0));
		let transfer4 = area4 * (incomingMask4 * incoming4 - outgoingMask4 * self.populations[15]);
		let type5 = (neighbors[FreeSurfaceWater using D3Q19, D14].kind else 3.0);
		let wall5 = maths::maximum(type5 - 2.0, 0.0);
		let gas5 = maths::maximum(1.0 - type5, 0.0);
		let wet5 = maths::minimum(type5, 1.0) - wall5;
		let fluid5 = maths::maximum(0.0, 1.0 - maths::abs(type5 - 2.0));
		let interface5 = maths::maximum(0.0, 1.0 - maths::abs(type5 - 1.0));
		let incoming5 = (neighbors[FreeSurfaceWater using D3Q19, D14].populations[5] else 0.0);
		let reconstructed5 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x) + self.velocity.y) + 4.5 * ((-self.velocity.x) + self.velocity.y) * ((-self.velocity.x) + self.velocity.y) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.x + (-self.velocity.y)) + 4.5 * (self.velocity.x + (-self.velocity.y)) * (self.velocity.x + (-self.velocity.y)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[14];
		let inward5 = (-self.surfaceGradient.x) + self.surfaceGradient.y;
		let reconstruct5 = wet5 * maths::clip(inward5 * 1000000000000.0, 0.0, 1.0);
		let interfacePair5 = self.interface * interface5;
		let incomingMask5 = 1.0 - interfacePair5 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D14].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask5 = 1.0 - interfacePair5 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D14].rank else 0.0), 0.0, 1.0);
		let area5 = wet5 * maths::maximum(self.fluid, fluid5) + self.interface * interface5 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D14].fill else 0.0));
		let transfer5 = area5 * (incomingMask5 * incoming5 - outgoingMask5 * self.populations[14]);
		let type6 = (neighbors[FreeSurfaceWater using D3Q19, D13].kind else 3.0);
		let wall6 = maths::maximum(type6 - 2.0, 0.0);
		let gas6 = maths::maximum(1.0 - type6, 0.0);
		let wet6 = maths::minimum(type6, 1.0) - wall6;
		let fluid6 = maths::maximum(0.0, 1.0 - maths::abs(type6 - 2.0));
		let interface6 = maths::maximum(0.0, 1.0 - maths::abs(type6 - 1.0));
		let incoming6 = (neighbors[FreeSurfaceWater using D3Q19, D13].populations[6] else 0.0);
		let reconstructed6 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.y) + (-self.velocity.z)) + 4.5 * ((-self.velocity.y) + (-self.velocity.z)) * ((-self.velocity.y) + (-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.y + self.velocity.z) + 4.5 * (self.velocity.y + self.velocity.z) * (self.velocity.y + self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[13];
		let inward6 = (-self.surfaceGradient.y) + (-self.surfaceGradient.z);
		let reconstruct6 = wet6 * maths::clip(inward6 * 1000000000000.0, 0.0, 1.0);
		let interfacePair6 = self.interface * interface6;
		let incomingMask6 = 1.0 - interfacePair6 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D13].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask6 = 1.0 - interfacePair6 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D13].rank else 0.0), 0.0, 1.0);
		let area6 = wet6 * maths::maximum(self.fluid, fluid6) + self.interface * interface6 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D13].fill else 0.0));
		let transfer6 = area6 * (incomingMask6 * incoming6 - outgoingMask6 * self.populations[13]);
		let type7 = (neighbors[FreeSurfaceWater using D3Q19, D12].kind else 3.0);
		let wall7 = maths::maximum(type7 - 2.0, 0.0);
		let gas7 = maths::maximum(1.0 - type7, 0.0);
		let wet7 = maths::minimum(type7, 1.0) - wall7;
		let fluid7 = maths::maximum(0.0, 1.0 - maths::abs(type7 - 2.0));
		let interface7 = maths::maximum(0.0, 1.0 - maths::abs(type7 - 1.0));
		let incoming7 = (neighbors[FreeSurfaceWater using D3Q19, D12].populations[7] else 0.0);
		let reconstructed7 = (0.05555555555555555 * 1.0 * (1.0 + 3.0 * ((-self.velocity.y)) + 4.5 * ((-self.velocity.y)) * ((-self.velocity.y)) - 1.5 * (lengthSquared(self.velocity)))) + (0.05555555555555555 * 1.0 * (1.0 + 3.0 * (self.velocity.y) + 4.5 * (self.velocity.y) * (self.velocity.y) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[12];
		let inward7 = (-self.surfaceGradient.y);
		let reconstruct7 = wet7 * maths::clip(inward7 * 1000000000000.0, 0.0, 1.0);
		let interfacePair7 = self.interface * interface7;
		let incomingMask7 = 1.0 - interfacePair7 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D12].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask7 = 1.0 - interfacePair7 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D12].rank else 0.0), 0.0, 1.0);
		let area7 = wet7 * maths::maximum(self.fluid, fluid7) + self.interface * interface7 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D12].fill else 0.0));
		let transfer7 = area7 * (incomingMask7 * incoming7 - outgoingMask7 * self.populations[12]);
		let type8 = (neighbors[FreeSurfaceWater using D3Q19, D11].kind else 3.0);
		let wall8 = maths::maximum(type8 - 2.0, 0.0);
		let gas8 = maths::maximum(1.0 - type8, 0.0);
		let wet8 = maths::minimum(type8, 1.0) - wall8;
		let fluid8 = maths::maximum(0.0, 1.0 - maths::abs(type8 - 2.0));
		let interface8 = maths::maximum(0.0, 1.0 - maths::abs(type8 - 1.0));
		let incoming8 = (neighbors[FreeSurfaceWater using D3Q19, D11].populations[8] else 0.0);
		let reconstructed8 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.y) + self.velocity.z) + 4.5 * ((-self.velocity.y) + self.velocity.z) * ((-self.velocity.y) + self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.y + (-self.velocity.z)) + 4.5 * (self.velocity.y + (-self.velocity.z)) * (self.velocity.y + (-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[11];
		let inward8 = (-self.surfaceGradient.y) + self.surfaceGradient.z;
		let reconstruct8 = wet8 * maths::clip(inward8 * 1000000000000.0, 0.0, 1.0);
		let interfacePair8 = self.interface * interface8;
		let incomingMask8 = 1.0 - interfacePair8 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D11].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask8 = 1.0 - interfacePair8 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D11].rank else 0.0), 0.0, 1.0);
		let area8 = wet8 * maths::maximum(self.fluid, fluid8) + self.interface * interface8 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D11].fill else 0.0));
		let transfer8 = area8 * (incomingMask8 * incoming8 - outgoingMask8 * self.populations[11]);
		let type9 = (neighbors[FreeSurfaceWater using D3Q19, D10].kind else 3.0);
		let wall9 = maths::maximum(type9 - 2.0, 0.0);
		let gas9 = maths::maximum(1.0 - type9, 0.0);
		let wet9 = maths::minimum(type9, 1.0) - wall9;
		let fluid9 = maths::maximum(0.0, 1.0 - maths::abs(type9 - 2.0));
		let interface9 = maths::maximum(0.0, 1.0 - maths::abs(type9 - 1.0));
		let incoming9 = (neighbors[FreeSurfaceWater using D3Q19, D10].populations[9] else 0.0);
		let reconstructed9 = (0.05555555555555555 * 1.0 * (1.0 + 3.0 * ((-self.velocity.z)) + 4.5 * ((-self.velocity.z)) * ((-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) + (0.05555555555555555 * 1.0 * (1.0 + 3.0 * (self.velocity.z) + 4.5 * (self.velocity.z) * (self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[10];
		let inward9 = (-self.surfaceGradient.z);
		let reconstruct9 = wet9 * maths::clip(inward9 * 1000000000000.0, 0.0, 1.0);
		let interfacePair9 = self.interface * interface9;
		let incomingMask9 = 1.0 - interfacePair9 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D10].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask9 = 1.0 - interfacePair9 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D10].rank else 0.0), 0.0, 1.0);
		let area9 = wet9 * maths::maximum(self.fluid, fluid9) + self.interface * interface9 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D10].fill else 0.0));
		let transfer9 = area9 * (incomingMask9 * incoming9 - outgoingMask9 * self.populations[10]);
		let type10 = (neighbors[FreeSurfaceWater using D3Q19, D9].kind else 3.0);
		let wall10 = maths::maximum(type10 - 2.0, 0.0);
		let gas10 = maths::maximum(1.0 - type10, 0.0);
		let wet10 = maths::minimum(type10, 1.0) - wall10;
		let fluid10 = maths::maximum(0.0, 1.0 - maths::abs(type10 - 2.0));
		let interface10 = maths::maximum(0.0, 1.0 - maths::abs(type10 - 1.0));
		let incoming10 = (neighbors[FreeSurfaceWater using D3Q19, D9].populations[10] else 0.0);
		let reconstructed10 = (0.05555555555555555 * 1.0 * (1.0 + 3.0 * (self.velocity.z) + 4.5 * (self.velocity.z) * (self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) + (0.05555555555555555 * 1.0 * (1.0 + 3.0 * ((-self.velocity.z)) + 4.5 * ((-self.velocity.z)) * ((-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[9];
		let inward10 = self.surfaceGradient.z;
		let reconstruct10 = wet10 * maths::clip(inward10 * 1000000000000.0, 0.0, 1.0);
		let interfacePair10 = self.interface * interface10;
		let incomingMask10 = 1.0 - interfacePair10 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D9].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask10 = 1.0 - interfacePair10 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D9].rank else 0.0), 0.0, 1.0);
		let area10 = wet10 * maths::maximum(self.fluid, fluid10) + self.interface * interface10 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D9].fill else 0.0));
		let transfer10 = area10 * (incomingMask10 * incoming10 - outgoingMask10 * self.populations[9]);
		let type11 = (neighbors[FreeSurfaceWater using D3Q19, D8].kind else 3.0);
		let wall11 = maths::maximum(type11 - 2.0, 0.0);
		let gas11 = maths::maximum(1.0 - type11, 0.0);
		let wet11 = maths::minimum(type11, 1.0) - wall11;
		let fluid11 = maths::maximum(0.0, 1.0 - maths::abs(type11 - 2.0));
		let interface11 = maths::maximum(0.0, 1.0 - maths::abs(type11 - 1.0));
		let incoming11 = (neighbors[FreeSurfaceWater using D3Q19, D8].populations[11] else 0.0);
		let reconstructed11 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.y + (-self.velocity.z)) + 4.5 * (self.velocity.y + (-self.velocity.z)) * (self.velocity.y + (-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.y) + self.velocity.z) + 4.5 * ((-self.velocity.y) + self.velocity.z) * ((-self.velocity.y) + self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[8];
		let inward11 = self.surfaceGradient.y + (-self.surfaceGradient.z);
		let reconstruct11 = wet11 * maths::clip(inward11 * 1000000000000.0, 0.0, 1.0);
		let interfacePair11 = self.interface * interface11;
		let incomingMask11 = 1.0 - interfacePair11 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D8].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask11 = 1.0 - interfacePair11 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D8].rank else 0.0), 0.0, 1.0);
		let area11 = wet11 * maths::maximum(self.fluid, fluid11) + self.interface * interface11 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D8].fill else 0.0));
		let transfer11 = area11 * (incomingMask11 * incoming11 - outgoingMask11 * self.populations[8]);
		let type12 = (neighbors[FreeSurfaceWater using D3Q19, D7].kind else 3.0);
		let wall12 = maths::maximum(type12 - 2.0, 0.0);
		let gas12 = maths::maximum(1.0 - type12, 0.0);
		let wet12 = maths::minimum(type12, 1.0) - wall12;
		let fluid12 = maths::maximum(0.0, 1.0 - maths::abs(type12 - 2.0));
		let interface12 = maths::maximum(0.0, 1.0 - maths::abs(type12 - 1.0));
		let incoming12 = (neighbors[FreeSurfaceWater using D3Q19, D7].populations[12] else 0.0);
		let reconstructed12 = (0.05555555555555555 * 1.0 * (1.0 + 3.0 * (self.velocity.y) + 4.5 * (self.velocity.y) * (self.velocity.y) - 1.5 * (lengthSquared(self.velocity)))) + (0.05555555555555555 * 1.0 * (1.0 + 3.0 * ((-self.velocity.y)) + 4.5 * ((-self.velocity.y)) * ((-self.velocity.y)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[7];
		let inward12 = self.surfaceGradient.y;
		let reconstruct12 = wet12 * maths::clip(inward12 * 1000000000000.0, 0.0, 1.0);
		let interfacePair12 = self.interface * interface12;
		let incomingMask12 = 1.0 - interfacePair12 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D7].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask12 = 1.0 - interfacePair12 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D7].rank else 0.0), 0.0, 1.0);
		let area12 = wet12 * maths::maximum(self.fluid, fluid12) + self.interface * interface12 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D7].fill else 0.0));
		let transfer12 = area12 * (incomingMask12 * incoming12 - outgoingMask12 * self.populations[7]);
		let type13 = (neighbors[FreeSurfaceWater using D3Q19, D6].kind else 3.0);
		let wall13 = maths::maximum(type13 - 2.0, 0.0);
		let gas13 = maths::maximum(1.0 - type13, 0.0);
		let wet13 = maths::minimum(type13, 1.0) - wall13;
		let fluid13 = maths::maximum(0.0, 1.0 - maths::abs(type13 - 2.0));
		let interface13 = maths::maximum(0.0, 1.0 - maths::abs(type13 - 1.0));
		let incoming13 = (neighbors[FreeSurfaceWater using D3Q19, D6].populations[13] else 0.0);
		let reconstructed13 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.y + self.velocity.z) + 4.5 * (self.velocity.y + self.velocity.z) * (self.velocity.y + self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.y) + (-self.velocity.z)) + 4.5 * ((-self.velocity.y) + (-self.velocity.z)) * ((-self.velocity.y) + (-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[6];
		let inward13 = self.surfaceGradient.y + self.surfaceGradient.z;
		let reconstruct13 = wet13 * maths::clip(inward13 * 1000000000000.0, 0.0, 1.0);
		let interfacePair13 = self.interface * interface13;
		let incomingMask13 = 1.0 - interfacePair13 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D6].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask13 = 1.0 - interfacePair13 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D6].rank else 0.0), 0.0, 1.0);
		let area13 = wet13 * maths::maximum(self.fluid, fluid13) + self.interface * interface13 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D6].fill else 0.0));
		let transfer13 = area13 * (incomingMask13 * incoming13 - outgoingMask13 * self.populations[6]);
		let type14 = (neighbors[FreeSurfaceWater using D3Q19, D5].kind else 3.0);
		let wall14 = maths::maximum(type14 - 2.0, 0.0);
		let gas14 = maths::maximum(1.0 - type14, 0.0);
		let wet14 = maths::minimum(type14, 1.0) - wall14;
		let fluid14 = maths::maximum(0.0, 1.0 - maths::abs(type14 - 2.0));
		let interface14 = maths::maximum(0.0, 1.0 - maths::abs(type14 - 1.0));
		let incoming14 = (neighbors[FreeSurfaceWater using D3Q19, D5].populations[14] else 0.0);
		let reconstructed14 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.x + (-self.velocity.y)) + 4.5 * (self.velocity.x + (-self.velocity.y)) * (self.velocity.x + (-self.velocity.y)) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x) + self.velocity.y) + 4.5 * ((-self.velocity.x) + self.velocity.y) * ((-self.velocity.x) + self.velocity.y) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[5];
		let inward14 = self.surfaceGradient.x + (-self.surfaceGradient.y);
		let reconstruct14 = wet14 * maths::clip(inward14 * 1000000000000.0, 0.0, 1.0);
		let interfacePair14 = self.interface * interface14;
		let incomingMask14 = 1.0 - interfacePair14 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D5].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask14 = 1.0 - interfacePair14 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D5].rank else 0.0), 0.0, 1.0);
		let area14 = wet14 * maths::maximum(self.fluid, fluid14) + self.interface * interface14 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D5].fill else 0.0));
		let transfer14 = area14 * (incomingMask14 * incoming14 - outgoingMask14 * self.populations[5]);
		let type15 = (neighbors[FreeSurfaceWater using D3Q19, D4].kind else 3.0);
		let wall15 = maths::maximum(type15 - 2.0, 0.0);
		let gas15 = maths::maximum(1.0 - type15, 0.0);
		let wet15 = maths::minimum(type15, 1.0) - wall15;
		let fluid15 = maths::maximum(0.0, 1.0 - maths::abs(type15 - 2.0));
		let interface15 = maths::maximum(0.0, 1.0 - maths::abs(type15 - 1.0));
		let incoming15 = (neighbors[FreeSurfaceWater using D3Q19, D4].populations[15] else 0.0);
		let reconstructed15 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.x + (-self.velocity.z)) + 4.5 * (self.velocity.x + (-self.velocity.z)) * (self.velocity.x + (-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x) + self.velocity.z) + 4.5 * ((-self.velocity.x) + self.velocity.z) * ((-self.velocity.x) + self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[4];
		let inward15 = self.surfaceGradient.x + (-self.surfaceGradient.z);
		let reconstruct15 = wet15 * maths::clip(inward15 * 1000000000000.0, 0.0, 1.0);
		let interfacePair15 = self.interface * interface15;
		let incomingMask15 = 1.0 - interfacePair15 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D4].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask15 = 1.0 - interfacePair15 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D4].rank else 0.0), 0.0, 1.0);
		let area15 = wet15 * maths::maximum(self.fluid, fluid15) + self.interface * interface15 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D4].fill else 0.0));
		let transfer15 = area15 * (incomingMask15 * incoming15 - outgoingMask15 * self.populations[4]);
		let type16 = (neighbors[FreeSurfaceWater using D3Q19, D3].kind else 3.0);
		let wall16 = maths::maximum(type16 - 2.0, 0.0);
		let gas16 = maths::maximum(1.0 - type16, 0.0);
		let wet16 = maths::minimum(type16, 1.0) - wall16;
		let fluid16 = maths::maximum(0.0, 1.0 - maths::abs(type16 - 2.0));
		let interface16 = maths::maximum(0.0, 1.0 - maths::abs(type16 - 1.0));
		let incoming16 = (neighbors[FreeSurfaceWater using D3Q19, D3].populations[16] else 0.0);
		let reconstructed16 = (0.05555555555555555 * 1.0 * (1.0 + 3.0 * (self.velocity.x) + 4.5 * (self.velocity.x) * (self.velocity.x) - 1.5 * (lengthSquared(self.velocity)))) + (0.05555555555555555 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x)) + 4.5 * ((-self.velocity.x)) * ((-self.velocity.x)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[3];
		let inward16 = self.surfaceGradient.x;
		let reconstruct16 = wet16 * maths::clip(inward16 * 1000000000000.0, 0.0, 1.0);
		let interfacePair16 = self.interface * interface16;
		let incomingMask16 = 1.0 - interfacePair16 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D3].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask16 = 1.0 - interfacePair16 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D3].rank else 0.0), 0.0, 1.0);
		let area16 = wet16 * maths::maximum(self.fluid, fluid16) + self.interface * interface16 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D3].fill else 0.0));
		let transfer16 = area16 * (incomingMask16 * incoming16 - outgoingMask16 * self.populations[3]);
		let type17 = (neighbors[FreeSurfaceWater using D3Q19, D2].kind else 3.0);
		let wall17 = maths::maximum(type17 - 2.0, 0.0);
		let gas17 = maths::maximum(1.0 - type17, 0.0);
		let wet17 = maths::minimum(type17, 1.0) - wall17;
		let fluid17 = maths::maximum(0.0, 1.0 - maths::abs(type17 - 2.0));
		let interface17 = maths::maximum(0.0, 1.0 - maths::abs(type17 - 1.0));
		let incoming17 = (neighbors[FreeSurfaceWater using D3Q19, D2].populations[17] else 0.0);
		let reconstructed17 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.x + self.velocity.z) + 4.5 * (self.velocity.x + self.velocity.z) * (self.velocity.x + self.velocity.z) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x) + (-self.velocity.z)) + 4.5 * ((-self.velocity.x) + (-self.velocity.z)) * ((-self.velocity.x) + (-self.velocity.z)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[2];
		let inward17 = self.surfaceGradient.x + self.surfaceGradient.z;
		let reconstruct17 = wet17 * maths::clip(inward17 * 1000000000000.0, 0.0, 1.0);
		let interfacePair17 = self.interface * interface17;
		let incomingMask17 = 1.0 - interfacePair17 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D2].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask17 = 1.0 - interfacePair17 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D2].rank else 0.0), 0.0, 1.0);
		let area17 = wet17 * maths::maximum(self.fluid, fluid17) + self.interface * interface17 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D2].fill else 0.0));
		let transfer17 = area17 * (incomingMask17 * incoming17 - outgoingMask17 * self.populations[2]);
		let type18 = (neighbors[FreeSurfaceWater using D3Q19, D1].kind else 3.0);
		let wall18 = maths::maximum(type18 - 2.0, 0.0);
		let gas18 = maths::maximum(1.0 - type18, 0.0);
		let wet18 = maths::minimum(type18, 1.0) - wall18;
		let fluid18 = maths::maximum(0.0, 1.0 - maths::abs(type18 - 2.0));
		let interface18 = maths::maximum(0.0, 1.0 - maths::abs(type18 - 1.0));
		let incoming18 = (neighbors[FreeSurfaceWater using D3Q19, D1].populations[18] else 0.0);
		let reconstructed18 = (0.027777777777777776 * 1.0 * (1.0 + 3.0 * (self.velocity.x + self.velocity.y) + 4.5 * (self.velocity.x + self.velocity.y) * (self.velocity.x + self.velocity.y) - 1.5 * (lengthSquared(self.velocity)))) + (0.027777777777777776 * 1.0 * (1.0 + 3.0 * ((-self.velocity.x) + (-self.velocity.y)) + 4.5 * ((-self.velocity.x) + (-self.velocity.y)) * ((-self.velocity.x) + (-self.velocity.y)) - 1.5 * (lengthSquared(self.velocity)))) - self.populations[1];
		let inward18 = self.surfaceGradient.x + self.surfaceGradient.y;
		let reconstruct18 = wet18 * maths::clip(inward18 * 1000000000000.0, 0.0, 1.0);
		let interfacePair18 = self.interface * interface18;
		let incomingMask18 = 1.0 - interfacePair18 * maths::clip((neighbors[FreeSurfaceWater using D3Q19, D1].rank else 0.0) - self.rank, 0.0, 1.0);
		let outgoingMask18 = 1.0 - interfacePair18 * maths::clip(self.rank - (neighbors[FreeSurfaceWater using D3Q19, D1].rank else 0.0), 0.0, 1.0);
		let area18 = wet18 * maths::maximum(self.fluid, fluid18) + self.interface * interface18 * 0.5 * (self.fill + (neighbors[FreeSurfaceWater using D3Q19, D1].fill else 0.0));
		let transfer18 = area18 * (incomingMask18 * incoming18 - outgoingMask18 * self.populations[1]);
		set self.populations[1] = maths::clip((wet1 - reconstruct1) * incoming1 + (gas1 + reconstruct1) * reconstructed1 + wall1 * self.populations[18], -1000000.0, 1000000.0);
		set self.populations[2] = maths::clip((wet2 - reconstruct2) * incoming2 + (gas2 + reconstruct2) * reconstructed2 + wall2 * self.populations[17], -1000000.0, 1000000.0);
		set self.populations[3] = maths::clip((wet3 - reconstruct3) * incoming3 + (gas3 + reconstruct3) * reconstructed3 + wall3 * self.populations[16], -1000000.0, 1000000.0);
		set self.populations[4] = maths::clip((wet4 - reconstruct4) * incoming4 + (gas4 + reconstruct4) * reconstructed4 + wall4 * self.populations[15], -1000000.0, 1000000.0);
		set self.populations[5] = maths::clip((wet5 - reconstruct5) * incoming5 + (gas5 + reconstruct5) * reconstructed5 + wall5 * self.populations[14], -1000000.0, 1000000.0);
		set self.populations[6] = maths::clip((wet6 - reconstruct6) * incoming6 + (gas6 + reconstruct6) * reconstructed6 + wall6 * self.populations[13], -1000000.0, 1000000.0);
		set self.populations[7] = maths::clip((wet7 - reconstruct7) * incoming7 + (gas7 + reconstruct7) * reconstructed7 + wall7 * self.populations[12], -1000000.0, 1000000.0);
		set self.populations[8] = maths::clip((wet8 - reconstruct8) * incoming8 + (gas8 + reconstruct8) * reconstructed8 + wall8 * self.populations[11], -1000000.0, 1000000.0);
		set self.populations[9] = maths::clip((wet9 - reconstruct9) * incoming9 + (gas9 + reconstruct9) * reconstructed9 + wall9 * self.populations[10], -1000000.0, 1000000.0);
		set self.populations[10] = maths::clip((wet10 - reconstruct10) * incoming10 + (gas10 + reconstruct10) * reconstructed10 + wall10 * self.populations[9], -1000000.0, 1000000.0);
		set self.populations[11] = maths::clip((wet11 - reconstruct11) * incoming11 + (gas11 + reconstruct11) * reconstructed11 + wall11 * self.populations[8], -1000000.0, 1000000.0);
		set self.populations[12] = maths::clip((wet12 - reconstruct12) * incoming12 + (gas12 + reconstruct12) * reconstructed12 + wall12 * self.populations[7], -1000000.0, 1000000.0);
		set self.populations[13] = maths::clip((wet13 - reconstruct13) * incoming13 + (gas13 + reconstruct13) * reconstructed13 + wall13 * self.populations[6], -1000000.0, 1000000.0);
		set self.populations[14] = maths::clip((wet14 - reconstruct14) * incoming14 + (gas14 + reconstruct14) * reconstructed14 + wall14 * self.populations[5], -1000000.0, 1000000.0);
		set self.populations[15] = maths::clip((wet15 - reconstruct15) * incoming15 + (gas15 + reconstruct15) * reconstructed15 + wall15 * self.populations[4], -1000000.0, 1000000.0);
		set self.populations[16] = maths::clip((wet16 - reconstruct16) * incoming16 + (gas16 + reconstruct16) * reconstructed16 + wall16 * self.populations[3], -1000000.0, 1000000.0);
		set self.populations[17] = maths::clip((wet17 - reconstruct17) * incoming17 + (gas17 + reconstruct17) * reconstructed17 + wall17 * self.populations[2], -1000000.0, 1000000.0);
		set self.populations[18] = maths::clip((wet18 - reconstruct18) * incoming18 + (gas18 + reconstruct18) * reconstructed18 + wall18 * self.populations[1], -1000000.0, 1000000.0);
		set self.mass = maths::clip(self.mass + transfer1 + transfer2 + transfer3 + transfer4 + transfer5 + transfer6 + transfer7 + transfer8 + transfer9 + transfer10 + transfer11 + transfer12 + transfer13 + transfer14 + transfer15 + transfer16 + transfer17 + transfer18, -1000000.0, 1000000.0);
	}
}
// Repair fluid/gas contact after painting, and bootstrap the initial surface.
behavior PrepareSurface for FreeSurfaceWater {
	update {
		set self.wet = maths::minimum(self.kind, 1.0);
		set self.fluid = maths::maximum(self.kind - 1.0, 0.0);
		set self.interface = 1.0 - maths::abs(self.kind - 1.0);
		set self.gas = maths::maximum(1.0 - self.kind, 0.0);
		set self.fill = maths::clip(self.mass / maths::maximum(self.rho, 0.000001), 0.0, 1.0);
	}
}

// Gradient points into the liquid. Rank implements the symmetric interface
// cleanup fluxes in Thuerey's table 4.1: isolated, regular, enclosed.
behavior SurfaceTopology for FreeSurfaceWater
{
	update {
		let hasFluid = maths::minimum(sum n in neighbors[FreeSurfaceWater using D3Q19] { n.fluid }, 1.0);
		let hasGas = maths::minimum(sum n in neighbors[FreeSurfaceWater using D3Q19] { n.gas }, 1.0);
		set self.rank = maths::clip(hasFluid * (2.0 - hasGas), 0.0, 2.0);
		set self.surfaceGradient.x = maths::clip(0.5 * ((neighbors[FreeSurfaceWater using D3Q19, +X].fill else self.fill) - (neighbors[FreeSurfaceWater using D3Q19, -X].fill else self.fill)), -1000000.0, 1000000.0);
		set self.surfaceGradient.y = maths::clip(0.5 * ((neighbors[FreeSurfaceWater using D3Q19, +Y].fill else self.fill) - (neighbors[FreeSurfaceWater using D3Q19, -Y].fill else self.fill)), -1000000.0, 1000000.0);
		set self.surfaceGradient.z = maths::clip(0.5 * ((neighbors[FreeSurfaceWater using D3Q19, +Z].fill else self.fill) - (neighbors[FreeSurfaceWater using D3Q19, -Z].fill else self.fill)), -1000000.0, 1000000.0);
	}
}

behavior RepairSurface for FreeSurfaceWater
{
	update when self.kind == 2.0 && (sum n in neighbors[FreeSurfaceWater using D3Q19] { n.gas }) > 0.0 {
		set self.kind = 1.0;
	}
}

// Proposals are decided from a snapshot before repairing the surrounding band.
behavior ProposeSurface for FreeSurfaceWater {
	update {
		set self.nextKind = self.kind;
		set self.filling = 0.0;
		set self.emptying = 0.0;
	}
}

behavior MarkFilled for FreeSurfaceWater
{
	update when self.kind == 1.0 && (self.mass + self.reserve > 1.001 * self.rho || ((sum n in neighbors[FreeSurfaceWater using D3Q19] { n.gas }) == 0.0 && self.mass > 0.9 * self.rho)) {
		set self.nextKind = 2.0;
		set self.filling = 1.0;
	}
}

behavior MarkEmptied for FreeSurfaceWater
{
	update when self.kind == 1.0 && (self.mass + self.reserve <= -0.001 * self.rho || ((sum n in neighbors[FreeSurfaceWater using D3Q19] { n.fluid }) == 0.0 && self.mass < 0.1 * self.rho)) {
		set self.nextKind = 0.0;
		set self.emptying = 1.0;
	}
}

behavior ProtectFilledNeighbors for FreeSurfaceWater
{
	update when (self.kind == 0.0 || self.nextKind == 0.0) && (sum n in neighbors[FreeSurfaceWater using D3Q19] { n.filling }) > 0.0 {
		set self.nextKind = 1.0;
		set self.emptying = 0.0;
	}
}

behavior ExposeFluidNeighbors for FreeSurfaceWater
{
	update when self.kind == 2.0 && (sum n in neighbors[FreeSurfaceWater using D3Q19] { n.emptying }) > 0.0 {
		set self.nextKind = 1.0;
	}
}

behavior MarkRecipients for FreeSurfaceWater {
	update { set self.recipient = 1.0 - maths::abs(self.nextKind - 1.0); }
}

// Retain positive overshoot until conversion. If topology protection cancels
// emptying, release the deficit to other interfaces but retain the negative
// conversion threshold: clearing it to zero pins the surviving interface.
// Missing recipients keep the excess in reserve; mass + reserve is conserved.
behavior PlanRedistribution for FreeSurfaceWater
{
	update {
		let interfaceMass = maths::maximum(self.mass + self.reserve, -0.001 * self.rho);
		let target = maths::maximum(self.nextKind - 1.0, 0.0) * self.rho + self.recipient * interfaceMass;
		let excess = self.mass - target + self.reserve;
		let count = sum n in neighbors[FreeSurfaceWater using D3Q19] { n.recipient };
		let hasRecipient = maths::minimum(count, 1.0);
		set self.targetMass = maths::clip(target, -1000000.0, 1000000.0);
		set self.share = maths::clip(hasRecipient * excess / maths::maximum(count, 1.0), -1000000.0, 1000000.0);
		set self.carried = maths::clip((1.0 - hasRecipient) * excess, -1000000.0, 1000000.0);
	}
}

// Newly wet cells start at the neighbors' mean density/velocity, without adding
// water. Initializing distributions creates pressure state, not physical mass.
behavior InitializeInterface for FreeSurfaceWater
{
	update when self.kind == 0.0 && self.nextKind == 1.0 {
		let count = maths::maximum(sum n in neighbors[FreeSurfaceWater using D3Q19] { n.wet }, 1.0);
		let density = maths::clip((sum n in neighbors[FreeSurfaceWater using D3Q19] { n.rho * n.wet }) / count, 0.000001, 4.0);
		let u = (sum n in neighbors[FreeSurfaceWater using D3Q19] { n.velocity.x * n.wet }) / count;
		let v = (sum n in neighbors[FreeSurfaceWater using D3Q19] { n.velocity.y * n.wet }) / count;
		let w = (sum n in neighbors[FreeSurfaceWater using D3Q19] { n.velocity.z * n.wet }) / count;
		set self.rho = density;
		set self.velocity.x = maths::clip(u, -1000000.0, 1000000.0);
		set self.velocity.y = maths::clip(v, -1000000.0, 1000000.0);
		set self.velocity.z = maths::clip(w, -1000000.0, 1000000.0);
		set self.populations[0] = maths::clip(0.3333333333333333 * density * (1.0 + 3.0 * (0.0) + 4.5 * (0.0) * (0.0) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[1] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * ((-u) + (-v)) + 4.5 * ((-u) + (-v)) * ((-u) + (-v)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[2] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * ((-u) + (-w)) + 4.5 * ((-u) + (-w)) * ((-u) + (-w)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[3] = maths::clip(0.05555555555555555 * density * (1.0 + 3.0 * ((-u)) + 4.5 * ((-u)) * ((-u)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[4] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * ((-u) + w) + 4.5 * ((-u) + w) * ((-u) + w) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[5] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * ((-u) + v) + 4.5 * ((-u) + v) * ((-u) + v) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[6] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * ((-v) + (-w)) + 4.5 * ((-v) + (-w)) * ((-v) + (-w)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[7] = maths::clip(0.05555555555555555 * density * (1.0 + 3.0 * ((-v)) + 4.5 * ((-v)) * ((-v)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[8] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * ((-v) + w) + 4.5 * ((-v) + w) * ((-v) + w) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[9] = maths::clip(0.05555555555555555 * density * (1.0 + 3.0 * ((-w)) + 4.5 * ((-w)) * ((-w)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[10] = maths::clip(0.05555555555555555 * density * (1.0 + 3.0 * (w) + 4.5 * (w) * (w) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[11] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * (v + (-w)) + 4.5 * (v + (-w)) * (v + (-w)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[12] = maths::clip(0.05555555555555555 * density * (1.0 + 3.0 * (v) + 4.5 * (v) * (v) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[13] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * (v + w) + 4.5 * (v + w) * (v + w) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[14] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * (u + (-v)) + 4.5 * (u + (-v)) * (u + (-v)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[15] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * (u + (-w)) + 4.5 * (u + (-w)) * (u + (-w)) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[16] = maths::clip(0.05555555555555555 * density * (1.0 + 3.0 * (u) + 4.5 * (u) * (u) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[17] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * (u + w) + 4.5 * (u + w) * (u + w) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
		set self.populations[18] = maths::clip(0.027777777777777776 * density * (1.0 + 3.0 * (u + v) + 4.5 * (u + v) * (u + v) - 1.5 * (u * u + v * v + w * w)), -1000000.0, 1000000.0);
	}
}

behavior CommitSurface for FreeSurfaceWater
{
	update {
		let incoming = sum n in neighbors[FreeSurfaceWater using D3Q19] { n.share };
		let raw = self.targetMass + self.recipient * incoming;
		let physicalMass = maths::clip(raw, 0.0, self.rho);
		set self.mass = physicalMass;
		set self.reserve = maths::clip(self.carried + raw - physicalMass, -1000000.0, 1000000.0);
		set self.kind = self.nextKind;
		set self.fill = maths::clip(physicalMass / maths::maximum(self.rho, 0.000001), 0.0, 1.0);
	}
}

// Latch invalid operating conditions visibly; wide interval guards must never
// turn an unstable high-Mach run into an apparently successful simulation.
behavior CheckWaterRegime for FreeSurfaceWater {
	update when self.kind > 0.0 && (self.rho < 0.9 || self.rho > 1.1 || lengthSquared(self.velocity) > 0.04) {
		set self.unstable = 1.0;
	}
}

phase prepare {
	run PrepareSurface on LbmMedium;
	run PrepareSurface on Water;
}

phase repair {
	run RepairSurface on LbmMedium;
	run RepairSurface on Water;
}

phase classify {
	run PrepareSurface on LbmMedium;
	run PrepareSurface on Water;
}

phase moments {
	run RecoverMoments on LbmMedium;
	run RecoverMoments on Water;
}

phase topology {
	run SurfaceTopology on LbmMedium;
	run SurfaceTopology on Water;
}

phase collision {
	run CollideD3Q19 on LbmMedium;
	run CollideD3Q19 on Water;
}

phase stream {
	run StreamBulkLiquid on LbmMedium;
	run StreamBulkLiquid on Water;
	run StreamFreeSurface on LbmMedium;
	run StreamFreeSurface on Water;
}

phase recover {
	run RecoverMoments on LbmMedium;
	run RecoverMoments on Water;
}

phase proposal {
	run ProposeSurface on LbmMedium;
	run ProposeSurface on Water;
}

phase fill_flags {
	run MarkFilled on LbmMedium;
	run MarkFilled on Water;
}

phase empty_flags {
	run MarkEmptied on LbmMedium;
	run MarkEmptied on Water;
}

phase protect {
	run ProtectFilledNeighbors on LbmMedium;
	run ProtectFilledNeighbors on Water;
}

phase expose {
	run ExposeFluidNeighbors on LbmMedium;
	run ExposeFluidNeighbors on Water;
}

phase recipients {
	run MarkRecipients on LbmMedium;
	run MarkRecipients on Water;
}

phase redistribute {
	run PlanRedistribution on LbmMedium;
	run PlanRedistribution on Water;
}

phase initialize {
	run InitializeInterface on LbmMedium;
	run InitializeInterface on Water;
}

phase commit {
	run CommitSurface on LbmMedium;
	run CommitSurface on Water;
}

phase regime {
	run CheckWaterRegime on LbmMedium;
	run CheckWaterRegime on Water;
}
visual LbmWater for FreeSurfaceWater {
	color {
		let speed = maths::clip((maths::abs(self.velocity.x) + maths::abs(self.velocity.y) + maths::abs(self.velocity.z)) * 20.0, 0.0, 1.0);
		// Dark blue is resting water; cyan reveals the low-Mach flow.
		// Opacity follows fractional fill instead of saturating at half a cell.
		// This remains a voxel view, not a reconstructed subcell surface.
		rgba(0.04 + speed * 0.26 + self.unstable * 0.7, 0.28 + speed * 0.6, 0.65 + speed * 0.3, maths::clip(self.fill, 0.0, 0.82))
	}
}
visual ClearTank for TankGlass { color { rgba(0.46, 0.79, 0.90, 0.055) } }
visual PorcelainTank for TankStone { color { rgba(0.48, 0.56, 0.62, 1.0) } }
visualize Glass with ClearTank;
visualize LbmMedium with LbmWater;
visualize Stone with PorcelainTank;
visualize Water with LbmWater;