Skip to content

Conversation

Copy link

Copilot AI commented Dec 2, 2025

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

Stability margin uses CN_α (lift coefficient derivative) to compute center of pressure:

CP = Σ(CN_α × X_cp) / Σ(CN_α)

This produces a CP independent of angle of attack, unlike OpenRocket's more dynamic behavior.

New behavior

Stability margin now uses CN(α) (actual lift coefficient) for CP calculation:

CP = Σ(CN(α) × X_cp) / Σ(CN(α))

New methods in Rocket class:

  • get_cp_position_from_alpha(alpha, mach) - computes CP as function of AoA and Mach
  • get_stability_margin_from_alpha(alpha, mach, time) - computes stability margin using AoA-dependent CP

Updated in Flight class:

  • stability_margin now uses simulation's angle of attack data
# New methods available
cp = rocket.get_cp_position_from_alpha(alpha=0.1, mach=0.5)
sm = rocket.get_stability_margin_from_alpha(alpha=0.1, mach=0.5, time=2.0)

# Flight.stability_margin now accounts for AoA automatically
flight.stability_margin  # Uses actual AoA from simulation

Note: With current linear lift models (cl = clalpha * α), the α cancels mathematically. This change establishes the correct framework for future non-linear aerodynamic models (custom airfoils, wind tunnel data).

Breaking change

  • No

Additional information

References:

Original prompt

This section details on the original issue you should resolve

<issue_title>ENH: Improve Stability Margin</issue_title>
<issue_description>Currently we use the rocket's total center of pressure to compute the stability margin of the Flight class.

Since this center of pressure is defined based on the normal force coefficient derivative with respect to the angle of attack (cnalpha) we end up with a curve that does not change with the angle of attack, as reported by user Eli [ARIS]
(dvoeli05_67950) in our discord.

On RocketPy:
Image

On OpenRocket:
Image

As you can see, in OpenRocket the stability margin is much more "dynamic" than ours.

Describe the solution you'd like

In the Flight class, instead of using the angle of attack independent center of pressure for the stability margin calculations :

$$CP = \frac{ \sum (C_{N})_{\alpha} \cdot (X_{cp})_{\text{surface}}} {\sum (C_{N})_{\alpha}}$$

We should use a calculation that is dependent on alpha:

$$CP = \frac{ \sum C_{N}(\alpha) \cdot (X_{cp})_{\text{surface}}} {\sum C_{N}(\alpha) }$$

References:

<agent_instructions>Please solve the issue described here.

Make a plan, find where in the code you're going to implement new changes. Then implement it following project guidelines.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Enhance stability margin calculation in Flight class ENH: Improve Stability Margin with Angle of Attack Dependency Dec 2, 2025
Copilot AI requested a review from Gui-FernandesBR December 2, 2025 13:56
Copilot finished work on behalf of Gui-FernandesBR December 2, 2025 13:56
@Gui-FernandesBR Gui-FernandesBR linked an issue Dec 2, 2025 that may be closed by this pull request
@Gui-FernandesBR Gui-FernandesBR force-pushed the copilot/improve-stability-margin-computation branch from 1898cb3 to 0dff1a7 Compare December 2, 2025 17:08
@codecov
Copy link

codecov bot commented Dec 2, 2025

Codecov Report

❌ Patch coverage is 96.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.03%. Comparing base (9cf3dd4) to head (d1e1a42).
⚠️ Report is 20 commits behind head on develop.

Files with missing lines Patch % Lines
rocketpy/rocket/rocket.py 95.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #899      +/-   ##
===========================================
+ Coverage    80.27%   81.03%   +0.76%     
===========================================
  Files          104      107       +3     
  Lines        12769    13671     +902     
===========================================
+ Hits         10250    11078     +828     
- Misses        2519     2593      +74     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +3124 to +3389
aoa_values = self.angle_of_attack.y_array
mach_values = self.mach_number.y_array
time_values = self.time

results = []
for i, t in enumerate(time_values):
# Convert angle of attack from degrees to radians
alpha_rad = np.deg2rad(aoa_values[i])
mach = mach_values[i]
sm = self.rocket.get_stability_margin_from_alpha(alpha_rad, mach, t)
results.append((t, sm))

return results
Copy link
Member

Choose a reason for hiding this comment

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

could be more pythonic

Copilot AI and others added 2 commits December 3, 2025 19:38
Add new methods to compute center of pressure and stability margin
as a function of angle of attack (α) instead of just using the lift
coefficient derivative. This provides a more accurate stability
margin that varies with angle of attack, similar to OpenRocket.

- Add Rocket.get_cp_position_from_alpha(alpha, mach) method
- Add Rocket.get_stability_margin_from_alpha(alpha, mach, time) method
- Update Flight.stability_margin to use angle of attack from simulation
- Add tests for new functionality

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>
- Add comment explaining degrees to radians conversion in Flight.stability_margin
- Replace magic number 1e-10 with named constant epsilon in get_cp_position_from_alpha

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>
@Gui-FernandesBR Gui-FernandesBR force-pushed the copilot/improve-stability-margin-computation branch from 0dff1a7 to d1e1a42 Compare December 3, 2025 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Backlog

Development

Successfully merging this pull request may close these issues.

ENH: Improve Stability Margin

2 participants