Simpatico  v1.10
ClusterHistogram.cpp
1 #ifndef MCMD_CLUSTER_HISTOGRAM_CPP
2 #define MCMD_CLUSTER_HISTOGRAM_CPP
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "ClusterHistogram.h"
12 #include <mcMd/simulation/System.h>
13 #include <mcMd/simulation/Simulation.h>
14 #include <mcMd/chemistry/Molecule.h>
15 #include <mcMd/chemistry/Atom.h>
16 
17 #include <simp/boundary/Boundary.h>
18 #include <simp/species/Species.h>
19 
20 #include <util/misc/FileMaster.h>
21 #include <util/archives/Serializable_includes.h>
22 #include <util/format/Int.h>
23 #include <util/format/Dbl.h>
24 #include <util/misc/ioUtil.h>
25 #include <util/space/Tensor.h>
26 #include <util/containers/DArray.h>
27 #include <sstream>
28 
29 namespace McMd
30 {
31 
32  using namespace Util;
33  using namespace Simp;
34 
37  : SystemAnalyzer<System>(system),
38  identifier_(system),
39  hist_(),
40  outputFile_(),
41  speciesId_(),
42  atomTypeId_(),
43  cutoff_(),
44  histMin_(),
45  histMax_(),
46  nSample_(0),
47  isInitialized_(false)
48  { setClassName("ClusterHistogram"); }
49 
51  void ClusterHistogram::readParameters(std::istream& in)
52  {
53  readInterval(in);
55  read<int>(in, "speciesId", speciesId_);
56  if (speciesId_ < 0) {
57  UTIL_THROW("Negative speciesId");
58  }
59  if (speciesId_ >= system().simulation().nSpecies()) {
60  UTIL_THROW("speciesId > nSpecies");
61  }
62 
63  read<int>(in, "atomTypeId", atomTypeId_);
64  if (atomTypeId_ < 0) {
65  UTIL_THROW("Negative atomTypeId");
66  }
67  if (atomTypeId_ >= system().simulation().nAtomType()) {
68  UTIL_THROW("nTypeId >= nAtomType");
69  }
70 
71  read<double>(in, "cutoff", cutoff_);
72  if (cutoff_ < 0) {
73  UTIL_THROW("Negative cutoff");
74  }
75 
76  // Initialize ClusterIdentifier
77  identifier_.initialize(speciesId_, atomTypeId_, cutoff_);
78  read<int>(in,"histMin", histMin_);
79  read<int>(in,"histMax", histMax_);
80  hist_.setParam(histMin_, histMax_);
81  hist_.clear();
82 
83  isInitialized_ = true;
84  }
85 
86  /*
87  * Load state from an archive.
88  */
90  {
91  // Load interval and outputFileName
93 
94  loadParameter<int>(ar,"speciesId", speciesId_);
95  if (speciesId_ < 0) {
96  UTIL_THROW("Negative speciesId");
97  }
98  if (speciesId_ >= system().simulation().nSpecies()) {
99  UTIL_THROW("speciesId > nSpecies");
100  }
101 
102  loadParameter<int>(ar, "atomTypeId", atomTypeId_);
103  if (atomTypeId_ < 0) {
104  UTIL_THROW("Negative atomTypeId");
105  }
106 
107  loadParameter<double>(ar, "cutoff", cutoff_);
108  if (cutoff_ < 0) {
109  UTIL_THROW("Negative cutoff");
110  }
111 
112  identifier_.initialize(speciesId_, atomTypeId_, cutoff_);
113  loadParameter<int>(ar, "histMin", histMin_);
114  loadParameter<int>(ar, "histMax", histMax_);
115  ar >> hist_;
116 
117  ar >> nSample_;
118 
119  isInitialized_ = true;
120  }
121 
122  /*
123  * Save state to archive.
124  */
126  {
127  Analyzer::save(ar);
128  ar & speciesId_;
129  ar & atomTypeId_;
130  ar & cutoff_;
131  ar & histMin_;
132  ar & histMax_;
133  ar & hist_;
134  ar & nSample_;
135  }
136 
137  /*
138  * Clear accumulators.
139  */
141  {
142  if (!isInitialized_) UTIL_THROW("Object is not initialized");
143  hist_.clear();
144  nSample_ = 0;
145  }
146 
147  /*
148  * Sample data by calling ClusterIdentifier::identifyClusters.
149  */
150  void ClusterHistogram::sample(long iStep)
151  {
152  if (isAtInterval(iStep)) {
153  //Identifies all clusters
154  identifier_.identifyClusters();
155  //Adds each cluster to the histogram of all clusters
156  for (int i = 0; i < identifier_.nCluster(); i++) {
157  hist_.sample(identifier_.cluster(i).size());
158  }
159  ++nSample_;
160  fileMaster().openOutputFile(outputFileName(".clusters"+toString(iStep)),outputFile_);
161  //Writes all of the clusters and their component molecules
162  Cluster thisCluster;
163  ClusterLink* thisClusterStart;
164  ClusterLink* next;
165  Molecule thisMolecule;
166  //Loop over each cluster
167  for (int i = 0; i < identifier_.nCluster(); i++) {
168  thisCluster = identifier_.cluster(i);
169  thisClusterStart = thisCluster.head();
170  outputFile_ << i << " " ;
171  //List out every molecule in that cluster
172  while (thisClusterStart) {
173  next = thisClusterStart->next();
174  thisMolecule = thisClusterStart->molecule();
175  outputFile_ << thisMolecule.id() << " ";
176  thisClusterStart = next;
177  }
178  outputFile_ << "\n";
179  }
180  outputFile_.close();
181 
182  //Calculate, store, and write the micelle center of mass
183  fileMaster().openOutputFile(outputFileName(".COMs"+toString(iStep)),outputFile_);
184  //comArray;
185  Vector clusterCOM;
186  Vector r0;
187  Vector dr;
188  Tensor moment;
189  Tensor rgDyad;
190  DArray<Vector> allCOMs;
191  DArray<Tensor> allMoments;
192  allCOMs.allocate(identifier_.nCluster());
193  allMoments.allocate(identifier_.nCluster());
195  for (int i = 0; i < identifier_.nCluster(); i++) {
196  thisCluster = identifier_.cluster(i);
197  outputFile_ << i << " " ;
198  //For that cluster, calculate the center of mass
199  clusterCOM = thisCluster.clusterCOM(atomTypeId_, system().boundary());
200  outputFile_ << clusterCOM;
201  outputFile_ << "\n";
202  allCOMs[i] = clusterCOM;
203  //Calculate Rg
204  moment = thisCluster.momentTensor(atomTypeId_, system().boundary());
205  allMoments[i] = moment;
206  }
207  outputFile_.close();
208  fileMaster().openOutputFile(outputFileName(".momentTensors"+toString(iStep)),outputFile_);
209  for (int i = 0; i < identifier_.nCluster(); i++) {
210  outputFile_ << i << " " << allMoments[i] << "\n";
211 
212  }
213  outputFile_.close();
214  }
215  }
216 
217  /*
218  * Output results to file after simulation is completed.
219  */
221  {
222  // Write parameter file
223  fileMaster().openOutputFile(outputFileName(".prm"), outputFile_);
224  writeParam(outputFile_);
225  outputFile_.close();
226 
227  // Write histogram output
228  fileMaster().openOutputFile(outputFileName(".hist"), outputFile_);
229  // hist_.output(outputFile_);
230  int min = hist_.min();
231  int nBin = hist_.nBin();
232  for (int i = 0; i < nBin; ++i) {
233  outputFile_ << Int(i + min) << " "
234  << Dbl(double(hist_.data()[i])/double(nSample_)) << "\n";
235  }
236  outputFile_.close();
237  }
238 
239 }
240 #endif
virtual void save(Serializable::OArchive &ar)
Load parameters to archive.
std::string toString(int n)
Return string representation of an integer.
Definition: ioUtil.cpp:52
A Vector is a Cartesian vector.
Definition: Vector.h:75
Cluster & cluster(int id)
Get a specific cluster, indexed in the order identified.
int nCluster() const
Get number of clusters.
virtual void initialize(int speciesId, int atomTypeId, double cutoff)
Clear accumulator.
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.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
int id() const
Get the index for this Molecule (unique in species).
A Tensor represents a Cartesian tensor.
Definition: Tensor.h:32
Forward const iterator for an Array or a C array.
Saving / output archive for binary ostream.
virtual void sample(long iStep)
Identify clusters in configuration.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
ClusterLink * head() const
Get a pointer to the first link in the linked list.
Definition: Cluster.h:80
void clear()
Clear (i.e., zero) previously allocated histogram.
void setParam(int min, int max)
Set parameters and initialize.
ClusterHistogram(System &system)
Constructor.
virtual void writeParam(std::ostream &out)
Write all parameters to an output stream.
void sample(int value)
Sample a value.
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void setup()
Clear accumulator.
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
virtual void readParameters(std::istream &in)
Read parameters from file, and allocate data array.
virtual void save(Serializable::OArchive &ar)
Save state to archive.
const DArray< long > & data() const
Get histogram array.
void identifyClusters()
Identify all clusters (main operation).
int nBin() const
Get the number of bins.
Template for Analyzer associated with one System.
Dynamically allocatable contiguous array template.
Definition: DArray.h:31
Saving archive for binary istream.
int size() const
Get the number of molecules or links in the cluster.
Definition: Cluster.h:72
Cluster of molecules.
Definition: Cluster.h:31
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
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.
int min() const
Get minimum value in range of histogram.
const std::string & outputFileName() const
Return outputFileName string.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
Vector clusterCOM(int atomTypeInCluster, Boundary const &boundary)
Return the cluster COM.
Definition: Cluster.cpp:77
A physical molecule (a set of covalently bonded Atoms).
Tensor momentTensor(int atomTypeInCluster, Boundary const &boundary)
Return the cluster radius of gyration tensor.
Definition: Cluster.cpp:111
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.
virtual void output()
Output results at end of simulation.