FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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