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

cfx4ToFoam.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 Application
00026     cfx4ToFoam
00027 
00028 Description
00029     Converts a CFX 4 mesh to FOAM format
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "argList.H"
00034 #include "Time.H"
00035 #include "IFstream.H"
00036 #include "hexBlock.H"
00037 #include "polyMesh.H"
00038 #include "wallPolyPatch.H"
00039 #include "symmetryPolyPatch.H"
00040 #include "preservePatchTypes.H"
00041 #include "cellShape.H"
00042 #include "cellModeller.H"
00043 
00044 using namespace Foam;
00045 
00046 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00047 // Main program:
00048 
00049 int main(int argc, char *argv[])
00050 {
00051     argList::noParallel();
00052     argList::validArgs.append("CFX geom file");
00053     argList::validOptions.insert("scale", "scale factor");
00054 
00055     argList args(argc, argv);
00056 
00057     if (!args.check())
00058     {
00059          FatalError.exit();
00060     }
00061 
00062     scalar scaleFactor = 1.0;
00063     args.optionReadIfPresent("scale", scaleFactor);
00064 
00065 #   include "createTime.H"
00066 
00067     IFstream cfxFile(args.additionalArgs()[0]);
00068 
00069     // Read the cfx information using a fixed format reader.
00070     // Comments in the file are in C++ style, so the stream parser will remove
00071     // them with no intervention
00072     label nblock, npatch, nglue, nelem, npoint;
00073 
00074     cfxFile >> nblock >> npatch >> nglue >> nelem >> npoint;
00075 
00076     Info << "Reading blocks" << endl;
00077 
00078     PtrList<hexBlock> blocks(nblock);
00079 
00080     {
00081         word blockName;
00082         label nx, ny, nz;
00083 
00084         forAll (blocks, blockI)
00085         {
00086             cfxFile >> blockName;
00087             cfxFile >> nx >> ny >> nz;
00088 
00089             blocks.set(blockI, new hexBlock(nx, ny, nz));
00090         }
00091     }
00092 
00093     Info << "Reading patch definitions" << endl;
00094 
00095     wordList cfxPatchTypes(npatch);
00096     wordList cfxPatchNames(npatch);
00097     labelList patchMasterBlocks(npatch);
00098     labelList patchDirections(npatch);
00099     labelListList patchRanges(npatch);
00100 
00101     {
00102         label no, blkNo, patchLabel;
00103 
00104         forAll (cfxPatchTypes, patchI)
00105         {
00106             // Grab patch type and name
00107             cfxFile >> cfxPatchTypes[patchI] >> cfxPatchNames[patchI] >> no;
00108 
00109             // Grab patch range
00110             patchRanges[patchI].setSize(6);
00111             labelList& curRange = patchRanges[patchI];
00112 
00113             forAll (curRange, rI)
00114             {
00115                 cfxFile >> curRange[rI];
00116             }
00117 
00118             // Grab patch direction and master block ID
00119             // Note: direc is the direction, from the cfx manual
00120             // 0 = solid (3-D patch),
00121             // 1 = high i, 2 = high j, 3 = high k
00122             // 4 = low i, 5 = low j, 6 = low k
00123             cfxFile >> patchDirections[patchI] >> blkNo >> patchLabel;
00124 
00125             patchMasterBlocks[patchI] = blkNo - 1;
00126         }
00127     }
00128 
00129     Info << "Reading block glueing information" << endl;
00130 
00131     labelList glueMasterPatches(nglue, -1);
00132     labelList glueSlavePatches(nglue, -1);
00133 
00134     {
00135         label masterPatch, slavePatch;
00136         label dirIndex1, dirIndex2, dirIndex3, joinNumber;
00137 
00138         for (label glueI = 0; glueI < nglue; glueI++)
00139         {
00140             cfxFile >> masterPatch >> slavePatch;
00141             cfxFile >> dirIndex1 >> dirIndex2 >> dirIndex3 >> joinNumber;
00142 
00143             glueMasterPatches[glueI] = masterPatch - 1;
00144             glueSlavePatches[glueI] = slavePatch - 1;
00145         }
00146     }
00147 
00148     Info << "Reading block points" << endl;
00149 
00150     forAll (blocks, blockI)
00151     {
00152         Info << "block " << blockI << " is a ";
00153         blocks[blockI].readPoints(cfxFile);
00154     }
00155 
00156     Info << "Calculating block offsets" << endl;
00157 
00158     labelList blockOffsets(nblock, -1);
00159 
00160     blockOffsets[0] = 0;
00161 
00162     label nMeshPoints = blocks[0].nBlockPoints();
00163     label nMeshCells = blocks[0].nBlockCells();
00164 
00165     for (label blockI = 1; blockI < nblock; blockI++)
00166     {
00167         nMeshPoints += blocks[blockI].nBlockPoints();
00168         nMeshCells +=  blocks[blockI].nBlockCells();
00169 
00170         blockOffsets[blockI] =
00171             blockOffsets[blockI - 1]
00172           + blocks[blockI - 1].nBlockPoints();
00173     }
00174 
00175     Info << "Assembling patches" << endl;
00176 
00177     faceListList rawPatches(npatch);
00178 
00179     forAll (rawPatches, patchI)
00180     {
00181         const word& patchType = cfxPatchTypes[patchI];
00182 
00183         // reject volume patches
00184         if
00185         (
00186             patchType == "POROUS" || patchType == "SOLID"
00187          || patchType == "SOLCON" || patchType == "USER3D"
00188         )
00189         {
00190             patchMasterBlocks[patchI] = -1;
00191             rawPatches[patchI].setSize(0);
00192         }
00193         else
00194         {
00195             // read and create a 2-D patch
00196             rawPatches[patchI] =
00197                 blocks[patchMasterBlocks[patchI]].patchFaces
00198                 (
00199                     patchDirections[patchI],
00200                     patchRanges[patchI]
00201                 );
00202 
00203         }
00204     }
00205 
00206     Info << "Merging points ";
00207 
00208     labelList pointMergeList(nMeshPoints, -1);
00209 
00210     // In order to ensure robust merging, it is necessary to traverse
00211     // the patch glueing list until the pointMergeList stops changing.
00212     // 
00213 
00214     // For efficiency, create merge pairs in the first pass
00215     labelListListList glueMergePairs(glueMasterPatches.size());
00216 
00217     forAll (glueMasterPatches, glueI)
00218     {
00219         const label masterPatch = glueMasterPatches[glueI];
00220         const label slavePatch = glueSlavePatches[glueI];
00221 
00222         const label blockPlabel = patchMasterBlocks[masterPatch];
00223         const label blockNlabel = patchMasterBlocks[slavePatch];
00224 
00225         const pointField& blockPpoints = blocks[blockPlabel].points();
00226         const pointField& blockNpoints = blocks[blockNlabel].points();
00227 
00228         const faceList& blockPFaces = rawPatches[masterPatch];
00229         const faceList& blockNFaces = rawPatches[slavePatch];
00230 
00231         labelListList& curPairs = glueMergePairs[glueI];
00232         curPairs.setSize(blockPFaces.size());
00233 
00234         if (blockPFaces.size() != blockNFaces.size())
00235         {
00236             FatalErrorIn(args.executable())
00237                 << "Inconsistent number of faces for glue pair "
00238                 << glueI << " between blocks " << blockPlabel + 1
00239                 << " and " << blockNlabel + 1
00240                 << abort(FatalError);
00241         }
00242 
00243         // Calculate sqr of the merge tolerance as 1/10th of the min
00244         // sqr point to point distance on the block face.  This is an
00245         // N^2 algorithm, sorry but I cannot quickly come up with
00246         // something better.
00247 
00248         scalar sqrMergeTol = GREAT;
00249 
00250         forAll (blockPFaces, blockPFaceLabel)
00251         {
00252             const labelList& blockPFacePoints =
00253                 blockPFaces[blockPFaceLabel];
00254 
00255             forAll (blockPFacePoints, blockPFacePointI)
00256             {
00257                 forAll (blockPFacePoints, blockPFacePointI2)
00258                 {
00259                     if (blockPFacePointI != blockPFacePointI2)
00260                     {
00261                         sqrMergeTol =
00262                             min
00263                             (
00264                                 sqrMergeTol,
00265                                 magSqr
00266                                 (
00267                                     blockPpoints
00268                                         [blockPFacePoints[blockPFacePointI]]
00269                                   - blockPpoints
00270                                         [blockPFacePoints[blockPFacePointI2]]
00271                                 )
00272                             );
00273                     }
00274                 }
00275             }
00276         }
00277 
00278         sqrMergeTol /= 10.0;
00279 
00280         register bool found = false;
00281 
00282         // N-squared point search over all points of all faces of
00283         // master block over all point of all faces of slave block
00284         forAll (blockPFaces, blockPFaceLabel)
00285         {
00286             const labelList& blockPFacePoints =
00287                 blockPFaces[blockPFaceLabel];
00288 
00289             labelList& cp = curPairs[blockPFaceLabel];
00290             cp.setSize(blockPFacePoints.size());
00291 
00292         forAll (blockPFacePoints, blockPFacePointI)
00293         {
00294             found = false;
00295 
00296             forAll (blockNFaces, blockNFaceLabel)
00297             {
00298                 const labelList& blockNFacePoints =
00299                     blockNFaces[blockNFaceLabel];
00300 
00301             forAll (blockNFacePoints, blockNFacePointI)
00302             {
00303                 if
00304                 (
00305                     magSqr
00306                     (
00307                         blockPpoints
00308                             [blockPFacePoints[blockPFacePointI]]
00309                       - blockNpoints
00310                             [blockNFacePoints[blockNFacePointI]]
00311                     )
00312                   < sqrMergeTol
00313                 )
00314                 {
00315                     // Found a new pair
00316                     found = true;
00317 
00318                     cp[blockPFacePointI] =
00319                         blockNFacePoints[blockNFacePointI];
00320 
00321                     label PpointLabel =
00322                         blockPFacePoints[blockPFacePointI]
00323                       + blockOffsets[blockPlabel];
00324 
00325                     label NpointLabel =
00326                         blockNFacePoints[blockNFacePointI]
00327                       + blockOffsets[blockNlabel];
00328 
00329                     label minPN = min(PpointLabel, NpointLabel);
00330 
00331                     if (pointMergeList[PpointLabel] != -1)
00332                     {
00333                         minPN = min(minPN, pointMergeList[PpointLabel]);
00334                     }
00335 
00336                     if (pointMergeList[NpointLabel] != -1)
00337                     {
00338                         minPN = min(minPN, pointMergeList[NpointLabel]);
00339                     }
00340 
00341                     pointMergeList[PpointLabel]
00342                   = pointMergeList[NpointLabel]
00343                   = minPN;
00344 
00345                     break;
00346                 }
00347             }
00348             if (found) break;
00349             }
00350         }
00351         }
00352     }
00353 
00354 
00355     register bool changedPointMerge = false;
00356     label nPasses = 0;
00357 
00358     do
00359     {
00360         changedPointMerge = false;
00361         nPasses++;
00362 
00363         forAll (glueMasterPatches, glueI)
00364         {
00365             const label masterPatch = glueMasterPatches[glueI];
00366             const label slavePatch = glueSlavePatches[glueI];
00367 
00368             const label blockPlabel = patchMasterBlocks[masterPatch];
00369             const label blockNlabel = patchMasterBlocks[slavePatch];
00370 
00371             const faceList& blockPFaces = rawPatches[masterPatch];
00372 
00373             const labelListList& curPairs = glueMergePairs[glueI];
00374 
00375             forAll (blockPFaces, blockPFaceLabel)
00376             {
00377                 const labelList& blockPFacePoints =
00378                     blockPFaces[blockPFaceLabel];
00379 
00380                 const labelList& cp = curPairs[blockPFaceLabel];
00381 
00382                 forAll (cp, blockPFacePointI)
00383                 {
00384                     label PpointLabel =
00385                         blockPFacePoints[blockPFacePointI]
00386                       + blockOffsets[blockPlabel];
00387 
00388                     label NpointLabel =
00389                         cp[blockPFacePointI]
00390                       + blockOffsets[blockNlabel];
00391 
00392                     if
00393                     (
00394                         pointMergeList[PpointLabel]
00395                      != pointMergeList[NpointLabel]
00396                     )
00397                     {
00398                         changedPointMerge = true;
00399                             
00400                         pointMergeList[PpointLabel]
00401                       = pointMergeList[NpointLabel]
00402                       = min
00403                         (
00404                             pointMergeList[PpointLabel],
00405                             pointMergeList[NpointLabel]
00406                         );
00407                     }
00408                 }
00409             }
00410         }
00411         Info << "." << flush;
00412     }
00413     while (changedPointMerge && nPasses < 8);
00414     Info << endl;
00415 
00416     if (changedPointMerge == true)
00417     {
00418         FatalErrorIn(args.executable())
00419             << "Point merging failed after max number of passes."
00420             << abort(FatalError);
00421     }
00422 
00423 
00424     forAll (glueMasterPatches, glueI)
00425     {
00426         const label masterPatch = glueMasterPatches[glueI];
00427         const label slavePatch = glueSlavePatches[glueI];
00428 
00429         const label blockPlabel = patchMasterBlocks[masterPatch];
00430         const label blockNlabel = patchMasterBlocks[slavePatch];
00431 
00432         const faceList& blockPFaces = rawPatches[masterPatch];
00433         const faceList& blockNFaces = rawPatches[slavePatch];
00434 
00435 
00436         forAll (blockPFaces, blockPFaceLabel)
00437         {
00438             const labelList& blockPFacePoints
00439                 = blockPFaces[blockPFaceLabel];
00440 
00441             forAll (blockPFacePoints, blockPFacePointI)
00442             {
00443                 label PpointLabel =
00444                     blockPFacePoints[blockPFacePointI]
00445                   + blockOffsets[blockPlabel];
00446 
00447                 if (pointMergeList[PpointLabel] == -1)
00448                 {
00449                     FatalErrorIn(args.executable())
00450                         << "Unable to merge point " << blockPFacePointI
00451                         << " of face " << blockPFaceLabel
00452                         << " of block " << blockPlabel
00453                         << abort(FatalError);
00454                 }
00455             }
00456         }
00457 
00458         forAll (blockNFaces, blockNFaceLabel)
00459         {
00460             const labelList& blockNFacePoints
00461                 = blockNFaces[blockNFaceLabel];
00462 
00463             forAll (blockNFacePoints, blockNFacePointI)
00464             {
00465                 label NpointLabel =
00466                     blockNFacePoints[blockNFacePointI]
00467                   + blockOffsets[blockNlabel];
00468 
00469                 if (pointMergeList[NpointLabel] == -1)
00470                 {
00471                     FatalErrorIn(args.executable())
00472                         << "Unable to merge point " << blockNFacePointI
00473                         << " of face " << blockNFaceLabel
00474                         << " of block " << blockNlabel
00475                         << abort(FatalError);
00476                 }
00477             }
00478         }
00479     }
00480 
00481 
00482     // sort merge list to return new point label (in new shorter list)
00483     // given old point label
00484     label nNewPoints = 0;
00485 
00486     forAll (pointMergeList, pointLabel)
00487     {
00488         if (pointMergeList[pointLabel] > pointLabel)
00489         {
00490             FatalErrorIn(args.executable())
00491                 << "ouch" << abort(FatalError);
00492         }
00493 
00494         if
00495         (
00496             (pointMergeList[pointLabel] == -1)
00497          || pointMergeList[pointLabel] == pointLabel
00498         )
00499         {
00500             pointMergeList[pointLabel] = nNewPoints;
00501             nNewPoints++;
00502         }
00503         else
00504         {
00505             pointMergeList[pointLabel] =
00506                 pointMergeList[pointMergeList[pointLabel]];
00507         }
00508     }
00509 
00510     nMeshPoints = nNewPoints;
00511 
00512     Info << "Creating points" << endl;
00513 
00514     pointField points(nMeshPoints);
00515 
00516     forAll (blocks, blockI)
00517     {
00518         const pointField& blockPoints = blocks[blockI].points();
00519 
00520         forAll (blockPoints, blockPointLabel)
00521         {
00522             points
00523             [
00524                 pointMergeList
00525                 [
00526                     blockPointLabel
00527                   + blockOffsets[blockI]
00528                 ]
00529             ] = blockPoints[blockPointLabel];
00530         }
00531     }
00532 
00533     // Scale the points
00534     if (scaleFactor > 1.0 + SMALL || scaleFactor < 1.0 - SMALL)
00535     {
00536         points *= scaleFactor;
00537     }
00538 
00539     Info << "Creating cells" << endl;
00540 
00541     cellShapeList cellShapes(nMeshCells);
00542 
00543     const cellModel& hex = *(cellModeller::lookup("hex"));
00544 
00545     label nCreatedCells = 0;
00546 
00547     forAll (blocks, blockI)
00548     {
00549         labelListList curBlockCells = blocks[blockI].blockCells();
00550 
00551         forAll (curBlockCells, blockCellI)
00552         {
00553             labelList cellPoints(curBlockCells[blockCellI].size());
00554 
00555             forAll (cellPoints, pointI)
00556             {
00557                 cellPoints[pointI] =
00558                     pointMergeList
00559                     [
00560                         curBlockCells[blockCellI][pointI]
00561                       + blockOffsets[blockI]
00562                     ];
00563             }
00564 
00565             cellShapes[nCreatedCells] = cellShape(hex, cellPoints);
00566 
00567             nCreatedCells++;
00568         }
00569     }
00570 
00571     Info << "Creating boundary patches" << endl;
00572 
00573     faceListList boundary(npatch);
00574     wordList patchNames(npatch);
00575     wordList patchTypes(npatch);
00576     word defaultFacesName = "defaultFaces";
00577     word defaultFacesType = wallPolyPatch::typeName;
00578     wordList patchPhysicalTypes(npatch);
00579 
00580     label nCreatedPatches = 0;
00581 
00582     forAll (rawPatches, patchI)
00583     {
00584         if (rawPatches[patchI].size() && cfxPatchTypes[patchI] != "BLKBDY")
00585         {
00586             // Check if this name has been already created
00587             label existingPatch = -1;
00588 
00589             for (label oldPatchI = 0; oldPatchI < nCreatedPatches; oldPatchI++)
00590             {
00591                 if (patchNames[oldPatchI] == cfxPatchNames[patchI])
00592                 {
00593                     existingPatch = oldPatchI;
00594                     break;
00595                 }
00596             }
00597 
00598             const faceList& curRawPatch = rawPatches[patchI];
00599             label curBlock = patchMasterBlocks[patchI];
00600 
00601             if (existingPatch >= 0)
00602             {
00603                 Info << "CFX patch " << patchI
00604                     << ", of type " << cfxPatchTypes[patchI]
00605                     << ", name " << cfxPatchNames[patchI]
00606                     << " already exists as FOAM patch " << existingPatch
00607                     << ".  Adding faces." << endl;
00608 
00609                 faceList& renumberedPatch = boundary[existingPatch];
00610                 label oldSize = renumberedPatch.size();
00611                 renumberedPatch.setSize(oldSize + curRawPatch.size());
00612 
00613                 forAll (curRawPatch, faceI)
00614                 {
00615                     const face& oldFace = curRawPatch[faceI];
00616 
00617                     face& newFace = renumberedPatch[oldSize + faceI];
00618                     newFace.setSize(oldFace.size());
00619 
00620                     forAll (oldFace, pointI)
00621                     {
00622                         newFace[pointI] =
00623                             pointMergeList
00624                             [
00625                                 oldFace[pointI]
00626                               + blockOffsets[curBlock]
00627                             ];
00628                     }
00629                 }
00630             }
00631             else
00632             {
00633                 // Real patch to be created
00634                 faceList& renumberedPatch = boundary[nCreatedPatches];
00635                 renumberedPatch.setSize(curRawPatch.size());
00636 
00637                 forAll (curRawPatch, faceI)
00638                 {
00639                     const face& oldFace = curRawPatch[faceI];
00640 
00641                     face& newFace = renumberedPatch[faceI];
00642                     newFace.setSize(oldFace.size());
00643 
00644                     forAll (oldFace, pointI)
00645                     {
00646                         newFace[pointI] =
00647                             pointMergeList
00648                             [
00649                                 oldFace[pointI]
00650                               + blockOffsets[curBlock]
00651                             ];
00652                     }
00653                 }
00654 
00655                 Info << "CFX patch " << patchI
00656                     << ", of type " << cfxPatchTypes[patchI]
00657                     << ", name " << cfxPatchNames[patchI]
00658                     << " converted into FOAM patch " << nCreatedPatches
00659                     << " type ";
00660 
00661                 if (cfxPatchTypes[patchI] == "WALL")
00662                 {
00663                     Info << "wall." << endl;
00664 
00665                     patchTypes[nCreatedPatches] = wallPolyPatch::typeName;
00666                     patchNames[nCreatedPatches] = cfxPatchNames[patchI];
00667                     nCreatedPatches++;
00668                 }
00669                 else if (cfxPatchTypes[patchI] == "SYMMET")
00670                 {
00671                     Info << "symmetryPlane." << endl;
00672 
00673                     patchTypes[nCreatedPatches] = symmetryPolyPatch::typeName;
00674                     patchNames[nCreatedPatches] = cfxPatchNames[patchI];
00675                     nCreatedPatches++;
00676                 }
00677                 else if
00678                 (
00679                     cfxPatchTypes[patchI] == "INLET"
00680                  || cfxPatchTypes[patchI] == "OUTLET"
00681                  || cfxPatchTypes[patchI] == "PRESS"
00682                  || cfxPatchTypes[patchI] == "CNDBDY"
00683                  || cfxPatchTypes[patchI] == "USER2D"
00684                 )
00685                 {
00686                     Info << "generic." << endl;
00687 
00688                     patchTypes[nCreatedPatches] = polyPatch::typeName;
00689                     patchNames[nCreatedPatches] = cfxPatchNames[patchI];
00690                     nCreatedPatches++;
00691                 }
00692                 else
00693                 {
00694                     FatalErrorIn(args.executable())
00695                         << "Unrecognised CFX patch type "
00696                         << cfxPatchTypes[patchI]
00697                         << abort(FatalError);
00698                 }
00699             }
00700         }
00701     }
00702 
00703     boundary.setSize(nCreatedPatches);
00704     patchTypes.setSize(nCreatedPatches);
00705     patchNames.setSize(nCreatedPatches);
00706 
00707     preservePatchTypes
00708     (
00709         runTime,
00710         runTime.constant(),
00711         polyMesh::defaultRegion,
00712         patchNames,
00713         patchTypes,
00714         defaultFacesName,
00715         defaultFacesType,
00716         patchPhysicalTypes
00717     );
00718 
00719     polyMesh pShapeMesh
00720     (
00721         IOobject
00722         (
00723             polyMesh::defaultRegion,
00724             runTime.constant(),
00725             runTime
00726         ),
00727         xferMove(points),
00728         cellShapes,
00729         boundary,
00730         patchNames,
00731         patchTypes,
00732         defaultFacesName,
00733         defaultFacesType,
00734         patchPhysicalTypes
00735     );
00736 
00737     // Set the precision of the points data to 10
00738     IOstream::defaultPrecision(10);
00739 
00740     Info << "Writing polyMesh" << endl;
00741     pShapeMesh.write();
00742 
00743     Info << "End\n" << endl;
00744 
00745     return 0;
00746 }
00747 
00748 
00749 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd