void MacCormackClamp(FlagGrid& flags, MACGrid& vel, Grid<T>& dst, Grid<T>& orig, Grid<T>& fwd, Real dt) { if (flags.isObstacle(i,j,k)) return; if ( isNotFluid(flags,i,j,k) ) { dst(i,j,k) = fwd(i,j,k); return; } T dval = dst(i,j,k); Vec3i upperClamp = flags.getSize() - 1; // lookup forward/backward Vec3i posFwd = toVec3i( Vec3(i,j,k) - vel.getCentered(i,j,k) * dt ); Vec3i posBwd = toVec3i( Vec3(i,j,k) + vel.getCentered(i,j,k) * dt ); dval = doClampComponent<T>(upperClamp, orig, dval, posFwd ); // test if lookups point out of grid or into obstacle if (posFwd.x < 0 || posFwd.y < 0 || posFwd.z < 0 || posBwd.x < 0 || posBwd.y < 0 || posBwd.z < 0 || posFwd.x > upperClamp.x || posFwd.y > upperClamp.y || ((posFwd.z > upperClamp.z)&&flags.is3D()) || posBwd.x > upperClamp.x || posBwd.y > upperClamp.y || ((posBwd.z > upperClamp.z)&&flags.is3D()) || flags.isObstacle(posFwd) || flags.isObstacle(posBwd) ) { dval = fwd(i,j,k); } dst(i,j,k) = dval; }
void MacCormackClampMAC (FlagGrid& flags, MACGrid& vel, MACGrid& dst, MACGrid& orig, MACGrid& fwd, Real dt) { if (flags.isObstacle(i,j,k)) return; if ( isNotFluidMAC(flags,i,j,k) ) { dst(i,j,k) = fwd(i,j,k); return; } Vec3 pos(i,j,k); Vec3 dval = dst(i,j,k); Vec3i upperClamp = flags.getSize() - 1; // get total fwd lookup Vec3i posFwd = toVec3i( Vec3(i,j,k) - vel.getCentered(i,j,k) * dt ); Vec3i posBwd = toVec3i( Vec3(i,j,k) + vel.getCentered(i,j,k) * dt ); // clamp individual components dval.x = doClampComponentMAC<0>(upperClamp, orig, dval.x, toVec3i( pos - vel.getAtMACX(i,j,k) * dt) ); dval.y = doClampComponentMAC<1>(upperClamp, orig, dval.y, toVec3i( pos - vel.getAtMACY(i,j,k) * dt) ); dval.z = doClampComponentMAC<2>(upperClamp, orig, dval.z, toVec3i( pos - vel.getAtMACZ(i,j,k) * dt) ); // test if lookups point out of grid or into obstacle if (posFwd.x < 0 || posFwd.y < 0 || posFwd.z < 0 || posBwd.x < 0 || posBwd.y < 0 || posBwd.z < 0 || posFwd.x > upperClamp.x || posFwd.y > upperClamp.y || ((posFwd.z > upperClamp.z)&&flags.is3D()) || posBwd.x > upperClamp.x || posBwd.y > upperClamp.y || ((posBwd.z > upperClamp.z)&&flags.is3D()) //|| flags.isObstacle(posFwd) || flags.isObstacle(posBwd) // note - this unfortunately introduces asymmetry... TODO update ) { dval = fwd(i,j,k); } // writeback dst(i,j,k) = dval; }
//! Perform pressure projection of the velocity grid PYTHON void solvePressure(MACGrid& vel, Grid<Real>& pressure, FlagGrid& flags, Grid<Real>* phi = 0, Grid<Real>* perCellCorr = 0, Real ghostAccuracy = 0, Real cgMaxIterFac = 1.5, Real cgAccuracy = 1e-3, string openBound = "", string outflow = "", int outflowHeight = 1, int precondition = 0, bool enforceCompatibility = false, bool useResNorm = true ) { //assertMsg(vel.is3D(), "Only 3D grids supported so far"); // parse strings Vector3D<bool> loOpenBound, upOpenBound, loOutflow, upOutflow; convertDescToVec(openBound, loOpenBound, upOpenBound); convertDescToVec(outflow, loOutflow, upOutflow); if (vel.is2D() && (loOpenBound.z || upOpenBound.z)) errMsg("open boundaries for z specified for 2D grid"); // reserve temp grids Grid<Real> rhs(parent); Grid<Real> residual(parent); Grid<Real> search(parent); Grid<Real> A0(parent); Grid<Real> Ai(parent); Grid<Real> Aj(parent); Grid<Real> Ak(parent); Grid<Real> tmp(parent); Grid<Real> pca0(parent); Grid<Real> pca1(parent); Grid<Real> pca2(parent); Grid<Real> pca3(parent); // setup matrix and boundaries MakeLaplaceMatrix (flags, A0, Ai, Aj, Ak); SetOpenBound (A0, Ai, Aj, Ak, vel, loOpenBound, upOpenBound); if (ghostAccuracy > 0) { if (!phi) errMsg("solve_pressure: if ghostAccuracy>0, need to specify levelset phi=xxx"); ApplyGhostFluid (flags, A0, *phi, ghostAccuracy); } // compute divergence and init right hand side MakeRhs kernMakeRhs (flags, rhs, vel, perCellCorr); if (!outflow.empty()) SetOutflow (rhs, loOutflow, upOutflow, outflowHeight); if (enforceCompatibility) rhs += (Real)(-kernMakeRhs.sum / (Real)kernMakeRhs.cnt); // CG const int maxIter = (int)(cgMaxIterFac * flags.getSize().max()); GridCgInterface *gcg; if (vel.is3D()) gcg = new GridCg<ApplyMatrix>(pressure, rhs, residual, search, flags, tmp, &A0, &Ai, &Aj, &Ak ); else gcg = new GridCg<ApplyMatrix2D>(pressure, rhs, residual, search, flags, tmp, &A0, &Ai, &Aj, &Ak ); gcg->setAccuracy( cgAccuracy ); gcg->setUseResNorm( useResNorm ); // optional preconditioning gcg->setPreconditioner( (GridCgInterface::PreconditionType)precondition, &pca0, &pca1, &pca2, &pca3); for (int iter=0; iter<maxIter; iter++) { if (!gcg->iterate()) iter=maxIter; } debMsg("FluidSolver::solvePressure iterations:"<<gcg->getIterations()<<", res:"<<gcg->getSigma(), 1); delete gcg; if(ghostAccuracy<=0.) { // ghost fluid off, normal correction CorrectVelocity (flags, vel, pressure ); } else { CorrectVelGhostFluid (flags, vel, pressure); } }