Fix max_acceleration_power_off_time returning 0 when controllers are present
Pull request type
- [x] Code changes (bugfix, features)
Checklist
- [x] Tests for the changes have been added (if needed)
- [ ] Docs have been reviewed and added / updated
- [x] Lint (
black rocketpy/ tests/) has passed locally - [x] All tests (
pytest tests -m slow --runslow) have passed locally - [ ]
CHANGELOG.mdhas been updated (if relevant)
Current behavior
When a Flight has controllers (e.g., AirBrakes), max_acceleration_power_off_time always returns 0.0 and max_acceleration_power_off returns 0.0 m/s².
flight.max_acceleration_power_off_time # Returns 0.0 (wrong)
flight.max_acceleration_power_off # Returns 0.0 (wrong)
Root cause: np.argmax() on sliced array self.acceleration[burn_out_time_index:, 1] returns index relative to the slice, but the code uses this index directly on the full array.
New behavior
Returns correct time and acceleration values after motor burnout:
flight.max_acceleration_power_off_time # Returns actual time (e.g., 3.9s)
flight.max_acceleration_power_off # Returns actual acceleration
Fix in max_acceleration_power_off_time:
# Before
return self.acceleration[max_acceleration_time_index, 0]
# After
return self.acceleration[burn_out_time_index + max_acceleration_time_index, 0]
Breaking change
- [x] No
Additional information
Fixes #803
Original prompt
This section details on the original issue you should resolve
<issue_title>BUG: Maximum acceleration values broken when airbrake is used</issue_title> <issue_description>Describe the bug
When running any Flight with a Rocket containing an Airbrake, the maximum acceleration and G values are broken as follows:
- "Maximum Acceleration During Motor Burn" is absurdly high (e.g. ~4000m/s²)
- "Maximum Gs During Motor Burn" is absurdly high (e.g. ~400g)
- "Maximum Acceleration After Motor Burn" is zero
- "Maximum Gs After Motor Burn" is zero
To Reproduce
Given an existing RocketPy simulation (such as given in https://docs.rocketpy.org/en/latest/user/first_simulation.html), add an airbrake to the rocket with an "empty" airbrake controller function:
# The following code assumes env is the Environment used by the simulation def airbrake_controller_function( time: float, sampling_rate: float, state_raw: list, state_history_raw: list, observed_variables: list, air_brakes: AirBrakes, ): air_brakes.deployment_level = 0.0 wind_x: float = env.wind_velocity_x(state_raw[2]) wind_y: float = env.wind_velocity_y(state_raw[2]) free_stream_speed = ( (wind_x - state_raw[3]) ** 2 + (wind_y - state_raw[4] ** 2 + (state_raw[5]) ** 2 ) ** 0.5 mach_number = free_stream_speed / env.speed_of_sound(state_raw[2]) return ( time, air_brakes.deployment_level, air_brakes.drag_coefficient(air_brakes.deployment_level, mach_number), ) rocket.add_air_brakes( drag_coefficient_curve="airbrake_drag_curve.csv", # Note that you need to provide that file in the same folder controller_function=airbrake_controller_function, sampling_rate=10, clamp=True, )Here is an example drag curve file in .csv format (store it as "airbrake_drag_curve.csv"):
0.0, 0.1, 0.0 0.0, 0.95, 0.0 1.0, 0.1728, 0.37 1.0, 0.5, 0.37 1.0, 0.6, 0.39 1.0, 0.7, 0.53 1.0, 0.8, 0.7 1.0, 0.9, 1.08 1.0, 0.95, 1.1After running the simulation, call
flight.prints.all()and observe the output.Expected behavior
(tested with a sounding rocket in development by BEARS e.V.) / without Airbrake added
Maximum Values Maximum Speed: 311.570 m/s at 1.82 s Maximum Mach Number: 0.920 Mach at 1.82 s Maximum Reynolds Number: 2.556e+06 at 1.79 s Maximum Dynamic Pressure: 5.721e+04 Pa at 1.79 s Maximum Acceleration During Motor Burn: 191.146 m/s² at 0.73 s Maximum Gs During Motor Burn: 19.491 g at 0.73 s Maximum Acceleration After Motor Burn: 11.323 m/s² at 14.41 s Maximum Gs After Motor Burn: 1.155 Gs at 14.41 s Maximum Stability Margin: 3.157 c at 1.85 s Maximum Upper Rail Button Normal Force: 0.000 N Maximum Upper Rail Button Shear Force: 0.000 N Maximum Lower Rail Button Normal Force: 0.000 N Maximum Lower Rail Button Shear Force: 0.000 NObserved behavior
(tested with a sounding rocket in development by BEARS e.V.) / with Airbrake added but zero deployment
Maximum Values Maximum Speed: 311.663 m/s at 1.81 s Maximum Mach Number: 0.920 Mach at 1.81 s Maximum Reynolds Number: 2.556e+06 at 1.80 s Maximum Dynamic Pressure: 5.723e+04 Pa at 1.80 s Maximum Acceleration During Motor Burn: 3893.683 m/s² at 1.83 s Maximum Gs During Motor Burn: 397.045 g at 1.83 s Maximum Acceleration After Motor Burn: 0.000 m/s² at 0.00 s Maximum Gs After Motor Burn: 0.000 Gs at 0.00 s Maximum Stability Margin: 3.157 c at 1.83 s Maximum Upper Rail Button Normal Force: 0.000 N Maximum Upper Rail Button Shear Force: 0.000 N Maximum Lower Rail Button Normal Force: 0.000 N Maximum Lower Rail Button Shear Force: 0.000 NNotice how the output maximum acceleration is absurdly high during motor burn and is zero after motor burn (despite parachutes deploying at some point etc.). The flight otherwise performs the same (apogee and everything else is identical).
Additional context
Tested in RocketPy 1.10.0.
The issue does not appear when no Airbrake is configured. </issue_description>
<agent_instructions>Please investigate, make a plan, and try to find where the BUG is</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@Gui-FernandesBR @MateusStano
- Fixes RocketPy-Team/RocketPy#840
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 81.08%. Comparing base (9cf3dd4) to head (c87f262).
:warning: Report is 25 commits behind head on develop.
Additional details and impacted files
@@ Coverage Diff @@
## develop #898 +/- ##
===========================================
+ Coverage 80.27% 81.08% +0.81%
===========================================
Files 104 107 +3
Lines 12769 13814 +1045
===========================================
+ Hits 10250 11201 +951
- Misses 2519 2613 +94
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.