Skip to content
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
2 changes: 2 additions & 0 deletions rocketpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from .exceptions import (
InvalidInertiaError,
InvalidParameterError,
StochasticTypeError,
StochasticValueError,
UnstableRocketWarning,
)
from .mathutils import (
Expand Down
18 changes: 18 additions & 0 deletions rocketpy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ class InvalidInertiaError(RocketPyError, ValueError):
"""Raised when the inertia tuple/list does not have the expected length."""


class StochasticTypeError(RocketPyError, TypeError, AssertionError):
"""Raised when a stochastic input has the wrong type or format (e.g. a
string where a number or tuple is expected).

Also an ``AssertionError``, which these checks used to raise, so existing
``except AssertionError`` clauses keep working.
"""


class StochasticValueError(RocketPyError, ValueError, AssertionError):
"""Raised when a stochastic input has the right type but an invalid value
(e.g. a tuple of the wrong length or an out-of-range ensemble member).

Also an ``AssertionError``, which these checks used to raise, so existing
``except AssertionError`` clauses keep working.
"""


class UnstableRocketWarning(UserWarning):
"""Issued when the rocket's static margin is negative at motor ignition,
indicating an aerodynamically unstable configuration.
Expand Down
27 changes: 14 additions & 13 deletions rocketpy/stochastic/stochastic_aero_surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np

from rocketpy.exceptions import StochasticTypeError, StochasticValueError
from rocketpy.rocket.aero_surface import (
AirBrakes,
EllipticalFins,
Expand Down Expand Up @@ -103,7 +104,7 @@ def _validate_kind(self, kind):
isinstance(kind, list)
and all(isinstance(member, str) for member in kind)
):
raise AssertionError("`kind` must be a list of strings")
raise StochasticTypeError("`kind` must be a list of strings")

def create_object(self):
"""Creates and returns a NoseCone object from the randomly generated
Expand Down Expand Up @@ -449,7 +450,7 @@ def _validate_shape_points(self, shape_points):

Raises
------
AssertionError
StochasticTypeError
If the input is not in a valid format.
"""
if shape_points is None or isinstance(
Expand All @@ -475,7 +476,7 @@ def _validate_shape_points(self, shape_points):
return [self._validate_outline(shape_points)]
return [self._validate_outline(outline) for outline in shape_points]

raise AssertionError(
raise StochasticTypeError(
"`shape_points` must be a tuple, list, numpy array, int, or float "
"or a custom sampler"
)
Expand All @@ -501,19 +502,19 @@ def _validate_shape_points_tuple(self, shape_points):

Raises
------
AssertionError
StochasticTypeError, StochasticValueError
If the input is not in a valid format.
"""
if len(shape_points) not in (2, 3):
raise AssertionError("'shape_points': tuple must have length 2 or 3")
raise StochasticValueError("'shape_points': tuple must have length 2 or 3")

if isinstance(shape_points[0], (int, float)):
# (standard deviation, distribution name), the nominal outline being
# taken from the object passed. A number in the second item would
# make the first one the nominal value, which for this argument is
# an outline rather than a number.
if not isinstance(shape_points[1], str):
raise AssertionError(
raise StochasticTypeError(
"'shape_points': when the first item of a tuple is a "
"standard deviation, the second must be a string naming a "
"valid numpy.random distribution function."
Expand All @@ -526,13 +527,13 @@ def _validate_shape_points_tuple(self, shape_points):
# accepts a string there and would read the outline as the deviation.
outline = self._validate_outline(shape_points[0])
if not isinstance(shape_points[1], (int, float)):
raise AssertionError(
raise StochasticTypeError(
"'shape_points': second item of tuple must be an int or float "
"standard deviation."
)
if len(shape_points) == 3:
if not isinstance(shape_points[2], str):
raise AssertionError(
raise StochasticTypeError(
"'shape_points': Third item of tuple must be a string containing "
"the name of a valid numpy.random distribution function."
)
Expand All @@ -552,12 +553,12 @@ def _validate_outline_distribution(cls, distribution_name):

Raises
------
AssertionError
StochasticValueError
If the distribution cannot be applied to an outline.
"""
if distribution_name not in cls._outline_distributions:
accepted = ", ".join(repr(name) for name in cls._outline_distributions)
raise AssertionError(
raise StochasticValueError(
f"'shape_points': the '{distribution_name}' distribution cannot "
f"be applied to an outline. Use one of {accepted}, which take "
"the nominal coordinate as the centre of the deviation."
Expand Down Expand Up @@ -590,17 +591,17 @@ def _validate_outline(outline):

Raises
------
AssertionError
StochasticValueError
If the outline is not a sequence of at least three (x, y) numbers.
"""
if not StochasticFreeFormFins._is_outline(outline):
raise AssertionError(
raise StochasticValueError(
"`shape_points` outlines must be sequences of (x, y) numbers, "
"i.e. have shape (n, 2)."
)
array = np.asarray(outline, dtype=float)
if array.shape[0] < 3:
raise AssertionError(
raise StochasticValueError(
"`shape_points` outlines must have at least 3 points to "
"enclose an area."
)
Expand Down
21 changes: 11 additions & 10 deletions rocketpy/stochastic/stochastic_environment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Defines the StochasticEnvironment class."""

from rocketpy.exceptions import StochasticTypeError, StochasticValueError

from .stochastic_model import StochasticModel


Expand Down Expand Up @@ -119,30 +121,29 @@ def _validate_ensemble(self, ensemble_member, environment):

Raises
------
AssertionError
StochasticTypeError, StochasticValueError
If the environment does not have ensemble members and the
ensemble_member input argument is not None.
"""
valid_atmospheric_types = ["Ensemble", "Reanalysis"]

if environment.atmospheric_model_type not in valid_atmospheric_types:
if ensemble_member is not None:
raise AssertionError(
raise StochasticValueError(
f"Environment with {environment.atmospheric_model_type} "
"does not have ensemble members"
)
return

if ensemble_member is not None:
if not isinstance(ensemble_member, list):
raise AssertionError("`ensemble_member` must be a list")
if not (
all(
isinstance(member, int) and member >= 0
for member in ensemble_member
raise StochasticTypeError("`ensemble_member` must be a list")
if not all(isinstance(member, int) for member in ensemble_member):
raise StochasticTypeError(
"`ensemble_member` must be a list of positive integers"
)
):
raise AssertionError(
if not all(member >= 0 for member in ensemble_member):
raise StochasticValueError(
"`ensemble_member` must be a list of positive integers"
)
if not (
Expand All @@ -151,7 +152,7 @@ def _validate_ensemble(self, ensemble_member, environment):
<= max(ensemble_member)
< environment.num_ensemble_members
):
raise AssertionError(
raise StochasticValueError(
"`ensemble_member` must be in the range from 0 to "
+ f"{environment.num_ensemble_members - 1}"
)
Expand Down
15 changes: 9 additions & 6 deletions rocketpy/stochastic/stochastic_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np

from rocketpy.exceptions import StochasticTypeError, StochasticValueError
from rocketpy.simulation import Flight

from .stochastic_model import StochasticModel
Expand Down Expand Up @@ -89,13 +90,13 @@ def __init__(
if terminate_on_apogee is not None and not isinstance(
terminate_on_apogee, bool
):
raise AssertionError("`terminate_on_apogee` must be a boolean")
raise StochasticTypeError("`terminate_on_apogee` must be a boolean")
if time_overshoot is not None:
if not isinstance(time_overshoot, bool):
raise TypeError("`time_overshoot` must be a boolean")
raise StochasticTypeError("`time_overshoot` must be a boolean")
if max_time is not None:
if not isinstance(max_time, (int, float)):
raise TypeError("`max_time` must be a number")
raise StochasticTypeError("`max_time` must be a number")
super().__init__(
flight,
rail_length=rail_length,
Expand Down Expand Up @@ -143,7 +144,7 @@ def _validate_initial_solution(self, initial_solution):
if initial_solution is not None:
if isinstance(initial_solution, (tuple, list)):
if not len(initial_solution) == 14:
raise AssertionError(
raise StochasticValueError(
"`initial_solution` must be a 14 element tuple, the "
"elements are:\n t_initial, x_init, y_init, z_init, "
"vx_init, vy_init, vz_init, e0_init, e1_init, e2_init, "
Expand All @@ -153,11 +154,13 @@ def _validate_initial_solution(self, initial_solution):
isinstance(i, Real) and not isinstance(i, (bool, np.bool_))
for i in initial_solution
):
raise AssertionError(
raise StochasticTypeError(
"`initial_solution` must be a tuple of numbers"
)
else:
raise TypeError("`initial_solution` must be a tuple of numbers")
raise StochasticTypeError(
"`initial_solution` must be a tuple of numbers"
)

def _sample_flight_inputs(self):
"""Sample rail_length, inclination, and heading in a single draw.
Expand Down
Loading
Loading