Skip to content
Open
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
21 changes: 21 additions & 0 deletions rocketpy/motors/point_mass_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,24 @@ def propellant_I_23(self):
@cached_property
def propellant_I_33(self):
return self._zero_inertia_func()

def to_dict(self, **kwargs):
data = super().to_dict(**kwargs)
data.update(
{
"propellant_initial_mass": self.propellant_initial_mass,
"propellant_final_mass": self.propellant_final_mass,
}
)
return data

@classmethod
def from_dict(cls, data):
return cls(
thrust_source=data["thrust_source"],
dry_mass=data["dry_mass"],
propellant_initial_mass=data["propellant_initial_mass"],
burn_time=data["burn_time"],
propellant_final_mass=data.get("propellant_final_mass"),
interpolation_method=data["interpolate"],
)
16 changes: 16 additions & 0 deletions rocketpy/rocket/point_mass_rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,19 @@ def evaluate_inertias(self):
self.I_13 = 0.0
self.I_23 = 0.0
return (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)

def to_dict(self, **kwargs):
data = super().to_dict(**kwargs)
data["weathercock_coeff"] = self.weathercock_coeff
return data

@classmethod
def _new_from_dict(cls, data):
return cls(
radius=data["radius"],
mass=data["mass"],
center_of_mass_without_motor=data["center_of_mass_without_motor"],
power_off_drag=data["power_off_drag"],
power_on_drag=data["power_on_drag"],
weathercock_coeff=data.get("weathercock_coeff", 0.0),
)
11 changes: 9 additions & 2 deletions rocketpy/rocket/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2298,8 +2298,11 @@ def to_dict(self, **kwargs):
return rocket_dict

@classmethod
def from_dict(cls, data):
rocket = cls(
def _new_from_dict(cls, data):
"""Build the rocket itself from a saved dictionary, before its motor,
surfaces and other parts are added. A subclass whose constructor takes
other arguments overrides this."""
return cls(
radius=data["radius"],
mass=data["mass"],
inertia=(
Expand All @@ -2316,6 +2319,10 @@ def from_dict(cls, data):
coordinate_system_orientation=data["coordinate_system_orientation"],
)

@classmethod
def from_dict(cls, data):
rocket = cls._new_from_dict(data)

if (motor := data["motor"]) is not None:
rocket.add_motor(
motor=motor,
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,22 @@ def test_encoder_no_pickle(parachute_name, request):

with pytest.raises(ValueError, match=r"non-hexadecimal number found"):
from_hex_decode(trigger_loaded)


@pytest.mark.parametrize("resimulate", [False, True])
def test_point_mass_flight_save_load(flight_3dof_with_weathercock, resimulate):
"""A flight of a point mass rocket and motor can be saved and loaded."""
flight = flight_3dof_with_weathercock

loaded = json.loads(
json.dumps(flight, cls=RocketPyEncoder),
cls=RocketPyDecoder,
resimulate=resimulate,
)

assert type(loaded.rocket).__name__ == "PointMassRocket"
assert type(loaded.rocket.motor).__name__ == "PointMassMotor"
assert loaded.rocket.weathercock_coeff == flight.rocket.weathercock_coeff
assert loaded.apogee_time == pytest.approx(flight.apogee_time, rel=1e-6)
assert loaded.apogee == pytest.approx(flight.apogee, rel=1e-6)
assert loaded.t_final == pytest.approx(flight.t_final, rel=1e-3)
30 changes: 30 additions & 0 deletions tests/unit/motors/test_point_mass_motor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import json

import numpy as np
import pytest

from rocketpy._encoders import RocketPyDecoder, RocketPyEncoder
from rocketpy.motors.point_mass_motor import PointMassMotor


Expand Down Expand Up @@ -77,3 +81,29 @@ def test_callable_thrust():
burn_time=4,
)
assert m.thrust_source(0.5) == 50


@pytest.mark.parametrize(
"arguments",
[
{"thrust_source": 10, "burn_time": 1.2},
{"thrust_source": np.array([(0, 0), (0.1, 30), (1.0, 25), (1.2, 0)])},
{"thrust_source": 10, "burn_time": 1.2, "propellant_final_mass": 0.1},
],
)
def test_save_and_load_give_the_same_motor(arguments):
"""A point mass motor takes other arguments than the motors it is saved
like, so it is rebuilt from its own."""
motor = PointMassMotor(dry_mass=1.0, propellant_initial_mass=0.5, **arguments)

loaded = json.loads(json.dumps(motor, cls=RocketPyEncoder), cls=RocketPyDecoder)

assert isinstance(loaded, PointMassMotor)
assert loaded.dry_mass == motor.dry_mass
assert loaded.propellant_initial_mass == motor.propellant_initial_mass
assert loaded.propellant_final_mass == motor.propellant_final_mass
assert loaded.burn_time == pytest.approx(motor.burn_time)
assert loaded.total_impulse == pytest.approx(motor.total_impulse)
for time in (0.0, 0.5, 1.0):
assert loaded.thrust(time) == pytest.approx(motor.thrust(time))
assert loaded.total_mass(time) == pytest.approx(motor.total_mass(time))
49 changes: 49 additions & 0 deletions tests/unit/rocket/test_point_mass_rocket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import json

import pytest

from rocketpy._encoders import RocketPyDecoder, RocketPyEncoder
from rocketpy.motors.point_mass_motor import PointMassMotor
from rocketpy.rocket.point_mass_rocket import PointMassRocket

Expand Down Expand Up @@ -57,3 +62,47 @@ def test_add_motor_overwrites():
rocket.add_motor(motor1, position=0)
rocket.add_motor(motor2, position=0)
assert rocket.motor == motor2


@pytest.mark.parametrize("with_motor", [True, False])
def test_save_and_load_give_the_same_rocket(with_motor):
"""A point mass rocket is rebuilt with its own arguments, and keeps its
weathercock coefficient and its motor."""
rocket = PointMassRocket(
radius=0.05,
mass=2.0,
center_of_mass_without_motor=0.1,
power_off_drag=0.3,
power_on_drag=0.4,
weathercock_coeff=2.5,
)
if with_motor:
motor = PointMassMotor(
thrust_source=10, dry_mass=1.0, propellant_initial_mass=0.5, burn_time=1.2
)
rocket.add_motor(motor, position=0)

loaded = json.loads(json.dumps(rocket, cls=RocketPyEncoder), cls=RocketPyDecoder)

assert isinstance(loaded, PointMassRocket)
assert loaded.radius == rocket.radius
assert loaded.mass == rocket.mass
assert loaded.weathercock_coeff == 2.5
assert loaded.center_of_mass_without_motor == rocket.center_of_mass_without_motor
assert loaded.power_off_drag(0.5) == pytest.approx(0.3)
assert loaded.power_on_drag(0.5) == pytest.approx(0.4)
assert (loaded.dry_I_11, loaded.dry_I_22, loaded.dry_I_33) == (0.0, 0.0, 0.0)
if with_motor:
assert isinstance(loaded.motor, PointMassMotor)
for time in (0.0, 0.6, 1.2):
assert loaded.total_mass(time) == pytest.approx(rocket.total_mass(time))


def test_a_file_saved_without_the_weathercock_coefficient_still_loads():
rocket = PointMassRocket(0.05, 2.0, 0.0, 0.3, 0.4, weathercock_coeff=2.5)
saved = json.loads(json.dumps(rocket, cls=RocketPyEncoder))
del saved["weathercock_coeff"]

loaded = json.loads(json.dumps(saved), cls=RocketPyDecoder)

assert loaded.weathercock_coeff == 0.0
Loading