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-2020 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 {
45  defineTypeNameAndDebug(boundaryRandom, 0);
46  addToRunTimeSelectionTable(sampledSet, boundaryRandom, word);
47 }
48 }
49 
50 
51 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
52 
53 void Foam::sampledSets::boundaryRandom::calcSamples
54 (
55  DynamicList<point>& samplingPts,
56  DynamicList<label>& samplingCells,
57  DynamicList<label>& samplingFaces,
58  DynamicList<label>& samplingSegments,
59  DynamicList<scalar>& samplingCurveDist
60 ) const
61 {
62  // Get the patch IDs
63  const labelList patchIDs(mesh().boundaryMesh().patchSet(patches_).toc());
64 
65  // Triangulate the patch faces
66  DynamicList<label> triFaces, triTetPts;
67  forAll(patchIDs, patchi)
68  {
69  const polyPatch& patch = mesh().boundaryMesh()[patchIDs[patchi]];
70 
71  forAll(patch, patchFacei)
72  {
73  const face& f = patch[patchFacei];
74  const label facei = patchFacei + patch.start();
75 
76  for (label tetPti = 1; tetPti < f.size() - 1; ++ tetPti)
77  {
78  triFaces.append(facei);
79  triTetPts.append(tetPti);
80  }
81  }
82  }
83 
84  // Generate the fractions which select the processor, patch and triangle
85  scalarField trisFraction(triFaces.size() + 1, 0);
86  forAll(triFaces, trii)
87  {
88  const tetIndices tetIs
89  (
90  mesh().faceOwner()[triFaces[trii]],
91  triFaces[trii],
92  triTetPts[trii]
93  );
94 
95  trisFraction[trii + 1] =
96  trisFraction[trii] + tetIs.faceTri(mesh()).mag();
97  }
98 
99  scalarField procsFraction(Pstream::nProcs() + 1, 0);
100  {
101  scalarField procsArea(Pstream::nProcs(), 0);
102  procsArea[Pstream::myProcNo()] = trisFraction.last();
103  Pstream::listCombineGather(procsArea, maxEqOp<scalar>());
104  Pstream::listCombineScatter(procsArea);
105  for(label proci = 0; proci < Pstream::nProcs(); ++ proci)
106  {
107  procsFraction[proci + 1] = procsFraction[proci] + procsArea[proci];
108  }
109  }
110 
111  if (triFaces.size())
112  {
113  trisFraction /= trisFraction.last();
114  }
115 
116  if (procsFraction.last() != 0)
117  {
118  procsFraction /= procsFraction.last();
119  }
120 
121  // Generate the samples
122  Random rndGen(261782);
123  const label proci = Pstream::myProcNo();
124  for (label i = 0; i < nPoints_; ++ i)
125  {
126  // Request all random numbers simultaneously on all processors so that
127  // the generator state stays consistent
128 
129  const scalar rProc = rndGen.scalar01();
130  const scalar rTri = rndGen.scalar01();
131  const barycentric2D r2D = barycentric2D01(rndGen);
132 
133  if (procsFraction[proci] < rProc && rProc <= procsFraction[proci + 1])
134  {
135  label trii = 0;
136  while (rTri > trisFraction[trii + 1])
137  {
138  ++ trii;
139  }
140 
141  const tetIndices tetIs
142  (
143  mesh().faceOwner()[triFaces[trii]],
144  triFaces[trii],
145  triTetPts[trii]
146  );
147 
148  const barycentric r3D
149  (
150  rootSmall,
151  (1 - rootSmall)*r2D.a(),
152  (1 - rootSmall)*r2D.b(),
153  (1 - rootSmall)*r2D.c()
154  );
155 
156  samplingPts.append(tetIs.tet(mesh()).barycentricToPoint(r3D));
157  samplingCells.append(tetIs.cell());
158  samplingFaces.append(tetIs.face());
159  samplingSegments.append(0);
160  samplingCurveDist.append(scalar(i));
161  }
162  }
163 }
164 
165 
166 void Foam::sampledSets::boundaryRandom::genSamples()
167 {
168  // Storage for sample points
169  DynamicList<point> samplingPts;
170  DynamicList<label> samplingCells;
171  DynamicList<label> samplingFaces;
172  DynamicList<label> samplingSegments;
173  DynamicList<scalar> samplingCurveDist;
174 
175  calcSamples
176  (
177  samplingPts,
178  samplingCells,
179  samplingFaces,
180  samplingSegments,
181  samplingCurveDist
182  );
183 
184  samplingPts.shrink();
185  samplingCells.shrink();
186  samplingFaces.shrink();
187  samplingSegments.shrink();
188  samplingCurveDist.shrink();
189 
190  setSamples
191  (
192  samplingPts,
193  samplingCells,
194  samplingFaces,
195  samplingSegments,
196  samplingCurveDist
197  );
198 }
199 
200 
201 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
202 
204 (
205  const word& name,
206  const polyMesh& mesh,
207  const meshSearch& searchEngine,
208  const dictionary& dict
209 )
210 :
211  sampledSet(name, mesh, searchEngine, dict),
212  patches_(dict.lookup("patches")),
213  nPoints_(dict.lookup<label>("nPoints"))
214 {
215  genSamples();
216 
217  if (debug)
218  {
219  write(Info);
220  }
221 }
222 
223 
224 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
225 
227 {}
228 
229 
230 // ************************************************************************* //
barycentric2D barycentric2D01(Random &rndGen)
Generate a random barycentric coordinate within the unit triangle.
Definition: barycentric2D.C:31
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:434
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:156
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
Barycentric< scalar > barycentric
A scalar version of the templated Barycentric.
Definition: barycentric.H:45
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))
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
addToRunTimeSelectionTable(sampledSet, arcUniform, word)
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
label patchi
virtual bool write()
Sample and write.
Definition: sampledSets.C:228
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
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:844