~$ fkstone_

mixtureAveragedFourier: Cantera-Style Mixture-Averaged Diffusion in OpenFOAM

· 2756 words · 13 min read

Contents

In multicomponent reacting flows the species diffuse at different rates (differential diffusion), and this has a large effect in problems such as lean hydrogen flames or the interface between fuel vapour and air. Among the laminar diffusion models in OpenFOAM (openfoam.org), FickianFourier is cheap but differs from the mixture-averaged model of Cantera and Chemkin in several places, while MaxwellStefanFourier is exact but needs a lot of memory and run time once the mechanism has many species. mixtureAveragedTransport is a library I built by modifying FickianFourier. It adds one laminar model, mixtureAveragedFourier, whose goal is to reproduce Cantera’s mixture-averaged model as closely as possible in OpenFOAM while keeping the memory use at the level of FickianFourier.

The model reads the same binary diffusion coefficient dictionary D that MaxwellStefanFourier uses. This is the dictionary that chemkin2foam (described in the previous post) writes when the Full option is selected, so an existing setup only needs the model name changed to mixtureAveragedFourier.

The post first gives the equations of the mixture-averaged model and then follows the structure of the code: the diffusion coefficients, the correction velocity, the time discretisation, the memory layout, the heat flux and the handling of mesh changes.

The three existing models

OpenFOAM offers three laminar models for multicomponent diffusion. unityLewisFourier lets every species diffuse with the thermal diffusivity, so there is no differential diffusion at all, and it is cheap. FickianFourier gives each species one diffusion coefficient and is also cheap. MaxwellStefanFourier solves the full multicomponent problem and is expensive, at about 70 KB of memory per cell for an 88-species mechanism.

According to the comparison in the repository README (OpenFOAM 14), FickianFourier with mixtureDiffusionCoefficients no differs from Cantera in three ways. The numerator of DiD_i is 1−Xi1-X_i , where Cantera uses 1−Yi1-Y_i . The flux is driven by ∇Yi\nabla Y_i and has no mean molecular weight gradient term. The species fluxes do not sum to zero, and the default species (usually N2) absorbs the imbalance.

MaxwellStefanFourier works with an n×nn\times n matrix in every cell, so its memory grows with the square of the number of species. For 88 species, the 88288^2 double-precision numbers alone take about 62 KB, and with the other arrays the total is about 70 KB per cell. A 2D case with 640 000 cells would need about 45 GB for this part alone.

The mixture-averaged model

The Hirschfelder–Curtiss approximation writes the diffusion velocity of species ii as Vi=−(Di/Xi)∇Xi\mathbf{V}_i = -(D_i/X_i)\nabla X_i , which gives the mass flux

jiMA=−ρ Di WiW ∇Xi \mathbf{j}_i^{\mathrm{MA}} = -\rho\,D_i\,\frac{W_i}{W}\,\nabla X_i

where WiW_i is the molar mass of the species and WW the mean molar mass of the mixture. The mixture-averaged diffusion coefficient is built from the binary coefficients DijD_{ij} :

Di=1−Yi∑j≠iXj/Dij D_i = \frac{1 - Y_i}{\displaystyle\sum_{j\neq i} X_j / D_{ij}}

This is the same formula used by the mixture-averaged models in Cantera and Chemkin.

From Xi=YiW/WiX_i = Y_i W / W_i it follows that

WiW∇Xi=∇Yi+Yi ∇ln⁡W \frac{W_i}{W}\nabla X_i = \nabla Y_i + Y_i\,\nabla\ln W

so a flux driven by the mole fraction gradient equals a flux driven by ∇Yi\nabla Y_i plus an extra term Yi∇ln⁡WY_i\nabla\ln W . Where the mean molecular weight changes sharply, for example at the interface between n-heptane vapour (W≈100W \approx 100 ) and air (W≈29W \approx 29 ) or in hydrogen-rich regions, this term is of the same order as ∇Yi\nabla Y_i . FickianFourier does not include it.

The approximate fluxes jiMA\mathbf{j}_i^{\mathrm{MA}} do not in general sum to zero, so they violate mass conservation. Cantera’s one-dimensional flame code fixes this with a correction velocity: it sums the fluxes of all species and subtracts that sum from each species in proportion to its mass fraction. The corrected flux is

ji=jiMA−Yi∑kjkMA \mathbf{j}_i = \mathbf{j}_i^{\mathrm{MA}} - Y_i\sum_k \mathbf{j}_k^{\mathrm{MA}}

Since ∑iYi=1\sum_i Y_i = 1 , the corrected fluxes satisfy ∑iji=0\sum_i \mathbf{j}_i = 0 exactly. mixtureAveragedFourier implements these equations without the Soret effect.

Computing the diffusion coefficients

updateDm() computes DiD_i for all species. It first evaluates the mean molar mass WW from the old-time mass fractions and then accumulates the denominator for each species:

cpp
// sumXbyD = sum_j Y_j/(W_j D_ij) = sum_j (X_j/D_ij)/W, so
// D_i = (1 - Y_i)/sum_j (X_j/D_ij) = (1 - Y_i)/(W sumXbyD)
forAll(Y, j)
{
    if (j != i)
    {
        sumXbyD += Y[j].oldTime()/(Wi(j)*D_ij(p, T));
    }
}

Dm_[i] = (1 - Y[i].oldTime())/(W0*max(sumXbyD, small));

Because Xj/W=Yj/WjX_j/W = Y_j/W_j , the sum can use mass fractions directly and no mole fraction fields have to be built. sumXbyD is a single temporary field that is reset and reused for every species, so the n2n^2 binary coefficients are evaluated one at a time and never stored together.

The binary coefficients Dij(p,T)D_{ij}(p,T) are Function2 objects read at construction, in the same way as in MaxwellStefanFourier. For every pair with i≤ji \le j the code looks for i-j or j-i in the D dictionary. A missing pair is a fatal error, and if both names are present it uses i-j and prints a warning. Only the upper triangle is kept, and evaluation picks i < j ? DFuncs_[i][j] : DFuncs_[j][i].

When a species is nearly pure (Yi→1Y_i \to 1 ), DiD_i goes to zero with 1−Yi1-Y_i . This is physically fine, since the diffusion of that species then comes mostly from the correction velocity, but it causes trouble for the time discretisation. The quantity Γi\Gamma_i introduced below deals with this case.

Correction velocity

updateSums() computes ∑kjkMA\sum_k \mathbf{j}_k^{\mathrm{MA}} . The uncorrected face flux of each species comes from jMA(i):

cpp
// j_i^MA = -rho D_i (grad(Y_i) + Y_i grad(ln W)), evaluated on the faces
-interp.interpolate(alpha*rho*Dm[i])
*(fvc::snGrad(Y0i) + interp.interpolate(Y0i)*snGradLnW)

Here snGradLnW is ∇ln⁡W\nabla\ln W on the faces, the face-normal gradient of WW divided by the interpolated face value of WW , and it is shared by all species. The sum runs over every species including the default one, and each species then subtracts Yi∑kjkMAY_i \sum_k \mathbf{j}_k^{\mathrm{MA}} .

In FickianFourier the mass fraction of the default species is 1−∑1-\sum of the others, so any error in the sum of the other fluxes ends up entirely in the default species. If that species is N2, its diffusion flux is wrong by the full imbalance. With the correction velocity the error is shared among all species according to their mass fractions, and every species flux agrees with Cantera.

Time discretisation

Implicit part and deferred correction

OpenFOAM solves the species transport equations one at a time, and each equation can only treat its own YiY_i implicitly. The ∇ln⁡W\nabla\ln W term and the correction velocity depend on the gradients of all species, so they cannot be written directly as an implicit Laplacian of a single species. Following the explicit/implicit split used in MaxwellStefan, the code introduces an implicit diffusivity Γi\Gamma_i and writes the flux as

ji=−ρ Γi ∇Yi n+1+[ ji+ρ Γi ∇Yi ]old \mathbf{j}_i = -\rho\,\Gamma_i\,\nabla Y_i^{\,n+1} + \left[\,\mathbf{j}_i + \rho\,\Gamma_i\,\nabla Y_i\,\right]^{\mathrm{old}}

Everything inside the bracket uses the old-time mass fractions and the DiD_i computed at the start of the time step. At the start of the step Yin+1=YioldY_i^{n+1} = Y_i^{\mathrm{old}} , so the implicit term cancels the ρΓi∇Yi\rho\Gamma_i\nabla Y_i inside the bracket and the flux equals the corrected mixture-averaged flux ji\mathbf{j}_i .

In the code, the implicit part comes from the parent class unityLewisFourier. Its j() and divj() take DEff(Yi) as the diffusion coefficient, and divj() contains an implicit fvm::laplacian. mixtureAveragedFourier overrides DEff() to return ρΓi\rho\Gamma_i and then adds the explicit bracket jexp(i) in j() and divj():

cpp
divj(Yi) = unityLewisFourier::divj(Yi)            // -fvm::laplacian(rho Gamma_i, Yi)
         + fvc::div(jexp(i)*Yi.mesh().magSf());    // the bracket, explicit

Choosing Γi\Gamma_i

The value of Γi\Gamma_i decides whether the scheme is stable. Consider the 1D model equation ∂Y/∂t=Deff ∂2Y/∂x2\partial Y/\partial t = D_{\mathrm{eff}}\,\partial^2 Y/\partial x^2 , where DeffD_{\mathrm{eff}} is the actual derivative of the flux with respect to the species’ own gradient, with Γ\Gamma treated implicitly and Deff−ΓD_{\mathrm{eff}}-\Gamma explicitly. For a Fourier mode with wavenumber kk , let s=4Δtsin⁡2(kΔx/2)/Δx2s = 4\Delta t\sin^2(k\Delta x/2)/\Delta x^2 . The amplification factor is

G=1−(Deff−Γ) s1+Γ s G = \frac{1 - (D_{\mathrm{eff}} - \Gamma)\,s}{1 + \Gamma\,s}

If Γ≥Deff>0\Gamma \ge D_{\mathrm{eff}} > 0 , then 0≤G<10 \le G < 1 for any Δt\Delta t , so the scheme is unconditionally stable and free of oscillations. If Γ\Gamma is much smaller than DeffD_{\mathrm{eff}} , modes with large ss have G→−(Deff−Γ)/ΓG \to -(D_{\mathrm{eff}}-\Gamma)/\Gamma , which gives ∣G∣>1|G| > 1 once Deff>2ΓD_{\mathrm{eff}} > 2\Gamma , and the solution blows up.

The obvious choice is Γi=Di\Gamma_i = D_i , with the ∇ln⁡W\nabla\ln W term and the correction velocity treated as explicit. This fails for a dominant species. As YiY_i approaches 1, DiD_i goes to zero and the actual diffusion comes mostly from the explicit correction velocity term, so Deff≫2DiD_{\mathrm{eff}} \gg 2D_i and the scheme diverges when DΔt/Δx2≫1D\Delta t/\Delta x^2 \gg 1 .

The code therefore computes the derivative DiiD_{ii} of the flux with respect to the species’ own gradient and sets Γi=max⁡(Di,Dii)\Gamma_i = \max(D_i, D_{ii}) . In this derivative the gradients of the other non-default species are held fixed, and the gradient of the default species dd follows from the mass fractions summing to one: when ∇Yi\nabla Y_i increases by δ\delta , ∇Yd\nabla Y_d decreases by δ\delta . Since ∇ln⁡W=−W∑k∇Yk/Wk\nabla\ln W = -W\sum_k \nabla Y_k / W_k , ∇ln⁡W\nabla\ln W then changes by gi δg_i\,\delta with

gi=−W(1Wi−1Wd) g_i = -W\left(\frac{1}{W_i} - \frac{1}{W_d}\right)

Differentiating the corrected flux gives

Dii=Di (1−Yi)+Yi Dd+Yi gi(Di−∑kYkDk) D_{ii} = D_i\,(1 - Y_i) + Y_i\,D_d + Y_i\,g_i\left(D_i - \sum_k Y_k D_k\right)

For a mixture of two species, DiiD_{ii} equals the binary coefficient D12D_{12} and does not depend on YiY_i . For a nearly pure species Di→0D_i \to 0 while DiiD_{ii} stays finite. The code reads:

cpp
max
(
    D[i],
    D[i]*(1 - Y0i) + Y0i*D[d]
  - Y0i*W0*(1/Wi(i) - 1/Wi(d))*(D[i] - sumYD)
)

The default species has no transport equation of its own, and the code simply sets Γd=Dd\Gamma_d = D_d . When Γi=Dii\Gamma_i = D_{ii} , the bracket has no first-order dependence on ∇Yi\nabla Y_i and only the coupling through the other species’ gradients remains. When Γi=Di>Dii\Gamma_i = D_i > D_{ii} , the implicit part is somewhat larger than needed. Both cases satisfy Γi≥Dii\Gamma_i \ge D_{ii} , so by the analysis above the scheme is stable for any DΔt/Δx2D\Delta t/\Delta x^2 .

DiD_i , WW and the related fields are computed in predict(), which runs once at the start of each time step, and they stay fixed during the step.

Memory

The memory of MaxwellStefanFourier is dominated by the n×nn\times n matrix in each cell. mixtureAveragedFourier caches only these fields:

cpp
PtrList<volScalarField>      Dm_;         // D_i of each species, n volume fields
autoPtr<volScalarField>      W0_;         // mean molar mass W at the old time
autoPtr<volScalarField>      sumYD_;      // sum_k Y_k D_k
autoPtr<surfaceScalarField>  snGradLnW_;  // grad(ln W) on the faces
autoPtr<surfaceScalarField>  sumJ_;       // sum of the uncorrected fluxes

Apart from the DiD_i there are only four fields shared by all species, a constant number on top of the DiD_i that FickianFourier also stores. The Γi\Gamma_i , jiMA\mathbf{j}_i^{\mathrm{MA}} and the bracket of each species are computed when that species’ equation is solved and released afterwards. jiMA\mathbf{j}_i^{\mathrm{MA}} is computed once for the sum in updateSums() and again in jexp(), which trades some extra computation for memory.

In the README test with 640 000 cells, 88 species and chemistry switched off, this model used 8.25 GB and FickianFourier used 8.07 GB.

Heat flux

The heat flux consists of Fourier conduction and the enthalpy carried by species diffusion:

q=−κ ∇T+∑ihs,i ji \mathbf{q} = -\kappa\,\nabla T + \sum_i h_{s,i}\,\mathbf{j}_i

The code accumulates hs,ijih_{s,i}\mathbf{j}_i and ji\mathbf{j}_i over the non-default species and takes the flux of the default species as minus the sum of the others. Because the corrected fluxes sum to zero exactly, this gives the same default-species flux as computing it directly.

In the energy source term divq(he), conduction is written explicitly in terms of temperature as ∇⋅(κ∇T)\nabla\cdot(\kappa\nabla T) . To help convergence the code adds fvm::laplacianCorrection(alphaEff, he), an implicit Laplacian of the energy variable he minus the explicit value of the same term. The two cancel at convergence and do not change the result. Conduction and the enthalpy flux are therefore correct for both energy forms, sensibleEnthalpy and sensibleInternalEnergy.

OpenFOAM’s own unityLewisFourier diffuses he with κ/cv\kappa/c_v when the energy variable is sensible internal energy. In the same case reported in the README, this heated the lean side before ignition by 4–7 K at 0.1 of the ignition delay and by 10–14 K at 0.6 of the ignition delay.

unityLewisSpecies

The species listed in the optional unityLewisSpecies entry use the thermal diffusivity in place of the mixture-averaged coefficient:

Di=κρ cp D_i = \frac{\kappa}{\rho\,c_p}

The driving force stays (Wi/W)∇Xi(W_i/W)\nabla X_i and the correction velocity is applied as for the other species, which matches Cantera’s definition Lek=α/Dk,mixLe_k = \alpha / D_{k,\mathrm{mix}} . The DjD_j of the remaining species still need the binary coefficients of the listed species, so the D dictionary must stay complete.

This entry can switch off differential diffusion for chosen species only, for example giving just the fuel a Lewis number of one, which helps separate the effects of different species. Listing all species gives an exact unity Lewis number model with the correct enthalpy flux for both energy forms. In that case the README reports that the temperature stays on the adiabatic mixing line before ignition, with a deviation of −0.4 K at 0.1 of the ignition delay.

Mesh changes and load balancing

In reacting DNS the chemistry cost is concentrated near the flame, and as the flame moves the mesh has to be redistributed with loadBalancer. The model class also inherits from TopoChangeableMeshObject<fvMesh>, so on a topology change, mesh mapping or redistribution, topoChange(), mapMesh() and distribute() call clear() to delete all cached fields.

The cached fields are not mapped. The next time they are needed, update() finds the cache empty and rebuilds DiD_i and the four shared fields from the current old-time fields. This avoids writing mapping code for each cached field, at the cost of one extra call to updateDm() after each mesh change. For pure mesh motion without topology change, movePoints() does nothing.

Usage

Build the library in a terminal with the OpenFOAM environment loaded:

sh
wmake libso        # -> $FOAM_USER_LIBBIN/libmixtureAveragedTransport.so

Load it in system/controlDict:

cpp
libs ("libmixtureAveragedTransport.so");

constant/thermophysicalTransport is written as for MaxwellStefanFourier, with only the model name changed:

cpp
simulationType laminar;

laminar
{
    model  mixtureAveragedFourier;

    // unityLewisSpecies (nc7h16);   // optional: these species get Le = 1

    D      // binary diffusion coefficients, as for MaxwellStefanFourier
    {
        o2-o2 { type binaryDiffusionCoefficient; ... }
        o2-n2 { ... }
        ...
    }
}

For the thermal conductivity of the mixture, the standard OpenFOAM option mixture coefficientWilkeMulticomponentMixture; in physicalProperties agreed with Cantera to within 0–4 %. The default multicomponentMixture was 5–17 % low in our flames.

Validation

The tests used the 88-species n-heptane skeletal mechanism of Lu and Law at 6 MPa, with n-heptane vapour in contact with H2/air.

On prescribed 1D profiles of TT and YY , the species fluxes agree with Cantera’s mixture-averaged transport to a relative difference of 10−410^{-4} , and the sum of the fluxes is zero to round-off. In an autoignition case on a closed 1D strip with a 1 µm grid, the results differ from MaxwellStefanFourier by 0.16 % in ignition delay, by less than 0.8 % in flame front position and by 0.2 % in mean pressure, and the run takes 46 % of the Maxwell–Stefan run time. The unityLewisSpecies fluxes agree to within 8×10−58\times10^{-5} with a reference built from the cell values of κ\kappa , ρ\rho and cpc_p .

With these numbers, fine-grid 2D DNS with skeletal mechanisms of 80 to 100 species can be run on a single workstation.

Summary and limitations

mixtureAveragedFourier builds mixture-averaged diffusion coefficients from the binary coefficients, drives the fluxes with mole fraction gradients and enforces mass conservation with a correction velocity, matching Cantera’s mixture-averaged model. In time, it uses Γi=max⁡(Di,Dii)\Gamma_i = \max(D_i, D_{ii}) as the implicit diffusivity and treats the rest as a deferred correction, which stays stable at large time steps. In memory, it caches only the DiD_i and four shared fields and computes everything else when needed.

Some limitations remain. The Soret and Dufour effects are not included, and the Soret effect may matter in hydrogen flames. Only a laminar version exists, with no RAS or LES counterpart. The DiD_i and the coupling terms in the bracket are computed at the start of each time step and are not updated during the step, so they lag by one time step. The model has only been tested with OpenFOAM 14 (openfoam.org).

The code is on GitHub at Fkstone/mixtureAveragedTransport under GPL-3.0, derived from OpenFOAM’s Fickian and FickianFourier. Issues and questions are welcome.