Simpatico  v1.10
RadiusGyration.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 "RadiusGyration.h"
9 #include <mcMd/simulation/Simulation.h>
10 #include <mcMd/chemistry/Molecule.h>
11 #include <mcMd/chemistry/Atom.h>
12 #include <simp/boundary/Boundary.h>
13 #include <simp/species/Species.h>
14 #include <util/misc/FileMaster.h>
15 #include <util/archives/Serializable_includes.h>
16 
17 namespace McMd
18 {
19 
20  using namespace Util;
21  using namespace Simp;
22 
23  /*
24  * Constructor.
25  */
27  : SystemAnalyzer<System>(system),
28  isInitialized_(false)
29  { setClassName("RadiusGyration"); }
30 
31  /*
32  * Read parameters, allocate memory, and initialize accumulator.
33  */
34  void RadiusGyration::readParameters(std::istream& in)
35  {
36  readInterval(in);
38  read<int>(in,"nSamplePerBlock", nSamplePerBlock_);
39  read<int>(in, "speciesId", speciesId_);
40 
41  if (speciesId_ < 0) {
42  UTIL_THROW("Negative speciesId");
43  }
44  if (speciesId_ >= system().simulation().nSpecies()) {
45  UTIL_THROW("speciesId > nSpecies");
46  }
47  speciesPtr_ = &system().simulation().species(speciesId_);
48  nAtom_ = speciesPtr_->nAtom();
49 
50  positions_.allocate(nAtom_);
51  accumulator_.setNSamplePerBlock(nSamplePerBlock_);
52  accumulator_.clear();
53 
54  // Open output file for block averages, if nSamplePerBlock != 0.
55  if (accumulator_.nSamplePerBlock()) {
56  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
57  }
58  isInitialized_ = true;
59  }
60 
61  /*
62  * Load state from an archive.
63  */
65  {
67  loadParameter<int>(ar,"nSamplePerBlock", nSamplePerBlock_);
68  loadParameter<int>(ar, "speciesId", speciesId_);
69  ar & nAtom_;
70 
71  // Validate
72  if (speciesId_ < 0) {
73  UTIL_THROW("Negative speciesId");
74  }
75  if (speciesId_ >= system().simulation().nSpecies()) {
76  UTIL_THROW("speciesId > nSpecies");
77  }
78  speciesPtr_ = &system().simulation().species(speciesId_);
79  if (nAtom_ != speciesPtr_->nAtom()) {
80  UTIL_THROW("Inconsistent values for nAtom");
81  }
82 
83  ar & accumulator_;
84  ar & positions_;
85 
86  // Open output file for block averages, if nSamplePerBlock != 0.
87  if (nSamplePerBlock_) {
88  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
89  }
90  isInitialized_ = true;
91  }
92 
93  /*
94  * Save state to archive.
95  */
97  { ar & *this; }
98 
99  /*
100  * Clear accumulator (public method).
101  */
103  {
104  if (!isInitialized_) {
105  UTIL_THROW("Object is not initialized");
106  }
107  accumulator_.clear();
108  }
109 
110  /*
111  * Evaluate squared radii of gyration of all chains, add to ensemble.
112  */
113  void RadiusGyration::sample(long iStep)
114  {
115  if (isAtInterval(iStep)) {
116  Molecule* moleculePtr;
117  Vector r1, r2, dR, Rcm;
118  double dRSq;
119  int i, j, nMolecule;
120 
121  dRSq = 0.0;
122  nMolecule = system().nMolecule(speciesId_);
123  for (i = 0; i < system().nMolecule(speciesId_); i++) {
124  moleculePtr = &system().molecule(speciesId_, i);
125 
126  // Construct unwrapped map of molecule (no periodic b.c.'s)
127  positions_[0] = moleculePtr->atom(0).position();
128  Rcm = positions_[0];
129  for (j = 1 ; j < nAtom_; j++) {
130  r1 = moleculePtr->atom(j-1).position();
131  r2 = moleculePtr->atom(j).position();
132  system().boundary().distanceSq(r1, r2, dR);
133  positions_[j] = positions_[j-1];
134  positions_[j] += dR;
135  Rcm += positions_[j];
136  }
137  Rcm /= double(nAtom_);
138 
139  // Calculate dRSq
140  for (j = 0 ; j < nAtom_; j++) {
141  dR.subtract(positions_[j], Rcm);
142  dRSq += dR.square();
143  }
144  }
145  dRSq /= double(nMolecule);
146  dRSq /= double(nAtom_);
147  accumulator_.sample(dRSq, outputFile_);
148  }
149  }
150 
151  /*
152  * Output final results to file, after simulation is completed.
153  */
155  {
156  // If outputFile_ was used to write block averages, close it.
157  if (accumulator_.nSamplePerBlock()) {
158  outputFile_.close();
159  }
160 
161  // Write parameters to file
162  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
163  ParamComposite::writeParam(outputFile_);
164  outputFile_.close();
165 
166  // Write average to file
167  fileMaster().openOutputFile(outputFileName(".ave"), outputFile_);
168  accumulator_.output(outputFile_);
169  outputFile_.close();
170  }
171 
172 }
void clear()
Clear all accumulators, set to empty initial state.
Definition: Average.cpp:42
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual void loadParameters(Serializable::IArchive &ar)
Load state to an archive.
int nAtom() const
Get number of atoms per molecule for this Species.
double distanceSq(const Vector &r1, const Vector &r2) const
Return square distance between positions r1 and r2.
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
virtual void loadParameters(Serializable::IArchive &ar)
Load parameters from archive.
A set of interacting Molecules enclosed by a Boundary.
Definition: System.h:115
System & system()
Return reference to parent system.
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
RadiusGyration(System &system)
Constructor.
Saving / output archive for binary ostream.
Molecule & molecule(int speciesId, int moleculeId)
Get a specific Molecule in this System, by integer index.
Definition: System.h:1137
int nMolecule(int speciesId) const
Get the number of molecules of one Species in this System.
Definition: System.h:1128
virtual void setup()
Clear accumulator.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
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.
Utility classes for scientific computation.
Definition: accumulators.mod:1
Template for Analyzer associated with one System.
void output(std::ostream &out) const
Output final statistical properties to file.
Definition: Average.cpp:178
void setNSamplePerBlock(int nSamplePerBlock)
Set nSamplePerBlock.
Definition: Average.cpp:63
Boundary & boundary() const
Get the Boundary by reference.
Definition: System.h:1064
Saving archive for binary istream.
void sample(double value)
Add a sampled value to the ensemble.
Definition: Average.cpp:94
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSamplePerBlock() const
Get number of samples per block average.
Definition: Average.h:220
Vector & subtract(const Vector &v1, const Vector &v2)
Subtract vector v2 from v1.
Definition: Vector.h:672
virtual void readParameters(std::istream &in)
Read parameters from file, and allocate data array.
void setClassName(const char *className)
Set class name string.
const Atom & atom(int localId) const
Get a specific Atom in this Molecule.
FileMaster & fileMaster()
Get the FileMaster by reference.
virtual void save(Serializable::OArchive &ar)
Save state to archive.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
const std::string & outputFileName() const
Return outputFileName string.
A physical molecule (a set of covalently bonded Atoms).
const Vector & position() const
Get the position Vector by const reference.
Species & species(int i)
Get a specific Species by reference.
double square() const
Return square magnitude of this vector.
Definition: Vector.h:616
virtual void output()
Output results at end of simulation.
virtual void sample(long iStep)
Evaluate squared radii of gyration for all molecules, add to ensemble.