Simpatico  v1.10
McPairEnergyAverage.cpp
1 #ifndef SIMP_NOPAIR
2 /*
3 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
4 *
5 * Copyright 2010 - 2017, The Regents of the University of Minnesota
6 * Distributed under the terms of the GNU General Public License.
7 */
8 
9 #include "McPairEnergyAverage.h" // class header
10 
11 #include <util/misc/FileMaster.h>
12 #include <mcMd/chemistry/Molecule.h>
13 #include <mcMd/chemistry/Atom.h>
14 #include <mcMd/potentials/pair/McPairPotential.h>
15 #include <util/archives/Serializable_includes.h>
16 
17 
18 #include <cstdio>
19 
20 namespace McMd
21 {
22 
23  using namespace Util;
24 
25  /*
26  * Constructor.
27  */
29  : SystemAnalyzer<McSystem>(system),
30  outputFile_(),
31  accumulator_(),
32  nSamplePerBlock_(1),
33  isInitialized_(false)
34  {
35  setClassName("McPairEnergyAverage");
36  selector_.setAvoidDoubleCounting(true);
37  }
38 
39 
40  /*
41  * Read parameters and initialize.
42  */
43  void McPairEnergyAverage::readParameters(std::istream& in)
44  {
45  readInterval(in);
47  read<int>(in,"nSamplePerBlock", nSamplePerBlock_);
48  read<PairSelector>(in,"selector", selector_);
49 
50  accumulator_.setNSamplePerBlock(nSamplePerBlock_);
51 
52  // If nSamplePerBlock != 0, open an output file for block averages.
53  if (nSamplePerBlock_) {
54  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
55  }
56  isInitialized_ = true;
57  }
58 
59  /*
60  * Load state from an archive.
61  */
63  {
65  loadParameter<int>(ar,"nSamplePerBlock", nSamplePerBlock_);
66  loadParameter<PairSelector>(ar,"selector", selector_);
67  ar & accumulator_;
68 
69  if (nSamplePerBlock_ != accumulator_.nSamplePerBlock()) {
70  UTIL_THROW("Inconsistent values of nSamplePerBlock");
71  }
72 
73  // If nSamplePerBlock != 0, open an output file for block averages.
74  if (nSamplePerBlock_) {
75  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
76  }
77  isInitialized_ = true;
78  }
79 
80  /*
81  * Save state to archive.
82  */
84  { ar & *this; }
85 
86  /*
87  * Clear accumulator.
88  */
90  { accumulator_.clear(); }
91 
92  /*
93  * Evaluate energy per particle, and add to ensemble.
94  */
95  void McPairEnergyAverage::sample(long iStep)
96  {
97 
98  if (!isAtInterval(iStep)) return;
99 
100  Atom *iAtomPtr, *jAtomPtr;
101  int nNeighbor, nInCell;
102  int ic, nc, i, j;
103  double energy;
104  double rsq;
105 
106  // Loop over cells
107  energy = 0.0;
108  nc = system().pairPotential().cellList().totCells();
109  for (ic = 0; ic < nc; ++ic) {
110 
111  // Get array of neighbors_
112  system().pairPotential().cellList().getCellNeighbors(ic, neighbors_, nInCell);
113  nNeighbor = neighbors_.size();
114 
115  // Loop over primary atoms in this cell
116  for (i = 0; i < nInCell; ++i) {
117  iAtomPtr = neighbors_[i];
118 
119  // Loop over secondary atoms in this and neighboring cells
120  for (j = 0; j < nNeighbor; ++j) {
121  jAtomPtr = neighbors_[j];
122 
123  if (selector_.match(*iAtomPtr, *jAtomPtr)) {
124 
125  // Exclude masked pairs
126  if (!iAtomPtr->mask().isMasked(*jAtomPtr)) {
127 
128  rsq = system().boundary().
129  distanceSq(iAtomPtr->position(), jAtomPtr->position());
130  energy += system().pairPotential().
131  energy(rsq, iAtomPtr->typeId(), jAtomPtr->typeId());
132 
133  }
134 
135  }
136 
137  } // secondary atoms
138 
139  } // primary atoms
140 
141  } // cells
142 
143  accumulator_.sample(energy, outputFile_);
144  }
145 
146  /*
147  * Output results to file after simulation is completed.
148  */
150  {
151  // If outputFile_ was used to write block averages, close it.
152  if (accumulator_.nSamplePerBlock()) {
153  outputFile_.close();
154  }
155 
156  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
157  writeParam(outputFile_);
158  outputFile_.close();
159 
160  fileMaster().openOutputFile(outputFileName(".ave"), outputFile_);
161  accumulator_.output(outputFile_);
162  outputFile_.close();
163 
164  }
165 
166 }
167 #endif
void clear()
Clear all accumulators, set to empty initial state.
Definition: Average.cpp:42
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
virtual void output()
Output results at end of simulation.
const CellList & cellList() const
Get the cellList by const reference.
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.
Mask & mask()
Get the associated Mask by reference.
McSystem & system()
Return reference to parent system.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
Saving / output archive for binary ostream.
bool isMasked(const Atom &atom) const
True if the atom is in the masked set for the target Atom.
bool match(const Atom &atom1, const Atom &atom2) const
Return true if pair of atoms matches the selector policy.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
int typeId() const
Get type index for this Atom.
void getCellNeighbors(int ic, NeighborArray &neighbors, int &nInCell) const
Fill an array with pointers to atoms in a cell and neighboring cells.
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
virtual void save(Serializable::OArchive &ar)
Save state to an archive.
void readInterval(std::istream &in)
Read interval from file, with error checking.
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void sample(long iStep)
Calculate, analyze and/or output a physical quantity.
McPairEnergyAverage(McSystem &system)
Constructor.
int totCells() const
Get total number of cells in this CellList.
virtual void readParameters(std::istream &in)
Read parameters and initialize.
void setAvoidDoubleCounting(bool avoidDoubleCounting)
Set policy to avoid double counting (true) or to not avoid (false).
Template for Analyzer associated with one System.
void output(std::ostream &out) const
Output final statistical properties to file.
Definition: Average.cpp:178
McPairPotential & pairPotential() const
Return the McPairPotential by reference.
Definition: McSystem.h:388
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
void setClassName(const char *className)
Set class name string.
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.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.
const Vector & position() const
Get the position Vector by const reference.
int size() const
Return logical size of this array (i.e., number of elements).
Definition: FSArray.h:207
virtual void setup()
Clear accumulator.