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

ccm26ToFoam.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 CCM files as written by Prostar/ccm using ccm 2.6 (not 2.4)
00027 
00028     - does polyhedral mesh
00029     - does not handle 'interfaces' (couples)
00030     - does not handle cyclics. Use createPatch to recreate these
00031     - does not do data
00032     - does patch names only if they are in the problem description
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #include "ListOps.H"
00037 #include "argList.H"
00038 #include "Time.H"
00039 #include "fvMesh.H"
00040 #include "volFields.H"
00041 #include "emptyPolyPatch.H"
00042 #include "symmetryPolyPatch.H"
00043 #include "wallPolyPatch.H"
00044 #include "SortableList.H"
00045 #include "cellSet.H"
00046 
00047 #include <ccmio.h>
00048 #include <vector>
00049 
00050 using namespace Foam;
00051 
00052 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00053 
00054 static char const kDefaultState[] = "default";
00055 static int const kVertOffset = 2;
00056 
00057 
00058 // Determine upper-triangular order for internal faces:
00059 labelList getInternalFaceOrder
00060 (
00061     const cellList& cells,
00062     const labelList& owner,
00063     const labelList& neighbour
00064 )
00065 {
00066     labelList oldToNew(owner.size(), -1);
00067 
00068     // First unassigned face
00069     label newFaceI = 0;
00070 
00071     forAll(cells, cellI)
00072     {
00073         const labelList& cFaces = cells[cellI];
00074 
00075         SortableList<label> nbr(cFaces.size());
00076 
00077         forAll(cFaces, i)
00078         {
00079             label faceI = cFaces[i];
00080 
00081             label nbrCellI = neighbour[faceI];
00082 
00083             if (nbrCellI != -1)
00084             {
00085                 // Internal face. Get cell on other side.
00086                 if (nbrCellI == cellI)
00087                 {
00088                     nbrCellI = owner[faceI];
00089                 }
00090 
00091                 if (cellI < nbrCellI)
00092                 {
00093                     // CellI is master
00094                     nbr[i] = nbrCellI;
00095                 }
00096                 else
00097                 {
00098                     // nbrCell is master. Let it handle this face.
00099                     nbr[i] = -1;
00100                 }
00101             }
00102             else
00103             {
00104                 // External face. Do later.
00105                 nbr[i] = -1;
00106             }
00107         }
00108 
00109         nbr.sort();
00110 
00111         forAll(nbr, i)
00112         {
00113             if (nbr[i] != -1)
00114             {
00115                 oldToNew[cFaces[nbr.indices()[i]]] = newFaceI++;
00116             }
00117         }
00118     }
00119 
00120     // Keep boundary faces in same order.
00121     for (label faceI = newFaceI; faceI < owner.size(); faceI++)
00122     {
00123         oldToNew[faceI] = faceI;
00124     }
00125 
00126     return oldToNew;
00127 }
00128 
00129 
00130 void storeCellInZone
00131 (
00132     const label cellI,
00133     const label cellType,
00134     Map<label>& typeToZone,
00135     List<DynamicList<label> >& zoneCells
00136 )
00137 {
00138     if (cellType >= 0)
00139     {
00140         Map<label>::iterator zoneFnd = typeToZone.find(cellType);
00141 
00142         if (zoneFnd == typeToZone.end())
00143         {
00144             // New type. Allocate zone for it.
00145             zoneCells.setSize(zoneCells.size() + 1);
00146 
00147             label zoneI = zoneCells.size()-1;
00148 
00149             Info<< "Mapping type " << cellType << " to Foam cellZone "
00150                 << zoneI << endl;
00151             typeToZone.insert(cellType, zoneI);
00152 
00153             zoneCells[zoneI].append(cellI);
00154         }
00155         else
00156         {
00157             // Existing zone for type
00158             zoneCells[zoneFnd()].append(cellI);
00159         }
00160     }
00161 }
00162 
00163 
00164 void CheckError(CCMIOError const &err, const Foam::string& str)
00165 {
00166     if (err != kCCMIONoErr)
00167     {
00168         FatalErrorIn("CheckError")
00169             << str << exit(FatalError);
00170     }
00171 }
00172 
00173 
00174 void ReadVertices
00175 (
00176     CCMIOError &err,
00177     CCMIOID &vertices,
00178     labelList& foamPointMap,
00179     pointField& foamPoints
00180 )
00181 {
00182 
00183     // Read the vertices.  This involves reading both the vertex data and
00184     // the map, which maps the index into the data array with the ID number.
00185     // As we process the vertices we need to be sure to scale them by the
00186     // appropriate scaling factor.  The offset is just to show you can read
00187     // any chunk.  Normally this would be in a for loop.
00188 
00189     CCMIOSize nVertices;
00190     CCMIOEntitySize(&err, vertices, &nVertices, NULL);
00191 
00192     List<int> mapData(nVertices, 0);
00193     List<float> verts(3*nVertices, 0);
00194 
00195     int offset = 0;
00196     int offsetPlusSize = offset+nVertices;
00197 
00198     int dims = 1;
00199     float scale;
00200     CCMIOID mapID;
00201     CCMIOReadVerticesf(&err, vertices, &dims, &scale, &mapID, verts.begin(),
00202                offset, offsetPlusSize);
00203     CCMIOReadMap(&err, mapID, mapData.begin(), offset, offsetPlusSize);
00204 
00205     //CCMIOSize size;
00206     //CCMIOEntityDescription(&err, vertices, &size, NULL);
00207     //char *desc = new char[size + 1];
00208     //CCMIOEntityDescription(&err, vertices, NULL, desc);
00209     //Pout<< "label: '" << desc << "'" << endl;
00210     //delete [] desc;
00211 
00212     // Convert to foamPoints
00213     foamPoints.setSize(nVertices);
00214     foamPoints = vector::zero;
00215     foamPointMap.setSize(nVertices);
00216 
00217     forAll(foamPointMap, i)
00218     {
00219         foamPointMap[i] = mapData[i];
00220         for (direction cmpt = 0; cmpt < dims; cmpt++)
00221         {
00222             foamPoints[i][cmpt] = verts[dims*i + cmpt]*scale;
00223         }
00224     }
00225 }
00226 
00227 
00228 void ReadProblem
00229 (
00230     CCMIOError &err,
00231     CCMIOID& problem,
00232 
00233     const Map<label>& prostarToFoamPatch,
00234     Map<word>& foamCellTypeNames,
00235     wordList& foamPatchTypes,
00236     wordList& foamPatchNames
00237 )
00238 {
00239     // ... walk through each cell type and print it...
00240     CCMIOID next;
00241     int i = 0;
00242     while
00243     (
00244         CCMIONextEntity(NULL, problem, kCCMIOCellType, &i, &next)
00245      == kCCMIONoErr
00246     )
00247     {
00248     char *name;
00249     int size;
00250         int cellType;
00251 
00252     // ... if it has a material type.  (Note that we do not pass in
00253     // an array to get the name because we do not know how long the
00254     // string is yet.  Many parameters to CCMIO functions that
00255         // return
00256     // data can be NULL if that data is not needed.)
00257     if
00258         (
00259             CCMIOReadOptstr(NULL, next, "MaterialType", &size, NULL)
00260          == kCCMIONoErr
00261         )
00262     {
00263         name = new char[size + 1];
00264         CCMIOReadOptstr(&err, next, "MaterialType", &size, name);
00265         CCMIOGetEntityIndex(&err, next, &cellType);
00266 
00267             foamCellTypeNames.insert(cellType, name);
00268             Pout<< "Celltype:" << cellType << " name:" << name << endl;
00269 
00270         delete [] name;
00271     }
00272     }
00273 
00274     // ... walk through each region description and print it...
00275 
00276     
00277     CCMIOID boundary;
00278     label regionI = 0;
00279     int k = 0;
00280     while
00281     (
00282         CCMIONextEntity(NULL, problem, kCCMIOBoundaryRegion, &k, &boundary)
00283      == kCCMIONoErr
00284     )
00285     {
00286         // Index of foam patch
00287         label foamPatchI = -1;
00288 
00289         // Read prostar id
00290 
00291         int prostarI = -1;
00292         if
00293         (
00294             CCMIOReadOpti(NULL, boundary, "ProstarRegionNumber", &prostarI)
00295          == kCCMIONoErr
00296         )
00297         {
00298             Pout<< "For region:" << regionI
00299                 << " found ProstarRegionNumber:" << prostarI << endl;
00300         }
00301         else
00302         {
00303             prostarI = regionI;
00304 
00305             Pout<< "For region:" << regionI
00306                 << "did not find ProstarRegionNumber entry. Assuming "
00307                 << prostarI << endl;
00308         }
00309 
00310 
00311         if (prostarToFoamPatch.found(prostarI))
00312         {
00313             foamPatchI = prostarToFoamPatch[prostarI];
00314 
00315             // Read boundary type
00316 
00317             int size;
00318             if
00319             (
00320                 CCMIOReadOptstr(NULL, boundary, "BoundaryType", &size, NULL)
00321              == kCCMIONoErr
00322             )
00323         {
00324             char* s = new char[size + 1];
00325             CCMIOReadOptstr(NULL, boundary, "BoundaryType", &size, s);
00326                 s[size] = '\0';
00327                 foamPatchTypes[foamPatchI] = string::validate<word>(string(s));
00328             delete [] s;
00329         }
00330 
00331 
00332             //foamPatchMap.append(prostarI);
00333 
00334 
00335             // Read boundary name:
00336             // - from BoundaryName field (Prostar)
00337             // - from 'Label' field (ccm+?)
00338             // - make up one from prostar id.
00339 
00340             if
00341             (
00342                 CCMIOReadOptstr(NULL, boundary, "BoundaryName", &size, NULL)
00343              == kCCMIONoErr
00344             )
00345         {
00346             char* name = new char[size + 1];
00347             CCMIOReadOptstr(NULL, boundary, "BoundaryName", &size, name);
00348                 name[size] = '\0';
00349                 foamPatchNames[foamPatchI] = string::validate<word>(string(name));
00350             delete [] name;
00351         }
00352             else if
00353             (
00354                 CCMIOReadOptstr(NULL, boundary, "Label", &size, NULL)
00355              == kCCMIONoErr
00356             )
00357         {
00358             char* name = new char[size + 1];
00359             CCMIOReadOptstr(NULL, boundary, "Label", &size, name);
00360                 name[size] = '\0';
00361                 foamPatchNames[foamPatchI] = string::validate<word>(string(name));
00362             delete [] name;
00363         }
00364             else
00365             {
00366                 foamPatchNames[foamPatchI] = 
00367                     foamPatchTypes[foamPatchI]
00368                   + Foam::name(foamPatchI);
00369                 Pout<< "Made up name:" << foamPatchNames[foamPatchI]
00370                     << endl;
00371             }
00372 
00373             Pout<< "Read patch:" << foamPatchI
00374                 << " name:" << foamPatchNames[foamPatchI]
00375                 << " foamPatchTypes:" << foamPatchTypes[foamPatchI]
00376                 << endl;
00377         }
00378 
00379         regionI++;
00380     }
00381 }
00382 
00383 
00384 void ReadCells
00385 (
00386     CCMIOError &err,
00387     CCMIOID& topology,
00388     labelList& foamCellMap,
00389     labelList& foamCellType,
00390     Map<label>& prostarToFoamPatch,
00391     DynamicList<label>& foamPatchSizes,
00392     DynamicList<label>& foamPatchStarts,
00393     labelList& foamFaceMap,
00394     labelList& foamOwner,
00395     labelList& foamNeighbour,
00396     faceList& foamFaces
00397 )
00398 {
00399     // Read the cells.
00400     // ~~~~~~~~~~~~~~~
00401 
00402     //  Store cell IDs so that we know what cells are in
00403     // this post data.
00404     CCMIOID id;
00405     CCMIOGetEntity(&err, topology, kCCMIOCells, 0, &id);
00406     CCMIOSize nCells;
00407     CCMIOEntitySize(&err, id, &nCells, NULL);
00408 
00409     std::vector<int> mapData(nCells);
00410     std::vector<int> cellType(nCells);
00411 
00412     CCMIOID mapID;
00413     CCMIOReadCells(&err, id, &mapID, &cellType[0], 0, nCells);
00414     CCMIOReadMap(&err, mapID, &mapData[0], 0, nCells);
00415     CheckError(err, "Error reading cells");
00416 
00417     foamCellMap.setSize(nCells);
00418     foamCellType.setSize(nCells);
00419     forAll(foamCellMap, i)
00420     {
00421         foamCellMap[i] = mapData[i];
00422         foamCellType[i] = cellType[i];
00423     }
00424 
00425 
00426     // Read the internal faces.
00427     // ~~~~~~~~~~~~~~~~~~~~~~~~
00428 
00429     CCMIOGetEntity(&err, topology, kCCMIOInternalFaces, 0, &id);
00430     CCMIOSize nInternalFaces;
00431     CCMIOEntitySize(&err, id, &nInternalFaces, NULL);
00432     Pout<< "nInternalFaces:" << nInternalFaces << endl;
00433 
00434     // Determine patch sizes before reading internal faces
00435     label foamNFaces = nInternalFaces;
00436     int index = 0;
00437     while
00438     (
00439         CCMIONextEntity(NULL, topology, kCCMIOBoundaryFaces, &index, &id)
00440      == kCCMIONoErr
00441     )
00442     {
00443     CCMIOSize size;
00444     CCMIOEntitySize(&err, id, &size, NULL);
00445 
00446         Pout<< "Read kCCMIOBoundaryFaces entry with " << size
00447             << " faces." << endl;
00448 
00449         foamPatchStarts.append(foamNFaces);
00450         foamPatchSizes.append(size);
00451         foamNFaces += size;
00452     }
00453     foamPatchStarts.shrink();
00454     foamPatchSizes.shrink();
00455 
00456     Pout<< "patchSizes:" << foamPatchSizes << endl;
00457     Pout<< "patchStarts:" << foamPatchStarts << endl;
00458     Pout<< "nFaces:" << foamNFaces << endl;
00459 
00460     mapData.resize(nInternalFaces);
00461     CCMIOGetEntity(&err, topology, kCCMIOInternalFaces, 0, &id);
00462     CCMIOSize size;
00463     CCMIOReadFaces(&err, id, kCCMIOInternalFaces, NULL, &size, NULL,
00464            kCCMIOStart, kCCMIOEnd);
00465     std::vector<int> faces(size);
00466     CCMIOReadFaces(&err, id, kCCMIOInternalFaces, &mapID, NULL, &faces[0],
00467            kCCMIOStart, kCCMIOEnd);
00468     std::vector<int> faceCells(2*nInternalFaces);
00469     CCMIOReadFaceCells(&err, id, kCCMIOInternalFaces, &faceCells[0],
00470                kCCMIOStart, kCCMIOEnd);
00471     CCMIOReadMap(&err, mapID, &mapData[0], kCCMIOStart, kCCMIOEnd);
00472     CheckError(err, "Error reading internal faces");
00473 
00474     // Copy into Foam lists
00475     foamFaceMap.setSize(foamNFaces);
00476     foamFaces.setSize(foamNFaces);
00477     foamOwner.setSize(foamNFaces);
00478     foamNeighbour.setSize(foamNFaces);
00479 
00480     unsigned int pos = 0;
00481 
00482     for (unsigned int faceI = 0; faceI < nInternalFaces; faceI++)
00483     {
00484         foamFaceMap[faceI] = mapData[faceI];
00485         foamOwner[faceI] = faceCells[2*faceI];
00486         foamNeighbour[faceI] = faceCells[2*faceI+1];
00487         face& f = foamFaces[faceI];
00488 
00489         f.setSize(faces[pos++]);
00490         forAll(f, fp)
00491         {
00492             f[fp] = faces[pos++];
00493         }
00494     }
00495 
00496     // Read the boundary faces.
00497     // ~~~~~~~~~~~~~~~~~~~~~~~~
00498 
00499     index = 0;
00500     label regionI = 0;
00501     while
00502     (
00503         CCMIONextEntity(NULL, topology, kCCMIOBoundaryFaces, &index, &id)
00504      == kCCMIONoErr
00505     )
00506     {
00507         CCMIOSize nFaces;
00508     CCMIOEntitySize(&err, id, &nFaces, NULL);
00509 
00510     mapData.resize(nFaces);
00511     faceCells.resize(nFaces);
00512     CCMIOReadFaces(&err, id, kCCMIOBoundaryFaces, NULL, &size, NULL,
00513                kCCMIOStart, kCCMIOEnd);
00514     faces.resize(size);
00515     CCMIOReadFaces(&err, id, kCCMIOBoundaryFaces, &mapID, NULL, &faces[0],
00516                kCCMIOStart, kCCMIOEnd);
00517     CCMIOReadFaceCells(&err, id, kCCMIOBoundaryFaces, &faceCells[0],
00518                kCCMIOStart, kCCMIOEnd);
00519     CCMIOReadMap(&err, mapID, &mapData[0], kCCMIOStart, kCCMIOEnd);
00520     CheckError(err, "Error reading boundary faces");
00521 
00522         // Read prostar id
00523         int prostarI;
00524         if
00525         (
00526             CCMIOReadOpti(NULL, id, "ProstarRegionNumber", &prostarI)
00527          == kCCMIONoErr
00528         )
00529         {
00530             Pout<< "For region:" << regionI
00531                 << " found ProstarRegionNumber:" << prostarI << endl;
00532         }
00533         else
00534         {
00535             prostarI = regionI;
00536 
00537             Pout<< "For region:" << regionI
00538                 << " did not find ProstarRegionNumber entry. Assuming "
00539                 << prostarI << endl;
00540         }
00541         prostarToFoamPatch.insert(prostarI, regionI);
00542 
00543 
00544         Pout<< "region:" << regionI
00545             << " ProstarRegionNumber:" << prostarI
00546             << " foamPatchStart:"
00547             << foamPatchStarts[regionI]
00548             << " size:"
00549             << foamPatchSizes[regionI]
00550             << endl;
00551 
00552         // Copy into Foam list.
00553         unsigned int pos = 0;
00554 
00555         for (unsigned int i = 0; i < nFaces; i++)
00556         {
00557             label foamFaceI = foamPatchStarts[regionI] + i;
00558 
00559             foamFaceMap[foamFaceI] = mapData[i];
00560             foamOwner[foamFaceI] = faceCells[i];
00561             foamNeighbour[foamFaceI] = -1;
00562             face& f = foamFaces[foamFaceI];
00563 
00564             f.setSize(faces[pos++]);
00565             forAll(f, fp)
00566             {
00567                 f[fp] = faces[pos++];
00568             }
00569         }
00570         Pout<< endl;
00571 
00572         regionI++;
00573     }
00574 }
00575 
00576 
00577 // Main program:
00578 
00579 int main(int argc, char *argv[])
00580 {
00581     argList::noParallel();
00582     argList::validArgs.append("ccm26 file");
00583 
00584 #   include "setRootCase.H"
00585 #   include "createTime.H"
00586 
00587 
00588     // Foam mesh data
00589     // ~~~~~~~~~~~~~~
00590 
00591     // Coordinates
00592     pointField foamPoints;
00593     labelList foamPointMap;
00594 
00595     // Cell type
00596     labelList foamCellType;
00597     labelList foamCellMap;
00598 
00599     // Patching info
00600     Map<label> prostarToFoamPatch;
00601     DynamicList<label> foamPatchSizes;
00602     DynamicList<label> foamPatchStarts;
00603     // Face connectivity
00604     labelList foamFaceMap;
00605     labelList foamOwner;
00606     labelList foamNeighbour;
00607     faceList foamFaces;
00608 
00609     // Celltypes (not the names of the zones; just the material type)
00610     // and patch type names
00611     Map<word> foamCellTypeNames;
00612     wordList foamPatchTypes;
00613     wordList foamPatchNames;
00614 
00615     {
00616         fileName ccmFile(args.additionalArgs()[0]);
00617 
00618         if (!isFile(ccmFile))
00619         {
00620             FatalErrorIn(args.executable())
00621                 << "Cannot read file " << ccmFile
00622                 << exit(FatalError);
00623         }
00624 
00625         word ccmExt = ccmFile.ext();
00626 
00627         if (ccmExt != "ccm" && ccmExt != "ccmg")
00628         {
00629             FatalErrorIn(args.executable())
00630                 << "Illegal extension " << ccmExt << " for file " << ccmFile
00631                 << nl << "Allowed extensions are '.ccm', '.ccmg'"
00632                 << exit(FatalError);
00633         }
00634 
00635         // Open the file.  Because we did not initialize 'err' we need to pass
00636         // in NULL (which always means kCCMIONoErr) and then assign the return
00637         // value to 'err'.).
00638         CCMIOID root;
00639         CCMIOError err = CCMIOOpenFile(NULL, ccmFile.c_str(), kCCMIORead, &root);
00640 
00641         // We are going to assume that we have a state with a known name.
00642         // We could instead use CCMIONextEntity() to walk through all the
00643         // states in the file and present the list to the user for selection.
00644         CCMIOID state;
00645         int stateI = 0;
00646         CCMIONextEntity(&err, root, kCCMIOState, &stateI, &state);
00647         CheckError(err, "Error opening state");
00648 
00649         unsigned int size;
00650         CCMIOEntityDescription(&err, state, &size, NULL);
00651         char *desc = new char[size + 1];
00652         CCMIOEntityDescription(&err, state, NULL, desc);
00653         Pout<< "Reading state '" << kDefaultState << "' (" << desc << ")"
00654             << endl;
00655         delete [] desc;
00656 
00657         // Find the first processor (i has previously been initialized to 0) and
00658         // read the mesh and solution information.
00659         int i = 0;
00660         CCMIOID processor;
00661         CCMIONextEntity(&err, state, kCCMIOProcessor, &i, &processor);
00662         CCMIOID solution, vertices, topology;
00663         CCMIOReadProcessor
00664         (
00665             &err,
00666             processor,
00667             &vertices,
00668             &topology,
00669             NULL,
00670             &solution
00671         );
00672 
00673         if (err != kCCMIONoErr)
00674         {
00675         // Maybe no solution;  try again
00676         err = kCCMIONoErr;
00677         CCMIOReadProcessor
00678             (
00679                 &err,
00680                 processor,
00681                 &vertices,
00682                 &topology,
00683                 NULL,
00684                 NULL
00685             );
00686         if (err != kCCMIONoErr)
00687         {
00688             FatalErrorIn(args.executable())
00689                     << "Could not read the file."
00690                     << exit(FatalError);
00691         }
00692         }
00693 
00694         ReadVertices(err, vertices, foamPointMap, foamPoints);
00695 
00696         Pout<< "nPoints:" << foamPoints.size() << endl
00697             << "bounding box:" << boundBox(foamPoints) << endl
00698             << endl;
00699 
00700         ReadCells
00701         (
00702             err,
00703             topology,
00704             foamCellMap,
00705             foamCellType,
00706             prostarToFoamPatch,
00707             foamPatchSizes,
00708             foamPatchStarts,
00709             foamFaceMap,
00710             foamOwner,
00711             foamNeighbour,
00712             foamFaces
00713         );
00714 
00715         Pout<< "nCells:" << foamCellMap.size() << endl
00716             << "nFaces:" << foamOwner.size() << endl
00717             << "nPatches:" << foamPatchStarts.size() << endl
00718             << "nInternalFaces:" << foamPatchStarts[0] << endl
00719             << endl;
00720 
00721         // Create some default patch names/types. These will be overwritten
00722         // by any problem desciption (if it is there)
00723         foamPatchTypes.setSize(foamPatchStarts.size());
00724         foamPatchNames.setSize(foamPatchStarts.size());
00725 
00726         forAll(foamPatchNames, i)
00727         {
00728             foamPatchNames[i] = word("patch") + Foam::name(i);
00729             foamPatchTypes[i] = "patch";
00730         }
00731 
00732         // Get problem description
00733 
00734         CCMIOID problem;
00735         int problemI = 0;
00736         CCMIONextEntity
00737         (
00738             &err,
00739             root,
00740             kCCMIOProblemDescription,
00741             &problemI,
00742             &problem
00743         );
00744         CheckError(err, "Error stepping to first problem description");
00745 
00746         if (CCMIOIsValidEntity(problem))   // if we have a problem description
00747         {
00748             ReadProblem
00749             (
00750                 err,
00751                 problem,
00752                 prostarToFoamPatch,
00753 
00754                 foamCellTypeNames,
00755                 foamPatchTypes,
00756                 foamPatchNames
00757             );
00758         }
00759 
00760 
00761         CCMIOCloseFile(&err, vertices);
00762         CCMIOCloseFile(&err, topology);
00763         CCMIOCloseFile(&err, solution);
00764         CCMIOCloseFile(&err, root);
00765     }
00766 
00767 
00768     Pout<< "foamPatchNames:" << foamPatchNames << endl;
00769 
00770 
00771     Pout<< "foamOwner : min:" << min(foamOwner)
00772         << " max:" << max(foamOwner)
00773         << nl
00774         << "foamNeighbour : min:" << min(foamNeighbour)
00775         << " max:" << max(foamNeighbour)
00776         << nl
00777         << "foamCellType : min:" << min(foamCellType)
00778         << " max:" << max(foamCellType)
00779         << nl << endl;
00780 
00781 
00782 
00783     // We now have extracted all info from CCMIO:
00784     // - coordinates (points)
00785     // - face to point addressing (faces)
00786     // - face to cell addressing (owner, neighbour)
00787     // - cell based data (cellId)
00788 
00789 
00790     // Renumber vertex labels to Foam point labels
00791     {
00792         label maxCCMPointI = max(foamPointMap);
00793         labelList toFoamPoints(invert(maxCCMPointI+1, foamPointMap));
00794 
00795         forAll(foamFaces, faceI)
00796         {
00797             inplaceRenumber(toFoamPoints, foamFaces[faceI]);
00798         }
00799     }
00800 
00801     // Renumber cell labels
00802     {
00803         label maxCCMCellI = max(foamCellMap);
00804         labelList toFoamCells(invert(maxCCMCellI+1, foamCellMap));
00805 
00806         inplaceRenumber(toFoamCells, foamOwner);
00807         inplaceRenumber(toFoamCells, foamNeighbour);
00808     }
00809 
00810 
00811     //
00812     // Basic mesh info complete. Now convert to Foam convention:
00813     // - owner < neighbour
00814     // - face vertices such that normal points away from owner
00815     // - order faces: upper-triangular for internal faces; boundary faces after
00816     //   internal faces
00817     //
00818 
00819     // Set owner/neighbour so owner < neighbour
00820     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00821 
00822     forAll(foamNeighbour, faceI)
00823     {
00824         label nbr = foamNeighbour[faceI];
00825         label own = foamOwner[faceI];
00826 
00827         if (nbr >= foamCellType.size() || own >= foamCellType.size())
00828         {
00829             FatalErrorIn(args.executable())
00830                 << "face:" << faceI
00831                 << " nbr:" << nbr
00832                 << " own:" << own
00833                 << " nCells:" << foamCellType.size()
00834                 << exit(FatalError);
00835         }
00836 
00837         if (nbr >= 0)
00838         {
00839             if (nbr < own)
00840             {
00841                 foamOwner[faceI] = foamNeighbour[faceI];
00842                 foamNeighbour[faceI] = own;
00843                 foamFaces[faceI] = foamFaces[faceI].reverseFace();
00844             }
00845         }
00846 
00847 
00848         // And check the face
00849         const face& f = foamFaces[faceI];
00850 
00851         forAll(f, fp)
00852         {
00853             if (f[fp] < 0 || f[fp] >= foamPoints.size())
00854             {
00855                 FatalErrorIn(args.executable()) << "face:" << faceI
00856                     << " f:" << f << abort(FatalError);
00857             }
00858         }
00859     }
00860 
00861 
00862     // Do upper-triangular ordering
00863     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00864 
00865     {
00866         // Create cells (inverse of face-to-cell addressing)
00867         cellList foamCells;
00868         primitiveMesh::calcCells
00869         (
00870             foamCells,
00871             foamOwner,
00872             foamNeighbour,
00873             foamCellType.size()
00874         );
00875 
00876         // Determine face order for upper-triangular ordering
00877         labelList oldToNew
00878         (
00879             getInternalFaceOrder
00880             (
00881                 foamCells,
00882                 foamOwner,
00883                 foamNeighbour
00884             )
00885         );
00886 
00887         // Reorder faces accordingly
00888         forAll(foamCells, cellI)
00889         {
00890             inplaceRenumber(oldToNew, foamCells[cellI]);
00891         }
00892 
00893         // Reorder faces.
00894         inplaceReorder(oldToNew, foamFaces);
00895         inplaceReorder(oldToNew, foamOwner);
00896         inplaceReorder(oldToNew, foamNeighbour);
00897     }
00898 
00899 
00900     // Construct fvMesh
00901     // ~~~~~~~~~~~~~~~~
00902 
00903     fvMesh mesh
00904     (
00905         IOobject
00906         (
00907             fvMesh::defaultRegion,
00908             runTime.constant(),
00909             runTime
00910         ),
00911         foamPoints,
00912         foamFaces,
00913         foamOwner,
00914         foamNeighbour
00915     );
00916 
00917     // Create patches. Use patch types to determine what Foam types to generate.
00918     List<polyPatch*> newPatches(foamPatchNames.size());
00919 
00920     label meshFaceI = foamPatchStarts[0];
00921 
00922     forAll(newPatches, patchI)
00923     {
00924         const word& patchName = foamPatchNames[patchI];
00925         const word& patchType = foamPatchTypes[patchI];
00926 
00927         Pout<< "Patch:" << patchName << " start at:" << meshFaceI
00928             << " size:" << foamPatchSizes[patchI]
00929             << " end at:" << meshFaceI+foamPatchSizes[patchI]
00930             << endl;
00931 
00932         if (patchType == "wall")
00933         {
00934             newPatches[patchI] =
00935                 new wallPolyPatch
00936                 (
00937                     patchName,
00938                     foamPatchSizes[patchI],
00939                     meshFaceI,
00940                     patchI,
00941                     mesh.boundaryMesh()
00942                 );
00943         }
00944         else if (patchType == "symmetryplane")
00945         {
00946             newPatches[patchI] =
00947                 new symmetryPolyPatch
00948                 (
00949                     patchName,
00950                     foamPatchSizes[patchI],
00951                     meshFaceI,
00952                     patchI,
00953                     mesh.boundaryMesh()
00954                 );
00955         }
00956         else if (patchType == "empty")
00957         {
00958             // Note: not ccm name, introduced by us above.
00959             newPatches[patchI] =
00960                 new emptyPolyPatch
00961                 (
00962                     patchName,
00963                     foamPatchSizes[patchI],
00964                     meshFaceI,
00965                     patchI,
00966                     mesh.boundaryMesh()
00967                 );
00968         }
00969         else
00970         {
00971             // All other ccm types become straight polyPatch:
00972             // 'inlet', 'outlet', 'pressured'.
00973             newPatches[patchI] =
00974                 new polyPatch
00975                 (
00976                     patchName,
00977                     foamPatchSizes[patchI],
00978                     meshFaceI,
00979                     patchI,
00980                     mesh.boundaryMesh()
00981                 );
00982         }
00983 
00984         meshFaceI += foamPatchSizes[patchI];
00985     }
00986 
00987     if (meshFaceI != foamOwner.size())
00988     {
00989         FatalErrorIn(args.executable())
00990             << "meshFaceI:" << meshFaceI
00991             << " nFaces:" << foamOwner.size()
00992             << abort(FatalError);
00993     }
00994     mesh.addFvPatches(newPatches);
00995 
00996 
00997 
00998     // Construct sets and zones from types
00999     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01000 
01001     label maxType = max(foamCellType);
01002     label minType = min(foamCellType);
01003 
01004     if (maxType > minType)
01005     {
01006         // From foamCellType physical region to Foam cellZone
01007         Map<label> typeToZone;
01008         // Storage for cell zones.
01009         List<DynamicList<label> > zoneCells(0);
01010 
01011         forAll(foamCellType, cellI)
01012         {
01013             storeCellInZone
01014             (
01015                 cellI,
01016                 foamCellType[cellI],
01017                 typeToZone,
01018                 zoneCells
01019             );
01020         }
01021 
01022         mesh.cellZones().clear();
01023         mesh.cellZones().setSize(typeToZone.size());
01024 
01025         label nValidCellZones = 0;
01026 
01027         forAllConstIter(Map<label>, typeToZone, iter)
01028         {
01029             label type = iter.key();
01030             label zoneI = iter();
01031 
01032             zoneCells[zoneI].shrink();
01033 
01034             word zoneName = "cellZone_" + name(type);
01035 
01036             Info<< "Writing zone " << type
01037                 << " size " << zoneCells[zoneI].size()
01038                 << " to cellZone "
01039                 << zoneName << " and cellSet " << zoneName
01040                 << endl;
01041 
01042             cellSet cset(mesh, zoneName, zoneCells[zoneI]);
01043             cset.write();
01044 
01045             mesh.cellZones().set
01046             (
01047                 nValidCellZones,
01048                 new cellZone
01049                 (
01050                     zoneName,
01051                     zoneCells[zoneI],
01052                     nValidCellZones,
01053                     mesh.cellZones()
01054                 )
01055             );
01056             nValidCellZones++;
01057         }
01058         mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
01059     }
01060 
01061 
01062     Info<< "Writing mesh to " << mesh.objectRegistry::objectPath()
01063         << "..." << nl << endl;
01064 
01065 
01066     // Construct field with calculated bc to hold Star cell Id.
01067     volScalarField cellIdField
01068     (
01069         IOobject
01070         (
01071             "cellId",
01072             runTime.timeName(),
01073             mesh,
01074             IOobject::NO_READ,
01075             IOobject::AUTO_WRITE
01076         ),
01077         mesh,
01078         dimensionedScalar("cellId", dimless, 0.0)
01079     );
01080 
01081     forAll(foamCellMap, cellI)
01082     {
01083         cellIdField[cellI] = foamCellMap[cellI];
01084     }
01085 
01086     // Construct field with calculated bc to hold cell type.
01087     volScalarField cellTypeField
01088     (
01089         IOobject
01090         (
01091             "cellType",
01092             runTime.timeName(),
01093             mesh,
01094             IOobject::NO_READ,
01095             IOobject::AUTO_WRITE
01096         ),
01097         mesh,
01098         dimensionedScalar("cellType", dimless, 0.0)
01099     );
01100 
01101     forAll(foamCellType, cellI)
01102     {
01103         cellTypeField[cellI] = foamCellType[cellI];
01104     }
01105 
01106     Info<< "Writing cellIds as volScalarField to " << cellIdField.objectPath()
01107         << "..." << nl << endl;
01108     mesh.write();
01109 
01110     Info<< "End\n" << endl;
01111 
01112     return 0;
01113 }
01114 
01115 
01116 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd