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

reconstructParMesh.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     reconstructParMesh
00027 
00028 Description
00029     Reconstructs a mesh using geometric information only.
00030 
00031     Writes point/face/cell procAddressing so afterwards reconstructPar can be
00032     used to reconstruct fields.
00033 
00034     Note:
00035     - uses geometric matching tolerance (set with -mergeTol option)
00036 
00037     If the parallel case does not have correct procBoundaries use the
00038     -fullMatch option which will check all boundary faces (bit slower).
00039 
00040 \*---------------------------------------------------------------------------*/
00041 
00042 #include "argList.H"
00043 #include "Time.H"
00044 #include "IOobjectList.H"
00045 #include "labelIOList.H"
00046 #include "processorPolyPatch.H"
00047 #include "mapAddedPolyMesh.H"
00048 #include "polyMeshAdder.H"
00049 #include "faceCoupleInfo.H"
00050 #include "fvMeshAdder.H"
00051 #include "polyTopoChange.H"
00052 
00053 using namespace Foam;
00054 
00055 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00056 
00057 // Tolerance (as fraction of the bounding box). Needs to be fairly lax since
00058 // usually meshes get written with limited precision (6 digits)
00059 static const scalar defaultMergeTol = 1E-7;
00060 
00061 
00062 static void renumber
00063 (
00064     const labelList& map,
00065     labelList& elems
00066 )
00067 {
00068     forAll(elems, i)
00069     {
00070         if (elems[i] >= 0)
00071         {
00072             elems[i] = map[elems[i]];
00073         }
00074     }
00075 }
00076 
00077 
00078 // Determine which faces are coupled. Uses geometric merge distance.
00079 // Looks either at all boundaryFaces (fullMatch) or only at the
00080 // procBoundaries for procI. Assumes that masterMesh contains already merged
00081 // all the processors < procI.
00082 autoPtr<faceCoupleInfo> determineCoupledFaces
00083 (
00084     const bool fullMatch,
00085     const label procI,
00086     const polyMesh& masterMesh,
00087     const polyMesh& meshToAdd,
00088     const scalar mergeDist
00089 )
00090 {
00091     if (fullMatch || masterMesh.nCells() == 0)
00092     {
00093         return autoPtr<faceCoupleInfo>
00094         (
00095             new faceCoupleInfo
00096             (
00097                 masterMesh,
00098                 meshToAdd,
00099                 mergeDist,      // absolute merging distance
00100                 true            // matching faces identical
00101             )
00102         );
00103     }
00104     else
00105     {
00106         // Pick up all patches on masterMesh ending in "toDDD" where DDD is
00107         // the processor number procI.
00108 
00109         const polyBoundaryMesh& masterPatches = masterMesh.boundaryMesh();
00110 
00111         const string toProcString("to" + name(procI));
00112 
00113         DynamicList<label> masterFaces
00114         (
00115             masterMesh.nFaces()
00116           - masterMesh.nInternalFaces()
00117         );
00118 
00119         forAll(masterPatches, patchI)
00120         {
00121             const polyPatch& pp = masterPatches[patchI];
00122 
00123             if
00124             (
00125                 isA<processorPolyPatch>(pp)
00126              && (
00127                     pp.name().rfind(toProcString)
00128                  == (pp.name().size()-toProcString.size())
00129                 )
00130             )
00131             {
00132                 label meshFaceI = pp.start();
00133                 forAll(pp, i)
00134                 {
00135                     masterFaces.append(meshFaceI++);
00136                 }
00137             }
00138         }
00139         masterFaces.shrink();
00140 
00141 
00142         // Pick up all patches on meshToAdd ending in "procBoundaryDDDtoYYY"
00143         // where DDD is the processor number procI and YYY is < procI.
00144 
00145         const polyBoundaryMesh& addPatches = meshToAdd.boundaryMesh();
00146 
00147         DynamicList<label> addFaces
00148         (
00149             meshToAdd.nFaces()
00150           - meshToAdd.nInternalFaces()
00151         );
00152 
00153         forAll(addPatches, patchI)
00154         {
00155             const polyPatch& pp = addPatches[patchI];
00156 
00157             if (isA<processorPolyPatch>(pp))
00158             {
00159                 bool isConnected = false;
00160 
00161                 for (label mergedProcI = 0; mergedProcI < procI; mergedProcI++)
00162                 {
00163                     const string fromProcString
00164                     (
00165                         "procBoundary"
00166                       + name(procI)
00167                       + "to"
00168                       + name(mergedProcI)
00169                     );
00170 
00171                     if (pp.name() == fromProcString)
00172                     {
00173                         isConnected = true;
00174                         break;
00175                     }
00176                 }
00177 
00178                 if (isConnected)
00179                 {
00180                     label meshFaceI = pp.start();
00181                     forAll(pp, i)
00182                     {
00183                         addFaces.append(meshFaceI++);
00184                     }
00185                 }
00186             }
00187         }
00188         addFaces.shrink();
00189 
00190         return autoPtr<faceCoupleInfo>
00191         (
00192             new faceCoupleInfo
00193             (
00194                 masterMesh,
00195                 masterFaces,
00196                 meshToAdd,
00197                 addFaces,
00198                 mergeDist,      // absolute merging distance
00199                 true,           // matching faces identical?
00200                 false,          // if perfectmatch are faces already ordered
00201                                 // (e.g. processor patches)
00202                 false           // are faces each on separate patch?
00203             )
00204         );
00205     }
00206 }
00207 
00208 
00209 autoPtr<mapPolyMesh> mergeSharedPoints
00210 (
00211     const scalar mergeDist,
00212     polyMesh& mesh,
00213     labelListList& pointProcAddressing
00214 )
00215 {
00216     // Find out which sets of points get merged and create a map from
00217     // mesh point to unique point.
00218     Map<label> pointToMaster
00219     (
00220         fvMeshAdder::findSharedPoints
00221         (
00222             mesh,
00223             mergeDist
00224         )
00225     );
00226 
00227     Info<< "mergeSharedPoints : detected " << pointToMaster.size()
00228         << " points that are to be merged." << endl;
00229 
00230     if (returnReduce(pointToMaster.size(), sumOp<label>()) == 0)
00231     {
00232         return autoPtr<mapPolyMesh>(NULL);
00233     }
00234 
00235     polyTopoChange meshMod(mesh);
00236 
00237     fvMeshAdder::mergePoints(mesh, pointToMaster, meshMod);
00238 
00239     // Change the mesh (no inflation). Note: parallel comms allowed.
00240     autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false, true);
00241 
00242     // Update fields. No inflation, parallel sync.
00243     mesh.updateMesh(map);
00244 
00245     // pointProcAddressing give indices into the master mesh so adapt them
00246     // for changed point numbering.
00247 
00248     // Adapt constructMaps for merged points.
00249     forAll(pointProcAddressing, procI)
00250     {
00251         labelList& constructMap = pointProcAddressing[procI];
00252 
00253         forAll(constructMap, i)
00254         {
00255             label oldPointI = constructMap[i];
00256 
00257             // New label of point after changeMesh.
00258             label newPointI = map().reversePointMap()[oldPointI];
00259 
00260             if (newPointI < -1)
00261             {
00262                 constructMap[i] = -newPointI-2;
00263             }
00264             else if (newPointI >= 0)
00265             {
00266                 constructMap[i] = newPointI;
00267             }
00268             else
00269             {
00270                 FatalErrorIn("fvMeshDistribute::mergeSharedPoints()")
00271                     << "Problem. oldPointI:" << oldPointI
00272                     << " newPointI:" << newPointI << abort(FatalError);
00273             }
00274         }
00275     }
00276 
00277     return map;
00278 }
00279 
00280 
00281 int main(int argc, char *argv[])
00282 {
00283     argList::noParallel();
00284     argList::validOptions.insert("mergeTol", "relative merge distance");
00285     argList::validOptions.insert("fullMatch", "");
00286 
00287 #   include "addTimeOptions.H"
00288 #   include "addRegionOption.H"
00289 #   include "setRootCase.H"
00290 #   include "createTime.H"
00291 
00292     Info<< "This is an experimental tool which tries to merge"
00293         << " individual processor" << nl
00294         << "meshes back into one master mesh. Use it if the original"
00295         << " master mesh has" << nl
00296         << "been deleted or if the processor meshes have been modified"
00297         << " (topology change)." << nl
00298         << "This tool will write the resulting mesh to a new time step"
00299         << " and construct" << nl
00300         << "xxxxProcAddressing files in the processor meshes so"
00301         << " reconstructPar can be" << nl
00302         << "used to regenerate the fields on the master mesh." << nl
00303         << nl
00304         << "Not well tested & use at your own risk!" << nl
00305         << endl;
00306 
00307 
00308     word regionName = polyMesh::defaultRegion;
00309     fileName regionPrefix = "";
00310     if (args.optionFound("region"))
00311     {
00312         regionName = args.option("region");
00313         regionPrefix = regionName;
00314         Info<< "Operating on region " << regionName << nl << endl;
00315     }
00316 
00317     scalar mergeTol = defaultMergeTol;
00318     args.optionReadIfPresent("mergeTol", mergeTol);
00319 
00320     scalar writeTol = Foam::pow(10.0, -scalar(IOstream::defaultPrecision()));
00321 
00322     Info<< "Merge tolerance : " << mergeTol << nl
00323         << "Write tolerance : " << writeTol << endl;
00324 
00325     if (runTime.writeFormat() == IOstream::ASCII && mergeTol < writeTol)
00326     {
00327         FatalErrorIn(args.executable())
00328             << "Your current settings specify ASCII writing with "
00329             << IOstream::defaultPrecision() << " digits precision." << endl
00330             << "Your merging tolerance (" << mergeTol << ") is finer than this."
00331             << endl
00332             << "Please change your writeFormat to binary"
00333             << " or increase the writePrecision" << endl
00334             << "or adjust the merge tolerance (-mergeTol)."
00335             << exit(FatalError);
00336     }
00337 
00338 
00339     const bool fullMatch = args.optionFound("fullMatch");
00340 
00341     if (fullMatch)
00342     {
00343         Info<< "Doing geometric matching on all boundary faces." << nl << endl;
00344     }
00345     else
00346     {
00347         Info<< "Doing geometric matching on correct procBoundaries only."
00348             << nl << "This assumes a correct decomposition." << endl;
00349     }
00350 
00351 
00352 
00353     int nProcs = 0;
00354 
00355     while
00356     (
00357         isDir
00358         (
00359             args.rootPath()
00360           / args.caseName()
00361           / fileName(word("processor") + name(nProcs))
00362         )
00363     )
00364     {
00365         nProcs++;
00366     }
00367 
00368     Info<< "Found " << nProcs << " processor directories" << nl << endl;
00369 
00370 
00371     // Read all databases.
00372     PtrList<Time> databases(nProcs);
00373 
00374     forAll (databases, procI)
00375     {
00376         Info<< "Reading database "
00377             << args.caseName()/fileName(word("processor") + name(procI))
00378             << endl;
00379 
00380         databases.set
00381         (
00382             procI,
00383             new Time
00384             (
00385                 Time::controlDictName,
00386                 args.rootPath(),
00387                 args.caseName()/fileName(word("processor") + name(procI))
00388             )
00389         );
00390 
00391         Time& procTime = databases[procI];
00392 
00393         instantList Times = procTime.times();
00394 
00395         // set startTime and endTime depending on -time and -latestTime options
00396 #       include "checkTimeOptions.H"
00397 
00398         procTime.setTime(Times[startTime], startTime);
00399 
00400         if (procI > 0 && databases[procI-1].value() != procTime.value())
00401         {
00402             FatalErrorIn(args.executable())
00403                 << "Time not equal on processors." << nl
00404                 << "Processor:" << procI-1
00405                 << " time:" << databases[procI-1].value() << nl
00406                 << "Processor:" << procI
00407                 << " time:" << procTime.value()
00408                 << exit(FatalError);
00409         }
00410     }
00411 
00412     // Set master time
00413     Info<< "Setting master time to " << databases[0].timeName() << nl << endl;
00414     runTime.setTime(databases[0]);
00415 
00416 
00417     // Read point on individual processors to determine merge tolerance
00418     // (otherwise single cell domains might give problems)
00419 
00420     boundBox bb = boundBox::invertedBox;
00421 
00422     for (label procI = 0; procI < nProcs; procI++)
00423     {
00424         fileName pointsInstance
00425         (
00426             databases[procI].findInstance
00427             (
00428                 regionPrefix/polyMesh::meshSubDir,
00429                 "points"
00430             )
00431         );
00432 
00433         if (pointsInstance != databases[procI].timeName())
00434         {
00435             FatalErrorIn(args.executable())
00436                 << "Your time was specified as " << databases[procI].timeName()
00437                 << " but there is no polyMesh/points in that time." << endl
00438                 << "(there is a points file in " << pointsInstance
00439                 << ")" << endl
00440                 << "Please rerun with the correct time specified"
00441                 << " (through the -constant, -time or -latestTime option)."
00442                 << endl << exit(FatalError);
00443         }
00444 
00445         Info<< "Reading points from "
00446             << databases[procI].caseName()
00447             << " for time = " << databases[procI].timeName()
00448             << nl << endl;
00449 
00450         pointIOField points
00451         (
00452             IOobject
00453             (
00454                 "points",
00455                 databases[procI].findInstance
00456                 (
00457                     regionPrefix/polyMesh::meshSubDir,
00458                     "points"
00459                 ),
00460                 regionPrefix/polyMesh::meshSubDir,
00461                 databases[procI],
00462                 IOobject::MUST_READ,
00463                 IOobject::NO_WRITE,
00464                 false
00465             )
00466         );
00467 
00468         boundBox domainBb(points, false);
00469 
00470         bb.min() = min(bb.min(), domainBb.min());
00471         bb.max() = max(bb.max(), domainBb.max());
00472     }
00473     const scalar mergeDist = mergeTol * bb.mag();
00474 
00475     Info<< "Overall mesh bounding box  : " << bb << nl
00476         << "Relative tolerance         : " << mergeTol << nl
00477         << "Absolute matching distance : " << mergeDist << nl
00478         << endl;
00479 
00480 
00481     // Addressing from processor to reconstructed case
00482     labelListList cellProcAddressing(nProcs);
00483     labelListList faceProcAddressing(nProcs);
00484     labelListList pointProcAddressing(nProcs);
00485     labelListList boundaryProcAddressing(nProcs);
00486 
00487     // Internal faces on the final reconstructed mesh
00488     label masterInternalFaces;
00489     // Owner addressing on the final reconstructed mesh
00490     labelList masterOwner;
00491 
00492     {
00493         // Construct empty mesh.
00494         Info<< "Constructing empty mesh to add to." << nl << endl;
00495         polyMesh masterMesh
00496         (
00497             IOobject
00498             (
00499                 regionName,
00500                 runTime.timeName(),
00501                 runTime,
00502                 IOobject::NO_READ
00503             ),
00504             xferCopy(pointField()),
00505             xferCopy(faceList()),
00506             xferCopy(cellList())
00507         );
00508 
00509         for (label procI = 0; procI < nProcs; procI++)
00510         {
00511             Info<< "Reading mesh to add from "
00512                 << databases[procI].caseName()
00513                 << " for time = " << databases[procI].timeName()
00514                 << nl << endl;
00515 
00516             polyMesh meshToAdd
00517             (
00518                 IOobject
00519                 (
00520                     regionName,
00521                     databases[procI].timeName(),
00522                     databases[procI]
00523                 )
00524             );
00525 
00526             // Initialize its addressing
00527             cellProcAddressing[procI] = identity(meshToAdd.nCells());
00528             faceProcAddressing[procI] = identity(meshToAdd.nFaces());
00529             pointProcAddressing[procI] = identity(meshToAdd.nPoints());
00530             boundaryProcAddressing[procI] =
00531                 identity(meshToAdd.boundaryMesh().size());
00532 
00533 
00534             // Find geometrically shared points/faces.
00535             autoPtr<faceCoupleInfo> couples = determineCoupledFaces
00536             (
00537                 fullMatch,
00538                 procI,
00539                 masterMesh,
00540                 meshToAdd,
00541                 mergeDist
00542             );
00543 
00544 
00545             // Add elements to mesh
00546             Info<< "Adding to master mesh" << nl << endl;
00547 
00548             autoPtr<mapAddedPolyMesh> map = polyMeshAdder::add
00549             (
00550                 masterMesh,
00551                 meshToAdd,
00552                 couples
00553             );
00554 
00555             // Update all addressing so xxProcAddressing points to correct item
00556             // in masterMesh.
00557 
00558             // Processors that were already in masterMesh
00559             for (label mergedI = 0; mergedI < procI; mergedI++)
00560             {
00561                 renumber(map().oldCellMap(), cellProcAddressing[mergedI]);
00562                 renumber(map().oldFaceMap(), faceProcAddressing[mergedI]);
00563                 renumber(map().oldPointMap(), pointProcAddressing[mergedI]);
00564                 // Note: boundary is special since can contain -1.
00565                 renumber(map().oldPatchMap(), boundaryProcAddressing[mergedI]);
00566             }
00567 
00568             // Added processor
00569             renumber(map().addedCellMap(), cellProcAddressing[procI]);
00570             renumber(map().addedFaceMap(), faceProcAddressing[procI]);
00571             renumber(map().addedPointMap(), pointProcAddressing[procI]);
00572             renumber(map().addedPatchMap(), boundaryProcAddressing[procI]);
00573 
00574             Info<< endl;
00575         }
00576 
00577         // See if any points on the mastermesh have become connected
00578         // because of connections through processor meshes.
00579         mergeSharedPoints(mergeDist, masterMesh, pointProcAddressing);
00580 
00581         // Save some properties on the reconstructed mesh
00582         masterInternalFaces = masterMesh.nInternalFaces();
00583         masterOwner = masterMesh.faceOwner();
00584 
00585 
00586         Info<< "\nWriting merged mesh to "
00587             << runTime.path()/runTime.timeName()
00588             << nl << endl;
00589 
00590         if (!masterMesh.write())
00591         {
00592             FatalErrorIn(args.executable())
00593                 << "Failed writing polyMesh."
00594                 << exit(FatalError);
00595         }
00596     }
00597 
00598 
00599     // Write the addressing
00600 
00601     Info<< "Reconstructing the addressing from the processor meshes"
00602         << " to the newly reconstructed mesh" << nl << endl;
00603 
00604     forAll(databases, procI)
00605     {
00606         Info<< "Reading processor " << procI << " mesh from "
00607             << databases[procI].caseName() << endl;
00608 
00609         polyMesh procMesh
00610         (
00611             IOobject
00612             (
00613                 regionName,
00614                 databases[procI].timeName(),
00615                 databases[procI]
00616             )
00617         );
00618 
00619 
00620         // From processor point to reconstructed mesh point
00621 
00622         Info<< "Writing pointProcAddressing to "
00623             << databases[procI].caseName()
00624               /procMesh.facesInstance()
00625               /polyMesh::meshSubDir
00626             << endl;
00627 
00628         labelIOList
00629         (
00630             IOobject
00631             (
00632                 "pointProcAddressing",
00633                 procMesh.facesInstance(),
00634                 polyMesh::meshSubDir,
00635                 procMesh,
00636                 IOobject::NO_READ,
00637                 IOobject::NO_WRITE,
00638                 false                       // do not register
00639             ),
00640             pointProcAddressing[procI]
00641         ).write();
00642 
00643 
00644         // From processor face to reconstructed mesh face
00645 
00646         Info<< "Writing faceProcAddressing to "
00647             << databases[procI].caseName()
00648               /procMesh.facesInstance()
00649               /polyMesh::meshSubDir
00650             << endl;
00651 
00652         labelIOList faceProcAddr
00653         (
00654             IOobject
00655             (
00656                 "faceProcAddressing",
00657                 procMesh.facesInstance(),
00658                 polyMesh::meshSubDir,
00659                 procMesh,
00660                 IOobject::NO_READ,
00661                 IOobject::NO_WRITE,
00662                 false                       // do not register
00663             ),
00664             faceProcAddressing[procI]
00665         );
00666 
00667         // Now add turning index to faceProcAddressing.
00668         // See reconstrurPar for meaning of turning index.
00669         forAll(faceProcAddr, procFaceI)
00670         {
00671             label masterFaceI = faceProcAddr[procFaceI];
00672 
00673             if
00674             (
00675                !procMesh.isInternalFace(procFaceI)
00676              && masterFaceI < masterInternalFaces
00677             )
00678             {
00679                 // proc face is now external but used to be internal face.
00680                 // Check if we have owner or neighbour.
00681 
00682                 label procOwn = procMesh.faceOwner()[procFaceI];
00683                 label masterOwn = masterOwner[masterFaceI];
00684 
00685                 if (cellProcAddressing[procI][procOwn] == masterOwn)
00686                 {
00687                     // No turning. Offset by 1.
00688                     faceProcAddr[procFaceI]++;
00689                 }
00690                 else
00691                 {
00692                     // Turned face.
00693                     faceProcAddr[procFaceI] =
00694                         -1 - faceProcAddr[procFaceI];
00695                 }
00696             }
00697             else
00698             {
00699                 // No turning. Offset by 1.
00700                 faceProcAddr[procFaceI]++;
00701             }
00702         }
00703 
00704         faceProcAddr.write();
00705 
00706 
00707         // From processor cell to reconstructed mesh cell
00708 
00709         Info<< "Writing cellProcAddressing to "
00710             << databases[procI].caseName()
00711               /procMesh.facesInstance()
00712               /polyMesh::meshSubDir
00713             << endl;
00714 
00715         labelIOList
00716         (
00717             IOobject
00718             (
00719                 "cellProcAddressing",
00720                 procMesh.facesInstance(),
00721                 polyMesh::meshSubDir,
00722                 procMesh,
00723                 IOobject::NO_READ,
00724                 IOobject::NO_WRITE,
00725                 false                       // do not register
00726             ),
00727             cellProcAddressing[procI]
00728         ).write();
00729 
00730 
00731 
00732         // From processor patch to reconstructed mesh patch
00733 
00734         Info<< "Writing boundaryProcAddressing to "
00735             << databases[procI].caseName()
00736               /procMesh.facesInstance()
00737               /polyMesh::meshSubDir
00738             << endl;
00739 
00740         labelIOList
00741         (
00742             IOobject
00743             (
00744                 "boundaryProcAddressing",
00745                 procMesh.facesInstance(),
00746                 polyMesh::meshSubDir,
00747                 procMesh,
00748                 IOobject::NO_READ,
00749                 IOobject::NO_WRITE,
00750                 false                       // do not register
00751             ),
00752             boundaryProcAddressing[procI]
00753         ).write();
00754 
00755         Info<< endl;
00756     }
00757 
00758     Info<< "End.\n" << endl;
00759 
00760     return 0;
00761 }
00762 
00763 
00764 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd