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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::Distribution
28 
29 Description
30  Accumulating histogram of component values.
31  Specified bin resolution, automatic generation of bins.
32 
33 SourceFiles
34  DistributionI.H
35  Distribution.C
36  DistributionIO.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Distribution_H
41 #define Distribution_H
42 
43 #include "List.H"
44 #include "Pair.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 template<class Type>
52 class Distribution;
53 
54 template<class Type>
56 
57 template<class Type>
59 
60 /*---------------------------------------------------------------------------*\
61  Class Distribution Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 template<class Type>
65 class Distribution
66 :
67  public List<List<scalar>>
68 {
69  // Private data
70 
71  //- Width of the bin for each component
72  Type binWidth_;
73 
74  //- The start bin index of each component
75  List<label> listStarts_;
76 
77 
78 public:
79 
80  //- Component type
81  typedef typename pTraits<Type>::cmptType cmptType;
82 
83 
84  // Constructors
85 
86  //- Construct null
87  Distribution();
88 
89  //- Construct from separate binWidth for each component
90  Distribution(const Type& binWidth);
91 
92  //- Construct as copy
93  Distribution(const Distribution& d);
94 
95 
96  //- Destructor
97  ~Distribution();
98 
99  // Member Functions
100 
101  //- Sum the total weight added to the component in the
102  // argument
103  scalar totalWeight(direction cmpt) const;
104 
105  List<label> keys(direction cmpt) const;
106 
107  //- Return the appropriate List index for the given bin index.
108  // Resizes the List if required
109  label index(direction cmpt, label n);
110 
111  //- Returns the indices of the first and last non-zero entries
112  Pair<label> validLimits(direction cmpt) const;
113 
114  Type mean() const;
115 
116  // From http://mathworld.wolfram.com/StatisticalMedian.html
117  // The statistical median is the value of the Distribution
118  // variable where the cumulative Distribution = 0.5.
119  Type median() const;
120 
121  //- Add a value to the distribution, optionally specifying a weight
122  void add
123  (
124  const Type& valueToAdd,
125  const Type& weight = pTraits<Type>::one
126  );
127 
128  //- Return the normalised distribution (probability density)
129  // and bins
131 
132  //- Return the distribution of the total bin weights
133  List<List < Pair<scalar>>> raw() const;
134 
135  //- Return the cumulative normalised distribution and
136  // integration locations (at end of bins)
138 
139  //- Return the cumulative total bin weights and integration
140  // locations (at end of bins)
142 
143  //- Resets the Distribution by clearing the stored lists.
144  // Leaves the same number of them and the same binWidth.
145  void clear();
146 
147 
148  // Access
149 
150  //- Return the bin width
151  inline const Type& binWidth() const;
152 
153  //- Return the List start bin indices
154  inline const List<label>& listStarts() const;
155 
156  // Write
157 
158  //- Write the distribution to file: key normalised raw.
159  // Produces a separate file for each component.
160  void write(const fileName& filePrefix) const;
161 
162 
163  // Member Operators
164 
165  void operator=(const Distribution<Type>&);
166 
167  // IOstream Operators
168 
169  friend Istream& operator>> <Type>
170  (
171  Istream&,
173  );
174 
175  friend Ostream& operator<< <Type>
176  (
177  Ostream&,
178  const Distribution<Type>&
179  );
180 };
181 
182 
183 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
184 
185 template<class Type>
186 Distribution<Type> operator+
187 (
188  const Distribution<Type>&,
189  const Distribution<Type>&
190 );
191 
192 
193 } // End namespace Foam
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 #include "DistributionI.H"
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #ifdef NoRepository
202  #include "Distribution.C"
203 #endif
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 #endif
208 
209 // ************************************************************************* //
Foam::Distribution::cmptType
pTraits< Type >::cmptType cmptType
Component type.
Definition: Distribution.H:80
Foam::Distribution::validLimits
Pair< label > validLimits(direction cmpt) const
Returns the indices of the first and last non-zero entries.
Definition: Distribution.C:162
List.H
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::Distribution::~Distribution
~Distribution()
Destructor.
Definition: Distribution.C:65
Foam::Distribution::cumulativeNormalised
List< List< Pair< scalar > > > cumulativeNormalised() const
Return the cumulative normalised distribution and.
Definition: Distribution.C:421
Foam::Distribution::cumulativeRaw
List< List< Pair< scalar > > > cumulativeRaw() const
Return the cumulative total bin weights and integration.
Definition: Distribution.C:457
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Pair.H
Distribution.C
Foam::Distribution::listStarts
const List< label > & listStarts() const
Return the List start bin indices.
Definition: DistributionI.H:40
Foam::Distribution::raw
List< List< Pair< scalar > > > raw() const
Return the distribution of the total bin weights.
Definition: Distribution.C:379
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::Distribution::totalWeight
scalar totalWeight(direction cmpt) const
Sum the total weight added to the component in the.
Definition: Distribution.C:72
Foam::Distribution::keys
List< label > keys(direction cmpt) const
Definition: Distribution.C:88
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Distribution::write
void write(const fileName &filePrefix) const
Write the distribution to file: key normalised raw.
Definition: Distribution.C:500
Foam::Distribution::median
Type median() const
Definition: Distribution.C:222
Foam::Distribution
Accumulating histogram of component values. Specified bin resolution, automatic generation of bins.
Definition: Distribution.H:51
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Distribution::index
label index(direction cmpt, label n)
Return the appropriate List index for the given bin index.
Definition: Distribution.C:96
DistributionI.H
Foam::Distribution::binWidth
const Type & binWidth() const
Return the bin width.
Definition: DistributionI.H:32
Foam::Pair< label >
Foam::Distribution::mean
Type mean() const
Definition: Distribution.C:193
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::Distribution::clear
void clear()
Resets the Distribution by clearing the stored lists.
Definition: Distribution.C:488
Foam::Distribution::normalised
List< List< Pair< scalar > > > normalised() const
Return the normalised distribution (probability density)
Definition: Distribution.C:331
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Distribution::add
void add(const Type &valueToAdd, const Type &weight=pTraits< Type >::one)
Add a value to the distribution, optionally specifying a weight.
Definition: Distribution.C:309
Foam::Distribution::operator=
void operator=(const Distribution< Type > &)
Definition: Distribution.C:557
Foam::Distribution::Distribution
Distribution()
Construct null.
Definition: Distribution.C:36