/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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 "BetaIntegrator.H"


#include "error.H"
#include "SubList.H"
#include "SubField.H"
#include "interpolateXY.H"

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

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


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


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

//extended Simpson's rule (Numerical recipes, 2nd Ed. p.128)
//for equally spaced and even N intervals (or odd N+1 points)
Foam::scalar Foam::CMC::BetaIntegrator::simps
(const scalar xl, const scalar xh, const label n, const UList<scalar>& fx)
    const
{
    scalar evensum(0.0), oddsum(0.0), sum(0.0);

    scalar h = (xh - xl)/scalar(n);

    for(label i=0 ; i<fx.size() ; i++)
    {
        if(i%2 == 0)
        {
            evensum += fx[i];
        }
        else
        {
            oddsum += fx[i];
        }
    }

    sum = -1.0*fx.first() + 2.0*evensum + 4.0*oddsum - 1.0*fx.last();

    return sum*(h/3.0);
}


Foam::scalar Foam::CMC::BetaIntegrator::sumSimps(const UList<scalar>& f) const
{
    scalar total = 0.0;

    const labelList &N(bg_.N());
    const scalarField &etaCut(bg_.etaCut());

    forAll(N, i)
    {
        const labelList::subList prev(N, i);
        const label baseI = sum(prev) + 1;

        const scalarField::subField subInterval(f, N[i]+1, baseI);

        total += simps(etaCut[i], etaCut[i+1], N[i], subInterval);
    }

    return total;
}

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


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

Foam::CMC::BetaIntegrator::BetaIntegrator(const BetaFunction& bf, const BetaGrid &bg)
:
    etaSpace_(bg.etaSpace()),
    pdfNum_(bg.etaSpace().size(), 0.0),
    pdfDen_(0.0),
    bf_(bf),
    bg_(bg)
{
    const scalarField::subField domain(etaSpace_, etaSpace_.size()-2, 1);
    scalarField::subField range(pdfNum_, etaSpace_.size()-2, 1);

    if (bf_.fdelta())
    {
        pdfDen_ = 1.0;
    }
    else
    {
        range = bf_.etaFunc(domain);

        if (!bf_.delta0())
        {
            pdfNum_.first() = bf_.etaFunc(etaSpace_.first());
        }

        if (!bf_.delta1())
        {
            pdfNum_.last() = bf_.etaFunc(etaSpace_.last());
        }

        pdfDen_ = sumSimps(pdfNum_)
            + Foam::pow(etaSpace_[1], bf_.alpha())/(bf_.alpha() + SMALL)
            + Foam::pow(etaSpace_[1], bf_.beta())/(bf_.beta() + SMALL);
    }
}


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

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


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

// beta-pdf weighted integration for given mf, mfVar and f
Foam::scalar Foam::CMC::BetaIntegrator::betaIntegrate(const scalarField& f) const
{
    scalar result = 0.0;

    if(bf_.fdelta())
    {
        result = interpolateXY(bf_.mf(), etaSpace_, f);
    }
    else
    {
        scalar num = sumSimps (f * pdfNum_)
            + f.first()*Foam::pow(etaSpace_[1], bf_.alpha())/(bf_.alpha() + SMALL)
            + f.last()*Foam::pow(etaSpace_[1], bf_.beta())/(bf_.beta() + SMALL);
        result = num/pdfDen_;
    }

    return result;
}

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

void Foam::CMC::BetaIntegrator::operator=(const BetaIntegrator& rhs)
{
    // Check for assignment to self
    if (this == &rhs)
    {
        FatalErrorInFunction
            << "Attempted assignment to self"
            << abort(FatalError);
    }
}

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


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


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