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-2018 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 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42 namespace sampledSets
43 {
44  defineTypeNameAndDebug(boundaryRandom, 0);
45  addToRunTimeSelectionTable(sampledSet, boundaryRandom, word);
46 }
47 }
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 void Foam::sampledSets::boundaryRandom::calcSamples
53 (
54  DynamicList<point>& samplingPts,
55  DynamicList<label>& samplingCells,
56  DynamicList<label>& samplingFaces,
57  DynamicList<label>& samplingSegments,
58  DynamicList<scalar>& samplingCurveDist
59 ) const
60 {
61  // Triangulate the patches
62  List<DynamicList<face>> trisDyn(patches_.size());
63  List<DynamicList<label>> trisFaceiDyn(patches_.size());
64  forAll(patches_, patchi)
65  {
66  const polyPatch& patch = mesh().boundaryMesh()[patchi];
67 
68  forAll(patch, patchFacei)
69  {
70  const face& f = patch[patchFacei];
71  const label facei = patchFacei + patch.start();
72 
73  DynamicList<face> faceTris(f.nTriangles());
74  f.triangles(patch.points(), faceTris);
75 
76  trisDyn[patchi].append(faceTris);
77  trisFaceiDyn[patchi].append(labelList(f.nTriangles(), facei));
78  }
79  }
80 
81  List<faceList> tris(patches_.size());
82  List<labelList> trisFacei(patches_.size());
83  forAll(patches_, patchi)
84  {
85  tris[patchi].transfer(trisDyn[patchi]);
86  trisFacei[patchi].transfer(trisFaceiDyn[patchi]);
87  }
88 
89  // Generate the fractions which select the processor, patch and triangle
90  List<scalarField> trisFraction(patches_.size());
91  forAll(patches_, patchi)
92  {
93  const polyPatch& patch = mesh().boundaryMesh()[patchi];
94  const pointField& points = patch.points();
95 
96  trisFraction[patchi] = scalarField(tris[patchi].size() + 1, 0);
97  forAll(tris[patchi], patchTrii)
98  {
99  trisFraction[patchi][patchTrii + 1] =
100  trisFraction[patchi][patchTrii]
101  + tris[patchi][patchTrii].mag(points);
102  }
103  }
104 
105  scalarField patchesFraction(patches_.size() + 1, 0);
106  forAll(patches_, patchi)
107  {
108  patchesFraction[patchi + 1] =
109  patchesFraction[patchi] + trisFraction[patchi].last();
110  }
111 
112  scalarField procsArea(Pstream::nProcs(), 0);
113  procsArea[Pstream::myProcNo()] = patchesFraction.last();
114  Pstream::listCombineGather(procsArea, maxEqOp<scalar>());
115  Pstream::listCombineScatter(procsArea);
116  scalarField procsFraction(Pstream::nProcs() + 1, 0);
117  for(label proci = 0; proci < Pstream::nProcs(); ++ proci)
118  {
119  procsFraction[proci + 1] = procsFraction[proci] + procsArea[proci];
120  }
121 
122  bool anyTris = false;
123  forAll(patches_, patchi)
124  {
125  if (tris[patchi].size())
126  {
127  trisFraction[patchi] /= trisFraction[patchi].last();
128  anyTris = true;
129  }
130  }
131 
132  if (anyTris)
133  {
134  patchesFraction /= patchesFraction.last();
135  }
136 
137  procsFraction /= procsFraction.last();
138 
139  // Generate the samples
140  Random rndGen(261782);
141  const label proci = Pstream::myProcNo();
142  for (label i = 0; i < nPoints_; ++ i)
143  {
144  // Request all random numbers simultaneously on all processors so that
145  // the generator state stays consistent
146 
147  const scalar rProc = rndGen.scalar01();
148  const scalar rPatch = rndGen.scalar01();
149  const scalar rTri = rndGen.scalar01();
151 
152  if (procsFraction[proci] < rProc && rProc <= procsFraction[proci + 1])
153  {
154  label patchi = 0;
155  while (rPatch > patchesFraction[patchi + 1])
156  {
157  ++ patchi;
158  }
159 
160  label trii = 0;
161  while (rTri > trisFraction[patchi][trii + 1])
162  {
163  ++ trii;
164  }
165 
166  const polyPatch& patch = mesh().boundaryMesh()[patchi];
167  const pointField& points = patch.points();
168  const face& tf = tris[patchi][trii];
169  const triPointRef tt(points[tf[0]], points[tf[1]], points[tf[2]]);
170 
171  samplingPts.append(tt.barycentricToPoint(r));
172  samplingCells.append(mesh().faceOwner()[trisFacei[patchi][trii]]);
173  samplingFaces.append(trisFacei[patchi][trii]);
174  samplingSegments.append(0);
175  samplingCurveDist.append(scalar(i));
176  }
177  }
178 }
179 
180 
181 void Foam::sampledSets::boundaryRandom::genSamples()
182 {
183  // Storage for sample points
184  DynamicList<point> samplingPts;
185  DynamicList<label> samplingCells;
186  DynamicList<label> samplingFaces;
187  DynamicList<label> samplingSegments;
188  DynamicList<scalar> samplingCurveDist;
189 
190  calcSamples
191  (
192  samplingPts,
193  samplingCells,
194  samplingFaces,
195  samplingSegments,
196  samplingCurveDist
197  );
198 
199  samplingPts.shrink();
200  samplingCells.shrink();
201  samplingFaces.shrink();
202  samplingSegments.shrink();
203  samplingCurveDist.shrink();
204 
205  setSamples
206  (
207  samplingPts,
208  samplingCells,
209  samplingFaces,
210  samplingSegments,
211  samplingCurveDist
212  );
213 }
214 
215 
216 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
217 
219 (
220  const word& name,
221  const polyMesh& mesh,
222  const meshSearch& searchEngine,
223  const dictionary& dict
224 )
225 :
226  sampledSet(name, mesh, searchEngine, dict),
227  patches_
228  (
229  mesh.boundaryMesh().patchSet
230  (
231  wordReList(dict.lookup("patches"))
232  )
233  ),
234  nPoints_(readLabel(dict.lookup("nPoints")))
235 {
236  genSamples();
237 
238  if (debug)
239  {
240  write(Info);
241  }
242 }
243 
244 
245 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
246 
248 {}
249 
250 
251 // ************************************************************************* //
barycentric2D barycentric2D01(Random &rndGen)
Generate a random barycentric coordinate within the unit triangle.
Definition: barycentric2D.C:31
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:424
Various (local, not parallel) searches on polyMesh; uses (demand driven) octree to search...
Definition: meshSearch.H:57
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:427
labelHashSet patchSet(const UList< wordRe > &patchNames, const bool warnNotFound=true, const bool usePatchGroups=true) const
Return the set of patch IDs corresponding to the given names.
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
virtual ~boundaryRandom()
Destructor.
Macros for easy insertion into run-time selection tables.
Random rndGen(label(0))
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
dynamicFvMesh & mesh
Holds list of sampling points which is filled at construction time. Various implementations of this b...
Definition: sampledSet.H:61
A class for handling words, derived from string.
Definition: word.H:59
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
List< label > labelList
A List of labels.
Definition: labelList.H:56
label readLabel(Istream &is)
Definition: label.H:64
addToRunTimeSelectionTable(sampledSet, arcUniform, word)
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:409
label patchi
virtual bool write()
Sample and write.
Definition: sampledSets.C:241
triangle< point, const point & > triPointRef
Definition: triPointRef.H:44
boundaryRandom(const word &name, const polyMesh &mesh, const meshSearch &searchEngine, const dictionary &dict)
Construct from dictionary.
messageStream Info
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
List< wordRe > wordReList
A List of wordRe (word or regular expression)
Definition: wordReList.H:50
defineTypeNameAndDebug(arcUniform, 0)
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576