boundaryRandom.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 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 "boundaryRandom.H"
27 #include "sampledSet.H"
28 #include "meshSearch.H"
29 #include "DynamicList.H"
30 #include "polyMesh.H"
32 #include "word.H"
33 #include "Random.H"
34 #include "SubField.H"
35 #include "barycentric2D.H"
36 #include "triPointRef.H"
37 #include "tetIndices.H"
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43 namespace sampledSets
44 {
47 }
48 }
49 
50 
51 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
52 
53 void Foam::sampledSets::boundaryRandom::calcSamples
54 (
55  DynamicList<point>& samplingPositions,
56  DynamicList<label>& samplingSegments,
57  DynamicList<label>& samplingCells,
58  DynamicList<label>& samplingFaces
59 ) const
60 {
61  // Get the patch IDs
62  const labelList patchIDs(mesh().boundaryMesh().patchSet(patches_).toc());
63 
64  // Triangulate the patch faces
65  DynamicList<label> triFaces, triTetPts;
66  forAll(patchIDs, patchi)
67  {
68  const polyPatch& patch = mesh().boundaryMesh()[patchIDs[patchi]];
69 
70  forAll(patch, patchFacei)
71  {
72  const face& f = patch[patchFacei];
73  const label facei = patchFacei + patch.start();
74 
75  for (label tetPti = 1; tetPti < f.size() - 1; ++ tetPti)
76  {
77  triFaces.append(facei);
78  triTetPts.append(tetPti);
79  }
80  }
81  }
82 
83  // Generate the fractions which select the processor, patch and triangle
84  scalarField trisFraction(triFaces.size() + 1, 0);
85  forAll(triFaces, trii)
86  {
87  const tetIndices tetIs
88  (
89  mesh().faceOwner()[triFaces[trii]],
90  triFaces[trii],
91  triTetPts[trii]
92  );
93 
94  trisFraction[trii + 1] =
95  trisFraction[trii] + tetIs.faceTri(mesh()).mag();
96  }
97 
98  scalarField procsFraction(Pstream::nProcs() + 1, 0);
99  {
100  scalarField procsArea(Pstream::nProcs(), 0);
101  procsArea[Pstream::myProcNo()] = trisFraction.last();
102  Pstream::listCombineGather(procsArea, maxEqOp<scalar>());
103  Pstream::listCombineScatter(procsArea);
104  for(label proci = 0; proci < Pstream::nProcs(); ++ proci)
105  {
106  procsFraction[proci + 1] = procsFraction[proci] + procsArea[proci];
107  }
108  }
109 
110  if (triFaces.size())
111  {
112  trisFraction /= trisFraction.last();
113  }
114 
115  if (procsFraction.last() != 0)
116  {
117  procsFraction /= procsFraction.last();
118  }
119 
120  // Generate the samples
121  Random rndGen(261782);
122  const label proci = Pstream::myProcNo();
123  for (label i = 0; i < nPoints_; ++ i)
124  {
125  // Request all random numbers simultaneously on all processors so that
126  // the generator state stays consistent
127 
128  const scalar rProc = rndGen.scalar01();
129  const scalar rTri = rndGen.scalar01();
130  const barycentric2D r2D = barycentric2D01(rndGen);
131 
132  if (procsFraction[proci] < rProc && rProc <= procsFraction[proci + 1])
133  {
134  label trii = 0;
135  while (rTri > trisFraction[trii + 1])
136  {
137  ++ trii;
138  }
139 
140  const tetIndices tetIs
141  (
142  mesh().faceOwner()[triFaces[trii]],
143  triFaces[trii],
144  triTetPts[trii]
145  );
146 
147  const barycentric r3D
148  (
149  rootSmall,
150  (1 - rootSmall)*r2D.a(),
151  (1 - rootSmall)*r2D.b(),
152  (1 - rootSmall)*r2D.c()
153  );
154 
155  samplingPositions.append(tetIs.tet(mesh()).barycentricToPoint(r3D));
156  samplingSegments.append(i);
157  samplingCells.append(tetIs.cell());
158  samplingFaces.append(tetIs.face());
159  }
160  }
161 }
162 
163 
164 void Foam::sampledSets::boundaryRandom::genSamples()
165 {
166  DynamicList<point> samplingPositions;
167  DynamicList<label> samplingSegments;
168  DynamicList<label> samplingCells;
169  DynamicList<label> samplingFaces;
170 
171  calcSamples
172  (
173  samplingPositions,
174  samplingSegments,
175  samplingCells,
176  samplingFaces
177  );
178 
179  samplingPositions.shrink();
180  samplingSegments.shrink();
181  samplingCells.shrink();
182  samplingFaces.shrink();
183 
184  setSamples
185  (
186  samplingPositions,
187  samplingSegments,
188  samplingCells,
189  samplingFaces
190  );
191 }
192 
193 
194 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
195 
197 (
198  const word& name,
199  const polyMesh& mesh,
200  const meshSearch& searchEngine,
201  const dictionary& dict
202 )
203 :
204  sampledSet(name, mesh, searchEngine, dict),
205  patches_(dict.lookup("patches")),
206  nPoints_(dict.lookup<label>("nPoints"))
207 {
208  genSamples();
209 }
210 
211 
212 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
213 
215 {}
216 
217 
218 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
Various (local, not parallel) searches on polyMesh; uses (demand driven) octree to search.
Definition: meshSearch.H:58
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:403
Holds list of sampling points which is filled at construction time. Various implementations of this b...
Definition: sampledSet.H:64
const polyMesh & mesh() const
Access the mesh.
Definition: sampledSet.H:210
Random samples within patches.
boundaryRandom(const word &name, const polyMesh &mesh, const meshSearch &searchEngine, const dictionary &dict)
Construct from dictionary.
virtual ~boundaryRandom()
Destructor.
Set of sets to sample. Call sampledSets.write() to sample&write files.
A class for handling words, derived from string.
Definition: word.H:62
label patchi
defineTypeNameAndDebug(arcUniform, 0)
addToRunTimeSelectionTable(sampledSet, arcUniform, word)
Namespace for OpenFOAM.
Barycentric< scalar > barycentric
A scalar version of the templated Barycentric.
Definition: barycentric.H:45
barycentric2D barycentric2D01(Random &rndGen)
Generate a random barycentric coordinate within the unit triangle.
Definition: barycentric2D.C:31
List< label > labelList
A List of labels.
Definition: labelList.H:56
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
Barycentric2D< scalar > barycentric2D
A scalar version of the templated Barycentric2D.
Definition: barycentric2D.H:45
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
labelList f(nPoints)
dictionary dict
Random rndGen(label(0))