522 lines
15 KiB
C#
522 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Pcg;
|
|
|
|
namespace Atomorph.FluidModel;
|
|
|
|
internal class FluidModel : IDisposable
|
|
{
|
|
private Particle[]? _particles;
|
|
private uint _gSizeX;
|
|
private uint _gSizeY;
|
|
private uint _particleCount;
|
|
private Node[] _active;
|
|
private uint _activeCount;
|
|
private Material _water;
|
|
|
|
private bool _pressed;
|
|
private bool _pressedPrev;
|
|
|
|
private double _mx;
|
|
private double _my;
|
|
private double _mxPrev;
|
|
private double _myPrev;
|
|
|
|
private Node[][] _grid;
|
|
private Line[] _lines;
|
|
|
|
public IReadOnlyList<Line> Lines => _lines;
|
|
public Material Material => _water;
|
|
public IReadOnlyList<Particle> Particles => _particles;
|
|
private PcgRandom _random = new PcgRandom();
|
|
|
|
public uint ParticleCount => _particleCount;
|
|
|
|
public FluidModel(uint gSizeX, uint gSizeY, uint particleCount)
|
|
{
|
|
_gSizeX = gSizeX;
|
|
_gSizeY = gSizeY;
|
|
_particleCount = particleCount;
|
|
|
|
_water = new Material(1.0, 1.0, 1.0, 1.0, 1.0, 1.0);
|
|
|
|
_grid = new Node[gSizeX][];
|
|
for (uint i = 0; i < gSizeX; i++)
|
|
_grid[i] = new Node[gSizeY];
|
|
for (uint j = 0; j < gSizeY; j++)
|
|
{
|
|
for (uint i = 0; i < gSizeX; i++)
|
|
{
|
|
_grid[i][j] = new Node() { Pos = (int)(j * gSizeX + i), };
|
|
}
|
|
}
|
|
_active = new Node[gSizeX * gSizeY];
|
|
_particles = new Particle[particleCount];
|
|
for (uint i = 0; i < particleCount; i++)
|
|
{
|
|
_particles[i] = new Particle(_water, gSizeX / 2.0, gSizeY / 2.0, 0.0, 0.0);
|
|
}
|
|
_lines = new Line[particleCount];
|
|
}
|
|
|
|
public void SetPressed(bool b) => _pressed = b;
|
|
|
|
public void SetMovePos(double x, double y)
|
|
{
|
|
_mx = x;
|
|
_my = y;
|
|
}
|
|
|
|
|
|
public void Step(ulong stepsLeft, double freedomRadius, double t)
|
|
{
|
|
if (_particles is null)
|
|
throw new InvalidOperationException("Particles not initialized.");
|
|
|
|
var drag = false;
|
|
var mdx = 0.0;
|
|
var mdy = 0.0;
|
|
if (_pressed && _pressedPrev)
|
|
{
|
|
drag = true;
|
|
mdx = _mx - _mxPrev;
|
|
mdy = _my - _myPrev;
|
|
}
|
|
_pressedPrev = _pressed;
|
|
_mxPrev = _mx;
|
|
_myPrev = _my;
|
|
foreach (var node in _active)
|
|
node?.Clear();
|
|
_activeCount = 0;
|
|
|
|
double phi;
|
|
double x, y;
|
|
double fx = 0.0, fy = 0.0;
|
|
|
|
Particle p;
|
|
Node n;
|
|
|
|
for (uint index = 0; index < _particleCount; ++index)
|
|
{
|
|
p = _particles[index];
|
|
if (!p.Active)
|
|
continue;
|
|
p.Cx = (uint)(p.X - 0.5);
|
|
p.Cy = (uint)(p.Y - 0.5);
|
|
|
|
x = p.Cx - p.X;
|
|
|
|
p.Px[0] = (0.5 * x * x + 1.5 * x + 1.125);
|
|
p.Gx[0] = (x + 1.5);
|
|
x += 1.0;
|
|
p.Px[1] = (-x * x + 0.75);
|
|
p.Gx[1] = (-2.0 * x);
|
|
x += 1.0;
|
|
p.Px[2] = (0.5 * x * x - 1.5 * x + 1.125);
|
|
p.Gx[2] = (x - 1.5);
|
|
|
|
y = p.Cy - p.Y;
|
|
|
|
p.Py[0] = (0.5 * y * y + 1.5 * y + 1.125);
|
|
p.Gy[0] = (y + 1.5);
|
|
y += 1.0;
|
|
p.Py[1] = (-y * y + 0.75);
|
|
p.Gy[1] = (-2.0 * y);
|
|
y += 1.0;
|
|
p.Py[2] = (0.5 * y * y - 1.5 * y + 1.125);
|
|
p.Gy[2] = (y - 1.5);
|
|
|
|
for (uint i = 0; i < 3; ++i)
|
|
{
|
|
for (uint j = 0; j < 3; ++j)
|
|
{
|
|
n = _grid[p.Cx + i][p.Cy + j];
|
|
if (!n.Active)
|
|
{
|
|
_active[_activeCount++] = n;
|
|
n.Active = true;
|
|
}
|
|
|
|
phi = p.Px[i] * p.Py[j];
|
|
n.M += phi * p.Material.M;
|
|
n.D += phi;
|
|
n.Gx += p.Gx[i] * p.Py[j];
|
|
n.Gy += p.Px[i] * p.Gy[j];
|
|
|
|
// CUSTOM:
|
|
if (p.Mature && p.Strength > 0.0)
|
|
{
|
|
double new_w = p.Strength;
|
|
double old_w = n.Weight;
|
|
double sum_w = new_w + old_w;
|
|
n.R = (old_w * n.R + new_w * p.r) / sum_w;
|
|
n.G = (old_w * n.G + new_w * p.g) / sum_w;
|
|
n.B = (old_w * n.B + new_w * p.b) / sum_w;
|
|
n.A = (old_w * n.A + new_w * p.a) / sum_w;
|
|
n.Weight = sum_w;
|
|
}
|
|
// END OF CUSTOM
|
|
}
|
|
}
|
|
}
|
|
|
|
uint cx, cy;
|
|
uint cxi, cyi;
|
|
|
|
double pdx, pdy;
|
|
double C20, C02, C30, C03;
|
|
double C21, C31, C12, C13, C11;
|
|
|
|
double csum1, csum2;
|
|
|
|
double u, u2, u3;
|
|
double v, v2, v3;
|
|
|
|
double vx, vy;
|
|
|
|
double density;
|
|
double pressure;
|
|
double weight;
|
|
|
|
Node n01, n02, n11, n12;
|
|
|
|
for (uint i = 0; i < _particleCount; ++i)
|
|
{
|
|
p = _particles[i];
|
|
if (p.Active == false) continue; // CUSTOMIZATION
|
|
|
|
cx = (uint)p.X;
|
|
cy = (uint)p.Y;
|
|
cxi = cx + 1;
|
|
cyi = cy + 1;
|
|
|
|
n01 = _grid[cx][cy];
|
|
n02 = _grid[cx][cyi];
|
|
n11 = _grid[cxi][cy];
|
|
n12 = _grid[cxi][cyi];
|
|
|
|
pdx = n11.D - n01.D;
|
|
pdy = n02.D - n01.D;
|
|
C20 = 3.0 * pdx - n11.Gx - 2.0 * n01.Gx;
|
|
C02 = 3.0 * pdy - n02.Gy - 2.0 * n01.Gy;
|
|
C30 = -2.0 * pdx + n11.Gx + n01.Gx;
|
|
C03 = -2.0 * pdy + n02.Gy + n01.Gy;
|
|
|
|
csum1 = n01.D + n01.Gy + C02 + C03;
|
|
csum2 = n01.D + n01.Gx + C20 + C30;
|
|
|
|
C21 = 3.0 * n12.D - 2.0 * n02.Gx - n12.Gx - 3.0 * csum1 - C20;
|
|
C31 = -2.0 * n12.D + n02.Gx + n12.Gx + 2.0 * csum1 - C30;
|
|
C12 = 3.0 * n12.D - 2.0 * n11.Gy - n12.Gy - 3.0 * csum2 - C02;
|
|
C13 = -2.0 * n12.D + n11.Gy + n12.Gy + 2.0 * csum2 - C03;
|
|
C11 = n02.Gx - C13 - C12 - n01.Gx;
|
|
|
|
u = p.X - cx;
|
|
u2 = u * u;
|
|
u3 = u * u2;
|
|
v = p.Y - cy;
|
|
v2 = v * v;
|
|
v3 = v * v2;
|
|
|
|
density = n01.D + n01.Gx * u + n01.Gy * v + C20 * u2 + C02 * v2 + C30 * u3 + C03 * v3 + C21 * u2 * v +
|
|
C31 * u3 * v + C12 * u * v2 + C13 * u * v3 + C11 * u * v;
|
|
|
|
pressure = density - 1.0;
|
|
if (pressure > 2.0)
|
|
{
|
|
pressure = 2.0;
|
|
}
|
|
|
|
fx = fy = 0.0;
|
|
|
|
if (p.X < 4.0)
|
|
{
|
|
fx += p.Material.M * (4.0 - p.X);
|
|
}
|
|
else if (p.X > _gSizeX - 5)
|
|
{
|
|
fx += p.Material.M * (_gSizeX - 5 - p.X);
|
|
}
|
|
|
|
if (p.Y < 4.0)
|
|
{
|
|
fy += p.Material.M * (4.0 - p.Y);
|
|
}
|
|
else if (p.Y > _gSizeY - 5)
|
|
{
|
|
fy += p.Material.M * (_gSizeY - 5 - p.Y);
|
|
}
|
|
|
|
if (drag)
|
|
{
|
|
vx = Math.Abs(p.X - _mx);
|
|
vy = Math.Abs(p.Y - _my);
|
|
|
|
if (vx < 10.0 && vy < 10.0)
|
|
{
|
|
weight = p.Material.M * (1.0 - vx * 0.1) * (1.0 - vy * 0.1);
|
|
fx += weight * (mdx - p.U);
|
|
fy += weight * (mdy - p.V);
|
|
}
|
|
}
|
|
|
|
for (uint index = 0; index < 3; ++index)
|
|
{
|
|
for (uint j = 0; j < 3; ++j)
|
|
{
|
|
n = _grid[p.Cx + index][p.Cy + j];
|
|
phi = p.Px[index] * p.Py[j];
|
|
n.Ax += -((p.Gx[index] * p.Py[j]) * pressure) + fx * phi;
|
|
n.Ay += -((p.Px[index] * p.Gy[j]) * pressure) + fy * phi;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (uint i = 0; i < _activeCount; ++i)
|
|
{
|
|
n = _active[i];
|
|
if (n.M > 0.0)
|
|
{
|
|
n.Ax /= n.M;
|
|
n.Ay /= n.M;
|
|
|
|
//ORIGINALLY GRAVITY WAS INDICATED BY THESE:
|
|
//n->ay += 0.03;
|
|
//n->ax += 0.03;
|
|
}
|
|
}
|
|
|
|
double mu, mv;
|
|
|
|
for (uint pi = 0; pi < _particleCount; ++pi)
|
|
{
|
|
p = _particles[pi];
|
|
if (p.Active == false) continue; // CUSTOMIZATION
|
|
|
|
for (uint i = 0; i < 3; ++i)
|
|
{
|
|
for (uint j = 0; j < 3; ++j)
|
|
{
|
|
n = _grid[p.Cx + i][p.Cy + j];
|
|
phi = p.Px[i] * p.Py[j];
|
|
|
|
/* CUSTOM STUFF */
|
|
double custom_ax = 0.0;
|
|
double custom_ay = 0.0;
|
|
{
|
|
double x1 = p.X;
|
|
double y1 = p.Y;
|
|
double x2 = p.GravityX;
|
|
double y2 = p.GravityY;
|
|
double a = 0.03;
|
|
|
|
// Gravitate towards (x2, y2).
|
|
double A = Math.Abs(y1 - y2);
|
|
double B = Math.Abs(x1 - x2);
|
|
double C = Math.Sqrt(A * A + B * B);
|
|
|
|
if (a >= C) a = C;
|
|
|
|
if (B <= 0.0)
|
|
{
|
|
if (y2 <= y1) custom_ay -= a;
|
|
else custom_ay += a;
|
|
}
|
|
else if (C > 0.0)
|
|
{
|
|
double dx = (a * B) / C;
|
|
double dy = (A * dx) / B;
|
|
|
|
if (x1 <= x2) custom_ax += dx;
|
|
else custom_ax -= dx;
|
|
|
|
if (y1 <= y2) custom_ay += dy;
|
|
else custom_ay -= dy;
|
|
}
|
|
}
|
|
/* END OF CUSTOM STUFF */
|
|
|
|
p.U += phi * (n.Ax + custom_ax);
|
|
p.V += phi * (n.Ay + custom_ay);
|
|
}
|
|
}
|
|
mu = p.Material.M * p.U;
|
|
mv = p.Material.M * p.V;
|
|
if (p.Mature == false)
|
|
{
|
|
mu *= 0.0;
|
|
mv *= 0.0;
|
|
}
|
|
for (uint i = 0; i < 3; ++i)
|
|
{
|
|
for (uint j = 0; j < 3; ++j)
|
|
{
|
|
n = _grid[p.Cx + i][p.Cy + j];
|
|
phi = p.Px[i] * p.Py[j];
|
|
n.U += phi * mu;
|
|
n.V += phi * mv;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (uint i = 0; i < _activeCount; ++i)
|
|
{
|
|
n = _active[i];
|
|
if (n.M > 0.0)
|
|
{
|
|
n.U /= n.M;
|
|
n.V /= n.M;
|
|
}
|
|
}
|
|
|
|
double gu, gv;
|
|
|
|
for (uint i = 0; i < _particleCount; ++i)
|
|
{
|
|
p = _particles[i];
|
|
|
|
// CUSTOMIZATION:
|
|
if (p.Active == false) continue;
|
|
double nR = 0.0, nG = 0.0, nB = 0.0, nA = 0.0;
|
|
double weight2 = 0.0;
|
|
// END OF CUSTOMIZATION
|
|
|
|
gu = 0.0;
|
|
gv = 0.0;
|
|
for (uint ii = 0; ii < 3; ++ii)
|
|
{
|
|
for (uint j = 0; j < 3; ++j)
|
|
{
|
|
n = _grid[p.Cx + ii][p.Cy + j];
|
|
phi = p.Px[ii] * p.Py[j];
|
|
gu += phi * n.U;
|
|
gv += phi * n.V;
|
|
|
|
if (n.Weight > 0.0)
|
|
{
|
|
weight2 += n.Weight;
|
|
nR += n.R * n.Weight;
|
|
nG += n.G * n.Weight;
|
|
nB += n.B * n.Weight;
|
|
nA += n.A * n.Weight;
|
|
}
|
|
}
|
|
}
|
|
|
|
// CUSTOMIZATION:
|
|
if (weight2 > 0.0)
|
|
{
|
|
double wr = 1.0;
|
|
double wg = 1.0;
|
|
double wb = 1.0;
|
|
double wa = 1.0;
|
|
nR /= weight2;
|
|
wr = Math.Abs(nR - p.R);
|
|
nG /= weight2;
|
|
wg = Math.Abs(nG - p.G);
|
|
nB /= weight2;
|
|
wb = Math.Abs(nB - p.B);
|
|
nA /= weight2;
|
|
wa = Math.Abs(nA - p.A);
|
|
|
|
if (p.Mature == false)
|
|
{
|
|
p.r = nR;
|
|
p.g = nG;
|
|
p.b = nB;
|
|
p.a = nA;
|
|
}
|
|
else
|
|
{
|
|
p.r = (1.0 - wr) * p.r + wr * nR;
|
|
p.g = (1.0 - wg) * p.g + wg * nG;
|
|
p.b = (1.0 - wb) * p.b + wb * nB;
|
|
p.a = (1.0 - wa) * p.a + wa * nA;
|
|
}
|
|
}
|
|
// END OF CUSTOMIZATION
|
|
|
|
p.X += gu;
|
|
p.Y += gv;
|
|
|
|
/* CUSTOM STUFF */
|
|
{
|
|
double x1 = p.X;
|
|
double y1 = p.Y;
|
|
double x2 = p.GravityX;
|
|
double y2 = p.GravityY;
|
|
|
|
// Gravitate towards (x2, y2) if the distance
|
|
// is greater than freedom radius.
|
|
double A = Math.Abs(y1 - y2);
|
|
double B = Math.Abs(x1 - x2);
|
|
double C = Math.Sqrt(A * A + B * B);
|
|
|
|
double r = freedomRadius * p.FreedomR;
|
|
if (C > r)
|
|
{
|
|
double mx = 0.0;
|
|
double my = 0.0;
|
|
|
|
double a = C - r;
|
|
if (a >= C) a = C;
|
|
|
|
if (B <= 0.0)
|
|
{
|
|
if (y2 <= y1) my -= a;
|
|
else my += a;
|
|
}
|
|
else if (C > 0.0)
|
|
{
|
|
double dx = (a * B) / C;
|
|
double dy = (A * dx) / B;
|
|
|
|
if (x1 <= x2) mx += dx;
|
|
else mx -= dx;
|
|
|
|
if (y1 <= y2) my += dy;
|
|
else my -= dy;
|
|
}
|
|
|
|
double w = 1.0 / (stepsLeft + 1);
|
|
|
|
mx *= w;
|
|
my *= w;
|
|
|
|
p.X += mx;
|
|
p.Y += my;
|
|
}
|
|
}
|
|
/* END OF CUSTOM STUFF */
|
|
|
|
p.U += gu - p.U;
|
|
p.V += gv - p.B;
|
|
if (p.X < 1.0)
|
|
{
|
|
p.X = 1.0 + _random.NextDouble() * 0.01;
|
|
p.U = 0.0;
|
|
}
|
|
else if (p.X > _gSizeX - 2)
|
|
{
|
|
p.X = _gSizeX - 2 - _random.NextDouble() * 0.01;
|
|
p.U = 0.0;
|
|
}
|
|
if (p.Y < 1.0)
|
|
{
|
|
p.Y = 1.0 + _random.NextDouble() * 0.01;
|
|
p.V = 0.0;
|
|
}
|
|
else if (p.Y > _gSizeY - 2)
|
|
{
|
|
p.Y = _gSizeY - 2 - _random.NextDouble() * 0.01;
|
|
p.V = 0.0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose()
|
|
{
|
|
|
|
}
|
|
} |