Skip to content

Commit 58580f0

Browse files
First Commit in this repository.
1 parent 3aa09cd commit 58580f0

12 files changed

+371
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Controlador:
2+
def __init__(self, simulador, vista):
3+
self.simulador = simulador
4+
self.vista = vista
5+
6+
def ejecutar_simulacion(self):
7+
self.simulador.simular()
8+
self.vista.configurar_grafico()
9+
self.vista.animar()
Binary file not shown.

Simulación orbital/Main.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from Models.EulerSimulador import EulerSimulador
2+
from Models.VerletSimulador import VerletSimulador
3+
from Views.VistaOrbital import VistaOrbital
4+
from Controllers.Controlador import Controlador
5+
6+
# Elegir método (EulerSimulador o VerletSimulador)
7+
simulador = VerletSimulador(dt=86400, velocidad_inicial=29783)
8+
vista = VistaOrbital(simulador)
9+
controlador = Controlador(simulador, vista)
10+
11+
controlador.ejecutar_simulacion()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .SimuladorBase import SimuladorBase
2+
3+
4+
class EulerSimulador(SimuladorBase):
5+
def simular(self):
6+
t = 0
7+
while t < self.t_total:
8+
ax, ay = self.calcular_aceleracion(self.x, self.y)
9+
self.vx += ax * self.dt
10+
self.vy += ay * self.dt
11+
self.x += self.vx * self.dt
12+
self.y += self.vy * self.dt
13+
self.x_vals.append(self.x)
14+
self.y_vals.append(self.y)
15+
t += self.dt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import math
2+
3+
4+
class SimuladorBase:
5+
G = 6.6748e-11 # Constante gravitacional
6+
M_SOL = 1.989e30 # Masa del Sol (kg)
7+
UA = 1.496e11 # 1 UA en metros
8+
9+
def __init__(self, dt=86400, velocidad_inicial=29783):
10+
self.dt = dt # Paso de tiempo (1 día)
11+
self.x = self.UA # Posición inicial en X
12+
self.y = 0.0 # Posición inicial en Y
13+
self.vx = 0.0 # Velocidad inicial en X
14+
self.vy = velocidad_inicial # Velocidad inicial en Y
15+
self.t_total = dt * 365 # 1 año en segundos
16+
self.x_vals = []
17+
self.y_vals = []
18+
19+
def calcular_aceleracion(self, x, y):
20+
r = math.sqrt(x**2 + y**2)
21+
a_mag = -self.G * self.M_SOL / r**3
22+
ax = a_mag * x
23+
ay = a_mag * y
24+
return ax, ay
25+
26+
def simular(self):
27+
raise NotImplementedError("Método simular debe ser implementado en subclases")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from .SimuladorBase import SimuladorBase
2+
3+
4+
class VerletSimulador(SimuladorBase):
5+
def simular(self):
6+
t = 0
7+
while t < self.t_total:
8+
ax, ay = self.calcular_aceleracion(self.x, self.y)
9+
10+
# Actualizar posiciones
11+
x_nuevo = self.x + self.vx * self.dt + 0.5 * ax * self.dt**2
12+
y_nuevo = self.y + self.vy * self.dt + 0.5 * ay * self.dt**2
13+
14+
# Calcular nueva aceleración
15+
ax_nuevo, ay_nuevo = self.calcular_aceleracion(x_nuevo, y_nuevo)
16+
17+
# Actualizar velocidades
18+
self.vx += 0.5 * (ax + ax_nuevo) * self.dt
19+
self.vy += 0.5 * (ay + ay_nuevo) * self.dt
20+
21+
self.x = x_nuevo
22+
self.y = y_nuevo
23+
self.x_vals.append(self.x)
24+
self.y_vals.append(self.y)
25+
t += self.dt
Binary file not shown.
Binary file not shown.
Binary file not shown.

Simulación orbital/Notebooks/Complejidad.ipynb

+253
Large diffs are not rendered by default.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.animation as animation
3+
4+
5+
class VistaOrbital:
6+
def __init__(self, simulador):
7+
self.simulador = simulador
8+
self.fig, self.ax = plt.subplots(figsize=(8, 8))
9+
10+
def configurar_grafico(self):
11+
self.ax.set_xlim(-1.6 * self.simulador.UA, 1.6 * self.simulador.UA)
12+
self.ax.set_ylim(-1.6 * self.simulador.UA, 1.6 * self.simulador.UA)
13+
self.ax.scatter(0, 0, color="yellow", s=100, label="Sol")
14+
self.ax.set_title("Simulación Orbital")
15+
self.ax.legend()
16+
17+
def animar(self):
18+
def update(frame):
19+
self.ax.clear()
20+
self.configurar_grafico()
21+
self.ax.plot(
22+
self.simulador.x_vals[:frame],
23+
self.simulador.y_vals[:frame],
24+
color="blue",
25+
label="Órbita",
26+
)
27+
28+
ani = animation.FuncAnimation(
29+
self.fig, update, frames=len(self.simulador.x_vals), interval=20
30+
)
31+
plt.show()
Binary file not shown.

0 commit comments

Comments
 (0)