Skip to content

Commit

Permalink
Merge pull request #559 from RocketPy-Team/bug/export-eng-liquid-motor
Browse files Browse the repository at this point in the history
BUG: Motor method 'export_eng' for liquid motors bug fix. (solves #473)
  • Loading branch information
lucasfourier authored Feb 26, 2024
2 parents 9ac0e4f + bb65a33 commit 43c941f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).


### Fixed

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

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

Expand Down
49 changes: 27 additions & 22 deletions rocketpy/motors/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,31 +1012,36 @@ def export_eng(self, file_name, motor_name):
None
"""
# Open file
file = open(file_name, "w")

# Write first line
file.write(
motor_name
+ " {:3.1f} {:3.1f} 0 {:2.3} {:2.3} RocketPy\n".format(
2000 * self.grain_outer_radius,
1000
* self.grain_number
* (self.grain_initial_height + self.grain_separation),
self.propellant_initial_mass,
self.propellant_initial_mass,
)
)
with open(file_name, "w") as file:
# Write first line
def get_attr_value(obj, attr_name, multiplier=1):
return multiplier * getattr(obj, attr_name, 0)

grain_outer_radius = get_attr_value(self, "grain_outer_radius", 2000)
grain_number = get_attr_value(self, "grain_number", 1000)
grain_initial_height = get_attr_value(self, "grain_initial_height")
grain_separation = get_attr_value(self, "grain_separation")

grain_total = grain_number * (grain_initial_height + grain_separation)

if grain_outer_radius == 0 or grain_total == 0:
warnings.warn(
"The motor object doesn't have some grain-related attributes. "
"Using zeros to write to file."
)

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

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

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

return None

Expand Down

0 comments on commit 43c941f

Please sign in to comment.