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-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 "PatchTools.H"
30 #include "polyMesh.H"
31 #include "indirectPrimitivePatch.H"
32 #include "globalMeshData.H"
33 
34 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35 
36 template<class FaceList, class PointField>
39 (
40  const polyMesh& mesh,
42 )
43 {
44  const globalMeshData& globalData = mesh.globalData();
45  const indirectPrimitivePatch& coupledPatch = globalData.coupledPatch();
46  const Map<label>& coupledPatchMP = coupledPatch.meshPointMap();
47  const mapDistribute& map = globalData.globalPointSlavesMap();
48  const globalIndexAndTransform& transforms =
49  globalData.globalTransforms();
50 
51 
52  // Combine normals. Note: do on all master points. Cannot just use
53  // patch points since the master point does not have to be on the
54  // patch!
55 
56  pointField coupledPointNormals(map.constructSize(), Zero);
57 
58  {
59  // Collect local pointFaces (sized on patch points only)
60  List<List<point>> pointFaceNormals(map.constructSize());
61  forAll(p.meshPoints(), patchPointi)
62  {
63  const label meshPointi = p.meshPoints()[patchPointi];
64 
65  const auto fnd = coupledPatchMP.cfind(meshPointi);
66  if (fnd.found())
67  {
68  const label coupledPointi = fnd.val();
69 
70  List<point>& pNormals = pointFaceNormals[coupledPointi];
71  const labelList& pFaces = p.pointFaces()[patchPointi];
72  pNormals.setSize(pFaces.size());
73  forAll(pFaces, i)
74  {
75  pNormals[i] = p.faceNormals()[pFaces[i]];
76  }
77  }
78  }
79 
80 
81  // Pull remote data into local slots
82  map.distribute
83  (
84  transforms,
85  pointFaceNormals,
87  );
88 
89 
90  // Combine all face normals (-local, -remote,untransformed,
91  // -remote,transformed)
92 
93  const labelListList& slaves = globalData.globalPointSlaves();
94  const labelListList& transformedSlaves =
95  globalData.globalPointTransformedSlaves();
96 
97  forAll(slaves, coupledPointi)
98  {
99  const labelList& slaveSlots = slaves[coupledPointi];
100  const labelList& transformedSlaveSlots =
101  transformedSlaves[coupledPointi];
102 
103  point& n = coupledPointNormals[coupledPointi];
104 
105  // Local entries
106  const List<point>& local = pointFaceNormals[coupledPointi];
107 
108  label nFaces =
109  local.size()
110  + slaveSlots.size()
111  + transformedSlaveSlots.size();
112 
113  n = sum(local);
114 
115  // Add any remote face normals
116  forAll(slaveSlots, i)
117  {
118  n += sum(pointFaceNormals[slaveSlots[i]]);
119  }
120  forAll(transformedSlaveSlots, i)
121  {
122  n += sum(pointFaceNormals[transformedSlaveSlots[i]]);
123  }
124 
125  if (nFaces >= 1)
126  {
127  n /= mag(n)+VSMALL;
128  }
129 
130  // Put back into slave slots
131  forAll(slaveSlots, i)
132  {
133  coupledPointNormals[slaveSlots[i]] = n;
134  }
135  forAll(transformedSlaveSlots, i)
136  {
137  coupledPointNormals[transformedSlaveSlots[i]] = n;
138  }
139  }
140 
141 
142  // Send back
144  (
145  transforms,
146  coupledPointNormals.size(),
147  coupledPointNormals,
149  );
150  }
151 
152 
153  // 1. Start off with local normals (note:without calculating pointNormals
154  // to avoid them being stored)
155 
156  tmp<pointField> textrudeN(new pointField(p.nPoints(), Zero));
157  pointField& extrudeN = textrudeN.ref();
158  {
159  const faceList& localFaces = p.localFaces();
160  const vectorField& faceNormals = p.faceNormals();
161 
162  forAll(localFaces, facei)
163  {
164  const face& f = localFaces[facei];
165  const vector& n = faceNormals[facei];
166  forAll(f, fp)
167  {
168  extrudeN[f[fp]] += n;
169  }
170  }
171  extrudeN /= mag(extrudeN)+VSMALL;
172  }
173 
174 
175  // 2. Override patch normals on coupled points
176  forAll(p.meshPoints(), patchPointi)
177  {
178  const label meshPointi = p.meshPoints()[patchPointi];
179 
180  const auto fnd = coupledPatchMP.cfind(meshPointi);
181  if (fnd.found())
182  {
183  const label coupledPointi = fnd.val();
184  extrudeN[patchPointi] = coupledPointNormals[coupledPointi];
185  }
186  }
187 
188  return textrudeN;
189 }
190 
191 
192 template<class FaceList, class PointField>
195 (
196  const polyMesh& mesh,
198  const labelList& patchEdges,
199  const labelList& coupledEdges
200 )
201 {
202  // 1. Start off with local normals
203 
204  auto tedgeNormals = tmp<pointField>::New(p.nEdges(), Zero);
205  auto& edgeNormals = tedgeNormals.ref();
206 
207  {
208  const labelListList& edgeFaces = p.edgeFaces();
209  const vectorField& faceNormals = p.faceNormals();
210 
211  forAll(edgeFaces, edgei)
212  {
213  const labelList& eFaces = edgeFaces[edgei];
214  for (const label facei : eFaces)
215  {
216  edgeNormals[edgei] += faceNormals[facei];
217  }
218  }
219  edgeNormals /= mag(edgeNormals)+VSMALL;
220  }
221 
222 
223 
224  const globalMeshData& globalData = mesh.globalData();
225  const mapDistribute& map = globalData.globalEdgeSlavesMap();
226 
227 
228  // Convert patch-edge data into cpp-edge data
229  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230 
231  //- Construct with all data in consistent orientation
232  pointField cppEdgeData(map.constructSize(), Zero);
233 
234  forAll(patchEdges, i)
235  {
236  label patchEdgeI = patchEdges[i];
237  label coupledEdgeI = coupledEdges[i];
238  cppEdgeData[coupledEdgeI] = edgeNormals[patchEdgeI];
239  }
240 
241 
242  // Synchronise
243  // ~~~~~~~~~~~
244 
245  globalData.syncData
246  (
247  cppEdgeData,
248  globalData.globalEdgeSlaves(),
249  globalData.globalEdgeTransformedSlaves(),
250  map,
251  globalData.globalTransforms(),
252  plusEqOp<point>(), // add since normalised later on
254  );
255  cppEdgeData /= mag(cppEdgeData)+VSMALL;
256 
257 
258  // Back from cpp-edge to patch-edge data
259  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
260 
261  forAll(patchEdges, i)
262  {
263  label patchEdgeI = patchEdges[i];
264  label coupledEdgeI = coupledEdges[i];
265  edgeNormals[patchEdgeI] = cppEdgeData[coupledEdgeI];
266  }
267 
268  return tedgeNormals;
269 }
270 
271 
272 // ************************************************************************* //
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::globalMeshData::globalPointTransformedSlaves
const labelListList & globalPointTransformedSlaves() const
Definition: globalMeshData.C:2180
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::PatchTools::pointNormals
static tmp< pointField > pointNormals(const polyMesh &, const PrimitivePatch< FaceList, PointField > &)
Return parallel consistent point normals for patches using mesh points.
Foam::globalMeshData::globalPointSlaves
const labelListList & globalPointSlaves() const
Definition: globalMeshData.C:2170
Foam::globalMeshData::globalEdgeSlavesMap
const mapDistribute & globalEdgeSlavesMap() const
Definition: globalMeshData.C:2245
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
PatchTools.H
globalMeshData.H
Foam::Map< label >
Foam::globalMeshData::globalPointSlavesMap
const mapDistribute & globalPointSlavesMap() const
Definition: globalMeshData.C:2191
Foam::transform
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:521
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:296
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::tmp::ref
T & ref() const
Definition: tmpI.H:227
faceNormals
surfaceVectorField faceNormals(mesh.Sf()/mesh.magSf())
Foam::Field< vector >
Foam::globalMeshData::globalTransforms
const globalIndexAndTransform & globalTransforms() const
Global transforms numbering.
Definition: globalMeshData.C:2160
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
Foam::mapDistribute
Class containing processor-to-processor mapping information.
Definition: mapDistribute.H:163
Foam::globalMeshData::globalEdgeSlaves
const labelListList & globalEdgeSlaves() const
Definition: globalMeshData.C:2214
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:107
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::mapDistribute::transform
Default transformation behaviour.
Definition: mapDistribute.H:209
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::mapDistributeBase::constructSize
label constructSize() const
Constructed data size.
Definition: mapDistributeBase.H:277
Foam::globalMeshData::globalEdgeTransformedSlaves
const labelListList & globalEdgeTransformedSlaves() const
Definition: globalMeshData.C:2224
f
labelList f(nPoints)
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: BitOps.H:63
Foam::globalMeshData::coupledPatch
const indirectPrimitivePatch & coupledPatch() const
Return patch of all coupled faces.
Definition: globalMeshData.C:2046
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:72
Foam::plusEqOp
Definition: ops.H:72
Foam::PrimitivePatch::meshPointMap
const Map< label > & meshPointMap() const
Mesh point map.
Definition: PrimitivePatch.C:343
Foam::globalIndexAndTransform
Determination and storage of the possible independent transforms introduced by coupledPolyPatches,...
Definition: globalIndexAndTransform.H:64
Foam::PatchTools::edgeNormals
static tmp< pointField > edgeNormals(const polyMesh &, const PrimitivePatch< FaceList, PointField > &, const labelList &patchEdges, const labelList &coupledEdges)
Return parallel consistent edge normals for patches using mesh points.
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].reset(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
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:79