Simpatico  v1.10
McMoveManager.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 <util/global.h>
9 #include <mcMd/mcMoves/McMoveManager.h>
10 #include <mcMd/mcMoves/McMoveFactory.h>
11 #include <mcMd/mcSimulation/McSimulation.h>
12 
13 #include <util/random/Random.h>
14 
15 namespace McMd
16 {
17 
18  using namespace Util;
19 
20  // Constructor.
22  : Manager<McMove>(),
23  simulationPtr_(&simulation),
24  systemPtr_(&simulation.system()),
25  randomPtr_(&simulation.random())
26  { setClassName("McMoveManager"); }
27 
28  // Destructor
30  {}
31 
35  Factory<McMove>* McMoveManager::newDefaultFactory() const
36  { return new McMoveFactory(*simulationPtr_, *systemPtr_); }
37 
38  /*
39  * Read instructions for creating objects from file.
40  */
41  void McMoveManager::readParameters(std::istream &in)
42  {
44 
45  // Allocate and store probabilities
46  probabilities_.allocate(size());
47  double totalProbability = 0.0;
48  int iMove;
49  for (iMove = 0; iMove < size(); ++iMove) {
50  probabilities_[iMove] = (*this)[iMove].probability();
51  totalProbability += probabilities_[iMove];
52  }
53 
54  // Allocate and store and normalize probabilities
55  for (iMove = 0; iMove < size(); ++iMove) {
56  probabilities_[iMove] = probabilities_[iMove]/totalProbability;
57  (*this)[iMove].setProbability(probabilities_[iMove]);
58  }
59  }
60 
61  /*
62  * Load internal state from an archive.
63  */
65  {
67  ar & probabilities_;
68  }
69 
70  /*
71  * Load internal state from an archive.
72  */
74  {
76  ar & probabilities_;
77  }
78 
79  /*
80  * Initialize all moves just prior to a run.
81  */
83  {
84  for (int iMove = 0; iMove < size(); ++iMove) {
85  (*this)[iMove].setup();
86  }
87  }
88 
89  /*
90  * Choose a McMove at random.
91  */
93  {
94  int iMove;
95  iMove = randomPtr_->drawFrom(&probabilities_[0], size());
96  return (*this)[iMove];
97  }
98 
99  /*
100  * Output statistics for every move.
101  */
103  {
104  for (int i=0; i< size(); i++) {
105  (*this)[i].output();
106  }
107  }
108 
109 }
virtual void loadParameters(Serializable::IArchive &ar)
Load a set of objects to an output archive.
Definition: Manager.h:411
virtual void save(Serializable::OArchive &ar)
Save a set of objects to an output archive.
Definition: Manager.h:448
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
virtual void readParameters(std::istream &in)
Read instructions for creating McMove objects.
McMove is an abstract base class for Monte Carlo moves.
Definition: McMove.h:31
File containing preprocessor macros for error handling.
long drawFrom(double probability[], long size)
Choose one of several outcomes with a specified set of probabilities.
Definition: Random.h:237
Saving / output archive for binary ostream.
A Monte-Carlo simulation of one McSystem.
Definition: McSimulation.h:32
void setup()
Initialize at beginning of simulation run.
void output()
Output statistics for all moves.
Template container for pointers to objects with a common base class.
Definition: Manager.h:38
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
virtual void readParameters(std::istream &in)
Read child blocks, return when closing bracket encountered.
Definition: Manager.h:356
McMoveFactory for an McSimulation.
Definition: McMoveFactory.h:28
~McMoveManager()
Destructor.
Saving archive for binary istream.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
void setClassName(const char *className)
Set class name string.
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
int size() const
Get logical size.
McMoveManager(McSimulation &simulation)
Constructor.
McMove & chooseMove()
Choose an McMove at random, using specified probabilities.