sphereRandom.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 "sphereRandom.H"
27 #include "sampledSet.H"
28 #include "meshSearch.H"
29 #include "DynamicList.H"
30 #include "polyMesh.H"
32 #include "word.H"
33 #include "mathematicalConstants.H"
34 #include "Random.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 namespace sampledSets
41 {
42  defineTypeNameAndDebug(sphereRandom, 0);
43  addToRunTimeSelectionTable(sampledSet, sphereRandom, word);
44 }
45 }
46 
47 
48 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
49 
50 void Foam::sampledSets::sphereRandom::calcSamples
51 (
52  DynamicList<point>& samplingPositions,
53  DynamicList<label>& samplingSegments,
54  DynamicList<label>& samplingCells,
55  DynamicList<label>& samplingFaces
56 ) const
57 {
58  Random rndGen(261782);
59 
60  for (label i = 0; i < nPoints_; ++ i)
61  {
62  // Request all random numbers simultaneously on all processors so that
63  // the generator state stays consistent
64 
65  vector dpt(vector::uniform(radius_));
66  while (magSqr(dpt) > sqr(radius_))
67  {
68  dpt = 2*radius_*(rndGen.sample01<vector>() - vector::uniform(0.5));
69  }
70 
71  const point pt = centre_ + dpt;
72  const label celli = searchEngine().findCell(pt);
73 
74  if (celli != -1)
75  {
76  samplingPositions.append(pt);
77  samplingSegments.append(i);
78  samplingCells.append(celli);
79  samplingFaces.append(-1);
80  }
81  }
82 }
83 
84 
85 void Foam::sampledSets::sphereRandom::genSamples()
86 {
87  DynamicList<point> samplingPositions;
88  DynamicList<label> samplingSegments;
89  DynamicList<label> samplingCells;
90  DynamicList<label> samplingFaces;
91 
92  calcSamples
93  (
94  samplingPositions,
95  samplingSegments,
96  samplingCells,
97  samplingFaces
98  );
99 
100  samplingPositions.shrink();
101  samplingSegments.shrink();
102  samplingCells.shrink();
103  samplingFaces.shrink();
104 
105  setSamples
106  (
107  samplingPositions,
108  samplingSegments,
109  samplingCells,
110  samplingFaces
111  );
112 }
113 
114 
115 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
116 
118 (
119  const word& name,
120  const polyMesh& mesh,
121  const meshSearch& searchEngine,
122  const dictionary& dict
123 )
124 :
125  sampledSet(name, mesh, searchEngine, dict),
126  centre_(dict.lookup("centre")),
127  radius_(dict.lookup<scalar>("radius")),
128  nPoints_(dict.lookup<label>("nPoints"))
129 {
130  genSamples();
131 }
132 
133 
134 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
135 
137 {}
138 
139 
140 // ************************************************************************* //
Various (local, not parallel) searches on polyMesh; uses (demand driven) octree to search...
Definition: meshSearch.H:57
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
Macros for easy insertion into run-time selection tables.
Random rndGen(label(0))
Holds list of sampling points which is filled at construction time. Various implementations of this b...
Definition: sampledSet.H:61
label findCell(const point &location, const label seedCelli=-1, const bool useTreeSearch=true) const
Find cell containing location.
Definition: meshSearch.C:750
A class for handling words, derived from string.
Definition: word.H:59
addToRunTimeSelectionTable(sampledSet, arcUniform, word)
dimensioned< scalar > magSqr(const dimensioned< Type > &)
vector point
Point is a vector.
Definition: point.H:41
static Vector< scalar > uniform(const scalar &s)
Return a VectorSpace with all elements = s.
Definition: VectorSpaceI.H:168
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:76
defineTypeNameAndDebug(arcUniform, 0)
sphereRandom(const word &name, const polyMesh &mesh, const meshSearch &searchEngine, const dictionary &dict)
Construct from dictionary.
Definition: sphereRandom.C:118
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864