Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. struct astral_body{
  6.     double position[3];
  7.     double velocity[3];
  8.     double mass;
  9. };
  10.  
  11. //function declaration
  12. void compute_leapfrog(astral_body &planet, astral_body &star_1, astral_body &star_2, double dt);
  13.  
  14. int main()
  15. {
  16.     double dt=1;
  17.     int N=10;
  18.    
  19.     astral_body planet;
  20.     astral_body star_1;
  21.     astral_body star_2;
  22.    
  23.     //initial parameters
  24.     star_1.position[0]=1;
  25.     star_1.position[1]=0;
  26.     star_1.position[2]=0;
  27.     star_2.position[0]=-1;
  28.     star_2.position[1]=0;
  29.     star_2.position[2]=0;
  30.     planet.position[0]=0;
  31.     planet.position[1]=0;
  32.     planet.position[2]=0;
  33.    
  34.     planet.mass=1; 
  35.     star_1.mass=100;   
  36.     star_2.mass=100;
  37.    
  38.     cout<<"enter the x component of the planet's velocity"<<endl;
  39.     cin>>planet.velocity[0];
  40.     cout<<"enter the y component of the planet's velocity"<<endl;
  41.     cin>>planet.velocity[1];
  42.     cout<<"enter the z component of the planet's velocity"<<endl;
  43.     cin>>planet.velocity[2];
  44.    
  45.    
  46.     for(int count=1; count<N; count++)
  47.     {
  48.         cout<<planet.position[0]<<" "<<planet.position[1]<<"    "<<planet.position[2]<<endl;
  49.         compute_leapfrog(planet, star_1, star_2, dt);
  50.     }
  51.    
  52. }
  53.  
  54. void compute_leapfrog(astral_body &planet, astral_body &star_1, astral_body &star_2, double dt)
  55. {
  56.     double G=100;
  57.     double f0, f1, f2;
  58.    
  59.     planet.position[0]+=0.5*dt*planet.velocity[0];
  60.     planet.position[1]+=0.5*dt*planet.velocity[1];
  61.     planet.position[2]+=0.5*dt*planet.velocity[2];
  62.  
  63.     f0=(G*planet.mass*star_1.mass)*((1.0/pow((star_1.position[0]-planet.position[0]),2.0)))+((G*planet.mass*star_2.mass)/(1.0/pow((star_2.position[0]-planet.position[0]),2.0)));
  64.     f1=(G*planet.mass*star_1.mass)*((1.0/pow((star_1.position[1]-planet.position[1]),2.0)))+((G*planet.mass*star_2.mass)/(1.0/pow((star_2.position[1]-planet.position[1]),2.0)));
  65.     f2=(G*planet.mass*star_1.mass)*((1.0/pow((star_1.position[2]-planet.position[2]),2.0)))+((G*planet.mass*star_2.mass)/(1.0/pow((star_2.position[2]-planet.position[2]),2.0)));
  66.    
  67.     cout<<f0<<f1<<f2<<endl;
  68.    
  69.     planet.velocity[0]+=dt*(f0/planet.mass);
  70.     planet.velocity[1]+=dt*(f1/planet.mass);
  71.     planet.velocity[2]+=dt*(f2/planet.mass);
  72.        
  73.     planet.position[0]+=0.5*dt*planet.velocity[0];
  74.     planet.position[1]+=0.5*dt*planet.velocity[1];
  75.     planet.position[2]+=0.5*dt*planet.velocity[2];
  76.    
  77.     //any steps about incrementing t have been left out as I am aiming to plot the planet's path, not anything with time.
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement