Solution (source code)

= Solution

Let $P$ denote total <gravitational potential energy>, $P_b$ the <background potential energy> of the adiabatically sorted density field, and $A=P-P_b$ the <available potential energy>. Define $B$ positive for reversible transfer from <kinetic energy> to $P$, $\epsilon$ as viscous kinetic-energy loss, $F$ as imposed mechanical work, $W$ as molecular transfer from <internal energy> to potential energy, and $M$ as the irreversible conversion of $A$ to $P_b$. With boundary contributions removed, the <stratified mixing energy budget> is
$$
\boxed{\dot K=F-B-\epsilon,\qquad
\dot P=B+W,\qquad
\dot P_b=M+W,\qquad
\dot A=B-M.}
$$
For an unforced flow set $F=0$. In an alternative notation, if $\Phi_d=\dot P_b$ is the full diffusive increase of background energy and $\Phi_i=W$, then $M=\Phi_d-\Phi_i$ and $\dot A=B+\Phi_i-\Phi_d$. This distinguishes irreversible density mixing from reversible buoyancy exchange. The internal reservoir obeys $\dot I=\epsilon-W$, so summing the kinetic, potential and internal budgets gives $d(K+P+I)/dt=F$ as required by <conservation of energy>.