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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "twoDPointCorrector.H"
27 #include "polyMesh.H"
28 #include "wedgePolyPatch.H"
29 #include "emptyPolyPatch.H"
30 #include "SubField.H"
31 #include "meshTools.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(twoDPointCorrector, 0);
38 }
39 
40 const Foam::scalar Foam::twoDPointCorrector::edgeOrthogonalityTol = 1.0 - 1e-4;
41 
42 
43 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
44 
46 {
47  // Find geometry normal
48  planeNormalPtr_ = new vector(0, 0, 0);
49  vector& pn = *planeNormalPtr_;
50 
51  // Algorithm:
52  // Attempt to find wedge patch and work out the normal from it.
53  // If not found, find an empty patch with faces in it and use the
54  // normal of the first face. If neither is found, declare an
55  // error.
56 
57  // Try and find a wedge patch
59 
60  forAll(patches, patchI)
61  {
62  if (isA<wedgePolyPatch>(patches[patchI]))
63  {
64  isWedge_ = true;
65 
66  const wedgePolyPatch& wp =
67  refCast<const wedgePolyPatch>(patches[patchI]);
68 
69  pn = wp.centreNormal();
70 
71  wedgeAxis_ = wp.axis();
72  wedgeAngle_ = mag(acos(wp.cosAngle()));
73 
74  if (polyMesh::debug)
75  {
76  Pout<< "Found normal from wedge patch " << patchI;
77  }
78 
79  break;
80  }
81  }
82 
83  // Try to find an empty patch with faces
84  if (!isWedge_)
85  {
86  forAll(patches, patchI)
87  {
88  if (isA<emptyPolyPatch>(patches[patchI]) && patches[patchI].size())
89  {
90  pn = patches[patchI].faceAreas()[0];
91 
92  if (polyMesh::debug)
93  {
94  Pout<< "Found normal from empty patch " << patchI;
95  }
96 
97  break;
98  }
99  }
100  }
101 
102 
103  if (mag(pn) < VSMALL)
104  {
106  << "Cannot determine normal vector from patches."
107  << abort(FatalError);
108  }
109  else
110  {
111  pn /= mag(pn);
112  }
113 
114  if (polyMesh::debug)
115  {
116  Pout<< " twoDPointCorrector normal: " << pn << endl;
117  }
118 
119  // Select edges to be included in check.
121  labelList& neIndices = *normalEdgeIndicesPtr_;
122 
123  const edgeList& meshEdges = mesh_.edges();
124 
125  const pointField& meshPoints = mesh_.points();
126 
127  label nNormalEdges = 0;
128 
129  forAll(meshEdges, edgeI)
130  {
131  const edge& e = meshEdges[edgeI];
132 
133  vector edgeVector = e.vec(meshPoints)/(e.mag(meshPoints) + VSMALL);
134 
135  if (mag(edgeVector & pn) > edgeOrthogonalityTol)
136  {
137  // this edge is normal to the plane. Add it to the list
138  neIndices[nNormalEdges++] = edgeI;
139  }
140  }
141 
142  neIndices.setSize(nNormalEdges);
143 
144  // Construction check: number of points in a read 2-D or wedge geometry
145  // should be odd and the number of edges normal to the plane should be
146  // exactly half the number of points
147  if (!isWedge_)
148  {
149  if (meshPoints.size() % 2 != 0)
150  {
152  << "the number of vertices in the geometry "
153  << "is odd - this should not be the case for a 2-D case. "
154  << "Please check the geometry."
155  << endl;
156  }
157 
158  if (2*nNormalEdges != meshPoints.size())
159  {
161  << "The number of points in the mesh is "
162  << "not equal to twice the number of edges normal to the plane "
163  << "- this may be OK only for wedge geometries.\n"
164  << " Please check the geometry or adjust "
165  << "the orthogonality tolerance.\n" << endl
166  << "Number of normal edges: " << nNormalEdges
167  << " number of points: " << meshPoints.size()
168  << endl;
169  }
170  }
171 }
172 
173 
175 {
176  deleteDemandDrivenData(planeNormalPtr_);
177  deleteDemandDrivenData(normalEdgeIndicesPtr_);
178 }
179 
180 
182 (
183  const vector& n,
184  const point& A,
185  point& p
186 ) const
187 {
188  scalar ADash = mag(A - wedgeAxis_*(wedgeAxis_ & A));
189  vector pDash = ADash*tan(wedgeAngle_)*planeNormal();
190 
191  p = A + sign(n & p)*pDash;
192 }
193 
194 
195 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
196 
198 :
200  required_(mesh_.nGeometricD() == 2),
201  planeNormalPtr_(NULL),
202  normalEdgeIndicesPtr_(NULL),
203  isWedge_(false),
204  wedgeAxis_(vector::zero),
205  wedgeAngle_(0.0)
206 {}
207 
208 
209 
210 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
211 
213 {
214  clearAddressing();
215 }
216 
217 
218 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
219 
221 {
222  const vector& pn = planeNormal();
223 
224  if (mag(pn.x()) >= edgeOrthogonalityTol)
225  {
226  return vector::X;
227  }
228  else if (mag(pn.y()) >= edgeOrthogonalityTol)
229  {
230  return vector::Y;
231  }
232  else if (mag(pn.z()) >= edgeOrthogonalityTol)
233  {
234  return vector::Z;
235  }
236  else
237  {
239  << "Plane normal not aligned with the coordinate system" << nl
240  << " pn = " << pn
241  << abort(FatalError);
242 
243  return vector::Z;
244  }
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  forAll(neIndices, edgeI)
286  {
287  point& pStart = p[meshEdges[neIndices[edgeI]].start()];
288 
289  point& pEnd = p[meshEdges[neIndices[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  forAll(neIndices, edgeI)
330  {
331  const edge& e = meshEdges[neIndices[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 
359 
361 {
362  clearAddressing();
363 }
364 
365 
367 {
368  return true;
369 }
370 
371 
372 // ************************************************************************* //
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:979
meshTools.H
Foam::twoDPointCorrector::snapToWedge
void snapToWedge(const vector &n, const point &A, point &p) const
Snap a point to the wedge patch(es)
Definition: twoDPointCorrector.C:182
Foam::Vector< scalar >::Z
@ Z
Definition: Vector.H:89
Foam::tan
dimensionedScalar tan(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:257
Foam::Vector< scalar >::Y
@ Y
Definition: Vector.H:89
p
p
Definition: pEqn.H:62
SubField.H
Foam::labelList
List< label > labelList
A List of labels.
Definition: labelList.H:56
Foam::wedgePolyPatch
Wedge front and back plane patch.
Definition: wedgePolyPatch.H:48
Foam::polyBoundaryMesh
Foam::polyBoundaryMesh.
Definition: polyBoundaryMesh.H:60
Foam::twoDPointCorrector::planeNormal
const vector & planeNormal() const
Return plane normal.
Definition: twoDPointCorrector.C:248
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::twoDPointCorrector::correctDisplacement
void correctDisplacement(const pointField &p, vectorField &disp) const
Correct motion displacements.
Definition: twoDPointCorrector.C:311
wedgePolyPatch.H
Foam::twoDPointCorrector::~twoDPointCorrector
~twoDPointCorrector()
Destructor.
Definition: twoDPointCorrector.C:212
Foam::twoDPointCorrector::isWedge_
bool isWedge_
Flag to indicate a wedge geometry.
Definition: twoDPointCorrector.H:77
Foam::edge
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:58
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::twoDPointCorrector::clearAddressing
void clearAddressing() const
Clear addressing.
Definition: twoDPointCorrector.C:174
Foam::primitiveMesh::edges
const edgeList & edges() const
Return mesh edges. Uses calcEdges.
Definition: primitiveMeshEdges.C:353
Foam::mag
dimensioned< scalar > mag(const dimensioned< Type > &)
Foam::sign
dimensionedScalar sign(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:179
polyMesh.H
Foam::primitiveMesh::nEdges
label nEdges() const
Definition: primitiveMeshI.H:41
Foam::UpdateableMeshObject
Definition: MeshObject.H:258
Foam::twoDPointCorrector::calcAddressing
void calcAddressing() const
Calculate addressing.
Definition: twoDPointCorrector.C:45
Foam::twoDPointCorrector::normalEdgeIndicesPtr_
labelList * normalEdgeIndicesPtr_
Indices of edges normal to plane.
Definition: twoDPointCorrector.H:74
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
A
simpleMatrix< scalar > A(Nc)
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:40
Foam::twoDPointCorrector
Class applies a two-dimensional correction to mesh motion point field.
Definition: twoDPointCorrector.H:61
Foam::wedgePolyPatch::axis
const vector & axis() const
Return axis of the wedge.
Definition: wedgePolyPatch.H:178
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::twoDPointCorrector::wedgeAngle_
scalar wedgeAngle_
Wedge angle (if wedge geometry)
Definition: twoDPointCorrector.H:83
Foam::twoDPointCorrector::wedgeAxis_
vector wedgeAxis_
Wedge axis (if wedge geometry)
Definition: twoDPointCorrector.H:80
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:59
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::twoDPointCorrector::twoDPointCorrector
twoDPointCorrector(const twoDPointCorrector &)
Disallow default bitwise copy construct.
Foam::Vector< scalar >::X
@ X
Definition: Vector.H:89
Foam::MeshObject< polyMesh, UpdateableMeshObject, twoDPointCorrector >::mesh_
const polyMesh & mesh_
Definition: MeshObject.H:89
Foam::Vector::x
const Cmpt & x() const
Definition: VectorI.H:65
Foam::twoDPointCorrector::planeNormalPtr_
vector * planeNormalPtr_
2-D plane unit normal
Definition: twoDPointCorrector.H:71
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Vector::z
const Cmpt & z() const
Definition: VectorI.H:77
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
emptyPolyPatch.H
Foam::List::setSize
void setSize(const label)
Reset size of List.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
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:59
Foam::acos
dimensionedScalar acos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:259
Foam::twoDPointCorrector::edgeOrthogonalityTol
static const scalar edgeOrthogonalityTol
Edge orthogonality tolerance.
Definition: twoDPointCorrector.H:108
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: doubleFloat.H:94
Foam::direction
unsigned char direction
Definition: direction.H:43
Foam::meshTools::constrainToMeshCentre
void constrainToMeshCentre(const polyMesh &mesh, point &pt)
Set the constrained components of position to mesh centre.
Definition: meshTools.C:606
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Foam::MeshObject
Templated abstract base-class for optional mesh objects used to automate their allocation to the mesh...
Definition: MeshObject.H:81
twoDPointCorrector.H
Foam::twoDPointCorrector::movePoints
bool movePoints()
Correct weighting factors for moving mesh.
Definition: twoDPointCorrector.C:366
Foam::wedgePolyPatch::cosAngle
scalar cosAngle() const
Return the cosine of the wedge angle.
Definition: wedgePolyPatch.H:196
patches
patches[0]
Definition: createSingleCellMesh.H:36
Foam::Vector::y
const Cmpt & y() const
Definition: VectorI.H:71
Foam::twoDPointCorrector::normalEdgeIndices
const labelList & normalEdgeIndices() const
Return indices of normal edges.
Definition: twoDPointCorrector.C:259
Foam::twoDPointCorrector::correctPoints
void correctPoints(pointField &p) const
Correct motion points.
Definition: twoDPointCorrector.C:270
Foam::twoDPointCorrector::normalDir
direction normalDir() const
Return direction normal to plane.
Definition: twoDPointCorrector.C:220
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::twoDPointCorrector::updateMesh
void updateMesh(const mapPolyMesh &)
Update topology.
Definition: twoDPointCorrector.C:360
Foam::wedgePolyPatch::centreNormal
const vector & centreNormal() const
Return plane normal between the wedge boundaries.
Definition: wedgePolyPatch.H:184
Foam::zero
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:47