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

parMetisDecomp.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 "syncTools.H"
00028 #include "parMetisDecomp.H"
00029 #include "metisDecomp.H"
00030 #include "addToRunTimeSelectionTable.H"
00031 #include "floatScalar.H"
00032 #include "polyMesh.H"
00033 #include "Time.H"
00034 #include "labelIOField.H"
00035 #include "globalIndex.H"
00036 
00037 #include <mpi.h>
00038 
00039 extern "C"
00040 {
00041 #   include "parmetis.h"
00042 }
00043 
00044 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00045 
00046 namespace Foam
00047 {
00048     defineTypeNameAndDebug(parMetisDecomp, 0);
00049 
00050     addToRunTimeSelectionTable
00051     (
00052         decompositionMethod,
00053         parMetisDecomp,
00054         dictionaryMesh
00055     );
00056 }
00057 
00058 
00059 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00060 
00061 //- Does prevention of 0 cell domains and calls parmetis.
00062 Foam::label Foam::parMetisDecomp::decompose
00063 (
00064     Field<int>& xadj,
00065     Field<int>& adjncy,
00066     const pointField& cellCentres,
00067     Field<int>& cellWeights,
00068     Field<int>& faceWeights,
00069     const List<int>& options,
00070 
00071     List<int>& finalDecomp
00072 )
00073 {
00074     // C style numbering
00075     int numFlag = 0;
00076 
00077     // Number of dimensions
00078     int nDims = 3;
00079 
00080 
00081     if (cellCentres.size() != xadj.size()-1)
00082     {
00083         FatalErrorIn("parMetisDecomp::decompose(..)")
00084             << "cellCentres:" << cellCentres.size()
00085             << " xadj:" << xadj.size()
00086             << abort(FatalError);
00087     }
00088 
00089 
00090     // Get number of cells on all processors
00091     List<int> nLocalCells(Pstream::nProcs());
00092     nLocalCells[Pstream::myProcNo()] = xadj.size()-1;
00093     Pstream::gatherList(nLocalCells);
00094     Pstream::scatterList(nLocalCells);
00095 
00096     // Get cell offsets.
00097     List<int> cellOffsets(Pstream::nProcs()+1);
00098     int nGlobalCells = 0;
00099     forAll(nLocalCells, procI)
00100     {
00101         cellOffsets[procI] = nGlobalCells;
00102         nGlobalCells += nLocalCells[procI];
00103     }
00104     cellOffsets[Pstream::nProcs()] = nGlobalCells;
00105 
00106     // Convert pointField into float
00107     Field<floatScalar> xyz(3*cellCentres.size());
00108     int compI = 0;
00109     forAll(cellCentres, cellI)
00110     {
00111         const point& cc = cellCentres[cellI];
00112         xyz[compI++] = float(cc.x());
00113         xyz[compI++] = float(cc.y());
00114         xyz[compI++] = float(cc.z());
00115     }
00116 
00117     // Make sure every domain has at least one cell
00118     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00119     // (Metis falls over with zero sized domains)
00120     // Trickle cells from processors that have them up to those that
00121     // don't.
00122 
00123 
00124     // Number of cells to send to the next processor
00125     // (is same as number of cells next processor has to receive)
00126     List<int> nSendCells(Pstream::nProcs(), 0);
00127 
00128     for (label procI = nLocalCells.size()-1; procI >=1; procI--)
00129     {
00130         if (nLocalCells[procI]-nSendCells[procI] < 1)
00131         {
00132             nSendCells[procI-1] = nSendCells[procI]-nLocalCells[procI]+1;
00133         }
00134     }
00135 
00136     // First receive (so increasing the sizes of all arrays)
00137 
00138     if (Pstream::myProcNo() >= 1 && nSendCells[Pstream::myProcNo()-1] > 0)
00139     {
00140         // Receive cells from previous processor
00141         IPstream fromPrevProc(Pstream::blocking, Pstream::myProcNo()-1);
00142 
00143         Field<int> prevXadj(fromPrevProc);
00144         Field<int> prevAdjncy(fromPrevProc);
00145         Field<floatScalar> prevXyz(fromPrevProc);
00146         Field<int> prevCellWeights(fromPrevProc);
00147         Field<int> prevFaceWeights(fromPrevProc);
00148 
00149         if (prevXadj.size() != nSendCells[Pstream::myProcNo()-1])
00150         {
00151             FatalErrorIn("parMetisDecomp::decompose(..)")
00152                 << "Expected from processor " << Pstream::myProcNo()-1
00153                 << " connectivity for " << nSendCells[Pstream::myProcNo()-1]
00154                 << " nCells but only received " << prevXadj.size()
00155                 << abort(FatalError);
00156         }
00157 
00158         // Insert adjncy
00159         prepend(prevAdjncy, adjncy);
00160         // Adapt offsets and prepend xadj
00161         xadj += prevAdjncy.size();
00162         prepend(prevXadj, xadj);
00163         // Coords
00164         prepend(prevXyz, xyz);
00165         // Weights
00166         prepend(prevCellWeights, cellWeights);
00167         prepend(prevFaceWeights, faceWeights);
00168     }
00169 
00170 
00171     // Send to my next processor
00172 
00173     if (nSendCells[Pstream::myProcNo()] > 0)
00174     {
00175         // Send cells to next processor
00176         OPstream toNextProc(Pstream::blocking, Pstream::myProcNo()+1);
00177 
00178         int nCells = nSendCells[Pstream::myProcNo()];
00179         int startCell = xadj.size()-1 - nCells;
00180         int startFace = xadj[startCell];
00181         int nFaces = adjncy.size()-startFace;
00182 
00183         // Send for all cell data: last nCells elements
00184         // Send for all face data: last nFaces elements
00185         toNextProc
00186             << Field<int>::subField(xadj, nCells, startCell)-startFace
00187             << Field<int>::subField(adjncy, nFaces, startFace)
00188             << SubField<floatScalar>(xyz, nDims*nCells, nDims*startCell)
00189             <<
00190             (
00191                 cellWeights.size()
00192               ? static_cast<const Field<int>&>
00193                 (
00194                     Field<int>::subField(cellWeights, nCells, startCell)
00195                 )
00196               : Field<int>(0)
00197             )
00198             <<
00199             (
00200                 faceWeights.size()
00201               ? static_cast<const Field<int>&>
00202                 (
00203                     Field<int>::subField(faceWeights, nFaces, startFace)
00204                 )
00205               : Field<int>(0)
00206             );
00207 
00208         // Remove data that has been sent
00209         if (faceWeights.size())
00210         {
00211             faceWeights.setSize(faceWeights.size()-nFaces);
00212         }
00213         if (cellWeights.size())
00214         {
00215             cellWeights.setSize(cellWeights.size()-nCells);
00216         }
00217         xyz.setSize(xyz.size()-nDims*nCells);
00218         adjncy.setSize(adjncy.size()-nFaces);
00219         xadj.setSize(xadj.size() - nCells);
00220     }
00221 
00222 
00223 
00224     // Adapt number of cells
00225     forAll(nSendCells, procI)
00226     {
00227         // Sent cells
00228         nLocalCells[procI] -= nSendCells[procI];
00229 
00230         if (procI >= 1)
00231         {
00232             // Received cells
00233             nLocalCells[procI] += nSendCells[procI-1];
00234         }
00235     }
00236     // Adapt cellOffsets
00237     nGlobalCells = 0;
00238     forAll(nLocalCells, procI)
00239     {
00240         cellOffsets[procI] = nGlobalCells;
00241         nGlobalCells += nLocalCells[procI];
00242     }
00243 
00244 
00245     if (nLocalCells[Pstream::myProcNo()] != (xadj.size()-1))
00246     {
00247         FatalErrorIn("parMetisDecomp::decompose(..)")
00248             << "Have connectivity for " << xadj.size()-1
00249             << " cells but nLocalCells:" << nLocalCells[Pstream::myProcNo()]
00250             << abort(FatalError);
00251     }
00252 
00253     // Weight info
00254     int wgtFlag = 0;
00255     int* vwgtPtr = NULL;
00256     int* adjwgtPtr = NULL;
00257 
00258     if (cellWeights.size())
00259     {
00260         vwgtPtr = cellWeights.begin();
00261         wgtFlag += 2;       // Weights on vertices
00262     }
00263     if (faceWeights.size())
00264     {
00265         adjwgtPtr = faceWeights.begin();
00266         wgtFlag += 1;       // Weights on edges
00267     }
00268 
00269 
00270     // Number of weights or balance constraints
00271     int nCon = 1;
00272     // Per processor, per constraint the weight
00273     Field<floatScalar> tpwgts(nCon*nProcessors_, 1./nProcessors_);
00274     // Imbalance tolerance
00275     Field<floatScalar> ubvec(nCon, 1.02);
00276     if (nProcessors_ == 1)
00277     {
00278         // If only one processor there is no imbalance.
00279         ubvec[0] = 1;
00280     }
00281 
00282     MPI_Comm comm = MPI_COMM_WORLD;
00283 
00284     // output: cell -> processor addressing
00285     finalDecomp.setSize(nLocalCells[Pstream::myProcNo()]);
00286 
00287     // output: number of cut edges
00288     int edgeCut = 0;
00289 
00290 
00291     ParMETIS_V3_PartGeomKway
00292     (
00293         cellOffsets.begin(),    // vtxDist
00294         xadj.begin(),
00295         adjncy.begin(),
00296         vwgtPtr,                // vertexweights
00297         adjwgtPtr,              // edgeweights
00298         &wgtFlag,
00299         &numFlag,
00300         &nDims,
00301         xyz.begin(),
00302         &nCon,
00303         &nProcessors_,          // nParts
00304         tpwgts.begin(),
00305         ubvec.begin(),
00306         const_cast<List<int>&>(options).begin(),
00307         &edgeCut,
00308         finalDecomp.begin(),
00309         &comm
00310     );
00311 
00312 
00313     // If we sent cells across make sure we undo it
00314     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00315 
00316     // Receive back from next processor if I sent something
00317     if (nSendCells[Pstream::myProcNo()] > 0)
00318     {
00319         IPstream fromNextProc(Pstream::blocking, Pstream::myProcNo()+1);
00320 
00321         List<int> nextFinalDecomp(fromNextProc);
00322 
00323         if (nextFinalDecomp.size() != nSendCells[Pstream::myProcNo()])
00324         {
00325             FatalErrorIn("parMetisDecomp::decompose(..)")
00326                 << "Expected from processor " << Pstream::myProcNo()+1
00327                 << " decomposition for " << nSendCells[Pstream::myProcNo()]
00328                 << " nCells but only received " << nextFinalDecomp.size()
00329                 << abort(FatalError);
00330         }
00331 
00332         append(nextFinalDecomp, finalDecomp);
00333     }
00334 
00335     // Send back to previous processor.
00336     if (Pstream::myProcNo() >= 1 && nSendCells[Pstream::myProcNo()-1] > 0)
00337     {
00338         OPstream toPrevProc(Pstream::blocking, Pstream::myProcNo()-1);
00339 
00340         int nToPrevious = nSendCells[Pstream::myProcNo()-1];
00341 
00342         toPrevProc <<
00343             SubList<int>
00344             (
00345                 finalDecomp,
00346                 nToPrevious,
00347                 finalDecomp.size()-nToPrevious
00348             );
00349 
00350         // Remove locally what has been sent
00351         finalDecomp.setSize(finalDecomp.size()-nToPrevious);
00352     }
00353 
00354     return edgeCut;
00355 }
00356 
00357 
00358 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00359 
00360 Foam::parMetisDecomp::parMetisDecomp
00361 (
00362     const dictionary& decompositionDict,
00363     const polyMesh& mesh
00364 )
00365 :
00366     decompositionMethod(decompositionDict),
00367     mesh_(mesh)
00368 {}
00369 
00370 
00371 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00372 
00373 Foam::labelList Foam::parMetisDecomp::decompose(const pointField& points)
00374 {
00375     if (points.size() != mesh_.nCells())
00376     {
00377         FatalErrorIn("parMetisDecomp::decompose(const pointField&)")
00378             << "Can use this decomposition method only for the whole mesh"
00379             << endl
00380             << "and supply one coordinate (cellCentre) for every cell." << endl
00381             << "The number of coordinates " << points.size() << endl
00382             << "The number of cells in the mesh " << mesh_.nCells()
00383             << exit(FatalError);
00384     }
00385 
00386     // For running sequential ...
00387     if (Pstream::nProcs() <= 1)
00388     {
00389         return metisDecomp(decompositionDict_, mesh_).decompose(points);
00390     }
00391 
00392     // Create global cell numbers
00393     // ~~~~~~~~~~~~~~~~~~~~~~~~~~
00394 
00395     // Get number of cells on all processors
00396     List<int> nLocalCells(Pstream::nProcs());
00397     nLocalCells[Pstream::myProcNo()] = mesh_.nCells();
00398     Pstream::gatherList(nLocalCells);
00399     Pstream::scatterList(nLocalCells);
00400 
00401     // Get cell offsets.
00402     List<int> cellOffsets(Pstream::nProcs()+1);
00403     int nGlobalCells = 0;
00404     forAll(nLocalCells, procI)
00405     {
00406         cellOffsets[procI] = nGlobalCells;
00407         nGlobalCells += nLocalCells[procI];
00408     }
00409     cellOffsets[Pstream::nProcs()] = nGlobalCells;
00410 
00411     int myOffset = cellOffsets[Pstream::myProcNo()];
00412 
00413 
00414     //
00415     // Make Metis Distributed CSR (Compressed Storage Format) storage
00416     //   adjncy      : contains cellCells (= edges in graph)
00417     //   xadj(celli) : start of information in adjncy for celli
00418     //
00419 
00420 
00421 
00422     const labelList& faceOwner = mesh_.faceOwner();
00423     const labelList& faceNeighbour = mesh_.faceNeighbour();
00424     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00425 
00426 
00427     // Get renumbered owner on other side of coupled faces
00428     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00429 
00430     List<int> globalNeighbour(mesh_.nFaces()-mesh_.nInternalFaces());
00431 
00432     forAll(patches, patchI)
00433     {
00434         const polyPatch& pp = patches[patchI];
00435 
00436         if (pp.coupled())
00437         {
00438             label faceI = pp.start();
00439             label bFaceI = pp.start() - mesh_.nInternalFaces();
00440 
00441             forAll(pp, i)
00442             {
00443                 globalNeighbour[bFaceI++] = faceOwner[faceI++] + myOffset;
00444             }
00445         }
00446     }
00447 
00448     // Get the cell on the other side of coupled patches
00449     syncTools::swapBoundaryFaceList(mesh_, globalNeighbour, false);
00450 
00451 
00452     // Count number of faces (internal + coupled)
00453     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00454 
00455     // Number of faces per cell
00456     List<int> nFacesPerCell(mesh_.nCells(), 0);
00457 
00458     // Number of coupled faces
00459     label nCoupledFaces = 0;
00460 
00461     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00462     {
00463         nFacesPerCell[faceOwner[faceI]]++;
00464         nFacesPerCell[faceNeighbour[faceI]]++;
00465     }
00466     // Handle coupled faces
00467     forAll(patches, patchI)
00468     {
00469         const polyPatch& pp = patches[patchI];
00470 
00471         if (pp.coupled())
00472         {
00473             label faceI = pp.start();
00474 
00475             forAll(pp, i)
00476             {
00477                 nCoupledFaces++;
00478                 nFacesPerCell[faceOwner[faceI++]]++;
00479             }
00480         }
00481     }
00482 
00483 
00484     // Fill in xadj
00485     // ~~~~~~~~~~~~
00486 
00487     Field<int> xadj(mesh_.nCells()+1, -1);
00488 
00489     int freeAdj = 0;
00490 
00491     for (label cellI = 0; cellI < mesh_.nCells(); cellI++)
00492     {
00493         xadj[cellI] = freeAdj;
00494 
00495         freeAdj += nFacesPerCell[cellI];
00496     }
00497     xadj[mesh_.nCells()] = freeAdj;
00498 
00499 
00500 
00501     // Fill in adjncy
00502     // ~~~~~~~~~~~~~~
00503 
00504     Field<int> adjncy(2*mesh_.nInternalFaces() + nCoupledFaces, -1);
00505 
00506     nFacesPerCell = 0;
00507 
00508     // For internal faces is just offsetted owner and neighbour
00509     for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00510     {
00511         label own = faceOwner[faceI];
00512         label nei = faceNeighbour[faceI];
00513 
00514         adjncy[xadj[own] + nFacesPerCell[own]++] = nei + myOffset;
00515         adjncy[xadj[nei] + nFacesPerCell[nei]++] = own + myOffset;
00516     }
00517     // For boundary faces is offsetted coupled neighbour
00518     forAll(patches, patchI)
00519     {
00520         const polyPatch& pp = patches[patchI];
00521 
00522         if (pp.coupled())
00523         {
00524             label faceI = pp.start();
00525             label bFaceI = pp.start()-mesh_.nInternalFaces();
00526 
00527             forAll(pp, i)
00528             {
00529                 label own = faceOwner[faceI];
00530                 adjncy[xadj[own] + nFacesPerCell[own]++] =
00531                     globalNeighbour[bFaceI];
00532 
00533                 faceI++;
00534                 bFaceI++;
00535             }
00536         }
00537     }
00538 
00539 
00540 
00541     // decomposition options. 0 = use defaults
00542     List<int> options(3, 0);
00543     //options[0] = 1;     // don't use defaults but use values below
00544     //options[1] = -1;    // full debug info
00545     //options[2] = 15;    // random number seed
00546 
00547     // cell weights (so on the vertices of the dual)
00548     Field<int> cellWeights;
00549 
00550     // face weights (so on the edges of the dual)
00551     Field<int> faceWeights;
00552 
00553     // Check for user supplied weights and decomp options
00554     if (decompositionDict_.found("metisCoeffs"))
00555     {
00556         const dictionary& metisCoeffs =
00557             decompositionDict_.subDict("metisCoeffs");
00558         word weightsFile;
00559 
00560         if (metisCoeffs.readIfPresent("cellWeightsFile", weightsFile))
00561         {
00562             Info<< "parMetisDecomp : Using cell-based weights read from "
00563                 << weightsFile << endl;
00564 
00565             labelIOField cellIOWeights
00566             (
00567                 IOobject
00568                 (
00569                     weightsFile,
00570                     mesh_.time().timeName(),
00571                     mesh_,
00572                     IOobject::MUST_READ,
00573                     IOobject::AUTO_WRITE
00574                 )
00575             );
00576             cellWeights.transfer(cellIOWeights);
00577 
00578             if (cellWeights.size() != mesh_.nCells())
00579             {
00580                 FatalErrorIn("parMetisDecomp::decompose(const pointField&)")
00581                     << "Number of cell weights " << cellWeights.size()
00582                     << " read from " << cellIOWeights.objectPath()
00583                     << " does not equal number of cells " << mesh_.nCells()
00584                     << exit(FatalError);
00585             }
00586         }
00587 
00588         if (metisCoeffs.readIfPresent("faceWeightsFile", weightsFile))
00589         {
00590             Info<< "parMetisDecomp : Using face-based weights read from "
00591                 << weightsFile << endl;
00592 
00593             labelIOField weights
00594             (
00595                 IOobject
00596                 (
00597                     weightsFile,
00598                     mesh_.time().timeName(),
00599                     mesh_,
00600                     IOobject::MUST_READ,
00601                     IOobject::AUTO_WRITE
00602                 )
00603             );
00604 
00605             if (weights.size() != mesh_.nFaces())
00606             {
00607                 FatalErrorIn("parMetisDecomp::decompose(const pointField&)")
00608                     << "Number of face weights " << weights.size()
00609                     << " does not equal number of internal and boundary faces "
00610                     << mesh_.nFaces()
00611                     << exit(FatalError);
00612             }
00613 
00614             faceWeights.setSize(2*mesh_.nInternalFaces()+nCoupledFaces);
00615 
00616             // Assume symmetric weights. Keep same ordering as adjncy.
00617             nFacesPerCell = 0;
00618 
00619             // Handle internal faces
00620             for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00621             {
00622                 label w = weights[faceI];
00623 
00624                 label own = faceOwner[faceI];
00625                 label nei = faceNeighbour[faceI];
00626 
00627                 faceWeights[xadj[own] + nFacesPerCell[own]++] = w;
00628                 faceWeights[xadj[nei] + nFacesPerCell[nei]++] = w;
00629             }
00630             // Coupled boundary faces
00631             forAll(patches, patchI)
00632             {
00633                 const polyPatch& pp = patches[patchI];
00634 
00635                 if (pp.coupled())
00636                 {
00637                     label faceI = pp.start();
00638 
00639                     forAll(pp, i)
00640                     {
00641                         label w = weights[faceI];
00642                         label own = faceOwner[faceI];
00643                         adjncy[xadj[own] + nFacesPerCell[own]++] = w;
00644                         faceI++;
00645                     }
00646                 }
00647             }
00648         }
00649 
00650         if (metisCoeffs.readIfPresent("options", options))
00651         {
00652             Info<< "Using Metis options     " << options
00653                 << nl << endl;
00654 
00655             if (options.size() != 3)
00656             {
00657                 FatalErrorIn("parMetisDecomp::decompose(const pointField&)")
00658                     << "Number of options " << options.size()
00659                     << " should be three." << exit(FatalError);
00660             }
00661         }
00662     }
00663 
00664 
00665     // Do actual decomposition
00666     List<int> finalDecomp;
00667     decompose
00668     (
00669         xadj,
00670         adjncy,
00671         points,
00672         cellWeights,
00673         faceWeights,
00674         options,
00675 
00676         finalDecomp
00677     );
00678 
00679     // Copy back to labelList
00680     labelList decomp(finalDecomp.size());
00681     forAll(decomp, i)
00682     {
00683         decomp[i] = finalDecomp[i];
00684     }
00685     return decomp;
00686 }
00687 
00688 
00689 Foam::labelList Foam::parMetisDecomp::decompose
00690 (
00691     const labelList& cellToRegion,
00692     const pointField& regionPoints
00693 )
00694 {
00695     const labelList& faceOwner = mesh_.faceOwner();
00696     const labelList& faceNeighbour = mesh_.faceNeighbour();
00697     const polyBoundaryMesh& patches = mesh_.boundaryMesh();
00698 
00699     if (cellToRegion.size() != mesh_.nCells())
00700     {
00701         FatalErrorIn
00702         (
00703             "parMetisDecomp::decompose(const labelList&, const pointField&)"
00704         )   << "Size of cell-to-coarse map " << cellToRegion.size()
00705             << " differs from number of cells in mesh " << mesh_.nCells()
00706             << exit(FatalError);
00707     }
00708 
00709 
00710     // Global region numbering engine
00711     globalIndex globalRegions(regionPoints.size());
00712 
00713 
00714     // Get renumbered owner region on other side of coupled faces
00715     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00716 
00717     List<int> globalNeighbour(mesh_.nFaces()-mesh_.nInternalFaces());
00718 
00719     forAll(patches, patchI)
00720     {
00721         const polyPatch& pp = patches[patchI];
00722 
00723         if (pp.coupled())
00724         {
00725             label faceI = pp.start();
00726             label bFaceI = pp.start() - mesh_.nInternalFaces();
00727 
00728             forAll(pp, i)
00729             {
00730                 label ownRegion = cellToRegion[faceOwner[faceI]];
00731                 globalNeighbour[bFaceI++] = globalRegions.toGlobal(ownRegion);
00732                 faceI++;
00733             }
00734         }
00735     }
00736 
00737     // Get the cell on the other side of coupled patches
00738     syncTools::swapBoundaryFaceList(mesh_, globalNeighbour, false);
00739 
00740 
00741     // Get globalCellCells on coarse mesh
00742     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00743 
00744     labelListList globalRegionRegions;
00745     {
00746         List<DynamicList<label> > dynRegionRegions(regionPoints.size());
00747 
00748         // Internal faces first
00749         forAll(faceNeighbour, faceI)
00750         {
00751             label ownRegion = cellToRegion[faceOwner[faceI]];
00752             label neiRegion = cellToRegion[faceNeighbour[faceI]];
00753 
00754             if (ownRegion != neiRegion)
00755             {
00756                 label globalOwn = globalRegions.toGlobal(ownRegion);
00757                 label globalNei = globalRegions.toGlobal(neiRegion);
00758 
00759                 if (findIndex(dynRegionRegions[ownRegion], globalNei) == -1)
00760                 {
00761                     dynRegionRegions[ownRegion].append(globalNei);
00762                 }
00763                 if (findIndex(dynRegionRegions[neiRegion], globalOwn) == -1)
00764                 {
00765                     dynRegionRegions[neiRegion].append(globalOwn);
00766                 }
00767             }
00768         }
00769 
00770         // Coupled boundary faces
00771         forAll(patches, patchI)
00772         {
00773             const polyPatch& pp = patches[patchI];
00774 
00775             if (pp.coupled())
00776             {
00777                 label faceI = pp.start();
00778                 label bFaceI = pp.start() - mesh_.nInternalFaces();
00779 
00780                 forAll(pp, i)
00781                 {
00782                     label ownRegion = cellToRegion[faceOwner[faceI]];
00783                     label globalNei = globalNeighbour[bFaceI++];
00784                     faceI++;
00785 
00786                     if (findIndex(dynRegionRegions[ownRegion], globalNei) == -1)
00787                     {
00788                         dynRegionRegions[ownRegion].append(globalNei);
00789                     }
00790                 }
00791             }
00792         }
00793 
00794         globalRegionRegions.setSize(dynRegionRegions.size());
00795         forAll(dynRegionRegions, i)
00796         {
00797             globalRegionRegions[i].transfer(dynRegionRegions[i]);
00798         }
00799     }
00800 
00801     labelList regionDecomp(decompose(globalRegionRegions, regionPoints));
00802 
00803     // Rework back into decomposition for original mesh_
00804     labelList cellDistribution(cellToRegion.size());
00805 
00806     forAll(cellDistribution, cellI)
00807     {
00808         cellDistribution[cellI] = regionDecomp[cellToRegion[cellI]];
00809     }
00810     return cellDistribution;
00811 }
00812 
00813 
00814 Foam::labelList Foam::parMetisDecomp::decompose
00815 (
00816     const labelListList& globalCellCells,
00817     const pointField& cellCentres
00818 )
00819 {
00820     if (cellCentres.size() != globalCellCells.size())
00821     {
00822         FatalErrorIn
00823         (
00824             "parMetisDecomp::decompose(const labelListList&, const pointField&)"
00825         )   << "Inconsistent number of cells (" << globalCellCells.size()
00826             << ") and number of cell centres (" << cellCentres.size()
00827             << ")." << exit(FatalError);
00828     }
00829 
00830     // For running sequential ...
00831     if (Pstream::nProcs() <= 1)
00832     {
00833         return metisDecomp(decompositionDict_, mesh_)
00834             .decompose(globalCellCells, cellCentres);
00835     }
00836 
00837 
00838     // Make Metis Distributed CSR (Compressed Storage Format) storage
00839 
00840     // Connections
00841     Field<int> adjncy;
00842     // Offsets into adjncy
00843     Field<int> xadj;
00844     metisDecomp::calcMetisCSR(globalCellCells, adjncy, xadj);
00845 
00846     // decomposition options. 0 = use defaults
00847     List<int> options(3, 0);
00848     //options[0] = 1;     // don't use defaults but use values below
00849     //options[1] = -1;    // full debug info
00850     //options[2] = 15;    // random number seed
00851 
00852     // cell weights (so on the vertices of the dual)
00853     Field<int> cellWeights;
00854 
00855     // face weights (so on the edges of the dual)
00856     Field<int> faceWeights;
00857 
00858     // Check for user supplied weights and decomp options
00859     if (decompositionDict_.found("metisCoeffs"))
00860     {
00861         const dictionary& metisCoeffs =
00862             decompositionDict_.subDict("metisCoeffs");
00863         word weightsFile;
00864 
00865         if (metisCoeffs.readIfPresent("cellWeightsFile", weightsFile))
00866         {
00867             Info<< "parMetisDecomp : Using cell-based weights read from "
00868                 << weightsFile << endl;
00869 
00870             labelIOField cellIOWeights
00871             (
00872                 IOobject
00873                 (
00874                     weightsFile,
00875                     mesh_.time().timeName(),
00876                     mesh_,
00877                     IOobject::MUST_READ,
00878                     IOobject::AUTO_WRITE
00879                 )
00880             );
00881             cellWeights.transfer(cellIOWeights);
00882 
00883             if (cellWeights.size() != cellCentres.size())
00884             {
00885                 FatalErrorIn
00886                 (
00887                     "parMetisDecomp::decompose"
00888                     "(const labelListList&, const pointField&)"
00889                 )   << "Number of cell weights " << cellWeights.size()
00890                     << " read from " << cellIOWeights.objectPath()
00891                     << " does not equal number of cells " << cellCentres.size()
00892                     << exit(FatalError);
00893             }
00894         }
00895 
00896         //- faceWeights disabled. Only makes sense for cellCells from mesh.
00897         //if (metisCoeffs.readIfPresent("faceWeightsFile", weightsFile))
00898         //{
00899         //    Info<< "parMetisDecomp : Using face-based weights read from "
00900         //        << weightsFile << endl;
00901         //
00902         //    labelIOField weights
00903         //    (
00904         //        IOobject
00905         //        (
00906         //            weightsFile,
00907         //            mesh_.time().timeName(),
00908         //            mesh_,
00909         //            IOobject::MUST_READ,
00910         //            IOobject::AUTO_WRITE
00911         //        )
00912         //    );
00913         //
00914         //    if (weights.size() != mesh_.nFaces())
00915         //    {
00916         //        FatalErrorIn("parMetisDecomp::decompose(const pointField&)")
00917         //            << "Number of face weights " << weights.size()
00918         //            << " does not equal number of internal and boundary faces "
00919         //            << mesh_.nFaces()
00920         //            << exit(FatalError);
00921         //    }
00922         //
00923         //    faceWeights.setSize(2*mesh_.nInternalFaces()+nCoupledFaces);
00924         //
00925         //    // Assume symmetric weights. Keep same ordering as adjncy.
00926         //    nFacesPerCell = 0;
00927         //
00928         //    // Handle internal faces
00929         //    for (label faceI = 0; faceI < mesh_.nInternalFaces(); faceI++)
00930         //    {
00931         //        label w = weights[faceI];
00932         //
00933         //        label own = faceOwner[faceI];
00934         //        label nei = faceNeighbour[faceI];
00935         //
00936         //        faceWeights[xadj[own] + nFacesPerCell[own]++] = w;
00937         //        faceWeights[xadj[nei] + nFacesPerCell[nei]++] = w;
00938         //    }
00939         //    // Coupled boundary faces
00940         //    forAll(patches, patchI)
00941         //    {
00942         //        const polyPatch& pp = patches[patchI];
00943         //
00944         //        if (pp.coupled())
00945         //        {
00946         //            label faceI = pp.start();
00947         //
00948         //            forAll(pp, i)
00949         //            {
00950         //                label w = weights[faceI];
00951         //                label own = faceOwner[faceI];
00952         //                adjncy[xadj[own] + nFacesPerCell[own]++] = w;
00953         //                faceI++;
00954         //            }
00955         //        }
00956         //    }
00957         //}
00958 
00959         if (metisCoeffs.readIfPresent("options", options))
00960         {
00961             Info<< "Using Metis options     " << options
00962                 << nl << endl;
00963 
00964             if (options.size() != 3)
00965             {
00966                 FatalErrorIn
00967                 (
00968                     "parMetisDecomp::decompose"
00969                     "(const labelListList&, const pointField&)"
00970                 )   << "Number of options " << options.size()
00971                     << " should be three." << exit(FatalError);
00972             }
00973         }
00974     }
00975 
00976 
00977     // Do actual decomposition
00978     List<int> finalDecomp;
00979     decompose
00980     (
00981         xadj,
00982         adjncy,
00983         cellCentres,
00984         cellWeights,
00985         faceWeights,
00986         options,
00987 
00988         finalDecomp
00989     );
00990 
00991     // Copy back to labelList
00992     labelList decomp(finalDecomp.size());
00993     forAll(decomp, i)
00994     {
00995         decomp[i] = finalDecomp[i];
00996     }
00997     return decomp;
00998 }
00999 
01000 
01001 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd