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

STARCDMeshWriter.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 "STARCDMeshWriter.H"
00028 
00029 #include "Time.H"
00030 #include "SortableList.H"
00031 #include "OFstream.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 const char* Foam::meshWriters::STARCD::defaultBoundaryName =
00036     "Default_Boundary_Region";
00037 
00038 const Foam::label Foam::meshWriters::STARCD::foamToStarFaceAddr[4][6] =
00039 {
00040     { 4, 5, 2, 3, 0, 1 },     // 11 = pro-STAR hex
00041     { 0, 1, 4, 5, 2, -1 },    // 12 = pro-STAR prism
00042     { 5, 4, 2, 0, -1, -1 },   // 13 = pro-STAR tetra
00043     { 0, 4, 3, 5, 2, -1 }     // 14 = pro-STAR pyramid
00044 };
00045 
00046 
00047 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00048 
00049 Foam::label Foam::meshWriters::STARCD::findDefaultBoundary() const
00050 {
00051     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00052 
00053     label id = -1;
00054 
00055     // find Default_Boundary_Region if it exists
00056     forAll(patches, patchI)
00057     {
00058         if (defaultBoundaryName == patches[patchI].name())
00059         {
00060             id = patchI;
00061             break;
00062         }
00063     }
00064     return id;
00065 }
00066 
00067 
00068 void Foam::meshWriters::STARCD::getCellTable()
00069 {
00070     // read constant/polyMesh/propertyName
00071     IOList<label> ioList
00072     (
00073         IOobject
00074         (
00075             "cellTableId",
00076             "constant",
00077             polyMesh::meshSubDir,
00078             mesh_,
00079             IOobject::READ_IF_PRESENT,
00080             IOobject::NO_WRITE,
00081             false
00082         )
00083     );
00084 
00085     bool useCellZones = false;
00086     cellTableId_.setSize(mesh_.nCells(), -1);
00087 
00088     // get information from constant/polyMesh/cellTableId if possible
00089     if (ioList.headerOk())
00090     {
00091         if (ioList.size() == mesh_.nCells())
00092         {
00093             cellTableId_.transfer(ioList);
00094 
00095             if (cellTable_.empty())
00096             {
00097                 Info<< "no cellTable information available" << endl;
00098             }
00099         }
00100         else
00101         {
00102             WarningIn("STARCD::getCellTable()")
00103                 << ioList.objectPath() << " has incorrect number of cells "
00104                 << " - use cellZone information"
00105                 << endl;
00106 
00107             ioList.clear();
00108             useCellZones = true;
00109         }
00110     }
00111     else
00112     {
00113         useCellZones = true;
00114     }
00115 
00116 
00117     if (useCellZones)
00118     {
00119         if (cellTable_.empty())
00120         {
00121             Info<< "created cellTable from cellZones" << endl;
00122             cellTable_ = mesh_;
00123         }
00124 
00125         // track if there are unzoned cells
00126         label nUnzoned = mesh_.nCells();
00127 
00128         // get the cellZone <-> cellTable correspondence
00129         Info<< "matching cellZones to cellTable" << endl;
00130 
00131         forAll (mesh_.cellZones(), zoneI)
00132         {
00133             const cellZone& cZone = mesh_.cellZones()[zoneI];
00134             if (cZone.size())
00135             {
00136                 nUnzoned -= cZone.size();
00137 
00138                 label tableId = cellTable_.findIndex(cZone.name());
00139                 if (tableId < 0)
00140                 {
00141                     dictionary dict;
00142 
00143                     dict.add("Label", cZone.name());
00144                     dict.add("MaterialType", "fluid");
00145                     tableId = cellTable_.append(dict);
00146                 }
00147 
00148                 forAll (cZone, i)
00149                 {
00150                     cellTableId_[cZone[i]] = tableId;
00151                 }
00152             }
00153         }
00154 
00155         if (nUnzoned)
00156         {
00157             dictionary dict;
00158 
00159             dict.add("Label", "__unZonedCells__");
00160             dict.add("MaterialType", "fluid");
00161             label tableId = cellTable_.append(dict);
00162 
00163             forAll (cellTableId_, i)
00164             {
00165                 if (cellTableId_[i] < 0)
00166                 {
00167                     cellTableId_[i] = tableId;
00168                 }
00169             }
00170         }
00171     }
00172 }
00173 
00174 
00175 void Foam::meshWriters::STARCD::writeHeader(Ostream& os, const char* filetype)
00176 {
00177     os  << "PROSTAR_" << filetype << nl
00178         << 4000
00179         << " " << 0
00180         << " " << 0
00181         << " " << 0
00182         << " " << 0
00183         << " " << 0
00184         << " " << 0
00185         << " " << 0
00186         << endl;
00187 }
00188 
00189 
00190 void Foam::meshWriters::STARCD::writePoints(const fileName& prefix) const
00191 {
00192     OFstream os(prefix + ".vrt");
00193     writeHeader(os, "VERTEX");
00194 
00195     // Set the precision of the points data to 10
00196     os.precision(10);
00197 
00198     // force decimal point for Fortran input
00199     os.setf(std::ios::showpoint);
00200 
00201     const pointField& points = mesh_.points();
00202 
00203     Info<< "Writing " << os.name() << " : "
00204         << points.size() << " points" << endl;
00205 
00206     forAll(points, ptI)
00207     {
00208         // convert [m] -> [mm]
00209         os
00210             << ptI + 1 << " "
00211             << scaleFactor_ * points[ptI].x() << " "
00212             << scaleFactor_ * points[ptI].y() << " "
00213             << scaleFactor_ * points[ptI].z() << nl;
00214     }
00215     os.flush();
00216 
00217 }
00218 
00219 
00220 void Foam::meshWriters::STARCD::writeCells(const fileName& prefix) const
00221 {
00222     OFstream os(prefix + ".cel");
00223     writeHeader(os, "CELL");
00224 
00225     // this is what we seem to need
00226     // map foam cellModeller index -> star shape
00227     Map<label> shapeLookupIndex;
00228     shapeLookupIndex.insert(hexModel->index(), 11);
00229     shapeLookupIndex.insert(prismModel->index(), 12);
00230     shapeLookupIndex.insert(tetModel->index(), 13);
00231     shapeLookupIndex.insert(pyrModel->index(), 14);
00232 
00233     const cellShapeList& shapes = mesh_.cellShapes();
00234     const cellList& cells  = mesh_.cells();
00235     const faceList& faces  = mesh_.faces();
00236     const labelList& owner = mesh_.faceOwner();
00237 
00238     Info<< "Writing " << os.name() << " : "
00239         << cells.size() << " cells" << endl;
00240 
00241     forAll(cells, cellId)
00242     {
00243         label tableId = cellTableId_[cellId];
00244         label materialType  = 1;        // 1(fluid)
00245         if (cellTable_.found(tableId))
00246         {
00247             const dictionary& dict = cellTable_[tableId];
00248             if (dict.found("MaterialType"))
00249             {
00250                 word matType;
00251                 dict.lookup("MaterialType") >> matType;
00252                 if (matType == "solid")
00253                 {
00254                     materialType = 2;
00255                 }
00256 
00257             }
00258         }
00259 
00260         const cellShape& shape = shapes[cellId];
00261         label mapIndex = shape.model().index();
00262 
00263         // a registered primitive type
00264         if (shapeLookupIndex.found(mapIndex))
00265         {
00266             label shapeId = shapeLookupIndex[mapIndex];
00267             const labelList& vrtList = shapes[cellId];
00268 
00269             os  << cellId + 1
00270                 << " " << shapeId
00271                 << " " << vrtList.size()
00272                 << " " << tableId
00273                 << " " << materialType;
00274 
00275             // primitives have <= 8 vertices, but prevent overrun anyhow
00276             // indent following lines for ease of reading
00277             label count = 0;
00278             forAll(vrtList, i)
00279             {
00280                 if ((count % 8) == 0)
00281                 {
00282                     os  << nl
00283                         << "  " << cellId + 1;
00284                 }
00285                 os << " " << vrtList[i] + 1;
00286                 count++;
00287             }
00288             os << endl;
00289 
00290         }
00291         else
00292         {
00293             label shapeId = 255;        // treat as general polyhedral
00294             const labelList& cFaces  = cells[cellId];
00295 
00296             // create (beg,end) indices
00297             List<label> indices(cFaces.size() + 1);
00298             indices[0] = indices.size();
00299 
00300             label count = indices.size();
00301             // determine the total number of vertices
00302             forAll(cFaces, faceI)
00303             {
00304                 count += faces[cFaces[faceI]].size();
00305                 indices[faceI+1] = count;
00306             }
00307 
00308             os  << cellId + 1
00309                 << " " << shapeId
00310                 << " " << count
00311                 << " " << tableId
00312                 << " " << materialType;
00313 
00314             // write indices - max 8 per line
00315             // indent following lines for ease of reading
00316             count = 0;
00317             forAll(indices, i)
00318             {
00319                 if ((count % 8) == 0)
00320                 {
00321                     os  << nl
00322                         << "  " << cellId + 1;
00323                 }
00324                 os << " " << indices[i];
00325                 count++;
00326             }
00327 
00328             // write faces - max 8 per line
00329             forAll(cFaces, faceI)
00330             {
00331                 label meshFace = cFaces[faceI];
00332                 face f;
00333 
00334                 if (owner[meshFace] == cellId)
00335                 {
00336                     f = faces[meshFace];
00337                 }
00338                 else
00339                 {
00340                     f = faces[meshFace].reverseFace();
00341                 }
00342 
00343                 forAll(f, i)
00344                 {
00345                     if ((count % 8) == 0)
00346                     {
00347                         os  << nl
00348                             << "  " << cellId + 1;
00349                     }
00350 
00351                     os << " " << f[i] + 1;
00352                     count++;
00353                 }
00354             }
00355 
00356             os << endl;
00357         }
00358     }
00359 }
00360 
00361 
00362 void Foam::meshWriters::STARCD::writeBoundary(const fileName& prefix) const
00363 {
00364     OFstream os(prefix + ".bnd");
00365     writeHeader(os, "BOUNDARY");
00366 
00367     const cellShapeList& shapes = mesh_.cellShapes();
00368     const cellList& cells  = mesh_.cells();
00369     const faceList& faces  = mesh_.faces();
00370     const labelList& owner = mesh_.faceOwner();
00371     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00372 
00373     // this is what we seem to need
00374     // these MUST correspond to foamToStarFaceAddr
00375     //
00376     Map<label> faceLookupIndex;
00377     faceLookupIndex.insert(hexModel->index(), 0);
00378     faceLookupIndex.insert(prismModel->index(), 1);
00379     faceLookupIndex.insert(tetModel->index(), 2);
00380     faceLookupIndex.insert(pyrModel->index(), 3);
00381 
00382     Info<< "Writing " << os.name() << " : "
00383         << (mesh_.nFaces() - patches[0].start()) << " boundaries" << endl;
00384 
00385 
00386     label defaultId = findDefaultBoundary();
00387 
00388     //
00389     // write boundary faces - skip Default_Boundary_Region entirely
00390     //
00391     label boundId = 0;
00392     forAll(patches, patchI)
00393     {
00394         label regionId = patchI;
00395         if (regionId == defaultId)
00396         {
00397             continue;   // skip - already written
00398         }
00399         else if (defaultId == -1 || regionId < defaultId)
00400         {
00401             regionId++;
00402         }
00403 
00404         label patchStart = patches[patchI].start();
00405         label patchSize  = patches[patchI].size();
00406         word  bndType = boundaryRegion_.boundaryType(patches[patchI].name());
00407 
00408         for
00409         (
00410             label faceI = patchStart;
00411             faceI < (patchStart + patchSize);
00412             ++faceI
00413         )
00414         {
00415             label cellId = owner[faceI];
00416             const labelList& cFaces  = cells[cellId];
00417             const cellShape& shape = shapes[cellId];
00418             label cellFaceId = findIndex(cFaces, faceI);
00419 
00420             //      Info<< "cell " << cellId + 1 << " face " << faceI
00421             //          << " == " << faces[faceI]
00422             //          << " is index " << cellFaceId << " from " << cFaces;
00423 
00424             // Unfortunately, the order of faces returned by
00425             //   primitiveMesh::cells() is not necessarily the same
00426             //   as defined by primitiveMesh::cellShapes()
00427             // Thus, for registered primitive types, do the lookup ourselves.
00428             // Finally, the cellModel face number is re-mapped to the
00429             // STAR-CD local face number
00430 
00431             label mapIndex = shape.model().index();
00432 
00433             // a registered primitive type
00434             if (faceLookupIndex.found(mapIndex))
00435             {
00436                 const faceList sFaces = shape.faces();
00437                 forAll(sFaces, sFaceI)
00438                 {
00439                     if (faces[faceI] == sFaces[sFaceI])
00440                     {
00441                         cellFaceId = sFaceI;
00442                         break;
00443                     }
00444                 }
00445 
00446                 mapIndex = faceLookupIndex[mapIndex];
00447                 cellFaceId = foamToStarFaceAddr[mapIndex][cellFaceId];
00448             }
00449             // Info<< endl;
00450 
00451             boundId++;
00452 
00453             os
00454                 << boundId
00455                 << " " << cellId + 1
00456                 << " " << cellFaceId + 1
00457                 << " " << regionId
00458                 << " " << 0
00459                 << " " << bndType.c_str()
00460                 << endl;
00461         }
00462     }
00463 }
00464 
00465 
00466 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00467 
00468 Foam::meshWriters::STARCD::STARCD
00469 (
00470     const polyMesh& mesh,
00471     const scalar scaleFactor
00472 )
00473 :
00474     meshWriter(mesh, scaleFactor)
00475 {
00476     boundaryRegion_.readDict(mesh_);
00477     cellTable_.readDict(mesh_);
00478     getCellTable();
00479 }
00480 
00481 
00482 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00483 
00484 Foam::meshWriters::STARCD::~STARCD()
00485 {}
00486 
00487 
00488 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00489 
00490 void Foam::meshWriters::STARCD::rmFiles(const fileName& baseName) const
00491 {
00492     rm(baseName + ".vrt");
00493     rm(baseName + ".cel");
00494     rm(baseName + ".bnd");
00495     rm(baseName + ".inp");
00496 }
00497 
00498 
00499 bool Foam::meshWriters::STARCD::write(const fileName& meshName) const
00500 {
00501     fileName baseName(meshName);
00502 
00503     if (baseName.empty())
00504     {
00505         baseName = meshWriter::defaultMeshName;
00506 
00507         if
00508         (
00509             mesh_.time().timeName() != "0"
00510          && mesh_.time().timeName() != "constant"
00511         )
00512         {
00513             baseName += "_" + mesh_.time().timeName();
00514         }
00515     }
00516 
00517     rmFiles(baseName);
00518     writePoints(baseName);
00519     writeCells(baseName);
00520 
00521     if (writeBoundary_)
00522     {
00523         writeBoundary(baseName);
00524     }
00525 
00526     return true;
00527 }
00528 
00529 
00530 bool Foam::meshWriters::STARCD::writeSurface
00531 (
00532     const fileName& meshName,
00533     const bool& triangulate
00534 ) const
00535 {
00536     fileName baseName(meshName);
00537 
00538     if (baseName.empty())
00539     {
00540         baseName = meshWriter::defaultSurfaceName;
00541 
00542         if
00543         (
00544             mesh_.time().timeName() != "0"
00545          && mesh_.time().timeName() != "constant"
00546         )
00547         {
00548             baseName += "_" + mesh_.time().timeName();
00549         }
00550     }
00551 
00552     rmFiles(baseName);
00553 
00554     OFstream celFile(baseName + ".cel");
00555     writeHeader(celFile, "CELL");
00556 
00557     Info << "Writing " << celFile.name() << endl;
00558 
00559     // mesh and patch info
00560     const pointField& points = mesh_.points();
00561     const labelList& owner = mesh_.faceOwner();
00562     const faceList& meshFaces = mesh_.faces();
00563     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00564 
00565     label shapeId = 3;  // shell/baffle element
00566     label typeId  = 4;  // 4(shell)
00567 
00568     // remember which points need to be written
00569     labelHashSet pointHash;
00570 
00571     // write boundary faces as normal STAR-CD mesh
00572     if (triangulate)
00573     {
00574         // cell Id has no particular meaning - just increment
00575         // use the cellTable id from the patch Number
00576         label cellId = 0;
00577 
00578         forAll(patches, patchI)
00579         {
00580             label patchStart = patches[patchI].start();
00581             label patchSize  = patches[patchI].size();
00582 
00583             label ctableId = patchI + 1;
00584 
00585             for
00586             (
00587                 label faceI = patchStart;
00588                 faceI < (patchStart + patchSize);
00589                 ++faceI
00590             )
00591             {
00592                 const face& f = meshFaces[faceI];
00593 
00594                 label nTri = f.nTriangles(points);
00595                 faceList triFaces;
00596 
00597                 // triangulate polygons, but not quads
00598                 if (nTri <= 2)
00599                 {
00600                     triFaces.setSize(1);
00601                     triFaces[0] = f;
00602                 }
00603                 else
00604                 {
00605                     triFaces.setSize(nTri);
00606                     nTri = 0;
00607                     f.triangles(points, nTri, triFaces);
00608                 }
00609 
00610                 forAll(triFaces, faceI)
00611                 {
00612                     const labelList& vrtList = triFaces[faceI];
00613 
00614                     celFile
00615                         << cellId + 1 << " "
00616                         << shapeId << " "
00617                         << vrtList.size() << " "
00618                         << ctableId << " "
00619                         << typeId;
00620 
00621                     // must be 3 (triangle) but could be quad
00622                     label count = 0;
00623                     forAll(vrtList, i)
00624                     {
00625                         if ((count % 8) == 0)
00626                         {
00627                             celFile
00628                                 << nl
00629                                 << "  " << cellId + 1;
00630                         }
00631                         // remember which points we'll need to write
00632                         pointHash.insert(vrtList[i]);
00633                         celFile << " " << vrtList[i] + 1;
00634                         count++;
00635                     }
00636                     celFile << endl;
00637 
00638                     cellId++;
00639                 }
00640             }
00641         }
00642     }
00643     else
00644     {
00645         // cell Id is the OpenFOAM face Id
00646         // use the cellTable id from the face owner
00647         // - allows separation of parts
00648         forAll(patches, patchI)
00649         {
00650             label patchStart = patches[patchI].start();
00651             label patchSize  = patches[patchI].size();
00652 
00653             for
00654             (
00655                 label faceI = patchStart;
00656                 faceI < (patchStart + patchSize);
00657                 ++faceI
00658             )
00659             {
00660                 const labelList& vrtList = meshFaces[faceI];
00661                 label cellId = faceI;
00662 
00663                 celFile
00664                     << cellId + 1 << " "
00665                     << shapeId << " "
00666                     << vrtList.size() << " "
00667                     << cellTableId_[owner[faceI]] << " "
00668                     << typeId;
00669 
00670                 // likely <= 8 vertices, but prevent overrun anyhow
00671                 label count = 0;
00672                 forAll(vrtList, i)
00673                 {
00674                     if ((count % 8) == 0)
00675                     {
00676                         celFile
00677                             << nl
00678                             << "  " << cellId + 1;
00679                     }
00680                     // remember which points we'll need to write
00681                     pointHash.insert(vrtList[i]);
00682                     celFile << " " << vrtList[i] + 1;
00683                     count++;
00684                 }
00685                 celFile << endl;
00686             }
00687         }
00688     }
00689 
00690     OFstream vrtFile(baseName + ".vrt");
00691     writeHeader(vrtFile, "VERTEX");
00692 
00693     vrtFile.precision(10);
00694     vrtFile.setf(std::ios::showpoint);  // force decimal point for Fortran
00695 
00696     Info << "Writing " << vrtFile.name() << endl;
00697 
00698     // build sorted table of contents
00699     SortableList<label> toc(pointHash.size());
00700     {
00701         label i = 0;
00702         forAllConstIter(labelHashSet, pointHash, iter)
00703         {
00704             toc[i++] = iter.key();
00705         }
00706     }
00707     toc.sort();
00708     toc.shrink();
00709     pointHash.clear();
00710 
00711     // write points in sorted order
00712     forAll(toc, i)
00713     {
00714         label vrtId = toc[i];
00715         vrtFile
00716             << vrtId + 1
00717             << " " << scaleFactor_ * points[vrtId].x()
00718             << " " << scaleFactor_ * points[vrtId].y()
00719             << " " << scaleFactor_ * points[vrtId].z()
00720             << endl;
00721     }
00722 
00723     return true;
00724 }
00725 
00726 
00727 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd