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

netgenNeutralToFoam.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 neutral file format as written by Netgen v4.4.
00027 
00028     Example:
00029 
00030     9
00031       1.000000  1.000000  1.000000
00032       0.000000  1.000000  1.000000
00033       0.000000  0.000000  1.000000
00034       1.000000  0.000000  1.000000
00035       0.000000  1.000000  0.000000
00036       1.000000  1.000000  0.000000
00037       1.000000  0.000000  0.000000
00038       0.000000  0.000000  0.000000
00039       0.500000  0.500000  0.500000
00040     12
00041        1          7        8        9        3
00042        1          5        9        6        8
00043        1          5        9        2        1
00044        1          4        9        7        6
00045        1          7        8        6        9
00046        1          4        6        1        9
00047        1          5        9        8        2
00048        1          4        1        2        9
00049        1          1        6        5        9
00050        1          2        3        4        9
00051        1          8        9        3        2
00052        1          4        9        3        7
00053     12
00054        1            1        2        4
00055        1            3        4        2
00056        2            5        6        8
00057        2            7        8        6
00058        3            1        4        6
00059        3            7        6        4
00060        5            2        1        5
00061        5            6        5        1
00062        5            3        2        8
00063        5            5        8        2
00064        6            4        3        7
00065        6            8        7        3
00066 
00067 NOTE:
00068     - reverse order of boundary faces using geometric test.
00069       (not very space efficient)
00070     - order of tet vertices only tested on one file.
00071     - all patch/cell/vertex numbers offset by one.
00072 
00073 \*---------------------------------------------------------------------------*/
00074 
00075 #include "argList.H"
00076 #include "Time.H"
00077 #include "polyMesh.H"
00078 #include "IFstream.H"
00079 #include "polyPatch.H"
00080 #include "cellModeller.H"
00081 #include "triFace.H"
00082 
00083 using namespace Foam;
00084 
00085 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00086 
00087 // Main program:
00088 
00089 int main(int argc, char *argv[])
00090 {
00091     argList::validArgs.append("Neutral file");
00092 
00093 #   include "setRootCase.H"
00094 #   include "createTime.H"
00095 
00096     fileName neuFile(args.additionalArgs()[0]);
00097 
00098 
00099     IFstream str(neuFile);
00100 
00101 
00102     //
00103     // Read nodes.
00104     //
00105     label nNodes(readLabel(str));
00106 
00107     Info<< "nNodes:" << nNodes << endl;
00108 
00109 
00110     pointField points(nNodes);
00111 
00112     forAll(points, pointI)
00113     {
00114         scalar x,y,z;
00115 
00116         str >> x >> y >> z;
00117 
00118         points[pointI] = point(x, y, z);
00119     }
00120 
00121 
00122 
00123 
00124     label nTets(readLabel(str));
00125 
00126     Info<< "nTets:" << nTets << endl;
00127 
00128     const cellModel& tet = *(cellModeller::lookup("tet"));
00129 
00130     cellShapeList cells(nTets);
00131 
00132     labelList tetPoints(4);
00133 
00134     forAll(cells, cellI)
00135     {
00136         label domain(readLabel(str));
00137 
00138         if (domain != 1)
00139         {
00140             WarningIn(args.executable())
00141                 << "Cannot handle multiple domains"
00142                 << nl << "Ignoring domain " << domain << " setting on line "
00143                 << str.lineNumber() << endl;
00144         }
00145 
00146         tetPoints[1] = readLabel(str) - 1;
00147         tetPoints[0] = readLabel(str) - 1;
00148         tetPoints[2] = readLabel(str) - 1;
00149         tetPoints[3] = readLabel(str) - 1;
00150 
00151         cells[cellI] = cellShape(tet, tetPoints);
00152     }
00153 
00154 
00155 
00156     label nFaces(readLabel(str));
00157 
00158     Info<< "nFaces:" << nFaces << endl;
00159 
00160     // Unsorted boundary faces
00161     faceList boundaryFaces(nFaces);
00162 
00163     // For each boundary faces the Foam patchID
00164     labelList boundaryPatch(nFaces, -1);
00165 
00166     // Max patch.
00167     label maxPatch = 0;
00168 
00169     // Boundary faces as three vertices
00170     HashTable<label, triFace, Hash<triFace> > vertsToBoundary(nFaces);
00171 
00172     forAll(boundaryFaces, faceI)
00173     {
00174         label patchI(readLabel(str));
00175 
00176         if (patchI < 0)
00177         {
00178             FatalErrorIn(args.executable())
00179                 << "Invalid boundary region number " << patchI
00180                 << " on line " << str.lineNumber()
00181                 << exit(FatalError);
00182         }
00183 
00184 
00185         maxPatch = max(maxPatch, patchI);
00186 
00187         triFace tri(readLabel(str)-1, readLabel(str)-1, readLabel(str)-1);
00188 
00189         // Store boundary face as is for now. Later on reverse it.
00190         boundaryFaces[faceI].setSize(3);
00191         boundaryFaces[faceI][0] = tri[0];
00192         boundaryFaces[faceI][1] = tri[1];
00193         boundaryFaces[faceI][2] = tri[2];
00194         boundaryPatch[faceI] = patchI;
00195 
00196         vertsToBoundary.insert(tri, faceI);
00197     }
00198 
00199     label nPatches = maxPatch + 1;
00200 
00201 
00202     // Use hash of points to get owner cell and orient the boundary face.
00203     // For storage reasons I store the triangles and loop over the cells instead
00204     // of the other way around (store cells and loop over triangles) though
00205     // that would be faster.
00206     forAll(cells, cellI)
00207     {
00208         const cellShape& cll = cells[cellI];
00209 
00210         // Get the four (outwards pointing) faces of the cell
00211         faceList tris(cll.faces());
00212 
00213         forAll(tris, i)
00214         {
00215             const face& f = tris[i];
00216 
00217             // Is there any boundary face with same vertices?
00218             // (uses commutative hash)
00219             HashTable<label, triFace, Hash<triFace> >::iterator iter =
00220                 vertsToBoundary.find(triFace(f[0], f[1], f[2]));
00221 
00222             if (iter != vertsToBoundary.end())
00223             {
00224                 label faceI = iter();
00225                 const triFace& tri = iter.key();
00226 
00227                 // Determine orientation of tri v.s. cell centre.
00228                 point cc(cll.centre(points));
00229                 point fc(tri.centre(points));
00230                 vector fn(tri.normal(points));
00231 
00232                 if (((fc - cc) & fn) < 0)
00233                 {
00234                     // Boundary face points inwards. Flip.
00235                     boundaryFaces[faceI] = boundaryFaces[faceI].reverseFace();
00236                 }
00237 
00238                 // Done this face so erase from hash
00239                 vertsToBoundary.erase(iter);
00240             }
00241         }
00242     }
00243 
00244 
00245     if (vertsToBoundary.size())
00246     {
00247         // Didn't find cells connected to boundary faces.
00248         WarningIn(args.executable())
00249             << "There are boundary faces without attached cells."
00250             << "Boundary faces (as triFaces):" << vertsToBoundary.toc()
00251             << endl;
00252     }
00253 
00254 
00255     // Storage for boundary faces sorted into patches
00256 
00257     faceListList patchFaces(nPatches);
00258 
00259     wordList patchNames(nPatches);
00260 
00261     forAll(patchNames, patchI)
00262     {
00263         patchNames[patchI] = word("patch") + name(patchI);
00264     }
00265 
00266     wordList patchTypes(nPatches, polyPatch::typeName);
00267     word defaultFacesName = "defaultFaces";
00268     word defaultFacesType = polyPatch::typeName;
00269     wordList patchPhysicalTypes(nPatches, polyPatch::typeName);
00270 
00271     {
00272         // Sort boundaryFaces by patch.
00273         List<DynamicList<face> > allPatchFaces(nPatches);
00274 
00275         forAll(boundaryPatch, faceI)
00276         {
00277             label patchI = boundaryPatch[faceI];
00278 
00279             allPatchFaces[patchI].append(boundaryFaces[faceI]);
00280         }
00281 
00282         Info<< "Patches:" << nl
00283             << "\tNeutral Boundary\tPatch name\tSize" << nl
00284             << "\t----------------\t----------\t----" << endl;
00285 
00286         forAll(allPatchFaces, patchI)
00287         {
00288             Info<< '\t' << patchI << "\t\t\t"
00289                 << patchNames[patchI] << "\t\t"
00290                 << allPatchFaces[patchI].size() << endl;
00291 
00292             patchFaces[patchI].transfer(allPatchFaces[patchI]);
00293         }
00294 
00295         Info<< endl;
00296     }
00297 
00298 
00299     polyMesh mesh
00300     (
00301         IOobject
00302         (
00303             polyMesh::defaultRegion,
00304             runTime.constant(),
00305             runTime
00306         ),
00307         xferMove(points),
00308         cells,
00309         patchFaces,
00310         patchNames,
00311         patchTypes,
00312         defaultFacesName,
00313         defaultFacesType,
00314         patchPhysicalTypes
00315     );
00316 
00317     Info<< "Writing mesh to " << runTime.constant() << endl << endl;
00318 
00319     mesh.write();
00320 
00321 
00322     Info<< "End\n" << endl;
00323 
00324     return 0;
00325 }
00326 
00327 
00328 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd