PSCF v1.3.3
rpg/fts/perturbation/EinsteinCrystalPerturbation.tpp
1#ifndef RPG_EINSTEIN_CRYSTAL_PERTURBATION_TPP
2#define RPG_EINSTEIN_CRYSTAL_PERTURBATION_TPP
3
4#include "EinsteinCrystalPerturbation.h"
5#include <rpg/fts/simulator/Simulator.h>
6#include <rpg/fts/VecOpFts.h>
7#include <rpg/system/System.h>
8#include <rpg/solvers/Mixture.h>
9#include <rpg/field/Domain.h>
10#include <prdc/cuda/resources.h>
11#include <util/global.h>
12
13namespace Pscf {
14namespace Rpg {
15
16 using namespace Util;
17 using namespace Prdc::Cuda;
18
19 /*
20 * Constructor.
21 */
22 template <int D>
26 ecHamiltonian_(0.0),
27 unperturbedHamiltonian_(0.0),
28 stateEcHamiltonian_(0.0),
29 stateUnperturbedHamiltonian_(0.0),
30 hasEpsilon_(false)
31 { setClassName("EinsteinCrystalPerturbation"); }
32
33 /*
34 * Destructor.
35 */
36 template <int D>
39
40 /*
41 * Read parameters from stream, empty default implementation.
42 */
43 template <int D>
45 {
46 read(in, "lambda", lambda_);
47
48 // Allocate and initialize epsilon_ array
49 const int nMonomer = system().mixture().nMonomer();
50 epsilon_.allocate(nMonomer - 1);
51 for (int i = 0; i < nMonomer - 1 ; ++i) {
52 epsilon_[i] = 0.0;
53 }
54
55 // Optionally read the parameters used in Einstein crystal integration
56 hasEpsilon_ =
57 readOptionalDArray(in, "epsilon", epsilon_, nMonomer-1).isActive();
58
59 read(in, "referenceFieldFileName", referenceFieldFileName_);
60 }
61
62 /*
63 * Setup before simulation.
64 */
65 template <int D>
67 {
68 const int nMonomer = system().mixture().nMonomer();
69 const IntVec<D>
70 meshDimensions = system().domain().mesh().dimensions();
71
72 // Allocate memory for reference field
73 w0_.allocate(nMonomer);
74 wc0_.allocate(nMonomer);
75 for (int i = 0; i < nMonomer; ++i) {
76 w0_[i].allocate(meshDimensions);
77 wc0_[i].allocate(meshDimensions);
78 }
79
80 /*
81 * If the user did not input values for epsilon, set values to -1.0
82 * times the nontrivial eigenvalues of the projected chi matrix.
83 */
84 if (!hasEpsilon_){
85 for (int i = 0; i < nMonomer - 1 ; ++i) {
86 epsilon_[i] = -1.0 * simulator().chiEval(i);
87 }
88 }
89
90 // Check that all epsilon values are positive
91 for (int i = 0; i < nMonomer - 1 ; ++i) {
92 UTIL_CHECK(epsilon_[i] > 0.0);
93 }
94
95 // Read in reference field from a file
96 UnitCell<D> tempUnitCell;
97 FieldIo<D> const & fieldIo = system().domain().fieldIo();
98 fieldIo.readFieldsRGrid(referenceFieldFileName_,
99 w0_, tempUnitCell);
100
101 // Compute eigenvector components of the reference field
102 computeWcReference();
103 }
104
105 /*
106 * Compute and return perturbation to Hamiltonian.
107 */
108 template <int D>
109 double
110 EinsteinCrystalPerturbation<D>::hamiltonian(double unperturbedHamiltonian)
111 {
112 // Compute Einstein crystal Hamiltonian
113 const int nMonomer = system().mixture().nMonomer();
114 const int meshSize = system().domain().mesh().size();
115 const IntVec<D> meshDimensions = system().domain().mesh().dimensions();
116 const double vSystem = system().domain().unitCell().volume();
117 const double vMonomer = system().mixture().vMonomer();
118 const double nMonomerSystem = vSystem / vMonomer;
119 double prefactor;
120 RField<D> wcs;
121 wcs.allocate(meshDimensions);
122 ecHamiltonian_ = 0.0;
123
124 for (int j = 0; j < nMonomer - 1; ++j) {
125 RField<D> const & Wc = simulator().wc(j);
126 prefactor = double(nMonomer)/(2.0 * epsilon_[j]);
127 VecOp::subVV(wcs, Wc, wc0_[j]); // wcs = Wc - wc0_[j]
128 double wSqure = 0;
129 wSqure = Reduce::innerProduct(wcs, wcs);
130 ecHamiltonian_ += prefactor * wSqure;
131 }
132
133 // Normalize EC hamiltonian to equal a value per monomer
134 ecHamiltonian_ /= double(meshSize);
135
136 // Compute EC hamiltonian of system
137 ecHamiltonian_ *= nMonomerSystem;
138
139 // Set unperturbedHamiltonian_ member variable
140 unperturbedHamiltonian_ = unperturbedHamiltonian;
141
142 return (1.0 - lambda_)*(ecHamiltonian_ - unperturbedHamiltonian_);
143 }
144
145 /*
146 * Modify functional derivatives, empty default implementation.
147 */
148 template <int D>
149 void
151 {
152 const IntVec<D> meshDimensions = system().domain().mesh().dimensions();
153 const int nMonomer = system().mixture().nMonomer();
154 const double vMonomer = system().mixture().vMonomer();
155 double prefactor;
156 RField<D> DcEC;
157 DcEC.allocate(meshDimensions);
158
159 // Loop over composition eigenvectors (exclude the last)
160 for (int i = 0; i < nMonomer - 1; ++i) {
161 RField<D>& Dc = dc[i];
162 RField<D> const & Wc = simulator().wc(i);
163 prefactor = double(nMonomer) / (epsilon_[i] * vMonomer);
164
165 // Compute EC derivative
166 // DcEC = prefactor * (Wc - wc0_);
167 VecOp::subVV(DcEC, Wc, wc0_[i]);
168 VecOp::mulEqS(DcEC, prefactor);
169
170 // Compute composite derivative
171 // Dc = (Dc * lambda_) + (DcEC * (1-lambda_))
172 VecOp::addVcVc(Dc, Dc, lambda_, DcEC, 1 - lambda_);
173 }
174 }
175
176 /*
177 * Compute and return derivative of free energy with respect to lambda.
178 */
179 template <int D>
181 return unperturbedHamiltonian_ - ecHamiltonian_;
182 }
183
184 /*
185 * Save any required internal state variables.
186 */
187 template <int D>
189 stateEcHamiltonian_ = ecHamiltonian_;
190 stateUnperturbedHamiltonian_ = unperturbedHamiltonian_;
191 }
192
193 /*
194 * Save any required internal state variables.
195 */
196 template <int D>
198 ecHamiltonian_ = stateEcHamiltonian_;
199 unperturbedHamiltonian_ = stateUnperturbedHamiltonian_;
200 }
201
202 /*
203 * Compute the eigenvector components of the w fields, using the
204 * eigenvectors chiEvecs of the projected chi matrix as a basis.
205 */
206 template <int D>
207 void EinsteinCrystalPerturbation<D>::computeWcReference()
208 {
209 const int nMonomer = system().mixture().nMonomer();
210 int i, j;
211
212 // Loop over eigenvectors (i is an eigenvector index)
213 for (i = 0; i < nMonomer; ++i) {
214
215 // Loop over grid points to zero out field wc0_[i]
216 RField<D>& Wc = wc0_[i];
217
218 VecOp::eqS(Wc, 0.0); // initialize to 0
219
220 // Loop over monomer types (j is a monomer index)
221 for (j = 0; j < nMonomer; ++j) {
222 cudaReal vec;
223 vec = (cudaReal) simulator().chiEvecs(i, j)/nMonomer;
224
225 // Loop over grid points
226 VecOp::addEqVc(Wc, w0_[j], vec);
227 }
228
229 }
230 }
231
232}
233}
234#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Field of real double precision values on an FFT mesh.
Definition cpu/RField.h:29
void allocate(IntVec< D > const &meshDimensions)
Allocate the underlying C array for an FFT grid.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:56
virtual double df()
Compute and return derivative of free energy.
DArrayParam< Type > & readOptionalDArray(std::istream &in, const char *label, DArray< Type > &array, int n)
Add and read an optional DArray < Type > parameter.
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.
virtual double hamiltonian(double unperturbedHamiltonian)
Compute and return the perturbation to the Hamiltonian.
virtual void setup()
Complete any required initialization.
virtual void incrementDc(DArray< RField< D > > &dc)
Modify the generalized forces to include perturbation.
virtual void restoreState()
Restore any required internal state variables.
virtual void saveState()
Save any required internal state variables.
virtual void readParameters(std::istream &in)
Read parameters from archive.
File input/output operations and format conversions for fields.
bool readFieldsRGrid(std::istream &in, DArray< RField< D > > &fields, UnitCell< D > &unitCell) const override
Read array of RField objects (r-grid fields) from a stream.
System< D > const & system() const
Get parent System<D> by const reference.
double lambda_
Strength of the perturbation.
Simulator< D > const & simulator() const
Get parent Simulator<D> by const reference.
Perturbation(Simulator< D > &simulator)
Constructor.
Field theoretic simulator (base class).
Dynamically allocatable contiguous array template.
Definition DArray.h:32
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
double innerProduct(Array< double > const &a, Array< double > const &b)
Compute inner product of two real arrays .
Definition Reduce.cpp:94
void subVV(Array< double > &a, Array< double > const &b, Array< double > const &c)
Vector subtraction, a[i] = b[i] - c[i].
Definition VecOp.cpp:81
void mulEqS(Array< double > &a, double b)
Vector multiplication in-place, a[i] *= b.
Definition VecOp.cpp:267
void eqS(Array< double > &a, double b)
Vector assignment, a[i] = b.
Definition VecOp.cpp:36
void addVcVc(DeviceArray< cudaReal > &a, DeviceArray< cudaReal > const &b, cudaReal const c, DeviceArray< cudaReal > const &d, cudaReal const e)
Vector addition w/ coefficient, a[i] = (b[i]*c) + (d[i]*e), kernel wrapper.
Definition VecOpMisc.cu:304
void addEqVc(DeviceArray< cudaReal > &a, DeviceArray< cudaReal > const &b, cudaReal const c)
Vector addition in-place w/ coefficient, a[i] += b[i] * c, kernel wrapper.
Definition VecOpMisc.cu:343
SCFT and PS-FTS with real periodic fields (GPU)
PSCF package top-level namespace.