general.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "general.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace distributionModels
37 {
39  addToRunTimeSelectionTable(distributionModel, general, dictionary);
40 }
41 }
42 
43 
44 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
45 
46 void Foam::distributionModels::general::initialise()
47 {
48  static scalar eps = ROOTVSMALL;
49 
50  const label nEntries = xy_.size();
51 
52  integral_.setSize(nEntries);
53 
54  // Normalize the cumulative distribution
55  integral_[0] = 0.0;
56  for (label i = 1; i < nEntries; i++)
57  {
58  scalar k = (xy_[i][1] - xy_[i-1][1])/(xy_[i][0] - xy_[i-1][0] + eps);
59  scalar d = xy_[i-1][1] - k*xy_[i-1][0];
60  scalar y1 = xy_[i][0]*(0.5*k*xy_[i][0] + d);
61  scalar y0 = xy_[i-1][0]*(0.5*k*xy_[i-1][0] + d);
62  scalar area = y1 - y0;
63 
64  integral_[i] = area + integral_[i-1];
65  }
66 
67  scalar sumArea = integral_.last();
68 
69  meanValue_ = sumArea/(maxValue() - minValue() + eps);
70 
71  for (label i=0; i < nEntries; i++)
72  {
73  xy_[i][1] /= sumArea + eps;
74  integral_[i] /= sumArea + eps;
75  }
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
80 
82 (
83  const dictionary& dict,
84  Random& rndGen
85 )
86 :
87  distributionModel(typeName, dict, rndGen),
88  xy_(distributionModelDict_.lookup("distribution")),
89  meanValue_(0.0),
90  integral_()
91 {
92  check();
93 
94  initialise();
95 }
96 
97 
99 (
100  const UList<scalar>& sampleData,
101  const scalar binWidth,
102  Random& rndGen
103 )
104 :
106  xy_(),
107  meanValue_(0.0),
108  integral_()
109 {
110  scalar minValue = GREAT;
111  scalar maxValue = -GREAT;
112  forAll(sampleData, i)
113  {
114  minValue = min(minValue, sampleData[i]);
115  maxValue = max(maxValue, sampleData[i]);
116  }
117 
118  label bin0 = floor(minValue/binWidth);
119  label bin1 = ceil(maxValue/binWidth);
120  label nEntries = bin1 - bin0;
121 
122  if (nEntries == 0)
123  {
125  << "Data cannot be binned - zero bins generated" << nl
126  << " Bin width : " << binWidth << nl
127  << " Sample data : " << sampleData
128  << endl;
129 
130  return;
131  }
132 
133  xy_.setSize(nEntries);
134 
135  // Populate bin boundaries and initialise occurrences
136  for (label bini = 0; bini < nEntries; ++bini)
137  {
138  xy_[bini][0] = (bin0 + bini)*binWidth;
139  xy_[bini][1] = 0;
140  }
141 
142  // Populate occurrences
143  forAll(sampleData, i)
144  {
145  label bini = floor(sampleData[i]/binWidth) - bin0;
146  xy_[bini][1]++;
147  }
148 
149  initialise();
150 }
151 
152 
154 :
156  xy_(p.xy_),
157  meanValue_(p.meanValue_),
158  integral_(p.integral_)
159 {}
160 
161 
162 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
163 
165 {}
166 
167 
168 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 
171 {
172  scalar y = rndGen_.sample01<scalar>();
173 
174  // Find the interval where y is in the table
175  label n = 1;
176  while (integral_[n] <= y)
177  {
178  n++;
179  }
180 
181  scalar k = (xy_[n][1] - xy_[n-1][1])/(xy_[n][0] - xy_[n-1][0]);
182  scalar d = xy_[n-1][1] - k*xy_[n-1][0];
183 
184  scalar alpha = y + xy_[n-1][0]*(0.5*k*xy_[n-1][0] + d) - integral_[n-1];
185  scalar x = 0.0;
186 
187  // If k is small it is a linear equation, otherwise it is of second order
188  if (mag(k) > SMALL)
189  {
190  scalar p = 2.0*d/k;
191  scalar q = -2.0*alpha/k;
192  scalar sqrtEr = sqrt(0.25*p*p - q);
193 
194  scalar x1 = -0.5*p + sqrtEr;
195  scalar x2 = -0.5*p - sqrtEr;
196  if ((x1 >= xy_[n-1][0]) && (x1 <= xy_[n][0]))
197  {
198  x = x1;
199  }
200  else
201  {
202  x = x2;
203  }
204  }
205  else
206  {
207  x = alpha/d;
208  }
209 
210  return x;
211 }
212 
213 
215 {
216  return xy_.first()[0];
217 }
218 
219 
221 {
222  return xy_.last()[0];
223 }
224 
225 
227 {
228  return meanValue_;
229 }
230 
231 
233 {
234 // distributionModel::readData(is);
235  is >> xy_;
236  initialise();
237 }
238 
239 
241 {
242 // distributionModel::writeData(os);
243  os << xy_;
244 }
245 
246 
248 (
249  const word& dictName
250 ) const
251 {
252  // dictionary dict = distributionModel::writeDict(dictName);
254  dict.add("x", x());
255  dict.add("y", y());
256 
257  return dict;
258 }
259 
260 
262 {
263  // distributionModel::readDict(dict);
264  List<scalar> x(dict.lookup("x"));
265  List<scalar> y(dict.lookup("y"));
266 
267  xy_.setSize(x.size());
268  forAll(xy_, i)
269  {
270  xy_[i][0] = x[i];
271  xy_[i][1] = y[i];
272  }
273 
274  initialise();
275 }
276 
277 
280 {
281  tmp<Field<scalar>> tx(new Field<scalar>(xy_.size()));
282  scalarField& xi = tx.ref();
283  forAll(xy_, i)
284  {
285  xi[i] = xy_[i][0];
286  }
287 
288  return tx;
289 }
290 
291 
294 {
295  tmp<Field<scalar>> ty(new Field<scalar>(xy_.size()));
296  scalarField& yi = ty.ref();
297  forAll(xy_, i)
298  {
299  yi[i] = xy_[i][1];
300  }
301 
302  return ty;
303 }
304 
305 
306 Foam::Ostream& Foam::operator<<
307 (
308  Ostream& os,
310 )
311 {
312  os.check(FUNCTION_NAME);
313 
314  b.writeData(os);
315  return os;
316 }
317 
318 
320 {
321  is.check(FUNCTION_NAME);
322 
323  b.readData(is);
324  return is;
325 }
326 
327 
328 // ************************************************************************* //
Foam::Random
Random number generator.
Definition: Random.H:59
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::distributionModels::general::x
virtual tmp< Field< scalar > > x() const
Bin boundaries.
Definition: general.C:279
Foam::distributionModels::general::~general
virtual ~general()
Destructor.
Definition: general.C:164
Foam::y1
dimensionedScalar y1(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:282
Foam::distributionModels::general::meanValue
virtual scalar meanValue() const
Return the mean value.
Definition: general.C:226
Foam::distributionModels::general::minValue
virtual scalar minValue() const
Return the minimum value.
Definition: general.C:214
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::constant::atomic::alpha
const dimensionedScalar alpha
Fine-structure constant: default SI units: [].
Definition: readThermalProperties.H:212
Foam::distributionModels::addToRunTimeSelectionTable
addToRunTimeSelectionTable(distributionModel, binned, dictionary)
dictName
const word dictName("blockMeshDict")
general.H
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::distributionModel
A library of runtime-selectable distribution models.
Definition: distributionModel.H:70
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::distributionModels::general
general distribution model
Definition: general.H:70
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::distributionModels::general::y
virtual tmp< Field< scalar > > y() const
Probabilities.
Definition: general.C:293
Foam::dictionary::null
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:385
Foam::distributionModels::general::sample
virtual scalar sample() const
Sample the distributionModel.
Definition: general.C:170
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::tmp::ref
T & ref() const
Definition: tmpI.H:228
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Field< scalar >
general
General relative velocity model.
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::distributionModels::general::readDict
virtual void readDict(const dictionary &dict)
Read data from dictionary.
Definition: general.C:261
Foam::y0
dimensionedScalar y0(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:281
Foam::distributionModels::general::writeDict
virtual dictionary writeDict(const word &dictName) const
Write data in dictionary format.
Definition: general.C:248
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::distributionModels::general::general
general(const dictionary &dict, Random &rndGen)
Construct from components.
Definition: general.C:82
Foam::distributionModels::general::maxValue
virtual scalar maxValue() const
Return the maximum value.
Definition: general.C:220
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< scalar >
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::distributionModels::defineTypeNameAndDebug
defineTypeNameAndDebug(binned, 0)
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::distributionModels::general::readData
virtual void readData(Istream &os)
Read data from stream.
Definition: general.C:232
x
x
Definition: LISASMDCalcMethod2.H:52
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::fieldTypes::area
const wordList area
Standard area field types (scalar, vector, tensor, etc)
minValue
scalar minValue
Definition: LISASMDCalcMethod2.H:12
rndGen
Random rndGen
Definition: createFields.H:23
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::distributionModels::general::writeData
virtual void writeData(Ostream &os) const
Write data to stream.
Definition: general.C:240
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303
y
scalar y
Definition: LISASMDCalcMethod1.H:14
maxValue
scalar maxValue
Definition: LISASMDCalcMethod1.H:5