Histogram.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 | Copyright (C) 2011-2015 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 "Histogram.H"
27 #include "ListOps.H"
28 
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class List>
33 void Foam::Histogram<List>::count(const List& bins, const List& l)
34 {
35  if (bins.size() < 2)
36  {
38  << "Should have at least two values in bins. Now:" << bins
39  << exit(FatalError);
40  }
41 
42  counts_.setSize(bins.size()-1);
43  counts_ = 0;
44 
45  nLow_ = 0;
46  nHigh_ = 0;
47 
48  forAll(l, i)
49  {
50  label index = findLower(bins, l[i]);
51 
52  if (index == -1)
53  {
54  nLow_++;
55  }
56  else if (index == bins.size()-1)
57  {
58  nHigh_++;
59  }
60  else
61  {
62  counts_[index]++;
63  }
64  }
65 }
66 
67 
68 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
69 
70 template<class List>
72 {
73  count(bins, l);
74 }
75 
76 
77 template<class List>
79 (
80  const typename List::const_reference min,
81  const typename List::const_reference max,
82  const label nBins,
83  const List& l
84 )
85 {
86  List bins(nBins+1);
87 
88  typename List::value_type span = (max-min) / nBins;
89 
90  bins[0] = min;
91 
92  for (label i = 1; i < nBins; i++)
93  {
94  bins[i] = bins[i-1] + span;
95  }
96 
97  // Set max directly to avoid truncation errors.
98  bins[nBins] = max;
99 
100  count(bins, l);
101 }
102 
103 
104 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Calculates the counts per bin of a list.
Definition: Histogram.H:50
error FatalError
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
Various functions to operate on Lists.
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
const T & const_reference
Type that can be used for storing into.
Definition: UList.H:259
label findLower(const ListType &, typename ListType::const_reference, const label stary, const BinaryOp &bop)
Find last element < given value in sorted list and return index,.
T value_type
Type of values the UList contains.
Definition: UList.H:251