/* Builds the pressure right hand side as the negative divergence */ void buildRhs() { double scale = 1.0/_hx; for (int y = 0, idx = 0; y < _h; y++) { for (int x = 0; x < _w; x++, idx++) { _r[idx] = -scale*(_u->at(x + 1, y) - _u->at(x, y) + _v->at(x, y + 1) - _v->at(x, y)); } } }
/* Applies the computed pressure to the velocity field */ void applyPressure(double timestep) { double scale = timestep/(_density*_hx); for (int y = 0, idx = 0; y < _h; y++) { for (int x = 0; x < _w; x++, idx++) { _u->at(x, y ) -= scale*_p[idx]; _u->at(x + 1, y ) += scale*_p[idx]; _v->at(x, y ) -= scale*_p[idx]; _v->at(x, y + 1) += scale*_p[idx]; } } for (int y = 0; y < _h; y++) _u->at(0, y) = _u->at(_w, y) = 0.0; for (int x = 0; x < _w; x++) _v->at(x, 0) = _v->at(x, _h) = 0.0; }