Skip to content

Commit f881d7e

Browse files
thatrandomasiandevGui-FernandesBR
authored andcommitted
MNT: store ref_factor on rocket aero surface components (#561)
1 parent cb6106a commit f881d7e

9 files changed

Lines changed: 143 additions & 28 deletions

File tree

‎rocketpy/plots/flight_plots.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ def _rocket_axial_display_coordinate(self, value, display_length):
694694
"""Map a rocket axial coordinate onto the centered display model."""
695695
coordinates = [
696696
float(position.z)
697-
for _surface, position in self.flight.rocket.aerodynamic_surfaces
697+
for _surface, position, _ref_factor in self.flight.rocket.aerodynamic_surfaces
698698
]
699699
coordinates.extend(
700700
[

‎rocketpy/plots/rocket_plots.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def _draw_aerodynamic_surfaces(self, ax, vis_args, plane, surfaces):
243243
# diameter changes. The final point of the last surface is the final
244244
# point of the last tube
245245

246-
for surface, position in surfaces:
246+
for surface, position, _ref_factor in surfaces:
247247
if isinstance(surface, NoseCone):
248248
self._draw_nose_cone(ax, surface, position.z, drawn_surfaces, vis_args)
249249
elif isinstance(surface, Tail):
@@ -645,7 +645,7 @@ def _draw_nozzle_tube(self, last_radius, last_x, nozzle_position, ax, vis_args):
645645
def _draw_rail_buttons(self, ax, vis_args):
646646
"""Draws the rail buttons of the rocket."""
647647
try:
648-
buttons, pos = self.rocket.rail_buttons[0]
648+
buttons, pos, _ref_factor = self.rocket.rail_buttons[0]
649649
lower = pos.z
650650
upper = lower + buttons.buttons_distance * self.rocket._csys
651651
ax.scatter(

‎rocketpy/prints/rocket_prints.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,18 @@ def rocket_aerodynamics_quantities(self):
102102
None
103103
"""
104104
print("\nAerodynamics Lift Coefficient Derivatives\n")
105-
for surface, _ in self.rocket.aerodynamic_surfaces:
105+
for surface, _position, ref_factor in self.rocket.aerodynamic_surfaces:
106106
if isinstance(surface, GenericSurface):
107107
continue
108108
name = surface.name
109109
# ref_factor corrects lift for different reference areas
110-
ref_factor = (surface.rocket_radius / self.rocket.radius) ** 2
111110
print(
112111
f"{name} Lift Coefficient Derivative: "
113112
f"{ref_factor * surface.clalpha(0):.3f}/rad"
114113
)
115114

116115
print("\nCenter of Pressure\n")
117-
for surface, position in self.rocket.aerodynamic_surfaces:
116+
for surface, position, _ref_factor in self.rocket.aerodynamic_surfaces:
118117
name = surface.name
119118
cpz = surface.cp[2] # relative to the user defined coordinate system
120119
print(

‎rocketpy/rocket/components.py‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ class Components:
1515
A list of named tuples representing all the components and their
1616
positions relative to the rocket.
1717
component_tuple : namedtuple
18-
A named tuple representing a component and its position within the
19-
rocket.
18+
A named tuple representing a component, its position within the
19+
rocket, and an optional reference-area correction factor.
2020
"""
2121

2222
def __init__(self):
2323
"""Initialize an empty components list instance."""
24-
self.component_tuple = namedtuple("component_tuple", "component position")
24+
self.component_tuple = namedtuple(
25+
"component_tuple", "component position ref_factor", defaults=(1.0,)
26+
)
2527
self._components = []
2628

2729
# List of components and their positions to avoid extra for loops in
@@ -34,6 +36,7 @@ def __repr__(self):
3436
components_str = "\n".join(
3537
[
3638
f"\tComponent: {str(c.component):80} Position: {c.position}"
39+
f" Ref Factor: {c.ref_factor}"
3740
for c in self._components
3841
]
3942
)
@@ -52,7 +55,7 @@ def __iter__(self):
5255
"""Return an iterator over the list of components."""
5356
return iter(self._components)
5457

55-
def add(self, component, position):
58+
def add(self, component, position, ref_factor=1.0):
5659
"""Add a component to the list of components.
5760
5861
Parameters
@@ -62,14 +65,19 @@ def add(self, component, position):
6265
position : int, float
6366
The position of the component relative to the rocket's
6467
coordinate system origin.
68+
ref_factor : int, float, optional
69+
Reference-area correction factor associating the component to the
70+
rocket reference area. Defaults to 1.0 when not applicable.
6571
6672
Returns
6773
-------
6874
None
6975
"""
7076
self.__component_list.append(component)
7177
self.__position_list.append(position)
72-
self._components.append(self.component_tuple(component, position))
78+
self._components.append(
79+
self.component_tuple(component, position, ref_factor)
80+
)
7381

7482
def get_by_type(self, component_type):
7583
"""Search the list of components and return a list with all the
@@ -207,7 +215,11 @@ def sort_by_position(self, reverse=False):
207215
def to_dict(self, **kwargs): # pylint: disable=unused-argument
208216
return {
209217
"components": [
210-
{"component": c.component, "position": c.position}
218+
{
219+
"component": c.component,
220+
"position": c.position,
221+
"ref_factor": c.ref_factor,
222+
}
211223
for c in self._components
212224
]
213225
}
@@ -216,5 +228,9 @@ def to_dict(self, **kwargs): # pylint: disable=unused-argument
216228
def from_dict(cls, data):
217229
components = cls()
218230
for component in data["components"]:
219-
components.add(component["component"], component["position"])
231+
components.add(
232+
component["component"],
233+
component["position"],
234+
ref_factor=component.get("ref_factor", 1.0),
235+
)
220236
return components

‎rocketpy/rocket/rocket.py‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,10 @@ def evaluate_center_of_pressure(self):
666666

667667
# Calculate total lift coefficient derivative and center of pressure
668668
if len(self.aerodynamic_surfaces) > 0:
669-
for aero_surface, position in self.aerodynamic_surfaces:
669+
for aero_surface, position, ref_factor in self.aerodynamic_surfaces:
670670
if isinstance(aero_surface, GenericSurface):
671671
continue
672672
# ref_factor corrects lift for different reference areas
673-
ref_factor = (aero_surface.rocket_radius / self.radius) ** 2
674673
self.total_lift_coeff_der += ref_factor * aero_surface.clalpha
675674
self.cp_position += (
676675
ref_factor
@@ -693,7 +692,7 @@ def evaluate_surfaces_cp_to_cdm(self):
693692
Dictionary mapping the relative position of each aerodynamic
694693
surface center of pressure to the rocket's center of mass.
695694
"""
696-
for surface, position in self.aerodynamic_surfaces:
695+
for surface, position, _ref_factor in self.aerodynamic_surfaces:
697696
self.__evaluate_single_surface_cp_to_cdm(surface, position)
698697
return self.surfaces_cp_to_cdm
699698

@@ -794,7 +793,7 @@ def warn_if_unstable(self):
794793
"""
795794
has_generic_surface = any(
796795
isinstance(aero_surface, GenericSurface)
797-
for aero_surface, _position in self.aerodynamic_surfaces
796+
for aero_surface, _position, _ref_factor in self.aerodynamic_surfaces
798797
)
799798
if has_generic_surface:
800799
return False
@@ -1169,7 +1168,12 @@ def __add_single_surface(self, surface, position):
11691168
self.rail_buttons = Components()
11701169
self.rail_buttons.add(surface, position)
11711170
else:
1172-
self.aerodynamic_surfaces.add(surface, position)
1171+
# ref_factor corrects lift for different reference areas
1172+
if getattr(surface, "rocket_radius", None) is not None:
1173+
ref_factor = (surface.rocket_radius / self.radius) ** 2
1174+
else:
1175+
ref_factor = 1.0
1176+
self.aerodynamic_surfaces.add(surface, position, ref_factor=ref_factor)
11731177
self.__evaluate_single_surface_cp_to_cdm(surface, position)
11741178

11751179
def add_surfaces(self, surfaces, positions):
@@ -2338,10 +2342,10 @@ def from_dict(cls, data):
23382342
position=data["motor_position"],
23392343
)
23402344

2341-
for surface, position in data["aerodynamic_surfaces"]:
2345+
for surface, position, _ref_factor in data["aerodynamic_surfaces"]:
23422346
rocket.add_surfaces(surfaces=surface, positions=position)
23432347

2344-
for button, position in data["rail_buttons"]:
2348+
for button, position, _ref_factor in data["rail_buttons"]:
23452349
rocket.set_rail_buttons(
23462350
upper_button_position=position[2] + button.buttons_distance,
23472351
lower_button_position=position[2],
@@ -2352,7 +2356,7 @@ def from_dict(cls, data):
23522356
for parachute in data["parachutes"]:
23532357
rocket.parachutes.append(parachute)
23542358

2355-
for sensor, position in data["sensors"]:
2359+
for sensor, position, _ref_factor in data["sensors"]:
23562360
rocket.add_sensor(sensor, position)
23572361

23582362
for air_brake in data["air_brakes"]:

‎rocketpy/simulation/flight.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ def __measure_sensors(self, component_sensors, u_dot, t=None, y_sol=None):
898898
Parameters
899899
----------
900900
component_sensors : list
901-
List of (sensor, position) tuples.
901+
List of (sensor, position, ref_factor) component tuples.
902902
u_dot : array_like
903903
State derivative vector.
904904
t : float, optional
@@ -911,7 +911,7 @@ def __measure_sensors(self, component_sensors, u_dot, t=None, y_sol=None):
911911
if y_sol is None:
912912
y_sol = self.y_sol
913913

914-
for sensor, position in component_sensors:
914+
for sensor, position, _ref_factor in component_sensors:
915915
relative_position = position - self.rocket._csys * Vector(
916916
[0, 0, self.rocket.center_of_dry_mass_position]
917917
)
@@ -2087,7 +2087,7 @@ def u_dot(self, t, u, post_processing=False): # pylint: disable=too-many-locals
20872087
# Calculate lift and moment for each component of the rocket
20882088
velocity_in_body_frame = Vector([vx_b, vy_b, vz_b])
20892089
w = Vector([omega1, omega2, omega3])
2090-
for aero_surface, _ in self.rocket.aerodynamic_surfaces:
2090+
for aero_surface, _, _ref_factor in self.rocket.aerodynamic_surfaces:
20912091
# Component cp relative to CDM in body frame
20922092
comp_cp = self.rocket.surfaces_cp_to_cdm[aero_surface]
20932093
# Component absolute velocity in body frame
@@ -2343,7 +2343,7 @@ def u_dot_generalized_3dof(self, t, u, post_processing=False):
23432343
# Velocity in body frame
23442344
vb_body = Kt @ v
23452345

2346-
for surface, _ in self.rocket.aerodynamic_surfaces:
2346+
for surface, _, _ref_factor in self.rocket.aerodynamic_surfaces:
23472347
cp = self.rocket.surfaces_cp_to_cdm[surface]
23482348
vb_component = vb_body + (w ^ cp)
23492349

@@ -2609,7 +2609,7 @@ def u_dot_generalized(self, t, u, post_processing=False): # pylint: disable=too
26092609
# Get rocket velocity in body frame
26102610
velocity_in_body_frame = Kt @ v
26112611
# Calculate lift and moment for each component of the rocket
2612-
for aero_surface, _ in self.rocket.aerodynamic_surfaces:
2612+
for aero_surface, _, _ref_factor in self.rocket.aerodynamic_surfaces:
26132613
# Component cp relative to CDM in body frame
26142614
comp_cp = self.rocket.surfaces_cp_to_cdm[aero_surface]
26152615
# Component absolute velocity in body frame

‎rocketpy/stochastic/stochastic_rocket.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def __reset_components(self, components, seed):
211211
input component.
212212
"""
213213
new_components = Components()
214-
for stochastic_obj, _ in components:
214+
for stochastic_obj, _position, _ref_factor in components:
215215
stochastic_obj_position_info = self.__components_map[stochastic_obj]
216216
stochastic_obj._set_stochastic(seed)
217217
new_components.add(
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""Unit tests for rocketpy.rocket.components.Components."""
2+
3+
import pytest
4+
5+
from rocketpy.mathutils.vector_matrix import Vector
6+
from rocketpy.rocket.components import Components
7+
8+
9+
def test_components_add_default_ref_factor():
10+
"""Components.add stores ref_factor=1.0 when omitted."""
11+
components = Components()
12+
component = object()
13+
position = Vector([0, 0, 1.0])
14+
15+
components.add(component, position)
16+
17+
assert len(components) == 1
18+
assert components[0].component is component
19+
assert components[0].position == position
20+
assert components[0].ref_factor == pytest.approx(1.0)
21+
22+
23+
def test_components_add_custom_ref_factor():
24+
"""Components.add stores an explicit ref_factor on the component tuple."""
25+
components = Components()
26+
component = object()
27+
position = Vector([0, 0, -0.5])
28+
ref_factor = 0.25
29+
30+
components.add(component, position, ref_factor=ref_factor)
31+
32+
stored = components[0]
33+
assert stored.component is component
34+
assert stored.position == position
35+
assert stored.ref_factor == pytest.approx(ref_factor)
36+
37+
38+
def test_components_to_dict_from_dict_preserves_ref_factor():
39+
"""Serialization round-trip keeps ref_factor, defaulting when absent."""
40+
components = Components()
41+
component = {"name": "dummy"}
42+
position = Vector([0, 0, 0.1])
43+
components.add(component, position, ref_factor=4.0)
44+
45+
restored = Components.from_dict(components.to_dict())
46+
assert restored[0].ref_factor == pytest.approx(4.0)
47+
48+
legacy = Components.from_dict(
49+
{"components": [{"component": component, "position": position}]}
50+
)
51+
assert legacy[0].ref_factor == pytest.approx(1.0)
52+
53+
54+
def test_rocket_add_surfaces_stores_computed_ref_factor(calisto):
55+
"""Rocket aero-surface add path stores (surface.rocket_radius / rocket.radius)**2."""
56+
from rocketpy import NoseCone
57+
58+
surface_radius = calisto.radius / 2
59+
expected_ref_factor = (surface_radius / calisto.radius) ** 2
60+
nose = NoseCone(
61+
length=0.55829,
62+
kind="vonkarman",
63+
base_radius=surface_radius,
64+
rocket_radius=surface_radius,
65+
name="Half-radius nose",
66+
)
67+
68+
calisto.add_surfaces(nose, 1.16)
69+
stored = next(
70+
entry for entry in calisto.aerodynamic_surfaces if entry.component is nose
71+
)
72+
73+
assert stored.ref_factor == pytest.approx(expected_ref_factor)
74+
assert stored.ref_factor == pytest.approx(0.25)
75+
76+
77+
def test_rocket_add_surfaces_matching_radius_stores_unit_ref_factor(calisto):
78+
"""Matching surface and rocket radii store ref_factor of 1.0."""
79+
from rocketpy import NoseCone
80+
81+
nose = NoseCone(
82+
length=0.55829,
83+
kind="vonkarman",
84+
base_radius=calisto.radius,
85+
rocket_radius=calisto.radius,
86+
name="Matching nose",
87+
)
88+
89+
calisto.add_surfaces(nose, 1.16)
90+
stored = next(
91+
entry for entry in calisto.aerodynamic_surfaces if entry.component is nose
92+
)
93+
94+
assert stored.ref_factor == pytest.approx(1.0)

‎tests/unit/rocket/test_rocket.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,9 @@ def test_add_trapezoidal_fins_two_fins_warns_but_succeeds(calisto):
914914
fins = calisto.add_trapezoidal_fins(
915915
2, span=0.1, root_chord=0.12, tip_chord=0.04, position=-1.0
916916
)
917-
assert fins in [surface for surface, _ in calisto.aerodynamic_surfaces]
917+
assert fins in [
918+
surface for surface, _position, _ref_factor in calisto.aerodynamic_surfaces
919+
]
918920

919921

920922
def test_unstable_rocket_warning_raised(calisto):

0 commit comments

Comments
 (0)