00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00058
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
00079
00080
00081
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,
00100 true
00101 )
00102 );
00103 }
00104 else
00105 {
00106
00107
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
00143
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,
00199 true,
00200 false,
00201
00202 false
00203 )
00204 );
00205 }
00206 }
00207
00208
00209 autoPtr<mapPolyMesh> mergeSharedPoints
00210 (
00211 const scalar mergeDist,
00212 polyMesh& mesh,
00213 labelListList& pointProcAddressing
00214 )
00215 {
00216
00217
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
00240 autoPtr<mapPolyMesh> map = meshMod.changeMesh(mesh, false, true);
00241
00242
00243 mesh.updateMesh(map);
00244
00245
00246
00247
00248
00249 forAll(pointProcAddressing, procI)
00250 {
00251 labelList& constructMap = pointProcAddressing[procI];
00252
00253 forAll(constructMap, i)
00254 {
00255 label oldPointI = constructMap[i];
00256
00257
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
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
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
00413 Info<< "Setting master time to " << databases[0].timeName() << nl << endl;
00414 runTime.setTime(databases[0]);
00415
00416
00417
00418
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
00482 labelListList cellProcAddressing(nProcs);
00483 labelListList faceProcAddressing(nProcs);
00484 labelListList pointProcAddressing(nProcs);
00485 labelListList boundaryProcAddressing(nProcs);
00486
00487
00488 label masterInternalFaces;
00489
00490 labelList masterOwner;
00491
00492 {
00493
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
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
00535 autoPtr<faceCoupleInfo> couples = determineCoupledFaces
00536 (
00537 fullMatch,
00538 procI,
00539 masterMesh,
00540 meshToAdd,
00541 mergeDist
00542 );
00543
00544
00545
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
00556
00557
00558
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
00565 renumber(map().oldPatchMap(), boundaryProcAddressing[mergedI]);
00566 }
00567
00568
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
00578
00579 mergeSharedPoints(mergeDist, masterMesh, pointProcAddressing);
00580
00581
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
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
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
00639 ),
00640 pointProcAddressing[procI]
00641 ).write();
00642
00643
00644
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
00663 ),
00664 faceProcAddressing[procI]
00665 );
00666
00667
00668
00669 forAll(faceProcAddr, procFaceI)
00670 {
00671 label masterFaceI = faceProcAddr[procFaceI];
00672
00673 if
00674 (
00675 !procMesh.isInternalFace(procFaceI)
00676 && masterFaceI < masterInternalFaces
00677 )
00678 {
00679
00680
00681
00682 label procOwn = procMesh.faceOwner()[procFaceI];
00683 label masterOwn = masterOwner[masterFaceI];
00684
00685 if (cellProcAddressing[procI][procOwn] == masterOwn)
00686 {
00687
00688 faceProcAddr[procFaceI]++;
00689 }
00690 else
00691 {
00692
00693 faceProcAddr[procFaceI] =
00694 -1 - faceProcAddr[procFaceI];
00695 }
00696 }
00697 else
00698 {
00699
00700 faceProcAddr[procFaceI]++;
00701 }
00702 }
00703
00704 faceProcAddr.write();
00705
00706
00707
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
00726 ),
00727 cellProcAddressing[procI]
00728 ).write();
00729
00730
00731
00732
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
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