1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "layerAdditionRemoval.H"
29 #include "primitiveMesh.H"
30 #include "polyTopoChange.H"
31 #include "polyTopoChanger.H"
32 #include "polyAddPoint.H"
33 #include "polyAddCell.H"
34 #include "polyAddFace.H"
35 #include "polyModifyFace.H"
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 //- Calculate the offset to the next layer
40 Foam::tmp<Foam::vectorField> Foam::layerAdditionRemoval::extrusionDir() const
42 const polyMesh& mesh = topoChanger().mesh();
43 const primitiveFacePatch& masterFaceLayer =
44 mesh.faceZones()[faceZoneID_.index()]();
46 const pointField& points = mesh.points();
47 const labelList& mp = masterFaceLayer.meshPoints();
49 tmp<vectorField> textrusionDir(new vectorField(mp.size()));
50 vectorField& extrusionDir = textrusionDir();
52 if (setLayerPairing())
56 Pout<< "void layerAdditionRemoval::extrusionDir() const "
57 << " for object " << name() << " : "
58 << "Using edges for point insertion" << endl;
61 // Detected a valid layer. Grab the point and face collapse mapping
62 const labelList& ptc = pointsPairing();
64 forAll (extrusionDir, mpI)
66 extrusionDir[mpI] = points[ptc[mpI]] - points[mp[mpI]];
73 Pout<< "void layerAdditionRemoval::extrusionDir() const "
74 << " for object " << name() << " : "
75 << "A valid layer could not be found in front of "
76 << "the addition face layer. Using face-based "
77 << "point normals for point addition"
81 extrusionDir = minLayerThickness_*masterFaceLayer.pointNormals();
87 void Foam::layerAdditionRemoval::addCellLayer
92 // Insert the layer addition instructions into the topological change
95 // 1. For every point in the master zone add a new point
96 // 2. For every face in the master zone add a cell
97 // 3. Go through all the edges of the master zone. For all internal edges,
98 // add a face with the correct orientation and make it internal.
99 // For all boundary edges, find the patch it belongs to and add the face
101 // 4. Create a set of new faces on the patch image and assign them to be
102 // between the old master cells and the newly created cells
103 // 5. Modify all the faces in the patch such that they are located
104 // between the old slave cells and newly created cells
108 Pout<< "void layerAdditionRemoval::addCellLayer("
109 << "polyTopoChange& ref) const for object " << name() << " : "
110 << "Adding cell layer" << endl;
115 const polyMesh& mesh = topoChanger().mesh();
116 const primitiveFacePatch& masterFaceLayer =
117 mesh.faceZones()[faceZoneID_.index()]();
119 const pointField& points = mesh.points();
120 const labelList& mp = masterFaceLayer.meshPoints();
122 // Get the extrusion direction for the added points
124 tmp<vectorField> tpointOffsets = extrusionDir();
126 // Add the new points
127 labelList addedPoints(mp.size());
131 // Add the point nominal thickness away from the master zone point
132 // and grab the label
133 addedPoints[pointI] =
138 points[mp[pointI]] // point
139 + addDelta_*tpointOffsets()[pointI],
140 mp[pointI], // master point
141 -1, // zone for point
142 true // supports a cell
147 // Pout << "mp: " << mp << " addedPoints: " << addedPoints << endl;
150 const labelList& mc =
151 mesh.faceZones()[faceZoneID_.index()].masterCells();
152 const labelList& sc =
153 mesh.faceZones()[faceZoneID_.index()].slaveCells();
154 // Pout << "mc: " << mc << " sc: " << sc << endl;
155 const labelList& mf = mesh.faceZones()[faceZoneID_.index()];
156 const boolList& mfFlip = mesh.faceZones()[faceZoneID_.index()].flipMap();
158 const labelList& own = mesh.faceOwner();
159 const labelList& nei = mesh.faceNeighbour();
161 labelList addedCells(mf.size());
172 mf[faceI], // master face
179 // Create the new faces
181 // Grab the local faces from the master zone
182 const faceList& zoneFaces = masterFaceLayer.localFaces();
184 // Create the new faces by copying the master zone. All the added
185 // faces need to point into the newly created cells, which means
186 // all the faces from the master layer are flipped. The flux flip
187 // is determined from the original master layer face and the face
188 // owner: if the master cell is equal to the face owner the flux
189 // remains the same; otherwise it is flipped
191 forAll (zoneFaces, faceI)
193 const face oldFace = zoneFaces[faceI].reverseFace();
195 face newFace(oldFace.size());
197 forAll (oldFace, pointI)
199 newFace[pointI] = addedPoints[oldFace[pointI]];
202 bool flipFaceFlux = false;
204 // Flip the face as necessary
207 mc[faceI] == nei[mf[faceI]]
208 || !mesh.isInternalFace(mf[faceI])
221 addedCells[faceI], // neighbour
224 mf[faceI], // master face for addition
225 flipFaceFlux, // flux flip
226 -1, // patch for face
228 false // face zone flip
232 // Pout << "adding face: " << newFace << " own: " << mc[faceI] << " nei: " << addedCells[faceI] << endl;
235 // Modify the faces from the master zone for the new neighbour
237 const faceList& faces = mesh.faces();
238 // Pout << "mfFlip: " << mfFlip << endl;
241 const label curfaceID = mf[faceI];
243 // If the face is internal, modify its owner to be the newly
244 // created cell. No flip is necessary
245 if (!mesh.isInternalFace(curfaceID))
251 faces[curfaceID], // modified face
252 curfaceID, // label of face being modified
253 addedCells[faceI], // owner
256 mesh.boundaryMesh().whichPatch(curfaceID),// patch for face
257 false, // remove from zone
258 faceZoneID_.index(), // zone for face
259 mfFlip[faceI] // face flip in zone
262 // Pout << "Modifying a boundary face. Face: " << curfaceID << " flip: " << mfFlip[faceI] << endl;
264 // If slave cell is owner, the face remains the same (but with
265 // a new neighbour - the newly created cell). Otherwise, the
267 else if (sc[faceI] == own[curfaceID])
269 // Orientation is good, just change neighbour
274 faces[curfaceID], // modified face
275 curfaceID, // label of face being modified
276 own[curfaceID], // owner
277 addedCells[faceI], // neighbour
279 mesh.boundaryMesh().whichPatch(curfaceID),// patch for face
280 false, // remove from zone
281 faceZoneID_.index(), // zone for face
282 mfFlip[faceI] // face flip in zone
286 // Pout << "modify face, no flip " << curfaceID << " own: " << own[curfaceID] << " nei: " << addedCells[faceI] << endl;
290 // Change in orientation; flip face
295 faces[curfaceID].reverseFace(), // modified face
296 curfaceID, // label of face being modified
297 nei[curfaceID], // owner
298 addedCells[faceI], // neighbour
300 mesh.boundaryMesh().whichPatch(curfaceID), // patch for face
301 false, // remove from zone
302 faceZoneID_.index(), // zone for face
303 !mfFlip[faceI] // face flip in zone
306 // Pout << "modify face, with flip " << curfaceID << " own: " << own[curfaceID] << " nei: " << addedCells[faceI] << endl;
310 // Create new faces from edges
312 const edgeList& zoneLocalEdges = masterFaceLayer.edges();
314 const labelListList& edgeFaces = masterFaceLayer.edgeFaces();
316 label nInternalEdges = masterFaceLayer.nInternalEdges();
318 const labelList& meshEdges =
319 mesh.faceZones()[faceZoneID_.index()].meshEdges();
321 // Do all internal edges
322 for (label curEdgeID = 0; curEdgeID < nInternalEdges; curEdgeID++)
326 newFace[0] = mp[zoneLocalEdges[curEdgeID].start()];
327 newFace[1] = mp[zoneLocalEdges[curEdgeID].end()];
328 newFace[2] = addedPoints[zoneLocalEdges[curEdgeID].end()];
329 newFace[3] = addedPoints[zoneLocalEdges[curEdgeID].start()];
336 addedCells[edgeFaces[curEdgeID][0]], // owner
337 addedCells[edgeFaces[curEdgeID][1]], // neighbour
339 meshEdges[curEdgeID], // master edge
342 -1, // patch for face
344 false // face zone flip
348 // Pout << "Add internal face off edge: " << newFace << " own: " << addedCells[edgeFaces[curEdgeID][0]] << " nei: " << addedCells[edgeFaces[curEdgeID][1]] << endl;
351 // Prepare creation of faces from boundary edges.
353 // A tricky part of the algorithm is finding the patch into which the
354 // newly created face will be added. For this purpose, take the edge
355 // and grab all the faces coming from it. From the set of faces
356 // eliminate the internal faces and find the boundary face from the rest.
357 // If there is more than one boundary face (excluding the ones in
358 // the master zone), the patch with the lower label is selected.
360 const labelListList& meshEdgeFaces = mesh.edgeFaces();
362 const faceZoneMesh& zoneMesh = mesh.faceZones();
364 // Do all boundary edges
368 label curEdgeID = nInternalEdges;
369 curEdgeID < zoneLocalEdges.size();
374 newFace[0] = mp[zoneLocalEdges[curEdgeID].start()];
375 newFace[1] = mp[zoneLocalEdges[curEdgeID].end()];
376 newFace[2] = addedPoints[zoneLocalEdges[curEdgeID].end()];
377 newFace[3] = addedPoints[zoneLocalEdges[curEdgeID].start()];
379 // Determine the patch for insertion
380 const labelList& curFaces = meshEdgeFaces[meshEdges[curEdgeID]];
385 forAll (curFaces, faceI)
387 const label cf = curFaces[faceI];
389 if (!mesh.isInternalFace(cf))
391 // Face not internal. Check if it is in the zone
392 if (zoneMesh.whichZone(cf) != faceZoneID_.index())
394 // Found the face in a boundary patch which is not in zone
395 patchID = mesh.boundaryMesh().whichPatch(cf);
396 zoneID = mesh.faceZones().whichZone(cf);
407 "void Foam::layerAdditionRemoval::setRefinement("
408 "polyTopoChange& ref)"
409 ) << "Cannot find patch for edge " << meshEdges[curEdgeID]
410 << ". Edge: " << mesh.edges()[meshEdges[curEdgeID]]
411 << abort(FatalError);
419 addedCells[edgeFaces[curEdgeID][0]], // owner
422 meshEdges[curEdgeID], // master edge
425 patchID, // patch for face
427 false // zone face flip
430 // Pout << "add boundary face: " << newFace << " into patch " << patchID << " own: " << addedCells[edgeFaces[curEdgeID][0]] << endl;
433 // Modify the remaining faces of the master cells to reconnect to the new
435 // Algorithm: Go through all the cells of the master zone and make
436 // a map of faces to avoid duplicates. Do not insert the faces in
437 // the master patch (as they have already been dealt with). Make
438 // a master layer point renumbering map, which for every point in
439 // the master layer gives its new label. Loop through all faces in
440 // the map and attempt to renumber them using the master layer
441 // point renumbering map. Once the face is renumbered, compare it
442 // with the original face; if they are the same, the face has not
443 // changed; if not, modify the face but keep all of its old
444 // attributes (apart from the vertex numbers).
446 // Create the map of faces in the master cell layer
447 labelHashSet masterCellFaceMap(primitiveMesh::facesPerCell_*mc.size());
449 const cellList& cells = mesh.cells();
453 const labelList& curFaces = cells[mc[cellI]];
455 forAll (curFaces, faceI)
457 // Check if the face belongs to the master zone; if not add it
458 if (zoneMesh.whichZone(curFaces[faceI]) != faceZoneID_.index())
460 masterCellFaceMap.insert(curFaces[faceI]);
465 // Create the master layer point map
466 Map<label> masterLayerPointMap(2*mp.size());
470 masterLayerPointMap.insert
477 // Grab the list of faces of the master layer
478 const labelList masterCellFaces = masterCellFaceMap.toc();
480 forAll (masterCellFaces, faceI)
482 // Attempt to renumber the face using the masterLayerPointMap.
483 // Missing point remain the same
485 const label curFaceID = masterCellFaces[faceI];
487 const face& oldFace = faces[curFaceID];
489 face newFace(oldFace.size());
491 bool changed = false;
493 forAll (oldFace, pointI)
495 if (masterLayerPointMap.found(oldFace[pointI]))
499 newFace[pointI] = masterLayerPointMap.find(oldFace[pointI])();
503 newFace[pointI] = oldFace[pointI];
507 // If the face has changed, create a modification entry
510 // Get face zone and its flip
511 label modifiedFaceZone = mesh.faceZones().whichZone(curFaceID);
512 bool modifiedFaceZoneFlip = false;
514 if (modifiedFaceZone >= 0)
516 modifiedFaceZoneFlip =
517 mesh.faceZones()[modifiedFaceZone].flipMap()
519 mesh.faceZones()[modifiedFaceZone].whichFace(curFaceID)
523 if (mesh.isInternalFace(curFaceID))
529 newFace, // modified face
530 curFaceID, // label of face being modified
531 own[curFaceID], // owner
532 nei[curFaceID], // neighbour
534 -1, // patch for face
535 false, // remove from zone
536 modifiedFaceZone, // zone for face
537 modifiedFaceZoneFlip // face flip in zone
540 // Pout << "modifying stick-out face. Internal Old face: " << oldFace << " new face: " << newFace << " own: " << own[curFaceID] << " nei: " << nei[curFaceID] << endl;
548 newFace, // modified face
549 curFaceID, // label of face being modified
550 own[curFaceID], // owner
553 mesh.boundaryMesh().whichPatch(curFaceID), // patch for face
554 false, // remove from zone
555 modifiedFaceZone, // zone for face
556 modifiedFaceZoneFlip // face flip in zone
559 // Pout << "modifying stick-out face. Boundary Old face: " << oldFace << " new face: " << newFace << " own: " << own[curFaceID] << " patch: " << mesh.boundaryMesh().whichPatch(curFaceID) << endl;
566 Pout<< "void layerAdditionRemoval::addCellLayer("
567 << "polyTopoChange& ref) const "
568 << " for object " << name() << " : "
569 << "Finished adding cell layer" << endl;
574 // ************************************************************************* //