void StableSolver2D::projection() { for(int i=1; i<=rowSize; i++) { for(int j=1; j<=colSize; j++) { div[getIndex(i, j)] = -0.5f*(vx[getIndex(i+1,j)]-vx[getIndex(i-1,j)]+vy[getIndex(i,j+1)]-vy[getIndex(i,j-1)]); p[getIndex(i,j)] = 0; } } setBoundary(div, 0); setBoundary(p, 0); lin_solve(p, div, 1.0, 4.0, 0); for(int i=1; i<=rowSize; i++) { for(int j=1; j<=colSize; j++) { vx[getIndex(i,j)] -= 0.5f*(p[getIndex(i+1,j)]-p[getIndex(i-1,j)]); vy[getIndex(i,j)] -= 0.5f*(p[getIndex(i,j+1)]-p[getIndex(i,j-1)]); } } setBoundary(vx, 1); setBoundary(vy, 2); }
void Vlasov::solve(Fields *fields, CComplex *_fs, CComplex *_fss, double dt, int rk_step, const double rk[3], bool useNonBlockingBoundary) { static CComplex *f_boundary = nullptr; useNonBlockingBoundary = false; // Need boundary_isclean to avoid deadlock at first iteration #pragma omp single nowait { if((f_boundary != nullptr) && useNonBlockingBoundary) setBoundary(f_boundary, Boundary::RECV); } Xi_max[:] = 0.; // Needed to calculate CFL time step // Calculate the collision operator // BUG : how to deal with velocity space decomposition ?! coll->solve(fields, _fs, f0, Coll, dt, rk_step); // Calculate the Vlasov equation #pragma omp barrier solve(equation_type, fields, _fs, _fss, dt, rk_step, rk); // Note : we have non-blocking boundaries as Poisson solver does not require ghosts // Set nowait, as field solver does not require boundaries & analysis too ... ?! #pragma omp single nowait { (useNonBlockingBoundary) ? setBoundary(_fss, Boundary::SEND) : setBoundary(_fss, Boundary::SENDRECV); } f_boundary = _fss; }
void StableSolver2D::addSource() { if(running == 0) return; for(int i=0; i<totSize; i++) { vx[i] += vx0[i]; vy[i] += vy0[i]; d[i] += d0[i]; } setBoundary(vx, 1); setBoundary(vy, 2); setBoundary(d, 0); }
void Lanes::init(const CGitHash& expectedSha) { clear(); activeLane = 0; setBoundary(false); add(BRANCH, expectedSha, activeLane); }
void ofxBoundaryBehavior::setup() { space = new float(); bAllocatedSpace = true; setSpace(0.0); setBoundary(0, ofGetWidth(), 0, ofGetHeight()); }
void labeling(void) { verticalProject(&cImg, vProjection); setBoundary(&cImg, vProjection, boundary); getYVals(&cImg, vProjection, boundary, heights); drawProject(&cImg, vProjection); drawBoundary(&cImg, boundary); }
void Lanes::init(const CGitHash& expectedSha) { clear(); activeLane = 0; setBoundary(false); bool wasEmptyCross = false; add(BRANCH, expectedSha, activeLane, wasEmptyCross); }
int subdivide(node* currentNode) { float cX, cY, half; // Create short access copy of bounds cX = currentNode->boundary->cX; cY = currentNode->boundary->cY; half = currentNode->boundary->half; // Create subdivision boundaries bounds* northWestBounds = setBoundary(cX-(half/2), cY+(half/2), half/2); bounds* northEastBounds = setBoundary(cX+(half/2), cY+(half/2), half/2); bounds* southEastBounds = setBoundary(cX+(half/2), cY-(half/2), half/2); bounds* southWestBounds = setBoundary(cX-(half/2), cY-(half/2), half/2); // Allocate memory for new nodes currentNode->northWest = newNode(northWestBounds); currentNode->northEast = newNode(northEastBounds); currentNode->southEast = newNode(southEastBounds); currentNode->southWest = newNode(southWestBounds); // Return 1 to signify successful subdivision return 1; }
void StableSolver2D::advection(float *value, float *value0, float *u, float *v, int flag) { int idxNow; float oldX; float oldY; int i0; int j0; int i1; int j1; float iL; float iR; float iT; float iB; for(int i=1; i<=rowSize; i++) { for(int j=1; j<=colSize; j++) { idxNow = getIndex(i, j); //implicit method, trace the position back to old position oldX = px[idxNow] - u[idxNow] * time_step; oldY = py[idxNow] - v[idxNow] * time_step; if(oldX < minX) oldX = minX; if(oldX > maxX) oldX = maxX; if(oldY < minY) oldY = minY; if(oldY > maxY) oldY = maxY; i0 = int(oldX - 0.5f); j0 = int(oldY - 0.5f); i1 = i0 + 1; j1 = j0 + 1; iR = oldX - px[getIndex(i0, j0)]; iT = oldY - py[getIndex(i0, j0)]; iL = 1.0f - iR; iB = 1.0f - iT; value[idxNow] = iB * (iL*value0[getIndex(i0, j0)] + iR*value0[getIndex(i1, j0)]) + iT * (iL*value0[getIndex(i0, j1)] + iR*value0[getIndex(i1, j1)]); } } setBoundary(value, flag); }
void StableSolver2D::lin_solve(float *value, float * value0, float a, float c, int flag) { for(int iteration=0; iteration<20; iteration++) { for(int i=1; i<=rowSize; i++) { for(int j=1; j<=colSize; j++) { value[getIndex(i, j)] = (value0[getIndex(i, j)] + a*(value[getIndex(i-1, j)]+value[getIndex(i+1, j)]+value[getIndex(i, j-1)]+value[getIndex(i, j+1)]))/c; } } setBoundary(value, flag); } }
void SWSolver::advanceTimestep(){ //std::cout << "advecting eta..." << std::endl; advect(ETA); //std::cout << "advecting velocity_x..." << std::endl; advect(VELOCITY_X); //std::cout << "advecting velocity_y..." << std::endl; advect(VELOCITY_Y); //std::cout << "updating heights..." << std::endl; updateHeight(); //std::cout << "updating velocities..." << std::endl; updateVelocity(); //std::cout << "setting boundaries..." << std::endl; setBoundary(); }
void ciMsaFluidSolver::advect( int bound, float* d, const float* d0, const ci::Vec2f* duv) { int i0, j0, i1, j1; float x, y, s0, t0, s1, t1; int index; const float dt0x = _dt * _NX; const float dt0y = _dt * _NY; for (int j = _NY; j > 0; --j) { for (int i = _NX; i > 0; --i) { index = FLUID_IX(i, j); x = i - dt0x * duv[index].x; y = j - dt0y * duv[index].y; if (x > _NX + 0.5) x = _NX + 0.5f; if (x < 0.5) x = 0.5f; i0 = (int) x; i1 = i0 + 1; if (y > _NY + 0.5) y = _NY + 0.5f; if (y < 0.5) y = 0.5f; j0 = (int) y; j1 = j0 + 1; s1 = x - i0; s0 = 1 - s1; t1 = y - j0; t0 = 1 - t1; d[index] = s0 * (t0 * d0[FLUID_IX(i0, j0)] + t1 * d0[FLUID_IX(i0, j1)]) + s1 * (t0 * d0[FLUID_IX(i1, j0)] + t1 * d0[FLUID_IX(i1, j1)]); } } setBoundary(bound, d); }
void Vlasov::setBoundary(CComplex *f) { setBoundary(f, Boundary::SENDRECV); }
// run the solver... int main(int argc, char *argv[]) { printf("Init\n"); // allocate memory vel_x.resize(SIZE*SIZE); vel_y.resize(SIZE*SIZE); temp.resize(SIZE*SIZE); eta.resize(SIZE*SIZE); // initialize grid for (int i=0;i<SIZE;i++) for (int j=0;j<SIZE;j++) { const int index = i+j*SIZE; vel_x[index] = vel_y[index] = temp[index] = 0.; // default water height 1 eta[index] = 1.; } // init an off-center initial wave for (int i=3*SIZE/6; i<4*SIZE/6; i++) for (int j=3*SIZE/6; j<4*SIZE/6; j++) { const int index = i + j*SIZE; //eta[index] = 1.1; } printf("Starting simulation...\n"); Real simulationTime = 0.; int simulationStep = 0; while (simulationTime < END_TIME) { printf("Step %d ",simulationStep); advectArray( eta ,0 ); advectArray( vel_x ,1 ); advectArray( vel_y ,2 ); updateHeight(); updateVelocities(); setBoundary(); addRandomDrop(); simulationTime += dt; simulationStep++; // should we write an image for this step? if ( (simulationStep % IMAGESTEPS)== 0) { writeImage( simulationStep ); } printf("\n"); } printf("SWS-simulation done!\n"); return 0; }
void StableFluid3D::projectVelocity(double dt) { setBoundary(); Vector divergence(vel.Length()); // fill in velocities double dx = width / (double)resX; double dy = height / (double)resY; double dz = depth / (double)resZ; for (int i = 0; i < resX; ++i) { for (int j = 0; j < resY; ++j) { for (int k = 0; k < resZ; ++k) { int index = makeIndex(i, j, k); double div = (tempVelocities[0][i + 1][j][k] - tempVelocities[0][i][j][k]) / dx + (tempVelocities[1][i][j + 1][k] - tempVelocities[1][i][j][k]) / dy + (tempVelocities[2][i][j][k + 1] - tempVelocities[2][i][j][k]) / dz; vel[index] = div; divergence[index] = div; } } } // vel *= density / dt; bool useOldPCG = false; if (useOldPCG) { int k = 0; Vector laplacianTimesPressure(pressure.Length()); laplacian.Multiply(pressure, laplacianTimesPressure); r[0] = vel - laplacianTimesPressure; double error = r[0].Magnitude2(); while ((std::sqrt(error) > PCG_EPS) && (k < PCG_MAXITER)) { //cerr << "PCG iteration " << k << ", error: " << std::sqrt(error) << endl; ConjugateGradient(preconditioner, z[k % 2], r[k % 2], 0.0001, 500.0); ++k; int i1 = (k - 1) % 2; int i2 = k % 2; if (k == 1) { p = z[0]; } else { double beta = (InnerProduct(r[i1], z[i1])) / (InnerProduct(r[i2], z[i2])); p *= beta; p += z[i1]; } Vector laplacianTimesP(p.Length()); laplacian.Multiply(p, laplacianTimesP); double alpha = (InnerProduct(r[i1], z[i1])) / (InnerProduct(p, laplacianTimesP)); pressure += alpha * p; r[i2] = r[i1] - alpha * (laplacianTimesP); error = r[i2].Magnitude2(); } if (std::sqrt(error) > PCG_EPS) { std::cout << "end preconditioned conj grad " << "error: " << error << std::endl; } } else { p = Vector(vel.Length()); r[0] = divergence; //ApplyPreconditioner(r[0], z[0]); double sigma = InnerProduct(r[0], z[0]); int k = 0; double error = r[0].Magnitude2(); while (k < PCG_MAXITER) { ++k; Vector s(vel.Length()); s = z[0]; int k = 0; laplacian.Multiply(s, z[0]); double rho = 1.0; double alpha = rho / InnerProduct(s, z[0]); p += alpha * s; r[0] -= alpha * z[0]; error = r[0].Magnitude2(); if (error < PCG_EPS * PCG_EPS) { pressure = p; break; } //ApplyPreconditioner(r[0], z[0]); double sigmaNew = InnerProduct(r[0], z[0]); double beta = sigmaNew / rho; s = z[0] + beta * s; sigma = sigmaNew; } Vector laplacianTimesPressure(pressure.Length()); laplacian.Multiply(pressure, laplacianTimesPressure); r[0] = vel - laplacianTimesPressure; error = r[0].Magnitude2(); while ((std::sqrt(error) > PCG_EPS) && (k < PCG_MAXITER)) { //cerr << "PCG iteration " << k << ", error: " << std::sqrt(error) << endl; ConjugateGradient(preconditioner, z[k % 2], r[k % 2], 0.0001, 500.0); ++k; int i1 = (k - 1) % 2; int i2 = k % 2; if (k == 1) { p = z[0]; } else { double beta = (InnerProduct(r[i1], z[i1])) / (InnerProduct(r[i2], z[i2])); p *= beta; p += z[i1]; } Vector laplacianTimesP(p.Length()); laplacian.Multiply(p, laplacianTimesP); double alpha = (InnerProduct(r[i1], z[i1])) / (InnerProduct(p, laplacianTimesP)); pressure += alpha * p; r[i2] = r[i1] - alpha * (laplacianTimesP); error = r[i2].Magnitude2(); } if (std::sqrt(error) > PCG_EPS) { std::cout << "end preconditioned conj grad " << "error: " << error << std::endl; } } /* mathlib::Vector PCGr, PCGd, PCGq, PCGs; PCGr.Resize(resX * resY * resZ); PCGd.Resize(resX * resY * resZ); PCGq.Resize(resX * resY * resZ); PCGs.Resize(resX * resY * resZ); int iter = 0; PCGr = vel - laplacian * pressure; mathlib::ConjugateGradient(preconditioner, PCGd, PCGr); double deltaNew = mathlib::InnerProduct(PCGr, PCGd); double errBound = PCG_EPS * PCG_EPS * deltaNew; while ((iter < PCG_MAXITER) && (deltaNew > errBound)) { if (!(iter % 3)) cerr << "iteration " << iter << ", error: " << deltaNew << endl; PCGq = laplacian * PCGd; double alpha = deltaNew / (mathlib::InnerProduct(PCGd, PCGq)); pressure += alpha * PCGd; if ((iter % 10) == 0) PCGr = vel - laplacian * pressure; else PCGr -= alpha * PCGq; mathlib::ConjugateGradient(preconditioner, PCGs, PCGr); double deltaOld = deltaNew; deltaNew = mathlib::InnerProduct(PCGr, PCGs); double beta = deltaNew / deltaOld; PCGd *= beta; PCGd += PCGs; ++iter; } cout << "end preconditioned conj grad "<< "error: " << deltaNew << endl; */ std::cout << "dt:" << dt << "\t density:" << density << std::endl; // dt = 1.0; for (int i = 0; i < resX; ++i) { for (int j = 0; j < resY; ++j) { for (int k = 0; k < resZ; ++k) { int i1 = makeIndex(i, j, k); int i2; double deltaPressure; i2 = (i == 0 ? i1 : i1 - 1); velocities[0][i][j][k] = tempVelocities[0][i][j][k] - dt* 0.5 * (pressure[i1] - pressure[i2]) / dx; deltaPressure = pressure[i1] - pressure[i2]; i2 = (j == 0 ? i1 : i1 - resX); velocities[1][i][j][k] = tempVelocities[1][i][j][k] - dt * 0.5 * (pressure[i1] - pressure[i2]) / dy; deltaPressure = pressure[i1] - pressure[i2]; i2 = (k == 0 ? i1 : i1 - resY * resX); velocities[2][i][j][k] = tempVelocities[2][i][j][k] - dt * 0.5 * (pressure[i1] - pressure[i2]) / dz; deltaPressure = pressure[i1] - pressure[i2]; } } } }
// Print HSV Color Matrix void findObjectColor(int interval) { int first_Flag = 1; ColorBoundary color_B; int color_Type = 0; while(1){ HSV hsv; int i, j; int count=0; int index=0; // vidbuf = camera_get_frame(fdCamera); // usleep(3000000); // delay 3sec // camera_release_frame(fdCamera, vidbuf); // vidbuf = camera_get_frame(fdCamera); if(first_Flag == 1) { first_Flag = 0; printf("Input first boundary Type\n"); printf("1 : blue, 2 : green, 3:red, 4:yellow\n"); scanf("%d", &color_Type); if(color_Type == 1) { color_B.hmin = 190; color_B.hmax = 250; color_B.smin = 40; color_B.smax = 100; color_B.vmin = 28; color_B.vmax = 100; } else if(color_Type == 2) { color_B.hmin = 120; color_B.hmax = 168; color_B.smin = 47; color_B.smax = 100; color_B.vmin = 18; color_B.vmax = 100; } else if(color_Type == 3) { color_B.hmin = 345; color_B.hmax = 15; color_B.smin = 45; color_B.smax = 100; color_B.vmin = 40; color_B.vmax = 100; } else if(color_Type == 4) { color_B.hmin = 43; color_B.hmax = 76; color_B.smin = 40; color_B.smax = 100; color_B.vmin = 40; color_B.vmax = 100; } } else { char change_Flag; printf("value change? y or n\n"); scanf("%s", &change_Flag); if(change_Flag == 'y') { int whichOne; printf("h : 1, s ; 2, v : 3\ninput : ");\ scanf("%d", &whichOne); if(whichOne == 1) { printf("H min : "); scanf("%d", &(color_B.hmin)); printf("H max : "); scanf("%d", &(color_B.hmax)); } else if(whichOne == 2) { printf("S min : "); scanf("%d", &(color_B.smin)); printf("S max : "); scanf("%d", &(color_B.smax)); } else if(whichOne == 3) { printf("V min : "); scanf("%d", &(color_B.vmin)); printf("V max : "); scanf("%d", &(color_B.vmax)); } } } printf("hmin : %d, hmax : %d\n", color_B.hmin, color_B.hmax); printf("smin : %d, smax : %d\n", color_B.smin, color_B.smax); printf("vmin : %d, vmax : %d\n", color_B.vmin, color_B.vmax); VideoCopy buf2 = bufCopy; //copy frame to var memcpy(vidbuf_overlay.ycbcr.y, buf2.ycbcr.y,len_vidbuf); memcpy(vidbuf_overlay.ycbcr.cb, buf2.ycbcr.cb,len_vidbuf/2); memcpy(vidbuf_overlay.ycbcr.cr, buf2.ycbcr.cr,len_vidbuf/2); for (i = MAX_Y - (interval / 2); i >= 0; i -= interval) { for (j = MAX_X - (interval / 2); j >= 0; j -= interval) { hsv = getHSV(&buf2, j, i); index = 320*i + j; if(isColor(hsv, color_B)) { count++; if(count==1) printf("in func\n"); obj = 1; setBoundary(hsv, j, i); vidbuf_overlay.ycbcr.y[index] = 100; vidbuf_overlay.ycbcr.cb[index/2] = 200; vidbuf_overlay.ycbcr.cr[index/2] = 200; } } } if(obj == 1){ printf("Found Milk\n"); printf("(minh, maxh, mins, maxs, minv, maxv : (%f, %f, %f, %f, %f, %f)\n", minh, maxh, mins, maxs, minv, maxv); } char tmptmp[100]; printf("insert something : "); scanf("%s", tmptmp); } }