PSCF v1.5.0
ExplicitBdStep.tpp
1#ifndef RP_EXPLICIT_BD_STEP_TPP
2#define RP_EXPLICIT_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 "ExplicitBdStep.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 : BdStep<D,T>(simulator),
36 w_(),
37 dwc_(),
38 gaussianField_(),
39 mobility_(0.0)
40 { ParamComposite::setClassName("ExplicitBdStep"); }
41
42 /*
43 * Read body of parameter file block and allocate memory.
44 */
45 template <int D, class T>
47 {
48 ParamComposite::read(in, "mobility", mobility_);
49
50 // Allocate memory
51 int nMonomer = system().mixture().nMonomer();
52 IntVec<D> meshDimensions = system().domain().mesh().dimensions();
53 w_.allocate(nMonomer);
54 for (int i=0; i < nMonomer; ++i) {
55 w_[i].allocate(meshDimensions);
56 }
57 dwc_.allocate(meshDimensions);
58 gaussianField_.allocate(meshDimensions);
59 }
60
61 /*
62 * Setup before entering simulation loop.
63 */
64 template <int D, class T>
66 {
67 // Check array capacities
68 IntVec<D> meshDimensions = system().domain().mesh().dimensions();
69 int meshSize = system().domain().mesh().size();
70 int nMonomer = system().mixture().nMonomer();
71 UTIL_CHECK(w_.capacity() == nMonomer);
72 for (int i = 0; i < nMonomer; ++i) {
73 UTIL_CHECK(w_[i].capacity() == meshSize);
74 UTIL_CHECK(w_[i].meshDimensions() == meshDimensions);
75 }
76 UTIL_CHECK(dwc_.capacity() == meshSize);
77 UTIL_CHECK(dwc_.meshDimensions() == meshDimensions);
78 UTIL_CHECK(gaussianField_.capacity() == meshSize);
79 UTIL_CHECK(gaussianField_.meshDimensions() == meshDimensions);
80 }
81
82 /*
83 * Take a single BD step.
84 */
85 template <int D, class T>
87 {
88 // Array sizes and indices
89 const int nMonomer = system().mixture().nMonomer();
90 const int meshSize = system().domain().mesh().size();
91 int i, j;
92
93 // Save current state
94 simulator().saveState();
95
96 // Copy current W fields from parent system into w_
97 for (i = 0; i < nMonomer; ++i) {
98 VecOp::eqV(w_[i], system().w().rgrid(i));
99 }
100
101 // Constants for dynamics
102 const double vSystem = system().domain().unitCell().volume();
103 const double a = -1.0*mobility_;
104 const double b = sqrt(2.0*mobility_*double(meshSize)/vSystem);
105
106 // Constants for normal distribution
107 const double stddev = 1.0;
108 const double mean = 0.0;
109
110 // Modify local field copy wc_
111 // Loop over eigenvectors of projected chi matrix
112 double evec;
113 for (j = 0; j < nMonomer - 1; ++j) {
114
115 // Generate normal distributed random numbers
116 vecRandom().normal(gaussianField_, stddev, mean);
117
118 // Compute change dwc_
119 RField<D,T> const & dc = simulator().dc(j);
120 VecOp::addVcVc(dwc_, dc, a, gaussianField_, b);
121
122 // Loop over monomer types
123 for (i = 0; i < nMonomer; ++i) {
124 RField<D,T> & w = w_[i];
125 evec = simulator().chiEvecs(j,i);
126 VecOp::addEqVc(w, dwc_, evec);
127 }
128
129 }
130
131 // Set modified fields in parent system
132 system().w().setRGrid(w_);
133 simulator().clearData();
134
135 // Enforce incompressibility (also solves MDE repeatedly)
136 bool isConverged = false;
137 int compress = simulator().compressor().compress();
138 if (compress != 0){
139 simulator().restoreState();
140 } else {
141 isConverged = true;
142 UTIL_CHECK(system().c().hasData());
143
144 // Evaluate component properties in new state
145 simulator().clearState();
146 simulator().computeWc();
147 simulator().computeCc();
148 simulator().computeDc();
149 }
150
151 return isConverged;
152 }
153
154}
155}
156#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
BdStep(BdSimulator< D, T > &simulator)
Constructor.
Definition BdStep.tpp:32
BdSimulator< D, T > & simulator()
Get parent BdSimulator object.
Definition BdStep.h:183
System< D, T > & system()
Get parent System object.
Definition BdStep.h:176
VecRandom< T > & vecRandom()
Get vector random number generator of parent simulator.
Definition BdStep.h:197
void setup() override
Setup before simulation.
ExplicitBdStep(BdSimulator< D, T > &simulator)
Constructor.
bool step() override
Take a single Brownian dynamics step.
void readParameters(std::istream &in) override
Read body of parameter file block.
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 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.