PSCF v1.5.0
PredCorrBdStep.tpp
1#ifndef RP_PRED_CORR_BD_STEP_TPP
2#define RP_PRED_CORR_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 "PredCorrBdStep.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
24namespace Pscf {
25namespace Rp {
26
27 using namespace Util;
28
29 /*
30 * Constructor.
31 */
32 template <int D, class T>
34 : BdStepT(simulator),
35 wf_(),
36 dci_(),
37 eta_(),
38 dwc_(),
39 dwp_(),
40 mobility_(0.0)
41 { ParamComposite::setClassName("PredCorrBdStep"); }
42
43 /*
44 * Read body of parameter file block and allocate memory.
45 */
46 template <int D, class T>
48 {
49 ParamComposite::read(in, "mobility", mobility_);
50
51 // Allocate memory for private containers
52 int nMonomer = system().mixture().nMonomer();
53 IntVec<D> meshDimensions = system().domain().mesh().dimensions();
54 wp_.allocate(nMonomer);
55 wf_.allocate(nMonomer);
56 for (int i = 0; i < nMonomer; ++i) {
57 wp_[i].allocate(meshDimensions);
58 wf_[i].allocate(meshDimensions);
59 }
60 dci_.allocate(nMonomer-1);
61 eta_.allocate(nMonomer-1);
62 for (int i = 0; i < nMonomer - 1; ++i) {
63 dci_[i].allocate(meshDimensions);
64 eta_[i].allocate(meshDimensions);
65 }
66 dwc_.allocate(meshDimensions);
67 dwp_.allocate(meshDimensions);
68 }
69
70 /*
71 * Setup before entering main simulation loop.
72 */
73 template <int D, class T>
75 {
76 // Check array capacities
77 const int meshSize = system().domain().mesh().size();
78 const int nMonomer = system().mixture().nMonomer();
79 UTIL_CHECK(wp_.capacity() == nMonomer);
80 UTIL_CHECK(wf_.capacity() == nMonomer);
81 for (int i = 0; i < nMonomer; ++i) {
82 UTIL_CHECK(wp_[i].capacity() == meshSize);
83 UTIL_CHECK(wf_[i].capacity() == meshSize);
84 }
85 UTIL_CHECK(dci_.capacity() == nMonomer - 1);
86 UTIL_CHECK(eta_.capacity() == nMonomer - 1);
87 for (int i=0; i < nMonomer - 1; ++i) {
88 UTIL_CHECK(dci_[i].capacity() == meshSize);
89 UTIL_CHECK(eta_[i].capacity() == meshSize);
90 }
91 UTIL_CHECK(dwc_.capacity() == meshSize);
92 UTIL_CHECK(dwp_.capacity() == meshSize);
93 }
94
95 /*
96 * Take one BD step.
97 */
98 template <int D, class T>
100 {
101 // Save initial state
102 simulator().saveState();
103
104 // Array sizes and indices
105 const int nMonomer = system().mixture().nMonomer();
106 const int meshSize = system().domain().mesh().size();
107 int i, j;
108
109 // Copy initial w monomer fields into wp_ and wf_
110 for (i = 0; i < nMonomer; ++i) {
111 wp_[i] = system().w().rgrid(i);
112 wf_[i] = wp_[i];
113 }
114
115 // Copy initial value of pressure field as dwp_
116 dwp_ = simulator().wc(nMonomer-1);
117
118 // Constants used for step
119 const double vSystem = system().domain().unitCell().volume();
120 const double a = -1.0 * mobility_;
121 const double stddev = sqrt(2.0*mobility_*double(meshSize)/vSystem);
122 const double mean = 0.0;
123
124 // Generate all random displacement components
125 for (j = 0; j < nMonomer - 1; ++j) {
126 BdStepT::vecRandom().normal(eta_[j], stddev, mean);
127 }
128
129 // Compute predicted state wp_, and store initial force as dci_
130
131 // Loop over composition eigenvectors of projected chi matrix
132 for (j = 0; j < nMonomer - 1; ++j) {
133 RFieldT const & dc = simulator().dc(j);
134 VecOp::addVcVc(dwc_, dc, a, eta_[j], 1.0);
135 VecOp::eqV(dci_[j], dc);
136 // Loop over monomer types
137 for (i = 0; i < nMonomer; ++i) {
138 double evec = simulator().chiEvecs(j,i);
139 VecOp::addEqVc(wp_[i], dwc_, evec);
140 }
141 }
142
143 // Set system fields to predicted state wp_
144 system().w().setRGrid(wp_);
145
146 // Apply compressor after predictor step
147 int compress = simulator().compressor().compress();
148 if (compress != 0){
149 simulator().restoreState();
150 bool isConverged = false;
151 return isConverged;
152 }
153
154 // Compute components and derivatives at wp_
155 UTIL_CHECK(system().c().hasData());
156 simulator().clearData();
157 simulator().computeWc();
158 simulator().computeCc();
159 simulator().computeDc();
160
161 // Compute change dwp_ in pressure field
162 // Note: On entry, dwp_ is the old pressure field
163 RFieldT const & wp = simulator().wc(nMonomer-1);
164 VecOp::subVV(dwp_, wp, dwp_);
165
166 // Adjust predicted pressure field
167 for (i = 0; i < nMonomer; ++i) {
168 VecOp::addEqV(wf_[i], dwp_);
169 }
170
171 // Compute corrected state wf_
172 const double ha = 0.5*a;
173 for (j = 0; j < nMonomer - 1; ++j) {
174 RFieldT const & dcp = simulator().dc(j);
175 VecOp::addVcVcVc(dwc_, dci_[j], ha, dcp, ha, eta_[j], 1.0);
176 for (i = 0; i < nMonomer; ++i) {
177 double evec = simulator().chiEvecs(j,i);
178 VecOp::addEqVc(wf_[i], dwc_, evec);
179 }
180 }
181
182 // Set system fields after corrector step
183 system().w().setRGrid(wf_);
184
185 // Apply compressor to final state
186 int compress2 = simulator().compressor().compress();
187 if (compress2 != 0){
188 simulator().restoreState();
189 bool isConverged = false;
190 return isConverged;
191 }
192
193 // Compute components and derivatives in final state
194 UTIL_CHECK(system().c().hasData());
195 simulator().clearState();
196 simulator().clearData();
197 simulator().computeWc();
198 simulator().computeCc();
199 simulator().computeDc();
200
201 // Success
202 bool isConverged = true;
203 return isConverged;
204 }
205
206}
207}
208#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
VecRandom< T > & vecRandom()
Get vector random number generator of parent simulator.
Definition BdStep.h:197
PredCorrBdStep(BdSimulator< D, T > &simulator)
Constructor.
BdSimulator< D, T > & simulator()
Get parent BdSimulator object.
Definition BdStep.h:183
void readParameters(std::istream &in) override
Read body of parameter file block and allocate memory.
void setup() override
Setup before simulation.
System< D, T > & system()
Get parent System object.
Definition BdStep.h:176
bool step() override
Take a single Brownian dynamics step.
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 addEqV(Array< double > &a, Array< double > const &b)
Vector-vector in-place addition, a[i] += b[i] (real).
Definition VecOp.cpp:203
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 subVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector-vector subtraction, a[i] = b[i] - c[i] (real).
Definition VecOp.cpp:100
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
void addVcVcVc(Array< double > &a, Array< double > const &b1, const double c1, Array< double > const &b2, const double c2, Array< double > const &b3, const double c3)
Add scaled vectors, a[i] = b1[i]*c1 + b2[i]*c2 + b3[i]*c3 (real).
Definition VecOp.cpp:431
PSCF package top-level namespace.