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

extendedLeastSquaresVectors.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 "extendedLeastSquaresVectors.H"
00028 #include "surfaceFields.H"
00029 #include "volFields.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035     defineTypeNameAndDebug(extendedLeastSquaresVectors, 0);
00036 }
00037 
00038 
00039 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
00040 
00041 Foam::extendedLeastSquaresVectors::extendedLeastSquaresVectors
00042 (
00043     const fvMesh& mesh,
00044     const scalar minDet
00045 )
00046 :
00047     MeshObject<fvMesh, extendedLeastSquaresVectors>(mesh),
00048     minDet_(minDet),
00049     pVectorsPtr_(NULL),
00050     nVectorsPtr_(NULL),
00051     additionalCellsPtr_(NULL),
00052     additionalVectorsPtr_(NULL)
00053 {}
00054 
00055 
00056 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
00057 
00058 Foam::extendedLeastSquaresVectors::~extendedLeastSquaresVectors()
00059 {
00060     deleteDemandDrivenData(pVectorsPtr_);
00061     deleteDemandDrivenData(nVectorsPtr_);
00062 
00063     deleteDemandDrivenData(additionalCellsPtr_);
00064     deleteDemandDrivenData(additionalVectorsPtr_);
00065 }
00066 
00067 
00068 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00069 
00070 void Foam::extendedLeastSquaresVectors::makeLeastSquaresVectors() const
00071 {
00072     if (debug)
00073     {
00074         Info<< "extendedLeastSquaresVectors::makeLeastSquaresVectors() :"
00075             << "Constructing least square gradient vectors"
00076             << endl;
00077     }
00078 
00079     const fvMesh& mesh = mesh_;
00080 
00081     pVectorsPtr_ = new surfaceVectorField
00082     (
00083         IOobject
00084         (
00085             "LeastSquaresP",
00086             mesh_.pointsInstance(),
00087             mesh_,
00088             IOobject::NO_READ,
00089             IOobject::NO_WRITE,
00090             false
00091         ),
00092         mesh_,
00093         dimensionedVector("zero", dimless/dimLength, vector::zero)
00094     );
00095     surfaceVectorField& lsP = *pVectorsPtr_;
00096 
00097     nVectorsPtr_ = new surfaceVectorField
00098     (
00099         IOobject
00100         (
00101             "LeastSquaresN",
00102             mesh_.pointsInstance(),
00103             mesh_,
00104             IOobject::NO_READ,
00105             IOobject::NO_WRITE,
00106             false
00107         ),
00108         mesh_,
00109         dimensionedVector("zero", dimless/dimLength, vector::zero)
00110     );
00111     surfaceVectorField& lsN = *nVectorsPtr_;
00112 
00113     // Set local references to mesh data
00114     const unallocLabelList& owner = mesh_.owner();
00115     const unallocLabelList& neighbour = mesh_.neighbour();
00116 
00117     // Build the d-vectors
00118     surfaceVectorField d = mesh_.Sf()/(mesh_.magSf()*mesh_.deltaCoeffs());
00119 
00120     if (!mesh_.orthogonal())
00121     {
00122         d -= mesh_.correctionVectors()/mesh_.deltaCoeffs();
00123     }
00124 
00125 
00126     // Set up temporary storage for the dd tensor (before inversion)
00127     symmTensorField dd(mesh_.nCells(), symmTensor::zero);
00128 
00129     forAll(owner, faceI)
00130     {
00131         symmTensor wdd = 1.0/magSqr(d[faceI])*sqr(d[faceI]);
00132 
00133         dd[owner[faceI]] += wdd;
00134         dd[neighbour[faceI]] += wdd;
00135     }
00136 
00137     // Visit the boundaries. Coupled boundaries are taken into account
00138     // in the construction of d vectors.  
00139     forAll(d.boundaryField(), patchI)
00140     {
00141         const fvsPatchVectorField& pd = d.boundaryField()[patchI];
00142 
00143         const fvPatch& p = pd.patch();
00144         const unallocLabelList& faceCells = p.faceCells();
00145 
00146         forAll(pd, patchFaceI)
00147         {
00148             dd[faceCells[patchFaceI]] +=
00149                 (1.0/magSqr(pd[patchFaceI]))*sqr(pd[patchFaceI]);
00150         }
00151     }
00152 
00153     scalarField detdd = det(dd);
00154 
00155     Info<< "max(detdd) = " << max(detdd) << endl;
00156     Info<< "min(detdd) = " << min(detdd) << endl;
00157     Info<< "average(detdd) = " << average(detdd) << endl;
00158 
00159     label nAddCells = 0;
00160     label maxNaddCells = 4*detdd.size();
00161     additionalCellsPtr_ = new List<labelPair>(maxNaddCells);
00162     List<labelPair>& additionalCells_ = *additionalCellsPtr_;
00163 
00164     forAll (detdd, i)
00165     {
00166         label count = 0;
00167 
00168         while (++count < 100 && detdd[i] < minDet_)
00169         {
00170             if (nAddCells == maxNaddCells)
00171             {
00172                 FatalErrorIn
00173                 (
00174                     "extendedLeastSquaresVectors::"
00175                     "makeLeastSquaresVectors() const"
00176                 )   << "nAddCells exceeds maxNaddCells"
00177                     << exit(FatalError);
00178             }
00179 
00180             labelList pointLabels = mesh.cells()[i].labels(mesh.faces());
00181 
00182             scalar maxDetddij = 0.0;
00183 
00184             label addCell = -1;
00185 
00186             forAll(pointLabels, j)
00187             {
00188                 forAll(mesh.pointCells()[pointLabels[j]], k)
00189                 {
00190                     label cellj = mesh.pointCells()[pointLabels[j]][k];
00191 
00192                     if (cellj != i)
00193                     {
00194                         if (cellj != -1)
00195                         {
00196                             vector dCij = (mesh.C()[cellj] - mesh.C()[i]);
00197                             
00198                             symmTensor ddij = 
00199                                 dd[i] + (1.0/magSqr(dCij))*sqr(dCij);
00200 
00201                             scalar detddij = det(ddij);
00202 
00203                             if (detddij > maxDetddij)
00204                             {
00205                                 addCell = cellj;
00206                                 maxDetddij = detddij;
00207                             }
00208                         }
00209                     }
00210                 }
00211             }
00212 
00213             if (addCell != -1)
00214             {
00215                 additionalCells_[nAddCells][0] = i;
00216                 additionalCells_[nAddCells++][1] = addCell;
00217                 vector dCij = mesh.C()[addCell] - mesh.C()[i];
00218                 dd[i] += (1.0/magSqr(dCij))*sqr(dCij);
00219                 detdd[i] = det(dd[i]);
00220             }
00221         }
00222     }
00223 
00224     additionalCells_.setSize(nAddCells);
00225 
00226     Info<< "max(detdd) = " << max(detdd) << endl;
00227     Info<< "min(detdd) = " << min(detdd) << endl;
00228     Info<< "average(detdd) = " << average(detdd) << endl;
00229     Info<< "nAddCells/nCells = " << scalar(nAddCells)/mesh.nCells() << endl;
00230 
00231     // Invert the dd tensor
00232     symmTensorField invDd = inv(dd);
00233 
00234 
00235     // Revisit all faces and calculate the lsP and lsN vectors
00236     forAll(owner, faceI)
00237     {
00238         lsP[faceI] =
00239             (1.0/magSqr(d[faceI]))*(invDd[owner[faceI]] & d[faceI]);
00240 
00241         lsN[faceI] =
00242             ((-1.0)/magSqr(d[faceI]))*(invDd[neighbour[faceI]] & d[faceI]);
00243     }
00244 
00245     forAll(lsP.boundaryField(), patchI)
00246     {
00247         const fvsPatchVectorField& pd = d.boundaryField()[patchI];
00248 
00249         fvsPatchVectorField& patchLsP = lsP.boundaryField()[patchI];
00250 
00251         const fvPatch& p = patchLsP.patch();
00252         const unallocLabelList& faceCells = p.faceCells();
00253 
00254         forAll(p, patchFaceI)
00255         {
00256             patchLsP[patchFaceI] =
00257                 (1.0/magSqr(pd[patchFaceI]))
00258                *(invDd[faceCells[patchFaceI]] & pd[patchFaceI]);
00259         }
00260     }
00261 
00262 
00263     additionalVectorsPtr_ = new vectorField(additionalCells_.size());
00264     vectorField& additionalVectors_ = *additionalVectorsPtr_;
00265 
00266     forAll(additionalCells_, i)
00267     {
00268         vector dCij = 
00269             mesh.C()[additionalCells_[i][1]] - mesh.C()[additionalCells_[i][0]];
00270 
00271         additionalVectors_[i] =
00272             (1.0/magSqr(dCij))*(invDd[additionalCells_[i][0]] & dCij);
00273     }
00274 
00275     if (debug)
00276     {
00277         Info<< "extendedLeastSquaresVectors::makeLeastSquaresVectors() :"
00278             << "Finished constructing least square gradient vectors"
00279             << endl;
00280     }
00281 }
00282 
00283 
00284 const Foam::surfaceVectorField&
00285 Foam::extendedLeastSquaresVectors::pVectors() const
00286 {
00287     if (!pVectorsPtr_)
00288     {
00289         makeLeastSquaresVectors();
00290     }
00291 
00292     return *pVectorsPtr_;
00293 }
00294 
00295 
00296 const Foam::surfaceVectorField&
00297 Foam::extendedLeastSquaresVectors::nVectors() const
00298 {
00299     if (!nVectorsPtr_)
00300     {
00301         makeLeastSquaresVectors();
00302     }
00303 
00304     return *nVectorsPtr_;
00305 }
00306 
00307 
00308 const Foam::List<Foam::labelPair>&
00309 Foam::extendedLeastSquaresVectors::additionalCells() const
00310 {
00311     if (!additionalCellsPtr_)
00312     {
00313         makeLeastSquaresVectors();
00314     }
00315 
00316     return *additionalCellsPtr_;
00317 }
00318 
00319 
00320 const Foam::vectorField&
00321 Foam::extendedLeastSquaresVectors::additionalVectors() const
00322 {
00323     if (!additionalVectorsPtr_)
00324     {
00325         makeLeastSquaresVectors();
00326     }
00327 
00328     return *additionalVectorsPtr_;
00329 }
00330 
00331 
00332 bool Foam::extendedLeastSquaresVectors::movePoints()
00333 {
00334     deleteDemandDrivenData(pVectorsPtr_);
00335     deleteDemandDrivenData(nVectorsPtr_);
00336 
00337     deleteDemandDrivenData(additionalCellsPtr_);
00338     deleteDemandDrivenData(additionalVectorsPtr_);
00339 
00340     return true;
00341 }
00342 
00343 
00344 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd