What we'll build up to, one small step at a time:
  1. Why we need derivatives at all (the Newton Jacobian)
  2. The dual number: a value that carries its own slope
  3. ForwardDiff's Dual{Tag, V, N}
  4. What Tag is, and why it exists
  5. IGT's analytic shortcut for a normal gas
  6. The new twist: a gas that carries a derivative
  7. The collision — the missing rule, traced live
  8. What the rule should return (the total derivative)
  9. Two fixes: the shim, and the clean upstream method
  10. The wishlist, now earned
§1

Why we need derivatives at all

PowerCycles solves the turbojet by Newton's method: it has a vector of unknowns $u$ (the station temperatures, pressures, FAR, mass flows…) and a residual function $r(u)$ that is zero at the solution. Newton repeatedly solves

$$J\,\Delta u = -r(u), \qquad J_{ij} = \frac{\partial r_i}{\partial u_j}$$

so it needs the Jacobian $J$ — every partial derivative of every residual row with respect to every unknown. We could approximate each $\partial r_i/\partial u_j$ with a finite difference (nudge $u_j$, re-evaluate, divide), but that is slow and inexact. Instead PowerCycles uses forward-mode automatic differentiation (the ForwardDiff package): it computes these partials exactly, to machine precision, in essentially one evaluation. The rest of this page is about how that machinery works — and the one spot where IdealGasThermo's piece of it has a gap.

§2

The dual number — a value that carries its own slope

Here is the whole idea in one line. Invent a tiny symbol $\varepsilon$ with the single rule

$$\varepsilon^2 = 0 \qquad(\varepsilon \neq 0).$$

A dual number is then $a + b\,\varepsilon$ — an ordinary value $a$ paired with a second slot $b$. Now watch what happens when you feed $x = a + \varepsilon$ (so $b=1$) through a function. Take $f(x) = x^2$:

$$f(a+\varepsilon) = (a+\varepsilon)^2 = a^2 + 2a\,\varepsilon + \underbrace{\varepsilon^2}_{=\,0} = \underbrace{a^2}_{\text{value}} + \underbrace{2a}_{\text{slope}}\,\varepsilon.$$

The $\varepsilon$-slot came out holding exactly $2a = f'(a)$. We never wrote down the derivative rule for squaring — it fell out of the arithmetic. That is forward-mode AD: carry $(\text{value},\ \text{slope})$ side by side through every operation, and the slope of the final answer is the derivative. Each elementary operation just needs to know how to update both slots; for example multiplication is

$$(a + b\varepsilon)(c + d\varepsilon) = ac + (ad + bc)\,\varepsilon \quad\;(\text{the } \varepsilon^2 \text{ term vanishes}),$$

which is just the product rule $\,(uv)' = u'v + uv'\,$ hiding in the $\varepsilon$-slot.

The two pieces have names

In ForwardDiff, the value $a$ is the value and the slope $b$ is the partials. A dual number prints as Dual(value, partials). Keep these two words in mind — every rule below is just "what is the value, and what is the partials."

§3

ForwardDiff's Dual{Tag, V, N}

Real problems differentiate with respect to several variables at once, so ForwardDiff generalizes the single $\varepsilon$ to $N$ of them — one per variable you are differentiating with respect to. A dual number's full type is

$$\texttt{Dual\{Tag,\ V,\ N\}} \;=\; \underbrace{\text{value of type } V}_{\text{the number itself}} \;+\; \underbrace{N \text{ partials}}_{\text{one slope per variable}}.$$

To ask "how does the answer change as variable #1 moves," you seed variable #1 with a partials-slot of $1$ and all others $0$. For a single variable, you seed it with $1$. Concretely, to differentiate with respect to FAR we hand the code FAR as a dual whose value is the current FAR and whose partials is $1$:

  # FAR = 0.03, seed ∂/∂FAR = 1   (one partial, so N = 1)
  FARd = ForwardDiff.Dual{:demo}(0.03, 1.0)
  # type is Dual{:demo, Float64, 1}

Run any function of FARd and read its partials — that is the derivative with respect to FAR. In jacobian!, PowerCycles seeds all the unknowns this way in one pass, then reads the partials off each residual row to fill in the Jacobian. (We will mostly use one variable at a time below to keep the arithmetic visible.)

But notice the first type parameter, :demo. That is the Tag, and it is the hinge of this whole story — so it gets its own section.

§4

What Tag is, and why it exists

The Tag is a label — a type-level name — that says which differentiation pass this dual number belongs to. Every dual created in one call to ForwardDiff.gradient (one Jacobian evaluation) shares the same tag; they are siblings in the same pass.

Why bother labelling them? Because sometimes you differentiate a function that itself differentiates something — a derivative of a derivative. Then you have two independent $\varepsilon$'s in flight at once, an inner one and an outer one, and if you mix them up you silently get the wrong answer. This classic bug even has a name: perturbation confusion. The tag is the fix: the outer pass uses one tag, the inner pass a different tag, and the arithmetic keeps them apart.

the rule of thumb that matters here

A dual number's value slot is supposed to be an ordinary number (or a dual from a different, inner tag — that is a legitimate second derivative). What should never happen is a Dual{Tag} whose value slot is itself a Dual{Tag} with the same tag. That is not a second derivative — it is one pass accidentally wrapping itself twice. It is malformed, and downstream code that expects a plain number in the value slot will break. Hold onto this — it is exactly what goes wrong in §7.

§5

IGT's analytic shortcut for a normal gas

Now to the thermodynamics. IdealGasThermo computes enthalpy $h(T)$ from a temperature polynomial. ForwardDiff would happily push a dual $T$ through that polynomial term by term — but IGT ships a faster analytic shortcut. It knows the textbook fact

$$\frac{dh}{dT} = c_p(T),$$

so instead of running the polynomial in dual arithmetic it just writes the answer down directly: the value is $h$ at the plain temperature, and the partials is $c_p$ times the incoming temperature-slope. Here is the real code — three lines from IGT's ForwardDiff extension:

32# cp, h, s0 of a gas at a Dual temperature — write the partials analytically
32cp(gas::FrozenGas, d::Dual{Tag}) where {Tag} =
33    Dual{Tag}(cp(gas, value(d)), cp_dT(gas, value(d)) * partials(d))
35h(gas::FrozenGas, d::Dual{Tag}) where {Tag} =
36    Dual{Tag}(h(gas, value(d)), cp(gas, value(d)) * partials(d))
38s0(gas::FrozenGas, d::Dual{Tag}) where {Tag} =
39    Dual{Tag}(s0(gas, value(d)), cp(gas, value(d)) / value(d) * partials(d))

~/.julia/packages/IdealGasThermo/.../ext/IdealGasThermoForwardDiffExt.jl, lines 32–39

Read the h rule (line 35) slowly against the math, because everything hinges on it:

the math

$$h(T+\dot T\varepsilon) = \underbrace{h(T)}_{\text{value}} + \underbrace{c_p(T)\,\dot T}_{\text{partials}}\,\varepsilon$$

the code (line 35)
# value      partials
Dual{Tag}( h(gas,value(d)) , cp(gas,value(d))*partials(d) )

value(d) strips the slope off the temperature to get the plain $T$; partials(d) is the incoming $\dot T$. The rule's two slots are exactly $h(T)$ and $c_p(T)\dot T$.

The hidden assumption. Look at what type h(gas, value(d)) has. For a normal gas — one whose polynomial coefficients are ordinary numbers — value(d) is a plain Float64, so h(gas, value(d)) is a plain Float64. The value slot is a plain number, exactly as §4 demands. This rule was written assuming the gas is constant — its coefficients do not carry any derivative. That assumption is about to be broken.
§6

The new twist — a gas that carries a derivative

In the TPG turbojet the hot gas is products(sys, FAR): its chemical composition — and therefore its polynomial coefficients, and therefore $c_p$, $\gamma$, $R$ — depend on the fuel–air ratio. When FAR is being differentiated (a dual), the composition moves with it, so the gas's own coefficients become dual numbers. Watch it happen live:

  FARd = ForwardDiff.Dual{:demo}(0.03, 1.0)   # FAR with ∂/∂FAR = 1
  gasd = products(sys, FARd)
  typeof(gasd)
FrozenGas{ForwardDiff.Dual{:demo, Float64, 1}}

The gas's element type is now Dual{:demo,Float64,1} — every coefficient carries its sensitivity to FAR. So if we evaluate $h$ of this dual-carrying gas at a plain temperature, the answer is a dual: its value is the enthalpy, and its partials is $\partial h/\partial\text{FAR}$ — the composition tangent:

  h(gasd, 1600.0)   # dual-carrying gas, PLAIN temperature
Dual{:demo}(216890.07, -4.0069e7)
        └─ value = h          └─ partials = ∂h/∂FAR  (composition tangent)

This makes sense: $h$ is built from the coefficients, the coefficients depend on FAR, so differentiating the coefficients propagates $\partial h/\partial\text{FAR}$ into the partials slot. Nothing is wrong yet — we fed a plain temperature. The trouble starts when the temperature is also a dual.

§7

The collision — the missing rule, traced live

Inside jacobian!, FAR and the temperatures are all seeded in the same pass, so they share one tag (:demo here). A hot-section kernel ends up calling h(gas, T) where both the gas and the temperature carry Dual{:demo}. Which method runs? The only matching one is line 35 from §5 — written for a Dual temperature on a constant gas. Let us trace it by hand, with the real numbers:

# the rule that fires (line 35):
h(gas, d::Dual{Tag}) = Dual{Tag}( h(gas, value(d)) , cp(gas, value(d)) * partials(d) )
1
value(d) strips the temperature's slope → 1600.0, a plain Float64. So far so good.
2
But h(gas, 1600.0) — from §6 — is already a Dual{:demo}, because gas carries dual coefficients. The value slot is being filled with a dual number, not a plain number.
3
Likewise cp(gas, 1600.0) is a Dual{:demo}, so the partials slot is a dual too.
4
The rule wraps both in another Dual{:demo} — same tag. We get a Dual{:demo} whose value is itself a Dual{:demo}: the malformed same-tag nesting §4 warned about.

Here is the actual type that comes back — not a hypothetical, the real run:

  Td = ForwardDiff.Dual{:demo}(1600.0, 1.0)   # temperature, same tag as FARd
  typeof( h(gasd, Td) )
ForwardDiff.Dual{:demo, ForwardDiff.Dual{:demo, Float64, 1}, 1}
                        └──────── value slot is a SAME-TAG Dual ────────┘   ← nested

Why this breaks the Jacobian

PowerCycles' jacobian! reads the partials off each residual row and stores them as Float64 entries of the sparse Jacobian. With a normal single-layer dual that partials is a Float64 — fine. With this nested dual the partials slot is itself a Dual, so the scatter tries Float64(::Dual) and throws. The derivative information is technically all in there, just wrapped one layer too deep for the matrix to hold.

the missing piece, stated plainly

IGT's extension has a rule for a Dual temperature on a constant gas (line 35), and — further down — it has full rules for a Dual-carrying gas in the inversions like T_from_h (lines 114–147, methods typed FrozenGas{<:Dual{Tag}}). What it does not have is a rule for a Dual-carrying gas at a Dual temperature in the forward properties h, s0, cp, gamma. With no such method, dispatch falls to line 35 — the constant-gas rule — and that is the one that nests.

§8

What the rule should return — the total derivative

When both the gas and the temperature move with the same seed, $h$ changes for two reasons at once, and calculus says you add them. With $\xi$ the thing being differentiated (FAR here), the chain rule gives the total derivative:

$$\frac{dh}{d\xi} \;=\; \underbrace{\frac{\partial h}{\partial T}\,\frac{dT}{d\xi}}_{\substack{\text{temperature tangent}\\ =\ c_p\,\dot T}} \;+\; \underbrace{\sum_i \frac{\partial h}{\partial a_i}\,\frac{da_i}{d\xi}}_{\substack{\text{composition tangent}\\ \text{(coefficients } a_i \text{ move)}}}$$

The first term is the one line 35 already knows ($c_p\dot T$). The second term — the coefficients $a_i$ shifting because the composition depends on FAR — is the one the constant-gas rule simply has no slot for. And crucially, the second term is exactly the partials of h(gasd, 1600.0) we saw in §6. Both terms are single numbers; their sum is a single number; the answer should be a plain single-layer dual.

We can watch the two terms add up with the real values — and check the total against a finite difference:

  composition tangent  ∂h/∂FAR   = -4.0069e7     # from h(gasd, 1600.0), §6
  temperature tangent  cp·Ṫ      = +1290.98       # cp(1600)·1
  ───────────────────────────────────────────
  total  (what the partials SHOULD be) = -4.00679e7
  central finite-difference check      = -4.00679e7   ✓ match to ~1e-11

The composition tangent outweighs the temperature tangent by about 31 000× here — so getting it right is not a rounding nicety; it carries essentially the entire derivative. (This is the same effect that, in the burner row, flips the Jacobian's sign versus a composition-frozen guess — see P7 §5.)

§9

Two ways to get the single-layer answer

Fix A — the shim PowerCycles ships today

Remember from §5 that the base package is generic over Real: if we bypass the analytic line-35 rule and force the generic polynomial method, ForwardDiff just runs the polynomial in ordinary dual arithmetic — and that arithmetic naturally produces the combined, single-layer tangent (both terms of §8, added by the product rule, no nesting). Julia's invoke calls a specific, less-specialized method on purpose. That is the whole trick:

86# force IGT's generic Real method, sidestepping the Dual-target analytic rule
86@inline tpg_h(gas::FrozenGas, T)  = invoke(h,   Tuple{FrozenGas,Real}, gas, T)
87@inline tpg_s0(gas::FrozenGas, T) = invoke(s0,  Tuple{FrozenGas,Real}, gas, T)
88@inline tpg_cp(gas::FrozenGas, T) = invoke(c_p, Tuple{FrozenGas,Real}, gas, T)

src/thermo/cpg.jl, lines 86–88 — the consolidated tpg_* shims

Run the shim on the exact collision input from §7 and the nesting is gone — a clean single-layer dual whose partials matches the finite-difference total:

  invoke(h, Tuple{FrozenGas,Real}, gasd, Td)
    = Dual{:demo}(216890.07, -4.00679e7)
      type = Dual{:demo, Float64, 1}        ← single layer ✓
      partials -4.00679282e7  vs  FD -4.00679282e7  ✓
The sharp edge — why tpg_sos and tpg_pr need more. invoke only redirects the outer call. Enthalpy and entropy are direct polynomials, so forcing their generic method is enough. But speed of sound and the isentropic pressure ratio are built from $c_p$, $\gamma$, $s_0$ — and the generic versions of those re-call the property functions, which hit the analytic line-32–39 rules again and re-nest. So the shims rebuild those two from the cp/s0 primitives (each already routed through invoke) using their closed forms:

90@inline function tpg_sos(gas::FrozenGas, T)
91    cp = tpg_cp(gas, T); Rg = R(gas)
93    return sqrt(cp / (cp - Rg) * Rg * T)        # √(γ·R·T),  γ = cp/(cp−R)
94end
96@inline function tpg_pr(gas::FrozenGas, T1, T2)
97    Rg = R(gas)
98    return exp((tpg_s0(gas, T2) - tpg_s0(gas, T1)) / Rg)   # P2/P1, isentropic
99end

src/thermo/cpg.jl, lines 90–99

Fix B — the clean upstream method (what would retire the shims)

The shim lives in PowerCycles and works around IGT from the outside. The tidy fix is a new method inside IGT that handles the collision directly — and the remarkable part is that the author already wrote this exact pattern, just for the inversions. Here is IGT's existing rule for a Dual-carrying gas in T_from_h:

114function _T_from_h_dualgas(gas::FrozenGas, hspec; Tguess)
116    gas0  = _value_gas(gas)                  # the plain-Float64 value-part gas
117    Tstar = T_from_h(gas0, value(hspec); ...)  # solve on the value rail
118    ∂comp = _tangent( h(gas, Tstar) )      # composition term: ONE dual eval at float T*
119    ∂targ = _tangent(hspec)
120    Dual{Tag}(Tstar, _minus(∂targ, ∂comp) / cp(gas0, Tstar))   # single layer
121end

~/.../IdealGasThermoForwardDiffExt.jl, lines 114–121 — the Dual-gas rule that already exists for inversions

It value-strips the gas (_value_gas), does the heavy work on plain Float64, and grabs the composition tangent with a single dual evaluation at the float temperature (_tangent(h(gas, Tstar))). The missing forward-property rule is the same recipe, even simpler — no Newton solve:

  # PROPOSED: the rule IGT is missing — h of a Dual gas at a Dual temperature
  function h(gas::FrozenGas{<:Dual{Tag}}, d::Dual{Tag}) where {Tag}
      gas0 = _value_gas(gas)
      Tv   = value(d)
      ∂T   = cp(gas0, Tv) * partials(d)     # temperature tangent   ∂h/∂T · Ṫ
      ∂c   = partials( h(gas, Tv) )         # composition tangent   Σ ∂h/∂aᵢ · ȧᵢ
      Dual{Tag}( h(gas0, Tv), ∂T + ∂c )   # ONE layer; the §8 total derivative
  end

The two added lines, ∂T and ∂c, are precisely the two terms of the §8 total derivative; their sum goes in a single partials slot. Add the analogues for s0, cp, gamma, speed_of_sound, pressure_ratio, and the tpg_* shims become unnecessary — every TPG kernel could call IGT's normal functions directly.

§10

The wishlist, now earned

With the mechanism in hand, the three requests from P7 §5 read plainly:

  1. Combined-tangent rules for h and s0 on FrozenGas{<:Dual{Tag}} at a Dual{Tag} — the single-layer total derivative of §8 (the proposed method above). These two are the primitives.
  2. Direct rules for the derived properties speed_of_sound, gamma, pressure_ratio — so callers need not rebuild them from cp/s0 (the part invoke alone cannot fix, §9).
  3. Or simply a guard: have the existing line-35 rules detect a Dual-carrying gas and fall through to the generic Real path — a one-line dispatch fix that makes the shims unnecessary at zero accuracy cost (just foregoing the analytic-speed win).

None of this changed any answer: the shims already give the exact single-layer derivative (matching finite differences to ~$10^{-11}$), the TPG turbojet solves zero-allocation, and the Jacobian is correct. The gap is one of ergonomics and locality — five shim functions in PowerCycles standing in for a rule that, by the evidence of its own inversion methods, IdealGasThermo is one short, familiar step from providing.

← Back to P7 — the TPG Ladder, where these shims appear in context (§5, "the Dual story").