Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 7722736
Merge: feb3299 62c8bb6
Author: MateusStano <[email protected]>
Date:   Sat Dec 2 11:54:10 2023 -0300

    Merge pull request #494 from RocketPy-Team/tst/testing-refactor

    TST: solid_motor.py testing refactors

commit feb3299
Merge: a9ac31c b27edc3
Author: MateusStano <[email protected]>
Date:   Sat Dec 2 11:46:54 2023 -0300

    Merge pull request #477 from RocketPy-Team/enh/get-attributes

    ENH: Get Instance Attributes

commit b27edc3
Author: MateusStano <[email protected]>
Date:   Wed Nov 29 19:01:32 2023 +0100

    TST: add test

commit dcdd6bd
Merge: 438f4c0 191ab9f
Author: MateusStano <[email protected]>
Date:   Wed Nov 29 18:04:31 2023 +0100

    Merge remote-tracking branch 'origin/develop' into enh/get-attributes

commit 438f4c0
Author: MateusStano <[email protected]>
Date:   Wed Nov 29 17:44:19 2023 +0100

    MNT: improve warnings

commit 62c8bb6
Author: Lint Action <[email protected]>
Date:   Wed Nov 29 03:39:29 2023 +0000

    Fix code style issues with Black

commit 0180755
Author: Lucas <[email protected]>
Date:   Wed Nov 29 00:16:45 2023 -0300

    TST: Simple refactoring

    While studying some of the tests that were written, I made some simple changes to conftest.py, test_function.py and test_solidmotor.py.

commit 04b751d
Author: MateusStano <[email protected]>
Date:   Fri Nov 17 19:08:21 2023 +0100

    BUG: fix time_array assignment in Environment class

commit 1dd5e1e
Author: MateusStano <[email protected]>
Date:   Fri Nov 17 19:08:09 2023 +0100

    ENH refactor get_instance_attributes function to
    filter out methods and protected attributes

commit 931f330
Author: MateusStano <[email protected]>
Date:   Fri Nov 17 16:16:09 2023 +0100

    MNT: add deprecation warnings

commit 2368cab
Author: MateusStano <[email protected]>
Date:   Fri Nov 17 16:15:56 2023 +0100

    ENH: add get attributes function
  • Loading branch information
MateusStano committed Dec 2, 2023
1 parent 0c7b1d9 commit 8f56eda
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 44 deletions.
27 changes: 25 additions & 2 deletions rocketpy/environment/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2365,7 +2365,7 @@ def process_forecast_reanalysis(self, file, dictionary):
self.wind_vs = wind_vs
self.levels = levels
self.temperatures = temperatures
self.time_array = time_array
self.time_array = time_array[:].tolist()
self.height = height

# Close weather data
Expand Down Expand Up @@ -2735,7 +2735,7 @@ def process_ensemble(self, file, dictionary):
self.wind_vs = wind_vs
self.levels = levels
self.temperatures = temperatures
self.time_array = time_array
self.time_array = time_array[:].tolist()
self.height = height

# Close weather data
Expand Down Expand Up @@ -3126,7 +3126,19 @@ def all_plot_info_returned(self):
------
plot_info : Dict
Dict of data relevant to plot externally
Warning
-------
Deprecated in favor of `utilities.get_instance_attributes`.
"""
warnings.warn(
"The method 'all_plot_info_returned' is deprecated as of version "
+ "1.2 and will be removed in version 1.4 "
+ "Use 'utilities.get_instance_attributes' instead.",
DeprecationWarning,
)

grid = np.linspace(self.elevation, self.max_expected_height)
plot_info = dict(
grid=[i for i in grid],
Expand Down Expand Up @@ -3187,7 +3199,18 @@ def all_info_returned(self):
------
info : Dict
Information relevant about the Environment class.
Warning
-------
Deprecated in favor of `utilities.get_instance_attributes`.
"""
warnings.warn(
"The method 'all_info_returned' is deprecated as of version "
+ "1.2 and will be removed in version 1.4 "
+ "Use 'utilities.get_instance_attributes' instead.",
DeprecationWarning,
)

# Dictionary creation, if not commented follows the SI
info = dict(
Expand Down
23 changes: 23 additions & 0 deletions rocketpy/utilities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import traceback
import warnings

Expand Down Expand Up @@ -654,3 +655,25 @@ def liftoff_speed(mass):
if plot:
retfunc.plot(min_mass, max_mass, points)
return retfunc


def get_instance_attributes(instance):
"""Returns a dictionary with all attributes of a given instance.
Parameters
----------
instance : object
Instance of a class.
Returns
-------
dictionary
Dictionary with all attributes of the given instance.
"""
attributes_dict = dict()
members = inspect.getmembers(instance)
for member in members:
# Filter out methods and protected attributes
if not inspect.ismethod(member[1]) and not member[0].startswith("__"):
attributes_dict[member[0]] = member[1]
return attributes_dict
35 changes: 35 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,41 @@ def cesaroni_m1670(): # old name: solid_motor
return example_motor


@pytest.fixture
def cesaroni_m1670_shifted(): # old name: solid_motor
"""Create a simple object of the SolidMotor class to be used in the tests.
This is the same motor that has been used in the getting started guide for
years. The difference relies in the thrust_source, which was shifted for
testing purposes.
Returns
-------
rocketpy.SolidMotor
A simple object of the SolidMotor class
"""
example_motor = SolidMotor(
thrust_source="tests/fixtures/motor/Cesaroni_M1670_shifted.eng",
burn_time=3.9,
dry_mass=1.815,
dry_inertia=(0.125, 0.125, 0.002),
center_of_dry_mass_position=0.317,
nozzle_position=0,
grain_number=5,
grain_density=1815,
nozzle_radius=33 / 1000,
throat_radius=11 / 1000,
grain_separation=5 / 1000,
grain_outer_radius=33 / 1000,
grain_initial_height=120 / 1000,
grains_center_of_mass_position=0.397,
grain_initial_inner_radius=15 / 1000,
interpolation_method="linear",
coordinate_system_orientation="nozzle_to_combustion_chamber",
reshape_thrust_curve=(5, 3000),
)
return example_motor


@pytest.fixture
def calisto_motorless():
"""Create a simple object of the Rocket class to be used in the tests. This
Expand Down
36 changes: 22 additions & 14 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,29 @@ def test_integral_spline_interpolation(request, func, a, b):
)


def test_differentiate():
"""Tests the differentiation method of the Function class.
Both with respect to return instances and expected behaviour.
"""
func = Function(1)
assert isinstance(func.differentiate(0), float)
assert np.isclose(func.differentiate(5), 0)

func_x = Function(lambda x: x)
assert isinstance(func_x.differentiate(0), float)
assert np.isclose(func_x.differentiate(0), 1)
@pytest.mark.parametrize(
"func_input, derivative_input, expected_derivative",
[
(1, 0, 0), # Test case 1: Function(1)
(lambda x: x, 0, 1), # Test case 2: Function(lambda x: x)
(lambda x: x**2, 1, 2), # Test case 3: Function(lambda x: x**2)
],
)
def test_differentiate(func_input, derivative_input, expected_derivative):
"""Test the differentiate method of the Function class.
f_square = Function(lambda x: x**2)
assert isinstance(f_square.differentiate(1), float)
assert np.isclose(f_square.differentiate(1), 2)
Parameters
----------
func_input : function
A function object created from a list of values.
derivative_input : int
Point at which to differentiate.
expected_derivative : float
Expected value of the derivative.
"""
func = Function(func_input)
assert isinstance(func.differentiate(derivative_input), float)
assert np.isclose(func.differentiate(derivative_input), expected_derivative)


def test_get_value():
Expand Down
71 changes: 43 additions & 28 deletions tests/test_solidmotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import pytest

from rocketpy import SolidMotor
from rocketpy import Function, SolidMotor

burn_time = 3.9
grain_number = 5
Expand All @@ -15,6 +15,8 @@
grain_initial_height = 120 / 1000
nozzle_radius = 33 / 1000
throat_radius = 11 / 1000
grain_vol = 0.12 * (np.pi * (0.033**2 - 0.015**2))
grain_mass = grain_vol * 1815 * 5


@patch("matplotlib.pyplot.show")
Expand Down Expand Up @@ -170,6 +172,14 @@ def test_evaluate_inertia_33_asserts_extreme_values(cesaroni_m1670):


def tests_import_eng_asserts_read_values_correctly(cesaroni_m1670):
"""Tests the import_eng method. It checks whether the import operation
extracts the values correctly.
Parameters
----------
cesaroni_m1670_shifted : rocketpy.SolidMotor
The SolidMotor object to be used in the tests.
"""
comments, description, data_points = cesaroni_m1670.import_eng(
"tests/fixtures/motor/Cesaroni_M1670.eng"
)
Expand Down Expand Up @@ -197,8 +207,14 @@ def tests_import_eng_asserts_read_values_correctly(cesaroni_m1670):


def tests_export_eng_asserts_exported_values_correct(cesaroni_m1670):
grain_vol = 0.12 * (np.pi * (0.033**2 - 0.015**2))
grain_mass = grain_vol * 1815 * 5
"""Tests the export_eng method. It checks whether the exported values
of the thrust curve still match data_points.
Parameters
----------
cesaroni_m1670_shifted : rocketpy.SolidMotor
The SolidMotor object to be used in the tests.
"""

cesaroni_m1670.export_eng(
file_name="tests/cesaroni_m1670.eng", motor_name="test_motor"
Expand Down Expand Up @@ -239,31 +255,30 @@ def tests_export_eng_asserts_exported_values_correct(cesaroni_m1670):
]


def test_reshape_thrust_curve_asserts_resultant_thrust_curve_correct():
example_motor = SolidMotor(
thrust_source="tests/fixtures/motor/Cesaroni_M1670_shifted.eng",
burn_time=burn_time,
dry_mass=1.815,
dry_inertia=(0.125, 0.125, 0.002),
center_of_dry_mass_position=0.317,
nozzle_position=0,
grain_number=grain_number,
grain_density=grain_density,
nozzle_radius=nozzle_radius,
throat_radius=throat_radius,
grain_separation=grain_separation,
grain_outer_radius=grain_outer_radius,
grain_initial_height=grain_initial_height,
grains_center_of_mass_position=0.397,
grain_initial_inner_radius=grain_initial_inner_radius,
interpolation_method="linear",
coordinate_system_orientation="nozzle_to_combustion_chamber",
reshape_thrust_curve=(5, 3000),
@pytest.mark.parametrize("tuple_parametric", [(5, 3000)])
def test_reshape_thrust_curve_asserts_resultant_thrust_curve_correct(
cesaroni_m1670_shifted, tuple_parametric, linear_func
):
"""Tests the reshape_thrust_curve. It checks whether the resultant
thrust curve is correct when the user passes a certain tuple to the
reshape_thrust_curve attribute. Also checking for the correct return
data type.
Parameters
----------
cesaroni_m1670_shifted : rocketpy.SolidMotor
The SolidMotor object to be used in the tests.
tuple_parametric : tuple
Tuple passed to the reshape_thrust_curve method.
"""

assert isinstance(
cesaroni_m1670_shifted.reshape_thrust_curve(linear_func, 1, 3000), Function
)
thrust_reshaped = cesaroni_m1670_shifted.thrust.get_source()

thrust_reshaped = example_motor.thrust.get_source()
assert thrust_reshaped[1][0] == 0.155 * (5 / 4)
assert thrust_reshaped[-1][0] == 5
assert thrust_reshaped[1][0] == 0.155 * (tuple_parametric[0] / 4)
assert thrust_reshaped[-1][0] == tuple_parametric[0]

assert thrust_reshaped[1][1] == 100 * (3000 / 7539.1875)
assert thrust_reshaped[7][1] == 2034 * (3000 / 7539.1875)
assert thrust_reshaped[1][1] == 100 * (tuple_parametric[1] / 7539.1875)
assert thrust_reshaped[7][1] == 2034 * (tuple_parametric[1] / 7539.1875)
13 changes: 13 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,16 @@ def test_fin_flutter_analysis(mock_show, flight):
)
== None
)


def test_get_instance_attributes(flight_calisto_robust):
"""Tests if get_instance_attributes returns the expected results for a
robust flight object."""

attributes = utilities.get_instance_attributes(flight_calisto_robust)
for key, value in attributes.items():
attr = getattr(flight_calisto_robust, key)
if isinstance(attr, np.ndarray):
assert np.allclose(attr, value)
else:
assert attr == value

0 comments on commit 8f56eda

Please sign in to comment.