Skip to content
Snippets Groups Projects
Commit 5f0868fb authored by Brayan Santiago Amorocho Lizcano's avatar Brayan Santiago Amorocho Lizcano
Browse files

N-Body-Simulation

parent 7738177d
No related branches found
No related tags found
No related merge requests found
X , Y , Z , t , Planeta
5 , 5 , 25 , 4 , Planeta: 1
52 , 55 , 11 , 4 , Planeta: 2
2.47033e-323 , 5 , 5 , 4 , Planeta: 3
4.68281e-312 , 52 , 55 , 4 , Planeta: 4
X , Y , Z , t , Planeta
5 , 5 , 25 , 5 , Planeta: 1
52 , 55 , 11 , 5 , Planeta: 2
#include <iostream>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
int main(){
// Introducimos el número de cuerpos
int n;
cout << "Introduzca el número de cuerpos: ";
cin >> n;
// Introducimos los datos iniciales de los cuerpos (posición, velocidad, masa)
double x[n], y[n], z[n], vx[n], vy[n], vz[n], m[n];
for(int i=0; i<n; i++){
cout << "Introduzca la posición del cuerpo " << i+1 << " en x: ";
cin >> x[i];
cout << "Introduzca la posición del cuerpo " << i+1 << " en y: ";
cin >> y[i];
cout << "Introduzca la posición del cuerpo " << i+1 << " en z: ";
cin >> z[i];
cout << "Introduzca la velocidad del cuerpo " << i+1 << " en x: ";
cin >> vx[i];
cout << "Introduzca la velocidad del cuerpo " << i+1 << " en y: ";
cin >> vy[i];
cout << "Introduzca la velocidad del cuerpo " << i+1 << " en z: ";
cin >> vz[i];
cout << "Introduzca la masa del cuerpo " << i+1 << ": ";
cin >> m[i];
}
// Imprimimos los datos para confirmar que se han introducido correctamente
cout << "Los datos iniciales son: " << endl;
for(int i=0; i<n; i++){
cout << "Cuerpo " << i+1 << ": " << endl;
cout << "Posición: (" << x[i] << ", " << y[i] << ", " << z[i] << ")" << endl;
cout << "Velocidad: (" << vx[i] << ", " << vy[i] << ", " << vz[i] << ")" << endl;
cout << "Masa: " << m[i] << endl;
}
// Calculamos las fuerzas que actúan sobre cada cuerpo
double Fx[n], Fy[n], Fz[n], r, G;
// fill(Fx.begin(), Fx.end(), 0);
// fill(Fy.begin(), Fy.end(), 0);
// fill(Fz, Fz+n, 0);
G = 6.67408e-11;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(i!=j){
r = sqrt(pow(x[i]-x[j], 2) + pow(y[i]-y[j], 2) + pow(z[i]-z[j], 2));
Fx[i] = Fx[i] + G*m[i]*m[j]*(x[j]-x[i])/pow(r, 3);
Fy[i] = Fy[i] + G*m[i]*m[j]*(y[j]-y[i])/pow(r, 3);
Fz[i] = Fz[i] + G*m[i]*m[j]*(z[j]-z[i])/pow(r, 3);
}
}
}
// Definimos un dt y un tiempo final
double dt, t, tf;
cout << "Introduzca tasa de cambio del tiempo: ";
cin >> dt;
t = 0;
cout << "Introduzca el tiempo final: ";
cin >> tf;
// Calculamos las aceleraciones iniciales de cada cuerpo
double ax[n], ay[n], az[n];
for(int i=0; i<n; i++){
ax[i] = Fx[i]/m[i];
ay[i] = Fy[i]/m[i];
az[i] = Fz[i]/m[i];
}
int contlineas = 0;
// Calculamos las posiciones y velocidades de cada cuerpo
while(t<tf){
for(int i = 0; i<n; i++){
// Calculamos las nuevas posiciones
x[i] = x[i] + vx[i]*dt + 0.5*ax[i]*pow(dt, 2);
y[i] = y[i] + vy[i]*dt + 0.5*ay[i]*pow(dt, 2);
z[i] = z[i] + vz[i]*dt + 0.5*az[i]*pow(dt, 2);
vx[i] = vx[i] + ax[i]*dt;
vy[i] = vy[i] + ay[i]*dt;
vz[i] = vz[i] + az[i]*dt;
// Calculamos las nuevas fuerzas que actúan sobre cada cuerpo
Fx[i] = 0;
Fy[i] = 0;
Fz[i] = 0;
for(int j=0; j<n; j++){
if(i!=j){
r = sqrt(pow(x[i]-x[j], 2) + pow(y[i]-y[j], 2) + pow(z[i]-z[j], 2));
Fx[i] = Fx[i] + G*m[i]*m[j]*(x[j]-x[i])/pow(r, 3);
Fy[i] = Fy[i] + G*m[i]*m[j]*(y[j]-y[i])/pow(r, 3);
Fz[i] = Fz[i] + G*m[i]*m[j]*(z[j]-z[i])/pow(r, 3);
}
}
// Calculamos las nuevas aceleraciones de cada cuerpo
ax[i] = Fx[i]/m[i];
ay[i] = Fy[i]/m[i];
az[i] = Fz[i]/m[i];
//Guardamos los datos en un archivo txt
ofstream archivo;
archivo.open("Data.txt");
archivo << "X" <<" , " << "Y" <<" , " << "Z" << " , " << "t" << " , " << "Planeta" << endl;
int number, j;
j = 0;
while (j < contlineas){
std::string number = std::to_string(j+1);
archivo << x[j] << " , " << y[j] << " , " << z[j] << " , " << t << " , " << "Planeta: " + number << endl;
j++;
}
}
t += dt;
contlineas += 1;
}
// Imprimimos los datos
cout << "Los datos finales son: " << endl;
for(int i=0; i<n; i++){
cout << "Cuerpo " << i+1 << ": " << endl;
cout << "Posición: (" << x[i] << ", " << y[i] << ", " << z[i] << ")" << endl;
cout << "Velocidad: (" << vx[i] << ", " << vy[i] << ", " << vz[i] << ")" << endl;
cout << "Masa: " << m[i] << endl;
}
// Guardamos los datos en un archivo final
ofstream archivo2;
archivo2.open("Data_final.txt");
archivo2 << "X" <<" , " << "Y" <<" , " << "Z" << " , " << "t" << " , " << "Planeta" << endl;
for (int i = 0; i<n; i++){
std::string number = std::to_string(i+1);
archivo2 << x[i] << " , " << y[i] << " , " << z[i] << " , " << t << " , " << "Planeta: " + number << endl;
}
}
\ No newline at end of file
File added
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
```
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment