Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Prevent out of bounds Tanks from Instantiation #484

Merged
merged 5 commits into from
Nov 24, 2023

Conversation

phmbressan
Copy link
Collaborator

Pull request type

  • Code changes (bugfix, features)
  • Code maintenance (refactoring, formatting, tests)
  • ReadMe, Docs and GitHub updates
  • Other (please describe):

Checklist

  • Tests for the changes have been added (if needed)
  • Docs have been reviewed and added / updated
  • Lint (black rocketpy/ tests/) has passed locally
  • All tests (pytest --runslow) have passed locally

Current behavior

Due to the fact that the Tank getters are evaluated lazily (post instance), it is possible that a Tank with invalid inputs is only detected by specific methods that may not be called.

New behavior

This PR introduces methods to check if the Tank fluids are inside the appropriate volume limits and calls them on instantiation.

Therefore, a invalid Tank shall not be constructed.

Breaking change

  • Yes
  • No

@phmbressan phmbressan added Bug Something isn't working Motors Every propulsion related issue or PR labels Nov 23, 2023
@phmbressan phmbressan self-assigned this Nov 23, 2023
Copy link

codecov bot commented Nov 23, 2023

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (3434ae9) 70.83% compared to head (dab946d) 70.84%.

Files Patch % Lines
rocketpy/motors/tank.py 69.69% 10 Missing ⚠️
Additional details and impacted files
@@                Coverage Diff                @@
##           hotfix/v1.1.1     #484      +/-   ##
=================================================
+ Coverage          70.83%   70.84%   +0.01%     
=================================================
  Files                 55       55              
  Lines               9240     9258      +18     
=================================================
+ Hits                6545     6559      +14     
- Misses              2695     2699       +4     
Flag Coverage Δ
unittests 70.84% <69.69%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

"""
if (self.fluid_volume > self.geometry.total_volume + 1e-6).any():
raise ValueError(
f"The tank '{self.name}' is overfilled. The fluid volume is "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you could create a single string to be used in both error messages, thus reducing the number of lines here.

Something like this.

error_msg = "The tank '{self.name}' is {fill_condition}'. " + "Consider increasing input fluid quantities or checking the fluid density values.\n\t\t" + "The fluid volume is {volume:.3f} m³ at {time:.3f} s."

raise ValueError(error_msg.format(volume=self.fluid_volume.y_array[max_volume_idx],
time=self.fluid_volume.x_array[max_volume_idx]))

Sorry for keeping this short.

Comment on lines 417 to 424
raise ValueError(
f"The tank '{self.name}' is overfilled. The fluid volume is "
+ "greater than the total volume of the tank.\n\t\t"
+ "Try increasing the tank height and check out the fluid density"
+ " values.\n\t\t"
+ f"The fluid volume is {np.max(self.fluid_volume.y_array):.3f} m³ at "
+ f"{self.fluid_volume.x_array[np.argmax(self.fluid_volume.y_array)]:.3f} s."
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValueError(
f"The tank '{self.name}' is overfilled. The fluid volume is "
+ "greater than the total volume of the tank.\n\t\t"
+ "Try increasing the tank height and check out the fluid density"
+ " values.\n\t\t"
+ f"The fluid volume is {np.max(self.fluid_volume.y_array):.3f} m³ at "
+ f"{self.fluid_volume.x_array[np.argmax(self.fluid_volume.y_array)]:.3f} s."
)
raise ValueError(
f"The tank '{self.name}' is overfilled. The fluid volume is "
+ "greater than the total volume of the tank.\n\t\t"
+ "Try increasing the tank height and check out the fluid density"
+ " values.\n\t\t"
+ f"The fluid volume is {np.max(self.fluid_volume.y_array):.3f} m³ at "
+ f"{self.fluid_volume.x_array[np.argmax(self.fluid_volume.y_array)]:.3f} s.\n\t\t"
+ f"The tank total volume is {self.geometry.total_volume:.3f} m³."
)

Copy link
Member

@MateusStano MateusStano Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we also print the tank total volume in all the over/underfilled error messages

@Gui-FernandesBR
Copy link
Member

Hey, last comment, should we add tests to ensure the exceptions are being raised correctly?

@MateusStano MateusStano merged commit 553c0f7 into hotfix/v1.1.1 Nov 24, 2023
10 of 11 checks passed
@Gui-FernandesBR Gui-FernandesBR deleted the bug/tanks-overfill branch November 25, 2023 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Motors Every propulsion related issue or PR
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

3 participants