Howto calculate mass flux with variable density flow

From OpenFOAMWiki

In traditional CFD, mass flow at a boundary is calculated by writing explicit loops. In OpenFOAM, these loops can be invoked implicitly. Also, most OpenFOAM solvers include a variable phi that represents the mass flux. So calculating the mass flux only requires the following steps:

  1. ) Identifying the boundary where we want to do the summation
  2. ) Implicitly doing the sum over the specified boundary.

The following code fragment shows how to do this for a boundary named "velocity_inlet."

 
    // find the identification number (e.g. label) for our boundary of interest.
    label inletPatch = mesh.boundaryMesh().findPatchID("velocity_inlet"); 
 
    // if we don't have such a patch, warn the user
    if (inletPatch==-1) 
    {
 
	Info << "Failure to find patch named velocity_inlet for mass flow calc."
	     <<endl;
 
    }
    else   // calculate the result and do output
    {
 
// the sum operator implicity loops over the boundary faces and stores the result in inFlux
 
// note inflow is negative according to dot product. Lets multiply by -1 to 
// make it positive.
	scalar inFlux = -1.0* sum(phi.boundaryField()[inletPatch]); 
 
	Info << "   Inflow= " << inFlux    <<" [kg/s]  " ;
    }