polyTopoChangeTemplates.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "polyTopoChange.H"
30 #include "polyMesh.H"
31 #include "HashOps.H"
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
35 template<class Type>
36 void Foam::polyTopoChange::reorder
37 (
38  const labelUList& oldToNew,
39  DynamicList<Type>& lst
40 )
41 {
42  // Create copy
43  DynamicList<Type> oldLst(lst);
44 
45  forAll(oldToNew, i)
46  {
47  const label newIdx = oldToNew[i];
48 
49  if (newIdx >= 0)
50  {
51  lst[newIdx] = oldLst[i];
52  }
53  }
54 }
55 
56 
57 template<class Type>
58 void Foam::polyTopoChange::reorder
59 (
60  const labelUList& oldToNew,
61  List<DynamicList<Type>>& lst
62 )
63 {
64  // Create copy
65  List<DynamicList<Type>> oldLst(lst);
66 
67  forAll(oldToNew, i)
68  {
69  const label newIdx = oldToNew[i];
70 
71  if (newIdx >= 0)
72  {
73  lst[newIdx].transfer(oldLst[i]);
74  }
75  }
76 }
77 
78 
79 template<class Type>
80 void Foam::polyTopoChange::renumberKey
81 (
82  const labelUList& oldToNew,
83  Map<Type>& map
84 )
85 {
86  Map<Type> newMap(map.capacity());
87 
88  forAllConstIters(map, iter)
89  {
90  const label newKey = oldToNew[iter.key()];
91 
92  if (newKey >= 0)
93  {
94  newMap.insert(newKey, iter.val());
95  }
96  }
97 
98  map.transfer(newMap);
99 }
100 
101 
102 template<class Type>
104 (
105  autoPtr<Type>& newMeshPtr,
106  const IOobject& io,
107  const polyMesh& mesh,
108  const bool syncParallel,
109  const bool orderCells,
110  const bool orderPoints
111 )
112 {
113  if (debug)
114  {
115  Pout<< "polyTopoChange::changeMesh"
116  << "(autoPtr<fvMesh>&, const IOobject&, const fvMesh&"
117  << ", const bool, const bool, const bool)"
118  << endl;
119  }
120 
121  if (debug)
122  {
123  Pout<< "Old mesh:" << nl;
124  writeMeshStats(mesh, Pout);
125  }
126 
127  // new mesh points
128  pointField newPoints;
129  // number of internal points
130  label nInternalPoints;
131  // patch slicing
132  labelList patchSizes;
133  labelList patchStarts;
134  // inflate maps
135  List<objectMap> pointsFromPoints;
136  List<objectMap> facesFromPoints;
137  List<objectMap> facesFromEdges;
138  List<objectMap> facesFromFaces;
139  List<objectMap> cellsFromPoints;
140  List<objectMap> cellsFromEdges;
141  List<objectMap> cellsFromFaces;
142  List<objectMap> cellsFromCells;
143 
144  // old mesh info
145  List<Map<label>> oldPatchMeshPointMaps;
146  labelList oldPatchNMeshPoints;
147  labelList oldPatchStarts;
148  List<Map<label>> oldFaceZoneMeshPointMaps;
149 
150  // Compact, reorder patch faces and calculate mesh/patch maps.
151  compactAndReorder
152  (
153  mesh,
154  syncParallel,
155  orderCells,
156  orderPoints,
157 
158  nInternalPoints,
159  newPoints,
160  patchSizes,
161  patchStarts,
162  pointsFromPoints,
163  facesFromPoints,
164  facesFromEdges,
165  facesFromFaces,
166  cellsFromPoints,
167  cellsFromEdges,
168  cellsFromFaces,
169  cellsFromCells,
170  oldPatchMeshPointMaps,
171  oldPatchNMeshPoints,
172  oldPatchStarts,
173  oldFaceZoneMeshPointMaps
174  );
175 
176  const label nOldPoints(mesh.nPoints());
177  const label nOldFaces(mesh.nFaces());
178  const label nOldCells(mesh.nCells());
179  autoPtr<scalarField> oldCellVolumes(new scalarField(mesh.cellVolumes()));
180 
181 
182  // Create the mesh
183  // ~~~~~~~~~~~~~~~
184 
185  //IOobject noReadIO(io);
186  //noReadIO.readOpt() = IOobject::NO_READ;
187  //noReadIO.writeOpt() = IOobject::AUTO_WRITE;
188  newMeshPtr.reset
189  (
190  new Type
191  (
192  io, //noReadIO
193  std::move(newPoints),
194  std::move(faces_),
195  std::move(faceOwner_),
196  std::move(faceNeighbour_)
197  )
198  );
199  Type& newMesh = *newMeshPtr;
200 
201  // Clear out primitives
202  {
203  retiredPoints_.clearStorage();
204  region_.clearStorage();
205  }
206 
207 
208  if (debug)
209  {
210  // Some stats on changes
211  label nAdd, nInflate, nMerge, nRemove;
212  countMap(pointMap_, reversePointMap_, nAdd, nInflate, nMerge, nRemove);
213  Pout<< "Points:"
214  << " added(from point):" << nAdd
215  << " added(from nothing):" << nInflate
216  << " merged(into other point):" << nMerge
217  << " removed:" << nRemove
218  << nl;
219 
220  countMap(faceMap_, reverseFaceMap_, nAdd, nInflate, nMerge, nRemove);
221  Pout<< "Faces:"
222  << " added(from face):" << nAdd
223  << " added(inflated):" << nInflate
224  << " merged(into other face):" << nMerge
225  << " removed:" << nRemove
226  << nl;
227 
228  countMap(cellMap_, reverseCellMap_, nAdd, nInflate, nMerge, nRemove);
229  Pout<< "Cells:"
230  << " added(from cell):" << nAdd
231  << " added(inflated):" << nInflate
232  << " merged(into other cell):" << nMerge
233  << " removed:" << nRemove
234  << nl
235  << endl;
236  }
237 
238 
239  {
240  const polyBoundaryMesh& oldPatches = mesh.boundaryMesh();
241 
242  List<polyPatch*> newBoundary(oldPatches.size());
243 
244  forAll(oldPatches, patchi)
245  {
246  newBoundary[patchi] = oldPatches[patchi].clone
247  (
248  newMesh.boundaryMesh(),
249  patchi,
250  patchSizes[patchi],
251  patchStarts[patchi]
252  ).ptr();
253  }
254  newMesh.addFvPatches(newBoundary);
255  }
256 
257 
258  // Zones
259  // ~~~~~
260 
261  // Start off from empty zones.
262  const pointZoneMesh& oldPointZones = mesh.pointZones();
263  List<pointZone*> pZonePtrs(oldPointZones.size());
264  {
265  forAll(oldPointZones, i)
266  {
267  pZonePtrs[i] = new pointZone
268  (
269  oldPointZones[i].name(),
270  i,
271  newMesh.pointZones()
272  );
273  }
274  }
275 
276  const faceZoneMesh& oldFaceZones = mesh.faceZones();
277  List<faceZone*> fZonePtrs(oldFaceZones.size());
278  {
279  forAll(oldFaceZones, i)
280  {
281  fZonePtrs[i] = new faceZone
282  (
283  oldFaceZones[i].name(),
284  i,
285  newMesh.faceZones()
286  );
287  }
288  }
289 
290  const cellZoneMesh& oldCellZones = mesh.cellZones();
291  List<cellZone*> cZonePtrs(oldCellZones.size());
292  {
293  forAll(oldCellZones, i)
294  {
295  cZonePtrs[i] = new cellZone
296  (
297  oldCellZones[i].name(),
298  i,
299  newMesh.cellZones()
300  );
301  }
302  }
303 
304  newMesh.addZones(pZonePtrs, fZonePtrs, cZonePtrs);
305 
306  // Inverse of point/face/cell zone addressing.
307  // For every preserved point/face/cells in zone give the old position.
308  // For added points, the index is set to -1
309  labelListList pointZoneMap(mesh.pointZones().size());
310  labelListList faceZoneFaceMap(mesh.faceZones().size());
311  labelListList cellZoneMap(mesh.cellZones().size());
312 
313  resetZones(mesh, newMesh, pointZoneMap, faceZoneFaceMap, cellZoneMap);
314 
315  // Clear zone info
316  {
317  pointZone_.clearStorage();
318  faceZone_.clearStorage();
319  faceZoneFlip_.clearStorage();
320  cellZone_.clearStorage();
321  }
322 
323  // Patch point renumbering
324  // For every preserved point on a patch give the old position.
325  // For added points, the index is set to -1
326  labelListList patchPointMap(newMesh.boundaryMesh().size());
327  calcPatchPointMap
328  (
329  oldPatchMeshPointMaps,
330  newMesh.boundaryMesh(),
331  patchPointMap
332  );
333 
334  // Create the face zone mesh point renumbering
335  labelListList faceZonePointMap(newMesh.faceZones().size());
336  calcFaceZonePointMap(newMesh, oldFaceZoneMeshPointMaps, faceZonePointMap);
337 
338  if (debug)
339  {
340  Pout<< "New mesh:" << nl;
341  writeMeshStats(mesh, Pout);
342  }
343 
344  labelHashSet flipFaceFluxSet(HashSetOps::used(flipFaceFlux_));
345 
347  (
348  newMesh,
349  nOldPoints,
350  nOldFaces,
351  nOldCells,
352 
353  pointMap_,
354  pointsFromPoints,
355 
356  faceMap_,
357  facesFromPoints,
358  facesFromEdges,
359  facesFromFaces,
360 
361  cellMap_,
362  cellsFromPoints,
363  cellsFromEdges,
364  cellsFromFaces,
365  cellsFromCells,
366 
367  reversePointMap_,
368  reverseFaceMap_,
369  reverseCellMap_,
370 
371  flipFaceFluxSet,
372 
373  patchPointMap,
374 
375  pointZoneMap,
376 
377  faceZonePointMap,
378  faceZoneFaceMap,
379  cellZoneMap,
380 
381  newPoints, // if empty signals no inflation.
382  oldPatchStarts,
383  oldPatchNMeshPoints,
384  oldCellVolumes,
385  true // steal storage.
386  );
387 
388  // At this point all member DynamicList (pointMap_, cellMap_ etc.) will
389  // be invalid.
390 }
391 
392 
393 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
Foam::autoPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:109
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::polyBoundaryMesh
A polyBoundaryMesh is a polyPatch list with additional search methods and registered IO.
Definition: polyBoundaryMesh.H:62
Foam::primitiveMesh::nFaces
label nFaces() const
Number of mesh faces.
Definition: primitiveMeshI.H:90
Foam::pointZone
A subset of mesh points.
Definition: pointZone.H:65
polyTopoChange.H
Foam::polyMesh::cellZones
const cellZoneMesh & cellZones() const
Return cell zone mesh.
Definition: polyMesh.H:492
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:444
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
polyMesh.H
Foam::HashSet< label, Hash< label > >
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::cellZone
A subset of mesh cells.
Definition: cellZone.H:62
Foam::polyMesh::faceZones
const faceZoneMesh & faceZones() const
Return face zone mesh.
Definition: polyMesh.H:486
Foam::primitiveMesh::nCells
label nCells() const
Number of mesh cells.
Definition: primitiveMeshI.H:96
Foam::Field< vector >
Foam::faceZone
A subset of mesh faces organised as a primitive patch.
Definition: faceZone.H:65
Foam::polyMesh::pointZones
const pointZoneMesh & pointZones() const
Return point zone mesh.
Definition: polyMesh.H:480
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::ZoneMesh< pointZone, polyMesh >
Foam::PtrList::clone
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::primitiveMesh::cellVolumes
const scalarField & cellVolumes() const
Definition: primitiveMeshCellCentresAndVols.C:96
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:385
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::List< label >
Foam::HashSetOps::used
labelHashSet used(const bitSet &select)
Convert a bitset to a labelHashSet of the indices used.
Definition: HashOps.C:35
Foam::primitiveMesh::nPoints
label nPoints() const
Number of mesh points.
Definition: primitiveMeshI.H:37
Foam::labelUList
UList< label > labelUList
A UList of labels.
Definition: UList.H:80
Foam::polyTopoChange::makeMesh
autoPtr< mapPolyMesh > makeMesh(autoPtr< Type > &newMesh, const IOobject &io, const polyMesh &mesh, const bool syncParallel=true, const bool orderCells=false, const bool orderPoints=false)
Create new mesh with old mesh patches. Additional dictionaries.
HashOps.H