Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: solid_motor.py testing refactors #494

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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)