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

#include "IFstream.H"

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

// const Foam::CMC::dataType Foam::CMC::FlameStructure::staticData();


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


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


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


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

Foam::CMC::FlameStructure::FlameStructure(word fileName)
:
    names_(),
    Y_(),
    W_(),
    T_(),
    Q_(),
    h_(),
    rho_(),
    Rgas_()
{
    IFstream slfmFile (fileName);

    word garbage;
    word yname;

    // number of eta grids and species
    label nEta(0);
    label nY(0);

    slfmFile.getLine(garbage);      //INPUT FILE FOR THE SR-CMC ROUTINES
    slfmFile.getLine(garbage);      //No.GRID POINTS No. SPECIES
    slfmFile >> nEta >> nY;

    Y_.setSize(nY);
    W_.setSize(nY);

    eta_.setSize(nEta);

    T_.setSize(nEta);
    Q_.setSize(nEta);
    h_.setSize(nEta);
    rho_.setSize(nEta);
    Rgas_.setSize(nEta);

    slfmFile.getLine(garbage);      //blank line (trailing white spaces)
    slfmFile.getLine(garbage);      //PRESSURE(ATM)
    slfmFile.getLine(garbage);      //1.0
    slfmFile.getLine(garbage);      //MIXTURE FRACTION GRID

    scalarField YTemp(nEta,0);
    scalarField WTemp(nEta,0);


    for(label j=0 ; j<nEta ; j++)
    {
        slfmFile >> eta_[j];
    }

    slfmFile.getLine(garbage);      //blank line (trailing white spaces)
    slfmFile.getLine(garbage);      //INITIAL CONDITIONS


    for(label i=0; i<nY ; i++)
    {
        //species name
        slfmFile >> yname;
        names_.append(yname);

        //species mass fractions
        for(label j=0 ; j<nEta ; j++)
        {
            slfmFile >> YTemp[j];
        }
        Y_.set(i, new scalarField(YTemp));

        //species production rates
        for(label j=0 ; j<nEta ; j++)
        {
            slfmFile >> WTemp[j];
        }
        W_.set(i, new scalarField(WTemp));

        slfmFile.getLine(garbage);      //blank line (trailing white spaces)
    }


    //read temperature
    slfmFile >> yname;

    for(label j=0 ; j<nEta ; j++)
    {
        slfmFile >> T_[j];
        T_[j] = max(0.0, T_[j]);
    }

    for(label j=0 ; j<nEta ; j++)
    {
        slfmFile >> Q_[j];
    }


    /* 
    // calcuate rather than read

    //read density
    slfmFile >> yname;

    for(label j=0 ; j<nEta ; j++)
    {
        slfmFile >> rho_[j];
    }


    //read enthalpy
    slfmFile >> yname;

    for(label j=0 ; j<nEta ; j++)
    {
        slfmFile >> Rgas_[j];
    }
    */













    /*
    Interpolation(etaValue, eta, 1, nY+1, yi_temp, yi);

    for(label j = 0 ; j<=etamax ; j++)
    {
        for(label i = 0 ; i<Ysize ; i++)
        {
            QiCMC[j*Ysize+i] = yi[j*(nY+1)+i];
        }
        TCMC[j] = yi[j*(nY+1)+nY];
    }

    for(label k=1 ; k<nf ; k++)
    {
        for(label j = 0 ; j<=etamax ; j++)
        {
            for(label i = 0 ; i<Ysize ; i++)
            {
                QiCMC[(k*(etamax+1)+j)*Ysize+i] = QiCMC[j*Ysize+i];
            }
            TCMC[j+k*(etamax+1)] = TCMC[j];
        }
    }



    for(label j=0 ; j <= etamax ; j++) //get conditional enthalpy 
    {
        for(label k = 0 ; k<nf ; k++)
        {
            scalar Tin = TCMC[ j+k*(etamax+1) ];
            scalarField QiCMCin(Ysize, 0);

            for(label i=0 ; i<Ysize ; i++)
            {
                QiCMCin[i] = QiCMC[(k*(etamax+1)+j)*Ysize + i];
            }

            rhoCMC[j+k*(etamax+1)] = chemistry->calculateRHOCMC(QiCMCin , Tin, Pin);

            QhCMC[j+k*(etamax+1)] = chemistry->calculateHCMC(QiCMCin , Tin, rhoCMC[ j+k*(etamax+1)], Pin);

        }
    }
    */
}


Foam::CMC::FlameStructure::FlameStructure(const FlameStructure&)
// :
    // baseClassName(),
    // data_()
{}


// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //

/*
Foam::autoPtr<Foam::CMC::FlameStructure>
Foam::CMC::FlameStructure::New()
{
    return autoPtr<FlameStructure>(new FlameStructure);
}
*/


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

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


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


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

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

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


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


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