// sequential.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "time.h"
#include < stdlib.h >
#include < iostream >
#include < math.h >
#define TIME 0.0001 //дискрет изменения времени
#define G 6.67e-11 //гравитационная постоянная
using namespace std;
struct body{
double weight;//масса тела
long double x,y,z;//координаты точки
long double vx,vy,vz;//компоненты скорости
};
void PrintSystem(body* system,int N,double Duration);
long double Length(body b1,body b2);
void Motion(body &b,long double Fx,long double Fy,long double Fz);
int _tmain(int argc, _TCHAR* argv[])
{
double time=0;//начальное время
int N;//число тел
double T;//длительность времени
body* system;//система тел
double Duration;
clock_t start,finish;
start=clock();
FILE* in;//файл с входными данными
in=fopen("inputdata.txt","r");
fscanf(in,"%d\t%lf\n",&N,&T);
system=new body[N];
int count=0;
while(!feof(in)){
fscanf(in,"%lf\t%lf\t%lf\n",&system[count].x,&system[count].y,&system[count].z);
system[count].weight=1e+20;
system[count].vx=system[count].vy=system[count].vz=0;
count++;
}
fclose(in);
long double Fx,Fy,Fz;
body* psystem;
psystem=new body[N];
for(int i=0;i < N;i++){
psystem[i].vx=psystem[i].vy=psystem[i].vz=0;
psystem[i].weight=1e+20;
psystem[i].x=system[i].x;
psystem[i].y=system[i].y;
psystem[i].z=system[i].z;
}
while(time < T){
for(int i=0;i < N;i++)
{
Fx=Fy=Fz=0;
for(int j=0;j < N;j++)
if(j!=i){
Fx+=G*system[i].weight*system[j].weight/pow(Length(system[i],system[j]),3)*(system[j].x-system[i].x);
Fy+=G*system[i].weight*system[j].weight/pow(Length(system[i],system[j]),3)*(system[j].y-system[i].y);
Fz+=G*system[i].weight*system[j].weight/pow(Length(system[i],system[j]),3)*(system[j].z-system[i].z);
}
Motion(psystem[i],Fx,Fy,Fz);
}
for(int i=0;i < N;i++){
system[i].x=psystem[i].x;
system[i].y=psystem[i].y;
system[i].z=psystem[i].z;
}
time+=TIME;
}
finish=clock();
Duration=(finish-start)/CLOCKS_PER_SEC;
PrintSystem(system,N,Duration);
delete[] psystem;
delete[] system;
return 0;
};
long double Length(body b1,body b2)
{
return sqrt((b1.x-b2.x)*(b1.x-b2.x)+(b1.y-b2.y)*(b1.y-b2.y)+(b1.z-b2.z)*(b1.z-b2.z));
};
void Motion(body &b,long double Fx,long double Fy,long double Fz){
long double accelx,accely,accelz;
accelx=Fx/b.weight;
accely=Fy/b.weight;
accelz=Fz/b.weight;
b.x=b.x+b.vx*TIME+accelx*TIME*TIME/2;
b.y=b.y+b.vy*TIME+accely*TIME*TIME/2;
b.z=b.z+b.vz*TIME+accelz*TIME*TIME/2;
b.vx=b.vx+accelx*TIME;
b.vy=b.vy+accely*TIME;
b.vz=b.vz+accelz*TIME;
};
void PrintSystem(body* system,int N,double Duration){
FILE *out;
out=fopen("outputdatasequential.txt","w");
for(int i=0;i < N;i++)
fprintf(out,"%lf\t%lf\t%lf\n",system[i].x,system[i].y,system[i].z);
fprintf(out,"%s %lf","Duration of sequential calculating:",Duration);
fclose(out);
};