PSCF v1.5.0
ForceBiasMoveBase.tpp
1#ifndef RP_FORCE_BIAS_MOVE_BASE_TPP
2#define RP_FORCE_BIAS_MOVE_BASE_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "ForceBiasMoveBase.h"
12
13#include <rp/fts/montecarlo/McSimulator.h>
14#include <rp/fts/simulator/Simulator.h>
15#include <rp/fts/compressor/Compressor.h>
16#include <rp/system/System.h>
17#include <rp/solvers/Mixture.h>
18#include <rp/field/Domain.h>
19#include <rp/field/CFields.h>
20#include <rp/field/WFields.h>
21
22#include <pscf/backend/VecRandom.h>
23#include <pscf/backend/Reduce.h>
24#include <pscf/backend/VecOp.h>
25#include <pscf/mesh/Mesh.h>
26#include <pscf/math/IntVec.h>
27
28#include <util/param/ParamComposite.h>
29#include <util/random/Random.h>
30
31namespace Pscf {
32namespace Rp {
33
34 using namespace Util;
35
36 /*
37 * Constructor.
38 */
39 template <int D, class T>
41 : McMove<D,T>(simulator),
42 w_(),
43 dwc_(),
44 mobility_(0.0)
45 { ParamComposite::setClassName("ForceBiasMove"); }
46
47 /*
48 * Read body of parameter file block and allocate memory.
49 */
50 template <int D, class T>
52 {
54 ParamComposite::read(in, "mobility", mobility_);
55
56 // Allocate memory for private containers
57 int nMonomer = system().mixture().nMonomer();
58 IntVec<D> meshDimensions = system().domain().mesh().dimensions();
59 w_.allocate(nMonomer);
60 for (int i=0; i < nMonomer; ++i) {
61 w_[i].allocate(meshDimensions);
62 }
63 dc_.allocate(nMonomer-1);
64 dwc_.allocate(nMonomer-1);
65 for (int i=0; i < nMonomer - 1; ++i) {
66 dc_[i].allocate(meshDimensions);
67 dwc_[i].allocate(meshDimensions);
68 }
69 biasField_.allocate(meshDimensions);
70 eta_.allocate(meshDimensions);
71 }
72
73 /*
74 * Setup before entering main simulation loop.
75 */
76 template <int D, class T>
78 {
79 // Check array capacities
80 int meshSize = system().domain().mesh().size();
81 int nMonomer = system().mixture().nMonomer();
82 UTIL_CHECK(w_.capacity() == nMonomer);
83 for (int i=0; i < nMonomer; ++i) {
84 UTIL_CHECK(w_[i].capacity() == meshSize);
85 }
86 UTIL_CHECK(dc_.capacity() == nMonomer-1);
87 UTIL_CHECK(dwc_.capacity() == nMonomer-1);
88 for (int i = 0; i < nMonomer - 1; ++i) {
89 UTIL_CHECK(dc_[i].capacity() == meshSize);
90 UTIL_CHECK(dwc_[i].capacity() == meshSize);
91 }
92
94 }
95
96 /*
97 * Attempt and accept or reject MC move
98 */
99 template <int D, class T>
101 {
102 McMove<D,T>::totalTimer_.start();
104
105 // Preconditions
106 UTIL_CHECK(simulator().hasWc());
107 UTIL_CHECK(simulator().hasDc());
108 UTIL_CHECK(simulator().hasHamiltonian());
109
110 // Array sizes and index variables
111 const int nMonomer = system().mixture().nMonomer();
112 const int meshSize = system().domain().mesh().size();
113 int i, j;
114
115 // Get current Hamiltonian
116 double oldHamiltonian = simulator().hamiltonian();
117
118 // Save current state
119 simulator().saveState();
120
121 // Clear eigen-components of the fields and Hamiltonian
122 simulator().clearData();
123
125
126 // Copy current W fields from parent system into wc_
127 for (i = 0; i < nMonomer; ++i) {
128 VecOp::eqV(w_[i], system().w().rgrid(i));
129 }
130
131 // Copy derivative fields into dc_
132 for (i = 0; i < nMonomer - 1; ++i) {
133 VecOp::eqV(dc_[i], simulator().dc(i));
134 }
135
136 // Constants for dynamics
137 const double vSystem = system().domain().unitCell().volume();
138 const double vNode = vSystem/double(meshSize);
139 const double a = -1.0*mobility_;
140 const double b = sqrt(2.0*mobility_/vNode);
141 const double stddev = 1.0;
142 const double mean = 0.0;
143
144 // Modify local variables dwc_ and w_
145 // Loop over composition eigenvectors of projected chi matrix
146 for (j = 0; j < nMonomer - 1; ++j) {
147
148 // Generate vector of normal distributed random numbers
149 McMove<D,T>::vecRandom().normal(eta_, stddev, mean);
150
151 // Compute vector dwc_[j] of field component changes
152 VecOp::addVcVc(dwc_[j], dc_[j], a, eta_, b);
153
154 // Loop over monomer types to add to w_
155 for (i = 0; i < nMonomer; ++i) {
156 double evec = simulator().chiEvecs(j,i);
157 VecOp::addEqVc(w_[i], dwc_[j], evec);
158 }
159
160 }
161
162 // Set modified fields in parent system
163 system().w().setRGrid(w_);
164 simulator().clearData();
165
167
168 // Call compressor
169 McMove<D,T>::compressorTimer_.start();
170 int compress = simulator().compressor().compress();
171 UTIL_CHECK(system().c().hasData());
172 McMove<D,T>::compressorTimer_.stop();
173
174 bool isConverged = false;
175 if (compress != 0){
177 simulator().restoreState();
178 } else {
179 isConverged = true;
180
181 // Compute eigenvector components of current fields
182 McMove<D,T>::componentTimer_.start();
183 simulator().computeWc();
184 UTIL_CHECK(system().c().hasData());
185 simulator().computeCc();
186 simulator().computeDc();
187 McMove<D,T>::componentTimer_.stop();
188
189 // Evaluate new Hamiltonian
190 McMove<D,T>::hamiltonianTimer_.start();
191 simulator().computeHamiltonian();
192 double newHamiltonian = simulator().hamiltonian();
193 double dH = newHamiltonian - oldHamiltonian;
194
195 // Compute force bias used in acceptance criterion
196 double bias = 0.0;
197 for (j = 0; j < nMonomer - 1; ++j) {
198 RField<D,T> const & di = dc_[j];
199 RField<D,T> const & df = simulator().dc(j);
200 computeForceBias(biasField_, di, df, dwc_[j], mobility_);
201 bias += Reduce::sum(biasField_);
202 }
203 bias *= vNode;
204 McMove<D,T>::hamiltonianTimer_.stop();
205
206 // Accept or reject move
207 McMove<D,T>::decisionTimer_.start();
208 bool accept = false;
209 double weight = exp(bias - dH);
210 accept = McMove<D,T>::random().metropolis(weight);
211 if (accept) {
213 simulator().clearState();
214 } else {
215 simulator().restoreState();
216 }
217 McMove<D,T>::decisionTimer_.stop();
218 }
219 McMove<D,T>::totalTimer_.stop();
220
221 return isConverged;
222 }
223
224 /*
225 * Output data to log file (do-nothing implementation).
226 */
227 template <int D, class T>
230
231}
232}
233#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
void setup() override
Setup before the beginning of each simulation run.
ForceBiasMoveBase(McSimulator< D, T > &simulator)
Constructor.
void output() override
Output statistics for this move (at the end of simulation).
bool move() override
Attempt and accept or reject a force bias Monte-Carlo move.
void readParameters(std::istream &in) override
Read body of parameter file block and allocate memory.
Random & random()
Get the scalar random number generator.
Definition McMove.h:356
McSimulator< D, T > & simulator()
Get parent McSimulator object (non-const ref).
Definition McMove.h:342
void incrementNFail()
Increment the number of failed moves.
Definition McMove.h:321
void readProbability(std::istream &in)
Read the probability from file.
Definition McMove.tpp:54
VecRandom< T > & vecRandom()
Get the vector random number generator.
Definition McMove.h:363
void incrementNAccept()
Increment the number of accepted moves.
Definition McMove.h:314
System< D, CPT > & system()
Definition McMove.h:328
virtual void setup()
Setup before the beginning of each simulation run.
Definition McMove.tpp:71
Timer attemptMoveTimer_
Timers for McMove.
Definition McMove.h:243
void incrementNAttempt()
Increment the number of attempted moves.
Definition McMove.h:307
McMove(McSimulator< D, T > &simulator)
Constructor.
Definition McMove.tpp:32
Monte-Carlo simulation coordinator.
Definition McSimulator.h:55
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void setClassName(const char *className)
Set class name string.
bool metropolis(double ratio)
Metropolis algorithm for whether to accept a MC move.
Definition Random.h:272
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
double sum(Array< double > const &in)
Compute sum of array elements (real).
Definition Reduce.cpp:19
void eqV(Array< double > &a, Array< double > const &b, const int beginIdA, const int beginIdB, const int n)
Vector assignment, a[i] = b[i] (real, slice).
Definition VecOp.cpp:26
void addEqVc(Array< double > &a, Array< double > const &b, const double c)
Add scaled vector in-place, a[i] += b[i]*c (real).
Definition VecOp.cpp:400
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
void addVcVc(Array< double > &a, Array< double > const &b1, const double c1, Array< double > const &b2, const double c2)
Add two scaled vectors, a[i] = b1[i]*c1 + b2[2]*c2 (real).
Definition VecOp.cpp:369
PSCF package top-level namespace.