8000 BUG: Fix the handling of reference pressure for older rpy files. (#808) · RocketPy-Team/RocketPy@ee23bc5 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit ee23bc5

Browse files
BUG: Fix the handling of reference pressure for older rpy files. (#808)
Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>
1 parent b5bfb1a commit ee23bc5

File tree

9 files changed

+80
-58
lines changed

9 files changed

+80
-58
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Attention: The newest changes should be on top -->
4242

4343

4444
### Fixed
45+
46+
- BUG: Fix the handling of reference pressure for older rpy files. [#808](https://github.com/RocketPy-Team/RocketPy/pull/808)
4547
- BUG: Non-overshootable simulations error on time parsing. [#807](https://github.com/RocketPy-Team/RocketPy/pull/807)
4648

4749
## v1.9.0 - 2025-03-24

rocketpy/_encoders.py

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import numpy as np
88

99
from rocketpy.mathutils.function import Function
10-
from rocketpy.prints.flight_prints import _FlightPrints
1110
from rocketpy.plots.flight_plots import _FlightPlots
11+
from rocketpy.prints.flight_prints import _FlightPrints
1212

1313

1414
class RocketPyEncoder(json.JSONEncoder):
@@ -91,50 +91,7 @@ def object_hook(self, obj):
9191
new_flight = class_.__new__(class_)
9292
new_flight.prints = _FlightPrints(new_flight)
9393
new_flight.plots = _FlightPlots(new_flight)
94-
attributes = (
95-
"rocket",
96-
"env",
97-
"rail_length",
98-
"inclination",
99-
"heading",
100-
"initial_solution",
101-
"terminate_on_apogee",
102-
"max_time",
103-
"max_time_step",
104-
"min_time_step",
105-
"rtol",
106-
"atol",
107-
"time_overshoot",
108-
"name",
109-
"solution",
110-
"out_of_rail_time",
111-
"apogee_time",
112-
"apogee",
113-
"parachute_events",
114-
"impact_state",
115-
"impact_velocity",
116-
"x_impact",
117-
"y_impact",
118-
"t_final",
119-
"flight_phases",
120-
"ax",
121-
"ay",
122-
"az",
123-
"out_of_rail_time_index",
124-
"function_evaluations",
125-
"alpha1",
126-
"alpha2",
127-
"alpha3",
128-
"R1",
129-
"R2",
130-
"R3",
131-
"M1",
132-
"M2",
133-
"M3",
134-
)
135-
for attribute in attributes:
136-
setattr(new_flight, attribute, obj[attribute])
137-
new_flight.t_initial = new_flight.initial_solution[0]
94+
set_minimal_flight_attributes(new_flight, obj)
13895
return new_flight
13996
elif hasattr(class_, "from_dict"):
14097
return class_.from_dict(obj)
@@ -153,6 +110,63 @@ def object_hook(self, obj):
153110
return obj
154111

155112

113+
def set_minimal_flight_attributes(flight, obj):
114+
attributes = (
115+
"rocket",
116+
"env",
117+
"rail_length",
118+
"inclination",
119+
"heading",
120+
"initial_solution",
121+
"terminate_on_apogee",
122+
"max_time",
123+
"max_time_step",
124+
"min_time_step",
125+
"rtol",
126+
"atol",
127+
"time_overshoot",
128+
"name",
129+
"solution",
130+
"out_of_rail_time",
131+
"apogee_time",
132+
"apogee",
133+
"parachute_events",
134+
"impact_state",
135+
"impact_velocity",
136+
"x_impact",
137+
"y_impact",
138+
"t_final",
139+
"flight_phases",
140+
"ax",
141+
"ay",
142+
"az",
143+
"out_of_rail_time_index",
144+
"function_evaluations",
145+
"speed",
146+
"alpha1",
147+
"alpha2",
148+
"alpha3",
149+
"R1",
150+
"R2",
151+
"R3",
152+
"M1",
153+
"M2",
154+
"M3",
155+
"net_thrust",
156+
)
157+
158+
for attribute in attributes:
159+
try:
160+
setattr(flight, attribute, obj[attribute])
161+
except KeyError:
162+
# Manual resolution of new attributes
163+
if attribute == "net_thrust":
164+
flight.net_thrust = obj["rocket"].motor.thrust
165+
flight.net_thrust.set_discrete_based_on_model(flight.speed)
166+
167+
flight.t_initial = flight.initial_solution[0]
168+
169+
156170
def get_class_signature(obj):
157171
"""Returns the signature of a class so it can be identified on
158172
decoding. The signature is a dictionary with the module and

rocketpy/motors/hybrid_motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def from_dict(cls, data):
699699
grains_center_of_mass_position=data["grains_center_of_mass_position"],
700700
nozzle_position=data["nozzle_position"],
701701
throat_radius=data["throat_radius"],
702-
reference_pressure=data["reference_pressure"],
702+
reference_pressure=data.get("reference_pressure"),
703703
)
704704

705705
for tank in data["positioned_tanks"]:

rocketpy/motors/liquid_motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def from_dict(cls, data):
528528
nozzle_position=data["nozzle_position"],
529529
interpolation_method=data["interpolate"],
530530
coordinate_system_orientation=data["coordinate_system_orientation"],
531-
reference_pressure=data["reference_pressure"],
531+
reference_pressure=data.get("reference_pressure"),
532532
)
533533

534534
for tank in data["positioned_tanks"]:

rocketpy/motors/motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,5 +1896,5 @@ def from_dict(cls, data):
18961896
),
18971897
nozzle_position=data["nozzle_position"],
18981898
interpolation_method=data["interpolate"],
1899-
reference_pressure=data["reference_pressure"],
1899+
reference_pressure=data.get("reference_pressure"),
19001900
)

rocketpy/motors/solid_motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,5 +821,5 @@ def from_dict(cls, data):
821821
throat_radius=data["throat_radius"],
822822
interpolation_method=data["interpolate"],
823823
coordinate_system_orientation=data["coordinate_system_orientation"],
824-
reference_pressure=data["reference_pressure"],
824+
reference_pressure=data.get("reference_pressure"),
825825
)

rocketpy/simulation/monte_carlo.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ def __run_in_serial(self):
281281
try:
282282
while sim_monitor.keep_simulating():
283283
sim_monitor.increment()
284+
inputs_json, outputs_json = "", ""
284285

285286
flight = self.__run_single_simulation()
286287
inputs_json = self.__evaluate_flight_inputs(sim_monitor.count)
@@ -406,6 +407,7 @@ def __sim_producer(self, seed, sim_monitor, mutex, error_event): # pylint: disa
406407

407408
while sim_monitor.keep_simulating():
408409
sim_idx = sim_monitor.increment() - 1
410+
inputs_json, outputs_json = "", ""
409411

410412
flight = self.__run_single_simulation()
411413
inputs_json = self.__evaluate_flight_inputs(sim_idx)
@@ -484,7 +486,9 @@ def __evaluate_flight_inputs(self, sim_idx):
484486
for item in d.items()
485487
)
486488
inputs_dict["index"] = sim_idx
487-
return json.dumps(inputs_dict, cls=RocketPyEncoder) + "\n"
489+
return (
490+
json.dumps(inputs_dict, cls=RocketPyEncoder, **self._export_config) + "\n"
491+
)
488492

489493
def __evaluate_flight_outputs(self, flight, sim_idx):
490494
"""Evaluates the outputs of a single flight simulation.
@@ -518,7 +522,9 @@ def __evaluate_flight_outputs(self, flight, sim_idx):
518522
) from e
519523
outputs_dict = outputs_dict | additional_exports
520524

521-
return json.dumps(outputs_dict, cls=RocketPyEncoder) + "\n"
525+
return (
526+
json.dumps(outputs_dict, cls=RocketPyEncoder, **self._export_config) + "\n"
527+
)
522528

523529
def __terminate_simulation(self):
524530
"""

rocketpy/utilities.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import ast
22
import inspect
3-
import traceback
4-
import warnings
53
import json
64
import os
7-
8-
from pathlib import Path
9-
from importlib.metadata import version
5+
import traceback
6+
import warnings
107
from datetime import date
8+
from importlib.metadata import version
9+
from pathlib import Path
10+
1111
import matplotlib.pyplot as plt
1212
import numpy as np
1313
from scipy.integrate import solve_ivp
1414

15+
from ._encoders import RocketPyDecoder, RocketPyEncoder
1516
from .environment.environment import Environment
1617
from .mathutils.function import Function
1718
from .plots.plot_helpers import show_or_save_plot
1819
from .rocket.aero_surface import TrapezoidalFins
1920
from .simulation.flight import Flight
20-
from ._encoders import RocketPyEncoder, RocketPyDecoder
2121

2222

2323
def compute_cd_s_from_drop_test(

tests/unit/test_utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import os
12
from unittest.mock import patch
23

3-
import os
44
import numpy as np
55
import pytest
66

0 commit comments

Comments
 (0)
0