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

primitiveMeshCellCentresAndVols.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 Description
00026     Efficient cell-centre calculation using face-addressing, face-centres and
00027     face-areas.
00028 
00029 \*---------------------------------------------------------------------------*/
00030 
00031 #include "primitiveMesh.H"
00032 
00033 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00034 
00035 void Foam::primitiveMesh::calcCellCentresAndVols() const
00036 {
00037     if (debug)
00038     {
00039         Pout<< "primitiveMesh::calcCellCentresAndVols() : "
00040             << "Calculating cell centres and cell volumes"
00041             << endl;
00042     }
00043 
00044     // It is an error to attempt to recalculate cellCentres
00045     // if the pointer is already set
00046     if (cellCentresPtr_ || cellVolumesPtr_)
00047     {
00048         FatalErrorIn("primitiveMesh::calcCellCentresAndVols() const")
00049             << "Cell centres or cell volumes already calculated"
00050             << abort(FatalError);
00051     }
00052 
00053     // set the accumulated cell centre to zero vector
00054     cellCentresPtr_ = new vectorField(nCells());
00055     vectorField& cellCtrs = *cellCentresPtr_;
00056 
00057     // Initialise cell volumes to 0
00058     cellVolumesPtr_ = new scalarField(nCells());
00059     scalarField& cellVols = *cellVolumesPtr_;
00060 
00061     // Make centres and volumes
00062     makeCellCentresAndVols(faceCentres(), faceAreas(), cellCtrs, cellVols);
00063 
00064     if (debug)
00065     {
00066         Pout<< "primitiveMesh::calcCellCentresAndVols() : "
00067             << "Finished calculating cell centres and cell volumes"
00068             << endl;
00069     }
00070 }
00071 
00072 
00073 void Foam::primitiveMesh::makeCellCentresAndVols
00074 (
00075     const vectorField& fCtrs,
00076     const vectorField& fAreas,
00077     vectorField& cellCtrs,
00078     scalarField& cellVols
00079 ) const
00080 {
00081     // Clear the fields for accumulation
00082     cellCtrs = vector::zero;
00083     cellVols = 0.0;
00084 
00085     const labelList& own = faceOwner();
00086     const labelList& nei = faceNeighbour();
00087 
00088     // first estimate the approximate cell centre as the average of
00089     // face centres
00090 
00091     vectorField cEst(nCells(), vector::zero);
00092     labelField nCellFaces(nCells(), 0);
00093 
00094     forAll (own, facei)
00095     {
00096         cEst[own[facei]] += fCtrs[facei];
00097         nCellFaces[own[facei]] += 1;
00098     }
00099 
00100     forAll (nei, facei)
00101     {
00102         cEst[nei[facei]] += fCtrs[facei];
00103         nCellFaces[nei[facei]] += 1;
00104     }
00105 
00106     forAll(cEst, celli)
00107     {
00108         cEst[celli] /= nCellFaces[celli];
00109     }
00110 
00111     forAll (own, facei)
00112     {
00113         // Calculate 3*face-pyramid volume
00114         scalar pyr3Vol =
00115             max(fAreas[facei] & (fCtrs[facei] - cEst[own[facei]]), VSMALL);
00116 
00117         // Calculate face-pyramid centre
00118         vector pc = (3.0/4.0)*fCtrs[facei] + (1.0/4.0)*cEst[own[facei]];
00119 
00120         // Accumulate volume-weighted face-pyramid centre
00121         cellCtrs[own[facei]] += pyr3Vol*pc;
00122 
00123         // Accumulate face-pyramid volume
00124         cellVols[own[facei]] += pyr3Vol;
00125     }
00126 
00127     forAll (nei, facei)
00128     {
00129         // Calculate 3*face-pyramid volume
00130         scalar pyr3Vol =
00131             max(fAreas[facei] & (cEst[nei[facei]] - fCtrs[facei]), VSMALL);
00132 
00133         // Calculate face-pyramid centre
00134         vector pc = (3.0/4.0)*fCtrs[facei] + (1.0/4.0)*cEst[nei[facei]];
00135 
00136         // Accumulate volume-weighted face-pyramid centre
00137         cellCtrs[nei[facei]] += pyr3Vol*pc;
00138 
00139         // Accumulate face-pyramid volume
00140         cellVols[nei[facei]] += pyr3Vol;
00141     }
00142 
00143     cellCtrs /= cellVols;
00144     cellVols *= (1.0/3.0);
00145 }
00146 
00147 
00148 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00149 
00150 const Foam::vectorField& Foam::primitiveMesh::cellCentres() const
00151 {
00152     if (!cellCentresPtr_)
00153     {
00154         calcCellCentresAndVols();
00155     }
00156 
00157     return *cellCentresPtr_;
00158 }
00159 
00160 
00161 const Foam::scalarField& Foam::primitiveMesh::cellVolumes() const
00162 {
00163     if (!cellVolumesPtr_)
00164     {
00165         calcCellCentresAndVols();
00166     }
00167 
00168     return *cellVolumesPtr_;
00169 }
00170 
00171 
00172 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd