From d35b4afc59dde46134532f62a90512018f652f86 Mon Sep 17 00:00:00 2001 From: Lucas <69172945+lucasfourier@users.noreply.github.com> Date: Tue, 20 Feb 2024 14:36:07 -0300 Subject: [PATCH 1/5] Motor method 'export_eng' for liquid motors. --- rocketpy/motors/motor.py | 45 ++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/rocketpy/motors/motor.py b/rocketpy/motors/motor.py index 0285a1203..38917dd83 100644 --- a/rocketpy/motors/motor.py +++ b/rocketpy/motors/motor.py @@ -1014,18 +1014,41 @@ def export_eng(self, file_name, motor_name): # 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, + if ( + hasattr(self, "grain_outer_radius") + and hasattr(self, "grain_number") + and hasattr(self, "grain_initial_height") + and hasattr(self, "grain_separation") + ): + # Write first line for motors with grains + 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, + ) + ) + + else: + # Warn the user that the motor doesn't have at least one grain attribute + warnings.warn( + "The motor object doesn't have some grain-related attributes. Using zeros to write to file." + ) + + # Write first line for motors without grain attributes + # The zeros are here because the first two placeholders + # are grain-related attributes. + file.write( + motor_name + + " 0 0 0 {:2.3} {:2.3} RocketPy\n".format( + self.propellant_initial_mass, + self.propellant_initial_mass, + ) ) - ) # Write thrust curve data points for time, thrust in self.thrust.source[1:-1, :]: From ca3e20a3225577ed05a20390712ad9c24be47443 Mon Sep 17 00:00:00 2001 From: Lucas <69172945+lucasfourier@users.noreply.github.com> Date: Sat, 24 Feb 2024 17:24:52 -0300 Subject: [PATCH 2/5] Reviewing the code after comments. --- rocketpy/motors/motor.py | 66 +++++++++++++++------------------------- 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/rocketpy/motors/motor.py b/rocketpy/motors/motor.py index 38917dd83..6c2242a9f 100644 --- a/rocketpy/motors/motor.py +++ b/rocketpy/motors/motor.py @@ -1012,54 +1012,36 @@ def export_eng(self, file_name, motor_name): None """ # Open file - file = open(file_name, "w") - - if ( - hasattr(self, "grain_outer_radius") - and hasattr(self, "grain_number") - and hasattr(self, "grain_initial_height") - and hasattr(self, "grain_separation") - ): - # Write first line for motors with grains - 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." ) - ) - - else: - # Warn the user that the motor doesn't have at least one grain attribute - warnings.warn( - "The motor object doesn't have some grain-related attributes. Using zeros to write to file." - ) - # Write first line for motors without grain attributes - # The zeros are here because the first two placeholders - # are grain-related attributes. file.write( - motor_name - + " 0 0 0 {:2.3} {:2.3} RocketPy\n".format( - self.propellant_initial_mass, - self.propellant_initial_mass, - ) + 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 thrust curve data points - for time, thrust in self.thrust.source[1:-1, :]: - # time, thrust = item - file.write("{:.4f} {:.3f}\n".format(time, thrust)) - - # 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 From 2f527ffef8beec44861ebc7f0e25e8b27470e4e2 Mon Sep 17 00:00:00 2001 From: Lucas <69172945+lucasfourier@users.noreply.github.com> Date: Sat, 24 Feb 2024 19:48:10 -0300 Subject: [PATCH 3/5] Updated changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3503a9e9..9f9e80366 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,7 @@ You can install this version by running `pip install rocketpy==1.2.0` ### Fixed +- BUG: export_eng 'Motor' method would not work for liquid motors. [#559](https://github.com/RocketPy-Team/RocketPy/pull/559) - BUG: Update flight trajectory plot axes limits [#552](https://github.com/RocketPy-Team/RocketPy/pull/552) - BUG: fix `get_controller_observed_variables` in the air brakes examples [#551](https://github.com/RocketPy-Team/RocketPy/pull/551) - MNT: small fixes before v1.2 [#550](https://github.com/RocketPy-Team/RocketPy/pull/550) From dc959747c8ab077d1ec1c58157eb4c3a71fcb52f Mon Sep 17 00:00:00 2001 From: Lucas <69172945+lucasfourier@users.noreply.github.com> Date: Sat, 24 Feb 2024 20:12:17 -0300 Subject: [PATCH 4/5] Fixing the changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f9e80366..5a8322e0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). +- BUG: export_eng 'Motor' method would not work for liquid motors. [#559](https://github.com/RocketPy-Team/RocketPy/pull/559) + ### Added @@ -71,7 +73,6 @@ You can install this version by running `pip install rocketpy==1.2.0` ### Fixed -- BUG: export_eng 'Motor' method would not work for liquid motors. [#559](https://github.com/RocketPy-Team/RocketPy/pull/559) - BUG: Update flight trajectory plot axes limits [#552](https://github.com/RocketPy-Team/RocketPy/pull/552) - BUG: fix `get_controller_observed_variables` in the air brakes examples [#551](https://github.com/RocketPy-Team/RocketPy/pull/551) - MNT: small fixes before v1.2 [#550](https://github.com/RocketPy-Team/RocketPy/pull/550) From bb65a337e3e166395039d526b0d8246d515b5e99 Mon Sep 17 00:00:00 2001 From: Lucas <69172945+lucasfourier@users.noreply.github.com> Date: Sun, 25 Feb 2024 21:53:57 -0300 Subject: [PATCH 5/5] Fixing changelog again --- CHANGELOG.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a8322e0f..05a3bfaca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,8 +30,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). -- BUG: export_eng 'Motor' method would not work for liquid motors. [#559](https://github.com/RocketPy-Team/RocketPy/pull/559) - ### Added @@ -39,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.0] - 2024-02-dd