Simpatico  v1.10
TypeDistribution.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 "TypeDistribution.h"
9 
10 #include <mcMd/simulation/Simulation.h>
11 #include <mcMd/species/SpeciesMutator.h>
12 #include <simp/species/Species.h>
13 #include <util/format/Dbl.h>
14 #include <util/format/Int.h>
15 #include <util/misc/FileMaster.h>
16 
17 namespace McMd
18 {
19 
20  using namespace Util;
21  using namespace Simp;
22 
23  // Constructor
25  : SystemAnalyzer<McSystem>(system),
26  outputFile_(),
27  distribution_(),
28  nSample_(0),
29  speciesId_(-1),
30  nState_(-1),
31  speciesPtr_(0),
32  mutatorPtr_(0)
33  { setClassName("TypeDistribution"); }
34 
35  /*
36  * Read parameters from file and initialize.
37  */
38  void TypeDistribution::readParameters(std::istream& in)
39  {
40  readInterval(in);
42  read<int>(in, "speciesId", speciesId_);
43 
44  speciesPtr_ = &(system().simulation().species(speciesId_));
45  if (!speciesPtr_->isMutable()) {
46  UTIL_THROW("Error: Species must be mutable");
47  }
48  mutatorPtr_ = &speciesPtr_->mutator();
49  nState_ = mutatorPtr_->nState();
50 
51  // Allocate IntDistribution accumulator
52  distribution_.allocate(nState_);
53  for (int i = 0; i < nState_; ++i) {
54  distribution_[i] = 0;
55  }
56 
57  }
58 
59  /*
60  * Load state from an archive.
61  */
63  {
65  loadParameter<int>(ar, "speciesId", speciesId_);
66  ar & nState_;
67  ar & nSample_;
68  ar & distribution_;
69 
70  if (speciesId_ < 0 || speciesId_ >= system().simulation().nSpecies()) {
71  UTIL_THROW("Invalid speciesId");
72  }
73  speciesPtr_ = &(system().simulation().species(speciesId_));
74  if (!speciesPtr_->isMutable()) {
75  UTIL_THROW("Error: Species must be mutable");
76  }
77  mutatorPtr_ = &speciesPtr_->mutator();
78  if (nState_ != mutatorPtr_->nState()) {
79  UTIL_THROW("Inconsistent values of nState");
80  }
81  }
82 
83  /*
84  * Save state to an archive.
85  */
87  { ar & *this; }
88 
89  /*
90  * Evaluate energy and print.
91  */
92  void TypeDistribution::sample(long iStep)
93  {
94  if (isAtInterval(iStep)) {
95 
96  for (int i = 0; i < nState_; ++i) {
97  distribution_[i] += mutatorPtr_->stateOccupancy(i);
98  }
99  ++nSample_;
100 
101  }
102  }
103 
104  /*
105  * Summary
106  */
108  {
109  // Open and write summary file
110  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
111 
112 
113  double norm = 1.0/double(nSample_);
114  for (int i = 0; i < nState_; ++i) {
115  outputFile_ << Int(i,5)
116  << Dbl(distribution_[i]*norm, 15) << std::endl;
117  }
118  outputFile_.close();
119  }
120 
121 }
virtual void readParameters(std::istream &in)
Read output file and nStepPerSample.
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
int nState() const
Get the number of possible molecule states.
void sample(long iStep)
Calculate, analyze and/or output a physical quantity.
TypeDistribution(McSystem &system)
Constructor.
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.
McSystem & system()
Return reference to parent system.
Wrapper for a double precision number, for formatted ostream output.
Definition: Dbl.h:39
virtual void save(Serializable::OArchive &ar)
Save state to an archive.
Classes used by all simpatico molecular simulations.
void readOutputFileName(std::istream &in)
Read outputFileName from file.
Saving / output archive for binary ostream.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
virtual void output()
Output any results at the end of the simulation.
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
void readInterval(std::istream &in)
Read interval from file, with error checking.
Utility classes for scientific computation.
Definition: accumulators.mod:1
Wrapper for an int, for formatted ostream output.
Definition: Int.h:36
Template for Analyzer associated with one System.
int stateOccupancy(int stateId) const
Get the number of molecules with a specified state.
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
int nSpecies() const
Get the number of Species in this Simulation.
bool isMutable() const
Is this a mutable Species?
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.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
McMd::SpeciesMutator & mutator()
Return the species mutator object by reference.
Species & species(int i)
Get a specific Species by reference.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.