Simpatico  v1.10
SemiGrandDistribution.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 "SemiGrandDistribution.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  /*
24  * Constructor.
25  */
27  : SystemAnalyzer<McSystem>(system),
28  outputFile_(),
29  distribution_(),
30  speciesId_(-1),
31  moleculeCapacity_(-1),
32  speciesPtr_(0),
33  mutatorPtr_(0)
34  { setClassName("SemiGrandDistribution"); }
35 
36  /*
37  * Read parameters from file and initialize.
38  */
40  {
41  readInterval(in);
43  read<int>(in, "speciesId", speciesId_);
44 
45  speciesPtr_ = &(system().simulation().species(speciesId_));
46  moleculeCapacity_ = speciesPtr_->capacity();
47  if (moleculeCapacity_ <= 0) {
48  UTIL_THROW("Error: moleculeCapacity <= 0");
49  }
50  if (!speciesPtr_->isMutable()) {
51  UTIL_THROW("Error: Species must be mutable");
52  }
53  mutatorPtr_ = &speciesPtr_->mutator();
54 
55  // Allocate IntDistribution accumulator
56  distribution_.setParam(0, moleculeCapacity_);
57  distribution_.clear();
58  }
59 
60  /*
61  * Load state from an archive.
62  */
64  {
66  loadParameter<int>(ar, "speciesId", speciesId_);
67  ar & moleculeCapacity_;
68  ar & distribution_;
69 
70  // Validate values and set pointer members.
71  if (speciesId_ < 0 || speciesId_ >= system().simulation().nSpecies()) {
72  UTIL_THROW("Invalid speciesId");
73  }
74  if (moleculeCapacity_ <= 0) {
75  UTIL_THROW("Error: moleculeCapacity <= 0");
76  }
77  speciesPtr_ = &(system().simulation().species(speciesId_));
78  if (moleculeCapacity_ != speciesPtr_->capacity()) {
79  UTIL_THROW("Inconsiste moleculeCapacity");
80  }
81  if (!speciesPtr_->isMutable()) {
82  UTIL_THROW("Error: Species must be mutable");
83  }
84  mutatorPtr_ = &speciesPtr_->mutator();
85  }
86 
87  /*
88  * Save state to an archive.
89  */
91  { ar & *this; }
92 
93 
94  /*
95  * Sample occupation.
96  */
98  {
99  if (isAtInterval(iStep)) {
100  distribution_.sample(mutatorPtr_->stateOccupancy(0));
101  }
102  }
103 
104  /*
105  * Open and write summary output file.
106  */
108  {
109  fileMaster().openOutputFile(outputFileName(".dat"), outputFile_);
110  distribution_.output(outputFile_);
111  outputFile_.close();
112  }
113 
114 }
A System for use in a Markov chain Monte Carlo simulation.
Definition: McSystem.h:52
SemiGrandDistribution(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.
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.
virtual void output()
Output final summary and file format.
void output(std::ostream &out)
Output the distribution to file.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void clear()
Clear (i.e., zero) previously allocated histogram.
void setParam(int min, int max)
Set parameters and initialize.
Simulation & simulation() const
Get the parent Simulation by reference.
Definition: System.h:1055
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 loadParameters(Serializable::IArchive &ar)
Load state from an archive.
Template for Analyzer associated with one System.
int stateOccupancy(int stateId) const
Get the number of molecules with a specified state.
void sample(long iStep)
Evaluate energy and print.
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?
virtual void readParameters(std::istream &in)
Read output file and nStepPerSample.
void setClassName(const char *className)
Set class name string.
int capacity() const
Maximum allowed number of molecules for this Species.
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.
McMd::SpeciesMutator & mutator()
Return the species mutator object by reference.
Species & species(int i)
Get a specific Species by reference.