diff --git a/tests/fixtures/hybrid/__init__.py b/tests/fixtures/hybrid/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/fixtures/hybrid/hybrid_fixtures.py b/tests/fixtures/hybrid/hybrid_fixtures.py deleted file mode 100644 index e19131e9b..000000000 --- a/tests/fixtures/hybrid/hybrid_fixtures.py +++ /dev/null @@ -1,275 +0,0 @@ -import numpy as np -import pytest - -from rocketpy import ( - CylindricalTank, - Fluid, - Function, - HybridMotor, - LevelBasedTank, - LiquidMotor, - MassBasedTank, - SphericalTank, - UllageBasedTank, -) - - -@pytest.fixture -def pressurant_fluid(): - """An example of a pressurant fluid as N2 gas at - 273.15K and 30MPa. - - Returns - ------- - rocketpy.Fluid - An object of the Fluid class. - """ - return Fluid(name="N2", density=300) - - -@pytest.fixture -def fuel_pressurant(): - """An example of a pressurant fluid as N2 gas at - 273.15K and 2MPa. - - Returns - ------- - rocketpy.Fluid - An object of the Fluid class. - """ - return Fluid(name="N2", density=25) - - -@pytest.fixture -def oxidizer_pressurant(): - """An example of a pressurant fluid as N2 gas at - 273.15K and 3MPa. - - Returns - ------- - rocketpy.Fluid - An object of the Fluid class. - """ - return Fluid(name="N2", density=35) - - -@pytest.fixture -def fuel_fluid(): - """An example of propane as fuel fluid at - 273.15K and 2MPa. - - Returns - ------- - rocketpy.Fluid - An object of the Fluid class. - """ - return Fluid(name="Propane", density=500) - - -@pytest.fixture -def oxidizer_fluid(): - """An example of liquid oxygen as oxidizer fluid at - 100K and 3MPa. - - Returns - ------- - rocketpy.Fluid - An object of the Fluid class. - """ - return Fluid(name="O2", density=1000) - - -@pytest.fixture -def pressurant_tank(pressurant_fluid): - """An example of a pressurant cylindrical tank with spherical - caps. - - Parameters - ---------- - pressurant_fluid : rocketpy.Fluid - Pressurizing fluid. This is a pytest fixture. - - Returns - ------- - rocketpy.MassBasedTank - An object of the CylindricalTank class. - """ - geometry = CylindricalTank(0.135 / 2, 0.981, spherical_caps=True) - pressurant_tank = MassBasedTank( - name="Pressure Tank", - geometry=geometry, - liquid_mass=0, - flux_time=(8, 20), - gas_mass="data/rockets/berkeley/pressurantMassFiltered.csv", - gas=pressurant_fluid, - liquid=pressurant_fluid, - ) - - return pressurant_tank - - -@pytest.fixture -def fuel_tank(fuel_fluid, fuel_pressurant): - """An example of a fuel cylindrical tank with spherical - caps. - - Parameters - ---------- - fuel_fluid : rocketpy.Fluid - Fuel fluid of the tank. This is a pytest fixture. - fuel_pressurant : rocketpy.Fluid - Pressurizing fluid of the fuel tank. This is a pytest - fixture. - - Returns - ------- - rocketpy.UllageBasedTank - """ - geometry = CylindricalTank(0.0744, 0.8068, spherical_caps=True) - ullage = ( - -Function("data/rockets/berkeley/test124_Propane_Volume.csv") * 1e-3 - + geometry.total_volume - ) - fuel_tank = UllageBasedTank( - name="Propane Tank", - flux_time=(8, 20), - geometry=geometry, - liquid=fuel_fluid, - gas=fuel_pressurant, - ullage=ullage, - ) - - return fuel_tank - - -@pytest.fixture -def oxidizer_tank(oxidizer_fluid, oxidizer_pressurant): - """An example of a oxidizer cylindrical tank with spherical - caps. - - Parameters - ---------- - oxidizer_fluid : rocketpy.Fluid - Oxidizer fluid of the tank. This is a pytest fixture. - oxidizer_pressurant : rocketpy.Fluid - Pressurizing fluid of the oxidizer tank. This is a pytest - fixture. - - Returns - ------- - rocketpy.UllageBasedTank - """ - geometry = CylindricalTank(0.0744, 0.8068, spherical_caps=True) - ullage = ( - -Function("data/rockets/berkeley/test124_Lox_Volume.csv") * 1e-3 - + geometry.total_volume - ) - oxidizer_tank = UllageBasedTank( - name="Lox Tank", - flux_time=(8, 20), - geometry=geometry, - liquid=oxidizer_fluid, - gas=oxidizer_pressurant, - ullage=ullage, - ) - - return oxidizer_tank - - -@pytest.fixture -def liquid_motor(pressurant_tank, fuel_tank, oxidizer_tank): - """An example of a liquid motor with pressurant, fuel and oxidizer tanks. - - Parameters - ---------- - pressurant_tank : rocketpy.MassBasedTank - Tank that contains pressurizing fluid. This is a pytest fixture. - fuel_tank : rocketpy.UllageBasedTank - Tank that contains the motor fuel. This is a pytest fixture. - oxidizer_tank : rocketpy.UllageBasedTank - Tank that contains the motor oxidizer. This is a pytest fixture. - - Returns - ------- - rocketpy.LiquidMotor - """ - liquid_motor = LiquidMotor( - thrust_source="data/rockets/berkeley/test124_Thrust_Curve.csv", - burn_time=(8, 20), - dry_mass=10, - dry_inertia=(5, 5, 0.2), - center_of_dry_mass_position=0, - nozzle_position=-1.364, - nozzle_radius=0.069 / 2, - ) - liquid_motor.add_tank(pressurant_tank, position=2.007) - liquid_motor.add_tank(fuel_tank, position=-1.048) - liquid_motor.add_tank(oxidizer_tank, position=0.711) - - return liquid_motor - - -@pytest.fixture -def spherical_oxidizer_tank(oxidizer_fluid, oxidizer_pressurant): - """An example of a oxidizer spherical tank. - - Parameters - ---------- - oxidizer_fluid : rocketpy.Fluid - Oxidizer fluid of the tank. This is a pytest fixture. - oxidizer_pressurant : rocketpy.Fluid - Pressurizing fluid of the oxidizer tank. This is a pytest - fixture. - - Returns - ------- - rocketpy.UllageBasedTank - """ - geometry = SphericalTank(0.051) - oxidizer_tank = LevelBasedTank( - name="Lox Tank", - flux_time=10, - geometry=geometry, - liquid=oxidizer_fluid, - gas=oxidizer_pressurant, - liquid_height=lambda t: 0.1 * np.exp(-t / 2) - 0.05, - ) - - return oxidizer_tank - - -@pytest.fixture -def hybrid_motor(spherical_oxidizer_tank): - """An example of a hybrid motor with spherical oxidizer - tank and fuel grains. - - Parameters - ---------- - spherical_oxidizer_tank : rocketpy.LevelBasedTank - Example Tank that contains the motor oxidizer. This is a - pytest fixture. - - Returns - ------- - rocketpy.HybridMotor - """ - motor = HybridMotor( - thrust_source=lambda t: 2000 - 100 * t, - burn_time=10, - center_of_dry_mass_position=0, - dry_inertia=(4, 4, 0.1), - dry_mass=8, - grain_density=1700, - grain_number=4, - grain_initial_height=0.1, - grain_separation=0, - grain_initial_inner_radius=0.04, - grain_outer_radius=0.1, - nozzle_position=-0.4, - nozzle_radius=0.07, - grains_center_of_mass_position=-0.1, - ) - - motor.add_tank(spherical_oxidizer_tank, position=0.3) - - return motor diff --git a/tests/integration/test_encoding.py b/tests/integration/test_encoding.py index ebe305b61..996fc533e 100644 --- a/tests/integration/test_encoding.py +++ b/tests/integration/test_encoding.py @@ -119,6 +119,42 @@ def test_environment_encoder(environment_name, request): ) +@pytest.mark.parametrize( + "motor_name", ["cesaroni_m1670", "liquid_motor", "hybrid_motor", "generic_motor"] +) +def test_motor_encoder(motor_name, request): + """Test encoding a ``rocketpy.Motor``. + + Parameters + ---------- + motor_name : str + Name of the motor fixture to encode. + request : pytest.FixtureRequest + Pytest request object. + """ + motor_to_encode = request.getfixturevalue(motor_name) + + json_encoded = json.dumps(motor_to_encode, cls=RocketPyEncoder) + + motor_loaded = json.loads(json_encoded, cls=RocketPyDecoder) + + sample_times = np.linspace(*motor_to_encode.burn_time, 100) + + assert np.allclose( + motor_to_encode.thrust(sample_times), motor_loaded.thrust(sample_times) + ) + assert np.allclose( + motor_to_encode.total_mass(sample_times), motor_loaded.total_mass(sample_times) + ) + assert np.allclose( + motor_to_encode.center_of_mass(sample_times), + motor_loaded.center_of_mass(sample_times), + ) + assert np.allclose( + motor_to_encode.I_11(sample_times), motor_loaded.I_11(sample_times) + ) + + @pytest.mark.parametrize( "rocket_name", ["calisto_robust", "calisto_liquid_modded", "calisto_hybrid_modded"] ) @@ -138,7 +174,7 @@ def test_rocket_encoder(rocket_name, request): rocket_loaded = json.loads(json_encoded, cls=RocketPyDecoder) - sample_times = np.linspace(0, 3.9, 100) + sample_times = np.linspace(*rocket_to_encode.motor.burn_time, 100) assert np.allclose( rocket_to_encode.evaluate_total_mass()(sample_times),