static public float Dot(UnityFluid.CellCenteredScalarGrid2D lhs, UnityFluid.CellCenteredScalarGrid2D rhs) { float r = 0; lhs.ForEachData((ref float value, int[] index) => { r += value * rhs[index[0], index[1]]; }); return(r); }
static public float Infnorm(UnityFluid.CellCenteredScalarGrid2D field) { float r = 0; field.ForEachData((ref float value, int[] list) => { if (!(Mathf.Abs(value) <= r)) { r = Mathf.Abs(value); } }); return(r); }
protected void SolveDistance(float p, float q, ref float r) { float d = Mathf.Min(p, q) + 1; if (d > Mathf.Max(p, q)) { d = (p + q + Mathf.Sqrt(2 - sqr(p - q))) / 2; } if (d < r) { r = d; } }
void ApplyPoisson(CellCenteredScalarGrid2D x, CellCenteredScalarGrid2D y) { y.Reset(0); var poissonnx = this.poisson.DataSize.x; var poissonny = this.poisson.DataSize.y; for (int j = 1; j < poissonny - 1; ++j) { for (int i = 1; i < poissonnx - 1; ++i) { if (this.marker.GetDataFromIndex(i, j) == FLUID) { var value = poisson.GetDataFromIndex(i, j).x *x.GetDataFromIndex(i, j) + poisson.GetDataFromIndex(i - 1, j).y *x.GetDataFromIndex(i - 1, j) + poisson.GetDataFromIndex(i, j).y *x.GetDataFromIndex(i + 1, j) + poisson.GetDataFromIndex(i, j - 1).z *x.GetDataFromIndex(i, j - 1) + poisson.GetDataFromIndex(i, j).z *x.GetDataFromIndex(i, j + 1); y.SetDataToIndex(value, i, j); } } } }
protected void InitData() { Grid2DConfigure config = new Grid2DConfigure(); config.CellSize = new Vector2(cellSpace, cellSpace); config.Resolution = this.gridSize; this.velocity = factory.MakeGrid2D(GridFactory.CenterType.FaceCentered, GridFactory.DataType.Vector, config) as FaceCenterdVectorGrid2D; this.savedVelocity = factory.MakeGrid2D(GridFactory.CenterType.FaceCentered, GridFactory.DataType.Vector, config) as FaceCenterdVectorGrid2D; //this.weightSum = factory.MakeGrid2D(GridFactory.CenterType.FaceCentered, GridFactory.DataType.Vector, config) as FaceCenteredGrid2D; this.pressure = factory.MakeGrid2D(GridFactory.CenterType.CellCentered, GridFactory.DataType.Scalar, config) as CellCenteredScalarGrid2D; this.marker = factory.MakeGrid2Di(GridFactory.CenterType.CellCentered, GridFactory.DataType.Scalar, config) as CellCenteredScalarGrid2Di; this.phi = factory.MakeGrid2D(GridFactory.CenterType.CellCentered, GridFactory.DataType.Scalar, config) as CellCenteredScalarGrid2D; this.poisson = factory.MakeGrid2D(GridFactory.CenterType.CellCentered, GridFactory.DataType.Vector, config) as CellCenteredVectorGrid2D; this.preconditioner = factory.MakeGrid2D(GridFactory.CenterType.CellCentered, GridFactory.DataType.Scalar, config) as CellCenteredScalarGrid2D; this.mField = factory.MakeGrid2D(GridFactory.CenterType.CellCentered, GridFactory.DataType.Scalar, config) as CellCenteredScalarGrid2D; this.r = factory.MakeGrid2D(GridFactory.CenterType.CellCentered, GridFactory.DataType.Scalar, config) as CellCenteredScalarGrid2D; this.z = factory.MakeGrid2D(GridFactory.CenterType.CellCentered, GridFactory.DataType.Scalar, config) as CellCenteredScalarGrid2D; this.s = factory.MakeGrid2D(GridFactory.CenterType.CellCentered, GridFactory.DataType.Scalar, config) as CellCenteredScalarGrid2D; }
void ApplyPreconditioner(CellCenteredScalarGrid2D x, CellCenteredScalarGrid2D y, CellCenteredScalarGrid2D m) { //Maybe Incomplete Cholesky?? int i, j; float d; var xnx = x.DataSize.x; var xny = x.DataSize.y; m.Reset(); // solve L*m=x for (j = 1; j < xny - 1; ++j) { for (i = 1; i < xnx - 1; ++i) { if (marker[i, j] == FLUID) { d = x[i, j] - poisson[i - 1, j].y * preconditioner[i - 1, j] * m[i - 1, j] - poisson[i, j - 1].z * preconditioner[i, j - 1] * m[i, j - 1]; m[i, j] = preconditioner[i, j] * d; } } } // solve L'*y=m y.Reset(); for (j = xny - 2; j > 0; --j) { for (i = xnx - 2; i > 0; --i) { if (marker[i, j] == FLUID) { d = m[i, j] - poisson[i, j].y * preconditioner[i, j] * y[i + 1, j] - poisson[i, j].z * preconditioner[i, j] * y[i, j + 1]; y[i, j] = preconditioner[i, j] * d; } } } }
static public void ScaleAndIncrement(UnityFluid.CellCenteredScalarGrid2D lhs, UnityFluid.CellCenteredScalarGrid2D rhs, float scale) { lhs.ForEachData((ref float value, int[] index) => { value = value * scale + rhs[index[0], index[1]]; }); }