Repository navigation
Expand file tree
/
Copy pathrocket_engine.cpp
More file actions
87 lines (73 loc) · 2.62 KB
/
Copy pathrocket_engine.cpp
File metadata and controls
87 lines (73 loc) · 2.62 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
#include <iostream>
#include <cmath>
#include <pybind11/pybind11.h>
namespace py = pybind11;
class Rocket {
private:
double height = 0.0;
double velocity = 0.0;
double fuel_mass = 0.0;
double acceleration = 0.0;
double rocket_mass = 0.0;
double total_mass = 0.0;
double fuel_burn_rate = 0.0;
double exhaust_velocity = 0.0;
double thrust = 0.0;
double diameter = 0.0;
double drag_coefficient = 0.3;
public:
Rocket() = default;
void set_parameters(double fuel_m, double rocket_m, double burn_rate,
double exhaust_v, double diam, double drag_c) {
fuel_mass = fuel_m;
rocket_mass = rocket_m;
fuel_burn_rate = burn_rate;
exhaust_velocity = exhaust_v;
diameter = diam;
drag_coefficient = drag_c;
total_mass = rocket_mass + fuel_mass;
}
double get_fuel_mass() const { return fuel_mass; }
double get_height() const { return height; }
double get_velocity() const { return velocity; }
double get_acceleration() const { return acceleration; }
double air_density(double h) {
double rho0 = 1.225;
double H = 8500.0;
return rho0 * std::exp(-h / H);
}
void update_step(double dt = 1.0) {
// 1. Ubytek paliwa i ciąg
if (fuel_mass > 0) {
thrust = exhaust_velocity * fuel_burn_rate;
fuel_mass -= fuel_burn_rate * dt;
if (fuel_mass < 0) fuel_mass = 0;
} else {
thrust = 0.0;
}
total_mass = rocket_mass + fuel_mass;
// 2. Aerodynamika i siły
double radius = diameter / 2.0;
double area = 3.14159265 * radius * radius;
double drag = 0.5 * air_density(height) * (velocity * std::abs(velocity)) * drag_coefficient * area;
double net_force = thrust - drag - (total_mass * 9.81);
acceleration = net_force / total_mass;
// 3. Całkowanie Eulera
height += velocity * dt;
if (height < 0.0) height = 0.0;
velocity += acceleration * dt;
}
bool is_burning() const { return fuel_mass > 0; }
};
// --- SEKCKA PYBIND11 (eksport do Pythona) ---
PYBIND11_MODULE(rocket_engine, m) {
py::class_<Rocket>(m, "Rocket")
.def(py::init<>())
.def("set_parameters", &Rocket::set_parameters)
.def("update_step", &Rocket::update_step, py::arg("dt") = 1.0)
.def("get_height", &Rocket::get_height)
.def("get_velocity", &Rocket::get_velocity)
.def("get_acceleration", &Rocket::get_acceleration)
.def("get_fuel_mass", &Rocket::get_fuel_mass)
.def("is_burning", &Rocket::is_burning);
}