/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2011-2013 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 "wsggmAbsorptionEmission.H"
#include "addToRunTimeSelectionTable.H"
#include "unitConversion.H"
#include "zeroGradientFvPatchFields.H"
#include "basicSpecieMixture.H"

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

namespace Foam
{
    namespace radiation
    {
        defineTypeNameAndDebug(wsggmAbsorptionEmission, 0);

        addToRunTimeSelectionTable
        (
            absorptionEmissionModel,
            wsggmAbsorptionEmission,
            dictionary
        );
    }
}


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

Foam::radiation::wsggmAbsorptionEmission::wsggmAbsorptionEmission
(
    const dictionary& dict,
    const fvMesh& mesh
)
:
    absorptionEmissionModel(dict, mesh),
    coeffsDict_((dict.subDict(typeName + "Coeffs"))),
    thermo_(mesh.lookupObject<fluidThermo>("thermophysicalProperties")),
    emissivityCoeffs_(coeffsDict_.lookup("emissivityCoeffs")),
    fittingFactors_(coeffsDict_.lookup("fittingFactors")),
    pathLength_(coeffsDict_.lookup("pathLength"))
{
    if (!isA<basicMultiComponentMixture>(thermo_))
    {
        FatalErrorIn
        (
            "radiation::wsggmAbsorptionEmission::wsggmAbsorptionEmission"
            "("
                "const dictionary&, "
                "const fvMesh&"
            ")"
        )   << "Model requires a multi-component thermo package"
            << abort(FatalError);
    }
}

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

Foam::radiation::wsggmAbsorptionEmission::~wsggmAbsorptionEmission()
{}


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

Foam::tmp<Foam::volScalarField>
Foam::radiation::wsggmAbsorptionEmission::aCont(const label bandI) const
{

    const basicSpecieMixture& mixture =
        dynamic_cast<const basicSpecieMixture&>(thermo_);

    const volScalarField& T = thermo_.T();
    const volScalarField& p = thermo_.p();

    tmp<volScalarField> ta
    (
        new volScalarField
        (
            IOobject
            (
                "aCont" + name(bandI),
                mesh().time().timeName(),
                mesh(),
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            mesh(),
            dimensionedScalar("a", dimless/dimLength, 0.0),
            zeroGradientFvPatchVectorField::typeName
        )
    );

    scalarField& a = ta.ref().primitiveFieldRef();

    label indexCO2, indexH2O;
    scalar wCO2, wH2O;
    forAll(mixture.Y(), specieI)
    {
        if(mixture.Y(specieI).name() == "CO2")
        {
            indexCO2 = specieI;
            wCO2 = mixture.W(indexCO2);
        }
        else if(mixture.Y(specieI).name() == "H2O")
        {
            indexH2O = specieI;
            wH2O = mixture.W(indexH2O);
        }
    }

    forAll(a,celli)
    {
        scalar invWt = 0.0;
        forAll(mixture.Y(), specieI)
        {
            invWt += mixture.Y(specieI)[celli]/mixture.W(specieI);
        }
        scalar meanWt = 1.0/invWt; //Mean molecular weight [kg/kmole]

        // Sum of partial pressures of all absorbing gases [atm]
        scalar Pcell_atm =  paToAtm(meanWt*p[celli]*(mixture.Y(indexCO2)[celli]/wCO2 + mixture.Y(indexH2O)[celli]/wH2O));

        // P*S at Eq.(1) of Ref. [atm*m]
        scalar presPathLength = Pcell_atm*pathLength_.value();

        // Limit cell temperature [K]
        scalar Tcell = min(T[celli],2400.0);

        scalar emissivity = 0.0;
        forAll(emissivityCoeffs_, i) //k_i at Eq.(1) of Ref.
        {
            scalar weightingFactor = 0.0;
            for(label j=0 ; j<4 ; j++)
            {
                //a_epsilon_i at Eq.(3) of Ref.
                weightingFactor += fittingFactors_[i][j]*pow(Tcell, j);
            }
            //epsilon at Eq.(1) of Ref.
            emissivity += weightingFactor * (1.0 - exp(-1.0*emissivityCoeffs_[i] * presPathLength));
        }
        emissivity = min(emissivity, 0.99999);

        //Fluent theory guide(ver. 13) Eq.(5-106) [1/m]
        a[celli] = max(1e-2, (-1.0)*log(1.0-emissivity)/pathLength_.value());
    }

    ta.ref().correctBoundaryConditions();

    return ta;
}


Foam::tmp<Foam::volScalarField>
Foam::radiation::wsggmAbsorptionEmission::eCont(const label bandI) const
{
   return aCont(bandI);
}


Foam::tmp<Foam::volScalarField>
Foam::radiation::wsggmAbsorptionEmission::ECont(const label bandI) const
{
    tmp<volScalarField> E
    (
        new volScalarField
        (
            IOobject
            (
                "ECont" + name(bandI),
                mesh_.time().timeName(),
                mesh_,
                IOobject::NO_READ,
                IOobject::NO_WRITE
            ),
            mesh_,
            dimensionedScalar("E", dimMass/dimLength/pow3(dimTime), 0.0)
        )
    );

    return E;
}


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