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

pairGAMGAgglomerate.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 "pairGAMGAgglomeration.H"
00028 #include "lduAddressing.H"
00029 
00030 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00031 
00032 Foam::tmp<Foam::labelField> Foam::pairGAMGAgglomeration::agglomerate
00033 (
00034     label& nCoarseCells,
00035     const lduAddressing& fineMatrixAddressing,
00036     const scalarField& faceWeights
00037 )
00038 {
00039     const label nFineCells = fineMatrixAddressing.size();
00040 
00041     const unallocLabelList& upperAddr = fineMatrixAddressing.upperAddr();
00042     const unallocLabelList& lowerAddr = fineMatrixAddressing.lowerAddr();
00043 
00044     // For each cell calculate faces
00045     labelList cellFaces(upperAddr.size() + lowerAddr.size());
00046     labelList cellFaceOffsets(nFineCells + 1);
00047 
00048     // memory management
00049     {
00050         labelList nNbrs(nFineCells, 0);
00051 
00052         forAll (upperAddr, facei)
00053         {
00054             nNbrs[upperAddr[facei]]++;
00055         }
00056 
00057         forAll (lowerAddr, facei)
00058         {
00059             nNbrs[lowerAddr[facei]]++;
00060         }
00061 
00062         cellFaceOffsets[0] = 0;
00063         forAll (nNbrs, celli)
00064         {
00065             cellFaceOffsets[celli+1] = cellFaceOffsets[celli] + nNbrs[celli];
00066         }
00067 
00068         // reset the whole list to use as counter
00069         nNbrs = 0;
00070 
00071         forAll (upperAddr, facei)
00072         {
00073             cellFaces
00074             [
00075                 cellFaceOffsets[upperAddr[facei]] + nNbrs[upperAddr[facei]]
00076             ] = facei;
00077 
00078             nNbrs[upperAddr[facei]]++;
00079         }
00080 
00081         forAll (lowerAddr, facei)
00082         {
00083             cellFaces
00084             [
00085                 cellFaceOffsets[lowerAddr[facei]] + nNbrs[lowerAddr[facei]]
00086             ] = facei;
00087 
00088             nNbrs[lowerAddr[facei]]++;
00089         }
00090     }
00091 
00092 
00093     // go through the faces and create clusters
00094 
00095     tmp<labelField> tcoarseCellMap(new labelField(nFineCells, -1));
00096     labelField& coarseCellMap = tcoarseCellMap();
00097 
00098     nCoarseCells = 0;
00099 
00100     for (label celli=0; celli<nFineCells; celli++)
00101     {
00102         if (coarseCellMap[celli] < 0)
00103         {
00104             label matchFaceNo = -1;
00105             scalar maxFaceWeight = -GREAT;
00106 
00107             // check faces to find ungrouped neighbour with largest face weight
00108             for
00109             (
00110                 label faceOs=cellFaceOffsets[celli];
00111                 faceOs<cellFaceOffsets[celli+1];
00112                 faceOs++
00113             )
00114             {
00115                 label facei = cellFaces[faceOs];
00116 
00117                 // I don't know whether the current cell is owner or neighbour.
00118                 // Therefore I'll check both sides
00119                 if
00120                 (
00121                     coarseCellMap[upperAddr[facei]] < 0
00122                  && coarseCellMap[lowerAddr[facei]] < 0
00123                  && faceWeights[facei] > maxFaceWeight
00124                 )
00125                 {
00126                     // Match found. Pick up all the necessary data
00127                     matchFaceNo = facei;
00128                     maxFaceWeight = faceWeights[facei];
00129                 }
00130             }
00131 
00132             if (matchFaceNo >= 0)
00133             {
00134                 // Make a new group
00135                 coarseCellMap[upperAddr[matchFaceNo]] = nCoarseCells;
00136                 coarseCellMap[lowerAddr[matchFaceNo]] = nCoarseCells;
00137                 nCoarseCells++;
00138             }
00139             else
00140             {
00141                 // No match. Find the best neighbouring cluster and
00142                 // put the cell there
00143                 label clusterMatchFaceNo = -1;
00144                 scalar clusterMaxFaceCoeff = -GREAT;
00145 
00146                 for
00147                 (
00148                     label faceOs=cellFaceOffsets[celli];
00149                     faceOs<cellFaceOffsets[celli+1];
00150                     faceOs++
00151                 )
00152                 {
00153                     label facei = cellFaces[faceOs];
00154 
00155                     if (faceWeights[facei] > clusterMaxFaceCoeff)
00156                     {
00157                         clusterMatchFaceNo = facei;
00158                         clusterMaxFaceCoeff = faceWeights[facei];
00159                     }
00160                 }
00161 
00162                 if (clusterMatchFaceNo >= 0)
00163                 {
00164                     // Add the cell to the best cluster
00165                     coarseCellMap[celli] = max
00166                     (
00167                         coarseCellMap[upperAddr[clusterMatchFaceNo]],
00168                         coarseCellMap[lowerAddr[clusterMatchFaceNo]]
00169                     );
00170                 }
00171             }
00172         }
00173     }
00174 
00175 
00176     // Check that all cells are part of clusters,
00177     // if not create single-cell "clusters" for each
00178     for (label celli=0; celli<nFineCells; celli++)
00179     {
00180         if (coarseCellMap[celli] < 0)
00181         {
00182             coarseCellMap[celli] = nCoarseCells;
00183             nCoarseCells++;
00184         }
00185     }
00186 
00187     // Reverese the map ordering to improve the next level of agglomeration
00188     // (doesn't always help and is sometimes detrimental)
00189     nCoarseCells--;
00190 
00191     forAll (coarseCellMap, celli)
00192     {
00193         coarseCellMap[celli] = nCoarseCells - coarseCellMap[celli];
00194     }
00195 
00196     nCoarseCells++;
00197 
00198     return tcoarseCellMap;
00199 }
00200 
00201 
00202 void Foam::pairGAMGAgglomeration::agglomerate
00203 (
00204     const lduMesh& mesh,
00205     const scalarField& faceWeights
00206 )
00207 {
00208     // Get the finest-level interfaces from the mesh
00209     interfaceLevels_.set
00210     (
00211         0,
00212         new lduInterfacePtrsList(mesh.interfaces())
00213     );
00214 
00215     // Start geometric agglomeration from the given faceWeights
00216     scalarField* faceWeightsPtr = const_cast<scalarField*>(&faceWeights);
00217 
00218     // Agglomerate until the required number of cells in the coarsest level
00219     // is reached
00220 
00221     label nPairLevels = 0;
00222     label nCreatedLevels = 0;
00223 
00224     while (nCreatedLevels < maxLevels_ - 1)
00225     {
00226         label nCoarseCells = -1;
00227 
00228         tmp<labelField> finalAgglomPtr = agglomerate
00229         (
00230             nCoarseCells,
00231             meshLevel(nCreatedLevels).lduAddr(),
00232             *faceWeightsPtr
00233         );
00234 
00235         if (continueAgglomerating(nCoarseCells))
00236         {
00237             nCells_[nCreatedLevels] = nCoarseCells;
00238             restrictAddressing_.set(nCreatedLevels, finalAgglomPtr);
00239         }
00240         else
00241         {
00242             break;
00243         }
00244 
00245         agglomerateLduAddressing(nCreatedLevels);
00246 
00247         // Agglomerate the faceWeights field for the next level
00248         {
00249             scalarField* aggFaceWeightsPtr
00250             (
00251                 new scalarField
00252                 (
00253                     meshLevels_[nCreatedLevels].upperAddr().size(),
00254                     0.0
00255                 )
00256             );
00257 
00258             restrictFaceField
00259             (
00260                 *aggFaceWeightsPtr,
00261                 *faceWeightsPtr,
00262                 nCreatedLevels
00263             );
00264 
00265             if (nCreatedLevels)
00266             {
00267                 delete faceWeightsPtr;
00268             }
00269 
00270             faceWeightsPtr = aggFaceWeightsPtr;
00271         }
00272 
00273         if (nPairLevels % mergeLevels_)
00274         {
00275             combineLevels(nCreatedLevels);
00276         }
00277         else
00278         {
00279             nCreatedLevels++;
00280         }
00281 
00282         nPairLevels++;
00283     }
00284 
00285     // Shrink the storage of the levels to those created
00286     compactLevels(nCreatedLevels);
00287 
00288     // Delete temporary geometry storage
00289     if (nCreatedLevels)
00290     {
00291         delete faceWeightsPtr;
00292     }
00293 }
00294 
00295 
00296 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd