Simpatico  v1.10
RadialDistribution.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include "RadialDistribution.h"
9 #include <util/math/feq.h>
10 #include <util/format/Int.h>
11 #include <util/format/Dbl.h>
12 
13 namespace Util
14 {
15 
16  /*
17  * Default constructor.
18  */
20  : Distribution(),
21  norm_(0.0),
22  nSnapshot_(0),
23  outputIntegral_(false)
24  { setClassName("RadialDistribution"); }
25 
26  /*
27  * Copy constructor.
28  */
30  : Distribution(other),
31  norm_(other.norm_),
32  nSnapshot_(other.nSnapshot_),
33  outputIntegral_(other.outputIntegral_)
34  {}
35 
36  /*
37  * Assignment operator.
38  */
41  {
42  // Call base class assignment operator
44 
45  // Assign the additional members
46  norm_ = other.norm_;
47  nSnapshot_ = other.nSnapshot_;
48 
49  return *this;
50  }
51 
52  /*
53  * Read parameters and initialize.
54  */
55  void RadialDistribution::readParameters(std::istream& in)
56  {
57  min_ = 0.0;
58  read<double>(in, "max", max_);
59  read<int>(in, "nBin", nBin_);
60  binWidth_ = (max_-min_)/double(nBin_);
62  clear();
63  }
64 
65  /*
66  * Set parameters and initialize.
67  */
69  {
70  min_ = 0.0;
71  max_ = max;
72  nBin_ = nBin;
73  binWidth_ = (max_ - min_)/double(nBin_);
75  clear();
76  }
77 
78  /*
79  * Load internal state from archive.
80  */
82  {
83  min_ = 0.0;
84  loadParameter<double>(ar, "max", max_);
85  loadParameter<int>(ar, "nBin", nBin_);
86  ar & nSample_;
87  ar & nReject_;
88  ar & binWidth_;
89  ar & histogram_;
90  ar & norm_;
91  ar & nSnapshot_;
92  ar & outputIntegral_;
93  if (!feq(binWidth_, (max_-min_)/double(nBin_))) {
94  UTIL_THROW("Inconsistent value for binWidth_");
95  }
96  if (nBin_ != histogram_.capacity()) {
97  UTIL_THROW("Inconsistent histogram capacity");
98  }
99  }
100 
101  /*
102  * Save internal state to archive.
103  */
105  { ar & *this; }
106 
107  /*
108  * Zero accumulators (virtual).
109  */
111  {
113  nSnapshot_ = 0;
114  }
115 
116  /*
117  * Set the factor used to normalize the RDF before output.
118  */
119  void RadialDistribution::setNorm(double norm)
120  { norm_ = norm; }
121 
122  /*
123  * Mark the beginning of a snapshot.
124  */
126  { ++nSnapshot_; }
127 
128  /*
129  * Set outputIntegral true/false to enable/disable output of spatial integral.
130  */
131  void RadialDistribution::setOutputIntegral(bool outputIntegral)
132  { outputIntegral_ = outputIntegral; }
133 
134  /*
135  * Output final results to file.
136  */
137  void RadialDistribution::output(std::ostream& out)
138  {
139  double r, rho, prefactor, dV, hist, integral;
140  prefactor = 4.0*3.14159265359/3.0;
141  prefactor = prefactor*binWidth_*binWidth_*binWidth_;
142  integral = 0.0;
143  for (int i=0; i < nBin_; ++i) {
144  r = binWidth_*(double(i) + 0.5);
145  dV = prefactor*double(3*i*i + 3*i + 1);
146  hist = double(histogram_[i])/double(nSnapshot_);
147  rho = hist/(dV*norm_);
148  out << Dbl(r, 18, 8) << Dbl(rho, 18, 8);
149  if (outputIntegral_) {
150  integral += (hist - dV*norm_)/norm_;
151  out << Dbl(integral, 18, 8);
152  }
153  out << std::endl;
154  }
155  }
156 
157 }
void setParam(double max, int nBin)
Set parameters and initialize.
double max_
maximum value.
Definition: Distribution.h:154
void output(std::ostream &out)
Output the distribution to file.
int nBin_
number of bins.
Definition: Distribution.h:156
int nSample_
Number of sampled values in Histogram.
Definition: Distribution.h:157
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
void setOutputIntegral(bool outputIntegral)
Set true to enable output of spatial integral of g(r).
RadialDistribution()
Default constructor.
double min_
minimum value.
Definition: Distribution.h:153
Saving / output archive for binary ostream.
void setNorm(double norm)
Set the factor used to normalize the RDF before output.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
double binWidth_
width of bin = (max_-min_)/nBin_ .
Definition: Distribution.h:155
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void beginSnapshot()
Mark the beginning of a "snapshot" (i.e., a sampled time step).
Utility classes for scientific computation.
Definition: accumulators.mod:1
Distribution & operator=(const Distribution &other)
Assignment operator.
RadialDistribution & operator=(const RadialDistribution &other)
Assignment.
virtual void clear()
Clear (i.e., zero) previously allocated histogram.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
DArray< long > histogram_
Histogram of occurences, one element per bin.
Definition: Distribution.h:152
Saving archive for binary istream.
int nReject_
Number of sampled values that were out of range.
Definition: Distribution.h:158
virtual void readParameters(std::istream &in)
Read values of min, max, and nBin from file.
int nBin() const
Get the number of bins.
Definition: Distribution.h:191
void setClassName(const char *className)
Set class name string.
Distribution (or histogram) of values for particle separations.
virtual void clear()
Clear all accumulators.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
A distribution (or histogram) of values for a real variable.
Definition: Distribution.h:23
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
double max() const
Get maximum value in range of histogram.
Definition: Distribution.h:179