|
| 1 | +Flight Comparator |
| 2 | +================= |
| 3 | + |
| 4 | +This example demonstrates how to use the RocketPy ``FlightComparator`` class to |
| 5 | +compare a Flight simulation against external data sources. |
| 6 | + |
| 7 | +Users must explicitly create a `FlightComparator` instance. |
| 8 | + |
| 9 | + |
| 10 | +This class is designed to compare a RocketPy Flight simulation against external |
| 11 | +data sources, such as: |
| 12 | + |
| 13 | +- Real flight data (avionics logs, altimeter CSVs) |
| 14 | +- Simulations from other software (OpenRocket, RASAero) |
| 15 | +- Theoretical models or manual calculations |
| 16 | + |
| 17 | +Unlike ``CompareFlights`` (which compares multiple RocketPy simulations), |
| 18 | +``FlightComparator`` specifically handles the challenge of aligning different |
| 19 | +time steps and calculating error metrics (RMSE, MAE, etc.) between a |
| 20 | +"Reference" simulation and "External" data. |
| 21 | + |
| 22 | +Importing classes |
| 23 | +----------------- |
| 24 | + |
| 25 | +We will start by importing the necessary classes and modules: |
| 26 | + |
| 27 | +.. jupyter-execute:: |
| 28 | + |
| 29 | + import numpy as np |
| 30 | + |
| 31 | + from rocketpy import Environment, Rocket, SolidMotor, Flight |
| 32 | + from rocketpy.simulation import FlightComparator, FlightDataImporter |
| 33 | + |
| 34 | +Create Simulation (Reference) |
| 35 | +----------------------------- |
| 36 | + |
| 37 | +First, let's create the standard RocketPy simulation that will serve as our |
| 38 | +"Ground Truth" or reference model. This follows the standard setup. |
| 39 | + |
| 40 | +.. jupyter-execute:: |
| 41 | + |
| 42 | + # 1. Setup Environment |
| 43 | + env = Environment( |
| 44 | + date=(2022, 10, 1, 12), |
| 45 | + latitude=32.990254, |
| 46 | + longitude=-106.974998, |
| 47 | + elevation=1400, |
| 48 | + ) |
| 49 | + env.set_atmospheric_model(type="standard_atmosphere") |
| 50 | + |
| 51 | + # 2. Setup Motor |
| 52 | + Pro75M1670 = SolidMotor( |
| 53 | + thrust_source="../data/motors/cesaroni/Cesaroni_M1670.eng", |
| 54 | + burn_time=3.9, |
| 55 | + grain_number=5, |
| 56 | + grain_density=1815, |
| 57 | + grain_outer_radius=33 / 1000, |
| 58 | + grain_initial_inner_radius=15 / 1000, |
| 59 | + grain_initial_height=120 / 1000, |
| 60 | + grain_separation=5 / 1000, |
| 61 | + nozzle_radius=33 / 1000, |
| 62 | + throat_radius=11 / 1000, |
| 63 | + interpolation_method="linear", |
| 64 | + coordinate_system_orientation="nozzle_to_combustion_chamber", |
| 65 | + dry_mass=1.815, |
| 66 | + dry_inertia=(0.125, 0.125, 0.002), |
| 67 | + grains_center_of_mass_position=0.33, |
| 68 | + center_of_dry_mass_position=0.317, |
| 69 | + nozzle_position=0, |
| 70 | + ) |
| 71 | + |
| 72 | + # 3. Setup Rocket |
| 73 | + calisto = Rocket( |
| 74 | + radius=127 / 2000, |
| 75 | + mass=19.197 - 2.956, |
| 76 | + inertia=(6.321, 6.321, 0.034), |
| 77 | + power_off_drag="../data/calisto/powerOffDragCurve.csv", |
| 78 | + power_on_drag="../data/calisto/powerOnDragCurve.csv", |
| 79 | + center_of_mass_without_motor=0, |
| 80 | + coordinate_system_orientation="tail_to_nose", |
| 81 | + ) |
| 82 | + |
| 83 | + calisto.set_rail_buttons(0.0818, -0.618, 45) |
| 84 | + calisto.add_motor(Pro75M1670, position=-1.255) |
| 85 | + |
| 86 | + # Add aerodynamic surfaces |
| 87 | + nosecone = calisto.add_nose(length=0.55829, kind="vonKarman", position=0.71971) |
| 88 | + fin_set = calisto.add_trapezoidal_fins( |
| 89 | + n=4, |
| 90 | + root_chord=0.120, |
| 91 | + tip_chord=0.040, |
| 92 | + span=0.100, |
| 93 | + position=-1.04956, |
| 94 | + cant_angle=0.5, |
| 95 | + airfoil=("../data/calisto/fins/NACA0012-radians.txt", "radians"), |
| 96 | + ) |
| 97 | + tail = calisto.add_tail( |
| 98 | + top_radius=0.0635, |
| 99 | + bottom_radius=0.0435, |
| 100 | + length=0.060, |
| 101 | + position=-1.194656, |
| 102 | + ) |
| 103 | + |
| 104 | + # 4. Simulate |
| 105 | + flight = Flight( |
| 106 | + rocket=calisto, |
| 107 | + environment=env, |
| 108 | + rail_length=5.2, |
| 109 | + inclination=85, |
| 110 | + heading=0, |
| 111 | + ) |
| 112 | + |
| 113 | + # 5. Create FlightComparator instance |
| 114 | + comparator = FlightComparator(flight) |
| 115 | + |
| 116 | +Adding Another Flight Object |
| 117 | +---------------------------- |
| 118 | + |
| 119 | +You can compare against another RocketPy Flight simulation directly: |
| 120 | + |
| 121 | +.. jupyter-execute:: |
| 122 | + |
| 123 | + # Create a second simulation with slightly different parameters |
| 124 | + flight2 = Flight( |
| 125 | + rocket=calisto, |
| 126 | + environment=env, |
| 127 | + rail_length=5.0, # Slightly shorter rail |
| 128 | + inclination=85, |
| 129 | + heading=0, |
| 130 | + ) |
| 131 | + |
| 132 | + # Add the second flight directly |
| 133 | + comparator.add_data("Alternative Sim", flight2) |
| 134 | + |
| 135 | + print(f"Added variables from flight2: {list(comparator.data_sources['Alternative Sim'].keys())}") |
| 136 | + |
| 137 | +Importing External Data (dict) |
| 138 | +------------------------------ |
| 139 | + |
| 140 | +The primary data format expected by ``FlightComparator.add_data`` is a dictionary |
| 141 | +where keys are variable names (e.g. ``"z"``, ``"vz"``, ``"altitude"``) and values |
| 142 | +are either: |
| 143 | + |
| 144 | +- A RocketPy ``Function`` object, or |
| 145 | +- A tuple of ``(time_array, data_array)``. |
| 146 | + |
| 147 | +Let's create some synthetic external data to compare against our reference |
| 148 | +simulation: |
| 149 | + |
| 150 | +.. jupyter-execute:: |
| 151 | + |
| 152 | + # Generate synthetic external data with realistic noise |
| 153 | + time_external = np.linspace(0, flight.t_final, 80) # Different time steps |
| 154 | + external_altitude = flight.z(time_external) + np.random.normal(0, 3, 80) # 3m noise |
| 155 | + external_velocity = flight.vz(time_external) + np.random.normal(0, 0.5, 80) # 0.5 m/s noise |
| 156 | + |
| 157 | + # Add the external data to our comparator |
| 158 | + comparator.add_data( |
| 159 | + "External Simulator", |
| 160 | + { |
| 161 | + "altitude": (time_external, external_altitude), |
| 162 | + "vz": (time_external, external_velocity), |
| 163 | + } |
| 164 | + ) |
| 165 | + |
| 166 | +Running Comparisons |
| 167 | +------------------- |
| 168 | + |
| 169 | +Now we can run the various comparison methods: |
| 170 | + |
| 171 | +.. jupyter-execute:: |
| 172 | + |
| 173 | + # Generate summary with key events |
| 174 | + comparator.summary() |
| 175 | + |
| 176 | + # Compare specific variable |
| 177 | + comparator.compare("altitude") |
| 178 | + |
| 179 | + # Compare all available variables |
| 180 | + comparator.all() |
| 181 | + |
| 182 | + # Plot 2D trajectory |
| 183 | + comparator.trajectories_2d(plane="xz") |
0 commit comments