Skip to content

Conversation

@Gui-FernandesBR
Copy link
Member

Pull request type

  • Code changes (bugfix, features)

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 tests -m slow --runslow) have passed locally
  • CHANGELOG.md has been updated (if relevant)

Current behavior

A common mistake on rocketpy is to try running a flight simulation when the rocket is unstable.
The simulation will likely fail or never stops.
It is true that not always it will fail, but I believe we should be more restricted in order to avoid these case scenarios;

New behavior

  • Throws a UnstableRocketException everytime a flight is instantiated using a rocket with static margin lower than 0.
  • Added rocketpy official exceptions

Breaking change

  • Yes and No. As you can see by the tests, some code may break. However, no current function will be

Additional information

Future PRs should add more custom exceptions if needed.

In this PR:

  • Update docs: (1) explaining about rocketpy exceptions and (2) explaining about the stability check.
  • CHANGELOG and slow tests.

@codecov
Copy link

codecov bot commented Feb 8, 2025

Codecov Report

Attention: Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 80.11%. Comparing base (83aa20e) to head (04d2354).
Report is 17 commits behind head on develop.

Files with missing lines Patch % Lines
rocketpy/exceptions.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #764      +/-   ##
===========================================
+ Coverage    76.42%   80.11%   +3.69%     
===========================================
  Files           95       96       +1     
  Lines        11090    11359     +269     
===========================================
+ Hits          8475     9100     +625     
+ Misses        2615     2259     -356     

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



class UnstableRocketError(RocketPyException):
"""Raised when the rocket jas negative static margin."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

has*

Comment on lines +669 to +677
def __validate_rocket_static_margin(self):
"""
Avoids running a flight simulation if the rocket stability margin is
negative. This is a common mistake that can lead to unstable flights,
which usually runs indefinitely.
"""
if (s := self.rocket.static_margin(0)) < 0:
raise UnstableRocketError(s)

Copy link
Member

Choose a reason for hiding this comment

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

I think this error check might not be suitable right now. I see a few points as to why:

  • If a user wants to simulate an unstable rocket, for any reason, we should not make that impossible. There could always be a rocket that has a negative static margin, but the stability margin could quickly become positive with fuel burn or other effects

  • When in monte carlo simulations, what would happen if one of the iterations has an unstable rocket? Will all the simulations break because of it?

  • The generic aerodynamic coefficients don't calculate static margin correctly. This essentially means that all rockets with custom coefficients will have a negative static margin.

Maybe raising a warning is the best we can do?

Copy link
Collaborator

@phmbressan phmbressan Feb 9, 2025

Choose a reason for hiding this comment

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

I agree with the first point you raised: not all rockets that are instantaneously unstable are necessarily unsimulatable.

A possible compromise would be either a warning or letting the user set a variable, such as ALLOW_UNSTABLE.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I like the Idea of having a flag with default to false (so current API is respected).

Then we can simply turn It on for infinity

Copy link
Member

@MateusStano MateusStano Feb 9, 2025

Choose a reason for hiding this comment

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

Would ALLOW_UNSTABLE be by default True or False?

And if the default is False would that be a breaking change? Since someone who has a sim with an unstable rocket might start getting an error with this SM check

And if the default is True is it really helping the user that does not know about the negative static margin problem?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I meant default true

Copy link
Member Author

Choose a reason for hiding this comment

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

Converting from an error to a warning is the easiest option right now.

However, I think we should better understand the cases where an unstable rocket can fly.
Perhaps the problem/bug happens inside the u_dot function (after rail exit) ?

If setting ALLOW_UNSTABLE to false, we should describe this behavior in the error message.
Setting ALLOW_UNSTABLE to true would not make much help.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think setting It to true by default mantains current behavior while allowing clients to deliberatively switch.

IMHO a simple warning would not be easy to consume for safe guarding purposes

Copy link
Member Author

Choose a reason for hiding this comment

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

I understand.

My point is that maybe we should not keep the current behavior at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will think more about this during the week

@Gui-FernandesBR Gui-FernandesBR force-pushed the enh/unstable-rocket-error branch from 05ba950 to 2793a62 Compare February 13, 2025 21:48
@Gui-FernandesBR Gui-FernandesBR force-pushed the enh/unstable-rocket-error branch from 2793a62 to 04d2354 Compare February 14, 2025 12:49
@Gui-FernandesBR
Copy link
Member Author

Gotta think a better way of solving this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Closed

Development

Successfully merging this pull request may close these issues.

ENH: Custom Exception errors and messages

5 participants