Simple Water Rocket Optimization Example¶
This notebook demonstrates basic usage of the water rocket optimizer to find optimal parameters for maximum altitude.
In [1]:
Copied!
from waterrocketpy.optimization.water_rocket_optimizer import optimize_for_altitude
print("Water Rocket Optimization Example")
print("=" * 40)
from waterrocketpy.optimization.water_rocket_optimizer import optimize_for_altitude
print("Water Rocket Optimization Example")
print("=" * 40)
Water Rocket Optimization Example ========================================
Optimize for Maximum Altitude¶
Run optimization with default parameter bounds to find the rocket configuration that achieves maximum altitude.
In [2]:
Copied!
# Run optimization with default bounds
print("Optimizing for maximum altitude...")
result = optimize_for_altitude(
maxiter=50, # Quick optimization for demo
seed=42 # For reproducible results
)
print("Optimization completed!")
# Run optimization with default bounds
print("Optimizing for maximum altitude...")
result = optimize_for_altitude(
maxiter=50, # Quick optimization for demo
seed=42 # For reproducible results
)
print("Optimization completed!")
Optimizing for maximum altitude... Starting optimization for max_altitude using differential_evolution Parameter bounds: [(0.1, 0.5), (0.05, 0.12), (2.0, 12.0), (0.005, 0.025), (0.1, 0.8)]
New best max_altitude: 4.1604 at evaluation 1 Params: L_body=0.102, d_body=0.065, p_max=2.5bar, nozzle_d=0.0183, water_frac=0.513 New best max_altitude: 103.8004 at evaluation 2 Params: L_body=0.241, d_body=0.113, p_max=11.2bar, nozzle_d=0.0146, water_frac=0.438 New best max_altitude: 123.2071 at evaluation 3 Params: L_body=0.495, d_body=0.090, p_max=11.9bar, nozzle_d=0.0069, water_frac=0.364
New best max_altitude: 123.5837 at evaluation 31 Params: L_body=0.482, d_body=0.106, p_max=10.1bar, nozzle_d=0.0159, water_frac=0.389
New best max_altitude: 127.3809 at evaluation 86 Params: L_body=0.495, d_body=0.118, p_max=10.2bar, nozzle_d=0.0196, water_frac=0.406
differential_evolution step 1: f(x)= -127.38086702551217 New best max_altitude: 137.7603 at evaluation 151 Params: L_body=0.478, d_body=0.109, p_max=12.0bar, nozzle_d=0.0124, water_frac=0.340
New best max_altitude: 138.5028 at evaluation 194 Params: L_body=0.489, d_body=0.115, p_max=11.2bar, nozzle_d=0.0227, water_frac=0.285
differential_evolution step 2: f(x)= -138.5028028381849
New best max_altitude: 140.6326 at evaluation 240 Params: L_body=0.466, d_body=0.114, p_max=11.7bar, nozzle_d=0.0246, water_frac=0.344
differential_evolution step 3: f(x)= -140.63264569280034
differential_evolution step 4: f(x)= -140.63264569280034
differential_evolution step 5: f(x)= -140.63264569280034
New best max_altitude: 141.0108 at evaluation 521 Params: L_body=0.452, d_body=0.120, p_max=12.0bar, nozzle_d=0.0239, water_frac=0.373
differential_evolution step 6: f(x)= -141.0107973655383
New best max_altitude: 142.3291 at evaluation 534 Params: L_body=0.490, d_body=0.113, p_max=11.8bar, nozzle_d=0.0236, water_frac=0.358
New best max_altitude: 142.6553 at evaluation 591 Params: L_body=0.497, d_body=0.110, p_max=12.0bar, nozzle_d=0.0185, water_frac=0.331
differential_evolution step 7: f(x)= -142.65533252544697 New best max_altitude: 145.4394 at evaluation 601 Params: L_body=0.500, d_body=0.117, p_max=12.0bar, nozzle_d=0.0228, water_frac=0.331
differential_evolution step 8: f(x)= -145.4393757513636
New best max_altitude: 145.5610 at evaluation 693 Params: L_body=0.497, d_body=0.118, p_max=12.0bar, nozzle_d=0.0235, water_frac=0.337
differential_evolution step 9: f(x)= -145.56102074027604
differential_evolution step 10: f(x)= -145.56102074027604
differential_evolution step 11: f(x)= -145.56102074027604
differential_evolution step 12: f(x)= -145.56102074027604
differential_evolution step 13: f(x)= -145.56102074027604
differential_evolution step 14: f(x)= -145.56102074027604
differential_evolution step 15: f(x)= -145.56102074027604
differential_evolution step 16: f(x)= -145.56102074027604
differential_evolution step 17: f(x)= -145.56102074027604
differential_evolution step 18: f(x)= -145.56102074027604
New best max_altitude: 145.7171 at evaluation 1471 Params: L_body=0.497, d_body=0.118, p_max=12.0bar, nozzle_d=0.0248, water_frac=0.333
differential_evolution step 19: f(x)= -145.71711930769237
differential_evolution step 20: f(x)= -145.71711930769237
differential_evolution step 21: f(x)= -145.71711930769237
New best max_altitude: 145.8152 at evaluation 1661 Params: L_body=0.499, d_body=0.117, p_max=12.0bar, nozzle_d=0.0241, water_frac=0.327
differential_evolution step 22: f(x)= -145.81516933225515
differential_evolution step 23: f(x)= -145.81516933225515
differential_evolution step 24: f(x)= -145.81516933225515
New best max_altitude: 145.8760 at evaluation 1901 Params: L_body=0.498, d_body=0.119, p_max=12.0bar, nozzle_d=0.0228, water_frac=0.320
New best max_altitude: 146.0248 at evaluation 1948 Params: L_body=0.497, d_body=0.120, p_max=12.0bar, nozzle_d=0.0237, water_frac=0.328 differential_evolution step 25: f(x)= -146.0247567568426
New best max_altitude: 146.1136 at evaluation 1957 Params: L_body=0.500, d_body=0.120, p_max=12.0bar, nozzle_d=0.0232, water_frac=0.324
differential_evolution step 26: f(x)= -146.1136005898963
New best max_altitude: 146.2311 at evaluation 2042 Params: L_body=0.499, d_body=0.120, p_max=12.0bar, nozzle_d=0.0234, water_frac=0.327
differential_evolution step 27: f(x)= -146.23106794785213
differential_evolution step 28: f(x)= -146.23106794785213 Polishing solution with 'L-BFGS-B' New best max_altitude: 146.2311 at evaluation 2177 Params: L_body=0.499, d_body=0.120, p_max=12.0bar, nozzle_d=0.0234, water_frac=0.327
New best max_altitude: 146.2311 at evaluation 2178 Params: L_body=0.499, d_body=0.120, p_max=12.0bar, nozzle_d=0.0234, water_frac=0.327
New best max_altitude: 146.2429 at evaluation 2188 Params: L_body=0.499, d_body=0.120, p_max=12.0bar, nozzle_d=0.0234, water_frac=0.323 New best max_altitude: 146.2429 at evaluation 2189 Params: L_body=0.499, d_body=0.120, p_max=12.0bar, nozzle_d=0.0234, water_frac=0.323
New best max_altitude: 146.2429 at evaluation 2190 Params: L_body=0.499, d_body=0.120, p_max=12.0bar, nozzle_d=0.0234, water_frac=0.323
New best max_altitude: 146.6261 at evaluation 2194 Params: L_body=0.500, d_body=0.120, p_max=12.0bar, nozzle_d=0.0250, water_frac=0.325
New best max_altitude: 146.6261 at evaluation 2200 Params: L_body=0.500, d_body=0.120, p_max=12.0bar, nozzle_d=0.0250, water_frac=0.325
New best max_altitude: 146.6261 at evaluation 2205 Params: L_body=0.500, d_body=0.120, p_max=12.0bar, nozzle_d=0.0250, water_frac=0.325
Optimization completed! Best max_altitude: 146.6261 Best parameters: L_body: 0.5000 d_body: 0.1200 p_max_bar: 12.0000 nozzle_diameter: 0.0250 water_fraction: 0.3246 Optimization completed!
Display Results¶
Show the optimization results including optimal parameters and performance metrics.
In [3]:
Copied!
# Display results
print(f"Success: {result['success']}")
print(f"Max altitude achieved: {result['best_value']:.2f} m")
print(f"Function evaluations: {result['n_evaluations']}")
print("\nOptimal parameters:")
params = result['best_params']
print(f" Body length: {params['L_body']:.3f} m ({params['L_body']*100:.1f} cm)")
print(f" Body diameter: {params['d_body']:.4f} m ({params['d_body']*1000:.1f} mm)")
print(f" Max pressure: {params['p_max_bar']:.1f} bar")
print(f" Nozzle diameter: {params['nozzle_diameter']:.4f} m ({params['nozzle_diameter']*1000:.1f} mm)")
print(f" Water fraction: {params['water_fraction']:.1%}")
# Display results
print(f"Success: {result['success']}")
print(f"Max altitude achieved: {result['best_value']:.2f} m")
print(f"Function evaluations: {result['n_evaluations']}")
print("\nOptimal parameters:")
params = result['best_params']
print(f" Body length: {params['L_body']:.3f} m ({params['L_body']*100:.1f} cm)")
print(f" Body diameter: {params['d_body']:.4f} m ({params['d_body']*1000:.1f} mm)")
print(f" Max pressure: {params['p_max_bar']:.1f} bar")
print(f" Nozzle diameter: {params['nozzle_diameter']:.4f} m ({params['nozzle_diameter']*1000:.1f} mm)")
print(f" Water fraction: {params['water_fraction']:.1%}")
Success: True Max altitude achieved: 146.63 m Function evaluations: 2211 Optimal parameters: Body length: 0.500 m (50.0 cm) Body diameter: 0.1200 m (120.0 mm) Max pressure: 12.0 bar Nozzle diameter: 0.0250 m (25.0 mm) Water fraction: 32.5%
Flight Performance Summary¶
Display detailed flight performance metrics for the optimized rocket.
In [4]:
Copied!
if result["best_flight_data"] is not None:
fd = result["best_flight_data"]
print("Flight Performance:")
print(f" Max Altitude : {fd.max_altitude:.2f} m")
print(f" Max Velocity : {fd.max_velocity:.2f} m/s")
print(f" Flight Time : {fd.flight_time:.2f} s")
print(f" Water Depletion Time: {fd.water_depletion_time:.2f} s")
if result["best_flight_data"] is not None:
fd = result["best_flight_data"]
print("Flight Performance:")
print(f" Max Altitude : {fd.max_altitude:.2f} m")
print(f" Max Velocity : {fd.max_velocity:.2f} m/s")
print(f" Flight Time : {fd.flight_time:.2f} s")
print(f" Water Depletion Time: {fd.water_depletion_time:.2f} s")
Flight Performance: Max Altitude : 146.63 m Max Velocity : 60.47 m/s Flight Time : 11.03 s Water Depletion Time: 0.10 s