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

extendedCellToFaceStencil.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 "extendedCellToFaceStencil.H"
00028 #include "globalIndex.H"
00029 #include "syncTools.H"
00030 #include "SortableList.H"
00031 
00032 /* * * * * * * * * * * * * * * Static Member Data  * * * * * * * * * * * * * */
00033 
00034 defineTypeNameAndDebug(Foam::extendedCellToFaceStencil, 0);
00035 
00036 
00037 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00038 
00039 void Foam::extendedCellToFaceStencil::writeStencilStats
00040 (
00041     Ostream& os,
00042     const labelListList& stencil,
00043     const mapDistribute& map
00044 )
00045 {
00046     label sumSize = 0;
00047     label nSum = 0;
00048     label minSize = labelMax;
00049     label maxSize = labelMin;
00050 
00051     forAll(stencil, i)
00052     {
00053         const labelList& sCells = stencil[i];
00054 
00055         if (sCells.size() > 0)
00056         {
00057             sumSize += sCells.size();
00058             nSum++;
00059             minSize = min(minSize, sCells.size());
00060             maxSize = max(maxSize, sCells.size());
00061         }
00062     }
00063     reduce(sumSize, sumOp<label>());
00064     reduce(nSum, sumOp<label>());
00065 
00066     reduce(minSize, minOp<label>());
00067     reduce(maxSize, maxOp<label>());
00068 
00069     os  << "Stencil size :" << nl
00070         << "    average : " << scalar(sumSize)/nSum << nl
00071         << "    min     : " << minSize << nl
00072         << "    max     : " << maxSize << nl
00073         << endl;
00074 
00075     // Sum all sent data
00076     label nSent = 0;
00077     label nLocal = 0;
00078     forAll(map.subMap(), procI)
00079     {
00080         if (procI != Pstream::myProcNo())
00081         {
00082             nSent += map.subMap()[procI].size();
00083         }
00084         else
00085         {
00086             nLocal += map.subMap()[procI].size();
00087         }
00088     }
00089 
00090     os  << "Local data size : " << returnReduce(nLocal, sumOp<label>()) << nl
00091         << "Sent data size  : " << returnReduce(nSent, sumOp<label>()) << nl
00092         << endl;
00093 }
00094 
00095 
00096 Foam::autoPtr<Foam::mapDistribute>
00097 Foam::extendedCellToFaceStencil::calcDistributeMap
00098 (
00099     const polyMesh& mesh,
00100     const globalIndex& globalNumbering,
00101     labelListList& faceStencil
00102 )
00103 {
00104     // Convert stencil to schedule
00105     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
00106 
00107     // We now know what information we need from other processors. This needs
00108     // to be converted into what information I need to send as well
00109     // (mapDistribute)
00110 
00111 
00112     // 1. Construct per processor compact addressing of the global cells
00113     //    needed. The ones from the local processor are not included since
00114     //    these are always all needed.
00115     List<Map<label> > globalToProc(Pstream::nProcs());
00116     {
00117         const labelList& procPatchMap = mesh.globalData().procPatchMap();
00118         const polyBoundaryMesh& patches = mesh.boundaryMesh();
00119 
00120         // Presize with (as estimate) size of patch to neighbour.
00121         forAll(procPatchMap, procI)
00122         {
00123             if (procPatchMap[procI] != -1)
00124             {
00125                 globalToProc[procI].resize
00126                 (
00127                     patches[procPatchMap[procI]].size()
00128                 );
00129             }
00130         }
00131 
00132         // Collect all (non-local) globalcells/faces needed.
00133         forAll(faceStencil, faceI)
00134         {
00135             const labelList& stencilCells = faceStencil[faceI];
00136 
00137             forAll(stencilCells, i)
00138             {
00139                 label globalCellI = stencilCells[i];
00140                 label procI = globalNumbering.whichProcID(stencilCells[i]);
00141 
00142                 if (procI != Pstream::myProcNo())
00143                 {
00144                     label nCompact = globalToProc[procI].size();
00145                     globalToProc[procI].insert(globalCellI, nCompact);
00146                 }
00147             }
00148         }
00149         // Sort global cells needed (not really necessary)
00150         forAll(globalToProc, procI)
00151         {
00152             if (procI != Pstream::myProcNo())
00153             {
00154                 Map<label>& globalMap = globalToProc[procI];
00155 
00156                 SortableList<label> sorted(globalMap.toc().xfer());
00157 
00158                 forAll(sorted, i)
00159                 {
00160                     Map<label>::iterator iter = globalMap.find(sorted[i]);
00161                     iter() = i;
00162                 }
00163             }
00164         }
00165 
00166 
00167         //forAll(globalToProc, procI)
00168         //{
00169         //    Pout<< "From processor:" << procI << " want cells/faces:" << endl;
00170         //    forAllConstIter(Map<label>, globalToProc[procI], iter)
00171         //    {
00172         //        Pout<< "    global:" << iter.key()
00173         //            << " local:" << globalNumbering.toLocal(procI, iter.key())
00174         //            << endl;
00175         //    }
00176         //    Pout<< endl;
00177         //}
00178     }
00179 
00180 
00181     // 2. The overall compact addressing is
00182     // - myProcNo data first (uncompacted)
00183     // - all other processors consecutively
00184 
00185     labelList compactStart(Pstream::nProcs());
00186     compactStart[Pstream::myProcNo()] = 0;
00187     label nCompact = globalNumbering.localSize();
00188     forAll(compactStart, procI)
00189     {
00190         if (procI != Pstream::myProcNo())
00191         {
00192             compactStart[procI] = nCompact;
00193             nCompact += globalToProc[procI].size();
00194         }
00195     }
00196 
00197 
00198     // 3. Find out what to receive/send in compact addressing.
00199     labelListList recvCompact(Pstream::nProcs());
00200     for (label procI = 0; procI < Pstream::nProcs(); procI++)
00201     {
00202         if (procI != Pstream::myProcNo())
00203         {
00204             labelList wantedGlobals(globalToProc[procI].size());
00205             recvCompact[procI].setSize(globalToProc[procI].size());
00206 
00207             label i = 0;
00208             forAllConstIter(Map<label>, globalToProc[procI], iter)
00209             {
00210                 wantedGlobals[i] = iter.key();
00211                 recvCompact[procI][i] = compactStart[procI]+iter();
00212                 i++;
00213             }
00214 
00215             // Send the global cell numbers I need from procI
00216             OPstream str(Pstream::blocking, procI);
00217             str << wantedGlobals;
00218         }
00219         else
00220         {
00221             recvCompact[procI] =
00222                 compactStart[procI]
00223               + identity(globalNumbering.localSize());
00224         }
00225     }
00226     labelListList sendCompact(Pstream::nProcs());
00227     for (label procI = 0; procI < Pstream::nProcs(); procI++)
00228     {
00229         if (procI != Pstream::myProcNo())
00230         {
00231             // See what neighbour wants to receive (= what I need to send)
00232 
00233             IPstream str(Pstream::blocking, procI);
00234             labelList globalCells(str);
00235 
00236             labelList& procCompact = sendCompact[procI];
00237             procCompact.setSize(globalCells.size());
00238 
00239             // Convert from globalCells (all on my processor!) into compact
00240             // addressing
00241             forAll(globalCells, i)
00242             {
00243                 label cellI = globalNumbering.toLocal(globalCells[i]);
00244                 procCompact[i] = compactStart[Pstream::myProcNo()]+cellI;
00245             }
00246         }
00247         else
00248         {
00249             sendCompact[procI] = recvCompact[procI];
00250         }
00251     }
00252 
00253     // Convert stencil to compact numbering
00254     forAll(faceStencil, faceI)
00255     {
00256         labelList& stencilCells = faceStencil[faceI];
00257 
00258         forAll(stencilCells, i)
00259         {
00260             label globalCellI = stencilCells[i];
00261             label procI = globalNumbering.whichProcID(globalCellI);
00262             if (procI != Pstream::myProcNo())
00263             {
00264                 label localCompact = globalToProc[procI][globalCellI];
00265                 stencilCells[i] = compactStart[procI]+localCompact;
00266             }
00267             else
00268             {
00269                 label localCompact = globalNumbering.toLocal(globalCellI);
00270                 stencilCells[i] = compactStart[procI]+localCompact;
00271             }
00272 
00273         }
00274     }
00275 
00276 
00277     // Constuct map for distribution of compact data.
00278     autoPtr<mapDistribute> mapPtr
00279     (
00280         new mapDistribute
00281         (
00282             nCompact,
00283             sendCompact,
00284             recvCompact,
00285             true            // reuse send/recv maps.
00286         )
00287     );
00288 
00289     return mapPtr;
00290 }
00291 
00292 
00293 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00294 
00295 Foam::extendedCellToFaceStencil::extendedCellToFaceStencil(const polyMesh& mesh)
00296 :
00297     mesh_(mesh)
00298 {
00299     // Check for transformation - not supported.
00300     const polyBoundaryMesh& patches = mesh.boundaryMesh();
00301 
00302     forAll(patches, patchI)
00303     {
00304         if (isA<coupledPolyPatch>(patches[patchI]))
00305         {
00306             const coupledPolyPatch& cpp =
00307                 refCast<const coupledPolyPatch>(patches[patchI]);
00308 
00309             if (!cpp.parallel() || cpp.separated())
00310             {
00311                 FatalErrorIn
00312                 (
00313                     "extendedCellToFaceStencil::extendedCellToFaceStencil"
00314                     "(const polyMesh&)"
00315                 )   << "Coupled patches with transformations not supported."
00316                     << endl
00317                     << "Problematic patch " << cpp.name() << exit(FatalError);
00318             }
00319         }
00320     }
00321 }
00322 
00323 
00324 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd