Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by CometPilot655

Why does my RocketPy Monte Carlo simulation trigger a 'Flight' object has no attribute 'apogee' error?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm modeling a rocket trajectory using RocketPy. The deterministic simulation runs fine, but when I try to run the Monte Carlo simulations, I encounter an error indicating that the Flight object is missing the apogee attribute:

AttributeError: 'Flight' object has no attribute 'apogee'

I have simplified my code as much as possible. Below is the rocket setup code:

PYTHON
# Imports from rocketpy import Environment, SolidMotor, Rocket, Flight, MonteCarlo from rocketpy.stochastic import ( StochasticEnvironment, StochasticSolidMotor, StochasticRocket, StochasticFlight, ) import numpy as np # Date: import datetime %matplotlib widget # Environment env = Environment(latitude=67.89325597913002,longitude=21.065756056273834, elevation=300) tomorrow = datetime.date.today() + datetime.timedelta(days=1) env.set_date((tomorrow.year, tomorrow.month, tomorrow.day, 12)) env.set_atmospheric_model(type="Ensemble", file="GEFS") # Motor RedMotor = SolidMotor( thrust_source=r"...\Python Modelling\Thrust curves\RedMotor.eng", burn_time=13.1, # s dry_mass=1.815, # kg dry_inertia=(1.86, 1.86, 0.13), center_of_dry_mass_position=0, grains_center_of_mass_position=0, grain_number=1, grain_separation=0, grain_density=1750, # kg/m^3 grain_outer_radius=0.2612, # m grain_initial_inner_radius=0.0726, # m grain_initial_height=2.6072, # m nozzle_radius=0.235, # m throat_radius=0.0726, # m interpolation_method="linear", nozzle_position=2.1, # from CG of motor coordinate_system_orientation="combustion_chamber_to_nozzle", # combustion_chamber_to_nozzle" ) # Rocket RedRocket = Rocket( radius=0.23, mass=184.77, inertia=(1315, 1315, 14.1155), power_off_drag=r"...\\Python Modelling\\Drag curves\\Cesaroni_6026M1670_PpowerOffDragCurve.eng", power_on_drag=r"...\\Python Modelling\\Drag curves\\Cesaroni_6026M1670_PpowerOnDragCurve.eng", center_of_mass_without_motor=4.45, coordinate_system_orientation="nose_to_tail", ) rail_buttons = RedRocket.set_rail_buttons( upper_button_position=3.8, lower_button_position=1.5, ) RedRocket.add_motor(RedMotor, position=6.6428125) nose_cone = RedRocket.add_nose(length=0.9375, kind="ogive", position=0) fin_set = RedRocket.add_trapezoidal_fins( n=4, root_chord=0.703125, tip_chord=0.703125, span=0.546875, position=8.01, ) transition = RedRocket.add_tail( top_radius=0.23, bottom_radius=0.2795, length=0.705625, position=4.57, ) Main = RedRocket.add_parachute( "Main", cd_s=2.2 * np.pi * (120 * 25.4 / 1000) * (120 * 25.4 / 1000) / 4, trigger=167.64, sampling_rate=105, lag=1, noise=(0, 8.3, 0.5), ) Drogue = RedRocket.add_parachute( "Drogue", cd_s=1.5 * np.pi * (24 * 25.4 / 1000) * (24 * 25.4 / 1000) / 4, trigger="apogee", sampling_rate=105, lag=1, noise=(0, 8.3, 0.5), ) # Flight test_flight = Flight( rocket=RedRocket, environment=env, rail_length=5.2, inclination=60, heading=0, ) test_flight.all_info()

For the stochastic setup, I use the following code:

PYTHON
# Stochastic Environment stochastic_env = StochasticEnvironment( environment=env, ensemble_member=list(range(env.num_ensemble_members)), ) stochastic_env.visualize_attributes() # Stochastic Motor stochastic_motor = StochasticSolidMotor( solid_motor=RedMotor, burn_start_time=(0, 0.1, "binomial"), grains_center_of_mass_position=0.001, grain_density=50, grain_separation=0.001, grain_initial_height=0.001, grain_initial_inner_radius= 0.00038, grain_outer_radius= 0.00038, total_impulse=(1.07*(RedMotor.total_impulse), (RedMotor.total_impulse/10)), throat_radius= 0.0005, nozzle_radius= 0.0005, nozzle_position=0.001, ) stochastic_motor.visualize_attributes() # Stochastic Rocket stochastic_rocket = StochasticRocket( rocket=RedRocket, radius=0.23, mass= 184.77, inertia_11= (1325.502, 0), inertia_22= (1325.502,0.01000), inertia_33= 0.01, center_of_mass_without_motor=4.45, ) stochastic_rocket.visualize_attributes() # Stochastic Flight stochastic_flight = StochasticFlight( flight=test_flight, inclination=(60, 1), heading=(0, 2), ) stochastic_flight.visualize_attributes()

Finally, this is how I run the Monte Carlo simulation:

PYTHON
# Monte Carlo Simulations test_dispersion = MonteCarlo( filename="monte_carlo_analysis_outputs/monte_carlo_class_example", environment=stochastic_env, rocket=stochastic_rocket, flight=stochastic_flight, ) test_dispersion.simulate( number_of_simulations=1000, append=False)

The error occurs during this Monte Carlo simulation. What am I doing wrong, and how can I ensure that the apogee attribute is properly computed during each simulation?

2

Comments 0

Answers 4

Views 72

4 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

The error

  AttributeError: 'Flight' object has no attribute 'apogee'

occurs because during the Monte Carlo simulation the flight instance does not have its apogee value computed when the drogue parachute (whose trigger is set to "apogee") needs it.

In RocketPy the drogue’s trigger value can be provided either as a fixed altitude (a number) or as the string "apogee". When you set it to "apogee", the code expects that the Flight object already has an apogee attribute (computed during the flight simulation) so that it can trigger the parachute at that altitude. In your deterministic flight (test_flight) you call test_flight.all_info(), which computes apogee, but then you create the StochasticFlight (and pass it into Monte Carlo) which internally uses a fresh iteration of the simulation that might not have computed the apogee attribute, triggering the error.

How to Fix

There are two common solutions:

  1. Simulate the flight to compute the apogee in each Monte Carlo iteration
     Ensure that the flight object used in the stochastic simulation runs a full simulation so that attributes like apogee are available when needed. In many cases the Monte Carlo simulation should call flight.simulate() for each iteration. Check that your version of RocketPy is up to date and that no configuration flags are preventing the computation of apogee.

  2. Use a numerical value for the drogue parachute trigger
     If the apogee is known from your deterministic simulation, you can replace the trigger from the string "apogee" to a fixed altitude. For example, if your deterministic flight calculated an apogee of around 5500 meters, modify your drogue definition as follows:

PYTHON
Drogue = RedRocket.add_parachute( "Drogue", cd_s=1.5 * np.pi * (24 * 25.4 / 1000) * (24 * 25.4 / 1000) / 4, trigger=5500, # Use a fixed altitude instead of "apogee" sampling_rate=105, lag=1, noise=(0, 8.3, 0.5), )

This bypasses the need to access the apogee attribute during Monte Carlo iterations.

Recommendation

If you prefer to use "apogee" as the trigger, verify that each flight simulation within the Monte Carlo loop is fully computed (i.e. that it runs its complete simulation so that the flight.apogee property is assigned). Otherwise, switching to a numerical trigger is a straightforward workaround.

Either approach should resolve the AttributeError you are encountering.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by AsteroidCosmonaut721 1 month ago

0

Got it! The other aerodynamic surfaces were actually essential. Without these, the simulation doesn't work and doesn't return any apogee values. Thanks for the help Onuralp!

No comments yet.

Answer by AsteroidVoyager087 1 month ago

0

Looks like your stochastic details do not properly created as we can see from "test_dispersion.prints.all()"

Reffering documents here -> for stochastic envorienment, it might be due to lack of stochastic details so
please try

PYTHON
stochastic_motor.visualize_attributes() print(total_impulse) stochastic_rocket.visualize_attributes() stochastic_flight.visualize_attributes()

If you are not seeing any detail again, it is highly possible that there is lack of data before the part of

PYTHON
test_dispersion = MonteCarlo( filename="monte_carlo_analysis_outputs/monte_carlo_class_example", environment=stochastic_env, rocket=stochastic_rocket, flight=stochastic_flight, )

which expalins the blank output from -> test_dispersion.prints.all().
Which in the documents link given it suggests an output

                Parameter            Mean       Std. Dev.  
------------------------------------------------------------  
                   apogee        3221.616         594.740  
     frontal_surface_wind          -3.839           0.481  
                 apogee_y         876.923         123.791  
     lateral_surface_wind          -5.259           0.621  
                  t_final         299.027          34.327  
     out_of_rail_velocity          25.690           2.194  
 initial_stability_margin           2.596           0.085  
         out_of_rail_time           0.361           0.027  
                 y_impact       -1839.493         486.133  
                 apogee_x         446.565         124.777  
          impact_velocity          -5.259           0.378  
          max_mach_number           0.859           0.131  
                 x_impact         312.319         195.323  
out_of_rail_stability_margin           2.669           0.084  
              apogee_time          24.782           2.015  

where apogee can bee seen in first line.

No comments yet.

Answer by EclipseTracker372 1 month ago

0

Thanks for that Onuralp. That's a good insight. I did get some good results for all the following:

PYTHON
stochastic_motor.visualize_attributes() print(stochastic_motor.total_impulse) stochastic_rocket.visualize_attributes() stochastic_flight.visualize_attributes()

but something that stuck out to me as a potential problem was that when I tried print(stochastic_motor.total_impulse), I got back the following:

PYTHON
(np.float64(2221943.0652824277), np.float64(207658.2304002269), <bound method RandomState.normal of RandomState(MT19937) at 0x1D833D56840>)

Do you think this may have been the cause of the problem since it's not a single float? I had set it up like so in the script:

PYTHON
total_impulse=(1.07*(RedMotor.total_impulse), (RedMotor.total_impulse/10)),

type(stochastic_motor.total_impulse) returns that it is a tuple. Could this be the cause of the problem?

No comments yet.

Discussion

No comments yet.