PSCF v1.1
Distribution.h
1#ifndef UTIL_DISTRIBUTION_H
2#define UTIL_DISTRIBUTION_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/param/ParamComposite.h> // base class
12#include <util/containers/DArray.h> // member template
13#include <util/math/feq.h> // Used in serialize template
14
15namespace Util
16{
17
24 {
25
26 public:
27
32
38 Distribution(const Distribution& other);
39
46
50 virtual ~Distribution();
51
60 virtual void readParameters(std::istream& in);
61
69 void setParam(double min, double max, int nBin);
70
76 virtual void loadParameters(Serializable::IArchive &ar);
77
83 virtual void save(Serializable::OArchive &ar);
84
91 template <class Archive>
92 void serialize(Archive& ar, const unsigned int version);
93
99 void sample(double value);
100
104 virtual void clear();
105
111 void output(std::ostream& out);
112
118 int binIndex(double value) const;
119
123 double min() const;
124
128 double max() const;
129
133 double binWidth() const;
134
138 int nBin() const;
139
140 #ifdef UTIL_MPI
147 void reduce(MPI::Intracomm& communicator, int root);
148 #endif
149
150 protected:
151
153 double min_;
154 double max_;
155 double binWidth_;
156 int nBin_;
159
160 };
161
162 // inline method definitions
163
164 /*
165 * Return the index of the bin for a value.
166 */
167 inline int Distribution::binIndex(double value) const
168 { return int( (value - min_)/binWidth_ ); }
169
170 /*
171 * Get minimum value in range of histogram.
172 */
173 inline double Distribution::min() const
174 { return min_; }
175
176 /*
177 * Get maximum value in range of histogram.
178 */
179 inline double Distribution::max() const
180 { return max_; }
181
182 /*
183 * Get binWidth, the width of each bin.
184 */
185 inline double Distribution::binWidth() const
186 { return binWidth_; }
187
188 /*
189 * Get the number of bins
190 */
191 inline int Distribution::nBin() const
192 { return nBin_; }
193
194 /*
195 * Serialize this Distribution.
196 */
197 template <class Archive>
198 void Distribution::serialize(Archive& ar, const unsigned int version)
199 {
200 ar & min_;
201 ar & max_;
202 ar & nBin_;
203 ar & nSample_;
204 ar & nReject_;
205 ar & binWidth_;
206 ar & histogram_;
207
208 // Validate
209 if (histogram_.capacity() != nBin_) {
210 UTIL_THROW("Inconsistent histogram capacity");
211 }
212 if (!feq(binWidth_, (max_ - min_)/double(nBin_))) {
213 UTIL_THROW("Inconsistent binWidth");
214 }
215 }
216
217}
218#endif
int capacity() const
Return allocated size.
Definition: Array.h:159
Saving archive for binary istream.
Saving / output archive for binary ostream.
Dynamically allocatable contiguous array template.
Definition: DArray.h:32
A distribution (or histogram) of values for a real variable.
Definition: Distribution.h:24
int nBin() const
Get the number of bins.
Definition: Distribution.h:191
void sample(double value)
Sample a value.
double binWidth_
width of bin = (max_-min_)/nBin_ .
Definition: Distribution.h:155
double min_
minimum value.
Definition: Distribution.h:153
double binWidth() const
Get binWidth, the width of each bin.
Definition: Distribution.h:185
DArray< long > histogram_
Histogram of occurences, one element per bin.
Definition: Distribution.h:152
double min() const
Get minimum value in range of histogram.
Definition: Distribution.h:173
void setParam(double min, double max, int nBin)
Set parameters and initialize.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void reduce(MPI::Intracomm &communicator, int root)
Reduce (add) distributions from multiple MPI processors.
double max_
maximum value.
Definition: Distribution.h:154
int nBin_
number of bins.
Definition: Distribution.h:156
virtual void readParameters(std::istream &in)
Read parameters from file and initialize.
void output(std::ostream &out)
Output the distribution to file.
int binIndex(double value) const
Return the index of the bin for a value.
Definition: Distribution.h:167
Distribution & operator=(const Distribution &other)
Assignment operator.
virtual ~Distribution()
Destructor.
void serialize(Archive &ar, const unsigned int version)
Serialize this Distribution to/from an archive.
Definition: Distribution.h:198
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Distribution()
Default constructor.
int nSample_
Number of sampled values in Histogram.
Definition: Distribution.h:157
int nReject_
Number of sampled values that were out of range.
Definition: Distribution.h:158
virtual void clear()
Clear (i.e., zero) previously allocated histogram.
double max() const
Get maximum value in range of histogram.
Definition: Distribution.h:179
An object that can read multiple parameters from file.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
bool feq(double x, double y, double eps=1.0E-10)
Are two floating point numbers equal to within round-off error?
Definition: feq.h:27
Utility classes for scientific computation.
Definition: accumulators.mod:1