Skip to content

Commit 43c941f

Browse files
authored
Merge pull request #559 from RocketPy-Team/bug/export-eng-liquid-motor
BUG: Motor method 'export_eng' for liquid motors bug fix. (solves #473)
2 parents 9ac0e4f + bb65a33 commit 43c941f

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3737

3838

3939
### Fixed
40-
40+
- BUG: export_eng 'Motor' method would not work for liquid motors. [#559](https://github.com/RocketPy-Team/RocketPy/pull/559)
4141

4242
## [v1.2.1] - 2024-02-22
4343

rocketpy/motors/motor.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,31 +1012,36 @@ def export_eng(self, file_name, motor_name):
10121012
None
10131013
"""
10141014
# Open file
1015-
file = open(file_name, "w")
1016-
1017-
# Write first line
1018-
file.write(
1019-
motor_name
1020-
+ " {:3.1f} {:3.1f} 0 {:2.3} {:2.3} RocketPy\n".format(
1021-
2000 * self.grain_outer_radius,
1022-
1000
1023-
* self.grain_number
1024-
* (self.grain_initial_height + self.grain_separation),
1025-
self.propellant_initial_mass,
1026-
self.propellant_initial_mass,
1027-
)
1028-
)
1015+
with open(file_name, "w") as file:
1016+
# Write first line
1017+
def get_attr_value(obj, attr_name, multiplier=1):
1018+
return multiplier * getattr(obj, attr_name, 0)
1019+
1020+
grain_outer_radius = get_attr_value(self, "grain_outer_radius", 2000)
1021+
grain_number = get_attr_value(self, "grain_number", 1000)
1022+
grain_initial_height = get_attr_value(self, "grain_initial_height")
1023+
grain_separation = get_attr_value(self, "grain_separation")
1024+
1025+
grain_total = grain_number * (grain_initial_height + grain_separation)
1026+
1027+
if grain_outer_radius == 0 or grain_total == 0:
1028+
warnings.warn(
1029+
"The motor object doesn't have some grain-related attributes. "
1030+
"Using zeros to write to file."
1031+
)
10291032

1030-
# Write thrust curve data points
1031-
for time, thrust in self.thrust.source[1:-1, :]:
1032-
# time, thrust = item
1033-
file.write("{:.4f} {:.3f}\n".format(time, thrust))
1033+
file.write(
1034+
f"{motor_name} {grain_outer_radius:3.1f} {grain_total:3.1f} 0 "
1035+
f"{self.propellant_initial_mass:2.3} "
1036+
f"{self.propellant_initial_mass:2.3} RocketPy\n"
1037+
)
10341038

1035-
# Write last line
1036-
file.write("{:.4f} {:.3f}\n".format(self.thrust.source[-1, 0], 0))
1039+
# Write thrust curve data points
1040+
for time, thrust in self.thrust.source[1:-1, :]:
1041+
file.write(f"{time:.4f} {thrust:.3f}\n")
10371042

1038-
# Close file
1039-
file.close()
1043+
# Write last line
1044+
file.write(f"{self.thrust.source[-1, 0]:.4f} {0:.3f}\n")
10401045

10411046
return None
10421047

0 commit comments

Comments
 (0)