How can one calculate work produced by a flow? I know differential work is $p dV$, since all standard Thermodynamics textbooks cover this. But, almost every case that they cover is about a gas (continuum-mechanics) that has low density, so its pressure does not vary with vertical coordinates. Since thermodyanimics deals with equilibrium states, all exercises are about constant-pressure gases or pressure in a $p = f(V)$ form. I'm talking about a pressure field $p = f(x, y, z)$, that could be obtained from a Navier-Stokes solution, for example. I thought it could be something like $W = \iiint\limits_{V_{2}} \, p dV - \iiint\limits_{V_{1}} \, p dV$
Being $V_{2}$ and $V_{1}$ the final volume frontier and the initial volume frontier, respectively. But I didn't find any references in standard books, and I don't know if $dW = p dV$ remains valid for non-equilibrium process.
 A: In the derivation of the energy balance equation in continuum mechanics, the rate of doing work at the boundary of the system is calculated by dotting the force per unit area (on the boundary) by the velocity vector (rate of displacement), and integrating over the area of the boundary.  The force per unit area on the boundary is obtained using the Cauchy stress relationship, by dotting the stress tensor with a unit outwardly directed normal to the boundary.  The (isotropic) pressure (as would be obtained from the solution to the Navier Stokes equation) is one contributor to the stress tensor, which also includes viscous stresses.  The rate of doing work at the boundary of the system as a result of the pressure portion of the stress tensor would be the local pressure times the local normal component of velocity, integrated over the boundary.  This is the extension of the simpler P(dV/dt) version of the work on the surroundings that we learned in thermodynamics, and reduces to that version if the volume of the gas is changing only as a result of a piston moving in a cylinder.  Also, if the divergence theorem is applied to the pressure work done at the boundary of the control volume, we obtain the volume integral given in my comment above.
