Simpatico  v1.10
SpeciesMutator.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 "SpeciesMutator.h"
9 #include <iostream>
10 
11 namespace McMd
12 {
13 
14  using namespace Util;
15 
16  /*
17  * Constructor.
18  */
20  : stateIdLabel_("stateId"),
21  nState_(0)
22  {}
23 
24  /*
25  * Destructor.
26  */
28  {}
29 
30  /*
31  *
32  */
34  {
35  // Allocate arrays
36  stateWeights_.allocate(nState);
37  stateOccupancies_.allocate(nState);
38  moleculeStateIds_.allocate(nMolecule);
39  nState_ = nState;
40 
41  // Initialize array elements
42  for (int i = 0; i < nState; ++i) {
43  stateWeights_[i] = 1.0;
44  }
45  for (int i = 0; i < nState; ++i) {
46  stateOccupancies_[i] = 0;
47  }
48  for (int i = 0; i < nMolecule; ++i) {
49  moleculeStateIds_[i] = NullStateId;
50  }
51 
52  }
53 
54  /*
55  * Set the state id for a molecule and update occupancy histogram.
56  */
57  void SpeciesMutator::setMoleculeStateId(const Molecule& molecule, int stateId)
58  {
59  if (stateId < 0 && stateId != NullStateId) {
60  UTIL_THROW("Invalid state index");
61  }
62  int molId = molecule.id();
63  int oldStateId = moleculeStateIds_[molId];
64  if (oldStateId != NullStateId) {
65  --stateOccupancies_[oldStateId];
66  }
67  moleculeStateIds_[molId] = stateId;
68  ++stateOccupancies_[stateId];
69  }
70 
71 
72  /*
73  * Write state index for a molecule to output stream.
74  */
75  void SpeciesMutator::writeMoleculeState(std::ostream& out,
76  const Molecule& molecule) const
77  {
78  out << "stateId "<< moleculeStateId(molecule) << std::endl;
79  }
80 
81  /*
82  * Read state index for a molecule to output stream, and set molecule state.
83  */
84  void SpeciesMutator::readMoleculeState(std::istream& in, Molecule& molecule)
85  {
86  int stateId;
87  in >> stateIdLabel_ >> stateId;
88  setMoleculeState(molecule, stateId);
89  }
90 
91 }
virtual ~SpeciesMutator()
Destructor.
int nState() const
Get the number of possible molecule states.
SpeciesMutator()
Constructor.
void allocateSpeciesMutator(int nMolecule, int nState)
Allocate arrays of molecule state ids and statistical weights.
static const int NullStateId
Null value for a state index.
void setMoleculeStateId(const Molecule &molecule, int stateId)
Set the state id of a specific molecule.
int moleculeStateId(const Molecule &molecule) const
Get the state id for a specific molecule.
int id() const
Get the index for this Molecule (unique in species).
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void readMoleculeState(std::istream &in, Molecule &molecule)
Read the state id for one molecule from a configuration file stream.
virtual void setMoleculeState(Molecule &molecule, int stateId)=0
Change the state of a specific molecule.
virtual void writeMoleculeState(std::ostream &out, const Molecule &molecule) const
Write the state id for one molecule to a configuration file stream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
A physical molecule (a set of covalently bonded Atoms).