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-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 "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>& samplingPts,
53  DynamicList<label>& samplingCells,
54  DynamicList<label>& samplingFaces,
55  DynamicList<label>& samplingSegments,
56  DynamicList<scalar>& samplingCurveDist
57 ) const
58 {
59  Random rndGen(261782);
60 
61  for (label i = 0; i < nPoints_; ++ i)
62  {
63  // Request all random numbers simultaneously on all processors so that
64  // the generator state stays consistent
65 
66  vector dpt(vector::uniform(radius_));
67  while (magSqr(dpt) > sqr(radius_))
68  {
69  dpt = 2*radius_*(rndGen.sample01<vector>() - vector::uniform(0.5));
70  }
71 
72  const point pt = centre_ + dpt;
73  const label celli = searchEngine().findCell(pt);
74 
75  if (celli != -1)
76  {
77  samplingPts.append(pt);
78  samplingCells.append(celli);
79  samplingFaces.append(-1);
80  samplingSegments.append(0);
81  samplingCurveDist.append(scalar(i));
82  }
83  }
84 }
85 
86 
87 void Foam::sampledSets::sphereRandom::genSamples()
88 {
89  // Storage for sample points
90  DynamicList<point> samplingPts;
91  DynamicList<label> samplingCells;
92  DynamicList<label> samplingFaces;
93  DynamicList<label> samplingSegments;
94  DynamicList<scalar> samplingCurveDist;
95 
96  calcSamples
97  (
98  samplingPts,
99  samplingCells,
100  samplingFaces,
101  samplingSegments,
102  samplingCurveDist
103  );
104 
105  samplingPts.shrink();
106  samplingCells.shrink();
107  samplingFaces.shrink();
108  samplingSegments.shrink();
109  samplingCurveDist.shrink();
110 
111  setSamples
112  (
113  samplingPts,
114  samplingCells,
115  samplingFaces,
116  samplingSegments,
117  samplingCurveDist
118  );
119 }
120 
121 
122 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
123 
125 (
126  const word& name,
127  const polyMesh& mesh,
128  const meshSearch& searchEngine,
129  const dictionary& dict
130 )
131 :
132  sampledSet(name, mesh, searchEngine, dict),
133  centre_(dict.lookup("centre")),
134  radius_(readScalar(dict.lookup("radius"))),
135  nPoints_(readLabel(dict.lookup("nPoints")))
136 {
137  genSamples();
138 
139  if (debug)
140  {
141  write(Info);
142  }
143 }
144 
145 
146 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
147 
149 {}
150 
151 
152 // ************************************************************************* //
Various (local, not parallel) searches on polyMesh; uses (demand driven) octree to search...
Definition: meshSearch.H:57
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
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
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
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if successful.
Definition: doubleScalar.H:68
label readLabel(Istream &is)
Definition: label.H:64
addToRunTimeSelectionTable(sampledSet, arcUniform, word)
dimensioned< scalar > magSqr(const dimensioned< Type > &)
vector point
Point is a vector.
Definition: point.H:41
virtual bool write()
Sample and write.
Definition: sampledSets.C:241
messageStream Info
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:74
defineTypeNameAndDebug(arcUniform, 0)
sphereRandom(const word &name, const polyMesh &mesh, const meshSearch &searchEngine, const dictionary &dict)
Construct from dictionary.
Definition: sphereRandom.C:125
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:583