PSCF v1.5.0
Distribution.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, David Morse (morse012@um.edu)
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Distribution.h"
9#include <util/format/Dbl.h>
10#include <util/global.h>
11
12namespace Util
13{
14
15 /*
16 * Default constructor.
17 */
19 : histogram_(),
20 min_(0.0),
21 max_(0.0),
22 binWidth_(0.0),
23 nBin_(0),
24 nSample_(0),
25 nReject_(0)
26 { setClassName("Distribution"); }
27
28 /*
29 * Copy constructor.
30 */
32 : histogram_(),
33 min_(other.min_),
34 max_(other.max_),
35 binWidth_(other.binWidth_),
36 nBin_(other.nBin_),
37 nSample_(other.nSample_),
38 nReject_(other.nReject_)
39 {
40 if (nBin_ > 0) {
41 assert(other.histogram_.capacity() != 0);
43 for (int i=0; i < nBin_; ++i) {
44 histogram_[i] = other.histogram_[i];
45 }
46 } else {
47 assert(nBin_ == 0);
48 assert(histogram_.capacity() == 0);
49 assert(nSample_ == 0);
50 assert(nReject_ == 0);
51 }
52 }
53
54 /*
55 * Assignment operator.
56 */
58 {
59 // Check for self assignment
60 if (this == &other) return *this;
61
62 // Check validity of other object
63 if (other.nBin_ > 0) {
64 assert(other.histogram_.capacity() != 0);
65 } else {
66 assert(other.nBin_ == 0);
67 assert(other.histogram_.capacity() == 0);
68 assert(other.nSample_ == 0);
69 assert(other.nReject_ == 0);
70 }
71
72 // Assign primitive values
73 min_ = other.min_;
74 max_ = other.max_;
75 binWidth_ = other.binWidth_;
76 nBin_ = other.nBin_;
77 nSample_ = other.nSample_;
78 nReject_ = other.nReject_;
79
80 // Allocate and copy histogram, if necessary
83 }
84 if (nBin_ > 0) {
85 if (!histogram_.isAllocated()) {
87 }
88 for (int i=0; i < nBin_; ++i) {
89 histogram_[i] = other.histogram_[i];
90 }
91 }
92
93 return *this;
94 }
95
96 /*
97 * Destructor.
98 */
101
102 /*
103 * Read parameters and initialize.
104 */
105 void Distribution::readParameters(std::istream& in)
106 {
107 read<double>(in, "min", min_);
108 read<double>(in, "max", max_);
109 read<int>(in, "nBin", nBin_);
110 binWidth_ = (max_ - min_)/double(nBin_);
112 clear();
113 }
114
115 /*
116 * Set parameters and initialize.
117 *
118 * \param min lower bound of range
119 * \param max upper bound of range
120 * \param nBin number of bins in range [min, max]
121 */
122 void Distribution::setParam(double min, double max, int nBin)
123 {
124 min_ = min;
125 max_ = max;
126 nBin_ = nBin;
127 binWidth_ = (max_ - min_)/double(nBin_);
129 clear();
130 }
131
132 /*
133 * Load internal state from archive.
134 */
136 {
137 loadParameter<double>(ar, "min", min_);
138 loadParameter<double>(ar, "max", max_);
139 loadParameter<int>(ar, "nBin", nBin_);
140 ar & nSample_;
141 ar & nReject_;
142 ar & binWidth_;
143 ar & histogram_;
144
145 // Validate
146 if (histogram_.capacity() != nBin_) {
147 UTIL_THROW("Inconsistent histogram capacity");
148 }
149 if (!feq(binWidth_, (max_ - min_)/double(nBin_))) {
150 UTIL_THROW("Inconsistent binWidth_");
151 }
152 }
153
154 /*
155 * Save internal state to archive.
156 */
158 { ar & *this; }
159
160 /*
161 * Zero all accumulators.
162 */
164 {
165 nSample_ = 0;
166 nReject_ = 0;
167 for (int i=0; i < nBin_; ++i) {
168 histogram_[i] = 0;
169 }
170 }
171
172 /*
173 * Add a value to the histogram
174 */
175 void Distribution::sample(double value)
176 {
177 int i;
178 if (value > min_ && value < max_) {
179 i = binIndex(value);
180 histogram_[i] += 1;
181 nSample_ += 1;
182 } else {
183 nReject_ += 1;
184 }
185 }
186
187 /*
188 * Output histogram
189 */
190 void Distribution::output(std::ostream& out)
191 {
192 double x, rho;
193 for (int i=0; i < nBin_; ++i) {
194 x = min_ + binWidth_*(double(i) + 0.5);
195 rho = double(histogram_[i])/double(nSample_);
196 rho = rho/binWidth_;
197 out << Dbl(x, 18, 8) << Dbl(rho, 18, 8) << std::endl;
198 }
199 }
200
201 #ifdef UTIL_MPI
202 /*
203 * Reduce (add) distributions from multiple MPI processors.
204 */
205 void Distribution::reduce(MPI::Intracomm& communicator, int root)
206 {
207
208 long* totHistogram = new long[nBin_];
209 communicator.Reduce(histogram_.cArray(), totHistogram, nBin_, MPI::LONG, MPI::SUM, root);
210 if (communicator.Get_rank() == root) {
211 for (int i=0; i < nBin_; ++i) {
212 histogram_[i] = totHistogram[i];
213 }
214 } else {
215 for (int i=0; i < nBin_; ++i) {
216 histogram_[i] = 0.0;
217 }
218 }
219 delete [] totHistogram;
220
221 long totSample;
222 communicator.Reduce(&nSample_, &totSample, 1, MPI::LONG, MPI::SUM, root);
223 if (communicator.Get_rank() == root) {
224 nSample_ = totSample;
225 } else {
226 nSample_ = 0;
227 }
228
229 long totReject;
230 communicator.Reduce(&nReject_, &totReject, 1, MPI::LONG, MPI::SUM, root);
231 if (communicator.Get_rank() == root) {
232 nReject_ = totReject;
233 } else {
234 nReject_ = 0;
235 }
236
237 }
238 #endif
239
240}
Data * cArray()
Return a pointer to the underlying C array (non-const data).
Definition Array.h:222
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
void deallocate()
Deallocate the underlying C array.
Definition DArray.h:278
Dbl()
Default constructor.
Definition Dbl.cpp:17
int nBin() const
Get the number of bins.
void sample(double value)
Sample a value.
double binWidth_
width of bin = (max_-min_)/nBin_ .
double min_
minimum value.
DArray< long > histogram_
Histogram of occurences, one element per bin.
double min() const
Get minimum value in range of histogram.
void setParam(double min, double max, int nBin)
Set parameters and initialize.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
double max_
maximum value.
int nBin_
number of bins.
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.
Distribution & operator=(const Distribution &other)
Assignment operator.
virtual ~Distribution()
Destructor.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Distribution()
Default constructor.
int nSample_
Number of sampled values in Histogram.
int nReject_
Number of sampled values that were out of range.
virtual void clear()
Clear (i.e., zero) previously allocated histogram.
double max() const
Get maximum value in range of histogram.
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void setClassName(const char *className)
Set class name string.
ScalarParam< Type > & loadParameter(Serializable::IArchive &ar, const char *label, Type &value, bool isRequired)
Add and load a new ScalarParam < Type > object.
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
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.