I’m currently trying to do an assignment where I have to write a simulation for the restricted 3 body gravitational problem, with two fixed masses and one test mass. I have been given the equations I need to use in order to do so, but either I’m not understanding them correctly or I’m not implementing correctly. I would be very grateful is someone could help to push me in the right direction.
The information I’ve been given is as follows:
http://www.flickr.com/photos/91029993@N07/8269806430/in/photostream
basically I’ve been trying to test for a single mass,but my program is giving me a straight line for the movement of the test mass, over small times it looks as though the program is functioning correctly but then it doesn’t work as you get higher. (see images, inputs for these were 1 0.4 0.5 0 -1)
http://www.flickr.com/photos/91029993@N07/8268764897/in/photostream
I originally wrote my program quite faithfully to the equations given as follows:
#include<stdlib.h>
#include<stdio.h>
#include <math.h>
int main (int argc, char* argv[])
{
double dt=0.005, x[20000],y[20000],xv,yv,ax[20000],ay[20000],mneg,mpos,time;
int n;
FILE* output=fopen("proj1.out", "w");
sscanf(argv[1], "%lf", &mneg);
sscanf(argv[2], "%lf", &mpos);
sscanf(argv[3], "%lf", &x[0]);
sscanf(argv[4], "%lf", &y[0]);
sscanf(argv[5], "%lf", &xv);
sscanf(argv[6], "%lf", &yv);
x[1]=x[0]+(xv*dt);
y[1]=y[0]+(yv*dt);
for(n=1;n<150;n++)
{
ax[n]= (-mneg*(x[n]+1)/(pow((sqrt(pow((x[n]+1),2))),3))) -(mpos*(x[n]-1)/(pow((sqrt(pow((x[n]-1),2))),3)));
ay[n]= (-mneg*(y[n])/(pow(y[n],3))) -(mpos*(y[n])/(pow(y[n],3)));
x[n+1]=((2*x[n])-x[n-1] +(dt*dt*ax[n]));
y[n+1]=((2*y[n])-y[n-1]+(dt*dt*ay[n]));
fprintf(output, "%lf %lf\n",x[n-1], y[n-1]);
}
return(0);
}
I then tried a new method following the advice given here :Simulate the gravitational pull of a star? , so that i now have:
#include<stdlib.h>
#include<stdio.h>
#include <math.h>
int main (int argc, char* argv[])
{
double dt=0.005, x[20000],y[20000],xv,yv,ax[20000],ay[20000],mneg,time,r,a;
int n;
FILE* output=fopen("proj1.out", "w");
sscanf(argv[1], "%lf", &mneg);
sscanf(argv[2], "%lf", &x[0]);
sscanf(argv[3], "%lf", &y[0]);
sscanf(argv[4], "%lf", &xv);
sscanf(argv[5], "%lf", &yv);
x[1]=x[0]+(xv*dt);
y[1]=y[0]+(yv*dt);
for(n=1;n<150;n++)
{
r=sqrt(pow((x[n]+1),2)+pow(y[n],2));
a=mneg/(r*r);
ax[n]=a*((x[n]+1)/r);
ay[n]=a*((y[n])/r);
x[n+1]=((2*x[n])-x[n-1] +(dt*dt*ax[n]));
y[n+1]=((2*y[n])-y[n-1]+(dt*dt*ay[n]));
fprintf(output, "%lf %lf\n",x[n-1], y[n-1]);
}
return(0);
}
however, this doesn’t give me any better results.
I really don’t know where to go from here, I think I’m using the method right but I cant see any issues in the actual programming so I really don’t know whats going on, so any advice or pointers in the right direction would be very very much appreciated!
You did not translate the equations properly into code, and in particular the expression for the distance between the test mass and the fixed masses. The proper way to compute the acceleration is:
Please, use temporary variables to store intermediate expressions – do not put it all in one single complicated expression. Modern compilers are very good at eliminating redundant temporary expressions when they can optimise the code. The code above uses the fact that multiplication is usually a bit faster than division and also that
1.0/pow(sqrt(x), 3.0) == pow(x, -1.5).I would recommend that you replace the Verlet integrator with velocity Verlet instead. It stores particle velocities explicitly which allows you to compute the total energy of the system (sum of the kinetic and the potential energy) at each step. It should remain almost the same through the entire run (give or take the rounding and discretisation errors). If it deviates wildly, then you known that you do not compute your forces correctly.