Quickstart¶
This runs a simulation of the sample rocket that comes with orhelper and prints some results. It needs only orhelper and OpenRocket (see Installation).
examples/quickstart.py
"""Minimal orhelper example: run a simulation and print a few results.
Needs only orhelper (and its numpy dependency) plus an OpenRocket install.
"""
import numpy as np
import orhelper
from orhelper import FlightDataType
# The JVM can only be started once per Python process, so keep everything that
# talks to OpenRocket inside this `with` block.
with orhelper.OpenRocketInstance(log_level="ERROR") as instance:
orh = orhelper.Helper(instance)
doc = orh.load_doc(orhelper.sample_ork_path()) # or "/path/to/your.ork"
sim = doc.getSimulation(0)
orh.run_simulation(sim)
data = orh.get_timeseries(sim, [FlightDataType.TYPE_TIME,
FlightDataType.TYPE_ALTITUDE,
FlightDataType.TYPE_VELOCITY_TOTAL])
events = orh.get_events(sim)
print(f"Rocket: {doc.getRocket().getName()}")
print(f"Apogee: {np.max(data[FlightDataType.TYPE_ALTITUDE]):.0f} m")
# Some series start with NaN (no velocity at t=0), so use nanmax, not max.
print(f"Max velocity: {np.nanmax(data[FlightDataType.TYPE_VELOCITY_TOTAL]):.0f} m/s")
print(f"Flight time: {data[FlightDataType.TYPE_TIME][-1]:.1f} s")
for event, times in events.items():
print(f" {event.name:<28} t = {times[0]:.1f} s")
Run it, and you get something like this (the numbers vary slightly from run to run):
Rocket: A simple model rocket
Apogee: 51 m
Max velocity: 29 m/s
Flight time: 16.0 s
LAUNCH t = 0.0 s
IGNITION t = 0.0 s
LIFTOFF t = 0.1 s
LAUNCHROD t = 0.2 s
BURNOUT t = 0.7 s
APOGEE t = 3.5 s
EJECTION_CHARGE t = 3.7 s
RECOVERY_DEVICE_DEPLOYMENT t = 3.7 s
GROUND_HIT t = 16.0 s
SIMULATION_END t = 16.0 s
What the script does¶
with orhelper.OpenRocketInstance(...) as instance:starts OpenRocket. Everything that uses OpenRocket goes inside this block.log_level="ERROR"hides OpenRocket's own, very chatty, logging.orh.load_doc(...)loads a.orkfile. Useorhelper.sample_ork_path()for the sample, or the path of your own file. You get a JavaOpenRocketDocument.doc.getSimulation(0)is the first simulation stored in the document. This is a Java call, as aregetRocket()andgetName(): see How orhelper works.orh.run_simulation(sim)runs the simulation.orh.get_timeseries(...)returns the values of the variables you ask for as NumPy arrays, one value per time step.orh.get_events(sim)returns when things like burnout and apogee happened.
np.nanmax, not max
Some series start with nan, for example velocity at the first time step, so max() would return
nan. Use numpy.nanmax and numpy.nanmin for statistics.
Next¶
- Running simulations: everything you can read from the results.
- Changing the rocket and launch: wind, launch angle, component masses.
- Examples: scripts for parameter sweeps, multistage rockets, Monte Carlo studies and more.