PSCF v1.5.0
LMBdStep.tpp
1#ifndef RP_LM_BD_STEP_TPP
2#define RP_LM_BD_STEP_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 "LMBdStep.h"
12
13#include <rp/fts/brownian/BdSimulator.h>
14#include <rp/fts/compressor/Compressor.h>
15#include <rp/system/System.h>
16#include <rp/solvers/Mixture.h>
17#include <rp/field/Domain.h>
18#include <rp/field/CFields.h>
19#include <rp/field/WFields.h>
20
21#include <pscf/backend/VecRandom.h>
22#include <pscf/backend/VecOp.h>
23#include <pscf/math/IntVec.h>
24
25namespace Pscf {
26namespace Rp {
27
28 using namespace Util;
29
30 /*
31 * Constructor.
32 */
33 template <int D, class T>
35 : BdStepT(simulator),
36 w_(),
37 etaA_(),
38 etaB_(),
39 dwc_(),
40 etaNewPtr_(nullptr),
41 etaOldPtr_(nullptr),
42 mobility_(0.0)
43 { ParamComposite::setClassName("LmBdStep"); }
44
45 /*
46 * Read body of parameter file block and allocate memory.
47 */
48 template <int D, class T>
49 void LMBdStep<D,T>::readParameters(std::istream &in)
50 {
51 ParamComposite::read(in, "mobility", mobility_);
52
53 // Allocate memory
54 int nMonomer = system().mixture().nMonomer();
55 IntVec<D> meshDimensions = system().domain().mesh().dimensions();
56 w_.allocate(nMonomer);
57 for (int i=0; i < nMonomer; ++i) {
58 w_[i].allocate(meshDimensions);
59 }
60 etaA_.allocate(nMonomer-1);
61 etaB_.allocate(nMonomer-1);
62 for (int i=0; i < nMonomer - 1; ++i) {
63 etaA_[i].allocate(meshDimensions);
64 etaB_[i].allocate(meshDimensions);
65 }
66 dwc_.allocate(meshDimensions);
67 }
68
69 /*
70 * Generate new random displacement values.
71 */
72 template <int D, class T>
73 void LMBdStep<D,T>::generateEtaNew()
74 {
75 // Constants
76 const int nMonomer = system().mixture().nMonomer();
77 const int meshSize = system().domain().mesh().size();
78 const double vSystem = system().domain().unitCell().volume();
79 const double stddev = sqrt(0.5*mobility_*double(meshSize)/vSystem);
80 const double mean = 0.0;
81
82 for (int j = 0; j < nMonomer - 1; ++j) {
83 vecRandom().normal(etaNew(j), stddev, mean);
84 }
85 }
86
87 /*
88 * Exchange pointers to old and new random fields.
89 */
90 template <int D, class T>
91 void LMBdStep<D,T>::exchangeOldNew()
92 {
94 temp = etaOldPtr_;
95 etaOldPtr_ = etaNewPtr_;
96 etaNewPtr_ = temp;
97 }
98
99 /*
100 * Setup before main simulation loop.
101 */
102 template <int D, class T>
104 {
105 int nMonomer = system().mixture().nMonomer();
106 int meshSize = system().domain().mesh().size();
107
108 // Check array capacities
109 UTIL_CHECK(etaA_.capacity() == nMonomer-1);
110 UTIL_CHECK(etaB_.capacity() == nMonomer-1);
111 for (int i=0; i < nMonomer - 1; ++i) {
112 UTIL_CHECK(etaA_[i].capacity() == meshSize);
113 UTIL_CHECK(etaB_[i].capacity() == meshSize);
114 }
115 UTIL_CHECK(dwc_.capacity() == meshSize);
116
117 // Initialize pointers
118 etaOldPtr_ = &etaA_;
119 etaNewPtr_ = &etaB_;
120
121 generateEtaNew();
122 exchangeOldNew();
123 }
124
125 /*
126 * One step of Leimkuhler-Matthews BD algorithm.
127 */
128 template <int D, class T>
130 {
131 // Save current state
132 simulator().saveState();
133
134 // Copy current W fields from parent system into w_
135 const int nMonomer = system().mixture().nMonomer();
136 for (int i = 0; i < nMonomer; ++i) {
137 VecOp::eqV(w_[i], system().w().rgrid(i));
138 }
139
140 // Generate new random displacement values
141 generateEtaNew();
142
143 // Take LM step
144 const double a = -1.0*mobility_;
145 double evec;
146 // Loop over eigenvectors of projected chi matrix
147 for (int j = 0; j < nMonomer - 1; ++j) {
148 RFieldT const & etaN = etaNew(j);
149 RFieldT const & etaO = etaOld(j);
150 RFieldT const & dc = simulator().dc(j);
151 VecOp::addVV(dwc_, etaN, etaO);
152 VecOp::addEqVc(dwc_, dc, a);
153 // Loop over monomer types
154 for (int i = 0; i < nMonomer; ++i) {
155 evec = simulator().chiEvecs(j,i);
156 VecOp::addEqVc(w_[i], dwc_, evec);
157 }
158 }
159
160 // Set modified fields in parent system
161 system().w().setRGrid(w_);
162
163 // Enforce incompressibility (also solves MDE repeatedly)
164 bool isConverged = false;
165 int compress = simulator().compressor().compress();
166 if (compress != 0){
167 simulator().restoreState();
168 } else {
169 isConverged = true;
170 UTIL_CHECK(system().c().hasData());
171
172 // Compute components and derivatives at wp_
173 simulator().clearState();
174 simulator().clearData();
175 simulator().computeWc();
176 simulator().computeCc();
177 simulator().computeDc();
178
179 // Exchange old and new random fields
180 exchangeOldNew();
181 }
182
183 return isConverged;
184 }
185
186}
187}
188#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Brownian dynamics simulator for PS-FTS.
Definition BdSimulator.h:52
void readParameters(std::istream &in) override
Read body of parameter file block.
Definition LMBdStep.tpp:49
LMBdStep(BdSimulator< D, T > &simulator)
Constructor.
Definition LMBdStep.tpp:34
bool step() override
Take a single Brownian dynamics step.
Definition LMBdStep.tpp:129
BdSimulator< D, T > & simulator()
Get parent BdSimulator object.
Definition BdStep.h:183
System< D, T > & system()
Get parent System object.
Definition BdStep.h:176
void setup() override
Setup before simulation loop.
Definition LMBdStep.tpp:103
int size() const
Return logical size of array.
Definition Array.h:243
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.