Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F81375810
compute_gravity.cc
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Sep 5, 15:47
Size
1 KB
Mime Type
text/x-c
Expires
Sat, Sep 7, 15:47 (2 d)
Engine
blob
Format
Raw Data
Handle
20558622
Attached To
R7571 SP4E-TB-TL-FR
compute_gravity.cc
View Options
#include <cmath>
#include "compute_gravity.hh"
/* -------------------------------------------------------------------------- */
void ComputeGravity::compute(System& system) {
// Get the total number of particles
int nparticles = system.getNbParticles();
//Initialize types
Vector v_ij;
Real r2_ij = 0.;
Real r_ij = 0;
Vector force;
// Loop over all the particles and gets each one's parameters
for (int pi = 0; pi < nparticles; ++pi) {
Particle & PI = system.getParticle(pi);
//loop over all next interacting particles
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 gravity force vector interaction on i with j
force = v_ij / r_ij* PI.getMass() * PJ.getMass() * G / r2_ij;
//F_ij=-F_ji Particles have opposite forces
PJ.getForce() -= force;
PI.getForce() += force;
}
}
}
Event Timeline
Log In to Comment