/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 1991-2015 OpenCFD Ltd.
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file uses OpenFOAM and is provided as free software without any warranty

    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 2 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, write to the Free Software Foundation,
    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Authors
    Luca Mangani   HSLU email: luca.mangani@hslu.ch
    Marwan Darwish AUB  email: darwish@aub.edu.lb
    Fadl Moukalled AUB  email: moukalled@aub.edu.lb

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

#include "totalTempFvPatchScalarField.H"
#include "addToRunTimeSelectionTable.H"
#include "fvPatchFieldMapper.H"
#include "volFields.H"
#include "surfaceFields.H"

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

namespace Foam
{

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

totalTempFvPatchScalarField::totalTempFvPatchScalarField
(
    const fvPatch& p,
    const DimensionedField<scalar,volMesh>& iF
)
:
    fixedValueFvPatchScalarField(p, iF),
    T0_(p.size(), 0.0)
{}


totalTempFvPatchScalarField::totalTempFvPatchScalarField
(
    const totalTempFvPatchScalarField& ptf,
    const fvPatch& p,
    const DimensionedField<scalar,volMesh>& iF,
    const fvPatchFieldMapper& mapper
)
:
    fixedValueFvPatchScalarField(ptf, p, iF, mapper),
    T0_(ptf.T0_, mapper)
{}


totalTempFvPatchScalarField::totalTempFvPatchScalarField
(
    const fvPatch& p,
    const DimensionedField<scalar,volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchScalarField(p, iF),
    T0_("T0", dict, p.size())
{
    if (dict.found("value"))
    {
        fvPatchField<scalar>::operator=
        (
            scalarField("value", dict, p.size())
        );
    }
    else
    {
        fvPatchField<scalar>::operator=(T0_);
    }
}


totalTempFvPatchScalarField::totalTempFvPatchScalarField
(
    const totalTempFvPatchScalarField& tppsf
)
:
    fixedValueFvPatchScalarField(tppsf),
    T0_(tppsf.T0_)
{}


totalTempFvPatchScalarField::totalTempFvPatchScalarField
(
    const totalTempFvPatchScalarField& tppsf,
    const DimensionedField<scalar,volMesh>& iF
)
:
    fixedValueFvPatchScalarField(tppsf, iF),
    T0_(tppsf.T0_)
{}


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

void totalTempFvPatchScalarField::autoMap
(
    const fvPatchFieldMapper& m
)
{
    scalarField::autoMap(m);
    T0_.autoMap(m);
}


void totalTempFvPatchScalarField::rmap
(
    const fvPatchScalarField& ptf,
    const labelList& addr
)
{
    fixedValueFvPatchScalarField::rmap(ptf, addr);

    const totalTempFvPatchScalarField& tiptf =
        refCast<const totalTempFvPatchScalarField>(ptf);

    T0_.rmap(tiptf.T0_, addr);
}

/*
void totalTempFvPatchScalarField::evaluate()
{

	  if (!this->updated())
    {
        this->updateCoeffs();
    }

    fvPatchField<scalar>::evaluate();
}
*/
void totalTempFvPatchScalarField::updateCoeffs()
{
    if (updated())
    {
        return;
    }

    const fvPatchField<vector>& Up =
        patch().lookupPatchField<volVectorField, vector>("U");

    const fvPatchField<scalar>& gammaB =
        patch().lookupPatchField<volScalarField, scalar>("gammaGas");

    const fvPatchField<scalar>& RB =
        patch().lookupPatchField<volScalarField, scalar>("RGas");

    scalarField Cp = gammaB*RB/(gammaB-1.0);

    operator==
    (
        (T0_ - 0.5*magSqr(Up)/Cp)
    );


    fixedValueFvPatchScalarField::updateCoeffs();
}


void totalTempFvPatchScalarField::write(Ostream& os) const
{
    fvPatchScalarField::write(os);
    T0_.writeEntry("T0", os);
    this->writeEntry("value", os);
}


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

makePatchTypeField(fvPatchScalarField, totalTempFvPatchScalarField);

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

} // End namespace Foam

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