twoDPointCorrector.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 "twoDPointCorrector.H"
30 #include "polyMesh.H"
31 #include "wedgePolyPatch.H"
32 #include "emptyPolyPatch.H"
33 #include "SubField.H"
34 #include "meshTools.H"
35 #include "demandDrivenData.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41  defineTypeNameAndDebug(twoDPointCorrector, 0);
42 }
43 
44 const Foam::scalar Foam::twoDPointCorrector::edgeOrthogonalityTol = 1.0 - 1e-4;
45 
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 void Foam::twoDPointCorrector::calcAddressing() const
50 {
51  // Find geometry normal
52  planeNormalPtr_ = new vector(0, 0, 0);
53  vector& pn = *planeNormalPtr_;
54 
55  // Algorithm:
56  // Attempt to find wedge patch and work out the normal from it.
57  // If not found, find an empty patch with faces in it and use the
58  // normal of the first face. If neither is found, declare an
59  // error.
60 
61  // Try and find a wedge patch
62  const polyBoundaryMesh& patches = mesh_.boundaryMesh();
63 
64  for (const polyPatch& p : patches)
65  {
66  if (isA<wedgePolyPatch>(p))
67  {
68  isWedge_ = true;
69 
70  const wedgePolyPatch& wp = refCast<const wedgePolyPatch>(p);
71 
72  pn = wp.centreNormal();
73 
74  wedgeAxis_ = wp.axis();
75  wedgeAngle_ = mag(acos(wp.cosAngle()));
76 
77  if (polyMesh::debug)
78  {
79  Pout<< "Found normal from wedge patch " << p.index() << nl;
80  }
81 
82  break;
83  }
84  }
85 
86  // Try to find an empty patch with faces
87  if (!isWedge_)
88  {
89  for (const polyPatch& p : patches)
90  {
91  if (isA<emptyPolyPatch>(p) && p.size())
92  {
93  pn = p.faceAreas()[0];
94 
95  if (polyMesh::debug)
96  {
97  Pout<< "Found normal from empty patch " << p.index() << nl;
98  }
99 
100  break;
101  }
102  }
103  }
104 
105 
106  if (mag(pn) < VSMALL)
107  {
109  << "Cannot determine normal vector from patches."
110  << abort(FatalError);
111  }
112  else
113  {
114  pn /= mag(pn);
115  }
116 
117  if (polyMesh::debug)
118  {
119  Pout<< " twoDPointCorrector normal: " << pn << nl;
120  }
121 
122  // Select edges to be included in check.
123  normalEdgeIndicesPtr_ = new labelList(mesh_.nEdges());
124  labelList& neIndices = *normalEdgeIndicesPtr_;
125 
126  const edgeList& meshEdges = mesh_.edges();
127 
128  const pointField& meshPoints = mesh_.points();
129 
130  label nNormalEdges = 0;
131 
132  forAll(meshEdges, edgeI)
133  {
134  const edge& e = meshEdges[edgeI];
135 
136  vector edgeVector = e.unitVec(meshPoints);
137 
138  if (mag(edgeVector & pn) > edgeOrthogonalityTol)
139  {
140  // this edge is normal to the plane. Add it to the list
141  neIndices[nNormalEdges++] = edgeI;
142  }
143  }
144 
145  neIndices.setSize(nNormalEdges);
146 
147  // Construction check: number of points in a read 2-D or wedge geometry
148  // should be odd and the number of edges normal to the plane should be
149  // exactly half the number of points
150  if (!isWedge_)
151  {
152  if (meshPoints.size() % 2 != 0)
153  {
155  << "the number of vertices in the geometry "
156  << "is odd - this should not be the case for a 2-D case. "
157  << "Please check the geometry."
158  << endl;
159  }
160 
161  if (2*nNormalEdges != meshPoints.size())
162  {
164  << "The number of points in the mesh is "
165  << "not equal to twice the number of edges normal to the plane "
166  << "- this may be OK only for wedge geometries.\n"
167  << " Please check the geometry or adjust "
168  << "the orthogonality tolerance.\n" << endl
169  << "Number of normal edges: " << nNormalEdges
170  << " number of points: " << meshPoints.size()
171  << endl;
172  }
173  }
174 }
175 
176 
177 void Foam::twoDPointCorrector::clearAddressing() const
178 {
179  deleteDemandDrivenData(planeNormalPtr_);
180  deleteDemandDrivenData(normalEdgeIndicesPtr_);
181 }
182 
183 
184 void Foam::twoDPointCorrector::snapToWedge
185 (
186  const vector& n,
187  const point& A,
188  point& p
189 ) const
190 {
191  scalar ADash = mag(A - wedgeAxis_*(wedgeAxis_ & A));
192  vector pDash = ADash*tan(wedgeAngle_)*planeNormal();
194  p = A + sign(n & p)*pDash;
195 }
196 
197 
198 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
199 
200 Foam::twoDPointCorrector::twoDPointCorrector(const polyMesh& mesh)
201 :
202  MeshObject_type(mesh),
203  required_(mesh_.nGeometricD() == 2),
204  planeNormalPtr_(nullptr),
205  normalEdgeIndicesPtr_(nullptr),
206  isWedge_(false),
207  wedgeAxis_(Zero),
208  wedgeAngle_(0.0)
209 {}
210 
211 
212 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
213 
215 {
216  clearAddressing();
217 }
218 
219 
220 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
221 
223 {
224  const vector& pn = planeNormal();
225 
226  if (mag(pn.x()) >= edgeOrthogonalityTol)
227  {
228  return vector::X;
229  }
230  else if (mag(pn.y()) >= edgeOrthogonalityTol)
231  {
232  return vector::Y;
233  }
234  else if (mag(pn.z()) >= edgeOrthogonalityTol)
235  {
236  return vector::Z;
237  }
238 
240  << "Plane normal not aligned with the coordinate system" << nl
241  << " pn = " << pn
242  << abort(FatalError);
243 
244  return vector::Z;
245 }
246 
247 
249 {
250  if (!planeNormalPtr_)
251  {
252  calcAddressing();
253  }
254 
255  return *planeNormalPtr_;
256 }
257 
258 
260 {
261  if (!normalEdgeIndicesPtr_)
262  {
263  calcAddressing();
264  }
265 
266  return *normalEdgeIndicesPtr_;
267 }
268 
269 
271 {
272  if (!required_) return;
273 
274  // Algorithm:
275  // Loop through all edges. Calculate the average point position A for
276  // the front and the back. Correct the position of point P (in two planes)
277  // such that vectors AP and planeNormal are parallel
278 
279  // Get reference to edges
280  const edgeList& meshEdges = mesh_.edges();
281 
282  const labelList& neIndices = normalEdgeIndices();
283  const vector& pn = planeNormal();
284 
285  for (const label edgei : neIndices)
286  {
287  point& pStart = p[meshEdges[edgei].start()];
288 
289  point& pEnd = p[meshEdges[edgei].end()];
290 
291  // calculate average point position
292  point A = 0.5*(pStart + pEnd);
294 
295  if (isWedge_)
296  {
297  snapToWedge(pn, A, pStart);
298  snapToWedge(pn, A, pEnd);
299  }
300  else
301  {
302  // correct point locations
303  pStart = A + pn*(pn & (pStart - A));
304  pEnd = A + pn*(pn & (pEnd - A));
305  }
306  }
307 }
308 
309 
311 (
312  const pointField& p,
313  vectorField& disp
314 ) const
315 {
316  if (!required_) return;
317 
318  // Algorithm:
319  // Loop through all edges. Calculate the average point position A for
320  // the front and the back. Correct the position of point P (in two planes)
321  // such that vectors AP and planeNormal are parallel
322 
323  // Get reference to edges
324  const edgeList& meshEdges = mesh_.edges();
325 
326  const labelList& neIndices = normalEdgeIndices();
327  const vector& pn = planeNormal();
328 
329  for (const label edgei : neIndices)
330  {
331  const edge& e = meshEdges[edgei];
332 
333  label startPointi = e.start();
334  point pStart = p[startPointi] + disp[startPointi];
335 
336  label endPointi = e.end();
337  point pEnd = p[endPointi] + disp[endPointi];
338 
339  // calculate average point position
340  point A = 0.5*(pStart + pEnd);
342 
343  if (isWedge_)
344  {
345  snapToWedge(pn, A, pStart);
346  snapToWedge(pn, A, pEnd);
347  disp[startPointi] = pStart - p[startPointi];
348  disp[endPointi] = pEnd - p[endPointi];
349  }
350  else
351  {
352  // correct point locations
353  disp[startPointi] = (A + pn*(pn & (pStart - A))) - p[startPointi];
354  disp[endPointi] = (A + pn*(pn & (pEnd - A))) - p[endPointi];
355  }
356  }
357 }
358 
361 {
362  clearAddressing();
363 }
364 
365 
367 {
368  return true;
369 }
370 
371 
372 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
void updateMesh(const mapPolyMesh &)
Update topology.
dimensionedScalar acos(const dimensionedScalar &ds)
uint8_t direction
Definition: direction.H:46
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
List< edge > edgeList
List of edge.
Definition: edgeList.H:32
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
const labelList & normalEdgeIndices() const
Return indices of normal edges.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1078
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
void correctPoints(pointField &p) const
Correct motion points.
void correctDisplacement(const pointField &p, vectorField &disp) const
Correct motion displacements.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
dynamicFvMesh & mesh
const polyBoundaryMesh & boundaryMesh() const noexcept
Return boundary mesh.
Definition: polyMesh.H:609
Vector< scalar > vector
Definition: vector.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:139
label nEdges() const
Number of mesh edges.
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
int debug
Static debugging option.
const vector & planeNormal() const
Return plane normal.
defineTypeNameAndDebug(combustionModel, 0)
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
Template functions to aid in the implementation of demand driven data.
vector point
Point is a vector.
Definition: point.H:37
#define WarningInFunction
Report a warning using Foam::Warning.
bool movePoints()
Correct weighting factors for moving mesh.
PtrList< volScalarField > & Y
direction normalDir() const
Return direction normal to plane.
const polyBoundaryMesh & patches
label n
Field< vector > vectorField
Specialisation of Field<T> for vector.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
List< label > labelList
A List of labels.
Definition: List.H:62
volScalarField & p
dimensionedScalar tan(const dimensionedScalar &ds)
static const Foam::dimensionedScalar A("", Foam::dimPressure, 611.21)
void deleteDemandDrivenData(DataPtr &dataPtr)
void constrainToMeshCentre(const polyMesh &mesh, point &pt)
Set the constrained components of position to mesh centre.
Definition: meshTools.C:622
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Namespace for OpenFOAM.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127