/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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 "fileName.H"
#include "IFstream.H"
#include "hashedWordList.H"
#include "interpolateXY.H"

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


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


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

Foam::SLFM::FlameStructure::FlameStructure(const string& NstDir, scalarList NstList, const scalarField &etaGrid, const hashedWordList &spTable)
:
    NstDir_(NstDir),
    NstList_(NstList),
    eta_(etaGrid),
    species_(spTable),
    Y_(spTable.size(), scalarFieldArray1d(NstList_.size(), scalarField(eta_.size(), 0.0))),
    W_(spTable.size(), scalarFieldArray1d(NstList_.size(), scalarField(eta_.size(), 0.0))),
    T_(NstList_.size(), scalarField(eta_.size(), 0.0)),
    Q_(NstList_.size(), scalarField(eta_.size(), 0.0)),
    h_(NstList_.size(), scalarField(eta_.size(), 0.0)),
    rho_(NstList_.size(), scalarField(eta_.size(), 0.0)),
    Rgas_(NstList_.size(), scalarField(eta_.size(), 0.0))
{
    for (label n = 0; n < NstList_.size(); n++)
    {
        scalar N = NstList_[n];

        fileName fname = "sdr"+Foam::name(N)+".inp";
        IFstream fin(NstDir_/fname);

        string gbg;
        label etamax_SLFM, NoSpecies;

        //INPUT FILE...
        fin.getLine(gbg);

        //No.GRID...
        fin.getLine(gbg);

        //Number of eta-point and species
        fin >> etamax_SLFM >> NoSpecies;

        //blank line
        fin.getLine(gbg);

        //PRESSURE...
        fin.getLine(gbg);

        //1.00...
        fin.getLine(gbg);

        //MIXTURE...
        fin.getLine(gbg);

        //read eta space (from SLFM library)
        scalarField etaValue_SLFM(etamax_SLFM, 0.0);
        for(label j = 0 ; j < etamax_SLFM ; j++)
        {
            fin>>etaValue_SLFM[j];
        }

        //blank line
        fin.getLine(gbg);

        //INITIAL...
        fin.getLine(gbg);

        //species loop
        scalarField temp(etamax_SLFM, 0.0);
        for(label i = 0 ; i < NoSpecies ; i++)
        {
            word spName;
            //H2... (species name)
            fin >> spName;
            const label spI (species_[spName]);

            // Info << spI << spName << endl;

            //read species mass fraction (from SLFM library)
            for(label j=0 ; j<etamax_SLFM ; j++)
            {
                scalar Yj = 0.0;
                fin >> Yj;
                temp[j] = max(0.0, Yj);
            }

            Y_[spI][n] = interpolateXY(etaGrid, etaValue_SLFM, temp);

            //read reaction rate (from SLFM library)
            for(label j = 0 ; j < etamax_SLFM ; j++)
            {
                fin>>temp[j];
            }

            W_[spI][n] = interpolateXY(etaGrid, etaValue_SLFM, temp);

            //blank line
            fin.getLine(gbg);
        }

        //read temperature
        {
            //H2... (species name)
            fin.getLine(gbg);

            //read temperature
            for(label j = 0 ; j < etamax_SLFM ; j++)
            {
                scalar Yj = 0.0;
                fin >> Yj;
                temp[j] = max(0.0, Yj);
            }

            T_[n] = interpolateXY(etaGrid, etaValue_SLFM, temp);

            //read heat source
            for(label j = 0 ; j < etamax_SLFM ; j++)
            {
                fin>>temp[j];
            }

            Q_[n] = interpolateXY(etaGrid, etaValue_SLFM, temp);

            //blank line
            fin.getLine(gbg);
        }

        //DENSITY...
        fin.getLine(gbg);

        //read density (from SLFM library)
        for(label j = 0 ; j < etamax_SLFM ; j++)
        {
            fin>>temp[j];
        }
        //Info<<rho_temp<<endl;

        rho_[n] = interpolateXY(etaGrid, etaValue_SLFM, temp);

        //blank line
        fin.getLine(gbg);

        //ENTHALPY...
        fin.getLine(gbg);

        //read enthalpy (from SLFM library)
        for(label j = 0 ; j < etamax_SLFM ; j++)
        {
            fin>>temp[j];
        }

        h_[n] = interpolateXY(etaGrid, etaValue_SLFM, temp);
    }
}


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

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


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


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


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


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


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