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

leastSquaresVectors.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 "leastSquaresVectors.H"
00028 #include "surfaceFields.H"
00029 #include "volFields.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035     defineTypeNameAndDebug(leastSquaresVectors, 0);
00036 }
00037 
00038 
00039 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
00040 
00041 Foam::leastSquaresVectors::leastSquaresVectors(const fvMesh& mesh)
00042 :
00043     MeshObject<fvMesh, leastSquaresVectors>(mesh),
00044     pVectorsPtr_(NULL),
00045     nVectorsPtr_(NULL)
00046 {}
00047 
00048 
00049 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
00050 
00051 Foam::leastSquaresVectors::~leastSquaresVectors()
00052 {
00053     deleteDemandDrivenData(pVectorsPtr_);
00054     deleteDemandDrivenData(nVectorsPtr_);
00055 }
00056 
00057 
00058 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00059 
00060 void Foam::leastSquaresVectors::makeLeastSquaresVectors() const
00061 {
00062     if (debug)
00063     {
00064         Info<< "leastSquaresVectors::makeLeastSquaresVectors() :"
00065             << "Constructing least square gradient vectors"
00066             << endl;
00067     }
00068 
00069     const fvMesh& mesh = mesh_;
00070 
00071     pVectorsPtr_ = new surfaceVectorField
00072     (
00073         IOobject
00074         (
00075             "LeastSquaresP",
00076             mesh_.pointsInstance(),
00077             mesh_,
00078             IOobject::NO_READ,
00079             IOobject::NO_WRITE,
00080             false
00081         ),
00082         mesh_,
00083         dimensionedVector("zero", dimless/dimLength, vector::zero)
00084     );
00085     surfaceVectorField& lsP = *pVectorsPtr_;
00086 
00087     nVectorsPtr_ = new surfaceVectorField
00088     (
00089         IOobject
00090         (
00091             "LeastSquaresN",
00092             mesh_.pointsInstance(),
00093             mesh_,
00094             IOobject::NO_READ,
00095             IOobject::NO_WRITE,
00096             false
00097         ),
00098         mesh_,
00099         dimensionedVector("zero", dimless/dimLength, vector::zero)
00100     );
00101     surfaceVectorField& lsN = *nVectorsPtr_;
00102 
00103     // Set local references to mesh data
00104     const unallocLabelList& owner = mesh_.owner();
00105     const unallocLabelList& neighbour = mesh_.neighbour();
00106 
00107     const volVectorField& C = mesh.C();
00108     const surfaceScalarField& w = mesh.weights();
00109     const surfaceScalarField& magSf = mesh.magSf();
00110 
00111 
00112     // Set up temporary storage for the dd tensor (before inversion)
00113     symmTensorField dd(mesh_.nCells(), symmTensor::zero);
00114 
00115     forAll(owner, facei)
00116     {
00117         label own = owner[facei];
00118         label nei = neighbour[facei];
00119 
00120         vector d = C[nei] - C[own];
00121         symmTensor wdd = (magSf[facei]/magSqr(d))*sqr(d);
00122 
00123         dd[own] += (1 - w[facei])*wdd;
00124         dd[nei] += w[facei]*wdd;
00125     }
00126 
00127 
00128     forAll(lsP.boundaryField(), patchi)
00129     {
00130         const fvsPatchScalarField& pw = w.boundaryField()[patchi];
00131         const fvsPatchScalarField& pMagSf = magSf.boundaryField()[patchi];
00132 
00133         const fvPatch& p = pw.patch();
00134         const unallocLabelList& faceCells = p.patch().faceCells();
00135 
00136         // Build the d-vectors
00137         vectorField pd =
00138             mesh.Sf().boundaryField()[patchi]
00139            /(
00140                mesh.magSf().boundaryField()[patchi]
00141               *mesh.deltaCoeffs().boundaryField()[patchi]
00142            );
00143 
00144         if (!mesh.orthogonal())
00145         {
00146             pd -= mesh.correctionVectors().boundaryField()[patchi]
00147                 /mesh.deltaCoeffs().boundaryField()[patchi];
00148         }
00149 
00150         if (p.coupled())
00151         {
00152             forAll(pd, patchFacei)
00153             {
00154                 const vector& d = pd[patchFacei];
00155 
00156                 dd[faceCells[patchFacei]] +=
00157                     ((1 - pw[patchFacei])*pMagSf[patchFacei]/magSqr(d))*sqr(d);
00158             }
00159         }
00160         else
00161         {
00162             forAll(pd, patchFacei)
00163             {
00164                 const vector& d = pd[patchFacei];
00165 
00166                 dd[faceCells[patchFacei]] +=
00167                     (pMagSf[patchFacei]/magSqr(d))*sqr(d);
00168             }
00169         }
00170     }
00171 
00172 
00173     // Invert the dd tensor
00174     symmTensorField invDd = inv(dd);
00175 
00176 
00177     // Revisit all faces and calculate the lsP and lsN vectors
00178     forAll(owner, facei)
00179     {
00180         label own = owner[facei];
00181         label nei = neighbour[facei];
00182 
00183         vector d = C[nei] - C[own];
00184         scalar magSfByMagSqrd = magSf[facei]/magSqr(d);
00185 
00186         lsP[facei] = (1 - w[facei])*magSfByMagSqrd*(invDd[own] & d);
00187         lsN[facei] = -w[facei]*magSfByMagSqrd*(invDd[nei] & d);
00188     }
00189 
00190     forAll(lsP.boundaryField(), patchi)
00191     {
00192         fvsPatchVectorField& patchLsP = lsP.boundaryField()[patchi];
00193 
00194         const fvsPatchScalarField& pw = w.boundaryField()[patchi];
00195         const fvsPatchScalarField& pMagSf = magSf.boundaryField()[patchi];
00196 
00197         const fvPatch& p = pw.patch();
00198         const unallocLabelList& faceCells = p.faceCells();
00199 
00200         // Build the d-vectors
00201         vectorField pd =
00202             mesh.Sf().boundaryField()[patchi]
00203            /(
00204                mesh.magSf().boundaryField()[patchi]
00205               *mesh.deltaCoeffs().boundaryField()[patchi]
00206            );
00207 
00208         if (!mesh.orthogonal())
00209         {
00210             pd -= mesh.correctionVectors().boundaryField()[patchi]
00211                 /mesh.deltaCoeffs().boundaryField()[patchi];
00212         }
00213 
00214 
00215         if (p.coupled())
00216         {
00217             forAll(pd, patchFacei)
00218             {
00219                 const vector& d = pd[patchFacei];
00220 
00221                 patchLsP[patchFacei] =
00222                     ((1 - pw[patchFacei])*pMagSf[patchFacei]/magSqr(d))
00223                    *(invDd[faceCells[patchFacei]] & d);
00224             }
00225         }
00226         else
00227         {
00228             forAll(pd, patchFacei)
00229             {
00230                 const vector& d = pd[patchFacei];
00231 
00232                 patchLsP[patchFacei] =
00233                     pMagSf[patchFacei]*(1.0/magSqr(d))
00234                    *(invDd[faceCells[patchFacei]] & d);
00235             }
00236         }
00237     }
00238 
00239 
00240     // For 3D meshes check the determinant of the dd tensor and switch to
00241     // Gauss if it is less than 3
00242     /* Currently the det(dd[celli]) criterion is incorrect: dd is weighted by Sf
00243     if (mesh.nGeometricD() == 3)
00244     {
00245         label nBadCells = 0;
00246 
00247         const cellList& cells = mesh.cells();
00248         const scalarField& V = mesh.V();
00249         const surfaceVectorField& Sf = mesh.Sf();
00250         const surfaceScalarField& w = mesh.weights();
00251 
00252         forAll (dd, celli)
00253         {
00254             if (det(dd[celli]) < 3)
00255             {
00256                 nBadCells++;
00257 
00258                 const cell& c = cells[celli];
00259 
00260                 forAll(c, cellFacei)
00261                 {
00262                     label facei = c[cellFacei];
00263 
00264                     if (mesh.isInternalFace(facei))
00265                     {
00266                         scalar wf = max(min(w[facei], 0.8), 0.2);
00267 
00268                         if (celli == owner[facei])
00269                         {
00270                             lsP[facei] = (1 - wf)*Sf[facei]/V[celli];
00271                         }
00272                         else
00273                         {
00274                             lsN[facei] = -wf*Sf[facei]/V[celli];
00275                         }
00276                     }
00277                     else
00278                     {
00279                         label patchi = mesh.boundaryMesh().whichPatch(facei);
00280 
00281                         if (mesh.boundary()[patchi].size())
00282                         {
00283                             label patchFacei =
00284                                 facei - mesh.boundaryMesh()[patchi].start();
00285 
00286                             if (mesh.boundary()[patchi].coupled())
00287                             {
00288                                 scalar wf = max
00289                                 (
00290                                     min
00291                                     (
00292                                         w.boundaryField()[patchi][patchFacei],
00293                                         0.8
00294                                     ),
00295                                     0.2
00296                                 );
00297 
00298                                 lsP.boundaryField()[patchi][patchFacei] =
00299                                     (1 - wf)
00300                                    *Sf.boundaryField()[patchi][patchFacei]
00301                                    /V[celli];
00302                             }
00303                             else
00304                             {
00305                                 lsP.boundaryField()[patchi][patchFacei] =
00306                                     Sf.boundaryField()[patchi][patchFacei]
00307                                    /V[celli];
00308                             }
00309                         }
00310                     }
00311                 }
00312             }
00313         }
00314 
00315         if (debug)
00316         {
00317             InfoIn("leastSquaresVectors::makeLeastSquaresVectors()")
00318                 << "number of bad cells switched to Gauss = " << nBadCells
00319                 << endl;
00320         }
00321     }
00322     */
00323 
00324     if (debug)
00325     {
00326         Info<< "leastSquaresVectors::makeLeastSquaresVectors() :"
00327             << "Finished constructing least square gradient vectors"
00328             << endl;
00329     }
00330 }
00331 
00332 
00333 const Foam::surfaceVectorField& Foam::leastSquaresVectors::pVectors() const
00334 {
00335     if (!pVectorsPtr_)
00336     {
00337         makeLeastSquaresVectors();
00338     }
00339 
00340     return *pVectorsPtr_;
00341 }
00342 
00343 
00344 const Foam::surfaceVectorField& Foam::leastSquaresVectors::nVectors() const
00345 {
00346     if (!nVectorsPtr_)
00347     {
00348         makeLeastSquaresVectors();
00349     }
00350 
00351     return *nVectorsPtr_;
00352 }
00353 
00354 
00355 bool Foam::leastSquaresVectors::movePoints()
00356 {
00357     deleteDemandDrivenData(pVectorsPtr_);
00358     deleteDemandDrivenData(nVectorsPtr_);
00359 
00360     return true;
00361 }
00362 
00363 
00364 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd