Simpatico  v1.10
EndtoEnd.cpp
1 #ifndef END_TO_END_CPP
2 #define END_TO_END_CPP
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2014, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "EndtoEnd.h"
12 #include <mcMd/simulation/Simulation.h>
13 #include <simp/species/Species.h>
14 #include <mcMd/chemistry/Molecule.h>
15 #include <mcMd/chemistry/Atom.h>
16 #include <simp/boundary/Boundary.h>
17 #include <util/misc/FileMaster.h>
18 
19 
20 namespace McMd
21 {
22 
23  using namespace Util;
24  using namespace Simp;
25 
28  : SystemAnalyzer<System>(system)
29  { setClassName("EndtoEnd"); }
30 
32  void EndtoEnd::readParameters(std::istream& in)
33  {
34 
35  readInterval(in);
37  read<int>(in,"nSamplePerBlock", nSamplePerBlock_);
38 
39  accumulator_.setNSamplePerBlock(nSamplePerBlock_);
40 
41  // If nSamplePerBlock != 0, open an output file for block averages.
42  if (accumulator_.nSamplePerBlock()) {
43  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
44  }
45 
46  read<int>(in, "speciesId", speciesId_);
47  if (speciesId_ < 0) {
48  UTIL_THROW("Negative speciesId");
49  }
50 
51  if (speciesId_ >= system().simulation().nSpecies()) {
52  UTIL_THROW("speciesId > nSpecies");
53  }
54 
55  speciesPtr_ = &system().simulation().species(speciesId_);
56  nAtom_ = speciesPtr_->nAtom();
57 
58  // Allocate an array of separation Vectors
59  positions_.allocate(nAtom_);
60  }
61 
62  /*
63  * Clear accumulator.
64  */
66  { accumulator_.clear(); }
67 
69  void EndtoEnd::sample(long iStep)
70  {
71  if (isAtInterval(iStep)) {
72 
73  Molecule* moleculePtr;
74  Vector r1, r2, dR;
75  double dRSq;
76  int i, j, nMolecule;
77 
78  dRSq = 0.0;
79  nMolecule = system().nMolecule(speciesId_);
80  for (i = 0; i < system().nMolecule(speciesId_); i++) {
81  moleculePtr = &system().molecule(speciesId_, i);
82 
83  // Construct map of molecule with no periodic boundary conditions
84  positions_[0] = moleculePtr->atom(0).position();
85  for (j = 1 ; j < nAtom_; j++) {
86  r1 = moleculePtr->atom(j-1).position();
87  r2 = moleculePtr->atom(j).position();
88  system().boundary().distanceSq(r1, r2, dR);
89  positions_[j] = positions_[j-1];
90  positions_[j] += dR;
91  }
92 
93  dR.subtract(positions_[0], positions_[nAtom_-1]);
94  dRSq += dR.square();
95 
96 
97  }
98  dRSq /= double(nMolecule);
99 
100  accumulator_.sample(dRSq, outputFile_);
101 
102  } // if isAtInterval
103 
104  }
105 
106  /*
107  * Output results to file after simulation is completed.
108  */
110  {
111  // If outputFile_ was used to write block averages, close it.
112  if (accumulator_.nSamplePerBlock()) {
113  outputFile_.close();
114  }
115 
116  // Write parameters to file
117  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
118  ParamComposite::writeParam(outputFile_);
119  outputFile_.close();
120 
121  // Write average to file
122  fileMaster().openOutputFile(outputFileName(".ave"), outputFile_);
123  accumulator_.output(outputFile_);
124  outputFile_.close();
125  }
126 
127 }
128 #endif
void clear()
Clear all accumulators, set to empty initial state.
Definition: Average.cpp:42
A Vector is a Cartesian vector.
Definition: Vector.h:75
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
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 at end of simulation.
Definition: EndtoEnd.cpp:109
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
EndtoEnd(System &system)
Constructor.
Definition: EndtoEnd.cpp:27
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
#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.
virtual void sample(long iStep)
Evaluate squared radii of gyration for all molecules, add to ensemble.
Definition: EndtoEnd.cpp:69
void readInterval(std::istream &in)
Read interval from file, with error checking.
virtual void readParameters(std::istream &in)
Read parameters from file, and allocate data array.
Definition: EndtoEnd.cpp:32
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
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 setup()
Clear accumulator.
Definition: EndtoEnd.cpp:65
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.
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