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
00044
00045
00046
00047
00048
00049 #include "SortableList.H"
00050 #include "argList.H"
00051 #include "regionSplit.H"
00052 #include "fvMeshSubset.H"
00053 #include "IOobjectList.H"
00054 #include "volFields.H"
00055 #include "faceSet.H"
00056 #include "cellSet.H"
00057 #include "polyTopoChange.H"
00058 #include "removeCells.H"
00059 #include "EdgeMap.H"
00060 #include "syncTools.H"
00061 #include "ReadFields.H"
00062 #include "directMappedWallPolyPatch.H"
00063
00064 using namespace Foam;
00065
00066
00067
00068 template<class GeoField>
00069 void addPatchFields(fvMesh& mesh, const word& patchFieldType)
00070 {
00071 HashTable<const GeoField*> flds
00072 (
00073 mesh.objectRegistry::lookupClass<GeoField>()
00074 );
00075
00076 for
00077 (
00078 typename HashTable<const GeoField*>::const_iterator iter = flds.begin();
00079 iter != flds.end();
00080 ++iter
00081 )
00082 {
00083 const GeoField& fld = *iter();
00084
00085 typename GeoField::GeometricBoundaryField& bfld =
00086 const_cast<typename GeoField::GeometricBoundaryField&>
00087 (
00088 fld.boundaryField()
00089 );
00090
00091 label sz = bfld.size();
00092 bfld.setSize(sz+1);
00093 bfld.set
00094 (
00095 sz,
00096 GeoField::PatchFieldType::New
00097 (
00098 patchFieldType,
00099 mesh.boundary()[sz],
00100 fld.dimensionedInternalField()
00101 )
00102 );
00103 }
00104 }
00105
00106
00107
00108 template<class GeoField>
00109 void trimPatchFields(fvMesh& mesh, const label nPatches)
00110 {
00111 HashTable<const GeoField*> flds
00112 (
00113 mesh.objectRegistry::lookupClass<GeoField>()
00114 );
00115
00116 for
00117 (
00118 typename HashTable<const GeoField*>::const_iterator iter = flds.begin();
00119 iter != flds.end();
00120 ++iter
00121 )
00122 {
00123 const GeoField& fld = *iter();
00124
00125 const_cast<typename GeoField::GeometricBoundaryField&>
00126 (
00127 fld.boundaryField()
00128 ).setSize(nPatches);
00129 }
00130 }
00131
00132
00133
00134 template<class GeoField>
00135 void reorderPatchFields(fvMesh& mesh, const labelList& oldToNew)
00136 {
00137 HashTable<const GeoField*> flds
00138 (
00139 mesh.objectRegistry::lookupClass<GeoField>()
00140 );
00141
00142 for
00143 (
00144 typename HashTable<const GeoField*>::const_iterator iter = flds.begin();
00145 iter != flds.end();
00146 ++iter
00147 )
00148 {
00149 const GeoField& fld = *iter();
00150
00151 typename GeoField::GeometricBoundaryField& bfld =
00152 const_cast<typename GeoField::GeometricBoundaryField&>
00153 (
00154 fld.boundaryField()
00155 );
00156
00157 bfld.reorder(oldToNew);
00158 }
00159 }
00160
00161
00162
00163 label addPatch
00164 (
00165 fvMesh& mesh,
00166 const word& patchName,
00167 const word& patchType
00168 )
00169 {
00170 polyBoundaryMesh& polyPatches =
00171 const_cast<polyBoundaryMesh&>(mesh.boundaryMesh());
00172
00173 label patchI = polyPatches.findPatchID(patchName);
00174 if (patchI != -1)
00175 {
00176 if (polyPatches[patchI].type() == patchType)
00177 {
00178
00179 return patchI;
00180 }
00181 }
00182
00183
00184 label insertPatchI = polyPatches.size();
00185 label startFaceI = mesh.nFaces();
00186
00187 forAll(polyPatches, patchI)
00188 {
00189 const polyPatch& pp = polyPatches[patchI];
00190
00191 if (isA<processorPolyPatch>(pp))
00192 {
00193 insertPatchI = patchI;
00194 startFaceI = pp.start();
00195 break;
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204 mesh.clearOut();
00205
00206 label sz = polyPatches.size();
00207
00208 fvBoundaryMesh& fvPatches = const_cast<fvBoundaryMesh&>(mesh.boundary());
00209
00210
00211 polyPatches.setSize(sz+1);
00212 polyPatches.set
00213 (
00214 sz,
00215 polyPatch::New
00216 (
00217 patchType,
00218 patchName,
00219 0,
00220 startFaceI,
00221 insertPatchI,
00222 polyPatches
00223 )
00224 );
00225 fvPatches.setSize(sz+1);
00226 fvPatches.set
00227 (
00228 sz,
00229 fvPatch::New
00230 (
00231 polyPatches[sz],
00232 mesh.boundary()
00233 )
00234 );
00235
00236 addPatchFields<volScalarField>
00237 (
00238 mesh,
00239 calculatedFvPatchField<scalar>::typeName
00240 );
00241 addPatchFields<volVectorField>
00242 (
00243 mesh,
00244 calculatedFvPatchField<vector>::typeName
00245 );
00246 addPatchFields<volSphericalTensorField>
00247 (
00248 mesh,
00249 calculatedFvPatchField<sphericalTensor>::typeName
00250 );
00251 addPatchFields<volSymmTensorField>
00252 (
00253 mesh,
00254 calculatedFvPatchField<symmTensor>::typeName
00255 );
00256 addPatchFields<volTensorField>
00257 (
00258 mesh,
00259 calculatedFvPatchField<tensor>::typeName
00260 );
00261
00262
00263
00264 addPatchFields<surfaceScalarField>
00265 (
00266 mesh,
00267 calculatedFvPatchField<scalar>::typeName
00268 );
00269 addPatchFields<surfaceVectorField>
00270 (
00271 mesh,
00272 calculatedFvPatchField<vector>::typeName
00273 );
00274 addPatchFields<surfaceSphericalTensorField>
00275 (
00276 mesh,
00277 calculatedFvPatchField<sphericalTensor>::typeName
00278 );
00279 addPatchFields<surfaceSymmTensorField>
00280 (
00281 mesh,
00282 calculatedFvPatchField<symmTensor>::typeName
00283 );
00284 addPatchFields<surfaceTensorField>
00285 (
00286 mesh,
00287 calculatedFvPatchField<tensor>::typeName
00288 );
00289
00290
00291
00292 labelList oldToNew(sz+1);
00293 for (label i = 0; i < insertPatchI; i++)
00294 {
00295 oldToNew[i] = i;
00296 }
00297
00298 for (label i = insertPatchI; i < sz; i++)
00299 {
00300 oldToNew[i] = i+1;
00301 }
00302
00303 oldToNew[sz] = insertPatchI;
00304
00305
00306 polyPatches.reorder(oldToNew);
00307 fvPatches.reorder(oldToNew);
00308
00309 reorderPatchFields<volScalarField>(mesh, oldToNew);
00310 reorderPatchFields<volVectorField>(mesh, oldToNew);
00311 reorderPatchFields<volSphericalTensorField>(mesh, oldToNew);
00312 reorderPatchFields<volSymmTensorField>(mesh, oldToNew);
00313 reorderPatchFields<volTensorField>(mesh, oldToNew);
00314 reorderPatchFields<surfaceScalarField>(mesh, oldToNew);
00315 reorderPatchFields<surfaceVectorField>(mesh, oldToNew);
00316 reorderPatchFields<surfaceSphericalTensorField>(mesh, oldToNew);
00317 reorderPatchFields<surfaceSymmTensorField>(mesh, oldToNew);
00318 reorderPatchFields<surfaceTensorField>(mesh, oldToNew);
00319
00320 return insertPatchI;
00321 }
00322
00323
00324
00325 void reorderPatches
00326 (
00327 fvMesh& mesh,
00328 const labelList& oldToNew,
00329 const label nNewPatches
00330 )
00331 {
00332 polyBoundaryMesh& polyPatches =
00333 const_cast<polyBoundaryMesh&>(mesh.boundaryMesh());
00334 fvBoundaryMesh& fvPatches = const_cast<fvBoundaryMesh&>(mesh.boundary());
00335
00336
00337 polyPatches.reorder(oldToNew);
00338 fvPatches.reorder(oldToNew);
00339
00340 reorderPatchFields<volScalarField>(mesh, oldToNew);
00341 reorderPatchFields<volVectorField>(mesh, oldToNew);
00342 reorderPatchFields<volSphericalTensorField>(mesh, oldToNew);
00343 reorderPatchFields<volSymmTensorField>(mesh, oldToNew);
00344 reorderPatchFields<volTensorField>(mesh, oldToNew);
00345 reorderPatchFields<surfaceScalarField>(mesh, oldToNew);
00346 reorderPatchFields<surfaceVectorField>(mesh, oldToNew);
00347 reorderPatchFields<surfaceSphericalTensorField>(mesh, oldToNew);
00348 reorderPatchFields<surfaceSymmTensorField>(mesh, oldToNew);
00349 reorderPatchFields<surfaceTensorField>(mesh, oldToNew);
00350
00351
00352 polyPatches.setSize(nNewPatches);
00353 fvPatches.setSize(nNewPatches);
00354 trimPatchFields<volScalarField>(mesh, nNewPatches);
00355 trimPatchFields<volVectorField>(mesh, nNewPatches);
00356 trimPatchFields<volSphericalTensorField>(mesh, nNewPatches);
00357 trimPatchFields<volSymmTensorField>(mesh, nNewPatches);
00358 trimPatchFields<volTensorField>(mesh, nNewPatches);
00359 trimPatchFields<surfaceScalarField>(mesh, nNewPatches);
00360 trimPatchFields<surfaceVectorField>(mesh, nNewPatches);
00361 trimPatchFields<surfaceSphericalTensorField>(mesh, nNewPatches);
00362 trimPatchFields<surfaceSymmTensorField>(mesh, nNewPatches);
00363 trimPatchFields<surfaceTensorField>(mesh, nNewPatches);
00364 }
00365
00366
00367 template<class GeoField>
00368 void subsetVolFields
00369 (
00370 const fvMesh& mesh,
00371 const fvMesh& subMesh,
00372 const labelList& cellMap,
00373 const labelList& faceMap
00374 )
00375 {
00376 const labelList patchMap(identity(mesh.boundaryMesh().size()));
00377
00378 HashTable<const GeoField*> fields
00379 (
00380 mesh.objectRegistry::lookupClass<GeoField>()
00381 );
00382 forAllConstIter(typename HashTable<const GeoField*>, fields, iter)
00383 {
00384 const GeoField& fld = *iter();
00385
00386 Info<< "Mapping field " << fld.name() << endl;
00387
00388 tmp<GeoField> tSubFld
00389 (
00390 fvMeshSubset::interpolate
00391 (
00392 fld,
00393 subMesh,
00394 patchMap,
00395 cellMap,
00396 faceMap
00397 )
00398 );
00399
00400
00401
00402 forAll(tSubFld().boundaryField(), patchI)
00403 {
00404 const fvPatchField<typename GeoField::value_type>& pfld =
00405 tSubFld().boundaryField()[patchI];
00406
00407 if
00408 (
00409 isA<calculatedFvPatchField<typename GeoField::value_type> >
00410 (pfld)
00411 )
00412 {
00413 tSubFld().boundaryField()[patchI] ==
00414 pTraits<typename GeoField::value_type>::zero;
00415 }
00416 }
00417
00418
00419 GeoField* subFld = tSubFld.ptr();
00420 subFld->rename(fld.name());
00421 subFld->writeOpt() = IOobject::AUTO_WRITE;
00422 subFld->store();
00423 }
00424 }
00425
00426
00427 template<class GeoField>
00428 void subsetSurfaceFields
00429 (
00430 const fvMesh& mesh,
00431 const fvMesh& subMesh,
00432 const labelList& faceMap
00433 )
00434 {
00435 const labelList patchMap(identity(mesh.boundaryMesh().size()));
00436
00437 HashTable<const GeoField*> fields
00438 (
00439 mesh.objectRegistry::lookupClass<GeoField>()
00440 );
00441 forAllConstIter(typename HashTable<const GeoField*>, fields, iter)
00442 {
00443 const GeoField& fld = *iter();
00444
00445 Info<< "Mapping field " << fld.name() << endl;
00446
00447 tmp<GeoField> tSubFld
00448 (
00449 fvMeshSubset::interpolate
00450 (
00451 fld,
00452 subMesh,
00453 patchMap,
00454 faceMap
00455 )
00456 );
00457
00458
00459
00460 forAll(tSubFld().boundaryField(), patchI)
00461 {
00462 const fvsPatchField<typename GeoField::value_type>& pfld =
00463 tSubFld().boundaryField()[patchI];
00464
00465 if
00466 (
00467 isA<calculatedFvsPatchField<typename GeoField::value_type> >
00468 (pfld)
00469 )
00470 {
00471 tSubFld().boundaryField()[patchI] ==
00472 pTraits<typename GeoField::value_type>::zero;
00473 }
00474 }
00475
00476
00477 GeoField* subFld = tSubFld.ptr();
00478 subFld->rename(fld.name());
00479 subFld->writeOpt() = IOobject::AUTO_WRITE;
00480 subFld->store();
00481 }
00482 }
00483
00484
00485 labelList getNonRegionCells(const labelList& cellRegion, const label regionI)
00486 {
00487 DynamicList<label> nonRegionCells(cellRegion.size());
00488 forAll(cellRegion, cellI)
00489 {
00490 if (cellRegion[cellI] != regionI)
00491 {
00492 nonRegionCells.append(cellI);
00493 }
00494 }
00495 return nonRegionCells.shrink();
00496 }
00497
00498
00499
00500
00501 void getInterfaceSizes
00502 (
00503 const polyMesh& mesh,
00504 const labelList& cellRegion,
00505 const bool sumParallel,
00506
00507 edgeList& interfaces,
00508 EdgeMap<label>& interfaceSizes
00509 )
00510 {
00511 forAll(mesh.faceNeighbour(), faceI)
00512 {
00513 label ownRegion = cellRegion[mesh.faceOwner()[faceI]];
00514 label neiRegion = cellRegion[mesh.faceNeighbour()[faceI]];
00515
00516 if (ownRegion != neiRegion)
00517 {
00518 edge interface
00519 (
00520 min(ownRegion, neiRegion),
00521 max(ownRegion, neiRegion)
00522 );
00523
00524 EdgeMap<label>::iterator iter = interfaceSizes.find(interface);
00525
00526 if (iter != interfaceSizes.end())
00527 {
00528 iter()++;
00529 }
00530 else
00531 {
00532 interfaceSizes.insert(interface, 1);
00533 }
00534 }
00535 }
00536
00537 if (sumParallel && Pstream::parRun())
00538 {
00539 if (Pstream::master())
00540 {
00541
00542 for
00543 (
00544 int slave=Pstream::firstSlave();
00545 slave<=Pstream::lastSlave();
00546 slave++
00547 )
00548 {
00549 IPstream fromSlave(Pstream::blocking, slave);
00550
00551 EdgeMap<label> slaveSizes(fromSlave);
00552
00553 forAllConstIter(EdgeMap<label>, slaveSizes, slaveIter)
00554 {
00555 EdgeMap<label>::iterator masterIter =
00556 interfaceSizes.find(slaveIter.key());
00557
00558 if (masterIter != interfaceSizes.end())
00559 {
00560 masterIter() += slaveIter();
00561 }
00562 else
00563 {
00564 interfaceSizes.insert(slaveIter.key(), slaveIter());
00565 }
00566 }
00567 }
00568
00569
00570 for
00571 (
00572 int slave=Pstream::firstSlave();
00573 slave<=Pstream::lastSlave();
00574 slave++
00575 )
00576 {
00577
00578 OPstream toSlave(Pstream::blocking, slave);
00579 toSlave << interfaceSizes;
00580 }
00581 }
00582 else
00583 {
00584
00585 {
00586 OPstream toMaster(Pstream::blocking, Pstream::master());
00587 toMaster << interfaceSizes;
00588 }
00589
00590 {
00591 IPstream fromMaster(Pstream::blocking, Pstream::master());
00592 fromMaster >> interfaceSizes;
00593 }
00594 }
00595 }
00596
00597
00598 interfaces = interfaceSizes.toc();
00599 if (sumParallel)
00600 {
00601 Pstream::scatter(interfaces);
00602 }
00603 }
00604
00605
00606
00607 autoPtr<mapPolyMesh> createRegionMesh
00608 (
00609 const regionSplit& cellRegion,
00610 const EdgeMap<label>& interfaceToPatch,
00611 const fvMesh& mesh,
00612 const label regionI,
00613 const word& regionName,
00614 autoPtr<fvMesh>& newMesh
00615 )
00616 {
00617
00618 {
00619 IOobject io
00620 (
00621 "fvSchemes",
00622 mesh.time().system(),
00623 regionName,
00624 mesh,
00625 IOobject::NO_READ,
00626 IOobject::NO_WRITE,
00627 false
00628 );
00629
00630 Info<< "Testing:" << io.objectPath() << endl;
00631
00632 if (!io.headerOk())
00633
00634 {
00635 Info<< "Writing dummy " << regionName/io.name() << endl;
00636 dictionary dummyDict;
00637 dictionary divDict;
00638 dummyDict.add("divSchemes", divDict);
00639 dictionary gradDict;
00640 dummyDict.add("gradSchemes", gradDict);
00641 dictionary laplDict;
00642 dummyDict.add("laplacianSchemes", laplDict);
00643
00644 IOdictionary(io, dummyDict).regIOobject::write();
00645 }
00646 }
00647 {
00648 IOobject io
00649 (
00650 "fvSolution",
00651 mesh.time().system(),
00652 regionName,
00653 mesh,
00654 IOobject::NO_READ,
00655 IOobject::NO_WRITE,
00656 false
00657 );
00658
00659 if (!io.headerOk())
00660
00661 {
00662 Info<< "Writing dummy " << regionName/io.name() << endl;
00663 dictionary dummyDict;
00664 IOdictionary(io, dummyDict).regIOobject::write();
00665 }
00666 }
00667
00668
00669
00670 polyTopoChange meshMod(mesh);
00671
00672
00673 removeCells cellRemover(mesh);
00674
00675
00676 labelList cellsToRemove(getNonRegionCells(cellRegion, regionI));
00677
00678
00679
00680
00681 labelList exposedFaces = cellRemover.getExposedFaces(cellsToRemove);
00682
00683 labelList exposedPatchIDs(exposedFaces.size());
00684 forAll(exposedFaces, i)
00685 {
00686 label faceI = exposedFaces[i];
00687
00688 if (!mesh.isInternalFace(faceI))
00689 {
00690 FatalErrorIn("createRegionMesh(..)")
00691 << "Exposed face:" << faceI << " is not an internal face."
00692 << " fc:" << mesh.faceCentres()[faceI]
00693 << exit(FatalError);
00694 }
00695
00696 label ownRegion = cellRegion[mesh.faceOwner()[faceI]];
00697 label neiRegion = cellRegion[mesh.faceNeighbour()[faceI]];
00698
00699 label otherRegion = -1;
00700
00701 if (ownRegion == regionI && neiRegion != regionI)
00702 {
00703 otherRegion = neiRegion;
00704 }
00705 else if (ownRegion != regionI && neiRegion == regionI)
00706 {
00707 otherRegion = ownRegion;
00708 }
00709 else
00710 {
00711 FatalErrorIn("createRegionMesh(..)")
00712 << "Exposed face:" << faceI
00713 << " fc:" << mesh.faceCentres()[faceI]
00714 << " has owner region " << ownRegion
00715 << " and neighbour region " << neiRegion
00716 << " when handling region:" << regionI
00717 << exit(FatalError);
00718 }
00719
00720 if (otherRegion != -1)
00721 {
00722 edge interface(regionI, otherRegion);
00723
00724
00725 if (regionI < otherRegion)
00726 {
00727 exposedPatchIDs[i] = interfaceToPatch[interface];
00728 }
00729 else
00730 {
00731 exposedPatchIDs[i] = interfaceToPatch[interface]+1;
00732 }
00733 }
00734 }
00735
00736
00737 cellRemover.setRefinement
00738 (
00739 cellsToRemove,
00740 exposedFaces,
00741 exposedPatchIDs,
00742 meshMod
00743 );
00744
00745 autoPtr<mapPolyMesh> map = meshMod.makeMesh
00746 (
00747 newMesh,
00748 IOobject
00749 (
00750 regionName,
00751 mesh.time().timeName(),
00752 mesh.time(),
00753 IOobject::NO_READ,
00754 IOobject::AUTO_WRITE
00755 ),
00756 mesh
00757 );
00758
00759 return map;
00760 }
00761
00762
00763 void createAndWriteRegion
00764 (
00765 const fvMesh& mesh,
00766 const regionSplit& cellRegion,
00767 const wordList& regionNames,
00768 const EdgeMap<label>& interfaceToPatch,
00769 const label regionI,
00770 const word& newMeshInstance
00771 )
00772 {
00773 Info<< "Creating mesh for region " << regionI
00774 << ' ' << regionNames[regionI] << endl;
00775
00776 autoPtr<fvMesh> newMesh;
00777 autoPtr<mapPolyMesh> map = createRegionMesh
00778 (
00779 cellRegion,
00780 interfaceToPatch,
00781 mesh,
00782 regionI,
00783 regionNames[regionI],
00784 newMesh
00785 );
00786
00787 Info<< "Mapping fields" << endl;
00788
00789
00790 newMesh().updateMesh(map());
00791
00792
00793 subsetVolFields<volScalarField>
00794 (
00795 mesh,
00796 newMesh(),
00797 map().cellMap(),
00798 map().faceMap()
00799 );
00800 subsetVolFields<volVectorField>
00801 (
00802 mesh,
00803 newMesh(),
00804 map().cellMap(),
00805 map().faceMap()
00806 );
00807 subsetVolFields<volSphericalTensorField>
00808 (
00809 mesh,
00810 newMesh(),
00811 map().cellMap(),
00812 map().faceMap()
00813 );
00814 subsetVolFields<volSymmTensorField>
00815 (
00816 mesh,
00817 newMesh(),
00818 map().cellMap(),
00819 map().faceMap()
00820 );
00821 subsetVolFields<volTensorField>
00822 (
00823 mesh,
00824 newMesh(),
00825 map().cellMap(),
00826 map().faceMap()
00827 );
00828
00829 subsetSurfaceFields<surfaceScalarField>
00830 (
00831 mesh,
00832 newMesh(),
00833 map().faceMap()
00834 );
00835 subsetSurfaceFields<surfaceVectorField>
00836 (
00837 mesh,
00838 newMesh(),
00839 map().faceMap()
00840 );
00841 subsetSurfaceFields<surfaceSphericalTensorField>
00842 (
00843 mesh,
00844 newMesh(),
00845 map().faceMap()
00846 );
00847 subsetSurfaceFields<surfaceSymmTensorField>
00848 (
00849 mesh,
00850 newMesh(),
00851 map().faceMap()
00852 );
00853 subsetSurfaceFields<surfaceTensorField>
00854 (
00855 mesh,
00856 newMesh(),
00857 map().faceMap()
00858 );
00859
00860
00861 const polyBoundaryMesh& newPatches = newMesh().boundaryMesh();
00862 newPatches.checkParallelSync(true);
00863
00864
00865
00866
00867
00868 labelList oldToNew(newPatches.size(), -1);
00869 label newI = 0;
00870
00871 Info<< "Deleting empty patches" << endl;
00872
00873
00874 forAll(newPatches, patchI)
00875 {
00876 const polyPatch& pp = newPatches[patchI];
00877
00878 if (!isA<processorPolyPatch>(pp))
00879 {
00880 if (returnReduce(pp.size(), sumOp<label>()) > 0)
00881 {
00882 oldToNew[patchI] = newI++;
00883 }
00884 }
00885 }
00886
00887
00888 forAll(newPatches, patchI)
00889 {
00890 const polyPatch& pp = newPatches[patchI];
00891
00892 if (isA<processorPolyPatch>(pp) && pp.size())
00893 {
00894 oldToNew[patchI] = newI++;
00895 }
00896 }
00897
00898 const label nNewPatches = newI;
00899
00900
00901 forAll(oldToNew, patchI)
00902 {
00903 if (oldToNew[patchI] == -1)
00904 {
00905 oldToNew[patchI] = newI++;
00906 }
00907 }
00908
00909 reorderPatches(newMesh(), oldToNew, nNewPatches);
00910
00911
00912 Info<< "Writing new mesh" << endl;
00913
00914 newMesh().setInstance(newMeshInstance);
00915 newMesh().write();
00916
00917
00918 Info<< "Writing addressing to base mesh" << endl;
00919
00920 labelIOList pointProcAddressing
00921 (
00922 IOobject
00923 (
00924 "pointRegionAddressing",
00925 newMesh().facesInstance(),
00926 newMesh().meshSubDir,
00927 newMesh(),
00928 IOobject::NO_READ,
00929 IOobject::NO_WRITE,
00930 false
00931 ),
00932 map().pointMap()
00933 );
00934 Info<< "Writing map " << pointProcAddressing.name()
00935 << " from region" << regionI
00936 << " points back to base mesh." << endl;
00937 pointProcAddressing.write();
00938
00939 labelIOList faceProcAddressing
00940 (
00941 IOobject
00942 (
00943 "faceRegionAddressing",
00944 newMesh().facesInstance(),
00945 newMesh().meshSubDir,
00946 newMesh(),
00947 IOobject::NO_READ,
00948 IOobject::NO_WRITE,
00949 false
00950 ),
00951 newMesh().nFaces()
00952 );
00953 forAll(faceProcAddressing, faceI)
00954 {
00955
00956
00957 label oldFaceI = map().faceMap()[faceI];
00958
00959 if
00960 (
00961 map().cellMap()[newMesh().faceOwner()[faceI]]
00962 == mesh.faceOwner()[oldFaceI]
00963 )
00964 {
00965 faceProcAddressing[faceI] = oldFaceI+1;
00966 }
00967 else
00968 {
00969 faceProcAddressing[faceI] = -(oldFaceI+1);
00970 }
00971 }
00972 Info<< "Writing map " << faceProcAddressing.name()
00973 << " from region" << regionI
00974 << " faces back to base mesh." << endl;
00975 faceProcAddressing.write();
00976
00977 labelIOList cellProcAddressing
00978 (
00979 IOobject
00980 (
00981 "cellRegionAddressing",
00982 newMesh().facesInstance(),
00983 newMesh().meshSubDir,
00984 newMesh(),
00985 IOobject::NO_READ,
00986 IOobject::NO_WRITE,
00987 false
00988 ),
00989 map().cellMap()
00990 );
00991 Info<< "Writing map " <<cellProcAddressing.name()
00992 << " from region" << regionI
00993 << " cells back to base mesh." << endl;
00994 cellProcAddressing.write();
00995 }
00996
00997
00998
00999
01000
01001
01002 EdgeMap<label> addRegionPatches
01003 (
01004 fvMesh& mesh,
01005 const regionSplit& cellRegion,
01006 const edgeList& interfaces,
01007 const EdgeMap<label>& interfaceSizes,
01008 const wordList& regionNames
01009 )
01010 {
01011
01012 mesh.boundaryMesh().checkParallelSync(true);
01013
01014 Info<< nl << "Adding patches" << nl << endl;
01015
01016 EdgeMap<label> interfaceToPatch(cellRegion.nRegions());
01017
01018 forAll(interfaces, interI)
01019 {
01020 const edge& e = interfaces[interI];
01021
01022 if (interfaceSizes[e] > 0)
01023 {
01024 label patchI = addPatch
01025 (
01026 mesh,
01027 regionNames[e[0]] + "_to_" + regionNames[e[1]],
01028 directMappedWallPolyPatch::typeName
01029 );
01030 addPatch
01031 (
01032 mesh,
01033 regionNames[e[1]] + "_to_" + regionNames[e[0]],
01034 directMappedWallPolyPatch::typeName
01035 );
01036
01037 Info<< "For interface between region " << e[0]
01038 << " and " << e[1] << " added patch " << patchI
01039 << " " << mesh.boundaryMesh()[patchI].name()
01040 << endl;
01041
01042 interfaceToPatch.insert(e, patchI);
01043 }
01044 }
01045 return interfaceToPatch;
01046 }
01047
01048
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107 label findCorrespondingRegion
01108 (
01109 const labelList& existingZoneID,
01110 const regionSplit& cellRegion,
01111 const label zoneI,
01112 const label minOverlapSize
01113 )
01114 {
01115
01116 labelList cellsInZone(cellRegion.nRegions(), 0);
01117
01118 forAll(cellRegion, cellI)
01119 {
01120 if (existingZoneID[cellI] == zoneI)
01121 {
01122 cellsInZone[cellRegion[cellI]]++;
01123 }
01124 }
01125
01126 Pstream::listCombineGather(cellsInZone, plusEqOp<label>());
01127 Pstream::listCombineScatter(cellsInZone);
01128
01129
01130 label regionI = findMax(cellsInZone);
01131
01132
01133 if (cellsInZone[regionI] < minOverlapSize)
01134 {
01135
01136 regionI = -1;
01137 }
01138 else
01139 {
01140
01141 forAll(cellRegion, cellI)
01142 {
01143 if (cellRegion[cellI] == regionI && existingZoneID[cellI] != zoneI)
01144 {
01145
01146 regionI = -1;
01147 break;
01148 }
01149 }
01150
01151
01152 reduce(regionI, minOp<label>());
01153 }
01154
01155 return regionI;
01156 }
01157
01158
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226 int main(int argc, char *argv[])
01227 {
01228 argList::validOptions.insert("cellZones", "");
01229 argList::validOptions.insert("blockedFaces", "faceSet");
01230 argList::validOptions.insert("makeCellZones", "");
01231 argList::validOptions.insert("largestOnly", "");
01232 argList::validOptions.insert("insidePoint", "point");
01233 argList::validOptions.insert("overwrite", "");
01234 argList::validOptions.insert("detectOnly", "");
01235 argList::validOptions.insert("sloppyCellZones", "");
01236
01237 # include "setRootCase.H"
01238 # include "createTime.H"
01239 runTime.functionObjects().off();
01240 # include "createMesh.H"
01241 const word oldInstance = mesh.pointsInstance();
01242
01243 word blockedFacesName;
01244 if (args.optionFound("blockedFaces"))
01245 {
01246 blockedFacesName = args.option("blockedFaces");
01247 Info<< "Reading blocked internal faces from faceSet "
01248 << blockedFacesName << nl << endl;
01249 }
01250
01251 bool makeCellZones = args.optionFound("makeCellZones");
01252 bool largestOnly = args.optionFound("largestOnly");
01253 bool insidePoint = args.optionFound("insidePoint");
01254 bool useCellZones = args.optionFound("cellZones");
01255 bool overwrite = args.optionFound("overwrite");
01256 bool detectOnly = args.optionFound("detectOnly");
01257 bool sloppyCellZones = args.optionFound("sloppyCellZones");
01258
01259 if (insidePoint && largestOnly)
01260 {
01261 FatalErrorIn(args.executable())
01262 << "You cannot specify both -largestOnly"
01263 << " (keep region with most cells)"
01264 << " and -insidePoint (keep region containing point)"
01265 << exit(FatalError);
01266 }
01267
01268
01269 const cellZoneMesh& cellZones = mesh.cellZones();
01270
01271
01272
01273
01274
01275
01276
01277
01278 labelList zoneID(mesh.nCells(), -1);
01279
01280 forAll(cellZones, zoneI)
01281 {
01282 const cellZone& cz = cellZones[zoneI];
01283
01284 forAll(cz, i)
01285 {
01286 label cellI = cz[i];
01287 if (zoneID[cellI] == -1)
01288 {
01289 zoneID[cellI] = zoneI;
01290 }
01291 else
01292 {
01293 FatalErrorIn(args.executable())
01294 << "Cell " << cellI << " with cell centre "
01295 << mesh.cellCentres()[cellI]
01296 << " is multiple zones. This is not allowed." << endl
01297 << "It is in zone " << cellZones[zoneID[cellI]].name()
01298 << " and in zone " << cellZones[zoneI].name()
01299 << exit(FatalError);
01300 }
01301 }
01302 }
01303
01304
01305 labelList neiZoneID(mesh.nFaces()-mesh.nInternalFaces());
01306
01307 forAll(neiZoneID, i)
01308 {
01309 neiZoneID[i] = zoneID[mesh.faceOwner()[i+mesh.nInternalFaces()]];
01310 }
01311 syncTools::swapBoundaryFaceList(mesh, neiZoneID, false);
01312
01313
01314
01315
01316
01317
01318 boolList blockedFace;
01319
01320
01321 if (blockedFacesName.size())
01322 {
01323 faceSet blockedFaceSet(mesh, blockedFacesName);
01324 Info<< "Read " << returnReduce(blockedFaceSet.size(), sumOp<label>())
01325 << " blocked faces from set " << blockedFacesName << nl << endl;
01326
01327 blockedFace.setSize(mesh.nFaces(), false);
01328
01329 forAllConstIter(faceSet, blockedFaceSet, iter)
01330 {
01331 blockedFace[iter.key()] = true;
01332 }
01333 }
01334
01335
01336 if (useCellZones)
01337 {
01338 blockedFace.setSize(mesh.nFaces(), false);
01339
01340 for (label faceI = 0; faceI < mesh.nInternalFaces(); faceI++)
01341 {
01342 label own = mesh.faceOwner()[faceI];
01343 label nei = mesh.faceNeighbour()[faceI];
01344
01345 if (zoneID[own] != zoneID[nei])
01346 {
01347 blockedFace[faceI] = true;
01348 }
01349 }
01350
01351
01352
01353 forAll(neiZoneID, i)
01354 {
01355 label faceI = i+mesh.nInternalFaces();
01356
01357 if (zoneID[mesh.faceOwner()[faceI]] != neiZoneID[i])
01358 {
01359
01360 FatalErrorIn(args.executable())
01361 << "Coupled face " << faceI
01362 << " fc:" << mesh.faceCentres()[faceI]
01363 << " has cellZone " << zoneID[mesh.faceOwner()[faceI]]
01364 << " on owner side but cellZone " << neiZoneID[i]
01365 << " on other side. This is not allowed."
01366 << exit(FatalError);
01367 }
01368 }
01369 }
01370
01371
01372
01373
01374
01375
01376 regionSplit cellRegion(mesh, blockedFace);
01377
01378 Info<< endl << "Number of regions:" << cellRegion.nRegions() << nl << endl;
01379
01380
01381
01382 {
01383 labelIOList cellToRegion
01384 (
01385 IOobject
01386 (
01387 "cellToRegion",
01388 mesh.facesInstance(),
01389 mesh,
01390 IOobject::NO_READ,
01391 IOobject::NO_WRITE,
01392 false
01393 ),
01394 cellRegion
01395 );
01396 cellToRegion.write();
01397
01398 Info<< "Writing region per cell file (for manual decomposition) to "
01399 << cellToRegion.objectPath() << nl << endl;
01400 }
01401
01402 {
01403 volScalarField cellToRegion
01404 (
01405 IOobject
01406 (
01407 "cellToRegion",
01408 mesh.facesInstance(),
01409 mesh,
01410 IOobject::NO_READ,
01411 IOobject::NO_WRITE,
01412 false
01413 ),
01414 mesh,
01415 dimensionedScalar("zero", dimless, 0)
01416 );
01417 forAll(cellRegion, cellI)
01418 {
01419 cellToRegion[cellI] = cellRegion[cellI];
01420 }
01421 cellToRegion.write();
01422
01423 Info<< "Writing region per cell as volScalarField to "
01424 << cellToRegion.objectPath() << nl << endl;
01425 }
01426
01427
01428
01429
01430
01431 labelList regionSizes(cellRegion.nRegions(), 0);
01432
01433 forAll(cellRegion, cellI)
01434 {
01435 regionSizes[cellRegion[cellI]]++;
01436 }
01437 forAll(regionSizes, regionI)
01438 {
01439 reduce(regionSizes[regionI], sumOp<label>());
01440 }
01441
01442 Info<< "Region\tCells" << nl
01443 << "------\t-----" << endl;
01444
01445 forAll(regionSizes, regionI)
01446 {
01447 Info<< regionI << '\t' << regionSizes[regionI] << nl;
01448 }
01449 Info<< endl;
01450
01451
01452
01453
01454
01455 labelList zoneSizes(cellZones.size(), 0);
01456 if (useCellZones || makeCellZones || sloppyCellZones)
01457 {
01458 List<wordList> zoneNames(Pstream::nProcs());
01459 zoneNames[Pstream::myProcNo()] = cellZones.names();
01460 Pstream::gatherList(zoneNames);
01461 Pstream::scatterList(zoneNames);
01462
01463 forAll(zoneNames, procI)
01464 {
01465 if (zoneNames[procI] != zoneNames[0])
01466 {
01467 FatalErrorIn(args.executable())
01468 << "cellZones not synchronised across processors." << endl
01469 << "Master has cellZones " << zoneNames[0] << endl
01470 << "Processor " << procI
01471 << " has cellZones " << zoneNames[procI]
01472 << exit(FatalError);
01473 }
01474 }
01475
01476 forAll(cellZones, zoneI)
01477 {
01478 zoneSizes[zoneI] = returnReduce
01479 (
01480 cellZones[zoneI].size(),
01481 sumOp<label>()
01482 );
01483 }
01484 }
01485
01486
01487
01488
01489
01490
01491 labelList regionToZone(cellRegion.nRegions(), -1);
01492
01493 wordList regionNames(cellRegion.nRegions());
01494
01495 labelList zoneToRegion(cellZones.size(), -1);
01496
01497 if (sloppyCellZones)
01498 {
01499 Info<< "Trying to match regions to existing cell zones;"
01500 << " region can be subset of cell zone." << nl << endl;
01501
01502 forAll(cellZones, zoneI)
01503 {
01504 label regionI = findCorrespondingRegion
01505 (
01506 zoneID,
01507 cellRegion,
01508 zoneI,
01509 label(0.5*zoneSizes[zoneI])
01510 );
01511
01512 if (regionI != -1)
01513 {
01514 Info<< "Sloppily matched region " << regionI
01515 << " size " << regionSizes[regionI]
01516 << " to zone " << zoneI << " size " << zoneSizes[zoneI]
01517 << endl;
01518 zoneToRegion[zoneI] = regionI;
01519 regionToZone[regionI] = zoneI;
01520 regionNames[regionI] = cellZones[zoneI].name();
01521 }
01522 }
01523 }
01524 else
01525 {
01526 Info<< "Trying to match regions to existing cell zones." << nl << endl;
01527
01528 forAll(cellZones, zoneI)
01529 {
01530 label regionI = findCorrespondingRegion
01531 (
01532 zoneID,
01533 cellRegion,
01534 zoneI,
01535 1
01536 );
01537
01538 if (regionI != -1)
01539 {
01540 zoneToRegion[zoneI] = regionI;
01541 regionToZone[regionI] = zoneI;
01542 regionNames[regionI] = cellZones[zoneI].name();
01543 }
01544 }
01545 }
01546
01547 forAll(regionToZone, regionI)
01548 {
01549 if (regionToZone[regionI] == -1)
01550 {
01551 regionNames[regionI] = "domain" + Foam::name(regionI);
01552 }
01553 }
01554
01555
01556
01557 Info<< "Region\tZone\tName" << nl
01558 << "------\t----\t----" << endl;
01559 forAll(regionToZone, regionI)
01560 {
01561 Info<< regionI << '\t' << regionToZone[regionI] << '\t'
01562 << regionNames[regionI] << nl;
01563 }
01564 Info<< endl;
01565
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580 mesh.boundaryMesh().checkParallelSync(true);
01581
01582
01583
01584
01585 edgeList interfaces;
01586 EdgeMap<label> interfaceSizes;
01587 getInterfaceSizes
01588 (
01589 mesh,
01590 cellRegion,
01591 true,
01592
01593 interfaces,
01594 interfaceSizes
01595 );
01596
01597 Info<< "Sizes inbetween regions:" << nl << nl
01598 << "Region\tRegion\tFaces" << nl
01599 << "------\t------\t-----" << endl;
01600
01601 forAll(interfaces, interI)
01602 {
01603 const edge& e = interfaces[interI];
01604
01605 Info<< e[0] << '\t' << e[1] << '\t' << interfaceSizes[e] << nl;
01606 }
01607 Info<< endl;
01608
01609
01610 if (detectOnly)
01611 {
01612 return 0;
01613 }
01614
01615
01616
01617
01618
01619 IOobjectList objects(mesh, runTime.timeName());
01620
01621
01622
01623 PtrList<volScalarField> vsFlds;
01624 ReadFields(mesh, objects, vsFlds);
01625
01626 PtrList<volVectorField> vvFlds;
01627 ReadFields(mesh, objects, vvFlds);
01628
01629 PtrList<volSphericalTensorField> vstFlds;
01630 ReadFields(mesh, objects, vstFlds);
01631
01632 PtrList<volSymmTensorField> vsymtFlds;
01633 ReadFields(mesh, objects, vsymtFlds);
01634
01635 PtrList<volTensorField> vtFlds;
01636 ReadFields(mesh, objects, vtFlds);
01637
01638
01639
01640 PtrList<surfaceScalarField> ssFlds;
01641 ReadFields(mesh, objects, ssFlds);
01642
01643 PtrList<surfaceVectorField> svFlds;
01644 ReadFields(mesh, objects, svFlds);
01645
01646 PtrList<surfaceSphericalTensorField> sstFlds;
01647 ReadFields(mesh, objects, sstFlds);
01648
01649 PtrList<surfaceSymmTensorField> ssymtFlds;
01650 ReadFields(mesh, objects, ssymtFlds);
01651
01652 PtrList<surfaceTensorField> stFlds;
01653 ReadFields(mesh, objects, stFlds);
01654
01655 Info<< endl;
01656
01657
01658
01659 if (cellRegion.nRegions() == 1)
01660 {
01661 Info<< "Only one region. Doing nothing." << endl;
01662 }
01663 else if (makeCellZones)
01664 {
01665 Info<< "Putting cells into cellZones instead of splitting mesh."
01666 << endl;
01667
01668
01669
01670 for (label regionI = 0; regionI < cellRegion.nRegions(); regionI++)
01671 {
01672 label zoneI = regionToZone[regionI];
01673
01674 if (zoneI != -1)
01675 {
01676 Info<< " Region " << regionI << " : corresponds to existing"
01677 << " cellZone "
01678 << zoneI << ' ' << cellZones[zoneI].name() << endl;
01679 }
01680 else
01681 {
01682
01683 labelList regionCells = findIndices(cellRegion, regionI);
01684
01685 word zoneName = "region" + Foam::name(regionI);
01686
01687 zoneI = cellZones.findZoneID(zoneName);
01688
01689 if (zoneI == -1)
01690 {
01691 zoneI = cellZones.size();
01692 mesh.cellZones().setSize(zoneI+1);
01693 mesh.cellZones().set
01694 (
01695 zoneI,
01696 new cellZone
01697 (
01698 zoneName,
01699 regionCells,
01700 zoneI,
01701 cellZones
01702 )
01703 );
01704 }
01705 else
01706 {
01707 mesh.cellZones()[zoneI].clearAddressing();
01708 mesh.cellZones()[zoneI] = regionCells;
01709 }
01710 Info<< " Region " << regionI << " : created new cellZone "
01711 << zoneI << ' ' << cellZones[zoneI].name() << endl;
01712 }
01713 }
01714 mesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
01715
01716 if (!overwrite)
01717 {
01718 runTime++;
01719 mesh.setInstance(runTime.timeName());
01720 }
01721 else
01722 {
01723 mesh.setInstance(oldInstance);
01724 }
01725
01726 Info<< "Writing cellZones as new mesh to time " << runTime.timeName()
01727 << nl << endl;
01728
01729 mesh.write();
01730
01731
01732
01733
01734
01735 Info<< "Writing cellSets corresponding to cellZones." << nl << endl;
01736
01737 forAll(cellZones, zoneI)
01738 {
01739 const cellZone& cz = cellZones[zoneI];
01740
01741 cellSet(mesh, cz.name(), cz).write();
01742 }
01743 }
01744 else
01745 {
01746
01747
01748
01749
01750 Info<< nl << "Adding patches" << nl << endl;
01751
01752 EdgeMap<label> interfaceToPatch
01753 (
01754 addRegionPatches
01755 (
01756 mesh,
01757 cellRegion,
01758 interfaces,
01759 interfaceSizes,
01760 regionNames
01761 )
01762 );
01763
01764
01765 if (!overwrite)
01766 {
01767 runTime++;
01768 }
01769
01770
01771
01772
01773
01774 if (insidePoint)
01775 {
01776 point insidePoint(args.optionLookup("insidePoint")());
01777
01778 label regionI = -1;
01779
01780 label cellI = mesh.findCell(insidePoint);
01781
01782 Info<< nl << "Found point " << insidePoint << " in cell " << cellI
01783 << endl;
01784
01785 if (cellI != -1)
01786 {
01787 regionI = cellRegion[cellI];
01788 }
01789
01790 reduce(regionI, maxOp<label>());
01791
01792 Info<< nl
01793 << "Subsetting region " << regionI
01794 << " containing point " << insidePoint << endl;
01795
01796 if (regionI == -1)
01797 {
01798 FatalErrorIn(args.executable())
01799 << "Point " << insidePoint
01800 << " is not inside the mesh." << nl
01801 << "Bounding box of the mesh:" << mesh.bounds()
01802 << exit(FatalError);
01803 }
01804
01805 createAndWriteRegion
01806 (
01807 mesh,
01808 cellRegion,
01809 regionNames,
01810 interfaceToPatch,
01811 regionI,
01812 (overwrite ? oldInstance : runTime.timeName())
01813 );
01814 }
01815 else if (largestOnly)
01816 {
01817 label regionI = findMax(regionSizes);
01818
01819 Info<< nl
01820 << "Subsetting region " << regionI
01821 << " of size " << regionSizes[regionI] << endl;
01822
01823 createAndWriteRegion
01824 (
01825 mesh,
01826 cellRegion,
01827 regionNames,
01828 interfaceToPatch,
01829 regionI,
01830 (overwrite ? oldInstance : runTime.timeName())
01831 );
01832 }
01833 else
01834 {
01835
01836 for (label regionI = 0; regionI < cellRegion.nRegions(); regionI++)
01837 {
01838 Info<< nl
01839 << "Region " << regionI << nl
01840 << "-------- " << endl;
01841
01842 createAndWriteRegion
01843 (
01844 mesh,
01845 cellRegion,
01846 regionNames,
01847 interfaceToPatch,
01848 regionI,
01849 (overwrite ? oldInstance : runTime.timeName())
01850 );
01851 }
01852 }
01853 }
01854
01855 Info<< "End\n" << endl;
01856
01857 return 0;
01858 }
01859
01860
01861