Simpatico  v1.10
CompositionProfile.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 "CompositionProfile.h"
9 #include <mcMd/simulation/Simulation.h>
10 #include <mcMd/simulation/McMd_mpi.h>
11 #include <mcMd/chemistry/Molecule.h>
12 #include <mcMd/chemistry/Atom.h>
13 #include <simp/boundary/Boundary.h>
14 #include <util/space/Dimension.h>
15 #include <util/misc/FileMaster.h>
16 #include <util/misc/ioUtil.h>
17 #include <util/format/Dbl.h>
18 
19 namespace McMd
20 {
21 
22  using namespace Util;
23  using namespace Simp;
24 
27  : SystemAnalyzer<System>(system),
28  isFirstStep_(true),
29  isInitialized_(false)
30  { setClassName("CompositionProfile"); }
31 
33  {}
34 
36  void CompositionProfile::readParameters(std::istream& in)
37  {
38  readInterval(in);
40 
41  // Read number of direction vectors and direction vectors
42  read<int>(in, "nDirection", nDirection_);
43  intVectors_.allocate(nDirection_);
44  readDArray<IntVector>(in, "intVectors", intVectors_, nDirection_);
45 
46  read<int>(in, "nBins", nBins_);
47 
49  waveVectors_.allocate(nDirection_);
50  accumulators_.allocate(nDirection_*nAtomType_);
51  currentAccumulators_.allocate(nDirection_*nAtomType_);
52  logFiles_.allocate(nDirection_*nAtomType_);
53 
54  isInitialized_ = true;
55  }
56 
57  /*
58  * Load internal state from an archive.
59  */
61  {
63  loadParameter<int>(ar, "nDirection", nDirection_);
64  loadDArray<IntVector>(ar, "intVectors", intVectors_, nDirection_);
65  ar & waveVectors_;
66  ar & accumulators_;
67  ar & nSample_;
68  ar & nAtomType_;
69 
70  if (nAtomType_ != system().simulation().nAtomType()) {
71  UTIL_THROW("Inconsistent values for nAtomType_");
72  }
73  if (nDirection_ != intVectors_.capacity()) {
74  UTIL_THROW("Inconsistent intVectors capacity");
75  }
76  if (nDirection_ != waveVectors_.capacity()) {
77  UTIL_THROW("Inconsistent waveVectors capacity");
78  }
79  if (nDirection_*nAtomType_ != accumulators_.capacity()) {
80  UTIL_THROW("Inconsistent waveVectors capacity");
81  }
82 
83  isInitialized_ = true;
84  }
85 
86  /*
87  * Save internal state to an archive.
88  */
90  {
91  Analyzer::save(ar);
92  ar & nDirection_;
93  ar & intVectors_;
94  ar & waveVectors_;
95  ar & accumulators_;
96  ar & nSample_;
97  ar & nAtomType_;
98  ar & nBins_;
99  ar & isFirstStep_;
100  }
101 
102  /*
103  * Clear accumulators.
104  */
106  {
107  double min, max;
108 
109  for (int i=0; i < nDirection_; ++i) {
110  for (int j=0; j < nAtomType_; ++j) {
111 
112  min = 0.0;
113  max = 1.0;
114 
115  accumulators_[i+j*nDirection_].setParam(min, max, nBins_);
116  currentAccumulators_[i+j*nDirection_].setParam(min, max, nBins_);
117  }
118  }
119 
120  makeWaveVectors();
121 
122  // Clear accumulators
123  for (int i = 0; i < nDirection_; ++i) {
124  for (int j = 0; j < nAtomType_; ++j){
125  accumulators_[i+j*nDirection_].clear();
126  }
127  }
128  nSample_ = 0;
129  }
130 
131  void CompositionProfile::sample(long iStep)
132  {
133  Vector blengths;
134 
135  // Lengths of simulation box
136  blengths = system().boundary().lengths();
137 
138  if (isAtInterval(iStep)) {
139  Vector position;
140  double product;
143  int nSpecies, iSpecies, typeId;
144 
145  // Clear accumulators for the current timestep
146  for (int i = 0; i < nDirection_; ++i) {
147  for (int j = 0; j < nAtomType_; ++j){
148  currentAccumulators_[i+j*nDirection_].clear();
149  }
150  }
151 
152  // Select open mode for output files
153  std::ios_base::openmode mode = std::ios_base::out;
154  if (!isFirstStep_) {
155  mode = std::ios_base::out | std::ios_base::app;
156  }
157 
158  // Open log files
159  for (int i = 0; i < nDirection_; ++i) {
160  for (int j = 0; j < nAtomType_; ++j){
161  std::ostringstream oss;
162  oss << outputFileName();
163 
164  for (int k = 0; k < Dimension; ++k) {
165  oss << "_" << intVectors_[i][k];
166  }
167  oss << "_type" << j << ".log";
168 
169  fileMaster().openOutputFile(oss.str(),
170  logFiles_[i+j*nDirection_],
171  mode);
172  }
173  }
174 
175  // Loop over all atoms
176  nSpecies = system().simulation().nSpecies();
177  for (iSpecies = 0; iSpecies < nSpecies; ++iSpecies) {
178  system().begin(iSpecies, molIter);
179 
180  for ( ; molIter.notEnd(); ++molIter) {
181  molIter->begin(atomIter);
182 
183  for ( ; atomIter.notEnd(); ++atomIter) {
184  position = atomIter->position();
185  typeId = atomIter->typeId();
186  for (int i = 0; i < Dimension; ++i) {
187  position[i] /= blengths[i];
188  }
189  // Loop over direction vectors
190  for (int i = 0; i < nDirection_; ++i) {
191  product = position.dot(waveVectors_[i]);
192  product /= waveVectors_[i].abs();
193  accumulators_[i+typeId*nDirection_].sample(product);
194  currentAccumulators_[i+typeId*nDirection_].sample(product);
195  }
196 
197  }
198 
199  }
200  }
201 
202  ++nSample_;
203 
204  // Output to log files
205  for (int i = 0; i < nDirection_; ++i) {
206  for (int j = 0; j < nAtomType_; ++j){
207  currentAccumulators_[i+j*nDirection_].output(logFiles_[i+j*nDirection_]);
208  logFiles_[i+j*nDirection_] << std::endl;
209  }
210  }
211 
212  // Close log files
213  for (int i = 0; i < nDirection_; ++i) {
214  for (int j = 0; j < nAtomType_; ++j){
215  logFiles_[i+j*nDirection_].close();
216  }
217  }
218 
219  isFirstStep_ = false;
220  }
221 
222  }
223 
228  {
229  Vector dWave;
230  Boundary* boundaryPtr = &system().boundary();
231  int i, j;
232 
233  // Calculate wavevectors
234  for (i = 0; i < nDirection_; ++i) {
236  for (j = 0; j < Dimension; ++j) {
237  dWave = boundaryPtr->reciprocalBasisVector(j);
238  dWave *= intVectors_[i][j];
239  waveVectors_[i] += dWave;
240  //std::cout << waveVectors_[i] << std::endl;
241  }
242  }
243  }
244 
246  {
247  int i, j, k;
248  std::string suffix;
249 
250  // Echo parameters to a log file
253  outputFile_.close();
254 
255  // Output statistical analysis to separate data file
257  for (i = 0; i < nDirection_; ++i) {
258  for (j = 0; j < Dimension; ++j) {
259  outputFile_ << Dbl(waveVectors_[i][j], 5);
260  }
261  outputFile_ << std::endl;
262 
263  for (k = 0; k < nAtomType_; ++k) {
265  outputFile_ << std::endl;
266  }
267  }
268  outputFile_.close();
269 
270 
271  }
272 
273 }
virtual void save(Serializable::OArchive &ar)
Load parameters to archive.
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual void readParameters(std::istream &in)
Read parameters from file.
bool isInitialized_
Has readParam been called?
float product(float a, float b)
Product for float Data.
Definition: product.h:22
void begin(int speciesId, MoleculeIterator &iterator)
Initialize an iterator for molecules of one species in this System.
Definition: System.h:1147
std::ofstream outputFile_
Output file stream.
bool isFirstStep_
True if this is the first step.
double dot(const Vector &v) const
Return dot product of this vector and vector v.
Definition: Vector.h:632
void makeWaveVectors()
Update wavevectors.
const Vector & lengths() const
Get Vector of unit cell lengths by const reference.
An orthorhombic periodic unit cell.
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
static const Vector Zero
Zero Vector = {0.0, 0.0, 0.0}.
Definition: Vector.h:369
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.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
Forward iterator for a PArray.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void sample(long iStep)
Add particle positions to histogram.
Forward const iterator for an Array or a C array.
Saving / output archive for binary ostream.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
#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.
const Vector & reciprocalBasisVector(int i) const
Return reciprocal lattice basis vector i.
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
int nDirection_
Number of directions.
DArray< Distribution > currentAccumulators_
Distribution statistical accumulators.
Template for Analyzer associated with one System.
virtual void output()
Output results to predefined output file.
bool notEnd() const
Is the current pointer not at the end of the array?
Boundary & boundary() const
Get the Boundary by reference.
Definition: System.h:1064
int nSample_
Number of samples thus far.
Saving archive for binary istream.
int nBins_
Number of bins for density profile.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
DArray< Distribution > accumulators_
Distribution statistical accumulators.
DArray< Vector > waveVectors_
Array of direction vectors.
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.
virtual void setup()
Clear accumulators.
const std::string & outputFileName() const
Return outputFileName string.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
DArray< IntVector > intVectors_
Array of Miller index vectors for directions.
int nAtomType() const
Get the number of atom types.
bool notEnd() const
Is this not the end of the array?
int nAtomType_
Number of atom types, copied from Simulation::nAtomType().
CompositionProfile(System &system)
Constructor.