PSCF v1.5.0
RadialDistribution.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 "RadialDistribution.h"
9#include <util/math/feq.h>
10#include <util/format/Int.h>
11#include <util/format/Dbl.h>
12
13namespace 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 outputIntegral_ = other.outputIntegral_;
49
50 return *this;
51 }
52
53 /*
54 * Read parameters and initialize.
55 */
57 {
58 min_ = 0.0;
59 read<double>(in, "max", max_);
60 read<int>(in, "nBin", nBin_);
61 binWidth_ = (max_-min_)/double(nBin_);
63 clear();
64 }
65
66 /*
67 * Set parameters and initialize.
68 */
70 {
71 min_ = 0.0;
72 max_ = max;
73 nBin_ = nBin;
74 binWidth_ = (max_ - min_)/double(nBin_);
76 clear();
77 }
78
79 /*
80 * Load internal state from archive.
81 */
83 {
84 ar & min_; // Saved by Distribution::serialize, always 0.0
85 loadParameter<double>(ar, "max", max_);
86 loadParameter<int>(ar, "nBin", nBin_);
87 ar & nSample_;
88 ar & nReject_;
89 ar & binWidth_;
90 ar & histogram_;
91 ar & norm_;
92 ar & nSnapshot_;
93 ar & outputIntegral_;
94 if (!feq(binWidth_, (max_-min_)/double(nBin_))) {
95 UTIL_THROW("Inconsistent value for binWidth_");
96 }
97 if (nBin_ != histogram_.capacity()) {
98 UTIL_THROW("Inconsistent histogram capacity");
99 }
100 }
101
102 /*
103 * Save internal state to archive.
104 */
107
108 /*
109 * Zero accumulators (virtual).
110 */
112 {
114 nSnapshot_ = 0;
115 }
116
117 /*
118 * Set the factor used to normalize the RDF before output.
119 */
121 { norm_ = norm; }
122
123 /*
124 * Mark the beginning of a snapshot.
125 */
127 { ++nSnapshot_; }
128
129 /*
130 * Set outputIntegral true/false to enable/disable output of spatial integral.
131 */
133 { outputIntegral_ = outputIntegral; }
134
135 /*
136 * Output final results to file.
137 */
138 void RadialDistribution::output(std::ostream& out)
139 {
140 double r, rho, prefactor, dV, hist, integral;
141 prefactor = 4.0*3.14159265359/3.0;
142 prefactor = prefactor*binWidth_*binWidth_*binWidth_;
143 integral = 0.0;
144 for (int i=0; i < nBin_; ++i) {
145 r = binWidth_*(double(i) + 0.5);
146 dV = prefactor*(3.0*double(i)*double(i + 1) + 1.0);
147 hist = double(histogram_[i])/double(nSnapshot_);
148 rho = hist/(dV*norm_);
149 out << Dbl(r, 18, 8) << Dbl(rho, 18, 8);
150 if (outputIntegral_) {
151 integral += (hist - dV*norm_)/norm_;
152 out << Dbl(integral, 18, 8);
153 }
154 out << std::endl;
155 }
156 }
157
158}
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
Dbl()
Default constructor.
Definition Dbl.cpp:17
int nBin() const
Get the number of bins.
double binWidth_
width of bin = (max_-min_)/nBin_ .
double min_
minimum value.
DArray< long > histogram_
Histogram of occurences, one element per bin.
double max_
maximum value.
int nBin_
number of bins.
Distribution & operator=(const Distribution &other)
Assignment operator.
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.
Distribution (or histogram) of values for particle separations.
void setParam(double max, int nBin)
Set parameters and initialize.
virtual void readParameters(std::istream &in)
Read values of min, max, and nBin from file.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
void setNorm(double norm)
Set the factor used to normalize the RDF before output.
RadialDistribution()
Default constructor.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void setOutputIntegral(bool outputIntegral)
Set true to enable output of spatial integral of g(r).
RadialDistribution & operator=(const RadialDistribution &other)
Assignment.
void output(std::ostream &out)
Output the distribution to file.
void beginSnapshot()
Mark the beginning of a "snapshot" (i.e., a sampled time step).
virtual void clear()
Clear all accumulators.
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
#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.