Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / fvAgglomerationMethods / MGridGenGamgAgglomeration / MGridGenGAMGAgglomerate.C
blob3766f9a7e806816f0175b54d0e7e868f746da577
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Agglomerate one level using the MGridGen algorithm.
27 \*---------------------------------------------------------------------------*/
29 #include "MGridGenGAMGAgglomeration.H"
30 #include "fvMesh.H"
31 #include "syncTools.H"
33 extern "C"
35 #   include "mgridgen.h"
36 #   ifdef darwin
37 #       undef FALSE
38 #       undef TRUE
39 #   endif
41 #undef sign
44 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
46 void Foam::MGridGenGAMGAgglomeration::
47 makeCompactCellFaceAddressingAndFaceWeights
49     const lduAddressing& fineAddressing,
50     labelList& cellCells,
51     labelList& cellCellOffsets,
52     const vectorField& Si,
53     List<scalar>& faceWeights
56     const label nFineCells = fineAddressing.size();
57     const label nFineFaces = fineAddressing.upperAddr().size();
59     const unallocLabelList& upperAddr = fineAddressing.upperAddr();
60     const unallocLabelList& lowerAddr = fineAddressing.lowerAddr();
62     // Number of neighbours for each cell
63     labelList nNbrs(nFineCells, 0);
65     forAll (upperAddr, facei)
66     {
67         nNbrs[upperAddr[facei]]++;
68     }
70     forAll (lowerAddr, facei)
71     {
72         nNbrs[lowerAddr[facei]]++;
73     }
75     // Set the sizes of the addressing and faceWeights arrays
76     cellCellOffsets.setSize(nFineCells + 1);
77     cellCells.setSize(2*nFineFaces);
78     faceWeights.setSize(2*nFineFaces);
81     cellCellOffsets[0] = 0;
82     forAll (nNbrs, celli)
83     {
84         cellCellOffsets[celli+1] = cellCellOffsets[celli] + nNbrs[celli];
85     }
87     // reset the whole list to use as counter
88     nNbrs = 0;
90     forAll (upperAddr, facei)
91     {
92         label own = upperAddr[facei];
93         label nei = lowerAddr[facei];
95         label l1 = cellCellOffsets[own] + nNbrs[own]++;
96         label l2 = cellCellOffsets[nei] + nNbrs[nei]++;
98         cellCells[l1] = nei;
99         cellCells[l2] = own;
101         faceWeights[l1] = mag(Si[facei]);
102         faceWeights[l2] = mag(Si[facei]);
103     }
107 Foam::tmp<Foam::labelField> Foam::MGridGenGAMGAgglomeration::agglomerate
109     label& nCoarseCells,
110     const label minSize,
111     const label maxSize,
112     const lduAddressing& fineAddressing,
113     const scalarField& V,
114     const vectorField& Sf,
115     const scalarField& Sb
118     const label nFineCells = fineAddressing.size();
120     // Compact addressing for cellCells
121     labelList cellCells;
122     labelList cellCellOffsets;
124     // Face weights = face areas of the internal faces
125     List<scalar> faceWeights;
127     // Create the compact addressing for cellCells and faceWeights
128     makeCompactCellFaceAddressingAndFaceWeights
129     (
130         fineAddressing,
131         cellCells,
132         cellCellOffsets,
133         Sf,
134         faceWeights
135     );
137     // MGridGen agglomeration options.
138     labelList options(4, 0);
139     options[0] = 4;                   // globular agglom
140     options[1] = 6;                   // objective F3 and F2
141     options[2] = 128;                 // debugging output level
142     options[3] = fvMesh_.nGeometricD(); // Dimensionality of the grid
145     // output: cell -> processor addressing
146     List<int> finalAgglom(nFineCells);
147     int nMoves = -1;
149 #   ifdef WM_DP
151     MGridGen
152     (
153         nFineCells,
154         cellCellOffsets.begin(),
155         const_cast<scalar*>(V.begin()),
156         const_cast<scalar*>(Sb.begin()),
157         cellCells.begin(),
158         faceWeights.begin(),
159         minSize,
160         maxSize,
161         options.begin(),
162         &nMoves,
163         &nCoarseCells,
164         finalAgglom.begin()
165     );
167 #   else
169     // Conversion of type for MGridGen interface
171     Field<realtype> dblVols(V.size());
172     forAll (dblVols, i)
173     {
174         dblVols[i] = V[i];
175     }
177     Field<realtype> dblAreas(Sb.size());
178     forAll (dblAreas, i)
179     {
180         dblAreas[i] = Sb[i];
181     }
183     Field<realtype> dblFaceWeights(faceWeights.size());
184     forAll (dblFaceWeights, i)
185     {
186         dblFaceWeights[i] = faceWeights[i];
187     }
189     MGridGen
190     (
191         nFineCells,
192         cellCellOffsets.begin(),
193         dblVols.begin(),
194         dblAreas.begin(),
195         cellCells.begin(),
196         dblFaceWeights.begin(),
197         minSize,
198         maxSize,
199         options.begin(),
200         &nMoves,
201         &nCoarseCells,
202         finalAgglom.begin()
203     );
205 #   endif
207     return tmp<labelField>(new labelField(finalAgglom));
211 // ************************************************************************* //