Skip to content

Commit

Permalink
TST: improve tests for the Monte Carlo analysis, tests are now passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Gui-FernandesBR committed Mar 5, 2024
1 parent 3dbb1f8 commit 4e00af7
Show file tree
Hide file tree
Showing 17 changed files with 609 additions and 80 deletions.
4 changes: 2 additions & 2 deletions rocketpy/stochastic/stochastic_rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def add_motor(self, motor, position=None):

# checks if input is a Motor
if not isinstance(motor, (Motor, StochasticMotorModel)):
raise AssertionError("`motor` must be a StochasticMotor or Motor type")
raise TypeError("`motor` must be a StochasticMotor or Motor type")
if isinstance(motor, Motor):
# create StochasticMotor
# TODO check motor type when hybrids and liquids are implemented
Expand Down Expand Up @@ -368,7 +368,7 @@ def add_parachute(self, parachute):
"""
# checks if input is a StochasticParachute type
if not isinstance(parachute, (Parachute, StochasticParachute)):
raise AssertionError(
raise TypeError(
"`parachute` must be of Parachute or StochasticParachute type"
)
if isinstance(parachute, Parachute):
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest


# Pytest configuration
pytest_plugins = [
"tests.fixtures.environment.environment_fixtures",
Expand All @@ -15,6 +14,9 @@
"tests.fixtures.rockets.rocket_fixtures",
"tests.fixtures.surfaces.surface_fixtures",
"tests.fixtures.units.numerical_fixtures",
"tests.fixtures.monte_carlo.monte_carlo_fixtures",
"tests.fixtures.monte_carlo.stochastic_fixtures",
"tests.fixtures.monte_carlo.stochastic_motors_fixtures",
]


Expand Down
Empty file.
13 changes: 13 additions & 0 deletions tests/fixtures/monte_carlo/monte_carlo_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from rocketpy.simulation import MonteCarlo


@pytest.fixture
def calisto_dispersion(stochastic_environment, stochastic_calisto, stochastic_flight):
return MonteCarlo(
filename="monte_carlo_test",
environment=stochastic_environment,
rocket=stochastic_calisto,
flight=stochastic_flight,
)
236 changes: 236 additions & 0 deletions tests/fixtures/monte_carlo/stochastic_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
"""This module contains fixtures for the stochastic module. The fixtures are
used to test the stochastic objects that will be used in the Monte Carlo
simulations. It is a team effort to keep it as documented as possible."""

import pytest

from rocketpy.stochastic import (
StochasticEnvironment,
StochasticFlight,
StochasticNoseCone,
StochasticParachute,
StochasticRailButtons,
StochasticRocket,
StochasticTail,
StochasticTrapezoidalFins,
)


@pytest.fixture
def stochastic_environment(example_env_robust):
"""This fixture is used to create a stochastic environment object for the
Calisto flight.
Parameters
----------
example_env_robust : Environment
This is another fixture.
Returns
-------
StochasticEnvironment
The stochastic environment object
"""
return StochasticEnvironment(
environment=example_env_robust,
elevation=(1400, 10, "normal"),
gravity=None,
latitude=None,
longitude=None,
ensemble_member=None,
wind_velocity_x_factor=(1.0, 0.033, "normal"),
wind_velocity_y_factor=(1.0, 0.033, "normal"),
)


@pytest.fixture
def stochastic_nose_cone(calisto_nose_cone):
"""This fixture is used to create a StochasticNoseCone object for the
Calisto rocket.
Parameters
----------
calisto_nose_cone : NoseCone
This is another fixture.
Returns
-------
StochasticNoseCone
The stochastic nose cone object
"""
return StochasticNoseCone(
nosecone=calisto_nose_cone,
length=0.001,
)


@pytest.fixture
def stochastic_trapezoidal_fins(calisto_trapezoidal_fins):
"""This fixture is used to create a StochasticTrapezoidalFins object for the
Calisto rocket.
Parameters
----------
calisto_trapezoidal_fins : TrapezoidalFins
This is another fixture.
Returns
-------
StochasticTrapezoidalFins
The stochastic trapezoidal fins object
"""
return StochasticTrapezoidalFins(
trapezoidal_fins=calisto_trapezoidal_fins,
root_chord=0.0005,
tip_chord=0.0005,
span=0.0005,
)


@pytest.fixture
def stochastic_tail(calisto_tail):
"""This fixture is used to create a StochasticTail object for the
Calisto rocket.
Parameters
----------
calisto_tail : Tail
This is another fixture.
Returns
-------
StochasticTail
The stochastic tail object
"""
return StochasticTail(
tail=calisto_tail,
top_radius=0.001,
bottom_radius=0.001,
length=0.001,
)


@pytest.fixture
def stochastic_rail_buttons(calisto_rail_buttons):
"""This fixture is used to create a StochasticRailButtons object for the
Calisto rocket.
Parameters
----------
calisto_rail_buttons : RailButtons
This is another fixture.
Returns
-------
StochasticRailButtons
The stochastic rail buttons object
"""
return StochasticRailButtons(
rail_buttons=calisto_rail_buttons, buttons_distance=0.001
)


@pytest.fixture
def stochastic_main_parachute(calisto_main_chute):
"""This fixture is used to create a StochasticParachute object for the
Calisto rocket.
Parameters
----------
calisto_main_chute : Parachute
This is another fixture.
Returns
-------
StochasticParachute
The stochastic parachute object
"""
return StochasticParachute(
parachute=calisto_main_chute,
cd_s=0.1,
lag=0.1,
)


@pytest.fixture
def stochastic_drogue_parachute(calisto_drogue_chute):
"""This fixture is used to create a StochasticParachute object for the
Calisto rocket. This time, the drogue parachute is created.
Parameters
----------
calisto_drogue_chute : Parachute
This is another fixture.
Returns
-------
StochasticParachute
The stochastic parachute object
"""
return StochasticParachute(
parachute=calisto_drogue_chute,
cd_s=0.07,
lag=0.2,
)


@pytest.fixture
def stochastic_calisto(
calisto_robust,
stochastic_nose_cone,
stochastic_trapezoidal_fins,
stochastic_tail,
stochastic_solid_motor,
stochastic_rail_buttons,
stochastic_main_parachute,
stochastic_drogue_parachute,
):
"""This fixture creates a StochasticRocket object for the Calisto rocket.
The fixture will already have the stochastic nose cone, trapezoidal fins,
tail, solid motor, rail buttons, main parachute, and drogue parachute.
Returns
-------
StochasticRocket
The stochastic rocket object
"""
rocket = StochasticRocket(
rocket=calisto_robust,
radius=0.0127 / 2000,
mass=(15.426, 0.5, "normal"),
inertia_11=(6.321, 0),
inertia_22=0.01,
inertia_33=0.01,
center_of_mass_without_motor=0,
)
rocket.add_motor(stochastic_solid_motor, position=0.001)
rocket.add_nose(stochastic_nose_cone, position=(1.134, 0.001))
rocket.add_trapezoidal_fins(stochastic_trapezoidal_fins, position=(0.001, "normal"))
rocket.add_tail(stochastic_tail)
rocket.set_rail_buttons(
stochastic_rail_buttons, lower_button_position=(-0.618, 0.001, "normal")
)
rocket.add_parachute(stochastic_main_parachute)
rocket.add_parachute(stochastic_drogue_parachute)
return rocket


@pytest.fixture
def stochastic_flight(flight_calisto_robust):
"""This fixture creates a StochasticFlight object for the Calisto flight.
Parameters
----------
flight_calisto_robust : Flight
This is another fixture.
Returns
-------
StochasticFlight
The stochastic flight object
"""
return StochasticFlight(
flight=flight_calisto_robust,
inclination=(84.7, 1),
heading=(53, 2),
)
78 changes: 78 additions & 0 deletions tests/fixtures/monte_carlo/stochastic_motors_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""This module contains fixtures for the stochastic motors tests."""

import pytest

from rocketpy.mathutils.function import Function
from rocketpy.stochastic import StochasticGenericMotor, StochasticSolidMotor


@pytest.fixture
def stochastic_solid_motor(cesaroni_m1670):
"""A Stochastic Solid Motor fixture for the Cesaroni M1670 motor.
Parameters
----------
cesaroni_m1670 : SolidMotor
This is another fixture.
Returns
-------
StochasticSolidMotor
The stochastic solid motor object.
"""
return StochasticSolidMotor(
solid_motor=cesaroni_m1670,
thrust_source=[
"data/motors/Cesaroni_M1670.eng",
[[0, 6000], [1, 6000], [2, 6000], [3, 6000], [4, 6000]],
Function([[0, 6000], [1, 6000], [2, 6000], [3, 6000], [4, 6000]]),
],
burn_out_time=(4, 0.1),
grains_center_of_mass_position=0.001,
grain_density=50,
grain_separation=1 / 1000,
grain_initial_height=1 / 1000,
grain_initial_inner_radius=0.375 / 1000,
grain_outer_radius=0.375 / 1000,
total_impulse=(6500, 1000),
throat_radius=0.5 / 1000,
nozzle_radius=0.5 / 1000,
nozzle_position=0.001,
)


@pytest.fixture
def stochastic_generic_motor(generic_motor):
"""A Stochastic Generic Motor fixture
Parameters
----------
generic_motor : GenericMotor
This is another fixture.
Returns
-------
StochasticGenericMotor
The stochastic generic motor object.
"""
return StochasticGenericMotor(
generic_motor,
thrust_source=None,
total_impulse=None,
burn_start_time=None,
burn_out_time=None,
propellant_initial_mass=None,
dry_mass=None,
dry_inertia_11=None,
dry_inertia_22=None,
dry_inertia_33=None,
dry_inertia_12=None,
dry_inertia_13=None,
dry_inertia_23=None,
chamber_radius=None,
chamber_height=(0.5, 0.005),
chamber_position=None,
nozzle_radius=None,
nozzle_position=None,
center_of_dry_mass_position=None,
)
Loading

0 comments on commit 4e00af7

Please sign in to comment.