Skip to main content
Version: 0.4.0

Compute gradients

After a successful solve, ComputeSensitivities() returns the derivatives of , , and with respect to every panel-node coordinate and the three operating conditions. One call gives you all of them.

scirocco::Scirocco foil("2412", 160);
foil.SetOper(4.0, -1.0, 1.0e6, 0.0);
if (!foil.Solve()) return 1;

const scirocco::Sensitivity s = foil.ComputeSensitivities();
std::printf("dCl/dalpha = %.6f per deg\n", s.dcl_dalpha);
std::printf("dCd/dalpha = %.6f per deg\n", s.dcd_dalpha);
std::printf("dCd/dRe = %.4e\n", s.dcd_dRe);
std::printf("dCd/dx is %ld x %ld\n", (long)s.dcd_dx.rows(), (long)s.dcd_dx.cols());
dCl/dalpha = 0.092430 per deg
dCd/dalpha = 0.000748 per deg
dCd/dRe = -1.3061e-09
dCd/dx is 2 x 161

A central finite difference of in , with a step of °, gives 0.000748 as well.

What you get​

FieldShapeDerivative of
dcl_dx, dcd_dx, dcm_dx2 × NEach output with respect to each node's x (row 0) and z (row 1)
dcl_dalpha, dcd_dalpha, dcm_dalphascalarPer degree of angle of attack
dcl_dRe, dcd_dRe, dcm_dRescalarPer unit Reynolds number
dcl_dMa, dcd_dMa, dcm_dMascalarPer unit Mach number

Node order matches GetFoilCoords(). The same values are available from getters such as foil.GetDcdDx() and foil.GetDclDalpha() until the next solve.

Chain them to your design variables​

The coordinate gradients are with respect to panel nodes. If your design variables are something else, say CST coefficients or a thickness scale, multiply by the Jacobian of node coordinates with respect to those variables:

An MDO framework does this for you when the geometry is its own discipline; Check the gradients shows it with OpenMDAO.

Lift-target mode​

With a lift target (cl ≥ 0 in SetOper), the derivatives include the change in angle of attack needed to keep on target. dcl_dx is then zero and extra fields describe the trim:

FieldMeaning
dcd_dcltarget, dcm_dcltargetChange per unit change of the target
dalpha_dx, dalpha_dRe, dalpha_dMaHow the solved angle moves

Cost​

The adjoint solves one linear system per output, so the cost does not grow with the number of design variables. A finite-difference gradient needs one extra solve per input: 2N + 3 of them.

On an Apple M1 Pro, the full gradient takes about 9 ms at 99 panels and 18 ms at 149 panels, less than the viscous solve before it. Finite differences over the same inputs would take about 2.5 s and 7 s. See Performance for the full table.

Where the shape gradient is rough​

On the same case, the drag gradient along the upper surface is smooth except for a few panels near the leading edge, where it jumps and alternates in sign:

-0.500.5100.030.060.090.12x/c, upper surfacedCd/dn
Show data
dCd/dn
x/c, upper surfacedCd/dn
0.0001-0.0038
0.0005-0.0041
0.0012-0.0041
0.0023-0.0051
0.0039-0.0093
0.00630.0009
0.0096-0.0155
0.0140-0.1846
0.0197-0.1957
0.02671.0696
0.0349-0.2059
0.0443-0.6834
0.05450.2035
0.06540.0215
0.07700.0074
0.08900.0018
0.10130.0000
0.11400.0001
00.511.5200.030.060.090.12x/c, upper surfaceñ
NACA 2412, α = 4°, Re = 10⁶, 160 panels. Left: derivative of Cd with respect to moving each node along its outward normal. Right: the amplification factor ñ, which is zero up to x/c ≈ 0.027 and then starts to grow; it reaches ncrit = 9 near x/c ≈ 0.39. The gradient spikes and alternates in sign where growth switches on. Complex step through mfoil gives the same values to about 10⁻¹¹.
Show data
ñ
x/c, upper surfaceñ
0.00010.000
0.00050.000
0.00120.000
0.00230.000
0.00390.000
0.00630.000
0.00960.000
0.01400.000
0.01970.000
0.02670.000
0.03490.019
0.04430.147
0.05450.388
0.06540.650
0.07700.911
0.08901.170
0.10131.426
0.11401.680

This is the exact derivative of the discrete model, not an adjoint error. Complex step through mfoil gives the same values at every node, and the test suite checks this case. The spike sits where the amplification factor starts to grow. The model switches growth on over a narrow band of , so near that point the growth rate is very sensitive to the local boundary-layer state. Moving one node there changes how much the disturbance grows downstream, and so where transition happens at .

The spike comes from how sharply the model switches growth on, not from the panel count: refining the paneling moves it but does not smooth it away. Other airfoils and operating points put the onset elsewhere. For gradient-based shape optimization:

  • Parameterize the shape with smooth design variables, such as CST coefficients. A smooth parameterization averages the gradient over many nodes, so single-node spikes don't dominate it.
  • Don't read node-by-node gradients near the onset as physical design guidance.

Rules​

  • Solve first. Gradients are taken at the converged state. A failed solve, or a call before any solve, throws.
  • Don't touch the state in between. A new solve, a new operating point, a geometry change, or the non-const GetState() clears cached gradients.
  • Positions, not shape. Moving a node doesn't change the reference chord or the moment reference point; they are held fixed.

The derivatives are exact for the discretized equations, to the solver's tolerance. Every piece of the chain is tested against complex-step reference data from mfoil. The adjoint explains the method.