PSCF v1.5.0
EinsteinCrystalPerturbation.tpp
1#ifndef RP_EINSTEIN_CRYSTAL_PERTURBATION_TPP
2#define RP_EINSTEIN_CRYSTAL_PERTURBATION_TPP
3
4
5#include "EinsteinCrystalPerturbation.h"
6
7#include <rp/fts/simulator/Simulator.h>
8#include <rp/system/System.h>
9#include <rp/solvers/Mixture.h>
10#include <rp/field/Domain.h>
11#include <rp/field/FieldIo.h>
12#include <pscf/crystal/UnitCell.h>
13
14#include <pscf/backend/VecOp.h>
15#include <pscf/backend/Reduce.h>
16
17#include <util/global.h>
18
19namespace Pscf {
20namespace Rp {
21
22 using namespace Util;
23
24 /*
25 * Constructor.
26 */
27 template <int D, class T>
30 : Perturbation<D,T>(simulator),
31 ecHamiltonian_(0.0),
32 unperturbedHamiltonian_(0.0),
33 stateEcHamiltonian_(0.0),
34 stateUnperturbedHamiltonian_(0.0),
35 hasEpsilon_(false)
36 { ParamComposite::setClassName("EinsteinCrystalPerturbation"); }
37
38 /*
39 * Read parameters from stream, empty default implementation.
40 */
41 template <int D, class T>
43 {
44 ParamComposite::read(in, "lambda", lambda_);
45
46 // Allocate and initialize epsilon_ array
47 const int nMonomer = system().mixture().nMonomer();
48 epsilon_.allocate(nMonomer - 1);
49 for (int i = 0; i < nMonomer - 1 ; ++i) {
50 epsilon_[i] = 0.0;
51 }
52
53 // Optionally read array of epsilon parameters
54 // hasEpsilon_ is set true iff epsilon values are present
55 int nc = nMonomer - 1;
56 hasEpsilon_
58 epsilon_, nc).isActive();
59
60 ParamComposite::read(in, "fieldFileName", fieldFileName_);
61
62 // Allocate work space for w fields
63 const IntVec<D> meshDimensions
64 = system().domain().mesh().dimensions();
65 w0_.allocate(nMonomer);
66 wc0_.allocate(nMonomer);
67 for (int i = 0; i < nMonomer; ++i) {
68 w0_[i].allocate(meshDimensions);
69 wc0_[i].allocate(meshDimensions);
70 }
71 dw_.allocate(meshDimensions);
72
73 }
74
75 /*
76 * Setup before simulation.
77 */
78 template <int D, class T>
80 {
81 const int nMonomer = system().mixture().nMonomer();
82 const int meshSize = system().domain().mesh().size();
83
84 // Check allocation of work space for fields
85 UTIL_CHECK(w0_.capacity() == nMonomer);
86 UTIL_CHECK(wc0_.capacity() == nMonomer);
87 for (int i = 0; i < nMonomer; ++i) {
88 UTIL_CHECK(w0_[i].capacity() == meshSize);
89 UTIL_CHECK(wc0_[i].capacity() == meshSize);
90 }
91 UTIL_CHECK(dw_.capacity() == meshSize);
92
93 /*
94 * If the user did not input epsilon_ values, set values to -1.0
95 * times the nontrivial eigenvalues of the projected chi matrix.
96 */
97 if (!hasEpsilon_){
98 for (int i = 0; i < nMonomer - 1 ; ++i) {
99 epsilon_[i] = -1.0 * simulator().chiEval(i);
100 }
101 }
102
103 // Check that all epsilon values are positive
104 for (int i = 0; i < nMonomer - 1 ; ++i) {
105 UTIL_CHECK(epsilon_[i] > 0.0);
106 }
107
108 // Read in reference field from a file
109 UnitCell<D> tempUnitCell;
110 FieldIo<D,T> const & fieldIo = system().domain().fieldIo();
111 fieldIo.readFieldsRGrid(fieldFileName_, w0_, tempUnitCell);
112
113 // Compute eigenvector components of the reference field
114 computeWcReference();
115 }
116
117 /*
118 * Compute and return perturbation to Hamiltonian.
119 */
120 template <int D, class T>
121 double
123 double unperturbedHamiltonian)
124 {
125 // Set unperturbedHamiltonian_ member variable
126 unperturbedHamiltonian_ = unperturbedHamiltonian;
127
128 // Constants
129 const int nMonomer = system().mixture().nMonomer();
130 const double vMonomer = system().mixture().vMonomer();
131 Domain<D,T> const & domain = system().domain();
132 const int meshSize = domain.mesh().size();
133 const double vSystem = domain.unitCell().volume();
134 const double nMonomerSystem = vSystem / vMonomer;
135
136 // Compute Einstein crystal Hamiltonian
137 double prefactor;
138 ecHamiltonian_ = 0.0;
139 for (int j = 0; j < nMonomer - 1; ++j) {
140 VecOp::subVV(dw_, simulator().wc(j), wc0_[j]);
141 prefactor = double(nMonomer)/(2.0 * epsilon_[j]);
142 ecHamiltonian_ += prefactor * Reduce::sumSq(dw_);
143 }
144 ecHamiltonian_ /= double(meshSize);
145 ecHamiltonian_ *= nMonomerSystem;
146
147 return (1.0 - lambda_)*(ecHamiltonian_ - unperturbedHamiltonian_);
148 }
149
150 /*
151 * Modify functional derivatives.
152 */
153 template <int D, class T>
154 void
156 {
157 const int nMonomer = system().mixture().nMonomer();
158 const double vMonomer = system().mixture().vMonomer();
159 double prefactor;
160
161 // Loop over composition eigenvectors (exclude the last)
162 for (int i = 0; i < nMonomer - 1; ++i) {
163 prefactor = double(nMonomer) / (epsilon_[i] * vMonomer);
164
165 // dw_ = prefactor * (wc - wc0_)
166 VecOp::subVV(dw_, simulator().wc(i), wc0_[i]);
167 VecOp::mulEqS(dw_, prefactor);
168
169 // dc = dc * lambda_ + dw_ * (1 - lambda_)
170 VecOp::mulEqS(dc[i], lambda_);
171 VecOp::addEqVc(dc[i], dw_, 1.0 - lambda_);
172 }
173 }
174
175 /*
176 * Compute and return derivative of free energy with respect to lambda.
177 */
178 template <int D, class T>
180 { return unperturbedHamiltonian_ - ecHamiltonian_; }
181
182 /*
183 * Save any required internal state variables.
184 */
185 template <int D, class T>
187 {
188 stateEcHamiltonian_ = ecHamiltonian_;
189 stateUnperturbedHamiltonian_ = unperturbedHamiltonian_;
190 }
191
192 /*
193 * Save any required internal state variables.
194 */
195 template <int D, class T>
197 {
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, class T>
207 void EinsteinCrystalPerturbation<D,T>::computeWcReference()
208 {
209 const int nMonomer = system().mixture().nMonomer();
210 int i, j;
211
212 // Loop over eigenvectors
213 for (i = 0; i < nMonomer; ++i) {
214
215 // Initialize to zero
216 VecOp::eqS(wc0_[i], 0.0);
217
218 // Loop over monomer types
219 for (j = 0; j < nMonomer; ++j) {
220 double vec = simulator().chiEvecs(i, j)/double(nMonomer);
221 VecOp::addEqVc(wc0_[i], w0_[j], vec);
222 }
223 }
224 }
225
226}
227}
228#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Spatial domain for a periodic structure with real fields.
UnitCell< D > & unitCell()
Get the UnitCell by non-const reference.
Mesh< D > & mesh()
Get the Mesh by non-const reference.
virtual void setup()
Complete any required initialization.
virtual double hamiltonian(double unperturbedHamiltonian)
Compute and return the perturbation to the Hamiltonian.
virtual void saveState()
Save any required internal state variables.
virtual void restoreState()
Restore any required internal state variables.
virtual void incrementDc(DArray< RField< D, T > > &dc)
Modify the generalized forces to include perturbation.
virtual double df()
Compute and return derivative of free energy w/ respect to lambda.
virtual void readParameters(std::istream &in)
Read body of parameter file block and initialize.
EinsteinCrystalPerturbation(Simulator< D, T > &simulator)
Constructor.
File input/output, format conversions and other utilities for fields.
virtual bool readFieldsRGrid(std::istream &in, DArray< RField< D, T > > &fields, UnitCell< D > &unitCell) const
Read array of r-grid fields from an input stream.
Definition FieldIo.tpp:304
Perturbation(Simulator< D, T > &simulator)
Constructor.
Simulator< D, T > const & simulator() const
Get parent Simulator<D,T> by const reference.
double lambda_
Strength of the perturbation.
System< D, T > const & system() const
Get parent System<D,T> by const reference.
Base class for field theoretic PS-FTS simulator.
Definition Simulator.h:93
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
Dynamically allocatable contiguous array template.
Definition DArray.h:32
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.
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 sumSq(Array< double > const &in)
Compute sum of of squares of array elements (real).
Definition Reduce.cpp:121
void mulEqS(Array< double > &a, double b)
Vector-scalar in-place multiplication, a[i] *= b (real).
Definition VecOp.cpp:270
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 eqS(Array< double > &a, double b)
Vector assignment, a[i] = b (real).
Definition VecOp.cpp:55
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.