diff --git a/work/week8/particles/starting_point/compute_gravity.cc b/work/week8/particles/starting_point/compute_gravity.cc index feaaa00..88fd034 100644 --- a/work/week8/particles/starting_point/compute_gravity.cc +++ b/work/week8/particles/starting_point/compute_gravity.cc @@ -1,5 +1,59 @@ #include "compute_gravity.hh" #include /* -------------------------------------------------------------------------- */ void ComputeGravity::compute(System& system) { + + +//C=Gets the total number of particles +int nparticles = system.getNbParticles(); + +//Loops over all the particles and gets each one's parameters + +Vector v_ij; +Real r2_ij= 0.; +Real r_ij=0; +Vector force; + + +for (int pi = 0; pi < nparticles; ++pi) { + Particle & PI = system.getParticle(pi); + for (int pj = pi+1; pj < nparticles; ++pj) { + Particle & PJ = system.getParticle(pj); + + + //Gets the vector between the two particles positions + v_ij = PJ.getPosition() - PI.getPosition(); + + + //Squared distance (obtained via vector class) + r2_ij = v_ij.squaredNorm(); + + //Obtained distance + r_ij = sqrt(r2_ij); + + //compute the pair force + force = v_ij/r_ij*PI.getMass()*PJ.getMass()*G/r2_ij; + + // add up the force for both concerned particles + PJ.getForce() -= force; + PI.getForce() += force; + } + } + + + + + + +//std::unique_ptr p; + + //Particle::getMass() { return mass; } + //! Get position + //Vector& getPosition() { return position; } + //! Get force + // Vector& getForce() { return force; } + //! Get velocity +// Vector& getVelocity() { return velocity; } + + }