Distribution.H
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 Class
25  Foam::Distribution
26 
27 Description
28  Accumulating histogram of component values.
29  Specified bin resolution, automatic generation of bins.
30 
31 SourceFiles
32  DistributionI.H
33  Distribution.C
34  DistributionIO.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Distribution_H
39 #define Distribution_H
40 
41 #include "List.H"
42 #include "Pair.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 template<class Type>
50 class Distribution;
51 
52 template<class Type>
54 
55 template<class Type>
56 Ostream& operator<<(Ostream&, const Distribution<Type>&);
57 
58 /*---------------------------------------------------------------------------*\
59  Class Distribution Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 template<class Type>
63 class Distribution
64 :
65  public List<List<scalar>>
66 {
67  // Private Data
68 
69  //- Width of the bin for each component
70  Type binWidth_;
71 
72  //- The start bin index of each component
73  List<label> listStarts_;
74 
75 
76 public:
77 
78  //- Component type
79  typedef typename pTraits<Type>::cmptType cmptType;
80 
81 
82  // Constructors
83 
84  //- Construct null
85  Distribution();
86 
87  //- Construct from separate binWidth for each component
88  Distribution(const Type& binWidth);
89 
90  //- Copy constructor
91  Distribution(const Distribution& d);
92 
93  //- Move constructor
95 
96 
97  //- Destructor
98  ~Distribution();
99 
100 
101  // Member Functions
102 
103  //- Sum the total weight added to the component in the
104  // argument
105  scalar totalWeight(direction cmpt) const;
106 
107  List<label> keys(direction cmpt) const;
108 
109  //- Return the appropriate List index for the given bin index.
110  // Resizes the List if required
111  label index(direction cmpt, label n);
112 
113  //- Returns the indices of the first and last non-zero entries
114  Pair<label> validLimits(direction cmpt) const;
115 
116  Type mean() const;
117 
118  // From http://mathworld.wolfram.com/StatisticalMedian.html
119  // The statistical median is the value of the Distribution
120  // variable where the cumulative Distribution = 0.5.
121  Type median() const;
122 
123  //- Add a value to the distribution, optionally specifying a weight
124  void add
125  (
126  const Type& valueToAdd,
127  const Type& weight = pTraits<Type>::one
128  );
129 
130  //- Return the normalised distribution (probability density)
131  // and bins
133 
134  //- Return the distribution of the total bin weights
135  List<List < Pair<scalar>>> raw() const;
136 
137  //- Return the cumulative normalised distribution and
138  // integration locations (at end of bins)
140 
141  //- Return the cumulative total bin weights and integration
142  // locations (at end of bins)
144 
145  //- Resets the Distribution by clearing the stored lists.
146  // Leaves the same number of them and the same binWidth.
147  void clear();
148 
149 
150  // Access
151 
152  //- Return the bin width
153  inline const Type& binWidth() const;
154 
155  //- Return the List start bin indices
156  inline const List<label>& listStarts() const;
157 
158  // Write
159 
160  //- Write the distribution to file: key normalised raw.
161  // Produces a separate file for each component.
162  void write(const fileName& filePrefix) const;
163 
164 
165  // Member Operators
166 
167  void operator=(const Distribution<Type>&);
168 
170 
171 
172  // IOstream Operators
173 
174  friend Istream& operator>> <Type>
175  (
176  Istream&,
178  );
179 
180  friend Ostream& operator<< <Type>
181  (
182  Ostream&,
183  const Distribution<Type>&
184  );
185 };
186 
187 
188 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
189 
190 template<class Type>
191 Distribution<Type> operator+
192 (
193  const Distribution<Type>&,
194  const Distribution<Type>&
195 );
196 
197 
198 } // End namespace Foam
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
201 
202 #include "DistributionI.H"
203 
204 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
205 
206 #ifdef NoRepository
207  #include "Distribution.C"
208 #endif
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 #endif
213 
214 // ************************************************************************* //
label index(direction cmpt, label n)
Return the appropriate List index for the given bin index.
Definition: Distribution.C:109
~Distribution()
Destructor.
Definition: Distribution.C:71
A class for handling file names.
Definition: fileName.H:79
Accumulating histogram of component values. Specified bin resolution, automatic generation of bins...
Definition: Distribution.H:49
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
uint8_t direction
Definition: direction.H:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const Type & binWidth() const
Return the bin width.
Definition: DistributionI.H:30
Traits class for primitives.
Definition: pTraits.H:50
void operator=(const Distribution< Type > &)
Definition: Distribution.C:571
Pair< label > validLimits(direction cmpt) const
Returns the indices of the first and last non-zero entries.
Definition: Distribution.C:175
List< List< Pair< scalar > > > normalised() const
Return the normalised distribution (probability density)
Definition: Distribution.C:344
const List< label > & listStarts() const
Return the List start bin indices.
Definition: DistributionI.H:38
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: contiguous.H:49
List< List< Pair< scalar > > > cumulativeNormalised() const
Return the cumulative normalised distribution and.
Definition: Distribution.C:434
Istream & operator>>(Istream &, directionInfo &)
Distribution()
Construct null.
Definition: Distribution.C:33
Type mean() const
Definition: Distribution.C:206
List< List< Pair< scalar > > > cumulativeRaw() const
Return the cumulative total bin weights and integration.
Definition: Distribution.C:470
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
void clear()
Resets the Distribution by clearing the stored lists.
Definition: Distribution.C:501
scalar totalWeight(direction cmpt) const
Sum the total weight added to the component in the.
Definition: Distribution.C:78
List< List< Pair< scalar > > > raw() const
Return the distribution of the total bin weights.
Definition: Distribution.C:392
pTraits< Type >::cmptType cmptType
Component type.
Definition: Distribution.H:78
Type median() const
Definition: Distribution.C:235
void add(const Type &valueToAdd, const Type &weight=pTraits< Type >::one)
Add a value to the distribution, optionally specifying a weight.
Definition: Distribution.C:322
label n
void write(const fileName &filePrefix) const
Write the distribution to file: key normalised raw.
Definition: Distribution.C:513
List< label > keys(direction cmpt) const
Definition: Distribution.C:94
Namespace for OpenFOAM.