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

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

namespace Foam
{

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

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

totalPressureCorrectorCompFvPatchScalarField::totalPressureCorrectorCompFvPatchScalarField
(
    const fvPatch& p,
    const DimensionedField<scalar,volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchScalarField(p, iF, dict),
 	p0_("p0", dict, p.size())
{   
   fvPatchField<scalar>::updateCoeffs();
   evaluate();
}

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

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


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


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

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


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

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

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

tmp<Field<scalar> > totalPressureCorrectorCompFvPatchScalarField::valueInternalCoeffs
(
    const tmp<scalarField>&
) const
{
    return tmp<Field<scalar> >
    (
        new Field<scalar>(this->size(), pTraits<scalar>::zero)
    );
}

tmp<Field<scalar> > totalPressureCorrectorCompFvPatchScalarField::gradientBoundaryCoeffs() const
{
    return tmp<Field<scalar> >
    (
        new Field<scalar>(this->size(), pTraits<scalar>::zero)
    );
}

tmp<Field<scalar> > totalPressureCorrectorCompFvPatchScalarField::valueBoundaryCoeffs
(
    const tmp<scalarField>&
) const
{
    return tmp<Field<scalar> >
    (
        new Field<scalar>(this->size(), pTraits<scalar>::zero)
    );
}

tmp<Field<scalar> > totalPressureCorrectorCompFvPatchScalarField::gradientInternalCoeffs() const
{
	const fvsPatchField<scalar>&  srUA = 
    patch().lookupPatchField<surfaceScalarField, scalar>("srUA");

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

    return( deltaM()/(-srUA*patch().magSf()*srho) ); //to remove the laplacian operator (see gaussLaplacianScheme)
}

void totalPressureCorrectorCompFvPatchScalarField::updateCoeffs()
{
    //if (updated())
    {
        return;
    }

    operator==
    (
        patchInternalField()//patchInternalField()*deltaM()*Cu()
    );
    
    fixedValueFvPatchScalarField::updateCoeffs();
}

Field<scalar> totalPressureCorrectorCompFvPatchScalarField::Cu() const
{
	const fvPatchField<vector>& UF = patch().lookupPatchField<volVectorField, vector>("U");                       

	const totalVelocityFvPatchVectorField& UB = refCast<const totalVelocityFvPatchVectorField>(UF);

	const vectorField& Udir_ = UB.inletDir();
 	
	const fvPatchField<scalar>&  T = 
    patch().lookupPatchField<volScalarField, scalar>("T");

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

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

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

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

    scalarField dmdp = - sqr(Udir_ & patch().Sf())/((UF & patch().Sf())) * 
                                ( scalar(1) + (gammaB - scalar(1.0))/(2.0*gammaB*RB * T) * sqr(sphi/(srho * (Udir_ & patch().Sf()))) );

     scalarField dpdm( - (p0_ * sphi)
				         / (sqr(srho) * sqr(Udir_ & patch().Sf()) * RB * T 
				         * (pow(
			                   (scalar(1.0) + (gammaB - scalar(1.0))/scalar(2.0) * sqr(sphi)
                                /(sqr(srho) * sqr(Udir_ & patch().Sf()) * gammaB * RB * T)),
			                   (2.0*gammaB - scalar(1.0))/(gammaB - scalar(1.0)))))

          );


    return (1/(dmdp ));

}


Field<scalar> totalPressureCorrectorCompFvPatchScalarField::deltaM() const
{

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

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

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

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

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

    scalarField Dp = patch().magSf()*patch().deltaCoeffs()*srUA;

    scalarField coeff = srho*Dp/(scalar(1.0) + srho*Dp*Cu() - (sphi/srho)*Cu()/(RB*T));

	return (coeff); 
}

void totalPressureCorrectorCompFvPatchScalarField::write(Ostream& os) const
{
    fixedValueFvPatchField<scalar>::write(os);
		p0_.writeEntry("p0", os);
}


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

makePatchTypeField(fvPatchScalarField, totalPressureCorrectorCompFvPatchScalarField);

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

} // End namespace Foam

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