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

mergePolyMesh.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 "mergePolyMesh.H"
00028 #include "Time.H"
00029 #include "polyTopoChanger.H"
00030 #include "mapPolyMesh.H"
00031 #include "polyAddPoint.H"
00032 #include "polyAddCell.H"
00033 #include "polyAddFace.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 defineTypeNameAndDebug(Foam::mergePolyMesh, 1);
00038 
00039 
00040 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00041 
00042 Foam::label Foam::mergePolyMesh::patchIndex(const polyPatch& p)
00043 {
00044     // Find the patch name on the list.  If the patch is already there
00045     // and patch types match, return index
00046     const word& pType = p.type();
00047     const word& pName = p.name();
00048 
00049     bool nameFound = false;
00050 
00051     forAll (patchNames_, patchI)
00052     {
00053         if (patchNames_[patchI] == pName)
00054         {
00055             if (patchTypes_[patchI] == pType)
00056             {
00057                 // Found name and types match
00058                 return patchI;
00059             }
00060             else
00061             {
00062                 // Found the name, but type is different
00063                 nameFound = true;
00064             }
00065         }
00066     }
00067 
00068     // Patch not found.  Append to the list
00069     patchTypes_.append(pType);
00070 
00071     if (nameFound)
00072     {
00073         // Duplicate name is not allowed.  Create a composite name from the
00074         // patch name and case name
00075         const word& caseName = p.boundaryMesh().mesh().time().caseName();
00076 
00077         patchNames_.append(pName + "_" + caseName);
00078 
00079         Info<< "label patchIndex(const polyPatch& p) : "
00080             << "Patch " << p.index() << " named "
00081             << pName << " in mesh " << caseName
00082             << " already exists, but patch types "
00083             << " do not match.\nCreating a composite name as "
00084             << patchNames_[patchNames_.size() - 1] << endl;
00085     }
00086     else
00087     {
00088         patchNames_.append(pName);
00089     }
00090 
00091     return patchNames_.size() - 1;
00092 }
00093 
00094 
00095 Foam::label Foam::mergePolyMesh::zoneIndex
00096 (
00097     DynamicList<word>& names,
00098     const word& curName
00099 )
00100 {
00101     forAll (names, zoneI)
00102     {
00103         if (names[zoneI] == curName)
00104         {
00105             return zoneI;
00106         }
00107     }
00108 
00109     // Not found.  Add new name to the list
00110     names.append(curName);
00111 
00112     return names.size() - 1;
00113 }
00114 
00115 
00116 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00117 
00118 Foam::mergePolyMesh::mergePolyMesh(const IOobject& io)
00119 :
00120     polyMesh(io),
00121     meshMod_(*this),
00122     patchTypes_(2*boundaryMesh().size()),
00123     patchNames_(2*boundaryMesh().size()),
00124     pointZoneNames_(),
00125     faceZoneNames_(),
00126     cellZoneNames_()
00127 {
00128     // Insert the original patches into the list
00129     wordList curPatchTypes = boundaryMesh().types();
00130     wordList curPatchNames = boundaryMesh().names();
00131 
00132     forAll (curPatchTypes, patchI)
00133     {
00134         patchTypes_.append(curPatchTypes[patchI]);
00135         patchNames_.append(curPatchNames[patchI]);
00136     }
00137 
00138     // Insert point, face and cell zones into the list
00139 
00140     // Point zones
00141     wordList curPointZoneNames = pointZones().names();
00142     if (curPointZoneNames.size())
00143     {
00144         pointZoneNames_.setCapacity(2*curPointZoneNames.size());
00145     }
00146 
00147     forAll (curPointZoneNames, zoneI)
00148     {
00149         pointZoneNames_.append(curPointZoneNames[zoneI]);
00150     }
00151 
00152     // Face zones
00153     wordList curFaceZoneNames = faceZones().names();
00154 
00155     if (curFaceZoneNames.size())
00156     {
00157         faceZoneNames_.setCapacity(2*curFaceZoneNames.size());
00158     }
00159     forAll (curFaceZoneNames, zoneI)
00160     {
00161         faceZoneNames_.append(curFaceZoneNames[zoneI]);
00162     }
00163 
00164     // Cell zones
00165     wordList curCellZoneNames = cellZones().names();
00166 
00167     if (curCellZoneNames.size())
00168     {
00169         cellZoneNames_.setCapacity(2*curCellZoneNames.size());
00170     }
00171     forAll (curCellZoneNames, zoneI)
00172     {
00173         cellZoneNames_.append(curCellZoneNames[zoneI]);
00174     }
00175 }
00176 
00177 
00178 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00179 
00180 
00181 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00182 
00183 void Foam::mergePolyMesh::addMesh(const polyMesh& m)
00184 {
00185     // Add all the points, faces and cells of the new mesh
00186 
00187     // Add points
00188 
00189     label zoneID = -1;
00190 
00191     const pointField& p = m.points();
00192     labelList renumberPoints(p.size());
00193 
00194     const pointZoneMesh& pz = m.pointZones();
00195     labelList pointZoneIndices(pz.size());
00196 
00197     forAll (pz, zoneI)
00198     {
00199         pointZoneIndices[zoneI] = zoneIndex(pointZoneNames_, pz[zoneI].name());
00200     }
00201 
00202     forAll (p, pointI)
00203     {
00204         // Grab zone ID.  If a point is not in a zone, it will return -1
00205         zoneID = pz.whichZone(pointI);
00206 
00207         if (zoneID > 0)
00208         {
00209             // Translate zone ID into the new index
00210             zoneID = pointZoneIndices[zoneID];
00211         }
00212 
00213         renumberPoints[pointI] =
00214             meshMod_.setAction
00215             (
00216                 polyAddPoint
00217                 (
00218                     p[pointI],            // Point to add
00219                     -1,                   // Master point (straight addition)
00220                     zoneID,               // Zone for point
00221                     pointI < m.nPoints()  // Is in cell?
00222                 )
00223             );
00224     }
00225 
00226     // Add cells
00227 
00228     const cellList& c = m.cells();
00229     labelList renumberCells(c.size());
00230 
00231     const cellZoneMesh& cz = m.cellZones();
00232     labelList cellZoneIndices(cz.size());
00233 
00234     forAll (cz, zoneI)
00235     {
00236         cellZoneIndices[zoneI] = zoneIndex(cellZoneNames_, cz[zoneI].name());
00237     }
00238 
00239     forAll (c, cellI)
00240     {
00241         // Grab zone ID.  If a cell is not in a zone, it will return -1
00242         zoneID = cz.whichZone(cellI);
00243 
00244         if (zoneID > 0)
00245         {
00246             // Translate zone ID into the new index
00247             zoneID = cellZoneIndices[zoneID];
00248         }
00249 
00250         renumberCells[cellI] =
00251             meshMod_.setAction
00252             (
00253                 polyAddCell
00254                 (
00255                     -1,                   // Master point
00256                     -1,                   // Master edge
00257                     -1,                   // Master face
00258                     -1,                   // Master cell
00259                     zoneID                // Zone for cell
00260                 )
00261             );
00262     }
00263 
00264     // Add faces
00265     const polyBoundaryMesh& bm = m.boundaryMesh();
00266 
00267     // Gather the patch indices
00268     labelList patchIndices(bm.size());
00269 
00270     forAll (patchIndices, patchI)
00271     {
00272         patchIndices[patchI] = patchIndex(bm[patchI]);
00273     }
00274 
00275     // Temporary: update number of allowable patches. This should be
00276     // determined at the top - before adding anything.
00277     meshMod_.setNumPatches(patchNames_.size());
00278 
00279 
00280 
00281     const faceZoneMesh& fz = m.faceZones();
00282     labelList faceZoneIndices(fz.size());
00283 
00284     forAll (fz, zoneI)
00285     {
00286         faceZoneIndices[zoneI] = zoneIndex(faceZoneNames_, fz[zoneI].name());
00287     }
00288 
00289     const faceList& f = m.faces();
00290     labelList renumberFaces(f.size());
00291 
00292     const labelList& own = m.faceOwner();
00293     const labelList& nei = m.faceNeighbour();
00294 
00295     label newOwn, newNei, newPatch, newZone;
00296     bool newZoneFlip;
00297 
00298     forAll (f, faceI)
00299     {
00300         const face& curFace = f[faceI];
00301 
00302         face newFace(curFace.size());
00303 
00304         forAll (curFace, pointI)
00305         {
00306             newFace[pointI] = renumberPoints[curFace[pointI]];
00307         }
00308 
00309         if (debug)
00310         {
00311             // Check that the face is valid
00312             if (min(newFace) < 0)
00313             {
00314                 FatalErrorIn("void mergePolyMesh::addMesh(const polyMesh&)")
00315                     << "Error in point mapping for face " << faceI
00316                     << ".  Old face: " << curFace << " New face: " << newFace
00317                     << abort(FatalError);
00318             }
00319         }
00320 
00321         if (faceI < m.nInternalFaces() || faceI >= m.nFaces())
00322         {
00323             newPatch = -1;
00324         }
00325         else
00326         {
00327             newPatch = patchIndices[bm.whichPatch(faceI)];
00328         }
00329 
00330         newOwn = own[faceI];
00331         if (newOwn > -1) newOwn = renumberCells[newOwn];
00332 
00333         if (newPatch > -1)
00334         {
00335             newNei = -1;
00336         }
00337         else
00338         {
00339             newNei = nei[faceI];
00340             newNei = renumberCells[newNei];
00341         }
00342 
00343 
00344         newZone = fz.whichZone(faceI);
00345         newZoneFlip = false;
00346 
00347         if (newZone > -1)
00348         {
00349             newZoneFlip = fz[newZone].flipMap()[fz[newZone].whichFace(faceI)];
00350 
00351             // Grab the new zone
00352             newZone = faceZoneIndices[newZone];
00353         }
00354 
00355         renumberFaces[faceI] =
00356             meshMod_.setAction
00357             (
00358                 polyAddFace
00359                 (
00360                     newFace,
00361                     newOwn,
00362                     newNei,
00363                     -1,
00364                     -1,
00365                     -1,
00366                     false,
00367                     newPatch,
00368                     newZone,
00369                     newZoneFlip
00370                 )
00371             );
00372     }
00373 
00374 }
00375 
00376 
00377 void Foam::mergePolyMesh::merge()
00378 {
00379     Info<< "patch names: " << patchNames_ << nl
00380         << "patch types: " << patchTypes_ << nl
00381         << "point zone names: " << pointZoneNames_ << nl
00382         << "face zone names: " << faceZoneNames_ << nl
00383         << "cell zone names: " << cellZoneNames_ << endl;
00384 
00385     // Add the patches if necessary
00386     if (patchNames_.size() != boundaryMesh().size())
00387     {
00388         Info << "Copying old patches" << endl;
00389 
00390         List<polyPatch*> newPatches(patchNames_.size());
00391 
00392         const polyBoundaryMesh& oldPatches = boundaryMesh();
00393 
00394         // Note.  Re-using counter in two for loops
00395         label patchI = 0;
00396 
00397         for (patchI = 0; patchI < oldPatches.size(); patchI++)
00398         {
00399             newPatches[patchI] = oldPatches[patchI].clone(oldPatches).ptr();
00400         }
00401 
00402         Info << "Adding new patches. " << endl;
00403 
00404         label endOfLastPatch =
00405             oldPatches[patchI - 1].start() + oldPatches[patchI - 1].size();
00406 
00407         for (; patchI < patchNames_.size(); patchI++)
00408         {
00409             // Add a patch
00410             newPatches[patchI] =
00411             (
00412                 polyPatch::New
00413                 (
00414                     patchTypes_[patchI],
00415                     patchNames_[patchI],
00416                     0,
00417                     endOfLastPatch,
00418                     patchI,
00419                     oldPatches
00420                 ).ptr()
00421             );
00422         }
00423 
00424         removeBoundary();
00425         addPatches(newPatches);
00426     }
00427 
00428     // Add the zones if necessary
00429     if
00430     (
00431         pointZoneNames_.size() != pointZones().size()
00432      || faceZoneNames_.size() != faceZones().size()
00433      || cellZoneNames_.size() != cellZones().size()
00434     )
00435     {
00436 
00437     }
00438 
00439     // Change mesh. No inflation
00440     meshMod_.changeMesh(*this, false);
00441 
00442     // Clear topo change for the next operation
00443     meshMod_.clear();
00444 }
00445 
00446 
00447 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd