Distribution.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 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  //- Construct as copy
91  Distribution(const Distribution& d);
92 
93 
94  //- Destructor
95  ~Distribution();
96 
97  // Member Functions
98 
99  //- Sum the total weight added to the component in the
100  // argument
101  scalar totalWeight(direction cmpt) const;
102 
103  List<label> keys(direction cmpt) const;
104 
105  //- Return the appropriate List index for the given bin index.
106  // Resizes the List if required
107  label index(direction cmpt, label n);
108 
109  //- Returns the indices of the first and last non-zero entries
110  Pair<label> validLimits(direction cmpt) const;
111 
112  Type mean() const;
113 
114  // From http://mathworld.wolfram.com/StatisticalMedian.html
115  // The statistical median is the value of the Distribution
116  // variable where the cumulative Distribution = 0.5.
117  Type median() const;
118 
119  //- Add a value to the distribution, optionally specifying a weight
120  void add
121  (
122  const Type& valueToAdd,
123  const Type& weight = pTraits<Type>::one
124  );
125 
126  //- Return the normalised distribution (probability density)
127  // and bins
129 
130  //- Return the distribution of the total bin weights
131  List<List < Pair<scalar>>> raw() const;
132 
133  //- Return the cumulative normalised distribution and
134  // integration locations (at end of bins)
136 
137  //- Return the cumulative total bin weights and integration
138  // locations (at end of bins)
140 
141  //- Resets the Distribution by clearing the stored lists.
142  // Leaves the same number of them and the same binWidth.
143  void clear();
144 
145 
146  // Access
147 
148  //- Return the bin width
149  inline const Type& binWidth() const;
150 
151  //- Return the List start bin indices
152  inline const List<label>& listStarts() const;
153 
154  // Write
155 
156  //- Write the distribution to file: key normalised raw.
157  // Produces a separate file for each component.
158  void write(const fileName& filePrefix) const;
159 
160 
161  // Member Operators
162 
163  void operator=(const Distribution<Type>&);
164 
165  // IOstream Operators
166 
167  friend Istream& operator>> <Type>
168  (
169  Istream&,
171  );
172 
173  friend Ostream& operator<< <Type>
174  (
175  Ostream&,
176  const Distribution<Type>&
177  );
178 };
179 
180 
181 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
182 
183 template<class Type>
184 Distribution<Type> operator+
185 (
186  const Distribution<Type>&,
187  const Distribution<Type>&
188 );
189 
190 
191 } // End namespace Foam
192 
193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
194 
195 #include "DistributionI.H"
196 
197 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
198 
199 #ifdef NoRepository
200  #include "Distribution.C"
201 #endif
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 
205 #endif
206 
207 // ************************************************************************* //
label index(direction cmpt, label n)
Return the appropriate List index for the given bin index.
Definition: Distribution.C:100
void write(const fileName &filePrefix) const
Write the distribution to file: key normalised raw.
Definition: Distribution.C:504
~Distribution()
Destructor.
Definition: Distribution.C:62
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
uint8_t direction
Definition: direction.H:46
A class for handling file names.
Definition: fileName.H:69
List< List< Pair< scalar > > > cumulativeNormalised() const
Return the cumulative normalised distribution and.
Definition: Distribution.C:425
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
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Type median() const
Definition: Distribution.C:226
Traits class for primitives.
Definition: pTraits.H:50
List< List< Pair< scalar > > > normalised() const
Return the normalised distribution (probability density)
Definition: Distribution.C:335
void operator=(const Distribution< Type > &)
Definition: Distribution.C:561
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: contiguous.H:49
const Type & binWidth() const
Return the bin width.
Definition: DistributionI.H:30
Istream & operator>>(Istream &, directionInfo &)
Distribution()
Construct null.
Definition: Distribution.C:33
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
List< List< Pair< scalar > > > raw() const
Return the distribution of the total bin weights.
Definition: Distribution.C:383
void clear()
Resets the Distribution by clearing the stored lists.
Definition: Distribution.C:492
List< label > keys(direction cmpt) const
Definition: Distribution.C:85
Type mean() const
Definition: Distribution.C:197
pTraits< Type >::cmptType cmptType
Component type.
Definition: Distribution.H:78
Pair< label > validLimits(direction cmpt) const
Returns the indices of the first and last non-zero entries.
Definition: Distribution.C:166
void add(const Type &valueToAdd, const Type &weight=pTraits< Type >::one)
Add a value to the distribution, optionally specifying a weight.
Definition: Distribution.C:313
label n
const List< label > & listStarts() const
Return the List start bin indices.
Definition: DistributionI.H:38
List< List< Pair< scalar > > > cumulativeRaw() const
Return the cumulative total bin weights and integration.
Definition: Distribution.C:461
Namespace for OpenFOAM.
scalar totalWeight(direction cmpt) const
Sum the total weight added to the component in the.
Definition: Distribution.C:69