/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2016 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "BetaFunction.H"

#include "error.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

// const scalar Foam::CMC::BetaFunction::staticData();


// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //


// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::CMC::BetaFunction::_setParameters()
{
        scalar gamma = mf_*(1.0 - mf_)/(mfVar_ + SMALL) - 1.0;

        if (gamma < SMALL || (mfVar_)/(mf_*(1 - mf_) + SMALL) < 0.001)
        {
            fdelta_ = true;
        }
        else
        {
            a_ = max(mf_*gamma, 0.0);
            b_ = max((1.0 - mf_)*gamma, 0.0);

            if(a_ < 1.0)
            {
                delta0_ = true;
            }
            if(b_ < 1.0)
            {
                delta1_ = true;
            }

            _limitAB();
        }
}


void Foam::CMC::BetaFunction::_limitAB()
{
    scalar fmax = 1.0/(1.0 + (b_ - 1.0)/(a_ - 1.0));

    if(a_ > 500.0)
    {
        a_ = 500.0;
        b_ = (a_ - 1.0 - fmax*(a_ - 2.0))/fmax;
    }
    else if(b_ > 500.0)
    {
        b_ = 500.0;
        a_ = (1.0 + fmax*(b_ - 2.0))/(1.0 - fmax);
    }
}

// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::CMC::BetaFunction::BetaFunction(const scalar mf, const scalar mfVar)
:
    mf_(mf),
    mfVar_(mfVar),
    a_(0.0),
    b_(0.0),
    fdelta_(false),
    delta0_(false),
    delta1_(false)
{
    _setParameters();
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::CMC::BetaFunction::~BetaFunction()
{}


// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //


// * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //


// * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * * //


// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //


// ************************************************************************* //
