PatchToolsNormals.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) 2019 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 "PatchTools.H"
30 #include "polyMesh.H"
31 #include "indirectPrimitivePatch.H"
32 #include "globalMeshData.H"
33 
34 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35 
36 template
37 <
38  class Face,
39  template<class> class FaceList,
40  class PointField,
41  class PointType
42 >
43 
46 (
47  const polyMesh& mesh,
49 )
50 {
51  const globalMeshData& globalData = mesh.globalData();
52  const indirectPrimitivePatch& coupledPatch = globalData.coupledPatch();
53  const Map<label>& coupledPatchMP = coupledPatch.meshPointMap();
54  const mapDistribute& map = globalData.globalPointSlavesMap();
55  const globalIndexAndTransform& transforms =
56  globalData.globalTransforms();
57 
58 
59  // Combine normals. Note: do on all master points. Cannot just use
60  // patch points since the master point does not have to be on the
61  // patch!
62 
63  pointField coupledPointNormals(map.constructSize(), Zero);
64 
65  {
66  // Collect local pointFaces (sized on patch points only)
67  List<List<point>> pointFaceNormals(map.constructSize());
68  forAll(p.meshPoints(), patchPointi)
69  {
70  const label meshPointi = p.meshPoints()[patchPointi];
71 
72  const auto fnd = coupledPatchMP.cfind(meshPointi);
73  if (fnd.found())
74  {
75  const label coupledPointi = fnd.val();
76 
77  List<point>& pNormals = pointFaceNormals[coupledPointi];
78  const labelList& pFaces = p.pointFaces()[patchPointi];
79  pNormals.setSize(pFaces.size());
80  forAll(pFaces, i)
81  {
82  pNormals[i] = p.faceNormals()[pFaces[i]];
83  }
84  }
85  }
86 
87 
88  // Pull remote data into local slots
89  map.distribute
90  (
91  transforms,
92  pointFaceNormals,
94  );
95 
96 
97  // Combine all face normals (-local, -remote,untransformed,
98  // -remote,transformed)
99 
100  const labelListList& slaves = globalData.globalPointSlaves();
101  const labelListList& transformedSlaves =
102  globalData.globalPointTransformedSlaves();
103 
104  forAll(slaves, coupledPointi)
105  {
106  const labelList& slaveSlots = slaves[coupledPointi];
107  const labelList& transformedSlaveSlots =
108  transformedSlaves[coupledPointi];
109 
110  point& n = coupledPointNormals[coupledPointi];
111 
112  // Local entries
113  const List<point>& local = pointFaceNormals[coupledPointi];
114 
115  label nFaces =
116  local.size()
117  + slaveSlots.size()
118  + transformedSlaveSlots.size();
119 
120  n = sum(local);
121 
122  // Add any remote face normals
123  forAll(slaveSlots, i)
124  {
125  n += sum(pointFaceNormals[slaveSlots[i]]);
126  }
127  forAll(transformedSlaveSlots, i)
128  {
129  n += sum(pointFaceNormals[transformedSlaveSlots[i]]);
130  }
131 
132  if (nFaces >= 1)
133  {
134  n /= mag(n)+VSMALL;
135  }
136 
137  // Put back into slave slots
138  forAll(slaveSlots, i)
139  {
140  coupledPointNormals[slaveSlots[i]] = n;
141  }
142  forAll(transformedSlaveSlots, i)
143  {
144  coupledPointNormals[transformedSlaveSlots[i]] = n;
145  }
146  }
147 
148 
149  // Send back
151  (
152  transforms,
153  coupledPointNormals.size(),
154  coupledPointNormals,
156  );
157  }
158 
159 
160  // 1. Start off with local normals (note:without calculating pointNormals
161  // to avoid them being stored)
162 
163  tmp<pointField> textrudeN(new pointField(p.nPoints(), Zero));
164  pointField& extrudeN = textrudeN.ref();
165  {
166  const faceList& localFaces = p.localFaces();
167  const vectorField& faceNormals = p.faceNormals();
168 
169  forAll(localFaces, facei)
170  {
171  const face& f = localFaces[facei];
172  const vector& n = faceNormals[facei];
173  forAll(f, fp)
174  {
175  extrudeN[f[fp]] += n;
176  }
177  }
178  extrudeN /= mag(extrudeN)+VSMALL;
179  }
180 
181 
182  // 2. Override patch normals on coupled points
183  forAll(p.meshPoints(), patchPointi)
184  {
185  const label meshPointi = p.meshPoints()[patchPointi];
186 
187  const auto fnd = coupledPatchMP.cfind(meshPointi);
188  if (fnd.found())
189  {
190  const label coupledPointi = fnd.val();
191  extrudeN[patchPointi] = coupledPointNormals[coupledPointi];
192  }
193  }
194 
195  return textrudeN;
196 }
197 
198 
199 template
200 <
201  class Face,
202  template<class> class FaceList,
203  class PointField,
204  class PointType
205 >
206 
209 (
210  const polyMesh& mesh,
212  const labelList& patchEdges,
213  const labelList& coupledEdges
214 )
215 {
216  // 1. Start off with local normals
217 
218  tmp<pointField> tedgeNormals(new pointField(p.nEdges(), Zero));
219  pointField& edgeNormals = tedgeNormals.ref();
220  {
221  const labelListList& edgeFaces = p.edgeFaces();
222  const vectorField& faceNormals = p.faceNormals();
223 
224  forAll(edgeFaces, edgei)
225  {
226  const labelList& eFaces = edgeFaces[edgei];
227  for (const label facei : eFaces)
228  {
229  edgeNormals[edgei] += faceNormals[facei];
230  }
231  }
232  edgeNormals /= mag(edgeNormals)+VSMALL;
233  }
234 
235 
236 
237  const globalMeshData& globalData = mesh.globalData();
238  const mapDistribute& map = globalData.globalEdgeSlavesMap();
239 
240 
241  // Convert patch-edge data into cpp-edge data
242  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243 
244  //- Construct with all data in consistent orientation
245  pointField cppEdgeData(map.constructSize(), Zero);
246 
247  forAll(patchEdges, i)
248  {
249  label patchEdgeI = patchEdges[i];
250  label coupledEdgeI = coupledEdges[i];
251  cppEdgeData[coupledEdgeI] = edgeNormals[patchEdgeI];
252  }
253 
254 
255  // Synchronise
256  // ~~~~~~~~~~~
257 
258  globalData.syncData
259  (
260  cppEdgeData,
261  globalData.globalEdgeSlaves(),
262  globalData.globalEdgeTransformedSlaves(),
263  map,
264  globalData.globalTransforms(),
265  plusEqOp<point>(), // add since normalised later on
267  );
268  cppEdgeData /= mag(cppEdgeData)+VSMALL;
269 
270 
271  // Back from cpp-edge to patch-edge data
272  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
273 
274  forAll(patchEdges, i)
275  {
276  label patchEdgeI = patchEdges[i];
277  label coupledEdgeI = coupledEdges[i];
278  edgeNormals[patchEdgeI] = cppEdgeData[coupledEdgeI];
279  }
280 
281  return tedgeNormals;
282 }
283 
284 
285 // ************************************************************************* //
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::globalMeshData::globalPointTransformedSlaves
const labelListList & globalPointTransformedSlaves() const
Definition: globalMeshData.C:2200
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::globalMeshData::globalPointSlaves
const labelListList & globalPointSlaves() const
Definition: globalMeshData.C:2190
Foam::globalMeshData::globalEdgeSlavesMap
const mapDistribute & globalEdgeSlavesMap() const
Definition: globalMeshData.C:2265
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
PatchTools.H
globalMeshData.H
Foam::Map< label >
Foam::globalMeshData::globalPointSlavesMap
const mapDistribute & globalPointSlavesMap() const
Definition: globalMeshData.C:2211
Foam::transform
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:519
polyMesh.H
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:290
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::tmp::ref
T & ref() const
Definition: tmpI.H:258
Foam::label
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:62
pFaces
Info<< "Finished reading KIVA file"<< endl;cellShapeList cellShapes(nPoints);labelList cellZoning(nPoints, -1);const cellModel &hex=cellModel::ref(cellModel::HEX);labelList hexLabels(8);label activeCells=0;labelList pointMap(nPoints);forAll(pointMap, i){ pointMap[i]=i;}for(label i=0;i< nPoints;i++){ if(f[i] > 0.0) { hexLabels[0]=i;hexLabels[1]=i1tab[i];hexLabels[2]=i3tab[i1tab[i]];hexLabels[3]=i3tab[i];hexLabels[4]=i8tab[i];hexLabels[5]=i1tab[i8tab[i]];hexLabels[6]=i3tab[i1tab[i8tab[i]]];hexLabels[7]=i3tab[i8tab[i]];cellShapes[activeCells]=cellShape(hex, hexLabels);edgeList edges=cellShapes[activeCells].edges();forAll(edges, ei) { if(edges[ei].mag(points)< SMALL) { label start=pointMap[edges[ei].start()];while(start !=pointMap[start]) { start=pointMap[start];} label end=pointMap[edges[ei].end()];while(end !=pointMap[end]) { end=pointMap[end];} label minLabel=min(start, end);pointMap[start]=pointMap[end]=minLabel;} } cellZoning[activeCells]=idreg[i];activeCells++;}}cellShapes.setSize(activeCells);cellZoning.setSize(activeCells);forAll(cellShapes, celli){ cellShape &cs=cellShapes[celli];forAll(cs, i) { cs[i]=pointMap[cs[i]];} cs.collapse();}label bcIDs[11]={-1, 0, 2, 4, -1, 5, -1, 6, 7, 8, 9};const label nBCs=12;const word *kivaPatchTypes[nBCs]={ &wallPolyPatch::typeName, &wallPolyPatch::typeName, &wallPolyPatch::typeName, &wallPolyPatch::typeName, &symmetryPolyPatch::typeName, &wedgePolyPatch::typeName, &polyPatch::typeName, &polyPatch::typeName, &polyPatch::typeName, &polyPatch::typeName, &symmetryPolyPatch::typeName, &oldCyclicPolyPatch::typeName};enum patchTypeNames{ PISTON, VALVE, LINER, CYLINDERHEAD, AXIS, WEDGE, INFLOW, OUTFLOW, PRESIN, PRESOUT, SYMMETRYPLANE, CYCLIC};const char *kivaPatchNames[nBCs]={ "piston", "valve", "liner", "cylinderHead", "axis", "wedge", "inflow", "outflow", "presin", "presout", "symmetryPlane", "cyclic"};List< SLList< face > > pFaces[nBCs]
Definition: readKivaGrid.H:235
faceNormals
surfaceVectorField faceNormals(mesh.Sf()/mesh.magSf())
Foam::Field< vector >
Foam::globalMeshData::globalTransforms
const globalIndexAndTransform & globalTransforms() const
Global transforms numbering.
Definition: globalMeshData.C:2180
Foam::mapDistribute
Class containing processor-to-processor mapping information.
Definition: mapDistribute.H:163
Foam::globalMeshData::globalEdgeSlaves
const labelListList & globalEdgeSlaves() const
Definition: globalMeshData.C:2234
Foam::globalMeshData::syncData
static void syncData(List< Type > &elems, const labelListList &slaves, const labelListList &transformedSlaves, const mapDistribute &slavesMap, const globalIndexAndTransform &, const CombineOp &cop, const TransformOp &top)
Helper: synchronise data with transforms.
Definition: globalMeshDataTemplates.C:37
Foam::mapDistribute::distribute
void distribute(List< T > &fld, const bool dummyTransform=true, const int tag=UPstream::msgType()) const
Distribute data using default commsType.
Definition: mapDistributeTemplates.C:152
indirectPrimitivePatch.H
Foam::globalMeshData
Various mesh related information for a parallel run. Upon construction, constructs all info using par...
Definition: globalMeshData.H:106
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::mapDistribute::transform
Default transformation behaviour.
Definition: mapDistribute.H:209
Foam::mapDistributeBase::constructSize
label constructSize() const
Constructed data size.
Definition: mapDistributeBase.H:270
Foam::globalMeshData::globalEdgeTransformedSlaves
const labelListList & globalEdgeTransformedSlaves() const
Definition: globalMeshData.C:2244
f
labelList f(nPoints)
Foam::PatchTools::pointNormals
static tmp< pointField > pointNormals(const polyMesh &, const PrimitivePatch< Face, FaceList, PointField, PointType > &)
Return parallel consistent point normals for patches using mesh points.
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::globalMeshData::coupledPatch
const indirectPrimitivePatch & coupledPatch() const
Return patch of all coupled faces.
Definition: globalMeshData.C:2066
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
Foam::mapDistribute::reverseDistribute
void reverseDistribute(const label constructSize, List< T > &, const bool dummyTransform=true, const int tag=UPstream::msgType()) const
Reverse distribute data using default commsType.
Definition: mapDistributeTemplates.C:182
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:74
Foam::plusEqOp
Definition: ops.H:72
Foam::PrimitivePatch::meshPointMap
const Map< label > & meshPointMap() const
Mesh point map.
Definition: PrimitivePatch.C:438
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::PatchTools::edgeNormals
static tmp< pointField > edgeNormals(const polyMesh &, const PrimitivePatch< Face, FaceList, PointField, PointType > &, const labelList &patchEdges, const labelList &coupledEdges)
Return parallel consistent edge normals for patches using mesh points.
Foam::globalIndexAndTransform
Determination and storage of the possible independent transforms introduced by coupledPolyPatches,...
Definition: globalIndexAndTransform.H:64
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:90