distribution.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-2024 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 "distribution.H"
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
34 }
35 
36 
37 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
38 
40 {
41  if (max() < min())
42  {
44  << type() << ": The maximum value is smaller than the minimum "
45  << "value:" << nl << " max = " << max() << ", min = "
46  << min() << abort(FatalIOError);
47  }
48 }
49 
50 
52 {
53  if (min() < 0)
54  {
56  << type() << ": The minimum value must be greater than "
57  << "zero." << nl << " min = " << min()
58  << abort(FatalIOError);
59  }
60 }
61 
62 
65 (
66  const scalarField& x,
67  const tmp<scalarField>& pdf
68 ) const
69 {
70  return pos0(x - min())*pos0(max() - x)*pdf;
71 }
72 
73 
74 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
75 
77 (
78  const word& name,
79  const unitConversion& units,
80  const dictionary& dict,
81  const label sampleQ,
83 )
84 :
85  Q_(dict.lookup<scalar>("Q")),
86  sampleQ_(sampleQ),
87  rndGen_("rndGen", dict, std::move(rndGen))
88 {
89  if (Q_ < 0)
90  {
92  << name << ": Size exponent cannot be negative" << nl
93  << " Q = " << Q_ << abort(FatalIOError);
94  }
95 
96  if (sampleQ_ < 0)
97  {
99  << name << ": Sampling size exponent cannot be negative" << nl
100  << " sampleQ = " << sampleQ_ << abort(FatalError);
101  }
102 }
103 
104 
106 (
107  const label Q,
108  const label sampleQ,
110 )
111 :
112  Q_(Q),
113  sampleQ_(sampleQ),
114  rndGen_(rndGen)
115 {}
116 
117 
119 :
120  Q_(d.Q_),
121  sampleQ_(sampleQ),
122  rndGen_(d.rndGen_)
123 {}
124 
125 
126 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
127 
129 {}
130 
131 
132 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 
135 {
136  writeEntry(os, "type", type());
137  writeEntry(os, "Q", Q_);
138 }
139 
140 
142 {
143  writeEntry(os, "rndGen", rndGen_);
144 }
145 
146 
148 {
149  const scalar x0 = min(), x1 = max(), d = 0.1*(x1 - x0);
150 
151  tmp<scalarField> tResult(new scalarField(n));
152  scalarField& result = tResult.ref();
153 
154  result[0] = x0 - d;
155  result[1] = x0*(1 - sign(x0)*small);
156  result[2] = x0*(1 + sign(x0)*small);
157 
158  for (label i = 3; i < n - 3; ++ i)
159  {
160  const scalar f = scalar(i - 2)/(n - 5);
161 
162  result[i] = (1 - f)*x0 + f*x1;
163  }
164 
165  result[n - 3] = x1*(1 - sign(x1)*small);
166  result[n - 2] = x1*(1 + sign(x1)*small);
167  result[n - 1] = x1 + d;
168 
169  return tResult;
170 }
171 
172 
173 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
174 
176 (
177  Ostream& os,
178  const unitConversion& units,
179  const distribution& d,
180  const bool write,
181  const bool writeState
182 )
183 {
184  os << nl << indent << token::BEGIN_BLOCK << nl << incrIndent;
185 
186  if (write) d.write(os, units);
187  if (writeState) d.writeState(os);
188 
189  os << decrIndent << indent << token::END_BLOCK << endl;
190 }
191 
192 
193 // ************************************************************************* //
label n
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Base class for statistical distributions.
Definition: distribution.H:76
virtual scalar max() const =0
Return the maximum value.
virtual void writeState(Ostream &os) const
Write the state to a stream.
Definition: distribution.C:141
virtual void validatePositive(const dictionary &dict) const
Validate that the lower bound is positive.
Definition: distribution.C:51
tmp< scalarField > clipPDF(const scalarField &x, const tmp< scalarField > &pdf) const
Clip the PDF values to zero outside the bounds.
Definition: distribution.C:65
const label Q_
Distribution size exponent.
Definition: distribution.H:82
virtual ~distribution()
Destructor.
Definition: distribution.C:128
virtual tmp< scalarField > x(const label n) const
Return coordinates to plot across the range of the distribution.
Definition: distribution.C:147
const label sampleQ_
Sample size exponent.
Definition: distribution.H:85
distribution(const word &name, const unitConversion &units, const dictionary &dict, const label sampleQ, randomGenerator &&rndGen)
Construct from dictionary.
Definition: distribution.C:77
virtual void validateBounds(const dictionary &dict) const
Validate that the bounds are monotonic.
Definition: distribution.C:39
virtual void write(Ostream &os, const unitConversion &units) const
Write to a stream.
Definition: distribution.C:134
virtual scalar min() const =0
Return the minimum value.
Random number generator.
A class for managing temporary objects.
Definition: tmp.H:55
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
@ BEGIN_BLOCK
Definition: token.H:113
@ END_BLOCK
Definition: token.H:114
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
Namespace for OpenFOAM.
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:241
dimensionedScalar pos0(const dimensionedScalar &ds)
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
dimensionedScalar sign(const dimensionedScalar &ds)
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:234
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
const HashTable< unitConversion > & units()
Get the table of unit conversions.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
defineTypeNameAndDebug(combustionModel, 0)
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
IOerror FatalIOError
error FatalError
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:227
static const char nl
Definition: Ostream.H:266
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
labelList f(nPoints)
dictionary dict
randomGenerator rndGen(653213)