manualGAMGProcAgglomeration.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2013-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
28 #include "GAMGAgglomeration.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  defineTypeNameAndDebug(manualGAMGProcAgglomeration, 0);
35 
37  (
38  GAMGProcAgglomeration,
39  manualGAMGProcAgglomeration,
40  GAMGAgglomeration
41  );
42 }
43 
44 
45 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
51 (
52  GAMGAgglomeration& agglom,
53  const dictionary& controlDict
54 )
55 :
56  GAMGProcAgglomeration(agglom, controlDict),
57  procAgglomMaps_(controlDict.lookup("processorAgglomeration"))
58 {}
59 
60 
61 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
62 
65 {
66  forAllReverse(comms_, i)
67  {
68  if (comms_[i] != -1)
69  {
70  UPstream::freeCommunicator(comms_[i]);
71  }
72  }
73 }
74 
75 
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77 
79 {
80  if (debug)
81  {
82  Pout<< nl << "Starting mesh overview" << endl;
83  printStats(Pout, agglom_);
84  }
85 
86  if (agglom_.size() >= 1)
87  {
88  forAll(procAgglomMaps_, i)
89  {
90  const label fineLevelIndex = procAgglomMaps_[i].first();
91 
92  if (fineLevelIndex >= agglom_.size())
93  {
95  << "Ignoring specification for level " << fineLevelIndex
96  << " since outside agglomeration." << endl;
97 
98  continue;
99  }
100 
101  if (agglom_.hasMeshLevel(fineLevelIndex))
102  {
103  // Get the fine mesh
104  const lduMesh& levelMesh = agglom_.meshLevel(fineLevelIndex);
105  label nProcs = UPstream::nProcs(levelMesh.comm());
106 
107  if (nProcs > 1)
108  {
109  // My processor id
110  const label myProcID = Pstream::myProcNo(levelMesh.comm());
111 
112  const List<labelList>& clusters =
113  procAgglomMaps_[i].second();
114 
115  // Coarse to fine master processor
116  labelList coarseToMaster(clusters.size());
117 
118  // Fine to coarse map
119  labelList procAgglomMap(nProcs, -1);
120 
121  // Cluster for my processor (with master index first)
122  labelList agglomProcIDs;
123 
124 
125 
126  forAll(clusters, coarseI)
127  {
128  const labelList& cluster = clusters[coarseI];
129  coarseToMaster[coarseI] = cluster[0];
130 
131  forAll(cluster, i)
132  {
133  procAgglomMap[cluster[i]] = coarseI;
134  }
135 
136  label masterIndex = findIndex
137  (
138  cluster,
139  coarseToMaster[coarseI]
140  );
141 
142  if (masterIndex == -1)
143  {
145  << "At level " << fineLevelIndex
146  << " the master processor "
147  << coarseToMaster[coarseI]
148  << " is not in the cluster "
149  << cluster
150  << exit(FatalError);
151  }
152 
153  if (findIndex(cluster, myProcID) != -1)
154  {
155  // This is my cluster. Make sure master index is
156  // first
157  agglomProcIDs = cluster;
158  Swap(agglomProcIDs[0], agglomProcIDs[masterIndex]);
159  }
160  }
161 
162 
163  // Check that we've done all processors
164  if (findIndex(procAgglomMap, -1) != -1)
165  {
167  << "At level " << fineLevelIndex
168  << " processor "
169  << findIndex(procAgglomMap, -1)
170  << " is not in any cluster"
171  << exit(FatalError);
172  }
173 
174 
175  // Allocate a communicator for the processor-agglomerated
176  // matrix
177  comms_.append
178  (
180  (
181  levelMesh.comm(),
182  coarseToMaster
183  )
184  );
185 
186  // Use procesor agglomeration maps to do the actual
187  // collecting
188  if (Pstream::myProcNo(levelMesh.comm()) != -1)
189  {
191  (
192  fineLevelIndex,
193  procAgglomMap,
194  coarseToMaster,
195  agglomProcIDs,
196  comms_.last()
197  );
198  }
199  }
200  }
201  }
202 
203  // Print a bit
204  if (debug)
205  {
206  Pout<< nl << "Agglomerated mesh overview" << endl;
207  printStats(Pout, agglom_);
208  }
209  }
210 
211  return true;
212 }
213 
214 
215 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual bool agglomerate()
Modify agglomeration. Return true if modified.
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Abstract base class for meshes which provide LDU addressing for the construction of lduMatrix and LDU...
Definition: lduMesh.H:59
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:446
virtual label comm() const =0
Return communicator used for parallel communication.
manualGAMGProcAgglomeration(GAMGAgglomeration &agglom, const dictionary &controlDict)
Construct given agglomerator and controls.
Macros for easy insertion into run-time selection tables.
void Swap(T &a, T &b)
Definition: Swap.H:43
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
static const char nl
Definition: Ostream.H:265
defineTypeNameAndDebug(combustionModel, 0)
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
virtual bool agglomerate()=0
Modify agglomeration. Return true if modified.
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
#define WarningInFunction
Report a warning using Foam::Warning.
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Processor agglomeration of GAMGAgglomerations.
static label allocateCommunicator(const label parent, const labelList &subRanks, const bool doPstream=true)
Allocate a new communicator.
Definition: UPstream.C:253
T & last()
Return the last element of the list.
Definition: UListI.H:128
Geometric agglomerated algebraic multigrid agglomeration class.
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:319
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:583