void ParticleManager::computeDensitypressureOptimised()
{
for (int i = 0; i < _indexes.size(); ++i)
{
for (int y = -1; y <= 1; ++y)
{
for (int x = -1; x <= 1; ++x)
{
if (_indexes[i] + x + y * COLUMN < 0 || _indexes[i] + x + y * COLUMN > GRIDMAX) { continue; }
for (int pi = 0; pi < _grid[_indexes[i] + x + y * COLUMN].size(); ++pi)
{
_grid[_indexes[i] + x + y * COLUMN][pi]->rho = 0.f;
for (int pj = 0; pj < _grid[_indexes[i] + x + y * COLUMN].size(); ++pj)
{
auto ppi = _grid[_indexes[i] + x + y * COLUMN][pi];
auto ppj = _grid[_indexes[i] + x + y * COLUMN][pj];
double distanceSquared = (ppj->x - ppi->x) * (ppj->x - ppi->x) + (ppj->y - ppi->y) * (ppj->y - ppi->y);
if (distanceSquared < HSQ)
{
ppi->rho += MASS * POLY6 * (HSQ - distanceSquared) * (HSQ - distanceSquared) * (HSQ - distanceSquared);
}
}
_grid[_indexes[i] + x + y * COLUMN][pi]->p = GAS_CONST * (_grid[_indexes[i] + x + y * COLUMN][pi]->rho - REST_DENS);
}
}
}
}
}
void ParticleManager::computeForcesOptimised()
{
for (int i = 0; i < _indexes.size(); ++i)
{
for (int y = -1; y <= 1; ++y)
{
for (int x = -1; x <= 1; ++x)
{
if (_indexes[i] + x + y * COLUMN < 0 || _indexes[i] + x + y * COLUMN > GRIDMAX) { continue; }
{
for (int pi = 0; pi < _grid[_indexes[i] + x + y * COLUMN].size(); ++pi)
{
double pressure_x = 0.f;
double pressure_y = 0.f;
double viscosity_x = 0.f;
double viscosity_y = 0.f;
for (int pj = 0; pj < _grid[_indexes[i] + x + y * COLUMN].size(); ++pj)
{
if (&_grid[_indexes[i] + x + y * COLUMN][pi] == &_grid[_indexes[i] + x + y * COLUMN][pj])
continue;
auto ppi = _grid[_indexes[i] + x + y * COLUMN][pi];
auto ppj = _grid[_indexes[i] + x + y * COLUMN][pj];
double rsqr = ((ppj->x - ppi->x) * (ppj->x - ppi->x) + (ppj->y - ppi->y) * (ppj->y - ppi->y));
if (rsqr < HSQ)
{
double r = sqrt(rsqr);
double fpress = MASSxSPIKY * (ppi->p + ppj->p) / (2.0 * ppj->rho) * (HSQ - 2 * H * r + rsqr);
pressure_x += (ppi->x - ppj->x) / r * fpress;
pressure_y += (ppi->y - ppj->y) / r * fpress;
viscosity_x += VISCxMASSxVISC_LAP * (ppj->vx - ppi->vx) / ppj->rho * (H - r);
viscosity_y += VISCxMASSxVISC_LAP * (ppj->vy - ppi->vy) / ppj->rho * (H - r);
}
}
_grid[_indexes[i] + x + y * COLUMN][pi]->fx = pressure_x + viscosity_x + _ax * _grid[_indexes[i] + x + y * COLUMN][pi]->rho;
_grid[_indexes[i] + x + y * COLUMN][pi]->fy = pressure_y + viscosity_y + _ay * _grid[_indexes[i] + x + y * COLUMN][pi]->rho;
}
}
}
}
}
}