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
00043 #include "Field.H"
00044 #include "fvMesh.H"
00045 #include "decompositionMethod.H"
00046 #include "PstreamReduceOps.H"
00047 #include "fvCFD.H"
00048 #include "fvMeshDistribute.H"
00049 #include "mapDistributePolyMesh.H"
00050 #include "IOobjectList.H"
00051
00052
00053
00054
00055
00056 static const scalar defaultMergeTol = 1E-6;
00057
00058
00059
00060
00061
00062 autoPtr<fvMesh> createMesh
00063 (
00064 const Time& runTime,
00065 const fileName& instDir,
00066 const bool haveMesh
00067 )
00068 {
00069 Pout<< "Create mesh for time = "
00070 << runTime.timeName() << nl << endl;
00071
00072
00073
00074 fvMesh dummyMesh
00075 (
00076 IOobject
00077 (
00078 fvMesh::defaultRegion,
00079 instDir,
00080 runTime,
00081 IOobject::MUST_READ
00082 ),
00083 xferCopy(pointField()),
00084 xferCopy(faceList()),
00085 xferCopy(labelList()),
00086 xferCopy(labelList())
00087 );
00088
00089 if (!haveMesh)
00090 {
00091 Pout<< "Writing dummy mesh to " << runTime.path()/instDir << endl;
00092 dummyMesh.write();
00093 }
00094
00095 Pout<< "Reading mesh from " << runTime.path()/instDir << endl;
00096 autoPtr<fvMesh> meshPtr
00097 (
00098 new fvMesh
00099 (
00100 IOobject
00101 (
00102 fvMesh::defaultRegion,
00103 instDir,
00104 runTime,
00105 IOobject::MUST_READ
00106 )
00107 )
00108 );
00109 fvMesh& mesh = meshPtr();
00110
00111
00112
00113 if (Pstream::master())
00114 {
00115
00116 for
00117 (
00118 int slave=Pstream::firstSlave();
00119 slave<=Pstream::lastSlave();
00120 slave++
00121 )
00122 {
00123 OPstream toSlave(Pstream::blocking, slave);
00124 toSlave << mesh.boundaryMesh();
00125 }
00126 }
00127 else
00128 {
00129
00130 IPstream fromMaster(Pstream::blocking, Pstream::masterNo());
00131 PtrList<entry> patchEntries(fromMaster);
00132
00133 if (haveMesh)
00134 {
00135
00136
00137 const polyBoundaryMesh& patches = mesh.boundaryMesh();
00138
00139 forAll(patchEntries, patchI)
00140 {
00141 const entry& e = patchEntries[patchI];
00142 const word type(e.dict().lookup("type"));
00143 const word& name = e.keyword();
00144
00145 if (type == processorPolyPatch::typeName)
00146 {
00147 break;
00148 }
00149
00150 if (patchI >= patches.size())
00151 {
00152 FatalErrorIn
00153 (
00154 "createMesh(const Time&, const fileName&, const bool)"
00155 ) << "Non-processor patches not synchronised."
00156 << endl
00157 << "Processor " << Pstream::myProcNo()
00158 << " has only " << patches.size()
00159 << " patches, master has "
00160 << patchI
00161 << exit(FatalError);
00162 }
00163
00164 if
00165 (
00166 type != patches[patchI].type()
00167 || name != patches[patchI].name()
00168 )
00169 {
00170 FatalErrorIn
00171 (
00172 "createMesh(const Time&, const fileName&, const bool)"
00173 ) << "Non-processor patches not synchronised."
00174 << endl
00175 << "Master patch " << patchI
00176 << " name:" << type
00177 << " type:" << type << endl
00178 << "Processor " << Pstream::myProcNo()
00179 << " patch " << patchI
00180 << " has name:" << patches[patchI].name()
00181 << " type:" << patches[patchI].type()
00182 << exit(FatalError);
00183 }
00184 }
00185 }
00186 else
00187 {
00188
00189 List<polyPatch*> patches(patchEntries.size());
00190 label nPatches = 0;
00191
00192 forAll(patchEntries, patchI)
00193 {
00194 const entry& e = patchEntries[patchI];
00195 const word type(e.dict().lookup("type"));
00196 const word& name = e.keyword();
00197
00198 if (type == processorPolyPatch::typeName)
00199 {
00200 break;
00201 }
00202
00203 Pout<< "Adding patch:" << nPatches
00204 << " name:" << name
00205 << " type:" << type << endl;
00206
00207 dictionary patchDict(e.dict());
00208 patchDict.remove("nFaces");
00209 patchDict.add("nFaces", 0);
00210 patchDict.remove("startFace");
00211 patchDict.add("startFace", 0);
00212
00213 patches[patchI] = polyPatch::New
00214 (
00215 name,
00216 patchDict,
00217 nPatches++,
00218 mesh.boundaryMesh()
00219 ).ptr();
00220 }
00221 patches.setSize(nPatches);
00222 mesh.addFvPatches(patches, false);
00223
00225
00226 }
00227 }
00228
00229 if (!haveMesh)
00230 {
00231
00232 Pout<< "Removing dummy mesh in " << runTime.path()/instDir << endl;
00233 rmDir(runTime.path()/instDir/polyMesh::meshSubDir);
00234 }
00235
00236
00237 mesh.clearOut();
00238 mesh.globalData();
00239
00240 return meshPtr;
00241 }
00242
00243
00244
00245 scalar getMergeDistance
00246 (
00247 const argList& args,
00248 const Time& runTime,
00249 const boundBox& bb
00250 )
00251 {
00252 scalar mergeTol = defaultMergeTol;
00253 args.optionReadIfPresent("mergeTol", mergeTol);
00254
00255 scalar writeTol =
00256 Foam::pow(scalar(10.0), -scalar(IOstream::defaultPrecision()));
00257
00258 Info<< "Merge tolerance : " << mergeTol << nl
00259 << "Write tolerance : " << writeTol << endl;
00260
00261 if (runTime.writeFormat() == IOstream::ASCII && mergeTol < writeTol)
00262 {
00263 FatalErrorIn("getMergeDistance")
00264 << "Your current settings specify ASCII writing with "
00265 << IOstream::defaultPrecision() << " digits precision." << endl
00266 << "Your merging tolerance (" << mergeTol << ") is finer than this."
00267 << endl
00268 << "Please change your writeFormat to binary"
00269 << " or increase the writePrecision" << endl
00270 << "or adjust the merge tolerance (-mergeTol)."
00271 << exit(FatalError);
00272 }
00273
00274 scalar mergeDist = mergeTol * bb.mag();
00275
00276 Info<< "Overall meshes bounding box : " << bb << nl
00277 << "Relative tolerance : " << mergeTol << nl
00278 << "Absolute matching distance : " << mergeDist << nl
00279 << endl;
00280
00281 return mergeDist;
00282 }
00283
00284
00285 void printMeshData(Ostream& os, const polyMesh& mesh)
00286 {
00287 os << "Number of points: " << mesh.points().size() << nl
00288 << " edges: " << mesh.edges().size() << nl
00289 << " faces: " << mesh.faces().size() << nl
00290 << " internal faces: " << mesh.faceNeighbour().size() << nl
00291 << " cells: " << mesh.cells().size() << nl
00292 << " boundary patches: " << mesh.boundaryMesh().size() << nl
00293 << " point zones: " << mesh.pointZones().size() << nl
00294 << " face zones: " << mesh.faceZones().size() << nl
00295 << " cell zones: " << mesh.cellZones().size() << nl;
00296 }
00297
00298
00299
00300 void writeDecomposition
00301 (
00302 const word& name,
00303 const fvMesh& mesh,
00304 const labelList& decomp
00305 )
00306 {
00307 Info<< "Writing wanted cell distribution to volScalarField " << name
00308 << " for postprocessing purposes." << nl << endl;
00309
00310 volScalarField procCells
00311 (
00312 IOobject
00313 (
00314 name,
00315 mesh.time().timeName(),
00316 mesh,
00317 IOobject::NO_READ,
00318 IOobject::AUTO_WRITE,
00319 false
00320 ),
00321 mesh,
00322 dimensionedScalar(name, dimless, -1),
00323 zeroGradientFvPatchScalarField::typeName
00324 );
00325
00326 forAll(procCells, cI)
00327 {
00328 procCells[cI] = decomp[cI];
00329 }
00330 procCells.write();
00331 }
00332
00333
00334
00335
00336 template<class GeoField>
00337 void readFields
00338 (
00339 const boolList& haveMesh,
00340 const fvMesh& mesh,
00341 const autoPtr<fvMeshSubset>& subsetterPtr,
00342 IOobjectList& allObjects,
00343 PtrList<GeoField>& fields
00344 )
00345 {
00346
00347
00348
00349 IOobjectList objects(allObjects.lookupClass(GeoField::typeName));
00350
00351 wordList objectNames = objects.toc();
00352
00353 wordList masterNames(objectNames);
00354 Pstream::scatter(masterNames);
00355
00356 if (haveMesh[Pstream::myProcNo()] && objectNames != masterNames)
00357 {
00358 FatalErrorIn("readFields()")
00359 << "differing fields of type " << GeoField::typeName
00360 << " on processors." << endl
00361 << "Master has:" << masterNames << endl
00362 << Pstream::myProcNo() << " has:" << objectNames
00363 << abort(FatalError);
00364 }
00365
00366 fields.setSize(masterNames.size());
00367
00368
00369 if (Pstream::master())
00370 {
00371 forAll(masterNames, i)
00372 {
00373 const word& name = masterNames[i];
00374 IOobject& io = *objects[name];
00375 io.writeOpt() = IOobject::AUTO_WRITE;
00376
00377
00378 fields.set(i, new GeoField(io, mesh));
00379
00380
00381 if (subsetterPtr.valid())
00382 {
00383 tmp<GeoField> tsubfld = subsetterPtr().interpolate(fields[i]);
00384
00385
00386 for (label procI = 1; procI < Pstream::nProcs(); procI++)
00387 {
00388 if (!haveMesh[procI])
00389 {
00390 OPstream toProc(Pstream::blocking, procI);
00391 toProc<< tsubfld();
00392 }
00393 }
00394 }
00395 }
00396 }
00397 else if (!haveMesh[Pstream::myProcNo()])
00398 {
00399
00400
00401 forAll(masterNames, i)
00402 {
00403 const word& name = masterNames[i];
00404
00405
00406 IPstream fromMaster(Pstream::blocking, Pstream::masterNo());
00407
00408 fields.set
00409 (
00410 i,
00411 new GeoField
00412 (
00413 IOobject
00414 (
00415 name,
00416 mesh.time().timeName(),
00417 mesh,
00418 IOobject::NO_READ,
00419 IOobject::AUTO_WRITE
00420 ),
00421 mesh,
00422 fromMaster
00423 )
00424 );
00425
00427
00428 }
00429 }
00430 else
00431 {
00432
00433 forAll(masterNames, i)
00434 {
00435 const word& name = masterNames[i];
00436 IOobject& io = *objects[name];
00437 io.writeOpt() = IOobject::AUTO_WRITE;
00438
00439
00440 fields.set(i, new GeoField(io, mesh));
00441 }
00442 }
00443 }
00444
00445
00446
00447 void compareFields
00448 (
00449 const scalar tolDim,
00450 const volVectorField& a,
00451 const volVectorField& b
00452 )
00453 {
00454 forAll(a, cellI)
00455 {
00456 if (mag(b[cellI] - a[cellI]) > tolDim)
00457 {
00458 FatalErrorIn
00459 (
00460 "compareFields"
00461 "(const scalar, const volVectorField&, const volVectorField&)"
00462 ) << "Did not map volVectorField correctly:" << nl
00463 << "cell:" << cellI
00464 << " transfer b:" << b[cellI]
00465 << " real cc:" << a[cellI]
00466 << abort(FatalError);
00467 }
00468 }
00469 forAll(a.boundaryField(), patchI)
00470 {
00471
00472
00473
00474 const fvPatchVectorField& aBoundary =
00475 a.boundaryField()[patchI];
00476
00477 const fvPatchVectorField& bBoundary =
00478 b.boundaryField()[patchI];
00479
00480 if (!bBoundary.coupled())
00481 {
00482 forAll(aBoundary, i)
00483 {
00484 if (mag(aBoundary[i] - bBoundary[i]) > tolDim)
00485 {
00486 FatalErrorIn
00487 (
00488 "compareFields"
00489 "(const scalar, const volVectorField&"
00490 ", const volVectorField&)"
00491 ) << "Did not map volVectorField correctly:"
00492 << endl
00493 << "patch:" << patchI << " patchFace:" << i
00494 << " cc:" << endl
00495 << " real :" << aBoundary[i] << endl
00496 << " mapped :" << bBoundary[i] << endl
00497 << abort(FatalError);
00498 }
00499 }
00500 }
00501 }
00502 }
00503
00504
00505
00506
00507 int main(int argc, char *argv[])
00508 {
00509 argList::validOptions.insert("mergeTol", "relative merge distance");
00510 # include "setRootCase.H"
00511
00512
00513 if (!Pstream::master() && !isDir(args.path()))
00514 {
00515 Pout<< "Creating case directory " << args.path() << endl;
00516 mkDir(args.path());
00517 }
00518
00519 # include "createTime.H"
00520
00521
00522
00523
00524 fileName masterInstDir;
00525 if (Pstream::master())
00526 {
00527 masterInstDir = runTime.findInstance(polyMesh::meshSubDir, "points");
00528 }
00529 Pstream::scatter(masterInstDir);
00530
00531
00532 const fileName meshDir = runTime.path()/masterInstDir/polyMesh::meshSubDir;
00533
00534 boolList haveMesh(Pstream::nProcs(), false);
00535 haveMesh[Pstream::myProcNo()] = isDir(meshDir);
00536 Pstream::gatherList(haveMesh);
00537 Pstream::scatterList(haveMesh);
00538 Info<< "Per processor mesh availability : " << haveMesh << endl;
00539 const bool allHaveMesh = (findIndex(haveMesh, false) == -1);
00540
00541
00542 autoPtr<fvMesh> meshPtr = createMesh
00543 (
00544 runTime,
00545 masterInstDir,
00546 haveMesh[Pstream::myProcNo()]
00547 );
00548 fvMesh& mesh = meshPtr();
00549
00550 Pout<< "Read mesh:" << endl;
00551 printMeshData(Pout, mesh);
00552 Pout<< endl;
00553
00554 IOdictionary decompositionDict
00555 (
00556 IOobject
00557 (
00558 "decomposeParDict",
00559 runTime.system(),
00560 mesh,
00561 IOobject::MUST_READ,
00562 IOobject::NO_WRITE
00563 )
00564 );
00565
00566 labelList finalDecomp;
00567
00568
00569 {
00570 autoPtr<decompositionMethod> decomposer
00571 (
00572 decompositionMethod::New
00573 (
00574 decompositionDict,
00575 mesh
00576 )
00577 );
00578
00579 if (!decomposer().parallelAware())
00580 {
00581 WarningIn(args.executable())
00582 << "You have selected decomposition method "
00583 << decomposer().typeName
00584 << " which does" << endl
00585 << "not synchronise the decomposition across"
00586 << " processor patches." << endl
00587 << " You might want to select a decomposition method which"
00588 << " is aware of this. Continuing."
00589 << endl;
00590 }
00591
00592 finalDecomp = decomposer().decompose(mesh.cellCentres());
00593 }
00594
00595
00596 writeDecomposition("decomposition", mesh, finalDecomp);
00597
00598
00599
00600
00601
00602
00603
00604 autoPtr<fvMeshSubset> subsetterPtr;
00605
00606 if (!allHaveMesh)
00607 {
00608
00609 const polyBoundaryMesh& patches = mesh.boundaryMesh();
00610
00611 label nonProcI = -1;
00612
00613 forAll(patches, patchI)
00614 {
00615 if (isA<processorPolyPatch>(patches[patchI]))
00616 {
00617 break;
00618 }
00619 nonProcI++;
00620 }
00621
00622 if (nonProcI == -1)
00623 {
00624 FatalErrorIn(args.executable())
00625 << "Cannot find non-processor patch on processor "
00626 << Pstream::myProcNo() << endl
00627 << " Current patches:" << patches.names() << abort(FatalError);
00628 }
00629
00630
00631
00632 subsetterPtr.reset(new fvMeshSubset(mesh));
00633 subsetterPtr().setLargeCellSubset(labelHashSet(0), nonProcI, false);
00634 }
00635
00636
00637
00638 IOobjectList objects(mesh, runTime.timeName());
00639
00640
00641 IOobjectList::iterator iter = objects.find("decomposition");
00642 if (iter != objects.end())
00643 {
00644 objects.erase(iter);
00645 }
00646
00647 PtrList<volScalarField> volScalarFields;
00648 readFields
00649 (
00650 haveMesh,
00651 mesh,
00652 subsetterPtr,
00653 objects,
00654 volScalarFields
00655 );
00656
00657 PtrList<volVectorField> volVectorFields;
00658 readFields
00659 (
00660 haveMesh,
00661 mesh,
00662 subsetterPtr,
00663 objects,
00664 volVectorFields
00665 );
00666
00667 PtrList<volSphericalTensorField> volSphereTensorFields;
00668 readFields
00669 (
00670 haveMesh,
00671 mesh,
00672 subsetterPtr,
00673 objects,
00674 volSphereTensorFields
00675 );
00676
00677 PtrList<volSymmTensorField> volSymmTensorFields;
00678 readFields
00679 (
00680 haveMesh,
00681 mesh,
00682 subsetterPtr,
00683 objects,
00684 volSymmTensorFields
00685 );
00686
00687 PtrList<volTensorField> volTensorFields;
00688 readFields
00689 (
00690 haveMesh,
00691 mesh,
00692 subsetterPtr,
00693 objects,
00694 volTensorFields
00695 );
00696
00697 PtrList<surfaceScalarField> surfScalarFields;
00698 readFields
00699 (
00700 haveMesh,
00701 mesh,
00702 subsetterPtr,
00703 objects,
00704 surfScalarFields
00705 );
00706
00707 PtrList<surfaceVectorField> surfVectorFields;
00708 readFields
00709 (
00710 haveMesh,
00711 mesh,
00712 subsetterPtr,
00713 objects,
00714 surfVectorFields
00715 );
00716
00717 PtrList<surfaceSphericalTensorField> surfSphereTensorFields;
00718 readFields
00719 (
00720 haveMesh,
00721 mesh,
00722 subsetterPtr,
00723 objects,
00724 surfSphereTensorFields
00725 );
00726
00727 PtrList<surfaceSymmTensorField> surfSymmTensorFields;
00728 readFields
00729 (
00730 haveMesh,
00731 mesh,
00732 subsetterPtr,
00733 objects,
00734 surfSymmTensorFields
00735 );
00736
00737 PtrList<surfaceTensorField> surfTensorFields;
00738 readFields
00739 (
00740 haveMesh,
00741 mesh,
00742 subsetterPtr,
00743 objects,
00744 surfTensorFields
00745 );
00746
00747
00748
00749
00750 volVectorField mapCc("mapCc", 1*mesh.C());
00751
00752
00753 const scalar tolDim = getMergeDistance
00754 (
00755 args,
00756 runTime,
00757 mesh.globalData().bb()
00758 );
00759
00760
00761 fvMeshDistribute distributor(mesh, tolDim);
00762
00763 Pout<< "Wanted distribution:"
00764 << distributor.countCells(finalDecomp) << nl << endl;
00765
00766
00767 autoPtr<mapDistributePolyMesh> map = distributor.distribute(finalDecomp);
00768
00770
00771
00772
00773
00774 Pout<< "After distribution mesh:" << endl;
00775 printMeshData(Pout, mesh);
00776 Pout<< endl;
00777
00778 runTime++;
00779 Pout<< "Writing redistributed mesh to " << runTime.timeName() << nl << endl;
00780 mesh.write();
00781
00782
00783
00784 compareFields(tolDim, mesh.C(), mapCc);
00785
00786
00787
00788
00789
00790 labelList nFaces(Pstream::nProcs());
00791 nFaces[Pstream::myProcNo()] = mesh.nFaces();
00792 Pstream::gatherList(nFaces);
00793 Pstream::scatterList(nFaces);
00794
00795 Info<< nl
00796 << "You can pick up the redecomposed mesh from the polyMesh directory"
00797 << " in " << runTime.timeName() << "." << nl
00798 << "If you redecomposed the mesh to less processors you can delete"
00799 << nl
00800 << "the processor directories with 0 sized meshes in them." << nl
00801 << "Below is a sample set of commands to do this."
00802 << " Take care when issueing these" << nl
00803 << "commands." << nl << endl;
00804
00805 forAll(nFaces, procI)
00806 {
00807 fileName procDir = "processor" + name(procI);
00808
00809 if (nFaces[procI] == 0)
00810 {
00811 Info<< " rm -r " << procDir.c_str() << nl;
00812 }
00813 else
00814 {
00815 fileName timeDir = procDir/runTime.timeName()/polyMesh::meshSubDir;
00816 fileName constDir = procDir/runTime.constant()/polyMesh::meshSubDir;
00817
00818 Info<< " rm -r " << constDir.c_str() << nl
00819 << " mv " << timeDir.c_str()
00820 << ' ' << constDir.c_str() << nl;
00821 }
00822 }
00823 Info<< endl;
00824
00825
00826 Pout<< "End\n" << endl;
00827
00828 return 0;
00829 }
00830
00831
00832