OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

leastSquaresGrad.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024     
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "leastSquaresGrad.H"
00028 #include "leastSquaresVectors.H"
00029 #include "gaussGrad.H"
00030 #include "fvMesh.H"
00031 #include "volMesh.H"
00032 #include "surfaceMesh.H"
00033 #include "GeometricField.H"
00034 #include "zeroGradientFvPatchField.H"
00035 
00036 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00037 
00038 namespace Foam
00039 {
00040 
00041 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00042 
00043 namespace fv
00044 {
00045 
00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00047 
00048 template<class Type>
00049 tmp
00050 <
00051     GeometricField
00052     <
00053         typename outerProduct<vector, Type>::type, fvPatchField, volMesh
00054     >
00055 >
00056 leastSquaresGrad<Type>::grad
00057 (
00058     const GeometricField<Type, fvPatchField, volMesh>& vsf
00059 ) const
00060 {
00061     typedef typename outerProduct<vector, Type>::type GradType;
00062 
00063     const fvMesh& mesh = vsf.mesh();
00064 
00065     tmp<GeometricField<GradType, fvPatchField, volMesh> > tlsGrad
00066     (
00067         new GeometricField<GradType, fvPatchField, volMesh>
00068         (
00069             IOobject
00070             (
00071                 "grad("+vsf.name()+')',
00072                 vsf.instance(),
00073                 mesh,
00074                 IOobject::NO_READ,
00075                 IOobject::NO_WRITE
00076             ),
00077             mesh,
00078             dimensioned<GradType>
00079             (
00080                 "zero",
00081                 vsf.dimensions()/dimLength,
00082                 pTraits<GradType>::zero
00083             ),
00084             zeroGradientFvPatchField<GradType>::typeName
00085         )
00086     );
00087     GeometricField<GradType, fvPatchField, volMesh>& lsGrad = tlsGrad();
00088 
00089     // Get reference to least square vectors
00090     const leastSquaresVectors& lsv = leastSquaresVectors::New(mesh);
00091 
00092     const surfaceVectorField& ownLs = lsv.pVectors();
00093     const surfaceVectorField& neiLs = lsv.nVectors();
00094 
00095     const unallocLabelList& own = mesh.owner();
00096     const unallocLabelList& nei = mesh.neighbour();
00097 
00098     forAll(own, facei)
00099     {
00100         register label ownFaceI = own[facei];
00101         register label neiFaceI = nei[facei];
00102 
00103         Type deltaVsf = vsf[neiFaceI] - vsf[ownFaceI];
00104 
00105         lsGrad[ownFaceI] += ownLs[facei]*deltaVsf;
00106         lsGrad[neiFaceI] -= neiLs[facei]*deltaVsf;
00107     }
00108 
00109     // Boundary faces
00110     forAll(vsf.boundaryField(), patchi)
00111     {
00112         const fvsPatchVectorField& patchOwnLs = ownLs.boundaryField()[patchi];
00113 
00114         const unallocLabelList& faceCells =
00115             lsGrad.boundaryField()[patchi].patch().faceCells();
00116 
00117         if (vsf.boundaryField()[patchi].coupled())
00118         {
00119             Field<Type> neiVsf = 
00120                 vsf.boundaryField()[patchi].patchNeighbourField();
00121 
00122             forAll(neiVsf, patchFaceI)
00123             {
00124                 lsGrad[faceCells[patchFaceI]] +=
00125                     patchOwnLs[patchFaceI]
00126                    *(neiVsf[patchFaceI] - vsf[faceCells[patchFaceI]]);
00127             }
00128         }
00129         else
00130         {
00131             const fvPatchField<Type>& patchVsf = vsf.boundaryField()[patchi];
00132 
00133             forAll(patchVsf, patchFaceI)
00134             {
00135                 lsGrad[faceCells[patchFaceI]] +=
00136                      patchOwnLs[patchFaceI]
00137                     *(patchVsf[patchFaceI] - vsf[faceCells[patchFaceI]]);
00138             }
00139         }
00140     }
00141 
00142 
00143     lsGrad.correctBoundaryConditions();
00144     gaussGrad<Type>::correctBoundaryConditions(vsf, lsGrad);
00145 
00146     return tlsGrad;
00147 }
00148 
00149 
00150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00151 
00152 } // End namespace fv
00153 
00154 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00155 
00156 } // End namespace Foam
00157 
00158 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd