PSCF v1.5.0
BdMove.tpp
1#ifndef RP_BD_MOVE_TPP
2#define RP_BD_MOVE_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 "BdMove.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/VecOp.h>
24#include <pscf/math/IntVec.h>
25
26namespace Pscf {
27namespace Rp {
28
29 using namespace Util;
30
31 /*
32 * Constructor.
33 */
34 template <int D, class T>
36 : McMoveT(simulator),
37 w_(),
38 etaA_(),
39 etaB_(),
40 dwc_(),
41 etaNewPtr_(nullptr),
42 etaOldPtr_(nullptr),
43 mobility_(0.0),
44 nStep_(0)
45 { ParamComposite::setClassName("BdMove"); }
46
47 /*
48 * Read body of parameter file block and allocate memory.
49 */
50 template <int D, class T>
51 void BdMove<D,T>::readParameters(std::istream &in)
52 {
54 ParamComposite::read(in, "mobility", mobility_);
55 ParamComposite::read(in, "nStep", nStep_);
56 UTIL_CHECK(nStep_ > 0);
57
58 // Allocate memory
59 int nMonomer = system().mixture().nMonomer();
60 IntVec<D> meshDimensions = system().domain().mesh().dimensions();
61 w_.allocate(nMonomer);
62 for (int i=0; i < nMonomer; ++i) {
63 w_[i].allocate(meshDimensions);
64 }
65 etaA_.allocate(nMonomer-1);
66 etaB_.allocate(nMonomer-1);
67 for (int i=0; i < nMonomer - 1; ++i) {
68 etaA_[i].allocate(meshDimensions);
69 etaB_[i].allocate(meshDimensions);
70 }
71 dwc_.allocate(meshDimensions);
72 }
73
74 template <int D, class T>
76 {
77 // Start timers
78 McMoveT::totalTimer_.start();
79
80 // Setup BD simulation
83 bdSetup();
84
85 UTIL_CHECK(simulator().hasWc());
86 UTIL_CHECK(simulator().hasCc());
87 UTIL_CHECK(simulator().hasDc());
88
89 // Run BD simulation of nStep steps
90 for (int i=0; i < nStep_; ++i) {
91 bdStep();
92 }
94
95 // Compute required properties in final state. If the compressor
96 // failed in the last BD step, restoreState() restored only what
97 // the stored state holds, and the c fields are not valid.
98 simulator().computeWc();
99 if (!system().c().hasData()) {
100 system().compute();
101 }
102 if (simulator().needsCc() || simulator().needsDc()) {
103 if (!simulator().hasCc()) {
104 simulator().computeCc();
105 }
106 }
107 if (simulator().needsDc() && !simulator().hasDc()) {
108 simulator().computeDc();
109 }
110 if (!simulator().hasHamiltonian()) {
111 simulator().computeHamiltonian();
112 }
113
115 McMoveT::totalTimer_.stop();
116
117 return true;
118 }
119
120 /*
121 * Setup before main simulation loop.
122 */
123 template <int D, class T>
125 {
126 UTIL_CHECK(system().w().hasData());
127
128 // Compute field components as needed
129 if (!system().c().hasData()){
130 system().compute();
131 }
132 if (!simulator().hasWc()) {
133 simulator().computeWc();
134 }
135 if (!simulator().hasCc()) {
136 simulator().computeCc();
137 }
138 if (!simulator().hasDc()) {
139 simulator().computeDc();
140 }
141 if (!simulator().hasHamiltonian()) {
142 simulator().computeHamiltonian();
143 }
144
145 // Check array capacities
146 int nMonomer = system().mixture().nMonomer();
147 int meshSize = system().domain().mesh().size();
148 UTIL_CHECK(etaA_.capacity() == nMonomer-1);
149 UTIL_CHECK(etaB_.capacity() == nMonomer-1);
150 for (int i=0; i < nMonomer - 1; ++i) {
151 UTIL_CHECK(etaA_[i].capacity() == meshSize);
152 UTIL_CHECK(etaB_[i].capacity() == meshSize);
153 }
154 UTIL_CHECK(dwc_.capacity() == meshSize);
155
156 // Initialize pointers
157 etaOldPtr_ = &etaA_;
158 etaNewPtr_ = &etaB_;
159
160 generateEtaNew();
161 exchangeOldNew();
162 }
163
164 /*
165 * One step of Leimkuhler-Matthews BD algorithm.
166 */
167 template <int D, class T>
169 {
170 // Preconditions
171 UTIL_CHECK(system().w().hasData());
172 UTIL_CHECK(simulator().hasDc());
173
174 // Save current state
175 simulator().saveState();
176
177 // Copy current W fields from parent system into w_
178 const int nMonomer = system().mixture().nMonomer();
179 for (int i = 0; i < nMonomer; ++i) {
180 VecOp::eqV(w_[i], system().w().rgrid(i));
181 }
182
183 // Generate new random displacement values
184 generateEtaNew();
185
186 // Take LM step
187 const double a = -1.0*mobility_;
188 double evec;
189 // Loop over eigenvectors of projected chi matrix
190 for (int j = 0; j < nMonomer - 1; ++j) {
191 RFieldT const & etaN = etaNew(j);
192 RFieldT const & etaO = etaOld(j);
193 RFieldT const & dc = simulator().dc(j);
194 VecOp::addVV(dwc_, etaN, etaO);
195 VecOp::addEqVc(dwc_, dc, a);
196 // Loop over monomer types
197 for (int i = 0; i < nMonomer; ++i) {
198 evec = simulator().chiEvecs(j,i);
199 VecOp::addEqVc(w_[i], dwc_, evec);
200 }
201 }
202
203 // Set modified fields in parent system
204 system().w().setRGrid(w_);
205
206 // Enforce incompressibility (also solves MDE repeatedly)
207 int compress = simulator().compressor().compress();
208
209 // Accept move or restore state
210 UTIL_CHECK(system().w().hasData());
211 if (compress == 0){
212 UTIL_CHECK(system().c().hasData());
213 simulator().clearState();
214 simulator().clearData();
215 simulator().computeWc();
216 simulator().computeCc();
217 simulator().computeDc();
218 simulator().computeHamiltonian();
219 } else {
220 simulator().restoreState();
221 UTIL_CHECK(system().w().hasData());
222 UTIL_CHECK(!system().c().hasData());
223 UTIL_CHECK(simulator().hasWc());
224 UTIL_CHECK(simulator().hasHamiltonian());
225 bool needCc = false;
226 if (simulator().needsCc() || !simulator().hasDc()) {
227 needCc = true;
228 }
229 if (needCc && !simulator().hasCc()) {
230 system().compute();
231 simulator().computeCc();
232 }
233 if (!simulator().hasDc()) {
234 simulator().computeDc();
235 }
236 }
237
238 // Exchange old and new random fields
239 exchangeOldNew();
240
241 return true;
242 }
243
244 /*
245 * Generate new random displacement values.
246 */
247 template <int D, class T>
248 void BdMove<D,T>::generateEtaNew()
249 {
250 // Constants
251 const int nMonomer = system().mixture().nMonomer();
252 const int meshSize = system().domain().mesh().size();
253 const double vSystem = system().domain().unitCell().volume();
254 const double stddev = sqrt(0.5*mobility_*double(meshSize)/vSystem);
255 const double mean = 0.0;
256
257 for (int j = 0; j < nMonomer - 1; ++j) {
258 vecRandom().normal(etaNew(j), stddev, mean);
259 }
260 }
261
262 /*
263 * Exchange pointers to old and new random fields.
264 */
265 template <int D, class T>
266 void BdMove<D,T>::exchangeOldNew()
267 {
268 DArray< RFieldT >* temp;
269 temp = etaOldPtr_;
270 etaOldPtr_ = etaNewPtr_;
271 etaNewPtr_ = temp;
272 }
273
274}
275}
276#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
McSimulator< D, T > & simulator()
Get parent McSimulator object (non-const ref).
Definition McMove.h:342
void bdSetup()
Setup before simulation loop.
Definition BdMove.tpp:124
void readParameters(std::istream &in) override
Read body of parameter file block.
Definition BdMove.tpp:51
bool needsDc() override
Do dc derivative components need to be saved before a step?
Definition BdMove.h:76
bool bdStep()
Take a single Brownian dynamics step.
Definition BdMove.tpp:168
BdMove(McSimulator< D, T > &simulator)
Constructor.
Definition BdMove.tpp:35
System< D, T > & system()
Get parent System object (non-const ref).
Definition McMove.h:328
bool move() override
Generate a short BD simulation.
Definition BdMove.tpp:75
void readProbability(std::istream &in)
Read the probability from file.
Definition McMove.tpp:54
void incrementNAccept()
Increment the number of accepted moves.
Definition McMove.h:314
virtual bool needsCc()
Decide whether cc fields need to be saved for move.
Definition McMove.h:120
Timer attemptMoveTimer_
Timers for McMove.
Definition McMove.h:243
void incrementNAttempt()
Increment the number of attempted moves.
Definition McMove.h:307
Monte-Carlo simulation coordinator.
Definition McSimulator.h:55
Dynamically allocatable contiguous array template.
Definition DArray.h:32
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.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
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 addVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector addition, a[i] = b[i] + c[i] (real).
Definition VecOp.cpp:69
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
PSCF package top-level namespace.