PSCF v1.4.0
McMoveManager.tpp
1#ifndef RP_MC_MOVE_MANAGER_TPP
2#define RP_MC_MOVE_MANAGER_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/random/Random.h>
12#include <util/global.h>
13
14namespace Pscf {
15namespace Rp {
16
17 using namespace Util;
18
19 /*
20 * Constructor.
21 */
22 template <int D, class T>
23 McMoveManager<D,T>::McMoveManager(typename T::McSimulator& simulator,
24 typename T::System& system)
25 : Base(),
26 simulatorPtr_(&simulator),
27 systemPtr_(&system),
28 randomPtr_(&simulator.random())
29 { ParamComposite::setClassName("McMoveManager"); }
30
31 /*
32 * Return a pointer to a new McMoveFactory object.
33 */
34 template <int D, class T>
36 const
37 { return new typename T::McMoveFactory(*simulatorPtr_); }
38
39 /*
40 * Read instructions for creating objects from file.
41 */
42 template <int D, class T>
44 {
45 // Read parameters for all McMoveT objects
47
48 // Allocate and store probabilities
49 probabilities_.allocate(Base::size());
50 double totalProbability = 0.0;
51 int iMove;
52 for (iMove = 0; iMove < Base::size(); ++iMove) {
53 probabilities_[iMove] = (*this)[iMove].probability();
54 totalProbability += probabilities_[iMove];
55 }
56
57 // Validate the sum of MC move probabilities
58 if (std::abs(totalProbability - 1.0) > 1.0E-4) {
59 std::string msg = "Error: ";
60 msg += "Sum of MC move proababilities differs too much from 1.0";
61 std::cout << msg;
62 UTIL_THROW(msg.c_str());
63 }
65 // Store normalized probabilities for use in MC move selection
66 for (iMove = 0; iMove < Base::size(); ++iMove) {
67 probabilities_[iMove] = probabilities_[iMove]/totalProbability;
68 (*this)[iMove].setProbability(probabilities_[iMove]);
69 }
70 }
71
72 /*
73 * Initialize all moves just prior to a run.
74 */
75 template <int D, class T>
77 {
78 for (int iMove = 0; iMove < Base::size(); ++iMove) {
79 (*this)[iMove].setup();
80 }
81 }
82
83 /*
84 * Choose a McMove at random.
85 */
86 template <int D, class T>
87 typename T::McMove& McMoveManager<D,T>::chooseMove()
88 {
89 int iMove;
90 iMove = randomPtr_->drawFrom(&probabilities_[0], Base::size());
91 return (*this)[iMove];
92 }
93
94 /*
95 * Output statistics for every move.
96 */
97 template <int D, class T>
99 {
100 for (int i = 0; i < Base::size(); ++i) {
101 (*this)[i].output();
102 }
103 }
104
105 /*
106 * Log output timing results
107 */
108 template <int D, class T>
109 void McMoveManager<D,T>::outputTimers(std::ostream& out) const
110 {
111 for (int i = 0; i < Base::size(); ++i) {
112 (*this)[i].outputTimers(out);
113 }
114 }
115
116 /*
117 * Clear timers
118 */
119 template <int D, class T>
121 {
122 for (int i = 0; i < Base::size(); ++i) {
123 (*this)[i].clearTimers();
124 }
125 }
126
127 /*
128 * Decide whether any move needs to store cc fields.
129 */
130 template <int D, class T>
132 {
133 for (int i = 0; i < Base::size(); ++i) {
134 if ((*this)[i].needsCc()) {
135 return true;
136 }
137 }
138 return false;
139 }
140
141 /*
142 * Decide whether any move needs to store dc fields.
143 */
144 template <int D, class T>
146 {
147 for (int i = 0; i < Base::size(); ++i) {
148 if ((*this)[i].needsDc()) {
149 return true;
150 }
151 }
152 return false;
153 }
154
155}
156}
157#endif
void outputTimers(std::ostream &out) const
Write timing results to an output stream.
void output() const
Output statistics for all moves.
bool needsCc()
Decide whether any move needs to store cc fields.
virtual void readParameters(std::istream &in)
Read instructions for creating McMove objects.
McMoveManager(typename T::McSimulator &simulator, typename T::System &system)
Constructor.
void clearTimers()
Clear timers.
void setup()
Initialize at beginning of system run.
bool needsDc()
Decide whether any move needs to store dc fields.
T::McMove & chooseMove()
Choose an McMove at random, using specified probabilities.
Factory template.
Definition Factory.h:34
virtual Factory< T::McMove > * newDefaultFactory() const
Definition Manager.h:541
virtual void readParameters(std::istream &in)
void setClassName(const char *className)
Set class name string.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
Class templates for real-valued periodic fields.
PSCF package top-level namespace.