#include<iostream>
#include<iomanip.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<fstream.h>
// Total number of bodies
const unsigned int N = 2;
const double G = 6.67e-11;
const double PI = 3.14159265358979;
double mass[N];
double position[N][2];
double velocity[N][2];
double force[N][2];
double sim_time;
const int NWEEK = 53;
double DT = 1;
// Function Prototypes
void moveBodies();
void calculateForce();
void computeForces(int index, float);
void write2File(char* filename);
typedef struct _runga{
double matrix[4][2];
}runga;
runga deltap[N];
runga deltav[N];
runga deltaf[N];
int main()
{
unsigned long i,j,k;
time_t start,end;
struct tm *tmstart, *tmend;
// Initializing the mass for the planets
mass[0] = 1.98911e30;
mass[1] = 5.97e24;
//Initializing the position of the planets according to given data
//Sun
position[0][0] = 0.0 ; position[0][1] = 0.0;
//Earth
position[1][0] = 150e9 ; position[1][1] = 0.0;
//Initializing the velocity of the planets according to given data
//Sun
velocity[0][0] = 0.0 ; velocity[0][1] = 0.0;
//Earth
velocity[1][0] = 0.0 ; velocity[1][1] = 2.9786e+004;
//Initializing the velocity and force
// Loop for week
for(j=0;j<N;j++)
{
force[j][0] = 0.0;
force[j][1] = 0.0;
}
//Initialization
for(i=0;i<N;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<2;k++)
{
deltap[i].matrix[j][k] = 0.0;
deltav[i].matrix[j][k] = 0.0;
deltaf[i].matrix[j][k] = 0.0;
}
}
}
ofstream outClientFile("solarRungaKutta4.txt",ios::out);
if(!outClientFile)
{
cerr << "File could not be opened" << endl;
exit(1);
}
//srand((unsigned) time(NULL));
// Starting time for the program
start = time(&start);
tmstart = localtime(&start);
cout << "The simulation is started at: ";
printf("%ld/%ld/%ld ",tmstart->tm_mday, tmstart->tm_mon + 1,
tmstart->tm_year + 1900);
printf("%ld:%ld:%ld\n\n",tmstart->tm_hour,tmstart->tm_min, tmstart->tm_sec);
outClientFile << "Starting time for the program:\t";
outClientFile << tmstart->tm_mday <<"/"<< (tmstart->tm_mon + 1) <<"/"<<
(tmstart->tm_year + 1900)<<" ";
outClientFile << tmstart->tm_hour <<":"<< tmstart->tm_min <<":"<<
tmstart->tm_sec<<endl;
outClientFile << "\nMass of Body 0 is: "<< mass[0]<< endl;
outClientFile << "Mass of Body 1 is: "<< mass[1]<< "\n" << endl;
outClientFile << setw(7) << setiosflags( ios::left ) << "Body";
outClientFile << setw(20) << setiosflags( ios::left ) << "X position";
outClientFile << setw(20) << setiosflags( ios::left ) << "Y position";
outClientFile << setw(20) << setiosflags( ios::left ) << "X velocity";
outClientFile << setw(20) << setiosflags( ios::left ) << "Y velocity";
outClientFile << setw(20) << setiosflags( ios::left ) <<
"Distance from Origin\n\n";
for(k=0; k<NWEEK; k++)
{
cout << "Week: " << (k+1) << endl;
for(i=0;i<604800;i++)
{
calculateForce();
moveBodies();
}
for(j=0;j<N;j++)
{
outClientFile << setw(7) << setiosflags( ios::left ) << j;
//outClientFile << setw(20) << setiosflags( ios::left ) << mass[j];
outClientFile << setw(20) << setiosflags( ios::left ) << position[j][0];
outClientFile << setw(20) << setiosflags( ios::left ) << position[j][1];
outClientFile << setw(20) << setiosflags( ios::left ) << velocity[j][0];
outClientFile << setw(20) << setiosflags( ios::left ) << velocity[j][1];
if(j==0)
outClientFile << setw(20) << setiosflags( ios::left ) <<
(sqrt(pow(position[0][0],2) + pow(position[0][1],2))) << endl;
else
outClientFile << setw(20) << setiosflags( ios::left ) <<
(sqrt(pow(position[j][0] - position[0][0],2) +
pow(position[j][1] - position[0][1],2))) << endl;
}
outClientFile << endl;
}
// Ending time for the program
end = time(&end);
tmend = localtime(&end);
cout << "The simulation is ended at: ";
printf("%ld/%ld/%ld ",tmend->tm_mday, tmend->tm_mon + 1,
tmend->tm_year + 1900);
printf("%ld:%ld:%ld",tmend->tm_hour,tmend->tm_min, tmend->tm_sec);
outClientFile << "Ending time for the program:\t";
outClientFile << tmend->tm_mday <<"/"<< (tmend->tm_mon + 1) <<"/"<<
(tmend->tm_year + 1900)<<" ";
outClientFile << tmend->tm_hour <<":"<< tmend->tm_min <<":"<<
tmend->tm_sec<<endl;
sim_time = difftime(end,start);
outClientFile << "\nThe time taken by the simulation is: " << sim_time <<
" seconds" << endl;
cout << "The time taken by the simulation is: " << sim_time <<
" seconds" << endl;
//Closing the file
outClientFile.close();
return 0;
}
/************************************************************************/
void calculateForce()
/************************************************************************/
{
double distance, magnitude, temp;
double direction[2];
long i, j;
for(i=0;i<(N-1);i++)
{
for(j=(i+1);j<N;j++)
{
temp = pow(position[i][0] - position[j][0], 2) + pow(position[i][1] - position[j][1], 2);
distance = sqrt(temp);
magnitude = ( G*mass[i]*mass[j] )/temp;
direction[0] = position[j][0] - position[i][0];
direction[1] = position[j][1] - position[i][1];
force[i][0] = force[i][0] + magnitude*direction[0]/distance;
force[j][0] = force[j][0] - magnitude*direction[0]/distance;
force[i][1] = force[i][1] + magnitude*direction[1]/distance;
force[j][1] = force[j][1] - magnitude*direction[1]/distance;
}
}
}
/************************************************************************/
void moveBodies()
/************************************************************************/
{
int i,j,k;
for(i=0;i<N;i++)
{
//Position
deltap[i].matrix[0][0] = DT*velocity[i][0];
deltap[i].matrix[0][1] = DT*velocity[i][1];
//Velocity
deltav[i].matrix[0][0] = DT*force[i][0]/mass[i];
deltav[i].matrix[0][1] = DT*force[i][1]/mass[i];
}
for(i=0;i<N;i++)
{
//Position
deltap[i].matrix[1][0] = DT*( velocity[i][0] + 0.5*deltav[i].matrix[0][0] );
deltap[i].matrix[1][1] = DT*( velocity[i][1] + 0.5*deltav[i].matrix[0][1] );
//Velocity
computeForces(0,0.5);
deltav[i].matrix[1][0] = DT*deltaf[i].matrix[0][0]/mass[i];
deltav[i].matrix[1][1] = DT*deltaf[i].matrix[0][1]/mass[i];
}
for(i=0;i<N;i++)
{
//Position
deltap[i].matrix[2][0] = DT*( velocity[i][0] + 0.5*deltav[i].matrix[1][0] );
deltap[i].matrix[2][1] = DT*( velocity[i][1] + 0.5*deltav[i].matrix[1][1] );
//Velocity
computeForces(1,0.5);
deltav[i].matrix[2][0] = DT*deltaf[i].matrix[1][0]/mass[i];
deltav[i].matrix[2][1] = DT*deltaf[i].matrix[1][1]/mass[i];
}
for(i=0;i<N;i++)
{
//Position
deltap[i].matrix[3][0] = DT*( velocity[i][0] + deltav[i].matrix[2][0] );
deltap[i].matrix[3][1] = DT*( velocity[i][1] + deltav[i].matrix[2][1] );
//Velocity
computeForces(2, 1.);
deltav[i].matrix[3][0] = DT*deltaf[i].matrix[2][0]/mass[i];
deltav[i].matrix[3][1] = DT*deltaf[i].matrix[2][1]/mass[i];
}
for(i=0;i<N;i++)
{
position[i][0] = position[i][0] + (deltap[i].matrix[0][0] + 2*deltap[i].matrix[1][0] + 2*deltap[i].matrix[2][0] + deltap[i].matrix[3][0])/6;
position[i][1] = position[i][1] + (deltap[i].matrix[0][1] + 2*deltap[i].matrix[1][1] + 2*deltap[i].matrix[2][1] + deltap[i].matrix[3][1])/6;
velocity[i][0] = velocity[i][0] + (deltav[i].matrix[0][0] + 2*deltav[i].matrix[1][0] + 2*deltav[i].matrix[2][0] + deltav[i].matrix[3][0])/6;
velocity[i][1] = velocity[i][1] + (deltav[i].matrix[0][1] + 2*deltav[i].matrix[1][1] + 2*deltav[i].matrix[2][1] + deltav[i].matrix[3][1])/6;
force[i][0] = 0.0;
force[i][1] = 0.0;
}
// Equating the force to zero
for(i=0;i<N;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<2;k++)
{
deltaf[i].matrix[j][k] = 0.0;
deltaf[i].matrix[j][k] = 0.0;
deltaf[i].matrix[j][k] = 0.0;
}
}
}
}