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
| Field | Shape | Derivative of |
|---|---|---|
dcl_dx, dcd_dx, dcm_dx | 2 × N | Each output with respect to each node's x (row 0) and z (row 1) |
dcl_dalpha, dcd_dalpha, dcm_dalpha | scalar | Per degree of angle of attack |
dcl_dRe, dcd_dRe, dcm_dRe | scalar | Per unit Reynolds number |
dcl_dMa, dcd_dMa, dcm_dMa | scalar | Per 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:
| Field | Meaning |
|---|---|
dcd_dcltarget, dcm_dcltarget | Change per unit change of the target |
dalpha_dx, dalpha_dRe, dalpha_dMa | How 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:
Show data
| x/c, upper surface | dCd/dn |
|---|---|
| 0.0001 | -0.0038 |
| 0.0005 | -0.0041 |
| 0.0012 | -0.0041 |
| 0.0023 | -0.0051 |
| 0.0039 | -0.0093 |
| 0.0063 | 0.0009 |
| 0.0096 | -0.0155 |
| 0.0140 | -0.1846 |
| 0.0197 | -0.1957 |
| 0.0267 | 1.0696 |
| 0.0349 | -0.2059 |
| 0.0443 | -0.6834 |
| 0.0545 | 0.2035 |
| 0.0654 | 0.0215 |
| 0.0770 | 0.0074 |
| 0.0890 | 0.0018 |
| 0.1013 | 0.0000 |
| 0.1140 | 0.0001 |
Show data
| x/c, upper surface | ñ |
|---|---|
| 0.0001 | 0.000 |
| 0.0005 | 0.000 |
| 0.0012 | 0.000 |
| 0.0023 | 0.000 |
| 0.0039 | 0.000 |
| 0.0063 | 0.000 |
| 0.0096 | 0.000 |
| 0.0140 | 0.000 |
| 0.0197 | 0.000 |
| 0.0267 | 0.000 |
| 0.0349 | 0.019 |
| 0.0443 | 0.147 |
| 0.0545 | 0.388 |
| 0.0654 | 0.650 |
| 0.0770 | 0.911 |
| 0.0890 | 1.170 |
| 0.1013 | 1.426 |
| 0.1140 | 1.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.