GAMGAgglomerationTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2014 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 
26 #include "GAMGAgglomeration.H"
27 #include "mapDistribute.H"
28 #include "globalIndex.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  const label comm,
36  const labelList& procIDs,
37  const Type& myVal,
38  List<Type>& allVals,
39  const int tag
40 )
41 {
42  if (Pstream::myProcNo(comm) == procIDs[0])
43  {
44  allVals.setSize(procIDs.size());
45 
46  allVals[0] = myVal;
47  for (label i = 1; i < procIDs.size(); i++)
48  {
49  IPstream fromSlave
50  (
51  Pstream::scheduled,
52  procIDs[i],
53  0,
54  tag,
55  comm
56  );
57 
58  fromSlave >> allVals[i];
59  }
60  }
61  else
62  {
63  OPstream toMaster
64  (
65  Pstream::scheduled,
66  procIDs[0],
67  0,
68  tag,
69  comm
70  );
71  toMaster << myVal;
72  }
73 }
74 
75 
76 template<class Type>
78 (
79  Field<Type>& cf,
80  const Field<Type>& ff,
81  const labelList& fineToCoarse
82 ) const
83 {
85 
86  forAll(ff, i)
87  {
88  cf[fineToCoarse[i]] += ff[i];
89  }
90 }
91 
92 
93 template<class Type>
95 (
96  Field<Type>& cf,
97  const Field<Type>& ff,
98  const label fineLevelIndex,
99  const bool procAgglom
100 ) const
101 {
102  const labelList& fineToCoarse = restrictAddressing_[fineLevelIndex];
103 
104  if (!procAgglom && ff.size() != fineToCoarse.size())
105  {
107  (
108  "void GAMGAgglomeration::restrictField"
109  "(Field<Type>& cf, const Field<Type>& ff, "
110  "const label fineLevelIndex) const"
111  ) << "field does not correspond to level " << fineLevelIndex
112  << " sizes: field = " << ff.size()
113  << " level = " << fineToCoarse.size()
114  << abort(FatalError);
115  }
116 
117  restrictField(cf, ff, fineToCoarse);
118 
119  label coarseLevelIndex = fineLevelIndex+1;
120 
121  if (procAgglom && hasProcMesh(coarseLevelIndex))
122  {
123  label fineComm = UPstream::parent(procCommunicator_[coarseLevelIndex]);
124 
125  const List<label>& procIDs = agglomProcIDs(coarseLevelIndex);
126  const labelList& offsets = cellOffsets(coarseLevelIndex);
127 
128  globalIndex::gather
129  (
130  offsets,
131  fineComm,
132  procIDs,
133  cf,
134  UPstream::msgType(),
135  Pstream::nonBlocking //Pstream::scheduled
136  );
137  }
138 }
139 
140 
141 template<class Type>
143 (
144  Field<Type>& cf,
145  const Field<Type>& ff,
146  const label fineLevelIndex
147 ) const
148 {
149  const labelList& fineToCoarse = faceRestrictAddressing_[fineLevelIndex];
150 
151  if (ff.size() != fineToCoarse.size())
152  {
154  (
155  "void GAMGAgglomeration::restrictFaceField"
156  "(Field<Type>& cf, const Field<Type>& ff, "
157  "const label fineLevelIndex) const"
158  ) << "field does not correspond to level " << fineLevelIndex
159  << " sizes: field = " << ff.size()
160  << " level = " << fineToCoarse.size()
161  << abort(FatalError);
162  }
163 
164  cf = pTraits<Type>::zero;
165 
166  forAll(fineToCoarse, ffacei)
167  {
168  label cFace = fineToCoarse[ffacei];
169 
170  if (cFace >= 0)
171  {
172  cf[cFace] += ff[ffacei];
173  }
174  }
175 }
176 
177 
178 template<class Type>
180 (
181  Field<Type>& ff,
182  const Field<Type>& cf,
183  const label levelIndex,
184  const bool procAgglom
185 ) const
186 {
187  const labelList& fineToCoarse = restrictAddressing_[levelIndex];
188 
189  label coarseLevelIndex = levelIndex+1;
190 
191  if (procAgglom && hasProcMesh(coarseLevelIndex))
192  {
193  label coarseComm = UPstream::parent
194  (
195  procCommunicator_[coarseLevelIndex]
196  );
197 
198  const List<label>& procIDs = agglomProcIDs(coarseLevelIndex);
199  const labelList& offsets = cellOffsets(coarseLevelIndex);
200 
201  label localSize = nCells_[levelIndex];
202 
203  Field<Type> allCf(localSize);
204  globalIndex::scatter
205  (
206  offsets,
207  coarseComm,
208  procIDs,
209  cf,
210  allCf,
211  UPstream::msgType(),
212  Pstream::nonBlocking //Pstream::scheduled
213  );
214 
215  forAll(fineToCoarse, i)
216  {
217  ff[i] = allCf[fineToCoarse[i]];
218  }
219  }
220  else
221  {
222  forAll(fineToCoarse, i)
223  {
224  ff[i] = cf[fineToCoarse[i]];
225  }
226  }
227 }
228 
229 
230 // ************************************************************************* //
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
Output inter-processor communications stream.
Definition: OPstream.H:50
static void gatherList(const label comm, const labelList &procIDs, const Type &myVal, List< Type > &allVals, const int tag=Pstream::msgType())
Gather value from all procIDs onto procIDs[0].
void setSize(const label)
Reset size of List.
Definition: List.C:318
#define forAll(list, i)
Definition: UList.H:421
Pre-declare SubField and related Field type.
Definition: Field.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
Traits class for primitives.
Definition: pTraits.H:50
error FatalError
Input inter-processor communications stream.
Definition: IPstream.H:50
void restrictFaceField(Field< Type > &cf, const Field< Type > &ff, const label fineLevelIndex) const
Restrict (integrate by summation) face field.
void restrictField(Field< Type > &cf, const Field< Type > &ff, const label fineLevelIndex, const bool procAgglom) const
Restrict (integrate by summation) cell field.
void prolongField(Field< Type > &ff, const Field< Type > &cf, const label coarseLevelIndex, const bool procAgglom) const
Prolong (interpolate by injection) cell field.