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

splitMeshRegions.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 Description
00026     Splits mesh into multiple regions.
00027 
00028     Each region is defined as a domain whose cells can all be reached by
00029     cell-face-cell walking without crossing
00030     - boundary faces
00031     - additional faces from faceset (-blockedFaces faceSet).
00032     - any face inbetween differing cellZones (-cellZones)
00033 
00034     Output is:
00035     - mesh with multiple regions or
00036     - mesh with cells put into cellZones (-makeCellZones)
00037 
00038     Note:
00039     - Should work in parallel but cellZone interfaces cannot align with
00040     processor boundaries so use the correct option in decomposition to
00041     preserve those interfaces.
00042     - If a cell zone gets split into more than one region it can detect
00043     the largest matching region (-sloppyCellZones). This will accept any
00044     region that covers more than 50% of the zone. It has to be a subset
00045     so cannot have any cells in any other zone.
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 // Remove last patch field
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 // Reorder patch field
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 // Adds patch if not yet there. Returns patchID.
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             // Already there
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     // Below is all quite a hack. Feel free to change once there is a better
00201     // mechanism to insert and reorder patches.
00202 
00203     // Clear local fields and e.g. polyMesh parallelInfo.
00204     mesh.clearOut();
00205 
00206     label sz = polyPatches.size();
00207 
00208     fvBoundaryMesh& fvPatches = const_cast<fvBoundaryMesh&>(mesh.boundary());
00209 
00210     // Add polyPatch at the end
00211     polyPatches.setSize(sz+1);
00212     polyPatches.set
00213     (
00214         sz,
00215         polyPatch::New
00216         (
00217             patchType,
00218             patchName,
00219             0,              // size
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],  // point to newly added polyPatch
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     // Surface fields
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     // Create reordering list
00291     // patches before insert position stay as is
00292     labelList oldToNew(sz+1);
00293     for (label i = 0; i < insertPatchI; i++)
00294     {
00295         oldToNew[i] = i;
00296     }
00297     // patches after insert position move one up
00298     for (label i = insertPatchI; i < sz; i++)
00299     {
00300         oldToNew[i] = i+1;
00301     }
00302     // appended patch gets moved to insert position
00303     oldToNew[sz] = insertPatchI;
00304 
00305     // Shuffle into place
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 // Reorder and delete patches.
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     // Shuffle into place
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     // Remove last.
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         // Hack: set value to 0 for introduced patches (since don't
00401         //       get initialised.
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         // Store on subMesh
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         // Hack: set value to 0 for introduced patches (since don't
00459         //       get initialised.
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         // Store on subMesh
00477         GeoField* subFld = tSubFld.ptr();
00478         subFld->rename(fld.name());
00479         subFld->writeOpt() = IOobject::AUTO_WRITE;
00480         subFld->store();
00481     }
00482 }
00483 
00484 // Select all cells not in the region
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 // Get per region-region interface the sizes. If sumParallel sums sizes.
00500 // Returns interfaces as straight list for looping in identical order.
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             // Receive and add to my sizes
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             // Distribute
00570             for
00571             (
00572                 int slave=Pstream::firstSlave();
00573                 slave<=Pstream::lastSlave();
00574                 slave++
00575             )
00576             {
00577                 // Receive the edges using shared points from the slave.
00578                 OPstream toSlave(Pstream::blocking, slave);
00579                 toSlave << interfaceSizes;
00580             }
00581         }
00582         else
00583         {
00584             // Send to master
00585             {
00586                 OPstream toMaster(Pstream::blocking, Pstream::master());
00587                 toMaster << interfaceSizes;
00588             }
00589             // Receive from master
00590             {
00591                 IPstream fromMaster(Pstream::blocking, Pstream::master());
00592                 fromMaster >> interfaceSizes;
00593             }
00594         }
00595     }
00596 
00597     // Make sure all processors have interfaces in same order
00598     interfaces = interfaceSizes.toc();
00599     if (sumParallel)
00600     {
00601         Pstream::scatter(interfaces);
00602     }
00603 }
00604 
00605 
00606 // Create mesh for region.
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     // Create dummy system/fv*
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         // if (!exists(io.objectPath()))
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         //if (!exists(io.objectPath()))
00661         {
00662             Info<< "Writing dummy " << regionName/io.name() << endl;
00663             dictionary dummyDict;
00664             IOdictionary(io, dummyDict).regIOobject::write();
00665         }
00666     }
00667 
00668 
00669     // Topology change container. Start off from existing mesh.
00670     polyTopoChange meshMod(mesh);
00671 
00672     // Cell remover engine
00673     removeCells cellRemover(mesh);
00674 
00675     // Select all but region cells
00676     labelList cellsToRemove(getNonRegionCells(cellRegion, regionI));
00677 
00678     // Find out which faces will get exposed. Note that this
00679     // gets faces in mesh face order. So both regions will get same
00680     // face in same order (important!)
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             // Find the patch.
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     // Remove faces
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     // Map existing fields
00790     newMesh().updateMesh(map());
00791 
00792     // Add subsetted fields
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     // Delete empty patches
00865     // ~~~~~~~~~~~~~~~~~~~~
00866 
00867     // Create reordering list to move patches-to-be-deleted to end
00868     labelList oldToNew(newPatches.size(), -1);
00869     label newI = 0;
00870 
00871     Info<< "Deleting empty patches" << endl;
00872 
00873     // Assumes all non-proc boundaries are on all processors!
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     // Same for processor patches (but need no reduction)
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     // Move all deleteable patches to the end
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     // Write addressing files like decomposePar
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         // face + turning index. (see decomposePar)
00956         // Is the face pointing in the same direction?
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 // Create for every region-region interface with non-zero size two patches.
00999 // First one is for minimumregion to maximumregion.
01000 // Note that patches get created in same order on all processors (if parallel)
01001 // since looping over synchronised 'interfaces'.
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     // Check that all patches are present in same order.
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 //label findCorrespondingSubZone
01052 //(
01053 //    const cellZoneMesh& cellZones,
01054 //    const labelList& existingZoneID,
01055 //    const labelList& cellRegion,
01056 //    const label regionI
01057 //)
01058 //{
01059 //    // Zone corresponding to region. No corresponding zone.
01060 //    label zoneI = labelMax;
01061 //
01062 //    labelList regionCells = findIndices(cellRegion, regionI);
01063 //
01064 //    if (regionCells.empty())
01065 //    {
01066 //        // My local portion is empty. Maps to any empty cellZone. Mark with
01067 //        // special value which can get overwritten by other processors.
01068 //        zoneI = -1;
01069 //    }
01070 //    else
01071 //    {
01072 //        // Get zone for first element.
01073 //        zoneI = existingZoneID[regionCells[0]];
01074 //
01075 //        if (zoneI == -1)
01076 //        {
01077 //            zoneI = labelMax;
01078 //        }
01079 //        else
01080 //        {
01081 //            // 1. All regionCells in zoneI?
01082 //            forAll(regionCells, i)
01083 //            {
01084 //                if (existingZoneID[regionCells[i]] != zoneI)
01085 //                {
01086 //                    zoneI = labelMax;
01087 //                    break;
01088 //                }
01089 //            }
01090 //        }
01091 //    }
01092 //
01093 //    // Determine same zone over all processors.
01094 //    reduce(zoneI, maxOp<label>());
01095 //
01096 //    if (zoneI == labelMax)
01097 //    {
01098 //        // Cells in region that are not in zoneI
01099 //        zoneI = -1;
01100 //    }
01101 //
01102 //    return zoneI;
01103 //}
01104 
01105 
01106 // Find region that covers most of cell zone
01107 label findCorrespondingRegion
01108 (
01109     const labelList& existingZoneID,    // per cell the (unique) zoneID
01110     const regionSplit& cellRegion,
01111     const label zoneI,
01112     const label minOverlapSize
01113 )
01114 {
01115     // Per region the number of cells in zoneI
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     // Pick region with largest overlap of zoneI
01130     label regionI = findMax(cellsInZone);
01131 
01132 
01133     if (cellsInZone[regionI] < minOverlapSize)
01134     {
01135         // Region covers too little of zone. Not good enough.
01136         regionI = -1;
01137     }
01138     else
01139     {
01140         // Check that region contains no cells that aren't in cellZone.
01141         forAll(cellRegion, cellI)
01142         {
01143             if (cellRegion[cellI] == regionI && existingZoneID[cellI] != zoneI)
01144             {
01145                 // cellI in regionI but not in zoneI
01146                 regionI = -1;
01147                 break;
01148             }
01149         }
01150         // If one in error, all should be in error. Note that branch gets taken
01151         // on all procs.
01152         reduce(regionI, minOp<label>());
01153     }
01154 
01155     return regionI;
01156 }
01157 
01158 
01160 //label findCorrespondingRegion
01161 //(
01162 //    const cellZoneMesh& cellZones,
01163 //    const labelList& existingZoneID,    // per cell the (unique) zoneID
01164 //    const regionSplit& cellRegion,
01165 //    const label zoneI
01166 //)
01167 //{
01168 //    // Region corresponding to zone. Start off with special value: no
01169 //    // corresponding region.
01170 //    label regionI = labelMax;
01171 //
01172 //    const cellZone& cz = cellZones[zoneI];
01173 //
01174 //    if (cz.empty())
01175 //    {
01176 //        // My local portion is empty. Maps to any empty cellZone. Mark with
01177 //        // special value which can get overwritten by other processors.
01178 //        regionI = -1;
01179 //    }
01180 //    else
01181 //    {
01182 //        regionI = cellRegion[cz[0]];
01183 //
01184 //        forAll(cz, i)
01185 //        {
01186 //            if (cellRegion[cz[i]] != regionI)
01187 //            {
01188 //                regionI = labelMax;
01189 //                break;
01190 //            }
01191 //        }
01192 //    }
01193 //
01194 //    // Determine same zone over all processors.
01195 //    reduce(regionI, maxOp<label>());
01196 //
01197 //
01198 //    // 2. All of region present?
01199 //
01200 //    if (regionI == labelMax)
01201 //    {
01202 //        regionI = -1;
01203 //    }
01204 //    else if (regionI != -1)
01205 //    {
01206 //        forAll(cellRegion, cellI)
01207 //        {
01208 //            if (cellRegion[cellI] == regionI && existingZoneID[cellI] != zoneI)
01209 //            {
01210 //                // cellI in regionI but not in zoneI
01211 //                regionI = -1;
01212 //                break;
01213 //            }
01214 //        }
01215 //        // If one in error, all should be in error. Note that branch gets taken
01216 //        // on all procs.
01217 //        reduce(regionI, minOp<label>());
01218 //    }
01219 //
01220 //    return regionI;
01221 //}
01222 
01223 
01224 // Main program:
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     // Collect zone per cell
01273     // ~~~~~~~~~~~~~~~~~~~~~
01274     // - non-unique zoning
01275     // - coupled zones
01276 
01277     // Existing zoneID
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     // Neighbour zoneID.
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     // Determine connected regions
01315     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
01316 
01317     // Mark additional faces that are blocked
01318     boolList blockedFace;
01319 
01320     // Read from faceSet
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     // Imply from differing cellZones
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         // Different cellZones on either side of processor patch are not
01352         // allowed for now. Convert to processorPatches or what?
01353         forAll(neiZoneID, i)
01354         {
01355             label faceI = i+mesh.nInternalFaces();
01356 
01357             if (zoneID[mesh.faceOwner()[faceI]] != neiZoneID[i])
01358             {
01359                 //blockedFace[faceI] = true;
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     // Do the topological walk to determine regions
01373     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01374 
01375     // regionSplit is the labelList with the region per cell.
01376     regionSplit cellRegion(mesh, blockedFace);
01377 
01378     Info<< endl << "Number of regions:" << cellRegion.nRegions() << nl << endl;
01379 
01380 
01381     // Write to manual decomposition option
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     // Write for postprocessing
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     // Sizes per region
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     // Sizes per cellzone
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     // Whether region corresponds to a cellzone
01488     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01489 
01490     // Region per zone
01491     labelList regionToZone(cellRegion.nRegions(), -1);
01492     // Name of region
01493     wordList regionNames(cellRegion.nRegions());
01494     // Zone to region
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]) // minimum overlap
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               // minimum overlap
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     // Allocate region names for unmatched regions.
01547     forAll(regionToZone, regionI)
01548     {
01549         if (regionToZone[regionI] == -1)
01550         {
01551             regionNames[regionI] = "domain" + Foam::name(regionI);
01552         }
01553     }
01554 
01555 
01556     // Print region to zone
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     //Info<< "Zone\tName\tRegion" << nl
01568     //    << "----\t----\t------" << endl;
01569     //forAll(zoneToRegion, zoneI)
01570     //{
01571     //    Info<< zoneI << '\t' << cellZones[zoneI].name() << '\t'
01572     //        << zoneToRegion[zoneI] << nl;
01573     //}
01574     //Info<< endl;
01575 
01576 
01577 
01578     // Since we're going to mess with patches make sure all non-processor ones
01579     // are on all processors.
01580     mesh.boundaryMesh().checkParallelSync(true);
01581 
01582 
01583     // Sizes of interface between regions. From pair of regions to number of
01584     // faces.
01585     edgeList interfaces;
01586     EdgeMap<label> interfaceSizes;
01587     getInterfaceSizes
01588     (
01589         mesh,
01590         cellRegion,
01591         true,      // sum in parallel?
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     // Read objects in time directory
01617     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01618 
01619     IOobjectList objects(mesh, runTime.timeName());
01620 
01621     // Read vol fields.
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     // Read surface fields.
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         // Check if region overlaps with existing zone. If so keep.
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                 // Create new cellZone.
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,           //name
01699                             regionCells,        //addressing
01700                             zoneI,              //index
01701                             cellZones           //cellZoneMesh
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         // Write cellSets for convenience
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         // Add patches for interfaces
01747         // ~~~~~~~~~~~~~~~~~~~~~~~~~~
01748 
01749         // Add all possible patches. Empty ones get filtered later on.
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         // Create regions
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             // Split all
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 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd