faceMapper.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) 2011-2024 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 "faceMapper.H"
27 #include "fieldTypes.H"
28 #include "demandDrivenData.H"
29 #include "polyMesh.H"
30 #include "polyTopoChangeMap.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 void Foam::faceMapper::calcAddressing() const
35 {
36  if
37  (
38  directAddrPtr_
39  || interpolationAddrPtr_
40  || weightsPtr_
41  || insertedFaceLabelsPtr_
42  )
43  {
45  << "Addressing already calculated."
46  << abort(FatalError);
47  }
48 
49  if (direct())
50  {
51  // Direct addressing, no weights
52 
53  directAddrPtr_ = new labelList(mpm_.faceMap());
54  labelList& directAddr = *directAddrPtr_;
55 
56  // Reset the size of addressing list to contain only live faces
57  directAddr.setSize(mesh_.nFaces());
58 
59  insertedFaceLabelsPtr_ = new labelList(mesh_.nFaces());
60  labelList& insertedFaces = *insertedFaceLabelsPtr_;
61 
62  label nInsertedFaces = 0;
63 
64  forAll(directAddr, facei)
65  {
66  if (directAddr[facei] < 0)
67  {
68  // Found inserted face
69  directAddr[facei] = 0;
70  insertedFaces[nInsertedFaces] = facei;
71  nInsertedFaces++;
72  }
73  }
74 
75  insertedFaces.setSize(nInsertedFaces);
76  }
77  else
78  {
79  // Interpolative addressing
80 
81  interpolationAddrPtr_ = new labelListList(mesh_.nFaces());
82  labelListList& addr = *interpolationAddrPtr_;
83 
84  weightsPtr_ = new scalarListList(mesh_.nFaces());
85  scalarListList& w = *weightsPtr_;
86 
87  const List<objectMap>& fff = mpm_.facesFromFacesMap();
88 
89  forAll(fff, fffI)
90  {
91  // Get addressing
92  const labelList& mo = fff[fffI].masterObjects();
93 
94  label facei = fff[fffI].index();
95 
96  if (addr[facei].size())
97  {
99  << "Master face " << facei
100  << " mapped from face faces " << mo
101  << " already destination of mapping." << abort(FatalError);
102  }
103 
104  // Map from masters, uniform weights
105  addr[facei] = mo;
106  w[facei] = scalarList(mo.size(), 1.0/mo.size());
107  }
108 
109 
110  // Do mapped faces. Note that can already be set from facesFromFaces
111  // so check if addressing size still zero.
112  const labelList& fm = mpm_.faceMap();
113 
114  forAll(fm, facei)
115  {
116  if (fm[facei] > -1 && addr[facei].empty())
117  {
118  // Mapped from a single face
119  addr[facei] = labelList(1, fm[facei]);
120  w[facei] = scalarList(1, 1.0);
121  }
122  }
123 
124 
125  // Grab inserted faces (for them the size of addressing is still zero)
126 
127  insertedFaceLabelsPtr_ = new labelList(mesh_.nFaces());
128  labelList& insertedFaces = *insertedFaceLabelsPtr_;
129 
130  label nInsertedFaces = 0;
131 
132  forAll(addr, facei)
133  {
134  if (addr[facei].empty())
135  {
137  << "No interpolative addressing provided for face " << facei
138  << abort(FatalError);
139  }
140  }
141 
142  insertedFaces.setSize(nInsertedFaces);
143  }
144 }
145 
146 
147 void Foam::faceMapper::clearOut()
148 {
149  deleteDemandDrivenData(directAddrPtr_);
150  deleteDemandDrivenData(interpolationAddrPtr_);
151  deleteDemandDrivenData(weightsPtr_);
152  deleteDemandDrivenData(insertedFaceLabelsPtr_);
153 }
154 
155 
156 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
157 
159 :
160  mesh_(mpm.mesh()),
161  mpm_(mpm),
162  insertedFaces_(true),
163  direct_(false),
164  directAddrPtr_(nullptr),
165  interpolationAddrPtr_(nullptr),
166  weightsPtr_(nullptr),
167  insertedFaceLabelsPtr_(nullptr)
168 {
169  // Check for possibility of direct mapping
170  if (mpm_.facesFromFacesMap().empty())
171  {
172  direct_ = true;
173  }
174  else
175  {
176  direct_ = false;
177  }
178 
179  // Check for inserted faces
180  if (direct_ && (mpm_.faceMap().empty() || min(mpm_.faceMap()) > -1))
181  {
182  insertedFaces_ = false;
183  }
184  else
185  {
186  // Need to check all 3 lists to see if there are inserted faces
187  // with no owner
188 
189  // Make a copy of the face map, add the entries for faces from points
190  // and faces from edges and check for left-overs
191  labelList fm(mesh_.nFaces(), -1);
192 
193  const List<objectMap>& fff = mpm_.facesFromFacesMap();
194 
195  forAll(fff, fffI)
196  {
197  fm[fff[fffI].index()] = 0;
198  }
199 
200  if (min(fm) < 0)
201  {
202  insertedFaces_ = true;
203  }
204  }
205 }
206 
207 
208 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
209 
211 {
212  clearOut();
213 }
214 
215 
216 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
217 
219 {
220  if (!direct())
221  {
223  << "Requested direct addressing for an interpolative mapper."
224  << abort(FatalError);
225  }
226 
227  if (!insertedObjects())
228  {
229  // No inserted faces. Reuse faceMap
230  return mpm_.faceMap();
231  }
232  else
233  {
234  if (!directAddrPtr_)
235  {
236  calcAddressing();
237  }
238 
239  return *directAddrPtr_;
240  }
241 }
242 
243 
245 {
246  if (direct())
247  {
249  << "Requested interpolative addressing for a direct mapper."
250  << abort(FatalError);
251  }
252 
253  if (!interpolationAddrPtr_)
254  {
255  calcAddressing();
256  }
257 
258  return *interpolationAddrPtr_;
259 }
260 
261 
263 {
264  if (direct())
265  {
267  << "Requested interpolative weights for a direct mapper."
268  << abort(FatalError);
269  }
270 
271  if (!weightsPtr_)
272  {
273  calcAddressing();
274  }
275 
276  return *weightsPtr_;
277 }
278 
279 
281 {
282  return mpm_.nOldFaces();
283 }
284 
285 
287 {
288  return mpm_.nOldInternalFaces();
289 }
290 
291 
293 {
294  if (!insertedFaceLabelsPtr_)
295  {
296  if (!insertedObjects())
297  {
298  // There are no inserted faces
299  insertedFaceLabelsPtr_ = new labelList(0);
300  }
301  else
302  {
303  calcAddressing();
304  }
305  }
306 
307  return *insertedFaceLabelsPtr_;
308 }
309 
310 
312 {
313  return mpm_.flipFaceFlux();
314 }
315 
316 
318 {
319  return mpm_.nOldInternalFaces();
320 }
321 
322 
324 {
325  return mpm_.oldPatchStarts();
326 }
327 
328 
330 {
331  return mpm_.oldPatchSizes();
332 }
333 
334 
335 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void setSize(const label)
Reset size of List.
Definition: List.C:281
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
virtual const labelListList & addressing() const
Return interpolated addressing.
Definition: faceMapper.C:244
virtual const scalarListList & weights() const
Return interpolation weights.
Definition: faceMapper.C:262
virtual const labelUList & directAddressing() const
Return direct addressing.
Definition: faceMapper.C:218
virtual const labelList & insertedObjectLabels() const
Return list of inserted faces.
Definition: faceMapper.C:292
const labelList & oldPatchStarts() const
Return old patch starts.
Definition: faceMapper.C:323
const labelList & oldPatchSizes() const
Return old patch sizes.
Definition: faceMapper.C:329
virtual label sizeBeforeMapping() const
Return size of field before mapping.
Definition: faceMapper.C:280
label nOldInternalFaces() const
Return number of old internalFaces.
Definition: faceMapper.C:317
label internalSizeBeforeMapping() const
Return number of internal faces before mapping.
Definition: faceMapper.C:286
virtual ~faceMapper()
Destructor.
Definition: faceMapper.C:210
const labelHashSet & flipFaceFlux() const
Return flux flip map.
Definition: faceMapper.C:311
faceMapper(const polyTopoChangeMap &mpm)
Construct from polyTopoChangeMap.
Definition: faceMapper.C:158
virtual bool direct() const
Is the mapping direct.
Definition: faceMapper.H:116
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
const List< objectMap > & facesFromFacesMap() const
Faces originating from faces.
const labelList & faceMap() const
Old face map.
label nFaces() const
Template functions to aid in the implementation of demand driven data.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
Include the header files for all the primitive types that Fields are instantiated for.
List< scalarList > scalarListList
Definition: scalarList.H:51
List< label > labelList
A List of labels.
Definition: labelList.H:56
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 deleteDemandDrivenData(DataType *&dataPtr)
errorManip< error > abort(error &err)
Definition: errorManip.H:131
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:57
error FatalError