-
Notifications
You must be signed in to change notification settings - Fork 122
/
removeCellLayer.C
420 lines (347 loc) · 12.3 KB
/
removeCellLayer.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "layerAdditionRemoval.H"
#include "polyMesh.H"
#include "primitiveMesh.H"
#include "polyTopoChange.H"
#include "oppositeFace.H"
#include "polyTopoChanger.H"
#include "polyRemoveCell.H"
#include "polyRemoveFace.H"
#include "polyRemovePoint.H"
#include "polyModifyFace.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::layerAdditionRemoval::validCollapse() const
{
// Check for valid layer collapse
// - no boundary-to-boundary collapse
if (debug)
{
Pout<< "Checking layer collapse for object " << name() << endl;
}
// Grab the face collapse mapping
const polyMesh& mesh = topoChanger().mesh();
const labelList& ftc = facesPairing();
const labelList& mf = mesh.faceZones()[faceZoneID_.index()];
label nBoundaryHits = 0;
forAll(mf, facei)
{
if
(
!mesh.isInternalFace(mf[facei])
&& !mesh.isInternalFace(ftc[facei])
)
{
nBoundaryHits++;
}
}
if (debug)
{
Pout<< "Finished checking layer collapse for object "
<< name() <<". Number of boundary-on-boundary hits: "
<< nBoundaryHits << endl;
}
if (returnReduce(nBoundaryHits, sumOp<label>()) > 0)
{
return false;
}
else
{
return true;
}
}
void Foam::layerAdditionRemoval::removeCellLayer
(
polyTopoChange& ref
) const
{
// Algorithm for layer removal. Second phase: topological change
// 2) Go through all the faces of the master cell layer and remove
// the ones that are not in the master face zone.
//
// 3) Grab all the faces coming out of points that are collapsed
// and select the ones that are not marked for removal. Go
// through the remaining faces and replace the point to remove by
// the equivalent point in the master face zone.
if (debug)
{
Pout<< "Removing the cell layer for object " << name() << endl;
}
const polyMesh& mesh = topoChanger().mesh();
const labelList& own = mesh.faceOwner();
const labelList& nei = mesh.faceNeighbour();
const labelList& ptc = pointsPairing();
const labelList& ftc = facesPairing();
// Remove all the cells from the master layer
const labelList& mc =
mesh.faceZones()[faceZoneID_.index()].masterCells();
forAll(mc, facei)
{
label slaveSideCell = own[ftc[facei]];
if (mesh.isInternalFace(ftc[facei]) && slaveSideCell == mc[facei])
{
// Owner cell of the face is being removed.
// Grab the neighbour instead
slaveSideCell = nei[ftc[facei]];
}
ref.setAction(polyRemoveCell(mc[facei], slaveSideCell));
}
// Remove all the faces from the master layer cells which are not in
// the master face layer
labelHashSet facesToRemoveMap(mc.size()*primitiveMesh::facesPerCell_);
const cellList& cells = mesh.cells();
forAll(mc, celli)
{
const cell& curCell = cells[mc[celli]];
forAll(curCell, facei)
{
// Check if the face is in the master zone. If not, remove it
if
(
mesh.faceZones().whichZone(curCell[facei])
!= faceZoneID_.index()
)
{
facesToRemoveMap.insert(curCell[facei]);
}
}
}
forAllConstIter(labelHashSet, facesToRemoveMap, iter)
{
ref.setAction(polyRemoveFace(iter.key()));
}
// Remove all points that will be collapsed
forAll(ptc, pointi)
{
ref.setAction(polyRemovePoint(ptc[pointi]));
}
// Grab all faces coming off points to be deleted. If the face
// has not been deleted, replace the removed point with the
// equivalent from the master layer.
// Make a map of all point to be removed, giving the master point label
// for each of them
Map<label> removedPointMap(2*ptc.size());
const labelList& meshPoints =
mesh.faceZones()[faceZoneID_.index()]().meshPoints();
forAll(ptc, pointi)
{
removedPointMap.insert(ptc[pointi], meshPoints[pointi]);
}
const labelListList& pf = mesh.pointFaces();
const faceList& faces = mesh.faces();
// Make a list of faces to be modified using the map to avoid duplicates
labelHashSet facesToModify(ptc.size()*primitiveMesh::facesPerPoint_);
forAll(ptc, pointi)
{
const labelList& curFaces = pf[ptc[pointi]];
forAll(curFaces, facei)
{
if (!facesToRemoveMap.found(curFaces[facei]))
{
facesToModify.insert(curFaces[facei]);
}
}
}
labelList ftm = facesToModify.toc();
if (debug > 1)
{
Pout<< "faces to modify: " << ftm << endl;
}
forAll(ftm, facei)
{
// For every face to modify, copy the face and re-map the vertices.
// It is known all the faces will be changed since they hang off
// re-mapped vertices
label curFaceID = ftm[facei];
face newFace(faces[curFaceID]);
forAll(newFace, pointi)
{
Map<label>::iterator rpmIter =
removedPointMap.find(newFace[pointi]);
if (rpmIter != removedPointMap.end())
{
// Point mapped. Replace it
newFace[pointi] = rpmIter();
}
}
if (debug > 1)
{
Pout<< "face label: " << curFaceID
<< " old face: " << faces[curFaceID]
<< " new face: " << newFace << endl;
}
// Get face zone and its flip
label modifiedFaceZone = mesh.faceZones().whichZone(curFaceID);
bool modifiedFaceZoneFlip = false;
if (modifiedFaceZone >= 0)
{
const faceZone& fz = mesh.faceZones()[modifiedFaceZone];
modifiedFaceZoneFlip = fz.flipMap()[fz.whichFace(curFaceID)];
}
label newNeighbour = -1;
if (curFaceID < mesh.nInternalFaces())
{
newNeighbour = nei[curFaceID];
}
// Modify the face
ref.setAction
(
polyModifyFace
(
newFace, // modified face
curFaceID, // label of face being modified
own[curFaceID], // owner
newNeighbour, // neighbour
false, // face flip
mesh.boundaryMesh().whichPatch(curFaceID),// patch for face
false, // remove from zone
modifiedFaceZone, // zone for face
modifiedFaceZoneFlip // face flip in zone
)
);
}
// Modify the faces in the master layer to point past the removed cells
const labelList& mf = mesh.faceZones()[faceZoneID_.index()];
const boolList& mfFlip = mesh.faceZones()[faceZoneID_.index()].flipMap();
forAll(mf, facei)
{
// Grab the owner and neighbour of the faces to be collapsed and get rid
// of the cell to be removed
label masterSideCell = own[mf[facei]];
if (masterSideCell == mc[facei])
{
if (mesh.isInternalFace(mf[facei]))
{
// Owner cell of the face is being removed.
// Grab the neighbour instead
masterSideCell = nei[mf[facei]];
}
else
{
masterSideCell = -1;
}
}
label slaveSideCell = own[ftc[facei]];
if (slaveSideCell == mc[facei])
{
if (mesh.isInternalFace(ftc[facei]))
{
// Owner cell of the face is being removed.
// Grab the neighbour instead
slaveSideCell = nei[ftc[facei]];
}
else
{
slaveSideCell = -1;
}
}
// Find out if the face needs to be flipped
label newOwner = -1;
label newNeighbour = -1;
bool flipFace = false;
label newPatchID = -1;
label newZoneID = -1;
// Is any of the faces a boundary face? If so, grab the patch
// A boundary-to-boundary collapse is checked for in validCollapse()
// and cannot happen here.
if (!mesh.isInternalFace(mf[facei]))
{
// Master is the boundary face: it gets a new owner but no flip
newOwner = slaveSideCell;
newNeighbour = -1;
flipFace = false;
newPatchID = mesh.boundaryMesh().whichPatch(mf[facei]);
newZoneID = mesh.faceZones().whichZone(mf[facei]);
}
else if (!mesh.isInternalFace(ftc[facei]))
{
// Slave is the boundary face: grab its patch
newOwner = slaveSideCell;
newNeighbour = -1;
// Find out if the face flip is necessary
if (own[mf[facei]] == slaveSideCell)
{
flipFace = false;
}
else
{
flipFace = true;
}
newPatchID = mesh.boundaryMesh().whichPatch(ftc[facei]);
// The zone of the master face is preserved
newZoneID = mesh.faceZones().whichZone(mf[facei]);
}
else
{
// Both faces are internal: flip is decided based on which of the
// new cells around it has a lower label
newOwner = min(masterSideCell, slaveSideCell);
newNeighbour = max(masterSideCell, slaveSideCell);
if (newOwner == own[mf[facei]] || newNeighbour == nei[mf[facei]])
{
flipFace = false;
}
else
{
flipFace = true;
}
newPatchID = -1;
// The zone of the master face is preserved
newZoneID = mesh.faceZones().whichZone(mf[facei]);
}
// Modify the face and flip if necessary
face newFace = faces[mf[facei]];
bool zoneFlip = mfFlip[facei];
if (flipFace)
{
newFace.flip();
zoneFlip = !zoneFlip;
}
if (debug > 1)
{
Pout<< "Modifying face " << mf[facei]
<< " newFace: " << newFace << nl
<< " newOwner: " << newOwner
<< " newNeighbour: " << newNeighbour
<< " flipFace: " << flipFace
<< " newPatchID: " << newPatchID
<< " newZoneID: " << newZoneID << nl
<< " oldOwn: " << own[mf[facei]]
<< " oldNei: " << nei[mf[facei]] << endl;
}
ref.setAction
(
polyModifyFace
(
newFace, // modified face
mf[facei], // label of face being modified
newOwner, // owner
newNeighbour, // neighbour
flipFace, // flip
newPatchID, // patch for face
false, // remove from zone
newZoneID, // new zone
zoneFlip // face flip in zone
)
);
}
}
// ************************************************************************* //