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-2025 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 
27 #include "GAMGAgglomeration.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
35 
37  (
41  );
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
46 
48 (
49  GAMGAgglomeration& agglom,
50  const dictionary& dict
51 )
52 :
53  GAMGProcAgglomeration(agglom, dict),
54  procAgglomMaps_(dict.lookup("processorAgglomeration"))
55 {}
56 
57 
58 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
59 
62 {
63  forAllReverse(comms_, i)
64  {
65  if (comms_[i] != -1)
66  {
67  UPstream::freeCommunicator(comms_[i]);
68  }
69  }
70 }
71 
72 
73 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
74 
76 {
77  if (debug)
78  {
79  Pout<< nl << "Starting mesh overview" << endl;
80  printStats(Pout, agglom_);
81  }
82 
83  if (agglom_.size() >= 1)
84  {
85  forAll(procAgglomMaps_, i)
86  {
87  const label fineLevelIndex = procAgglomMaps_[i].first();
88 
89  if (fineLevelIndex >= agglom_.size())
90  {
92  << "Ignoring specification for level " << fineLevelIndex
93  << " since outside agglomeration." << endl;
94 
95  continue;
96  }
97 
98  if (agglom_.hasMeshLevel(fineLevelIndex))
99  {
100  // Get the fine mesh
101  const lduMesh& levelMesh = agglom_.meshLevel(fineLevelIndex);
102  label nProcs = UPstream::nProcs(levelMesh.comm());
103 
104  if (nProcs > 1)
105  {
106  // My processor id
107  const label myProcID = Pstream::myProcNo(levelMesh.comm());
108 
109  const List<labelList>& clusters =
110  procAgglomMaps_[i].second();
111 
112  // Coarse to fine master processor
113  labelList coarseToMaster(clusters.size());
114 
115  // Fine to coarse map
116  labelList procAgglomMap(nProcs, -1);
117 
118  // Cluster for my processor (with master index first)
119  labelList agglomProcIDs;
120 
121 
122 
123  forAll(clusters, coarseI)
124  {
125  const labelList& cluster = clusters[coarseI];
126  coarseToMaster[coarseI] = cluster[0];
127 
128  forAll(cluster, i)
129  {
130  procAgglomMap[cluster[i]] = coarseI;
131  }
132 
133  label masterIndex = findIndex
134  (
135  cluster,
136  coarseToMaster[coarseI]
137  );
138 
139  if (masterIndex == -1)
140  {
142  << "At level " << fineLevelIndex
143  << " the master processor "
144  << coarseToMaster[coarseI]
145  << " is not in the cluster "
146  << cluster
147  << exit(FatalError);
148  }
149 
150  if (findIndex(cluster, myProcID) != -1)
151  {
152  // This is my cluster. Make sure master index is
153  // first
154  agglomProcIDs = cluster;
155  Swap(agglomProcIDs[0], agglomProcIDs[masterIndex]);
156  }
157  }
158 
159 
160  // Check that we've done all processors
161  if (findIndex(procAgglomMap, -1) != -1)
162  {
164  << "At level " << fineLevelIndex
165  << " processor "
166  << findIndex(procAgglomMap, -1)
167  << " is not in any cluster"
168  << exit(FatalError);
169  }
170 
171 
172  // Allocate a communicator for the processor-agglomerated
173  // matrix
174  comms_.append
175  (
177  (
178  levelMesh.comm(),
179  coarseToMaster
180  )
181  );
182 
183  // Use processor agglomeration maps to do the actual
184  // collecting
185  if (Pstream::myProcNo(levelMesh.comm()) != -1)
186  {
188  (
189  fineLevelIndex,
190  procAgglomMap,
191  coarseToMaster,
192  agglomProcIDs,
193  comms_.last()
194  );
195  }
196  }
197  }
198  }
199 
200  // Print a bit
201  if (debug)
202  {
203  Pout<< nl << "Agglomerated mesh overview" << endl;
204  printStats(Pout, agglom_);
205  }
206  }
207 
208  return true;
209 }
210 
211 
212 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:461
Macros for easy insertion into run-time selection tables.
Geometric agglomerated algebraic multigrid agglomeration class.
Processor agglomeration of GAMGAgglomerations.
virtual bool agglomerate()=0
Modify agglomeration. Return true if modified.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
T & last()
Return the last element of the list.
Definition: UListI.H:128
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:311
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
static label allocateCommunicator(const label parent, const labelList &subRanks, const bool doPstream=true)
Allocate a new communicator.
Definition: UPstream.C:245
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Abstract base class for meshes which provide LDU addressing for the construction of lduMatrix and LDU...
Definition: lduMesh.H:60
virtual label comm() const =0
Return communicator used for parallel communication.
virtual bool agglomerate()
Modify agglomeration. Return true if modified.
manualGAMGProcAgglomeration(GAMGAgglomeration &agglom, const dictionary &dict)
Construct given agglomerator and controls.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
#define WarningInFunction
Report a warning using Foam::Warning.
const unitSet & lookup(const word &unitName)
Lookup and return the named unit from the table.
Definition: units.C:346
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
addToRunTimeSelectionTable(polyPatch, mergedCyclicPolyPatch, word)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
error FatalError
void Swap(T &a, T &b)
Definition: Swap.H:43
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)
static const char nl
Definition: Ostream.H:297
dictionary dict