Skip to content

Commit b624ccc

Browse files
committed
ENH: first version of tests for 3dof rocketpy
- ENH: added first versions of tests using python assert statements for pointmassmotor, pointmassrocket and 3dof segments of flight.
1 parent f047a45 commit b624ccc

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
from rocketpy.motors.point_mass_motor import PointMassMotor
3+
4+
def test_init_required_args():
5+
m = PointMassMotor(thrust_source=10, dry_mass=1.0, propellant_initial_mass=0.5, burn_time=1.2)
6+
assert isinstance(m, PointMassMotor)
7+
assert m.dry_mass == 1.0
8+
assert m.propellant_initial_mass == 0.5
9+
10+
def test_missing_required_args_raises():
11+
with pytest.raises(ValueError):
12+
PointMassMotor(10, 1.0, None, 1)
13+
with pytest.raises(ValueError):
14+
PointMassMotor(10, 1.0, 1.2)
15+
with pytest.raises(TypeError):
16+
PointMassMotor([], 1.0, 1.2, 1)
17+
18+
def test_exhaustvelocity_and_totalmassflowrate():
19+
m = PointMassMotor(thrust_source=10, dry_mass=1.0, propellant_initial_mass=1.0, burn_time=2.0)
20+
ev_func = m.exhaustvelocity()
21+
assert hasattr(ev_func, 'getValue')
22+
tmf_func = m.totalmassflowrate
23+
assert hasattr(tmf_func, 'getValue')
24+
25+
def test_zero_inertias():
26+
m = PointMassMotor(thrust_source=10, dry_mass=1.0, propellant_initial_mass=1.0, burn_time=2.0)
27+
assert m.propellantI11().getValue(0) == 0
28+
assert m.propellantI22().getValue(0) == 0
29+
assert m.propellantI33().getValue(0) == 0
30+
31+
def test_callable_thrust():
32+
m = PointMassMotor(thrust_source=lambda t: 100*t, dry_mass=2, propellant_initial_mass=2, burn_time=4)
33+
assert m.thrust_source(0.5) == 50
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from rocketpy.rocket.rocket import PointMassRocket
2+
from rocketpy.motors.point_mass_motor import PointMassMotor
3+
4+
def test_point_mass_rocket_basic_properties():
5+
motor = PointMassMotor(10, 1.0, 0.5, 1.0)
6+
rocket = PointMassRocket(
7+
radius=0.05,
8+
mass=2.0,
9+
center_of_mass_without_motor=0.1,
10+
power_off_drag=0.7,
11+
power_on_drag=0.8
12+
)
13+
rocket.addmotor(motor, 0)
14+
assert rocket.radius == 0.05
15+
assert rocket.mass == 2.0
16+
assert rocket.motor is motor
17+
assert rocket.dryI11 == 0.0 # 3-DOF: inertias are forced zero
18+
19+
def test_structural_and_total_mass():
20+
"""Test structural and total mass properties of point mass rocket."""
21+
motor = PointMassMotor(10, 1.0, 1.1, 2.0)
22+
rocket = PointMassRocket(
23+
radius=0.03,
24+
mass=2.5,
25+
center_of_mass_without_motor=0,
26+
power_off_drag=0.3,
27+
power_on_drag=0.4
28+
)
29+
rocket.addmotor(motor, 0)
30+
31+
# Test that structural mass and total mass are calculated correctly
32+
assert rocket.mass == 2.5
33+
expected_total_mass = rocket.mass + motor.propellant_initial_mass
34+
assert abs(rocket.total_mass(0) - expected_total_mass) < 1e-6
35+
36+
def test_add_motor_overwrites():
37+
"""Test that adding a motor overwrites the previous motor."""
38+
motor1 = PointMassMotor(10, 1, 1.1, 2.0)
39+
motor2 = PointMassMotor(20, 2, 1.5, 3.0)
40+
rocket = PointMassRocket(0.02, 1.0, 0.0, 0.2, 0.5)
41+
rocket.addmotor(motor1, position=0)
42+
rocket.addmotor(motor2, position=0)
43+
assert rocket.motor == motor2
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytest
2+
import numpy as np
3+
from rocketpy.simulation.flight import Flight
4+
from rocketpy.rocket.rocket import PointMassRocket
5+
from rocketpy.motors.point_mass_motor import PointMassMotor
6+
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")
25+
assert flight.simulation_mode == "3 DOF"
26+
27+
def test_3dof_simulation_mode_warning(monkeypatch):
28+
env, rocket = make_simple_3dof_components()
29+
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"
32+
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
37+
result = flight.udotgeneralized3dof(0, u)
38+
assert isinstance(result, list) or isinstance(result, np.ndarray)
39+
40+
def test_invalid_simulation_mode():
41+
env, rocket = make_simple_3dof_components()
42+
with pytest.raises(ValueError):
43+
Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="2 DOF")

0 commit comments

Comments
 (0)