Simpatico  v1.10
BondLengthDist.cpp
1 /*
2 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
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 "BondLengthDist.h"
9 #include <mcMd/simulation/Simulation.h>
10 #include <mcMd/chemistry/Molecule.h>
11 #include <mcMd/chemistry/Atom.h>
12 #include <simp/species/Species.h>
13 #include <simp/boundary/Boundary.h>
14 #include <util/math/feq.h>
15 #include <util/misc/FileMaster.h>
16 #include <util/archives/Serializable_includes.h>
17 
18 #include <util/global.h>
19 
20 namespace McMd
21 {
22 
23  using namespace Util;
24  using namespace Simp;
25 
28  : SystemAnalyzer<System>(system),
29  min_(0.0),
30  max_(0.0),
31  nBin_(0),
32  isInitialized_(false)
33  { setClassName("BondLengthDist"); }
34 
36  void BondLengthDist::readParameters(std::istream& in)
37  {
38  readInterval(in);
40  read<int>(in, "speciesId", speciesId_);
41  read<double>(in, "min", min_);
42  read<double>(in, "max", max_);
43  read<double>(in, "nBin", nBin_);
44  if (speciesId_ < 0) {
45  UTIL_THROW("Negative speciesId");
46  }
47  //readParamComposite(in, accumulator_);
48  accumulator_.setParam(min_, max_, nBin_);
49  accumulator_.clear();
50  isInitialized_ = true;
51  }
52 
53  /*
54  * Load state from an archive.
55  */
57  {
58  loadInterval(ar);
60  loadParameter<int>(ar, "speciesId", speciesId_);
61  loadParameter<double>(ar, "min", min_);
62  loadParameter<double>(ar, "max", max_);
63  loadParameter<double>(ar, "nBin", nBin_);
64  ar & accumulator_;
65  //loadParamComposite(in, accumulator_);
66 
67  // Validate
68  if (speciesId_ < 0) {
69  UTIL_THROW("Negative speciesId");
70  }
71  if (!feq(accumulator_.min(),min_)) {
72  UTIL_THROW("Inconsistent values of min");
73  }
74  if (!feq(accumulator_.max(), max_)) {
75  UTIL_THROW("Inconsistent values of max");
76  }
77  if (accumulator_.nBin() != nBin_) {
78  UTIL_THROW("Inconsistent values of max");
79  }
80 
81  isInitialized_ = true;
82  }
83 
84  /*
85  * Save state to archive.
86  */
88  { ar & *this; }
89 
90  /*
91  * Clear accumulator.
92  */
94  {
95  if (!isInitialized_) {
96  UTIL_THROW("Object is not initialized");
97  }
98  accumulator_.clear();
99  }
100 
102  void BondLengthDist::sample(long iStep)
103  {
104  if (isAtInterval(iStep)) {
105 
106  System::MoleculeIterator molIter;
107  Molecule::BondIterator bondIter;
108  double lsq, l;
109 
110  for (system().begin(speciesId_, molIter); molIter.notEnd(); ++molIter) {
111  for (molIter->begin(bondIter); bondIter.notEnd(); ++bondIter) {
112  lsq = system().boundary().distanceSq( bondIter->atom(0).position(),
113  bondIter->atom(1).position());
114  l = sqrt(lsq);
115  accumulator_.sample(l);
116  }
117  }
118  }
119  }
120 
121 
124  {
125 
126  // Echo parameters to a log file
127  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
128  writeParam(outputFile_);
129  outputFile_.close();
130 
131  // Output statistical analysis to separate data file
132  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
133  accumulator_.output(outputFile_);
134  outputFile_.close();
135 
136  }
137 
138 }
virtual void save(Serializable::OArchive &ar)
Save state to archive.
double distanceSq(const Vector &r1, const Vector &r2) const
Return square distance between positions r1 and r2.
bool notEnd() const
Is the current pointer not at the end of the array?
Definition: ArrayIterator.h:83
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
Definition: FileMaster.cpp:290
BondLengthDist(System &system)
Constructor.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.
A set of interacting Molecules enclosed by a Boundary.
Definition: System.h:115
System & system()
Return reference to parent system.
virtual void output()
Output results to output file.
File containing preprocessor macros for error handling.
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
virtual void readParameters(std::istream &in)
Read parameters from file.
Saving / output archive for binary ostream.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
void readInterval(std::istream &in)
Read interval from file, with error checking.
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
Definition: accumulators.mod:1
void sample(double value)
Sample a value.
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
Forward iterator for a PArray.
Definition: ArraySet.h:19
Template for Analyzer associated with one System.
void output(std::ostream &out)
Output the distribution to file.
virtual void clear()
Clear (i.e., zero) previously allocated histogram.
Boundary & boundary() const
Get the Boundary by reference.
Definition: System.h:1064
Saving archive for binary istream.
virtual void setup()
Clear accumulator.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
void setClassName(const char *className)
Set class name string.
FileMaster & fileMaster()
Get the FileMaster by reference.
void loadInterval(Serializable::IArchive &ar)
Load interval from archive, with error checking.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
const std::string & outputFileName() const
Return outputFileName string.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name from archive.
void setParam(double min, double max, int nBin)
Set parameters and initialize.
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
void sample(long iStep)
Add particle pairs to BondLengthDist histogram.