Simpatico  v1.10
OrderParamNucleation.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 "OrderParamNucleation.h"
9 #include <ddMd/simulation/Simulation.h>
10 #include <ddMd/simulation/SimulationAccess.h>
11 #include <ddMd/storage/AtomStorage.h>
12 #include <ddMd/storage/AtomIterator.h>
13 #include <ddMd/communicate/Exchanger.h>
14 #include <simp/boundary/Boundary.h>
15 #include <util/math/Constants.h>
16 #include <util/misc/ioUtil.h>
17 #include <util/format/Int.h>
18 #include <util/format/Dbl.h>
19 
20 namespace DdMd
21 {
22 
23  using namespace Util;
24  using namespace Simp;
25 
28  : Analyzer(simulation),
29  isInitialized_(false)
30  { setClassName("OrderParamNucleation"); }
31 
33  {}
34 
36  void OrderParamNucleation::readParameters(std::istream& in)
37  {
39 
40  readInterval(in);
42  read<int>(in, "perpIndex", perpIndex_);
43  if (perpIndex_ < 0 || perpIndex_ >= Dimension) {
44  UTIL_THROW("Invalid index for ordering direction.");
45  }
46  read<int>(in, "parallelIndex", parallelIndex_);
47  if (parallelIndex_ < 0 || parallelIndex_ >= Dimension) {
48  UTIL_THROW("Invalid index for parallel direction.");
49  }
50  read<int>(in, "periodicity", periodicity_);
51  read<int>(in, "nBin", nBin_);
52 
55 
56  isInitialized_ = true;
57  }
58 
59  /*
60  * Load internal state from an archive.
61  */
63  {
65 
66  // Load and broadcast parameter file parameters
67  loadInterval(ar);
69  loadParameter<int>(ar, "perpIndex", perpIndex_);
70  if (perpIndex_ < 0 || perpIndex_ >= Dimension) {
71  UTIL_THROW("Invalid index for ordering direction.");
72  }
73  loadParameter<int>(ar, "parallelIndex", parallelIndex_);
74  if (parallelIndex_ < 0 || parallelIndex_ >= Dimension) {
75  UTIL_THROW("Invalid index for parallel direction.");
76  }
77  loadParameter<int>(ar, "periodicity", periodicity_);
78  loadParameter<int>(ar, "nBin", nBin_);
79 
82 
83  isInitialized_ = true;
84  }
85 
86  /*
87  * Save internal state to an archive.
88  */
90  {
91  saveInterval(ar);
93  ar << perpIndex_;
94  ar << parallelIndex_;
95  ar << periodicity_;
96  ar << nBin_;
97  }
98 
99  /*
100  * Clear accumulators.
101  */
103  {
104  if (!isInitialized_) {
105  UTIL_THROW("Error: object is not initialized");
106  }
107  nSample_ = 0;
108 
109  int i, j;
110  for (i = 0; i < nAtomType_; ++i) {
111  for (j = 0; j < nBin_; ++j) {
112  cosFactors_(i, j) = 0.0;
113  totalCosFactors_(i, j) = 0.0;
114  }
115  }
116 
117  }
118 
119  /*
120  * Increment cos factor.
121  */
123  {
124  if (isAtInterval(iStep)) {
125 
126  Vector blengths;
127  Boundary* boundaryPtr = &simulation().boundary();
128  blengths = boundaryPtr->lengths();
129 
130  Vector position;
131  double slabLength, product, cosFactor;
132  AtomIterator atomIter;
133  int typeId, bin;
134 
135  simulation().atomStorage().begin(atomIter);
136  for ( ; atomIter.notEnd(); ++atomIter) {
137  position = atomIter->position();
138  typeId = atomIter->typeId();
139 
140  slabLength = blengths[parallelIndex_]/nBin_;
141  bin = position[parallelIndex_]/slabLength;
142 
143  product = position[perpIndex_]*2.0*Constants::Pi*periodicity_;
144  product /= blengths[perpIndex_];
145  cosFactor = cos(product)*cos(product);
146  cosFactors_(typeId, bin) += cosFactor;
147  }
148 
149  #ifdef UTIL_MPI
150  // Loop over wavevectors
151  for (int i = 0; i < nAtomType_; ++i) {
152  for (int j = 0; j < nBin_; ++j) {
153  //Sum values from all processors.
155  Reduce(&cosFactors_(i, j), &totalCosFactors_(i, j),
156  1, MPI::DOUBLE, MPI::SUM, 0);
157  }
158  }
159  #else
160  for (int i = 0; i < nAtomType_; ++i) {
161  for (int j = 0; j < nBin_; ++j) {
162  totalCosFactors_(i, j) = cosFactors_(i, j);
163  }
164  }
165  #endif
166 
167  ++nSample_;
168  }
169 
170  }
171 
172  /*
173  * Write data to three output files.
174  */
176  {
177  if (simulation().domain().isMaster()) {
178 
179  // Write parameters to a *.prm file
181  outputFile_);
183  outputFile_.close();
184 
185  // Output structure factors to one *.dat file
187  outputFile_);
188  double volume, value;
189  int i, j;
190  volume = simulation().boundary().volume();
191  for (i = 0; i < nBin_; ++i) {
192  outputFile_ << Int(i, 4);
193  for (j = 0; j < nAtomType_; ++j) {
194  value = totalCosFactors_(j, i)/volume/double(nSample_);
195  outputFile_ << Dbl(value, 18, 8);
196  }
197  outputFile_ << std::endl;
198  }
199  outputFile_.close();
200  }
201 
202  }
203 
204 }
DMatrix< double > totalCosFactors_
Total cos factors of size nAtomType*nBin (gathered from all processors)
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
Abstract base for periodic output and/or analysis actions.
Simulation & simulation()
Get the parent Simulation by reference.
A Vector is a Cartesian vector.
Definition: Vector.h:75
virtual void output()
Output results to predefined output file.
float product(float a, float b)
Product for float Data.
Definition: product.h:22
void readOutputFileName(std::istream &in)
Read outputFileName from file.
void saveInterval(Serializable::OArchive &ar)
Save interval parameter to an archive.
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
Definition: DMatrix.h:170
AtomStorage & atomStorage()
Get the AtomStorage by reference.
double volume() const
Return unit cell volume.
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
void readInterval(std::istream &in)
Read parameter interval from file.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Parallel domain decomposition (DD) MD simulation.
Classes used by all simpatico molecular simulations.
Main object for a domain-decomposition MD simulation.
MPI::Intracomm & communicator() const
Return Cartesian communicator by reference.
Definition: Domain.h:257
DMatrix< double > cosFactors_
Cos factors of size nAtomType*nBin.
Saving / output archive for binary ostream.
void loadOutputFileName(Serializable::IArchive &ar)
Load output file name to an archive.
FileMaster & fileMaster()
Get the associated FileMaster by reference.
virtual void readParameters(std::istream &in)
Read parameters from file.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
OrderParamNucleation(Simulation &simulation)
Constructor.
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
virtual void clear()
Clear accumulators.
bool isAtInterval(long counter) const
Return true iff counter is a multiple of the interval.
bool notEnd() const
Is the current pointer not at the end of the PArray?
Utility classes for scientific computation.
Definition: accumulators.mod:1
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
int nAtomType_
Number of atom types, copied from Simulation::nAtomType().
const std::string & outputFileName() const
Return outputFileName string.
int parallelIndex_
Index of direction parallel to the lamellar surfaces.
int perpIndex_
Index of direction perpendicular to lamellae (direction of external field)
Saving archive for binary istream.
std::ofstream outputFile_
Output file stream.
void sample(long iStep)
Add particles to OrderParamNucleation accumulators.
static const double Pi
Trigonometric constant Pi.
Definition: Constants.h:35
void setClassName(const char *className)
Set class name string.
void loadInterval(Serializable::IArchive &ar)
Load parameter interval from input archive.
int nAtomType()
Get maximum number of atom types.
int nSample_
Number of samples thus far.
Boundary & boundary()
Get the Boundary by reference.
void saveOutputFileName(Serializable::OArchive &ar)
Save output file name to an archive.
int nBin_
Number of bins to divide the length along parallel direction.
Domain & domain()
Get the Domain by reference.
Iterator for all atoms owned by an AtomStorage.
Definition: AtomIterator.h:24
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
void begin(AtomIterator &iterator)
Set iterator to beginning of the set of atoms.
bool isInitialized_
Has readParam been called?
int periodicity_
Number of periods in the perpendicular direction.