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

gmshToFoam.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     Reads .msh file as written by Gmsh.
00027 
00028     Needs surface elements on mesh to be present and aligned with outside faces
00029     of the mesh. I.e. if the mesh is hexes, the outside faces need to be quads
00030 
00031     Note: There is something seriously wrong with the ordering written in the
00032     .msh file. Normal operation is to check the ordering and invert prisms
00033     and hexes if found to be wrong way round.
00034     Use the -keepOrientation to keep the raw information.
00035 
00036     Note: The code now uses the element (cell,face) physical region id number
00037     to create cell zones and faces zones (similar to
00038     fluentMeshWithInternalFaces).
00039 
00040     A use of the cell zone information, is for field initialization with the
00041     "setFields" utility. see the classes:  topoSetSource, zoneToCell.
00042 \*---------------------------------------------------------------------------*/
00043 
00044 #include "argList.H"
00045 #include "polyMesh.H"
00046 #include "Time.H"
00047 #include "polyMesh.H"
00048 #include "IFstream.H"
00049 #include "cellModeller.H"
00050 #include "repatchPolyTopoChanger.H"
00051 #include "cellSet.H"
00052 #include "faceSet.H"
00053 
00054 using namespace Foam;
00055 
00056 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00057 
00058 // Element type numbers
00059 static label MSHTRI   = 2;
00060 static label MSHQUAD  = 3;
00061 static label MSHTET   = 4;
00062 static label MSHPYR   = 7;
00063 static label MSHPRISM = 6;
00064 static label MSHHEX   = 5;
00065 
00066 
00067 // Skips till end of section. Returns false if end of file.
00068 bool skipSection(IFstream& inFile)
00069 {
00070     string line;
00071     do
00072     {
00073         inFile.getLine(line);
00074 
00075         if (!inFile.good())
00076         {
00077             return false;
00078         }
00079     }
00080     while (line.size() < 4 || line.substr(0, 4) != "$End");
00081 
00082     return true;
00083 }
00084 
00085 
00086 void renumber
00087 (
00088     const Map<label>& mshToFoam,
00089     labelList& labels
00090 )
00091 {
00092     forAll(labels, labelI)
00093     {
00094         labels[labelI] = mshToFoam[labels[labelI]];
00095     }
00096 }
00097 
00098 
00099 // Find face in pp which uses all vertices in meshF (in mesh point labels)
00100 label findFace(const primitivePatch& pp, const labelList& meshF)
00101 {
00102     const Map<label>& meshPointMap = pp.meshPointMap();
00103 
00104     // meshF[0] in pp labels.
00105     if (!meshPointMap.found(meshF[0]))
00106     {
00107         Warning<< "Not using gmsh face " << meshF
00108             << " since zero vertex is not on boundary of polyMesh" << endl;
00109         return -1;
00110     }
00111 
00112     // Find faces using first point
00113     const labelList& pFaces = pp.pointFaces()[meshPointMap[meshF[0]]];
00114 
00115     // Go through all these faces and check if there is one which uses all of
00116     // meshF vertices (in any order ;-)
00117     forAll(pFaces, i)
00118     {
00119         label faceI = pFaces[i];
00120 
00121         const face& f = pp[faceI];
00122 
00123         // Count uses of vertices of meshF for f
00124         label nMatched = 0;
00125 
00126         forAll(f, fp)
00127         {
00128             if (findIndex(meshF, f[fp]) != -1)
00129             {
00130                 nMatched++;
00131             }
00132         }
00133 
00134         if (nMatched == meshF.size())
00135         {
00136             return faceI;
00137         }
00138     }
00139 
00140     return -1;
00141 }
00142 
00143 
00144 // Same but find internal face. Expensive addressing.
00145 label findInternalFace(const primitiveMesh& mesh, const labelList& meshF)
00146 {
00147     const labelList& pFaces = mesh.pointFaces()[meshF[0]];
00148 
00149     forAll(pFaces, i)
00150     {
00151         label faceI = pFaces[i];
00152 
00153         const face& f = mesh.faces()[faceI];
00154 
00155         // Count uses of vertices of meshF for f
00156         label nMatched = 0;
00157 
00158         forAll(f, fp)
00159         {
00160             if (findIndex(meshF, f[fp]) != -1)
00161             {
00162                 nMatched++;
00163             }
00164         }
00165 
00166         if (nMatched == meshF.size())
00167         {
00168             return faceI;
00169         }
00170     }
00171     return -1;
00172 }
00173 
00174 
00175 // Determine whether cell is inside-out by checking for any wrong-oriented
00176 // face.
00177 bool correctOrientation(const pointField& points, const cellShape& shape)
00178 {
00179     // Get centre of shape.
00180     point cc(shape.centre(points));
00181 
00182     // Get outwards pointing faces.
00183     faceList faces(shape.faces());
00184 
00185     forAll(faces, i)
00186     {
00187         const face& f = faces[i];
00188 
00189         vector n(f.normal(points));
00190 
00191         // Check if vector from any point on face to cc points outwards
00192         if (((points[f[0]] - cc) & n) < 0)
00193         {
00194             // Incorrectly oriented
00195             return false;
00196         }
00197     }
00198 
00199     return true;
00200 }
00201 
00202 
00203 void storeCellInZone
00204 (
00205     const label regPhys,
00206     const label cellI,
00207     Map<label>& physToZone,
00208 
00209     labelList& zoneToPhys,
00210     List<DynamicList<label> >& zoneCells
00211 )
00212 {
00213     Map<label>::const_iterator zoneFnd = physToZone.find(regPhys);
00214 
00215     if (zoneFnd == physToZone.end())
00216     {
00217         // New region. Allocate zone for it.
00218         label zoneI = zoneCells.size();
00219         zoneCells.setSize(zoneI+1);
00220         zoneToPhys.setSize(zoneI+1);
00221 
00222         Info<< "Mapping region " << regPhys << " to Foam cellZone "
00223             << zoneI << endl;
00224         physToZone.insert(regPhys, zoneI);
00225 
00226         zoneToPhys[zoneI] = regPhys;
00227         zoneCells[zoneI].append(cellI);
00228     }
00229     else
00230     {
00231         // Existing zone for region
00232         zoneCells[zoneFnd()].append(cellI);
00233     }
00234 }
00235 
00236 
00237 // Reads points and map
00238 void readPoints(IFstream& inFile, pointField& points, Map<label>& mshToFoam)
00239 {
00240     Info<< "Starting to read points at line " << inFile.lineNumber() << endl;
00241 
00242     string line;
00243     inFile.getLine(line);
00244     IStringStream lineStr(line);
00245 
00246     label nVerts;
00247     lineStr >> nVerts;
00248 
00249     Info<< "Vertices to be read:" << nVerts << endl;
00250 
00251     points.setSize(nVerts);
00252     mshToFoam.resize(2*nVerts);
00253 
00254     for (label pointI = 0; pointI < nVerts; pointI++)
00255     {
00256         label mshLabel;
00257         scalar xVal, yVal, zVal;
00258 
00259         string line;
00260         inFile.getLine(line);
00261         IStringStream lineStr(line);
00262 
00263         lineStr >> mshLabel >> xVal >> yVal >> zVal;
00264 
00265         point& pt = points[pointI];
00266 
00267         pt.x() = xVal;
00268         pt.y() = yVal;
00269         pt.z() = zVal;
00270 
00271         mshToFoam.insert(mshLabel, pointI);
00272     }
00273 
00274     Info<< "Vertices read:" << mshToFoam.size() << endl;
00275 
00276     inFile.getLine(line);
00277     IStringStream tagStr(line);
00278     word tag(tagStr);
00279 
00280     if (tag != "$ENDNOD" && tag != "$EndNodes")
00281     {
00282         FatalErrorIn("readPoints(..)")
00283             << "Did not find $ENDNOD tag on line "
00284             << inFile.lineNumber() << exit(FatalError);
00285     }
00286     Info<< endl;
00287 }
00288 
00289 
00290 // Reads physical names
00291 void readPhysNames(IFstream& inFile, Map<word>& physicalNames)
00292 {
00293     Info<< "Starting to read physical names at line " << inFile.lineNumber()
00294         << endl;
00295 
00296     string line;
00297     inFile.getLine(line);
00298     IStringStream lineStr(line);
00299 
00300     label nNames;
00301     lineStr >> nNames;
00302 
00303     Info<< "Physical names:" << nNames << endl;
00304 
00305     physicalNames.resize(nNames);
00306 
00307     for (label i = 0; i < nNames; i++)
00308     {
00309         label regionI;
00310         string regionName;
00311 
00312         string line;
00313         inFile.getLine(line);
00314         IStringStream lineStr(line);
00315 
00316         lineStr >> regionI >> regionName;
00317 
00318         Info<< "    " << regionI << '\t' << string::validate<word>(regionName)
00319             << endl;
00320 
00321         physicalNames.insert(regionI, string::validate<word>(regionName));
00322     }
00323 
00324     inFile.getLine(line);
00325     IStringStream tagStr(line);
00326     word tag(tagStr);
00327 
00328     if (tag != "$EndPhysicalNames")
00329     {
00330         FatalErrorIn("readPhysicalNames(..)")
00331             << "Did not find $EndPhysicalNames tag on line "
00332             << inFile.lineNumber() << exit(FatalError);
00333     }
00334     Info<< endl;
00335 }
00336 
00337 
00338 // Reads cells and patch faces
00339 void readCells
00340 (
00341     const bool version2Format,
00342     const bool keepOrientation,
00343     const pointField& points,
00344     const Map<label>& mshToFoam,
00345     IFstream& inFile,
00346     cellShapeList& cells,
00347 
00348     labelList& patchToPhys,
00349     List<DynamicList<face> >& patchFaces,
00350 
00351     labelList& zoneToPhys,
00352     List<DynamicList<label> >& zoneCells
00353 )
00354 {
00355     Info<< "Starting to read cells at line " << inFile.lineNumber() << endl;
00356 
00357     const cellModel& hex = *(cellModeller::lookup("hex"));
00358     const cellModel& prism = *(cellModeller::lookup("prism"));
00359     const cellModel& pyr = *(cellModeller::lookup("pyr"));
00360     const cellModel& tet = *(cellModeller::lookup("tet"));
00361 
00362     face triPoints(3);
00363     face quadPoints(4);
00364     labelList tetPoints(4);
00365     labelList pyrPoints(5);
00366     labelList prismPoints(6);
00367     labelList hexPoints(8);
00368 
00369 
00370     string line;
00371     inFile.getLine(line);
00372     IStringStream lineStr(line);
00373 
00374     label nElems;
00375     lineStr >> nElems;
00376 
00377     Info<< "Cells to be read:" << nElems << endl << endl;
00378 
00379 
00380     // Storage for all cells. Too big. Shrink later
00381     cells.setSize(nElems);
00382 
00383     label cellI = 0;
00384     label nTet = 0;
00385     label nPyr = 0;
00386     label nPrism = 0;
00387     label nHex = 0;
00388 
00389 
00390     // From gmsh physical region to Foam patch
00391     Map<label> physToPatch;
00392 
00393     // From gmsh physical region to Foam cellZone
00394     Map<label> physToZone;
00395 
00396 
00397     for (label elemI = 0; elemI < nElems; elemI++)
00398     {
00399         string line;
00400         inFile.getLine(line);
00401         IStringStream lineStr(line);
00402 
00403         label elmNumber, elmType, regPhys;
00404 
00405         if (version2Format)
00406         {
00407             lineStr >> elmNumber >> elmType;
00408 
00409             label nTags;
00410             lineStr>> nTags;
00411 
00412             label regElem, partition;
00413 
00414             if (nTags == 3)
00415             {
00416                 lineStr >> regPhys >> regElem >> partition;
00417             }
00418             else
00419             {
00420                 regPhys = 0;
00421                 for (label i = 0; i < nTags; i++)
00422                 {
00423                     label dummy;
00424                     lineStr>> dummy;
00425                 }
00426             }
00427         }
00428         else
00429         {
00430             label regElem, nNodes;
00431             lineStr >> elmNumber >> elmType >> regPhys >> regElem >> nNodes;
00432         }
00433 
00434         // regPhys on surface elements is region number.
00435 
00436         if (elmType == MSHTRI)
00437         {
00438             lineStr >> triPoints[0] >> triPoints[1] >> triPoints[2];
00439 
00440             renumber(mshToFoam, triPoints);
00441 
00442             Map<label>::iterator regFnd = physToPatch.find(regPhys);
00443 
00444             label patchI = -1;
00445             if (regFnd == physToPatch.end())
00446             {
00447                 // New region. Allocate patch for it.
00448                 patchI = patchFaces.size();
00449 
00450                 patchFaces.setSize(patchI + 1);
00451                 patchToPhys.setSize(patchI + 1);
00452 
00453                 Info<< "Mapping region " << regPhys << " to Foam patch "
00454                     << patchI << endl;
00455                 physToPatch.insert(regPhys, patchI);
00456                 patchToPhys[patchI] = regPhys;
00457             }
00458             else
00459             {
00460                 // Existing patch for region
00461                 patchI = regFnd();
00462             }
00463 
00464             // Add triangle to correct patchFaces.
00465             patchFaces[patchI].append(triPoints);
00466         }
00467         else if (elmType == MSHQUAD)
00468         {
00469             lineStr
00470                 >> quadPoints[0] >> quadPoints[1] >> quadPoints[2]
00471                 >> quadPoints[3];
00472 
00473             renumber(mshToFoam, quadPoints);
00474 
00475             Map<label>::iterator regFnd = physToPatch.find(regPhys);
00476 
00477             label patchI = -1;
00478             if (regFnd == physToPatch.end())
00479             {
00480                 // New region. Allocate patch for it.
00481                 patchI = patchFaces.size();
00482 
00483                 patchFaces.setSize(patchI + 1);
00484                 patchToPhys.setSize(patchI + 1);
00485 
00486                 Info<< "Mapping region " << regPhys << " to Foam patch "
00487                     << patchI << endl;
00488                 physToPatch.insert(regPhys, patchI);
00489                 patchToPhys[patchI] = regPhys;
00490             }
00491             else
00492             {
00493                 // Existing patch for region
00494                 patchI = regFnd();
00495             }
00496 
00497             // Add quad to correct patchFaces.
00498             patchFaces[patchI].append(quadPoints);
00499         }
00500         else if (elmType == MSHTET)
00501         {
00502             storeCellInZone
00503             (
00504                 regPhys,
00505                 cellI,
00506                 physToZone,
00507                 zoneToPhys,
00508                 zoneCells
00509             );
00510 
00511             lineStr
00512                 >> tetPoints[0] >> tetPoints[1] >> tetPoints[2]
00513                 >> tetPoints[3];
00514 
00515             renumber(mshToFoam, tetPoints);
00516 
00517             cells[cellI++] = cellShape(tet, tetPoints);
00518 
00519             nTet++;
00520         }
00521         else if (elmType == MSHPYR)
00522         {
00523             storeCellInZone
00524             (
00525                 regPhys,
00526                 cellI,
00527                 physToZone,
00528                 zoneToPhys,
00529                 zoneCells
00530             );
00531 
00532             lineStr
00533                 >> pyrPoints[0] >> pyrPoints[1] >> pyrPoints[2]
00534                 >> pyrPoints[3] >> pyrPoints[4];
00535 
00536             renumber(mshToFoam, pyrPoints);
00537 
00538             cells[cellI++] = cellShape(pyr, pyrPoints);
00539 
00540             nPyr++;
00541         }
00542         else if (elmType == MSHPRISM)
00543         {
00544             storeCellInZone
00545             (
00546                 regPhys,
00547                 cellI,
00548                 physToZone,
00549                 zoneToPhys,
00550                 zoneCells
00551             );
00552 
00553             lineStr
00554                 >> prismPoints[0] >> prismPoints[1] >> prismPoints[2]
00555                 >> prismPoints[3] >> prismPoints[4] >> prismPoints[5];
00556 
00557             renumber(mshToFoam, prismPoints);
00558 
00559             cells[cellI] = cellShape(prism, prismPoints);
00560 
00561             const cellShape& cell = cells[cellI];
00562 
00563             if (!keepOrientation && !correctOrientation(points, cell))
00564             {
00565                 Info<< "Inverting prism " << cellI << endl;
00566                 // Reorder prism.
00567                 prismPoints[0] = cell[0];
00568                 prismPoints[1] = cell[2];
00569                 prismPoints[2] = cell[1];
00570                 prismPoints[3] = cell[3];
00571                 prismPoints[4] = cell[4];
00572                 prismPoints[5] = cell[5];
00573 
00574                 cells[cellI] = cellShape(prism, prismPoints);
00575             }
00576 
00577             cellI++;
00578 
00579             nPrism++;
00580         }
00581         else if (elmType == MSHHEX)
00582         {
00583             storeCellInZone
00584             (
00585                 regPhys,
00586                 cellI,
00587                 physToZone,
00588                 zoneToPhys,
00589                 zoneCells
00590             );
00591 
00592             lineStr
00593                 >> hexPoints[0] >> hexPoints[1]
00594                 >> hexPoints[2] >> hexPoints[3]
00595                 >> hexPoints[4] >> hexPoints[5]
00596                 >> hexPoints[6] >> hexPoints[7];
00597 
00598             renumber(mshToFoam, hexPoints);
00599 
00600             cells[cellI] = cellShape(hex, hexPoints);
00601 
00602             const cellShape& cell = cells[cellI];
00603 
00604             if (!keepOrientation && !correctOrientation(points, cell))
00605             {
00606                 Info<< "Inverting hex " << cellI << endl;
00607                 // Reorder hex.
00608                 hexPoints[0] = cell[4];
00609                 hexPoints[1] = cell[5];
00610                 hexPoints[2] = cell[6];
00611                 hexPoints[3] = cell[7];
00612                 hexPoints[4] = cell[0];
00613                 hexPoints[5] = cell[1];
00614                 hexPoints[6] = cell[2];
00615                 hexPoints[7] = cell[3];
00616 
00617                 cells[cellI] = cellShape(hex, hexPoints);
00618             }
00619 
00620             cellI++;
00621 
00622             nHex++;
00623         }
00624         else
00625         {
00626             Info<< "Unhandled element " << elmType << " at line "
00627                 << inFile.lineNumber() << endl;
00628         }
00629     }
00630 
00631 
00632     inFile.getLine(line);
00633     IStringStream tagStr(line);
00634     word tag(tagStr);
00635 
00636     if (tag != "$ENDELM" && tag != "$EndElements")
00637     {
00638         FatalErrorIn("readCells(..)")
00639             << "Did not find $ENDELM tag on line "
00640             << inFile.lineNumber() << exit(FatalError);
00641     }
00642 
00643 
00644     cells.setSize(cellI);
00645 
00646     forAll(patchFaces, patchI)
00647     {
00648         patchFaces[patchI].shrink();
00649     }
00650 
00651 
00652     Info<< "Cells:" << endl
00653     << "    total:" << cells.size() << endl
00654     << "    hex  :" << nHex << endl
00655     << "    prism:" << nPrism << endl
00656     << "    pyr  :" << nPyr << endl
00657     << "    tet  :" << nTet << endl
00658     << endl;
00659 
00660     if (cells.size() == 0)
00661     {
00662         FatalErrorIn("readCells(..)")
00663             << "No cells read from file " << inFile.name() << nl
00664             << "Does your file specify any 3D elements (hex=" << MSHHEX
00665             << ", prism=" << MSHPRISM << ", pyramid=" << MSHPYR
00666             << ", tet=" << MSHTET << ")?" << nl
00667             << "Perhaps you have not exported the 3D elements?"
00668             << exit(FatalError);
00669     }
00670 
00671     Info<< "CellZones:" << nl
00672         << "Zone\tSize" << endl;
00673 
00674     forAll(zoneCells, zoneI)
00675     {
00676         zoneCells[zoneI].shrink();
00677 
00678         const labelList& zCells = zoneCells[zoneI];
00679 
00680         if (zCells.size())
00681         {
00682             Info<< "    " << zoneI << '\t' << zCells.size() << endl;
00683         }
00684     }
00685     Info<< endl;
00686 }
00687 
00688 
00689 // Main program:
00690 
00691 int main(int argc, char *argv[])
00692 {
00693     argList::noParallel();
00694     argList::validArgs.append(".msh file");
00695     argList::validOptions.insert("keepOrientation", "");
00696 
00697 #   include "setRootCase.H"
00698 #   include "createTime.H"
00699 
00700     fileName mshName(args.additionalArgs()[0]);
00701 
00702     bool keepOrientation = args.optionFound("keepOrientation");
00703 
00704     // Storage for points
00705     pointField points;
00706     Map<label> mshToFoam;
00707 
00708     // Storage for all cells.
00709     cellShapeList cells;
00710 
00711     // Map from patch to gmsh physical region
00712     labelList patchToPhys;
00713     // Storage for patch faces.
00714     List<DynamicList<face> > patchFaces(0);
00715 
00716     // Map from cellZone to gmsh physical region
00717     labelList zoneToPhys;
00718     // Storage for cell zones.
00719     List<DynamicList<label> > zoneCells(0);
00720 
00721     // Name per physical region
00722     Map<word> physicalNames;
00723 
00724     // Version 1 or 2 format
00725     bool version2Format = false;
00726 
00727 
00728     IFstream inFile(mshName);
00729 
00730     while (inFile.good())
00731     {
00732         string line;
00733         inFile.getLine(line);
00734         IStringStream lineStr(line);
00735 
00736         word tag(lineStr);
00737 
00738         if (tag == "$MeshFormat")
00739         {
00740             Info<< "Found $MeshFormat tag; assuming version 2 file format."
00741                 << endl;
00742             version2Format = true;
00743 
00744             if (!skipSection(inFile))
00745             {
00746                 break;
00747             }
00748         }
00749         else if (tag == "$PhysicalNames")
00750         {
00751             readPhysNames(inFile, physicalNames);
00752         }
00753         else if (tag == "$NOD" || tag == "$Nodes")
00754         {
00755             readPoints(inFile, points, mshToFoam);
00756         }
00757         else if (tag == "$ELM" || tag == "$Elements")
00758         {
00759             readCells
00760             (
00761                 version2Format,
00762                 keepOrientation,
00763                 points,
00764                 mshToFoam,
00765                 inFile,
00766                 cells,
00767                 patchToPhys,
00768                 patchFaces,
00769                 zoneToPhys,
00770                 zoneCells
00771             );
00772         }
00773         else
00774         {
00775             Info<< "Skipping tag " << tag << " at line "
00776                 << inFile.lineNumber()
00777                 << endl;
00778 
00779             if (!skipSection(inFile))
00780             {
00781                 break;
00782             }
00783         }
00784     }
00785 
00786 
00787     label nValidCellZones = 0;
00788 
00789     forAll(zoneCells, zoneI)
00790     {
00791         if (zoneCells[zoneI].size())
00792         {
00793             nValidCellZones++;
00794         }
00795     }
00796 
00797 
00798     // Problem is that the orientation of the patchFaces does not have to
00799     // be consistent with the outwards orientation of the mesh faces. So
00800     // we have to construct the mesh in two stages:
00801     // 1. define mesh with all boundary faces in one patch
00802     // 2. use the read patchFaces to find the corresponding boundary face
00803     //    and repatch it.
00804 
00805 
00806     // Create correct number of patches
00807     // (but without any faces in it)
00808     faceListList boundaryFaces(patchFaces.size());
00809 
00810     wordList boundaryPatchNames(boundaryFaces.size());
00811 
00812     forAll(boundaryPatchNames, patchI)
00813     {
00814         label physReg = patchToPhys[patchI];
00815 
00816         Map<word>::const_iterator iter = physicalNames.find(physReg);
00817 
00818         if (iter != physicalNames.end())
00819         {
00820             boundaryPatchNames[patchI] = iter();
00821         }
00822         else
00823         {
00824             boundaryPatchNames[patchI] = word("patch") + name(patchI);
00825         }
00826         Info<< "Patch " << patchI << " gets name "
00827             << boundaryPatchNames[patchI] << endl;
00828     }
00829     Info<< endl;
00830 
00831     wordList boundaryPatchTypes(boundaryFaces.size(), polyPatch::typeName);
00832     word defaultFacesName = "defaultFaces";
00833     word defaultFacesType = polyPatch::typeName;
00834     wordList boundaryPatchPhysicalTypes
00835     (
00836         boundaryFaces.size(),
00837         polyPatch::typeName
00838     );
00839 
00840     polyMesh mesh
00841     (
00842         IOobject
00843         (
00844             polyMesh::defaultRegion,
00845             runTime.constant(),
00846             runTime
00847         ),
00848         xferMove(points),
00849         cells,
00850         boundaryFaces,
00851         boundaryPatchNames,
00852         boundaryPatchTypes,
00853         defaultFacesName,
00854         defaultFacesType,
00855         boundaryPatchPhysicalTypes
00856     );
00857 
00858     repatchPolyTopoChanger repatcher(mesh);
00859 
00860     // Now use the patchFaces to patch up the outside faces of the mesh.
00861 
00862     // Get the patch for all the outside faces (= default patch added as last)
00863     const polyPatch& pp = mesh.boundaryMesh()[mesh.boundaryMesh().size()-1];
00864 
00865     // Storage for faceZones.
00866     List<DynamicList<label> > zoneFaces(patchFaces.size());
00867 
00868 
00869     // Go through all the patchFaces and find corresponding face in pp.
00870     forAll(patchFaces, patchI)
00871     {
00872         const DynamicList<face>& pFaces = patchFaces[patchI];
00873 
00874         Info<< "Finding faces of patch " << patchI << endl;
00875 
00876         forAll(pFaces, i)
00877         {
00878             const face& f = pFaces[i];
00879 
00880             // Find face in pp using all vertices of f.
00881             label patchFaceI = findFace(pp, f);
00882 
00883             if (patchFaceI != -1)
00884             {
00885                 label meshFaceI = pp.start() + patchFaceI;
00886 
00887                 repatcher.changePatchID(meshFaceI, patchI);
00888             }
00889             else
00890             {
00891                 // Maybe internal face? If so add to faceZone with same index
00892                 // - might be useful.
00893                 label meshFaceI = findInternalFace(mesh, f);
00894 
00895                 if (meshFaceI != -1)
00896                 {
00897                     zoneFaces[patchI].append(meshFaceI);
00898                 }
00899                 else
00900                 {
00901                     WarningIn(args.executable())
00902                         << "Could not match gmsh face " << f
00903                         << " to any of the interior or exterior faces"
00904                         << " that share the same 0th point" << endl;
00905                 }
00906             }
00907         }
00908     }
00909     Info<< nl;
00910 
00911     // Face zones
00912     label nValidFaceZones = 0;
00913 
00914     Info<< "FaceZones:" << nl
00915         << "Zone\tSize" << endl;
00916 
00917     forAll(zoneFaces, zoneI)
00918     {
00919         zoneFaces[zoneI].shrink();
00920 
00921         const labelList& zFaces = zoneFaces[zoneI];
00922 
00923         if (zFaces.size())
00924         {
00925             nValidFaceZones++;
00926 
00927             Info<< "    " << zoneI << '\t' << zFaces.size() << endl;
00928         }
00929     }
00930     Info<< endl;
00931 
00932 
00933     //Get polyMesh to write to constant
00934     runTime.setTime(instant(runTime.constant()), 0);
00935 
00936     repatcher.repatch();
00937 
00938     List<cellZone*> cz;
00939     List<faceZone*> fz;
00940 
00941     // Construct and add the zones. Note that cell ordering does not change
00942     // because of repatch() and neither does internal faces so we can
00943     // use the zoneCells/zoneFaces as is.
00944 
00945     if (nValidCellZones > 0)
00946     {
00947         cz.setSize(nValidCellZones);
00948 
00949         nValidCellZones = 0;
00950 
00951         forAll(zoneCells, zoneI)
00952         {
00953             if (zoneCells[zoneI].size())
00954             {
00955                 label physReg = zoneToPhys[zoneI];
00956 
00957                 Map<word>::const_iterator iter = physicalNames.find(physReg);
00958 
00959                 word zoneName = "cellZone_" + name(zoneI);
00960                 if (iter != physicalNames.end())
00961                 {
00962                     zoneName = iter();
00963                 }
00964 
00965                 Info<< "Writing zone " << zoneI << " to cellZone "
00966                     << zoneName << " and cellSet"
00967                     << endl;
00968 
00969                 cellSet cset(mesh, zoneName, zoneCells[zoneI]);
00970                 cset.write();
00971 
00972                 cz[nValidCellZones] = new cellZone
00973                 (
00974                     zoneName,
00975                     zoneCells[zoneI],
00976                     nValidCellZones,
00977                     mesh.cellZones()
00978                 );
00979                 nValidCellZones++;
00980             }
00981         }
00982     }
00983 
00984     if (nValidFaceZones > 0)
00985     {
00986         fz.setSize(nValidFaceZones);
00987 
00988         nValidFaceZones = 0;
00989 
00990         forAll(zoneFaces, zoneI)
00991         {
00992             if (zoneFaces[zoneI].size())
00993             {
00994                 label physReg = zoneToPhys[zoneI];
00995 
00996                 Map<word>::const_iterator iter = physicalNames.find(physReg);
00997 
00998                 word zoneName = "faceZone_" + name(zoneI);
00999                 if (iter != physicalNames.end())
01000                 {
01001                     zoneName = iter();
01002                 }
01003 
01004                 Info<< "Writing zone " << zoneI << " to faceZone "
01005                     << zoneName << " and faceSet"
01006                     << endl;
01007 
01008                 faceSet fset(mesh, zoneName, zoneFaces[zoneI]);
01009                 fset.write();
01010 
01011                 fz[nValidFaceZones] = new faceZone
01012                 (
01013                     zoneName,
01014                     zoneFaces[zoneI],
01015                     boolList(zoneFaces[zoneI].size(), true),
01016                     nValidFaceZones,
01017                     mesh.faceZones()
01018                 );
01019                 nValidFaceZones++;
01020             }
01021         }
01022     }
01023 
01024     if (cz.size() || fz.size())
01025     {
01026         mesh.addZones(List<pointZone*>(0), fz, cz);
01027     }
01028 
01029     mesh.write();
01030 
01031     Info<< "End\n" << endl;
01032 
01033     return 0;
01034 }
01035 
01036 
01037 // ************************************************************************* //
01038 
Copyright © 2000-2009 OpenCFD Ltd