Skip to content

Commit 90553f5

Browse files
ENH: Add Eccentricity to Stochastic Simulations (#792)
* wind factor bug corrected the wind factor wasn't applied to the env.wind_velocity properties * BUG: StochasticModel visualize attributes of a uniform distribution It showed the nominal and the standard deviation values and it doesn't make sense in a uniform distribution. In a np.random.uniform the 'nominal value' is the lower bound of the distribution, and the 'standard deviation' value is the upper bound. Now, a new condition has been added for the uniform distributions where the mean and semi range are calculated and showed. This way the visualize_attribute function will show the whole range where the random values are uniformly taken in * variable names corrections * Corrections requested by the pylint test * ENH: more intuitive uniform distribution display in StochasticModel Co-authored-by: MateusStano <[email protected]> * ENH: Eccentricities added to the StochasticRocket class A bug has been corrected in Flight class and an enhancement has been performed in the Rocket class as well * BUG: thrust eccentricity bug corrected eccentricity_y was defined by x coordinate and eccentricity_x was defined by y coordinate * BUG: Undo some Rocket class changes * ENH: add eccentricities to StochasticRocket * BUG: fix MonteCarlo eccentricity inputs * ENH: pylint and ruff recommended changes * TST: fix tests with eccentricity --------- Co-authored-by: Gui-FernandesBR <[email protected]>
1 parent 7348053 commit 90553f5

File tree

4 files changed

+144
-23
lines changed

4 files changed

+144
-23
lines changed

rocketpy/rocket/rocket.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,8 +1744,8 @@ def set_rail_buttons(
17441744
def add_cm_eccentricity(self, x, y):
17451745
"""Moves line of action of aerodynamic and thrust forces by
17461746
equal translation amount to simulate an eccentricity in the
1747-
position of the center of mass of the rocket relative to its
1748-
geometrical center line.
1747+
position of the center of dry mass of the rocket relative to
1748+
its geometrical center line.
17491749
17501750
Parameters
17511751
----------
@@ -1781,20 +1781,18 @@ def add_cm_eccentricity(self, x, y):
17811781
def add_cp_eccentricity(self, x, y):
17821782
"""Moves line of action of aerodynamic forces to simulate an
17831783
eccentricity in the position of the center of pressure relative
1784-
to the center of mass of the rocket.
1784+
to the center of dry mass of the rocket.
17851785
17861786
Parameters
17871787
----------
17881788
x : float
17891789
Distance in meters by which the CP is to be translated in
1790-
the x direction relative to the center of mass axial line.
1791-
The x axis is defined according to the body axes coordinate
1792-
system.
1790+
the x direction relative to the center of dry mass axial line.
1791+
The x axis is defined according to the body axes coordinate system.
17931792
y : float
17941793
Distance in meters by which the CP is to be translated in
1795-
the y direction relative to the center of mass axial line.
1796-
The y axis is defined according to the body axes coordinate
1797-
system.
1794+
the y direction relative to the center of dry mass axial line.
1795+
The y axis is defined according to the body axes coordinate system.
17981796
17991797
Returns
18001798
-------
@@ -1811,19 +1809,19 @@ def add_cp_eccentricity(self, x, y):
18111809

18121810
def add_thrust_eccentricity(self, x, y):
18131811
"""Moves line of action of thrust forces to simulate a
1814-
misalignment of the thrust vector and the center of mass.
1812+
misalignment of the thrust vector and the center of dry mass.
18151813
18161814
Parameters
18171815
----------
18181816
x : float
18191817
Distance in meters by which the line of action of the
18201818
thrust force is to be translated in the x direction
1821-
relative to the center of mass axial line. The x axis
1819+
relative to the center of dry mass axial line. The x axis
18221820
is defined according to the body axes coordinate system.
18231821
y : float
18241822
Distance in meters by which the line of action of the
1825-
thrust force is to be translated in the x direction
1826-
relative to the center of mass axial line. The y axis
1823+
thrust force is to be translated in the y direction
1824+
relative to the center of dry mass axial line. The y axis
18271825
is defined according to the body axes coordinate system.
18281826
18291827
Returns
@@ -1835,8 +1833,8 @@ def add_thrust_eccentricity(self, x, y):
18351833
--------
18361834
:ref:`rocket_axes`
18371835
"""
1838-
self.thrust_eccentricity_y = x
1839-
self.thrust_eccentricity_x = y
1836+
self.thrust_eccentricity_x = x
1837+
self.thrust_eccentricity_y = y
18401838
return self
18411839

18421840
def draw(self, vis_args=None, plane="xz", *, filename=None):

rocketpy/simulation/flight.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,8 @@ def u_dot(self, t, u, post_processing=False): # pylint: disable=too-many-locals
14691469
# Thrust
14701470
thrust = self.rocket.motor.thrust.get_value_opt(t)
14711471
# Off center moment
1472-
M1 += self.rocket.thrust_eccentricity_x * thrust
1473-
M2 -= self.rocket.thrust_eccentricity_y * thrust
1472+
M1 += self.rocket.thrust_eccentricity_y * thrust
1473+
M2 -= self.rocket.thrust_eccentricity_x * thrust
14741474
else:
14751475
# Motor stopped
14761476
# Inertias
@@ -1856,11 +1856,11 @@ def u_dot_generalized(self, t, u, post_processing=False): # pylint: disable=too
18561856
thrust = self.rocket.motor.thrust.get_value_opt(t)
18571857
M1 += (
18581858
self.rocket.cp_eccentricity_y * R3
1859-
+ self.rocket.thrust_eccentricity_x * thrust
1859+
+ self.rocket.thrust_eccentricity_y * thrust
18601860
)
18611861
M2 -= (
18621862
self.rocket.cp_eccentricity_x * R3
1863-
- self.rocket.thrust_eccentricity_y * thrust
1863+
+ self.rocket.thrust_eccentricity_x * thrust
18641864
)
18651865
M3 += self.rocket.cp_eccentricity_x * R2 - self.rocket.cp_eccentricity_y * R1
18661866

rocketpy/stochastic/stochastic_rocket.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,105 @@ def set_rail_buttons(
389389
rail_buttons, self._validate_position(rail_buttons, lower_button_position)
390390
)
391391

392+
def add_cp_eccentricity(self, x=None, y=None):
393+
"""Moves line of action of aerodynamic forces to simulate an
394+
eccentricity in the position of the center of pressure relative
395+
to the center of dry mass of the rocket.
396+
397+
Parameters
398+
----------
399+
x : tuple, list, int, float, optional
400+
Distance in meters by which the CP is to be translated in
401+
the x direction relative to the center of dry mass axial line.
402+
The x axis is defined according to the body axes coordinate system.
403+
y : tuple, list, int, float, optional
404+
Distance in meters by which the CP is to be translated in
405+
the y direction relative to the center of dry mass axial line.
406+
The y axis is defined according to the body axes coordinate system.
407+
408+
Returns
409+
-------
410+
self : StochasticRocket
411+
Object of the StochasticRocket class.
412+
"""
413+
self.cp_eccentricity_x = self._validate_eccentricity("cp_eccentricity_x", x)
414+
self.cp_eccentricity_y = self._validate_eccentricity("cp_eccentricity_y", y)
415+
return self
416+
417+
def add_thrust_eccentricity(self, x=None, y=None):
418+
"""Moves line of action of thrust forces to simulate a
419+
misalignment of the thrust vector and the center of dry mass.
420+
421+
Parameters
422+
----------
423+
x : tuple, list, int, float, optional
424+
Distance in meters by which the line of action of the
425+
thrust force is to be translated in the x direction
426+
relative to the center of dry mass axial line. The x axis
427+
is defined according to the body axes coordinate system.
428+
y : tuple, list, int, float, optional
429+
Distance in meters by which the line of action of the
430+
thrust force is to be translated in the y direction
431+
relative to the center of dry mass axial line. The y axis
432+
is defined according to the body axes coordinate system.
433+
434+
Returns
435+
-------
436+
self : StochasticRocket
437+
Object of the StochasticRocket class.
438+
"""
439+
self.thrust_eccentricity_x = self._validate_eccentricity(
440+
"thrust_eccentricity_x", x
441+
)
442+
self.thrust_eccentricity_y = self._validate_eccentricity(
443+
"thrust_eccentricity_y", y
444+
)
445+
return self
446+
447+
def _validate_eccentricity(self, eccentricity, position):
448+
"""Validate the eccentricity argument.
449+
450+
Parameters
451+
----------
452+
eccentricity : str
453+
The eccentricity to which the position argument refers to.
454+
position : tuple, list, int, float
455+
The position argument to be validated.
456+
457+
Returns
458+
-------
459+
tuple or list
460+
Validated position argument.
461+
462+
Raises
463+
------
464+
ValueError
465+
If the position argument does not conform to the specified formats.
466+
"""
467+
if isinstance(position, tuple):
468+
return self._validate_tuple(
469+
eccentricity,
470+
position,
471+
)
472+
elif isinstance(position, (int, float)):
473+
return self._validate_scalar(
474+
eccentricity,
475+
position,
476+
)
477+
elif isinstance(position, list):
478+
return self._validate_list(
479+
eccentricity,
480+
position,
481+
)
482+
elif position is None:
483+
position = []
484+
return self._validate_list(
485+
eccentricity,
486+
position,
487+
)
488+
else:
489+
raise AssertionError("`position` must be a tuple, list, int, or float")
490+
392491
def _validate_position(self, validated_object, position):
393492
"""Validate the position argument.
394493
@@ -578,6 +677,13 @@ def _create_parachute(self, stochastic_parachute):
578677
self.last_rnd_dict["parachutes"].append(stochastic_parachute.last_rnd_dict)
579678
return parachute
580679

680+
def _create_eccentricities(self, stochastic_x, stochastic_y, eccentricity):
681+
x_rnd = self._randomize_position(stochastic_x)
682+
self.last_rnd_dict[eccentricity + "_x"] = x_rnd
683+
y_rnd = self._randomize_position(stochastic_y)
684+
self.last_rnd_dict[eccentricity + "_y"] = y_rnd
685+
return x_rnd, y_rnd
686+
581687
def create_object(self):
582688
"""Creates and returns a Rocket object from the randomly generated input
583689
arguments.
@@ -609,6 +715,23 @@ def create_object(self):
609715
rocket.power_off_drag *= generated_dict["power_off_drag_factor"]
610716
rocket.power_on_drag *= generated_dict["power_on_drag_factor"]
611717

718+
if hasattr(self, "cp_eccentricity_x") and hasattr(self, "cp_eccentricity_y"):
719+
cp_ecc_x, cp_ecc_y = self._create_eccentricities(
720+
self.cp_eccentricity_x,
721+
self.cp_eccentricity_y,
722+
"cp_eccentricity",
723+
)
724+
rocket.add_cp_eccentricity(cp_ecc_x, cp_ecc_y)
725+
if hasattr(self, "thrust_eccentricity_x") and hasattr(
726+
self, "thrust_eccentricity_y"
727+
):
728+
thrust_ecc_x, thrust_ecc_y = self._create_eccentricities(
729+
self.thrust_eccentricity_x,
730+
self.thrust_eccentricity_y,
731+
"thrust_eccentricity",
732+
)
733+
rocket.add_thrust_eccentricity(thrust_ecc_x, thrust_ecc_y)
734+
612735
for component_motor in self.motors:
613736
motor, position_rnd = self._create_motor(component_motor)
614737
rocket.add_motor(motor, position_rnd)

tests/unit/test_rocket.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,15 @@ def test_add_cm_eccentricity_assert_properties_set(calisto):
331331
assert calisto.cp_eccentricity_x == -4
332332
assert calisto.cp_eccentricity_y == -5
333333

334-
assert calisto.thrust_eccentricity_y == -4
335-
assert calisto.thrust_eccentricity_x == -5
334+
assert calisto.thrust_eccentricity_x == -4
335+
assert calisto.thrust_eccentricity_y == -5
336336

337337

338338
def test_add_thrust_eccentricity_assert_properties_set(calisto):
339339
calisto.add_thrust_eccentricity(x=4, y=5)
340340

341-
assert calisto.thrust_eccentricity_y == 4
342-
assert calisto.thrust_eccentricity_x == 5
341+
assert calisto.thrust_eccentricity_x == 4
342+
assert calisto.thrust_eccentricity_y == 5
343343

344344

345345
def test_add_cp_eccentricity_assert_properties_set(calisto):

0 commit comments

Comments
 (0)