/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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 "totalPressureCompFvPatchScalarField.H"
#include "addToRunTimeSelectionTable.H"
#include "fvPatchFieldMapper.H"
#include "volFields.H"
#include "surfaceFields.H"

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

namespace Foam
{

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

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


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


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


totalPressureCompFvPatchScalarField::totalPressureCompFvPatchScalarField
(
    const totalPressureCompFvPatchScalarField& tppsf
)
:
    fixedValueFvPatchScalarField(tppsf),
    p0_(tppsf.p0_)
{}


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


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

void totalPressureCompFvPatchScalarField::autoMap
(
    const fvPatchFieldMapper& m
)
{
    scalarField::autoMap(m);
    p0_.autoMap(m);
}


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

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

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


void totalPressureCompFvPatchScalarField::updateCoeffs()
{
    if (updated())
    {
        return;
    }

    const fvPatchScalarField& TB =
        patch().lookupPatchField<volScalarField, scalar>("T");

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

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

	const fvsPatchField<scalar>&  sphi = 
        patch().lookupPatchField<surfaceScalarField, scalar>("mdotf");

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

    scalarField newp = max(min(p0_/pow((scalar(1.0) + (gammaB - scalar(1.0) )*(sqr(sphi)
                                /(sqr(srho) * sqr(patch().magSf())))/(scalar(2.0) * gammaB * RB * TB  ))
               , gammaB/(gammaB - scalar(1.0))), p0_),SMALL);

    operator==
    (
        newp
    );

    fixedValueFvPatchScalarField::updateCoeffs();
}

void totalPressureCompFvPatchScalarField::write(Ostream& os) const
{
    fvPatchScalarField::write(os);
    p0_.writeEntry("p0", os);
    writeEntry("value", os);
}


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

makePatchTypeField(fvPatchScalarField, totalPressureCompFvPatchScalarField);

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

} // End namespace Foam

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