update method

void update()

Implementation

void update() {
  final N = particles.length;
  final dist = _sphSystemUpdateDist;
  final cs = speedOfSound;
  final eps = this.eps;

  for (int i = 0; i != N; i++) {
    final p = particles[i]; // Current particle
    final neighbors = this.neighbors[i];

    // Get neighbors
    neighbors.clear();
    getNeighbors(p, neighbors);
    neighbors.add(particles[i]); // Add current too
    final numNeighbors = neighbors.length;

    // Accumulate density for the particle
    double sum = 0.0;
    for (int j = 0; j != numNeighbors; j++) {
      //printf("Current particle has position %f %f %f\n",objects[id].pos.x(),objects[id].pos.y(),objects[id].pos.z());
      p.position.vsub(neighbors[j].position, dist);
      final len = dist.length();

      final weight = w(len);
      sum += neighbors[j].mass * weight;
    }

    // Save
    densities[i] = sum;
    pressures[i] = cs * cs * (densities[i]! - density);
  }

  // Add forces

  // Sum to these accelerations
  final aPressure = _sphSystemUpdateAPressure;
  final aVisc = _sphSystemUpdateAVisc;
  final gradW = _sphSystemUpdateGradW;
  final rVec = _sphSystemUpdateRVec;
  final u = _sphSystemUpdateU;

  for (int i = 0; i != N; i++) {
    final particle = particles[i];

    aPressure.set(0, 0, 0);
    aVisc.set(0, 0, 0);

    // Init vars
    double pij;
    double nabla;

    // Sum up for all other neighbors
    final neighbors = this.neighbors[i];
    final numNeighbors = neighbors.length;

    //printf("Neighbors: ");
    for (int j = 0; j != numNeighbors; j++) {
      final neighbor = neighbors[j];
      //printf("%d ",nj);

      // Get r once for all..
      particle.position.vsub(neighbor.position, rVec);
      final r = rVec.length();

      // Pressure contribution
      pij =
        -neighbor.mass *
        (pressures[i]! / (densities[i]! * densities[i]! + eps) +
          pressures[j]! / (densities[j]! * densities[j]! + eps));
      gradw(rVec, gradW);
      // Add to pressure acceleration
      gradW.scale(pij, gradW);
      aPressure.vadd(gradW, aPressure);

      // Viscosity contribution
      neighbor.velocity.vsub(particle.velocity, u);
      u.scale((1.0 / (0.0001 + densities[i]! * densities[j]!)) * viscosity * neighbor.mass, u);
      nabla = nablaw(r);
      u.scale(nabla, u);
      // Add to viscosity acceleration
      aVisc.vadd(u, aVisc);
    }

    // Calculate force
    aVisc.scale(particle.mass, aVisc);
    aPressure.scale(particle.mass, aPressure);

    // Add force to particles
    particle.force.vadd(aVisc, particle.force);
    particle.force.vadd(aPressure, particle.force);
  }
}