Skip to main content

Module custom_models

Module custom_models 

Source
Expand description

A drag model, a wind or an atmosphere of your own, flown in hpr’s place.

Three of hpr’s models are traits a program can implement:

TraitWhat it givesWhere it goes
DragModelthe rocket’s zero-lift drag coefficient at a flowFlightBuilder::drag_model
Windthe wind’s velocity at a heightEnvironment::with_wind
Atmospherethe air’s pressure, temperature, density, speed of sound and viscosity at a heightEnvironment::with_atmosphere

A drag model replaces the zero-lift drag only, as a drag table from another tool does. The flight still scales it for the angle of attack, and the normal force, center of pressure, roll and damping stay hpr’s, so the margin a rocket reports doesn’t change. A model is asked a DragQuery: the Mach number and angles, the Reynolds number, whether a motor burns, and hpr’s own drag at that flow (DragQuery::buildup), so a model can adjust hpr’s number instead of replacing it. The coefficient is on the rocket’s reference area, by default a circle of its largest body diameter, and unlike a drag table’s it isn’t rescaled: a curve measured on another area is converted before it is returned (DragQuery::reference_area_m2).

use hpr::hpr_aero::{AeroError, DragModel, DragQuery};
use hpr::{Environment, Flight};

/// hpr's own drag below Mach 0.5, and 0.6 from there: a made-up rule, to show the idea. It
/// jumps at Mach 0.5, which a real model would smooth.
#[derive(Debug)]
struct Mine;

impl DragModel for Mine {
    fn zero_lift_drag(&self, query: &DragQuery<'_>) -> Result<f64, AeroError> {
        if query.mach() < 0.5 {
            Ok(query.buildup()?.zero_lift_coefficient)
        } else {
            Ok(0.6)
        }
    }
}

let environment = Environment::new(32.99, -106.97, 1400.0)?;
let flight = Flight::builder(&rocket, &environment, 1.8)
    .drag_model(Mine)
    .fly()?;
assert!(flight.max_mach().is_some_and(|mach| mach > 0.5));

How far to trust it: as far as the model, and no further than hpr’s other models, which still fly the rest of the rocket and are not yet validated against real flights. hpr refuses a drag coefficient that is negative or not finite; it can’t know whether a model is right. A model is asked many times a step, so keep it quick, and give the same answer to the same question: a flight is only as repeatable as its models.

hpr refuses a wind velocity that isn’t finite wherever it reads the wind, climbing or under a canopy, with a SimError::Domain that names the wind and gives the height above sea level, m. It refuses air it can’t use the same way, naming the field: a density or pressure that is negative or not finite, or a temperature, speed of sound or viscosity that is zero, negative or not finite (issue #301, the report that asked for it). A zero density or pressure is taken, as in a vacuum: the standard atmosphere’s own pressure and density shrink to zero far above its 86 km top.

The site’s page Models of your own runs the custom_drag and custom_wind examples.