-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.py
More file actions
129 lines (114 loc) · 3.83 KB
/
Copy pathsim.py
File metadata and controls
129 lines (114 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import contextlib, io
import matplotlib
from dotenv import load_dotenv
import os
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from rocketpy import Environment, SolidMotor, Rocket, Flight
load_dotenv()
L1395_THRUST = [
(0.0, 0.0),
(0.1, 1530.0),
(0.3, 1590.0),
(0.7, 1560.0),
(1.2, 1520.0),
(1.8, 1500.0),
(2.4, 1490.0),
(3.0, 1480.0),
(3.5, 1460.0),
(3.9, 1440.0),
(4.0, 0.0),
]
def _build_and_fly(name: str, mass: float, inclination: float) -> dict:
env = Environment(latitude=41.775, longitude=-86.572, elevation=236)
motor = SolidMotor(
thrust_source=L1395_THRUST,
burn_time=4.0,
dry_mass=2.952,
dry_inertia=(0, 0, 0),
center_of_dry_mass_position=0.397,
grains_center_of_mass_position=0.397,
grain_number=5,
grain_separation=0.005,
grain_density=1750,
grain_outer_radius=0.033,
grain_initial_inner_radius=0.015,
grain_initial_height=0.12,
nozzle_radius=0.033,
throat_radius=0.011,
interpolation_method="linear",
nozzle_position=0.0,
coordinate_system_orientation="combustion_chamber_to_nozzle",
)
rocket = Rocket(
radius=0.0635,
mass=mass,
inertia=(6.321, 6.321, 0.034),
power_off_drag=0.43,
power_on_drag=0.43,
center_of_mass_without_motor=0.0,
coordinate_system_orientation="nose_to_tail",
)
rocket.set_rail_buttons(1.5, 2, 45)
rocket.add_motor(motor=motor, position=-1.373)
rocket.add_nose(length=0.55829, kind="tangent", position=1.278)
rocket.add_trapezoidal_fins(
4, span=0.170, root_chord=0.270, tip_chord=0.090, position=-1.04
)
rocket.add_parachute("Main", cd_s=10.0, trigger="apogee", sampling_rate=20, lag=1.5)
with contextlib.redirect_stdout(io.StringIO()):
flight = Flight(
rocket=rocket,
environment=env,
rail_length=5.18,
inclination=inclination,
heading=133,
terminate_on_apogee=True,
max_time_step=0.1,
)
apogee_m = flight.apogee - env.elevation
apogee_ft = apogee_m * 3.28084
filename = "orbit_sim.png"
plot_path = f"assets/{filename}"
os.makedirs("assets", exist_ok=True)
fig, axs = plt.subplots(3, 1, figsize=(8, 10))
fig.suptitle(f"Orbit — {name}", fontsize=14, fontweight="bold")
t = flight.z[:, 0]
alt = flight.z[:, 1] - env.elevation
axs[0].plot(t, alt, linewidth=2)
axs[0].set_title("Altitude vs Time")
axs[0].set_xlabel("Time (s)")
axs[0].set_ylabel("Altitude AGL (m)")
axs[0].grid(True, alpha=0.3)
vt = flight.speed[:, 0]
v = flight.speed[:, 1]
axs[1].plot(vt, v, linewidth=2)
axs[1].set_title("Velocity vs Time")
axs[1].set_xlabel("Time (s)")
axs[1].set_ylabel("Velocity (m/s)")
axs[1].grid(True, alpha=0.3)
mt = flight.mach_number[:, 0]
mach = flight.mach_number[:, 1]
axs[2].plot(mt, mach, linewidth=2)
axs[2].set_title("Mach Number vs Time")
axs[2].set_xlabel("Time (s)")
axs[2].set_ylabel("Mach")
axs[2].grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(plot_path, dpi=150)
plt.close()
return {
"name": name,
"apogee_ft": round(apogee_ft, 1),
"apogee_m": round(apogee_m, 1),
"max_velocity_ms": round(flight.max_speed, 1),
"max_mach": round(flight.max_mach_number, 3),
"time_to_apogee_s": round(flight.apogee_time, 1),
"plot_url": f"https://orbitdemo.up.railway.app/assets/{filename}",
}
def run_simulation_from_params(params: dict) -> dict:
return _build_and_fly(
name=params.get("name", "Custom Rocket"),
mass=max(1.0, min(float(params.get("mass", 15.0)), 50.0)),
inclination=max(45.0, min(float(params.get("inclination", 84.0)), 90.0)),
)