|
1 | | -import pytest |
2 | 1 | import numpy as np |
3 | | -from rocketpy.simulation.flight import Flight |
4 | | -from rocketpy.rocket.rocket import PointMassRocket |
| 2 | +import pytest |
| 3 | + |
5 | 4 | from rocketpy.motors.point_mass_motor import PointMassMotor |
| 5 | +from rocketpy.rocket.point_mass_rocket import PointMassRocket |
| 6 | +from rocketpy.simulation.flight import Flight |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def point_mass_motor(): |
| 11 | + """Simple PointMassMotor for 3-DOF tests. |
| 12 | +
|
| 13 | + Returns |
| 14 | + ------- |
| 15 | + rocketpy.PointMassMotor |
| 16 | + """ |
| 17 | + return PointMassMotor( |
| 18 | + thrust_source=10, |
| 19 | + dry_mass=1.0, |
| 20 | + propellant_initial_mass=0.5, |
| 21 | + burn_time=2.2, |
| 22 | + ) |
6 | 23 |
|
7 | | -class DummyEnv: |
8 | | - # Minimal stub, adapt with real Environment in your RocketPy setup |
9 | | - windvelocityx = windvelocityy = speedofsound = pressure = density = dynamicviscosity = lambda self: 0 |
10 | | - gravity = lambda self: 9.81 |
11 | | - elevation = 0 |
12 | | - def __getattr__(self, name): |
13 | | - return lambda *a, **k: 0 |
14 | | - |
15 | | -def make_simple_3dof_components(): |
16 | | - env = DummyEnv() |
17 | | - motor = PointMassMotor(10, dry_mass=1.0, propellant_initial_mass=0.5, burn_time=2.2) |
18 | | - rocket = PointMassRocket(0.05, 2.0, 0.1, 0.5, 0.6) |
19 | | - rocket.addmotor(motor, 0) |
20 | | - return env, rocket |
21 | | - |
22 | | -def test_3dof_simulation_mode_autoset(): |
23 | | - env, rocket = make_simple_3dof_components() |
24 | | - flight = Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="3 DOF") |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def point_mass_rocket(point_mass_motor): |
| 27 | + """Simple PointMassRocket for 3-DOF tests. |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + rocketpy.PointMassRocket |
| 32 | + """ |
| 33 | + rocket = PointMassRocket( |
| 34 | + radius=0.05, |
| 35 | + mass=2.0, |
| 36 | + center_of_mass_without_motor=0.1, |
| 37 | + power_off_drag=0.5, |
| 38 | + power_on_drag=0.6, |
| 39 | + ) |
| 40 | + rocket.add_motor(point_mass_motor, position=0) |
| 41 | + return rocket |
| 42 | + |
| 43 | + |
| 44 | +def test_3dof_simulation_mode_autoset(example_plain_env, point_mass_rocket): |
| 45 | + """Test that simulation mode is correctly set to 3 DOF.""" |
| 46 | + flight = Flight( |
| 47 | + rocket=point_mass_rocket, |
| 48 | + environment=example_plain_env, |
| 49 | + rail_length=1, |
| 50 | + simulation_mode="3 DOF", |
| 51 | + ) |
25 | 52 | assert flight.simulation_mode == "3 DOF" |
26 | 53 |
|
27 | | -def test_3dof_simulation_mode_warning(monkeypatch): |
28 | | - env, rocket = make_simple_3dof_components() |
| 54 | + |
| 55 | +def test_3dof_simulation_mode_warning( |
| 56 | + monkeypatch, example_plain_env, point_mass_rocket |
| 57 | +): |
| 58 | + """Test that a warning is issued when 6 DOF is requested with PointMassRocket.""" |
29 | 59 | monkeypatch.setattr("warnings.warn", lambda *a, **k: None) |
30 | | - f = Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="6 DOF") |
31 | | - assert f.simulation_mode == "3 DOF" |
| 60 | + flight = Flight( |
| 61 | + rocket=point_mass_rocket, |
| 62 | + environment=example_plain_env, |
| 63 | + rail_length=1, |
| 64 | + simulation_mode="6 DOF", |
| 65 | + ) |
| 66 | + assert flight.simulation_mode == "3 DOF" |
32 | 67 |
|
33 | | -def test_3dof_equations_of_motion_functions(): |
34 | | - env, rocket = make_simple_3dof_components() |
35 | | - flight = Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="3 DOF") |
36 | | - u = [0]*13 # Proper size for generalized state for 3/6 DOF probably |
| 68 | + |
| 69 | +def test_3dof_equations_of_motion_functions(example_plain_env, point_mass_rocket): |
| 70 | + """Test that 3-DOF equations of motion return valid results.""" |
| 71 | + flight = Flight( |
| 72 | + rocket=point_mass_rocket, |
| 73 | + environment=example_plain_env, |
| 74 | + rail_length=1, |
| 75 | + simulation_mode="3 DOF", |
| 76 | + ) |
| 77 | + u = [0] * 13 # Generalized state vector size |
37 | 78 | result = flight.udotgeneralized3dof(0, u) |
38 | | - assert isinstance(result, list) or isinstance(result, np.ndarray) |
| 79 | + assert isinstance(result, (list, np.ndarray)) |
| 80 | + |
39 | 81 |
|
40 | | -def test_invalid_simulation_mode(): |
41 | | - env, rocket = make_simple_3dof_components() |
| 82 | +def test_invalid_simulation_mode(example_plain_env, point_mass_rocket): |
| 83 | + """Test that invalid simulation mode raises ValueError.""" |
42 | 84 | with pytest.raises(ValueError): |
43 | | - Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="2 DOF") |
| 85 | + Flight( |
| 86 | + rocket=point_mass_rocket, |
| 87 | + environment=example_plain_env, |
| 88 | + rail_length=1, |
| 89 | + simulation_mode="2 DOF", |
| 90 | + ) |
0 commit comments