// http://www.cs.unc.edu/~andrewz/comp203/hw2/index.html /* gcc mygravity7.c -lm ./a.out > gravity7.out This version: 12 particles, (0,0) mass 100000000 set xrange [-1:10] set yrange [-1:10] 40 time steps. Plot with Gnuplot plot 'gravity7.out' using 1:2, 'gravity7.out' using 3:4, \ 'gravity7.out' using 5:6, 'gravity7.out' using 7:8, \ 'gravity7.out' using 9:10, 'gravity7.out' using 11:12; */ #include //============================================================================ // mygravity5.cpp //============================================================================ // http://www.cs.unc.edu/~andrewz/comp203/hw2/gravity/gravity.cpp #include #include #define G 6.673e-11 // GRAVITATIONAL CONSTANT #define PI 3.1415926535898 // CONSTANT PI struct Particle { double px, py; double vx, vy; double Mass; double ForceAccumX, ForceAccumY; }; void ComputeGravForces_Fast(struct Particle Particles[], int NumParticles); void UpdateParticles_MidPoint(struct Particle Particles[], int NumParticles, float DeltaT); void CheckMomentumConservation(struct Particle Particles[], int NumParticles); void InitConfig1(struct Particle Particles[], int NumParticles); void ComputeGravForce(struct Particle A, struct Particle B, double *ForceX, double *ForceY) { double ABx = B.px - A.px; double ABy = B.py - A.py; double DistAB2 = ABx*ABx+ABy*ABy; double DistAB = sqrt(DistAB2); double DistAB3 = DistAB2 * DistAB; double Scale = (G * A.Mass * B.Mass) / DistAB3; *ForceX = Scale * ABx; *ForceY = Scale * ABy; } void ComputeGravForces_Fast(struct Particle *Particles, int NumParticles) { int i,j; double ForceX, ForceY; for (i=0; i