Skip to main content
Version: 0.2.0

Gradients

Bora can return the derivative of every output with respect to all 23 inputs. The derivatives are analytic: they are propagated through the network and the input and output transformations by hand-written differentiation code, not estimated by finite differences. They are exact to rounding error.

NeuralFoil's Python implementation leaves differentiation to an external automatic-differentiation tool (Sharpe, 2023). Having the derivatives built in, with no extra dependency, is the main thing Bora adds to the model.

Layout​

A gradient is a fixed-size array with one entry per input:

pub const NUM_INPUTS: usize = 23;
pub type Gradient = [f64; NUM_INPUTS];

The constants in bora::input_index name the entries.

IndexConstantInputDerivative is per
0–7UPPER_WEIGHTS + kUpper-surface weight kunit weight
8–15LOWER_WEIGHTS + kLower-surface weight kunit weight
16LEADING_EDGE_WEIGHTLeading-edge weightunit weight
17TE_THICKNESSTrailing-edge thicknesschord length
18ALPHAAngle of attackdegree
19REReynolds numberunit Reynolds number
20N_CRITCritical amplification factorunit
21XTR_UPPERForced transition, upperchord fraction
22XTR_LOWERForced transition, lowerchord fraction

The gradient types mirror the output types field for field, so the derivative of an output lives under the same name:

use bora::input_index as idx;

let result = model.analyze_with_gradients(&input);

// Lift-curve slope, per degree.
let cl_alpha = result.gradient.cl[idx::ALPHA];

// Sensitivity of drag to the third upper-surface weight.
let dcd_dw = result.gradient.cd[idx::UPPER_WEIGHTS + 2];

// Sensitivity of the last upper-surface momentum thickness to Reynolds number.
let dtheta_dre = result.gradient.upper.theta[bora::NUM_BL_POINTS - 1][idx::RE];

:::info Units Derivatives with respect to alpha are per degree, not per radian. Derivatives with respect to re are per unit of Reynolds number and therefore very small numbers; scale your design variables accordingly if you optimize over Reynolds number. :::

Two gradient calls​

analyze_with_gradientsanalyze_scalars_with_gradients
Outputs differentiatedAll 198The 6 scalars
Differentiation modeForward: one pass per inputReverse: one pass per output
ReturnsAeroResultAeroScalarResult
Time, NeuralFoil xlarge network322 µs72 µs

The cost of forward-mode differentiation grows with the number of inputs and that of reverse mode with the number of outputs. With 23 inputs, 198 outputs in total and 6 scalar outputs, reverse mode is the cheaper way to differentiate the scalars and forward mode the cheaper way to differentiate everything.

Use the scalar call unless you need boundary-layer derivatives. An objective or constraint built from lift, drag, moment, transition location or confidence needs nothing else, and the scalar call is four to eight times faster depending on the network size (see Choosing a network size). The field names are the same, so switching is a one-line change:

let result = model.analyze_scalars_with_gradients(&input);
let dcd_dalpha = result.gradient.cd[idx::ALPHA];

The scalar values from both calls are bitwise identical to analyze. The gradients from the two calls agree to rounding, not bitwise, because the same sums are accumulated in a different order; across the test suite the difference stays below 10⁻¹³ of each gradient's largest entry.

Directional derivatives​

When you need the derivative along one direction only, for example ∂CL/∂α\partial C_L / \partial \alpha inside a Newton iteration on lift, analyze_with_tangent(&input, &direction) is far cheaper than either gradient call. direction is a Gradient in the input space of the table above, and the returned AeroTangent holds output, bitwise identical to analyze, and tangent, an AeroOutput whose every field is the directional derivative of the matching output:

tangent.X=∑kgradient.X[k]  direction[k].\texttt{tangent.}X = \sum_k \texttt{gradient.}X[k]\;\texttt{direction}[k] .
let mut direction = [0.0; bora::NUM_INPUTS];
direction[idx::ALPHA] = 1.0;

let result = model.analyze_with_tangent(&input, &direction);
let (cl, dcl_dalpha) = (result.output.cl, result.tangent.cl);

It carries one direction through the network alongside the values, forward mode with a single seed, and it covers the boundary-layer outputs too.

Time per call in microseconds (Apple M1 Pro, one thread), with the cost relative to analyze in parentheses.

ModelSizeanalyzeanalyze_with_tangentanalyze_with_gradients
XXSmall4.99.0 (1.8×)56 (11×)
Medium9.217.6 (1.9×)104 (11×)
XLarge2754 (2.0×)321 (12×)
XXXLarge358701 (2.0×)4,470 (12×)

The tangent agrees with the contracted result of analyze_with_gradients to rounding. To differentiate the six scalar outputs with respect to all inputs, one analyze_scalars_with_gradients call is cheaper than 23 tangents.

Transition locations​

The two transition outputs are clipped to the interval from 0 to 1. Where the clip is active, typically a surface that stays laminar to the trailing edge and reports exactly 1, the output does not change with the inputs and its gradient is exactly zero. An optimizer that constrains a transition location should expect a flat region there.

How the gradients are verified​

Independent checks run in the test suite:

  1. Against Jacobians of Python NeuralFoil computed with CasADi, to a relative tolerance of 10⁻⁶ (tests/reference_gradients.rs).
  2. Against central finite differences of Bora's own outputs (tests/gradients.rs).
  3. The reverse-mode scalar gradients against the forward-mode ones (tests/scalars.rs).
  4. The tangents against the full gradients contracted with the same direction (tests/tangent.rs).

Using the gradients in an optimizer​

When driving Bora from Rust, the pattern is to build the objective from the outputs and assemble its gradient from the corresponding rows by the chain rule:

let result = model.analyze_scalars_with_gradients(&input);
let aero = result.output;
let g = &result.gradient;

// Minimize drag with a quadratic penalty on missing a lift target.
let cl_error = aero.cl - CL_TARGET;
let objective = aero.cd + WEIGHT * cl_error * cl_error;

let gradient: [f64; bora::NUM_INPUTS] =
std::array::from_fn(|k| g.cd[k] + 2.0 * WEIGHT * cl_error * g.cl[k]);