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

domainDecomposition.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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "domainDecomposition.H"
00028 #include "Time.H"
00029 #include "dictionary.H"
00030 #include "labelIOList.H"
00031 #include "processorPolyPatch.H"
00032 #include "fvMesh.H"
00033 #include "OSspecific.H"
00034 #include "Map.H"
00035 #include "globalMeshData.H"
00036 #include "DynamicList.H"
00037 
00038 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00039 
00040 void domainDecomposition::mark
00041 (
00042     const labelList& zoneElems,
00043     const label zoneI,
00044     labelList& elementToZone
00045 )
00046 {
00047     forAll(zoneElems, i)
00048     {
00049         label pointi = zoneElems[i];
00050 
00051         if (elementToZone[pointi] == -1)
00052         {
00053             // First occurrence
00054             elementToZone[pointi] = zoneI;
00055         }
00056         else if (elementToZone[pointi] >= 0)
00057         {
00058             // Multiple zones
00059             elementToZone[pointi] = -2;
00060         }
00061     }
00062 }
00063 
00064 
00065 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00066 
00067 // from components
00068 domainDecomposition::domainDecomposition(const IOobject& io)
00069 :
00070     fvMesh(io),
00071     decompositionDict_
00072     (
00073         IOobject
00074         (
00075             "decomposeParDict",
00076             time().system(),
00077             *this,
00078             IOobject::MUST_READ,
00079             IOobject::NO_WRITE
00080         )
00081     ),
00082     nProcs_(readInt(decompositionDict_.lookup("numberOfSubdomains"))),
00083     distributed_(false),
00084     cellToProc_(nCells()),
00085     procPointAddressing_(nProcs_),
00086     procFaceAddressing_(nProcs_),
00087     procCellAddressing_(nProcs_),
00088     procBoundaryAddressing_(nProcs_),
00089     procPatchSize_(nProcs_),
00090     procPatchStartIndex_(nProcs_),
00091     procNeighbourProcessors_(nProcs_),
00092     procProcessorPatchSize_(nProcs_),
00093     procProcessorPatchStartIndex_(nProcs_),
00094     globallySharedPoints_(0),
00095     cyclicParallel_(false)
00096 {
00097     if (decompositionDict_.found("distributed"))
00098     {
00099         Switch distributed(decompositionDict_.lookup("distributed"));
00100         distributed_ = distributed;
00101     }
00102 }
00103 
00104 
00105 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00106 
00107 domainDecomposition::~domainDecomposition()
00108 {}
00109 
00110 
00111 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00112 
00113 bool domainDecomposition::writeDecomposition()
00114 {
00115     Info<< "\nConstructing processor meshes" << endl;
00116 
00117     // Make a lookup map for globally shared points
00118     Map<label> sharedPointLookup(2*globallySharedPoints_.size());
00119 
00120     forAll (globallySharedPoints_, pointi)
00121     {
00122         sharedPointLookup.insert(globallySharedPoints_[pointi], pointi);
00123     }
00124 
00125 
00126     // Mark point/faces/cells that are in zones.
00127     // -1   : not in zone
00128     // -2   : in multiple zones
00129     // >= 0 : in single given zone
00130     // This will give direct lookup of elements that are in a single zone
00131     // and we'll only have to revert back to searching through all zones
00132     // for the duplicate elements
00133 
00134     // Point zones
00135     labelList pointToZone(points().size(), -1);
00136 
00137     forAll(pointZones(), zoneI)
00138     {
00139         mark(pointZones()[zoneI], zoneI, pointToZone);
00140     }
00141 
00142     // Face zones
00143     labelList faceToZone(faces().size(), -1);
00144 
00145     forAll(faceZones(), zoneI)
00146     {
00147         mark(faceZones()[zoneI], zoneI, faceToZone);
00148     }
00149 
00150     // Cell zones
00151     labelList cellToZone(nCells(), -1);
00152 
00153     forAll(cellZones(), zoneI)
00154     {
00155         mark(cellZones()[zoneI], zoneI, cellToZone);
00156     }
00157 
00158 
00159     label totProcFaces = 0;
00160     label maxProcPatches = 0;
00161     label maxProcFaces = 0;
00162 
00163 
00164     // Write out the meshes
00165     for (label procI = 0; procI < nProcs_; procI++)
00166     {
00167         // Create processor points
00168         const labelList& curPointLabels = procPointAddressing_[procI];
00169 
00170         const pointField& meshPoints = points();
00171 
00172         labelList pointLookup(nPoints(), -1);
00173 
00174         pointField procPoints(curPointLabels.size());
00175 
00176         forAll (curPointLabels, pointi)
00177         {
00178             procPoints[pointi] = meshPoints[curPointLabels[pointi]];
00179 
00180             pointLookup[curPointLabels[pointi]] = pointi;
00181         }
00182 
00183         // Create processor faces
00184         const labelList& curFaceLabels = procFaceAddressing_[procI];
00185 
00186         const faceList& meshFaces = faces();
00187 
00188         labelList faceLookup(nFaces(), -1);
00189 
00190         faceList procFaces(curFaceLabels.size());
00191 
00192         forAll (curFaceLabels, facei)
00193         {
00194             // Mark the original face as used
00195             // Remember to decrement the index by one (turning index)
00196             //
00197             label curF = mag(curFaceLabels[facei]) - 1;
00198 
00199             faceLookup[curF] = facei;
00200 
00201             // get the original face
00202             labelList origFaceLabels;
00203 
00204             if (curFaceLabels[facei] >= 0)
00205             {
00206                 // face not turned
00207                 origFaceLabels = meshFaces[curF];
00208             }
00209             else
00210             {
00211                 origFaceLabels = meshFaces[curF].reverseFace();
00212             }
00213 
00214             // translate face labels into local point list
00215             face& procFaceLabels = procFaces[facei];
00216 
00217             procFaceLabels.setSize(origFaceLabels.size());
00218 
00219             forAll (origFaceLabels, pointi)
00220             {
00221                 procFaceLabels[pointi] = pointLookup[origFaceLabels[pointi]];
00222             }
00223         }
00224 
00225         // Create processor cells
00226         const labelList& curCellLabels = procCellAddressing_[procI];
00227 
00228         const cellList& meshCells = cells();
00229 
00230         cellList procCells(curCellLabels.size());
00231 
00232         forAll (curCellLabels, celli)
00233         {
00234             const labelList& origCellLabels = meshCells[curCellLabels[celli]];
00235 
00236             cell& curCell = procCells[celli];
00237 
00238             curCell.setSize(origCellLabels.size());
00239 
00240             forAll (origCellLabels, cellFaceI)
00241             {
00242                 curCell[cellFaceI] = faceLookup[origCellLabels[cellFaceI]];
00243             }
00244         }
00245 
00246         // Create processor mesh without a boundary
00247 
00248         fileName processorCasePath
00249         (
00250             time().caseName()/fileName(word("processor") + Foam::name(procI))
00251         );
00252 
00253         // make the processor directory
00254         mkDir(time().rootPath()/processorCasePath);
00255 
00256         // create a database
00257         Time processorDb
00258         (
00259             Time::controlDictName,
00260             time().rootPath(),
00261             processorCasePath,
00262             "system",
00263             "constant"
00264         );
00265 
00266         // create the mesh
00267         polyMesh procMesh
00268         (
00269             IOobject
00270             (
00271                 this->polyMesh::name(),  // region name of undecomposed mesh
00272                 pointsInstance(),
00273                 processorDb
00274             ),
00275             xferMove(procPoints),
00276             xferMove(procFaces),
00277             xferMove(procCells)
00278         );
00279 
00280         // Create processor boundary patches
00281         const labelList& curBoundaryAddressing = procBoundaryAddressing_[procI];
00282 
00283         const labelList& curPatchSizes = procPatchSize_[procI];
00284 
00285         const labelList& curPatchStarts = procPatchStartIndex_[procI];
00286 
00287         const labelList& curNeighbourProcessors =
00288             procNeighbourProcessors_[procI];
00289 
00290         const labelList& curProcessorPatchSizes =
00291             procProcessorPatchSize_[procI];
00292 
00293         const labelList& curProcessorPatchStarts =
00294             procProcessorPatchStartIndex_[procI];
00295 
00296         const polyPatchList& meshPatches = boundaryMesh();
00297 
00298         List<polyPatch*> procPatches
00299         (
00300             curPatchSizes.size()
00301           + curProcessorPatchSizes.size(),
00302             reinterpret_cast<polyPatch*>(0)
00303         );
00304 
00305         label nPatches = 0;
00306 
00307         forAll (curPatchSizes, patchi)
00308         {
00309             procPatches[nPatches] =
00310                 meshPatches[curBoundaryAddressing[patchi]].clone
00311                 (
00312                     procMesh.boundaryMesh(),
00313                     nPatches,
00314                     curPatchSizes[patchi],
00315                     curPatchStarts[patchi]
00316                 ).ptr();
00317 
00318             nPatches++;
00319         }
00320 
00321         forAll (curProcessorPatchSizes, procPatchI)
00322         {
00323             procPatches[nPatches] =
00324                 new processorPolyPatch
00325                 (
00326                     word("procBoundary") + Foam::name(procI)
00327                   + word("to")
00328                   + Foam::name(curNeighbourProcessors[procPatchI]),
00329                     curProcessorPatchSizes[procPatchI],
00330                     curProcessorPatchStarts[procPatchI],
00331                     nPatches,
00332                     procMesh.boundaryMesh(),
00333                     procI,
00334                     curNeighbourProcessors[procPatchI]
00335             );
00336 
00337             nPatches++;
00338         }
00339 
00340         // Add boundary patches
00341         procMesh.addPatches(procPatches);
00342 
00343         // Create and add zones
00344 
00345         // Point zones
00346         {
00347             const pointZoneMesh& pz = pointZones();
00348 
00349             // Go through all the zoned points and find out if they
00350             // belong to a zone.  If so, add it to the zone as
00351             // necessary
00352             List<DynamicList<label> > zonePoints(pz.size());
00353 
00354             // Estimate size
00355             forAll(zonePoints, zoneI)
00356             {
00357                 zonePoints[zoneI].setCapacity(pz[zoneI].size() / nProcs_);
00358             }
00359 
00360             // Use the pointToZone map to find out the single zone (if any),
00361             // use slow search only for shared points.
00362             forAll (curPointLabels, pointi)
00363             {
00364                 label curPoint = curPointLabels[pointi];
00365 
00366                 label zoneI = pointToZone[curPoint];
00367 
00368                 if (zoneI >= 0)
00369                 {
00370                     // Single zone.
00371                     zonePoints[zoneI].append(pointi);
00372                 }
00373                 else if (zoneI == -2)
00374                 {
00375                     // Multiple zones. Lookup.
00376                     forAll(pz, zoneI)
00377                     {
00378                         label index = pz[zoneI].whichPoint(curPoint);
00379 
00380                         if (index != -1)
00381                         {
00382                             zonePoints[zoneI].append(pointi);
00383                         }
00384                     }
00385                 }
00386             }
00387 
00388             procMesh.pointZones().clearAddressing();
00389             procMesh.pointZones().setSize(zonePoints.size());
00390             forAll(zonePoints, zoneI)
00391             {
00392                 procMesh.pointZones().set
00393                 (
00394                     zoneI,
00395                     pz[zoneI].clone
00396                     (
00397                         procMesh.pointZones(),
00398                         zoneI,
00399                         zonePoints[zoneI].shrink()
00400                     )
00401                 );
00402             }
00403 
00404             if (pz.size())
00405             {
00406                 // Force writing on all processors
00407                 procMesh.pointZones().writeOpt() = IOobject::AUTO_WRITE;
00408             }
00409         }
00410 
00411         // Face zones
00412         {
00413             const faceZoneMesh& fz = faceZones();
00414 
00415             // Go through all the zoned face and find out if they
00416             // belong to a zone.  If so, add it to the zone as
00417             // necessary
00418             List<DynamicList<label> > zoneFaces(fz.size());
00419             List<DynamicList<bool> > zoneFaceFlips(fz.size());
00420 
00421             // Estimate size
00422             forAll(zoneFaces, zoneI)
00423             {
00424                 label procSize = fz[zoneI].size() / nProcs_;
00425 
00426                 zoneFaces[zoneI].setCapacity(procSize);
00427                 zoneFaceFlips[zoneI].setCapacity(procSize);
00428             }
00429 
00430             // Go through all the zoned faces and find out if they
00431             // belong to a zone.  If so, add it to the zone as
00432             // necessary
00433             forAll (curFaceLabels, facei)
00434             {
00435                 // Remember to decrement the index by one (turning index)
00436                 //
00437                 label curF = mag(curFaceLabels[facei]) - 1;
00438 
00439                 label zoneI = faceToZone[curF];
00440 
00441                 if (zoneI >= 0)
00442                 {
00443                     // Single zone. Add the face
00444                     zoneFaces[zoneI].append(facei);
00445 
00446                     label index = fz[zoneI].whichFace(curF);
00447 
00448                     bool flip = fz[zoneI].flipMap()[index];
00449 
00450                     if (curFaceLabels[facei] < 0)
00451                     {
00452                         flip = !flip;
00453                     }
00454 
00455                     zoneFaceFlips[zoneI].append(flip);
00456                 }
00457                 else if (zoneI == -2)
00458                 {
00459                     // Multiple zones. Lookup.
00460                     forAll(fz, zoneI)
00461                     {
00462                         label index = fz[zoneI].whichFace(curF);
00463 
00464                         if (index != -1)
00465                         {
00466                             zoneFaces[zoneI].append(facei);
00467 
00468                             bool flip = fz[zoneI].flipMap()[index];
00469 
00470                             if (curFaceLabels[facei] < 0)
00471                             {
00472                                 flip = !flip;
00473                             }
00474 
00475                             zoneFaceFlips[zoneI].append(flip);
00476                         }
00477                     }
00478                 }
00479             }
00480 
00481             procMesh.faceZones().clearAddressing();
00482             procMesh.faceZones().setSize(zoneFaces.size());
00483             forAll(zoneFaces, zoneI)
00484             {
00485                 procMesh.faceZones().set
00486                 (
00487                     zoneI,
00488                     fz[zoneI].clone
00489                     (
00490                         zoneFaces[zoneI].shrink(),          // addressing
00491                         zoneFaceFlips[zoneI].shrink(),      // flipmap
00492                         zoneI,
00493                         procMesh.faceZones()
00494                     )
00495                 );
00496             }
00497 
00498             if (fz.size())
00499             {
00500                 // Force writing on all processors
00501                 procMesh.faceZones().writeOpt() = IOobject::AUTO_WRITE;
00502             }
00503         }
00504 
00505         // Cell zones
00506         {
00507             const cellZoneMesh& cz = cellZones();
00508 
00509             // Go through all the zoned cells and find out if they
00510             // belong to a zone.  If so, add it to the zone as
00511             // necessary
00512             List<DynamicList<label> > zoneCells(cz.size());
00513 
00514             // Estimate size
00515             forAll(zoneCells, zoneI)
00516             {
00517                 zoneCells[zoneI].setCapacity(cz[zoneI].size() / nProcs_);
00518             }
00519 
00520             forAll (curCellLabels, celli)
00521             {
00522                 label curCellI = curCellLabels[celli];
00523 
00524                 label zoneI = cellToZone[curCellI];
00525 
00526                 if (zoneI >= 0)
00527                 {
00528                     // Single zone.
00529                     zoneCells[zoneI].append(celli);
00530                 }
00531                 else if (zoneI == -2)
00532                 {
00533                     // Multiple zones. Lookup.
00534                     forAll(cz, zoneI)
00535                     {
00536                         label index = cz[zoneI].whichCell(curCellI);
00537 
00538                         if (index != -1)
00539                         {
00540                             zoneCells[zoneI].append(celli);
00541                         }
00542                     }
00543                 }
00544             }
00545 
00546             procMesh.cellZones().clearAddressing();
00547             procMesh.cellZones().setSize(zoneCells.size());
00548             forAll(zoneCells, zoneI)
00549             {
00550                 procMesh.cellZones().set
00551                 (
00552                     zoneI,
00553                     cz[zoneI].clone
00554                     (
00555                         zoneCells[zoneI].shrink(),
00556                         zoneI,
00557                         procMesh.cellZones()
00558                     )
00559                 );
00560             }
00561 
00562             if (cz.size())
00563             {
00564                 // Force writing on all processors
00565                 procMesh.cellZones().writeOpt() = IOobject::AUTO_WRITE;
00566             }
00567         }
00568 
00569         // Set the precision of the points data to 10
00570         IOstream::defaultPrecision(10);
00571 
00572         procMesh.write();
00573 
00574         Info<< endl
00575             << "Processor " << procI << nl
00576             << "    Number of cells = " << procMesh.nCells()
00577             << endl;
00578 
00579         label nBoundaryFaces = 0;
00580         label nProcPatches = 0;
00581         label nProcFaces = 0;
00582 
00583         forAll (procMesh.boundaryMesh(), patchi)
00584         {
00585             if
00586             (
00587                 procMesh.boundaryMesh()[patchi].type()
00588              == processorPolyPatch::typeName
00589             )
00590             {
00591                 const processorPolyPatch& ppp =
00592                 refCast<const processorPolyPatch>
00593                 (
00594                     procMesh.boundaryMesh()[patchi]
00595                 );
00596 
00597                 Info<< "    Number of faces shared with processor "
00598                     << ppp.neighbProcNo() << " = " << ppp.size() << endl;
00599 
00600                 nProcPatches++;
00601                 nProcFaces += ppp.size();
00602             }
00603             else
00604             {
00605                 nBoundaryFaces += procMesh.boundaryMesh()[patchi].size();
00606             }
00607         }
00608 
00609         Info<< "    Number of processor patches = " << nProcPatches << nl
00610             << "    Number of processor faces = " << nProcFaces << nl
00611             << "    Number of boundary faces = " << nBoundaryFaces << endl;
00612 
00613         totProcFaces += nProcFaces;
00614         maxProcPatches = max(maxProcPatches, nProcPatches);
00615         maxProcFaces = max(maxProcFaces, nProcFaces);
00616 
00617         // create and write the addressing information
00618         labelIOList pointProcAddressing
00619         (
00620             IOobject
00621             (
00622                 "pointProcAddressing",
00623                 procMesh.facesInstance(),
00624                 procMesh.meshSubDir,
00625                 procMesh,
00626                 IOobject::NO_READ,
00627                 IOobject::NO_WRITE
00628             ),
00629             procPointAddressing_[procI]
00630         );
00631         pointProcAddressing.write();
00632 
00633         labelIOList faceProcAddressing
00634         (
00635             IOobject
00636             (
00637                 "faceProcAddressing",
00638                 procMesh.facesInstance(),
00639                 procMesh.meshSubDir,
00640                 procMesh,
00641                 IOobject::NO_READ,
00642                 IOobject::NO_WRITE
00643             ),
00644             procFaceAddressing_[procI]
00645         );
00646         faceProcAddressing.write();
00647 
00648         labelIOList cellProcAddressing
00649         (
00650             IOobject
00651             (
00652                 "cellProcAddressing",
00653                 procMesh.facesInstance(),
00654                 procMesh.meshSubDir,
00655                 procMesh,
00656                 IOobject::NO_READ,
00657                 IOobject::NO_WRITE
00658             ),
00659             procCellAddressing_[procI]
00660         );
00661         cellProcAddressing.write();
00662 
00663         labelIOList boundaryProcAddressing
00664         (
00665             IOobject
00666             (
00667                 "boundaryProcAddressing",
00668                 procMesh.facesInstance(),
00669                 procMesh.meshSubDir,
00670                 procMesh,
00671                 IOobject::NO_READ,
00672                 IOobject::NO_WRITE
00673             ),
00674             procBoundaryAddressing_[procI]
00675         );
00676         boundaryProcAddressing.write();
00677     }
00678 
00679     Info<< nl
00680         << "Number of processor faces = " << totProcFaces/2 << nl
00681         << "Max number of processor patches = " << maxProcPatches << nl
00682         << "Max number of faces between processors = " << maxProcFaces
00683         << endl;
00684 
00685     return true;
00686 }
00687 
00688 
00689 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd