Test-syncTools.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 Application
25  syncToolsTest
26 
27 Description
28  Test some functionality in syncTools.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "syncTools.H"
33 #include "argList.H"
34 #include "polyMesh.H"
35 #include "Time.H"
36 #include "Random.H"
37 #include "PackedList.H"
38 
39 using namespace Foam;
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
44 {
45  Info<< nl << "Testing PackedList synchronisation." << endl;
46 
47  {
48  PackedList<3> bits(mesh.nEdges());
49  forAll(bits, i)
50  {
51  bits.set(i, rndGen.integer(0,3));
52  }
53 
54  labelList edgeValues(mesh.nEdges());
55  forAll(bits, i)
56  {
57  edgeValues[i] = bits.get(i);
58  }
59 
60  PackedList<3> maxBits(bits);
61  labelList maxEdgeValues(edgeValues);
62 
64  syncTools::syncEdgeList(mesh, edgeValues, minEqOp<label>(), 0);
65 
68  (
69  mesh,
70  maxEdgeValues,
72  0
73  );
74 
75  forAll(bits, i)
76  {
77  if
78  (
79  edgeValues[i] != label(bits.get(i))
80  || maxEdgeValues[i] != label(maxBits.get(i))
81  )
82  {
84  << "edge:" << i
85  << " minlabel:" << edgeValues[i]
86  << " minbits:" << bits.get(i)
87  << " maxLabel:" << maxEdgeValues[i]
88  << " maxBits:" << maxBits.get(i)
89  << exit(FatalError);
90  }
91  }
92  }
93 
94  {
95  PackedList<3> bits(mesh.nPoints());
96  forAll(bits, i)
97  {
98  bits.set(i, rndGen.integer(0,3));
99  }
100 
101  labelList pointValues(mesh.nPoints());
102  forAll(bits, i)
103  {
104  pointValues[i] = bits.get(i);
105  }
106 
107  PackedList<3> maxBits(bits);
108  labelList maxPointValues(pointValues);
109 
111  syncTools::syncPointList(mesh, pointValues, minEqOp<label>(), 0);
112 
115  (
116  mesh,
117  maxPointValues,
118  maxEqOp<label>(),
119  0
120  );
121 
122  forAll(bits, i)
123  {
124  if
125  (
126  pointValues[i] != label(bits.get(i))
127  || maxPointValues[i] != label(maxBits.get(i))
128  )
129  {
131  << "point:" << i
132  << " at:" << mesh.points()[i]
133  << " minlabel:" << pointValues[i]
134  << " minbits:" << bits.get(i)
135  << " maxLabel:" << maxPointValues[i]
136  << " maxBits:" << maxBits.get(i)
137  << exit(FatalError);
138  }
139  }
140  }
141 
142  {
143  PackedList<3> bits(mesh.nFaces());
144  forAll(bits, faceI)
145  {
146  bits.set(faceI, rndGen.integer(0,3));
147  }
148 
149  labelList faceValues(mesh.nFaces());
150  forAll(bits, faceI)
151  {
152  faceValues[faceI] = bits.get(faceI);
153  }
154 
155  PackedList<3> maxBits(bits);
156  labelList maxFaceValues(faceValues);
157 
160 
162  syncTools::syncFaceList(mesh, maxFaceValues, maxEqOp<label>());
163 
164  forAll(bits, faceI)
165  {
166  if
167  (
168  faceValues[faceI] != label(bits.get(faceI))
169  || maxFaceValues[faceI] != label(maxBits.get(faceI))
170  )
171  {
173  << "face:" << faceI
174  << " minlabel:" << faceValues[faceI]
175  << " minbits:" << bits.get(faceI)
176  << " maxLabel:" << maxFaceValues[faceI]
177  << " maxBits:" << maxBits.get(faceI)
178  << exit(FatalError);
179  }
180  }
181  }
182 }
183 
184 
186 {
187  Info<< nl << "Testing Map synchronisation." << endl;
188 
190  << "Position test of sparse data only correct for cases without cyclics"
191  << " with shared points." << endl;
192 
193  primitivePatch allBoundary
194  (
196  (
197  mesh.faces(),
200  ),
201  mesh.points()
202  );
203  const pointField& localPoints = allBoundary.localPoints();
204 
205 
206  // Point data
207  // ~~~~~~~~~~
208 
209  {
210  // Create some data. Use slightly perturbed positions.
211  Map<point> sparseData;
212  pointField fullData(mesh.nPoints(), point(GREAT, GREAT, GREAT));
213 
214  forAll(localPoints, i)
215  {
216  const point pt = localPoints[i] + 1e-4*rndGen.vector01();
217 
218  label meshPointI = allBoundary.meshPoints()[i];
219 
220  sparseData.insert(meshPointI, pt);
221  fullData[meshPointI] = pt;
222  }
223 
224  //Pout<< "sparseData:" << sparseData << endl;
225 
227  (
228  mesh,
229  sparseData,
231  // true // apply separation
232  );
234  (
235  mesh,
236  fullData,
238  point(GREAT, GREAT, GREAT)
239  // true // apply separation
240  );
241 
242  // Compare.
243  // 1. Is all fullData also present in sparseData and same value
244  forAll(fullData, meshPointI)
245  {
246  const point& fullPt = fullData[meshPointI];
247 
248  if (fullPt != point(GREAT, GREAT, GREAT))
249  {
250  const point& sparsePt = sparseData[meshPointI];
251 
252  if (fullPt != sparsePt)
253  {
255  << "point:" << meshPointI
256  << " full:" << fullPt
257  << " sparse:" << sparsePt
258  << exit(FatalError);
259  }
260  }
261  }
262 
263  // 2. Does sparseData contain more?
264  forAllConstIter(Map<point>, sparseData, iter)
265  {
266  const point& sparsePt = iter();
267  label meshPointI = iter.key();
268  const point& fullPt = fullData[meshPointI];
269 
270  if (fullPt != sparsePt)
271  {
273  << "point:" << meshPointI
274  << " full:" << fullPt
275  << " sparse:" << sparsePt
276  << exit(FatalError);
277  }
278  }
279  }
280 
281 
282  // Edge data
283  // ~~~~~~~~~
284 
285  {
286  // Create some data. Use slightly perturbed positions.
287  EdgeMap<point> sparseData;
288  pointField fullData(mesh.nEdges(), point(GREAT, GREAT, GREAT));
289 
290  const edgeList& edges = allBoundary.edges();
291  const labelList meshEdges = allBoundary.meshEdges
292  (
293  mesh.edges(),
294  mesh.pointEdges()
295  );
296 
297  forAll(edges, i)
298  {
299  const edge& e = edges[i];
300 
301  const point pt = e.centre(localPoints) + 1e-4*rndGen.vector01();
302 
303  label meshEdgeI = meshEdges[i];
304 
305  sparseData.insert(mesh.edges()[meshEdgeI], pt);
306  fullData[meshEdgeI] = pt;
307  }
308 
309  //Pout<< "sparseData:" << sparseData << endl;
310 
312  (
313  mesh,
314  sparseData,
316  );
318  (
319  mesh,
320  fullData,
322  point(GREAT, GREAT, GREAT)
323  );
324 
325  // Compare.
326  // 1. Is all fullData also present in sparseData and same value
327  forAll(fullData, meshEdgeI)
328  {
329  const point& fullPt = fullData[meshEdgeI];
330 
331  if (fullPt != point(GREAT, GREAT, GREAT))
332  {
333  const point& sparsePt = sparseData[mesh.edges()[meshEdgeI]];
334 
335  if (fullPt != sparsePt)
336  {
338  << "edge:" << meshEdgeI
339  << " points:" << mesh.edges()[meshEdgeI]
340  << " full:" << fullPt
341  << " sparse:" << sparsePt
342  << exit(FatalError);
343  }
344  }
345  }
346 
347  // 2. Does sparseData contain more?
348  forAll(fullData, meshEdgeI)
349  {
350  const edge& e = mesh.edges()[meshEdgeI];
351 
352  EdgeMap<point>::const_iterator iter = sparseData.find(e);
353 
354  if (iter != sparseData.end())
355  {
356  const point& sparsePt = iter();
357  const point& fullPt = fullData[meshEdgeI];
358 
359  if (fullPt != sparsePt)
360  {
362  << "Extra edge:" << meshEdgeI
363  << " points:" << mesh.edges()[meshEdgeI]
364  << " full:" << fullPt
365  << " sparse:" << sparsePt
366  << exit(FatalError);
367  }
368  }
369  }
370  }
371 }
372 
373 
374 void testPointSync(const polyMesh& mesh, Random& rndGen)
375 {
376  Info<< nl << "Testing point-wise data synchronisation." << endl;
377 
378  // Test position.
379 
380  {
381  pointField syncedPoints(mesh.points());
383  (
384  mesh,
385  syncedPoints,
387  point(GREAT, GREAT, GREAT)
388  );
389 
390  forAll(syncedPoints, pointI)
391  {
392  if (mag(syncedPoints[pointI] - mesh.points()[pointI]) > SMALL)
393  {
395  << "Point " << pointI
396  << " original location " << mesh.points()[pointI]
397  << " synced location " << syncedPoints[pointI]
398  << exit(FatalError);
399  }
400  }
401  }
402 
403  // Test masterPoints
404 
405  {
406  labelList nMasters(mesh.nPoints(), 0);
407 
409 
410  forAll(isMasterPoint, pointI)
411  {
412  if (isMasterPoint[pointI])
413  {
414  nMasters[pointI] = 1;
415  }
416  }
417 
419  (
420  mesh,
421  nMasters,
422  plusEqOp<label>(),
423  0
424  );
425 
426  forAll(nMasters, pointI)
427  {
428  if (nMasters[pointI] != 1)
429  {
431  << "Point " << pointI
432  << " original location " << mesh.points()[pointI]
433  << " has " << nMasters[pointI]
434  << " masters."
435  << endl;
436  }
437  }
438  }
439 }
440 
441 
442 void testEdgeSync(const polyMesh& mesh, Random& rndGen)
443 {
444  Info<< nl << "Testing edge-wise data synchronisation." << endl;
445 
446  const edgeList& edges = mesh.edges();
447 
448  // Test position.
449 
450  {
451  pointField syncedMids(edges.size());
452  forAll(syncedMids, edgeI)
453  {
454  syncedMids[edgeI] = edges[edgeI].centre(mesh.points());
455  }
457  (
458  mesh,
459  syncedMids,
461  point(GREAT, GREAT, GREAT)
462  );
463 
464  forAll(syncedMids, edgeI)
465  {
466  point eMid = edges[edgeI].centre(mesh.points());
467 
468  if (mag(syncedMids[edgeI] - eMid) > SMALL)
469  {
471  << "Edge " << edgeI
472  << " original midpoint " << eMid
473  << " synced location " << syncedMids[edgeI]
474  << exit(FatalError);
475  }
476  }
477  }
478 
479  // Test masterEdges
480 
481  {
482  labelList nMasters(edges.size(), 0);
483 
485 
486  forAll(isMasterEdge, edgeI)
487  {
488  if (isMasterEdge[edgeI])
489  {
490  nMasters[edgeI] = 1;
491  }
492  }
493 
495  (
496  mesh,
497  nMasters,
498  plusEqOp<label>(),
499  0
500  );
501 
502  forAll(nMasters, edgeI)
503  {
504  if (nMasters[edgeI] != 1)
505  {
506  const edge& e = edges[edgeI];
508  << "Edge " << edgeI
509  << " at:" << mesh.points()[e[0]] << mesh.points()[e[1]]
510  << " has " << nMasters[edgeI]
511  << " masters."
512  << endl;
513  }
514  }
515  }
516 }
517 
518 
519 void testFaceSync(const polyMesh& mesh, Random& rndGen)
520 {
521  Info<< nl << "Testing face-wise data synchronisation." << endl;
522 
523  // Test position.
524 
525  {
526  pointField syncedFc(mesh.faceCentres());
527 
529  (
530  mesh,
531  syncedFc,
533  );
534 
535  forAll(syncedFc, faceI)
536  {
537  if (mag(syncedFc[faceI] - mesh.faceCentres()[faceI]) > SMALL)
538  {
540  << "Face " << faceI
541  << " original centre " << mesh.faceCentres()[faceI]
542  << " synced centre " << syncedFc[faceI]
543  << exit(FatalError);
544  }
545  }
546  }
547 
548  // Test masterFaces
549 
550  {
551  labelList nMasters(mesh.nFaces(), 0);
552 
554 
555  forAll(isMasterFace, faceI)
556  {
557  if (isMasterFace[faceI])
558  {
559  nMasters[faceI] = 1;
560  }
561  }
562 
564  (
565  mesh,
566  nMasters,
568  );
569 
570  forAll(nMasters, faceI)
571  {
572  if (nMasters[faceI] != 1)
573  {
575  << "Face " << faceI
576  << " centre " << mesh.faceCentres()[faceI]
577  << " has " << nMasters[faceI]
578  << " masters."
579  << exit(FatalError);
580  }
581  }
582  }
583 }
584 
585 
586 // Main program:
587 
588 int main(int argc, char *argv[])
589 {
590  #include "setRootCase.H"
591  #include "createTime.H"
592  #include "createPolyMesh.H"
593 
594 
595  Random rndGen(5341*(Pstream::myProcNo()+1));
596 
597 
598  // Face sync
600 
601  // Edge sync
603 
604  // Point sync
606 
607  // PackedList synchronisation
609 
610  // Sparse synchronisation
612 
613  Info<< "End\n" << endl;
614 
615  return 0;
616 }
617 
618 
619 // ************************************************************************* //
Foam::Random
Simple random number generator.
Definition: Random.H:49
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:979
Foam::PackedBoolList
A bit-packed bool list.
Definition: PackedBoolList.H:63
PackedList.H
Foam::syncTools::getMasterFaces
static PackedBoolList getMasterFaces(const polyMesh &)
Get per face whether it is uncoupled or a master of a.
Definition: syncTools.C:153
Foam::PrimitivePatch::edges
const edgeList & edges() const
Return list of edges, address into LOCAL point list.
Definition: PrimitivePatchTemplate.C:212
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::PrimitivePatch::localPoints
const Field< PointType > & localPoints() const
Return pointField of points in patch.
Definition: PrimitivePatchTemplate.C:432
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::syncTools::syncEdgePositions
static void syncEdgePositions(const polyMesh &mesh, List< point > &l, const CombineOp &cop, const point &nullValue)
Synchronize locations on all mesh edges.
Definition: syncTools.H:283
Foam::PrimitivePatch::meshEdges
labelList meshEdges(const edgeList &allEdges, const labelListList &cellEdges, const labelList &faceCells) const
Return labels of patch edges in the global edge list using.
Definition: PrimitivePatchMeshEdges.C:41
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::maxEqOp
Definition: ops.H:77
Foam::HashTable< T, edge, Hash< edge > >::insert
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: PrimitivePatchTemplate.H:68
Foam::maxMagSqrEqOp
Definition: ops.H:80
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::syncTools::syncPointPositions
static void syncPointPositions(const polyMesh &mesh, List< point > &l, const CombineOp &cop, const point &nullValue)
Synchronize locations on all mesh points.
Definition: syncTools.H:196
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::primitiveMesh::pointEdges
const labelListList & pointEdges() const
Definition: primitiveMeshPointEdges.C:96
polyMesh.H
Foam::primitiveMesh::nEdges
label nEdges() const
Definition: primitiveMeshI.H:41
syncTools.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
testPackedList
void testPackedList(const polyMesh &mesh, Random &rndGen)
Definition: Test-syncTools.C:40
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
testFaceSync
void testFaceSync(const polyMesh &mesh, Random &rndGen)
Definition: Test-syncTools.C:516
main
int main(int argc, char *argv[])
Definition: Test-syncTools.C:585
Foam::primitiveMesh::nPoints
label nPoints() const
Definition: primitiveMeshI.H:35
Foam::syncTools::syncEdgeMap
static void syncEdgeMap(const polyMesh &, EdgeMap< T > &edgeValues, const CombineOp &cop, const TransformOp &top)
Synchronize values on selected edges.
Definition: syncToolsTemplates.C:391
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::Info
messageStream Info
Foam::minEqOp
Definition: ops.H:78
argList.H
Foam::FatalError
error FatalError
Foam::primitiveMesh::nInternalFaces
label nInternalFaces() const
Definition: primitiveMeshI.H:52
Foam::PackedList::set
bool set(const label, const unsigned int val=~0u)
Set value at index I. Return true if value changed.
Definition: PackedListI.H:995
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::plusEqOp
Definition: ops.H:71
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::PackedList::get
unsigned int get(const label) const
Get value at index I.
Definition: PackedListI.H:964
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::HashTable< T, edge, Hash< edge > >::find
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Random.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::syncTools::getMasterEdges
static PackedBoolList getMasterEdges(const polyMesh &)
Get per edge whether it is uncoupled or a master of a.
Definition: syncTools.C:109
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
setRootCase.H
Foam::EdgeMap
Map from edge (expressed as its endpoints) to value.
Definition: EdgeMap.H:47
Foam::polyMesh::faces
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1004
Foam::syncTools::syncPointMap
static void syncPointMap(const polyMesh &, Map< T > &pointValues, const CombineOp &cop, const TransformOp &top)
Synchronize values on selected points.
Definition: syncToolsTemplates.C:86
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::syncTools::syncEdgeList
static void syncEdgeList(const polyMesh &, List< T > &, const CombineOp &cop, const T &nullValue, const TransformOp &top)
Synchronize values on all mesh edges.
Definition: syncToolsTemplates.C:1141
Foam::primitiveMesh::nFaces
label nFaces() const
Definition: primitiveMeshI.H:58
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::PackedList
A dynamically allocatable list of packed unsigned integers.
Definition: PackedList.H:117
Foam::primitiveMesh::faceCentres
const vectorField & faceCentres() const
Definition: primitiveMeshFaceCentresAndAreas.C:130
Foam::syncTools::getMasterPoints
static PackedBoolList getMasterPoints(const polyMesh &)
Get per point whether it is uncoupled or a master of a.
Definition: syncTools.C:65
createTime.H
testSparseData
void testSparseData(const polyMesh &mesh, Random &rndGen)
Definition: Test-syncTools.C:182
testEdgeSync
void testEdgeSync(const polyMesh &mesh, Random &rndGen)
Definition: Test-syncTools.C:439
Foam::minMagSqrEqOp
Definition: ops.H:79
Foam::syncTools::syncFaceList
static void syncFaceList(const polyMesh &mesh, UList< T > &l, const CombineOp &cop)
Synchronize values on all mesh faces.
Definition: syncTools.H:381
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::point
vector point
Point is a vector.
Definition: point.H:41
testPointSync
void testPointSync(const polyMesh &mesh, Random &rndGen)
Definition: Test-syncTools.C:371
Foam::PrimitivePatch::meshPoints
const labelList & meshPoints() const
Return labelList of mesh points in patch.
Definition: PrimitivePatchTemplate.C:392
createPolyMesh.H
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
rndGen
cachedRandom rndGen(label(0), -1)
Foam::syncTools::syncPointList
static void syncPointList(const polyMesh &, List< T > &, const CombineOp &cop, const T &nullValue, const TransformOp &top)
Synchronize values on all mesh points.
Definition: syncToolsTemplates.C:984
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatchTemplate.H:88
Foam::syncTools::syncFacePositions
static void syncFacePositions(const polyMesh &mesh, UList< point > &l, const CombineOp &cop)
Synchronize locations on all mesh faces.
Definition: syncTools.H:406