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

cellMatcher.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 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "cellMatcher.H"
00030 
00031 #include "primitiveMesh.H"
00032 #include "Map.H"
00033 #include "faceList.H"
00034 #include "labelList.H"
00035 
00036 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00037 
00038 Foam::labelList Foam::cellMatcher::makeIdentity(const label nElems)
00039 {
00040     labelList result(nElems);
00041 
00042     forAll(result, elemI)
00043     {
00044         result[elemI] = elemI;
00045     }
00046     return result;
00047 }
00048 
00049 
00050 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00051 
00052 // Construct from components
00053 Foam::cellMatcher::cellMatcher
00054 (
00055     const label vertPerCell,
00056     const label facePerCell,
00057     const label maxVertPerFace,
00058     const word& cellModelName
00059 )
00060 :
00061     localPoint_(100),
00062     localFaces_(facePerCell),
00063     faceSize_(facePerCell, -1),
00064     pointMap_(vertPerCell),
00065     faceMap_(facePerCell),
00066     edgeFaces_(2*vertPerCell*vertPerCell),
00067     pointFaceIndex_(vertPerCell),
00068     vertLabels_(vertPerCell),
00069     faceLabels_(facePerCell),
00070     cellModelName_(cellModelName),
00071     cellModelPtr_(NULL)
00072 {
00073     forAll(localFaces_, faceI)
00074     {
00075         face& f = localFaces_[faceI];
00076 
00077         f.setSize(maxVertPerFace);
00078     }
00079 
00080     forAll(pointFaceIndex_, vertI)
00081     {
00082         pointFaceIndex_[vertI].setSize(facePerCell);
00083     }
00084 }
00085 
00086 
00087 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00088 
00089 // Create localFaces_ , pointMap_ , faceMap_
00090 Foam::label Foam::cellMatcher::calcLocalFaces
00091 (
00092     const faceList& faces,
00093     const labelList& myFaces
00094 )
00095 {
00096     // Clear map from global to cell numbering
00097     localPoint_.clear();
00098 
00099     // Renumber face vertices and insert directly into localFaces_
00100     label newVertI = 0;
00101     forAll(myFaces, myFaceI)
00102     {
00103         label faceI = myFaces[myFaceI];
00104 
00105         const face& f = faces[faceI];
00106         face& localFace = localFaces_[myFaceI];
00107 
00108         // Size of localFace
00109         faceSize_[myFaceI] = f.size();
00110 
00111         forAll(f, localVertI)
00112         {
00113             label vertI = f[localVertI];
00114 
00115             Map<label>::iterator iter = localPoint_.find(vertI);
00116             if (iter == localPoint_.end())
00117             {
00118                 // Not found. Assign local vertex number.
00119 
00120                 if (newVertI >= pointMap_.size())
00121                 {
00122                     // Illegal face: more unique vertices than vertPerCell
00123                     return -1;
00124                 }
00125 
00126                 localFace[localVertI] = newVertI;
00127                 localPoint_.insert(vertI, newVertI);
00128                 newVertI++;
00129             }
00130             else
00131             {
00132                 // Reuse local vertex number.
00133                 localFace[localVertI] = *iter;
00134             }
00135         }
00136 
00137         // Create face from localvertex labels
00138         faceMap_[myFaceI] = faceI;
00139     }
00140 
00141     // Create local to global vertex mapping
00142     for
00143     (
00144         Map<label>::iterator iter = localPoint_.begin();
00145         iter != localPoint_.end();
00146         ++iter
00147     )
00148     {
00149         label fp = iter();
00150         pointMap_[fp] = iter.key();
00151     }
00152 
00154     //write(Info);
00155 
00156     return newVertI;
00157 }
00158 
00159 
00160 // Create edgeFaces_ : map from edge to two localFaces for single cell.
00161 void Foam::cellMatcher::calcEdgeAddressing(const label numVert)
00162 {
00163     edgeFaces_ = -1;
00164 
00165     forAll(localFaces_, localFaceI)
00166     {
00167         const face& f = localFaces_[localFaceI];
00168 
00169         label prevVertI = faceSize_[localFaceI] - 1;
00170         //forAll(f, fp)
00171         for
00172         (
00173             label fp = 0;
00174             fp < faceSize_[localFaceI];
00175             fp++
00176         )
00177         {
00178             label start = f[prevVertI];
00179             label end = f[fp];
00180             
00181             label key1 = edgeKey(numVert, start, end);
00182             label key2 = edgeKey(numVert, end, start);
00183 
00184             if (edgeFaces_[key1] == -1)
00185             {
00186                 // Entry key1 unoccupied. Store both permutations.
00187                 edgeFaces_[key1] = localFaceI;
00188                 edgeFaces_[key2] = localFaceI;
00189             }
00190             else if (edgeFaces_[key1+1] == -1)
00191             {
00192                 // Entry key1+1 unoccupied
00193                 edgeFaces_[key1+1] = localFaceI;
00194                 edgeFaces_[key2+1] = localFaceI;
00195             }
00196             else
00197             {
00198                 FatalErrorIn
00199                 (
00200                     "calcEdgeAddressing"
00201                     "(const faceList&, const label)"
00202                 )   << "edgeFaces_ full at entry:" << key1
00203                     << " for edge " << start << " " << end
00204                     << abort(FatalError);
00205             }
00206 
00207             prevVertI = fp;
00208         }
00209     }
00210 }
00211 
00212 
00213 // Create pointFaceIndex_ : map from vertI, faceI to index of vertI on faceI.
00214 void Foam::cellMatcher::calcPointFaceIndex()
00215 {
00216     // Fill pointFaceIndex_ with -1
00217     forAll(pointFaceIndex_, i)
00218     {
00219         labelList& faceIndices = pointFaceIndex_[i];
00220 
00221         faceIndices = -1;
00222     }
00223 
00224     forAll(localFaces_, localFaceI)
00225     {
00226         const face& f = localFaces_[localFaceI];
00227 
00228         for
00229         (
00230             label fp = 0;
00231             fp < faceSize_[localFaceI];
00232             fp++
00233         )
00234         {
00235             label vert = f[fp];
00236             pointFaceIndex_[vert][localFaceI] = fp;
00237         }
00238     }
00239 }
00240 
00241 
00242 // Given edge(v0,v1) and (local)faceI return the other face
00243 Foam::label Foam::cellMatcher::otherFace
00244 (
00245     const label numVert,
00246     const label v0,
00247     const label v1,
00248     const label localFaceI
00249 ) const
00250 {
00251     label key = edgeKey(numVert, v0, v1);
00252 
00253     if (edgeFaces_[key] == localFaceI)
00254     {
00255         return edgeFaces_[key+1];
00256     }
00257     else if (edgeFaces_[key+1] == localFaceI)
00258     {
00259         return edgeFaces_[key];
00260     }
00261     else
00262     {
00263         FatalErrorIn
00264         (
00265             "otherFace"
00266             "(const label, const labelList&, const label, const label, "
00267             "const label)"
00268         )   << "edgeFaces_ does not contain:" << localFaceI
00269             << " for edge " << v0 << " " << v1 << " at key " << key
00270             << " edgeFaces_[key, key+1]:" <<  edgeFaces_[key]
00271             << " , " << edgeFaces_[key+1]
00272             << abort(FatalError);
00273 
00274         return -1;
00275     }
00276 }
00277 
00278 
00279 void Foam::cellMatcher::write(Foam::Ostream& os) const
00280 {
00281     os  << "Faces:" << endl;
00282 
00283     forAll(localFaces_, faceI)
00284     {
00285         os  << "    ";
00286 
00287         for(label fp = 0; fp < faceSize_[faceI]; fp++)
00288         {
00289             os  << ' ' << localFaces_[faceI][fp];
00290         }
00291         os  << endl;
00292     }
00293 
00294     os  <<  "Face map  : " << faceMap_ << endl;
00295     os  <<  "Point map : " << pointMap_ << endl;
00296 }
00297 
00298 
00299 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd