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

namespace Foam
{

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

totalVelocityFvPatchVectorField::totalVelocityFvPatchVectorField
(
    const fvPatch& p,
    const DimensionedField<vector,volMesh>& iF
)
:
    fixedValueFvPatchVectorField(p, iF),
    inletDir_(p.size())

{
    fixedValueFvPatchVectorField::updateCoeffs();
}


totalVelocityFvPatchVectorField::totalVelocityFvPatchVectorField
(
    const totalVelocityFvPatchVectorField& ptf,
    const fvPatch& p,
    const DimensionedField<vector,volMesh>& iF,
    const fvPatchFieldMapper& mapper
)
:
    fixedValueFvPatchVectorField(ptf, p, iF, mapper),
    inletDir_(ptf.inletDir_, mapper)

{
    fixedValueFvPatchVectorField::updateCoeffs();
}


totalVelocityFvPatchVectorField::totalVelocityFvPatchVectorField
(
    const fvPatch& p,
    const DimensionedField<vector,volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchVectorField(p, iF),
    inletDir_(p.size())

{

	inletDir_ = -patch().nf();

    if (dict.found("value"))
    {
        operator==
        (
            vectorField("value", dict, p.size())
        );
        fvPatchField<vector>::updateCoeffs();
    }
    else
    {   
        operator==(this->patchInternalField());
    }

    inletDir_ = inletDir_/mag(inletDir_);
	
}


totalVelocityFvPatchVectorField::totalVelocityFvPatchVectorField
(
    const totalVelocityFvPatchVectorField& tppsf
)
:
    fixedValueFvPatchVectorField(tppsf),
    inletDir_(tppsf.inletDir_)

{
    fixedValueFvPatchVectorField::updateCoeffs();
}


totalVelocityFvPatchVectorField::totalVelocityFvPatchVectorField
(
    const totalVelocityFvPatchVectorField& tppsf,
    const DimensionedField<vector,volMesh>& iF
)
:
    fixedValueFvPatchVectorField(tppsf, iF),
    inletDir_(tppsf.inletDir_)

{
    fixedValueFvPatchVectorField::updateCoeffs();
}


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

void totalVelocityFvPatchVectorField::autoMap
(
    const fvPatchFieldMapper& m
)
{
    vectorField::autoMap(m);
    inletDir_.autoMap(m);
}


void totalVelocityFvPatchVectorField::rmap
(
    const fvPatchVectorField& ptf,
    const labelList& addr
)
{
    fixedValueFvPatchVectorField::rmap(ptf, addr);
    const totalVelocityFvPatchVectorField& tiptf =
           refCast<const totalVelocityFvPatchVectorField>(ptf);

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

void totalVelocityFvPatchVectorField::evaluate()
{
	 if (!this->updated())
    {
        this->updateCoeffs();
    }

    fvPatchField<vector>::evaluate();
}

void totalVelocityFvPatchVectorField::updateCoeffs()
{	

	if (updated())
    {
        return;
    }

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

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

    vectorField n = patch().nf();
    scalarField ndmagS = (n & inletDir())*patch().magSf();

    scalarField clip = neg(sphi);

    scalarField newvel = (sphi*clip)/(rhop*ndmagS);

    operator==(inletDir()*newvel);

    fixedValueFvPatchVectorField::updateCoeffs();
}


void totalVelocityFvPatchVectorField::write(Ostream& os) const
{
    fvPatchVectorField::write(os);

    this->writeEntry("value", os);
}


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

makePatchTypeField(fvPatchVectorField, totalVelocityFvPatchVectorField);

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

} // End namespace Foam

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