
//CMC P(eta) equation 
// Return probability by Beta function from sampling mixture fraction, eta (NOT mf and mfVar !!)
// eta = (1/etamax)*etacount

{

    forAll(mf, celli) //cell number loop
    {
        scalar mfi = mf[celli]; //karam, test

        Switch pdfCalculated = false;

        scalarField pdf(etamax+1,0.0);

        label loop_counter = 0;
        do 
        {
            //Adjust mixture fraction and its variance
            if(mfVar[celli] < 1.0e-30)
            {
                mfVar[celli] = 1.0e-30;
            }

            if(mfi < 0.0)
            {
                mfi = 0.0;
            }

            //calculate alpha and beta for PDF function
            scalar gammaCMC = mfi*(1.0-mfi) / mfVar[celli] - 1.0;
            scalar betaCMC = (1.0-mfi) * gammaCMC;
            scalar alphaCMC = mfi * gammaCMC;

            if(mfi < 5.0e-4) //karam, 5e-4 -> 1e-4 )
            {
                pdf = 0.0;
                pdf[0] = 1.0 / deta[0];//1;
                deltaftn[celli] = 0;
                pdfCalculated = true;
            }
            else if(gammaCMC <= 0.0)
            {
                //      Info<<"GammaCMC is lower than zero -- two delta function";
                pdf = 0.0;
                pdf[0] = 0.5 * 1.0/deta[0]; //0.5;
                pdf[etamax] = 0.5 * 1.0/deta[etamax]; //0.5;
                deltaftn[celli] = 1;
                pdfCalculated = true;
            }
            else if(alphaCMC <= 1.0 && betaCMC <= 1.0)
            {
                //      Info<<"AlphaCMC and betaCMC are lower than one -- two delta function";
                pdf = 0.0;
                pdf[0] = 0.5 * 1.0/deta[0]; //0.5;
                pdf[etamax] = 0.5 * 1.0/deta[etamax]; //0.5;
                deltaftn[celli] = 1;
                pdfCalculated = true;
            }
            else if(alphaCMC < 1.0 && betaCMC >= 1.0)
            {
                pdf = 0.0;
                pdf[0] = 1.0/deta[0]; //1;
                deltaftn[celli] = 0;
                pdfCalculated = true;
            }
            else if(alphaCMC >= 1.0 && betaCMC < 1.0)
            {
                pdf = 0.0;
                pdf[etamax] = 1.0/deta[etamax]; //1;
                deltaftn[celli] = 0;
                pdfCalculated = true;
            }
            else 
            {
                scalar betaftn = std::exp(  gammaln(alphaCMC) + gammaln(betaCMC) - gammaln(alphaCMC + betaCMC) );       //Numerial recipe 2nd Edition 206page  

                if (betaftn > 1.0e-50)
                {
                    pdf[0] = 0.0;
                    pdf[etamax] = 0.0;
                    for(label etacount = 1 ; etacount < etamax ; etacount++)
                    {
                        pdf[etacount] = Foam::pow( etaValue[etacount] , alphaCMC - 1.0 ) 
                            * Foam::pow ( 1.0 - etaValue[etacount] ,  betaCMC - 1.0 ) / betaftn ;
                    }

                    deltaftn[celli] = 2;

                    scalarField f(etamax+1, 1.0);
                    scalar pTotal = integration(deltaftn[celli], MFcut, Neta, pdf, f);

                    for(label etacount = 0; etacount <= etamax ; etacount++)
                    {
                        pdf[etacount] = pdf[etacount]/pTotal;
                    }

                    scalar mfnew = integration(deltaftn[celli], MFcut, Neta, pdf, etaValue);
                    scalar mf_diff = (mf[celli] - mfnew)/mf[celli];

                    if(std::fabs(mf_diff) < 0.00001) // 1e-5)
                    {
                        pdfCalculated = true;
                    }
                    else
                    {
                        mfi = mfi + 0.5*mf_diff*mf[celli];
                    }
                }
                else
                {
                    loop_counter = 1000;
                }
            }

            loop_counter++;

        } while ((!pdfCalculated) && (loop_counter < 100));

        if(!pdfCalculated)
        {
            pdf = 0.0;
            for(label etacount = 0; etacount<=etamax ; etacount++) //set one delta function
            {
                if( etaValue[etacount] >= mfi)
                {
                    pdf[etacount] = 1.0/deta[etacount]; //1;
                    deltaftn[celli] = 0;
                    break;
                }
            }
        }

        for(label etacount = 0; etacount <= etamax ; etacount++)
        {
            Peta[etacount][celli] = pdf[etacount];
        }
    }
}
