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

tetgenToFoam.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     Converts .ele and .node and .face files, written by tetgen.
00027 
00028     Make sure to use add boundary attributes to the smesh file
00029     (5 fifth column in the element section)
00030     and run tetgen with -f option.
00031 
00032     Sample smesh file:
00033 
00034         # cube.smesh -- A 10x10x10 cube
00035         8 3
00036         1   0 0 0
00037         2   0 10 0
00038         3   10 10 0
00039         4   10 0 0
00040         5   0 0 10
00041         6   0 10 10
00042         7   10 10 10
00043         8   10 0 10
00044         6 1                 # 1 for boundary info present
00045         4   1 2 3 4 11  # region number 11
00046         4   5 6 7 8 21  # region number 21
00047         4   1 2 6 5 3
00048         4   4 3 7 8 43
00049         4   1 5 8 4 5
00050         4   2 6 7 3 65
00051         0
00052         0
00053 
00054 NOTE:
00055 - for some reason boundary faces point inwards. I just reverse them
00056 always. Might use some geometric check instead.
00057 - marked faces might not actually be boundary faces of mesh.
00058 This is hopefully handled now by first constructing without boundaries
00059 and then reconstructing with boundary faces.
00060 
00061 \*---------------------------------------------------------------------------*/
00062 
00063 #include "argList.H"
00064 #include "Time.H"
00065 #include "polyMesh.H"
00066 #include "IFstream.H"
00067 #include "cellModeller.H"
00068 
00069 using namespace Foam;
00070 
00071 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00072 
00073 // Find label of face.
00074 label findFace(const primitiveMesh& mesh, const face& f)
00075 {
00076     const labelList& pFaces = mesh.pointFaces()[f[0]];
00077 
00078     forAll(pFaces, i)
00079     {
00080         label faceI = pFaces[i];
00081 
00082         if (mesh.faces()[faceI] == f)
00083         {
00084             return faceI;
00085         }
00086     }
00087 
00088     FatalErrorIn("findFace(const primitiveMesh&, const face&)")
00089         << "Cannot find face " << f << " in mesh." << abort(FatalError);
00090 
00091     return -1;
00092 }
00093 
00094 
00095 // Main program:
00096 
00097 int main(int argc, char *argv[])
00098 {
00099     argList::validArgs.append("file prefix");
00100     argList::validOptions.insert("noFaceFile", "");
00101 
00102 #   include "setRootCase.H"
00103 #   include "createTime.H"
00104 
00105 
00106     bool readFaceFile = !args.optionFound("noFaceFile");
00107 
00108     fileName prefix(args.additionalArgs()[0]);
00109 
00110     fileName nodeFile(prefix + ".node");
00111     fileName eleFile(prefix + ".ele");
00112     fileName faceFile(prefix + ".face");
00113 
00114     if (!readFaceFile)
00115     {
00116         Info<< "Files:" << endl
00117             << "    nodes : " << nodeFile << endl
00118             << "    elems : " << eleFile << endl
00119             << endl;
00120     }
00121     else
00122     {
00123         Info<< "Files:" << endl
00124             << "    nodes : " << nodeFile << endl
00125             << "    elems : " << eleFile << endl
00126             << "    faces : " << faceFile << endl
00127             << endl;
00128 
00129         Info<< "Reading .face file for boundary information" << nl << endl;
00130     }
00131 
00132     if (!isFile(nodeFile) || !isFile(eleFile))
00133     {
00134         FatalErrorIn(args.executable())
00135             << "Cannot read " << nodeFile << " or " << eleFile
00136             << exit(FatalError);
00137     }
00138 
00139     if (readFaceFile && !isFile(faceFile))
00140     {
00141         FatalErrorIn(args.executable())
00142             << "Cannot read " << faceFile << endl
00143             << "Did you run tetgen with -f option?" << endl
00144             << "If you don't want to read the .face file and thus not have"
00145             << " patches please\nrerun with the -noFaceFile option"
00146             << exit(FatalError);
00147     }
00148 
00149 
00150     IFstream nodeStream(nodeFile);
00151 
00152     //
00153     // Read nodes.
00154     //
00155 
00156     // Read header.
00157     string line;
00158 
00159     do
00160     {
00161         nodeStream.getLine(line);
00162     }
00163     while (line.size() && line[0] == '#');
00164 
00165     IStringStream nodeLine(line);
00166 
00167     label nNodes, nDims, nNodeAttr;
00168     bool hasRegion;
00169 
00170     nodeLine >> nNodes >> nDims >> nNodeAttr >> hasRegion;
00171 
00172 
00173     Info<< "Read .node header:" << endl
00174         << "    nodes     : " << nNodes << endl
00175         << "    nDims     : " << nDims << endl
00176         << "    nAttr     : " << nNodeAttr << endl
00177         << "    hasRegion : " << hasRegion << endl
00178         << endl;
00179 
00180     //
00181     // read points
00182     //
00183 
00184     pointField points(nNodes);
00185     Map<label> nodeToPoint(nNodes);
00186 
00187     {
00188         labelList pointIndex(nNodes);
00189 
00190         label pointI = 0;
00191 
00192         while (nodeStream.good())
00193         {
00194             nodeStream.getLine(line);
00195 
00196             if (line.size() && line[0] != '#')
00197             {
00198                 IStringStream nodeLine(line);
00199 
00200                 label nodeI;
00201                 scalar x, y, z;
00202                 label dummy;
00203 
00204                 nodeLine >> nodeI >> x >> y >> z;
00205 
00206                 for (label i = 0; i < nNodeAttr; i++)
00207                 {
00208                     nodeLine >> dummy;
00209                 }
00210 
00211                 if (hasRegion)
00212                 {
00213                     nodeLine >> dummy;
00214                 }
00215 
00216                 // Store point and node number.
00217                 points[pointI] = point(x, y, z);
00218                 nodeToPoint.insert(nodeI, pointI);
00219                 pointI++;
00220             }
00221         }
00222         if (pointI != nNodes)
00223         {
00224             FatalIOErrorIn(args.executable().c_str(), nodeStream)
00225                 << "Only " << pointI << " nodes present instead of " << nNodes
00226                 << " from header." << exit(FatalIOError);
00227         }
00228     }
00229 
00230     //
00231     // read elements
00232     //
00233 
00234     IFstream eleStream(eleFile);
00235 
00236     do
00237     {
00238         eleStream.getLine(line);
00239     }
00240     while (line.size() && line[0] == '#');
00241 
00242     IStringStream eleLine(line);
00243 
00244     label nTets, nPtsPerTet, nElemAttr;
00245 
00246     eleLine >> nTets >> nPtsPerTet >> nElemAttr;
00247 
00248 
00249     Info<< "Read .ele header:" << endl
00250         << "    tets         : " << nTets << endl
00251         << "    pointsPerTet : " << nPtsPerTet << endl
00252         << "    nAttr        : " << nElemAttr << endl
00253         << endl;
00254 
00255     if (nPtsPerTet != 4)
00256     {
00257         FatalIOErrorIn(args.executable().c_str(), eleStream)
00258             << "Cannot handle tets with "
00259             << nPtsPerTet << " points per tetrahedron in .ele file" << endl
00260             << "Can only handle tetrahedra with four points"
00261             << exit(FatalIOError);
00262     }
00263 
00264     if (nElemAttr != 0)
00265     {
00266         WarningIn(args.executable())
00267             << "Element attributes (third elemenent in .ele header)"
00268             << " not used" << endl;
00269     }
00270 
00271 
00272 
00273     const cellModel& tet = *(cellModeller::lookup("tet"));
00274 
00275     labelList tetPoints(4);
00276 
00277     cellShapeList cells(nTets);
00278     label cellI = 0;
00279 
00280     while (eleStream.good())
00281     {
00282         eleStream.getLine(line);
00283 
00284         if (line.size() && line[0] != '#')
00285         {
00286             IStringStream eleLine(line);
00287 
00288             label elemI;
00289             eleLine >> elemI;
00290 
00291             for (label i = 0; i < 4; i++)
00292             {
00293                 label nodeI;
00294                 eleLine >> nodeI;
00295                 tetPoints[i] = nodeToPoint[nodeI];
00296             }
00297 
00298             cells[cellI++] = cellShape(tet, tetPoints);
00299 
00300             // Skip attributes
00301             for (label i = 0; i < nElemAttr; i++)
00302             {
00303                 label dummy;
00304 
00305                 eleLine >> dummy;
00306             }
00307         }
00308     }
00309 
00310 
00311     //
00312     // Construct mesh with default boundary only
00313     //
00314 
00315     autoPtr<polyMesh> meshPtr
00316     (
00317         new polyMesh
00318         (
00319             IOobject
00320             (
00321                 polyMesh::defaultRegion,
00322                 runTime.constant(),
00323                 runTime
00324             ),
00325             xferCopy(points),
00326             cells,
00327             faceListList(0),
00328             wordList(0),    // boundaryPatchNames
00329             wordList(0),    // boundaryPatchTypes
00330             "defaultFaces",
00331             polyPatch::typeName,
00332             wordList(0)
00333         )
00334     );
00335     const polyMesh& mesh = meshPtr;
00336 
00337 
00338 
00339     if (readFaceFile)
00340     {
00341         label nPatches = 0;
00342 
00343         // List of Foam vertices per boundary face
00344         faceList boundaryFaces;
00345 
00346         // For each boundary faces the Foam patchID
00347         labelList boundaryPatch;
00348 
00349         //
00350         // read boundary faces
00351         //
00352 
00353         IFstream faceStream(faceFile);
00354 
00355         do
00356         {
00357             faceStream.getLine(line);
00358         }
00359         while (line.size() && line[0] == '#');
00360 
00361         IStringStream faceLine(line);
00362 
00363         label nFaces, nFaceAttr;
00364 
00365         faceLine >> nFaces >> nFaceAttr;
00366 
00367 
00368         Info<< "Read .face header:" << endl
00369             << "    faces : " << nFaces << endl
00370             << "    nAttr : " << nFaceAttr << endl
00371             << endl;
00372 
00373 
00374         if (nFaceAttr != 1)
00375         {
00376             FatalIOErrorIn(args.executable().c_str(), faceStream)
00377                 << "Expect boundary markers to be"
00378                 << " present in .face file." << endl
00379                 << "This is the second number in the header which is now:"
00380                 << nFaceAttr << exit(FatalIOError);
00381         }
00382 
00383         // List of Foam vertices per boundary face
00384         boundaryFaces.setSize(nFaces);
00385 
00386         // For each boundary faces the Foam patchID
00387         boundaryPatch.setSize(nFaces);
00388         boundaryPatch = -1;
00389 
00390         label faceI = 0;
00391 
00392         // Region to patch conversion
00393         Map<label> regionToPatch;
00394 
00395         face f(3);
00396 
00397         while (faceStream.good())
00398         {
00399             faceStream.getLine(line);
00400 
00401             if (line.size() && line[0] != '#')
00402             {
00403                 IStringStream faceLine(line);
00404 
00405                 label tetGenFaceI, dummy, region;
00406 
00407                 faceLine >> tetGenFaceI;
00408 
00409                 // Read face and reverse orientation (Foam needs outwards
00410                 // pointing)
00411                 for (label i = 0; i < 3; i++)
00412                 {
00413                     label nodeI;
00414                     faceLine >> nodeI;
00415                     f[2-i] = nodeToPoint[nodeI];
00416                 }
00417 
00418 
00419                 if (findFace(mesh, f) >= mesh.nInternalFaces())
00420                 {
00421                     boundaryFaces[faceI] = f;
00422 
00423                     if (nFaceAttr > 0)
00424                     {
00425                         // First attribute is the region number
00426                         faceLine >> region;
00427 
00428 
00429                         // Get Foam patchID and update region->patch table.
00430                         label patchI = 0;
00431 
00432                         Map<label>::iterator patchFind =
00433                             regionToPatch.find(region);
00434 
00435                         if (patchFind == regionToPatch.end())
00436                         {
00437                             patchI = nPatches;
00438 
00439                             Info<< "Mapping tetgen region " << region
00440                                 << " to Foam patch "
00441                                 << patchI << endl;
00442 
00443                             regionToPatch.insert(region, nPatches++);
00444                         }
00445                         else
00446                         {
00447                             patchI = patchFind();
00448                         }
00449 
00450                         boundaryPatch[faceI] = patchI;
00451 
00452                         // Skip remaining attributes
00453                         for (label i = 1; i < nFaceAttr; i++)
00454                         {
00455                             faceLine >> dummy;
00456                         }
00457                     }
00458 
00459                     faceI++;
00460                 }
00461             }
00462         }
00463 
00464 
00465         // Trim
00466         boundaryFaces.setSize(faceI);
00467         boundaryPatch.setSize(faceI);
00468 
00469 
00470          // Print region to patch mapping
00471         Info<< "Regions:" << endl;
00472 
00473         for
00474         (
00475             Map<label>::const_iterator iter = regionToPatch.begin();
00476             iter != regionToPatch.end();
00477             ++iter
00478         )
00479         {
00480             Info<< "    region:" << iter.key() << '\t' << "patch:"
00481                 << iter() << endl;
00482         }
00483         Info<< endl;
00484 
00485 
00486         // Storage for boundary faces
00487         faceListList patchFaces(nPatches);
00488         wordList patchNames(nPatches);
00489 
00490         forAll(patchNames, patchI)
00491         {
00492             patchNames[patchI] = word("patch") + name(patchI);
00493         }
00494 
00495         wordList patchTypes(nPatches, polyPatch::typeName);
00496         word defaultFacesName = "defaultFaces";
00497         word defaultFacesType = polyPatch::typeName;
00498         wordList patchPhysicalTypes(nPatches, polyPatch::typeName);
00499 
00500 
00501         // Sort boundaryFaces by patch using boundaryPatch.
00502         List<DynamicList<face> > allPatchFaces(nPatches);
00503 
00504         forAll(boundaryPatch, faceI)
00505         {
00506             label patchI = boundaryPatch[faceI];
00507 
00508             allPatchFaces[patchI].append(boundaryFaces[faceI]);
00509         }
00510 
00511         Info<< "Patch sizes:" << endl;
00512 
00513         forAll(allPatchFaces, patchI)
00514         {
00515             Info<< "    " << patchNames[patchI] << " : "
00516                 << allPatchFaces[patchI].size() << endl;
00517 
00518             patchFaces[patchI].transfer(allPatchFaces[patchI]);
00519         }
00520 
00521         Info<< endl;
00522 
00523 
00524         meshPtr.reset
00525         (
00526             new polyMesh
00527             (
00528                 IOobject
00529                 (
00530                     polyMesh::defaultRegion,
00531                     runTime.constant(),
00532                     runTime
00533                 ),
00534                 xferMove(points),
00535                 cells,
00536                 patchFaces,
00537                 patchNames,
00538                 patchTypes,
00539                 defaultFacesName,
00540                 defaultFacesType,
00541                 patchPhysicalTypes
00542             )
00543         );
00544     }
00545 
00546 
00547     Info<< "Writing mesh to " << runTime.constant() << endl << endl;
00548 
00549     meshPtr().write();
00550 
00551     Info<< "End\n" << endl;
00552 
00553     return 0;
00554 }
00555 
00556 
00557 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd