/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2012 OpenFOAM Foundation
     \\/     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 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/>.
 
 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 "noSlipWallFvPatchVectorField.H"
#include "volFields.H"
#include "addToRunTimeSelectionTable.H"
#include "fvPatchFieldMapper.H"
#include "surfaceFields.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

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

noSlipWallFvPatchVectorField::noSlipWallFvPatchVectorField
(
    const fvPatch& p,
    const DimensionedField<vector, volMesh>& iF
)
:
    fixedValueFvPatchField<vector>(p, iF)
{}


noSlipWallFvPatchVectorField::noSlipWallFvPatchVectorField
(
    const noSlipWallFvPatchVectorField& ptf,
    const fvPatch& p,
    const DimensionedField<vector, volMesh>& iF,
    const fvPatchFieldMapper& mapper
)
:
    fixedValueFvPatchField<vector>(ptf, p, iF, mapper)
{}


noSlipWallFvPatchVectorField::noSlipWallFvPatchVectorField
(
    const fvPatch& p,
    const DimensionedField<vector, volMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchField<vector>(p, iF, dict)
{}


noSlipWallFvPatchVectorField::noSlipWallFvPatchVectorField
(
    const noSlipWallFvPatchVectorField& ptf
)
:
    fixedValueFvPatchField<vector>(ptf)
{}


noSlipWallFvPatchVectorField::noSlipWallFvPatchVectorField
(
    const noSlipWallFvPatchVectorField& ptf,
    const DimensionedField<vector, volMesh>& iF
)
:
    fixedValueFvPatchField<vector>(ptf, iF)
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
tmp<Field<vector> > noSlipWallFvPatchVectorField::gradientInternalCoeffs() const
{

    vectorField normal = this->patch().nf();
    vectorField impCoeff(this->size(),pTraits<vector>::zero);
    forAll(impCoeff,faceI)
    {
        impCoeff[faceI][0] = (scalar(1.0) - pow(normal[faceI][0],2));
        impCoeff[faceI][1] = (scalar(1.0) - pow(normal[faceI][1],2));
        impCoeff[faceI][2] = (scalar(1.0) - pow(normal[faceI][2],2));
    }
    
    return -impCoeff*this->patch().deltaCoeffs();
}


tmp<Field<vector> > noSlipWallFvPatchVectorField::gradientBoundaryCoeffs() const
{
    // Explicit contribution to the gradient of the boundary
    // Implicit Contribuution on right hand side -> GaussLaplacian already 
    // accounts for the sign switch in the equation discretisation
    // -> no sign change needed
    vectorField normal = this->patch().nf();
    vectorField expCoeff(this->size(),pTraits<vector>::zero);
    vectorField boundTanField = *this - ((*this) & normal) * normal;
    vectorField intField = this->patchInternalField();
    forAll(expCoeff,faceI)
    {
        expCoeff[faceI][0] = boundTanField[faceI][0]
                             +normal[faceI][0]*normal[faceI][1]*intField[faceI][1]
                             +normal[faceI][2]*normal[faceI][0]*intField[faceI][2];
        expCoeff[faceI][1] = boundTanField[faceI][1]
                             +normal[faceI][1]*normal[faceI][0]*intField[faceI][0]
                             +normal[faceI][2]*normal[faceI][1]*intField[faceI][2];
        expCoeff[faceI][2] = boundTanField[faceI][2]
                             +normal[faceI][2]*normal[faceI][0]*intField[faceI][0]
                             +normal[faceI][1]*normal[faceI][2]*intField[faceI][1];
    }

    return this->patch().deltaCoeffs()*expCoeff;
} 
void noSlipWallFvPatchVectorField::updateCoeffs()
{
    if (this->updated())
    {
        return;
    }
    fixedValueFvPatchField<vector>::updateCoeffs();
}


void noSlipWallFvPatchVectorField::write(Ostream& os) const
{
    fvPatchField<vector>::write(os);
    this->writeEntry("value", os);
}

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

makePatchTypeField(fvPatchVectorField, noSlipWallFvPatchVectorField);

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

} // End namespace Foam

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