1 |
< |
#include <math.h> |
2 |
< |
#include "MatVec3.h" |
3 |
< |
#include "Atom.hpp" |
4 |
< |
#include "SRI.hpp" |
5 |
< |
#include "AbstractClasses.hpp" |
6 |
< |
#include "SimInfo.hpp" |
7 |
< |
#include "ForceFields.hpp" |
8 |
< |
#include "Thermo.hpp" |
9 |
< |
#include "ReadWrite.hpp" |
10 |
< |
#include "Integrator.hpp" |
11 |
< |
#include "simError.h" |
1 |
> |
#include "integrators/NPTxyz.hpp" |
2 |
|
|
13 |
– |
#ifdef IS_MPI |
14 |
– |
#include "mpiSimulation.hpp" |
15 |
– |
#endif |
16 |
– |
|
3 |
|
// Basic non-isotropic thermostating and barostating via the Melchionna |
4 |
|
// modification of the Hoover algorithm: |
5 |
|
// |
10 |
|
// |
11 |
|
// Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. |
12 |
|
|
13 |
< |
template<typename T> NPTxyz<T>::NPTxyz ( SimInfo *theInfo, ForceFields* the_ff): |
14 |
< |
T( theInfo, the_ff ) |
15 |
< |
{ |
13 |
> |
namespace oopse { |
14 |
> |
/* |
15 |
> |
NPTxyz::NPTxyz (SimInfo* info): NPT(info) { |
16 |
|
GenericData* data; |
17 |
< |
DoubleArrayData * etaValue; |
32 |
< |
vector<double> etaArray; |
17 |
> |
DoubleVectorGenericData * etaValue; |
18 |
|
int i,j; |
19 |
|
|
20 |
|
for(i = 0; i < 3; i++){ |
21 |
|
for (j = 0; j < 3; j++){ |
22 |
|
|
23 |
< |
eta[i][j] = 0.0; |
24 |
< |
oldEta[i][j] = 0.0; |
23 |
> |
eta(i, j) = 0.0; |
24 |
> |
oldEta(i, j) = 0.0; |
25 |
|
} |
26 |
|
} |
27 |
|
|
29 |
|
if( theInfo->useInitXSstate ){ |
30 |
|
|
31 |
|
// retrieve eta array from simInfo if it exists |
32 |
< |
data = info->getProperty(ETAVALUE_ID); |
32 |
> |
data = info->getPropertyByName(ETAVALUE_ID); |
33 |
|
if(data){ |
34 |
< |
etaValue = dynamic_cast<DoubleArrayData*>(data); |
34 |
> |
etaValue = dynamic_cast<DoubleVectorGenericData*>(data); |
35 |
|
|
36 |
|
if(etaValue){ |
52 |
– |
etaArray = etaValue->getData(); |
37 |
|
|
38 |
|
for(i = 0; i < 3; i++){ |
39 |
|
for (j = 0; j < 3; j++){ |
40 |
< |
eta[i][j] = etaArray[3*i+j]; |
41 |
< |
oldEta[i][j] = eta[i][j]; |
40 |
> |
eta(i, j) = (*etaValue)[3*i+j]; |
41 |
> |
oldEta(i, j) = eta(i, j); |
42 |
|
} |
43 |
|
} |
44 |
|
} |
46 |
|
} |
47 |
|
} |
48 |
|
|
65 |
– |
template<typename T> NPTxyz<T>::~NPTxyz() { |
49 |
|
|
67 |
– |
// empty for now |
68 |
– |
} |
50 |
|
|
51 |
< |
template<typename T> void NPTxyz<T>::resetIntegrator() { |
51 |
> |
void NPTxyz::evolveEtaA() { |
52 |
|
|
53 |
|
int i, j; |
54 |
|
|
55 |
< |
for(i = 0; i < 3; i++) |
56 |
< |
for (j = 0; j < 3; j++) |
57 |
< |
eta[i][j] = 0.0; |
58 |
< |
|
59 |
< |
T::resetIntegrator(); |
60 |
< |
} |
61 |
< |
|
62 |
< |
template<typename T> void NPTxyz<T>::evolveEtaA() { |
82 |
< |
|
83 |
< |
int i, j; |
84 |
< |
|
85 |
< |
for(i = 0; i < 3; i ++){ |
86 |
< |
for(j = 0; j < 3; j++){ |
87 |
< |
if( i == j) |
88 |
< |
eta[i][j] += dt2 * instaVol * |
89 |
< |
(press[i][j] - targetPressure/p_convert) / (NkBT*tb2); |
90 |
< |
else |
91 |
< |
eta[i][j] = 0.0; |
55 |
> |
for(i = 0; i < 3; i ++){ |
56 |
> |
for(j = 0; j < 3; j++){ |
57 |
> |
if( i == j) { |
58 |
> |
eta(i, j) += dt2 * instaVol *(press(i, j) - targetPressure/p_convert) / (NkBT*tb2); |
59 |
> |
} else { |
60 |
> |
eta(i, j) = 0.0; |
61 |
> |
} |
62 |
> |
} |
63 |
|
} |
93 |
– |
} |
64 |
|
|
65 |
< |
for(i = 0; i < 3; i++) |
66 |
< |
for (j = 0; j < 3; j++) |
67 |
< |
oldEta[i][j] = eta[i][j]; |
65 |
> |
for(i = 0; i < 3; i++) { |
66 |
> |
for (j = 0; j < 3; j++) { |
67 |
> |
oldEta(i, j) = eta(i, j); |
68 |
> |
} |
69 |
> |
} |
70 |
> |
|
71 |
|
} |
72 |
|
|
73 |
< |
template<typename T> void NPTxyz<T>::evolveEtaB() { |
73 |
> |
void NPTxyz::evolveEtaB() { |
74 |
|
|
75 |
|
int i,j; |
76 |
|
|
77 |
|
for(i = 0; i < 3; i++) |
78 |
|
for (j = 0; j < 3; j++) |
79 |
< |
prevEta[i][j] = eta[i][j]; |
79 |
> |
prevEta(i, j) = eta(i, j); |
80 |
|
|
81 |
|
for(i = 0; i < 3; i ++){ |
82 |
|
for(j = 0; j < 3; j++){ |
83 |
|
if( i == j) { |
84 |
< |
eta[i][j] = oldEta[i][j] + dt2 * instaVol * |
85 |
< |
(press[i][j] - targetPressure/p_convert) / (NkBT*tb2); |
84 |
> |
eta(i, j) = oldEta(i, j) + dt2 * instaVol * |
85 |
> |
(press(i, j) - targetPressure/p_convert) / (NkBT*tb2); |
86 |
|
} else { |
87 |
< |
eta[i][j] = 0.0; |
87 |
> |
eta(i, j) = 0.0; |
88 |
|
} |
89 |
|
} |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
< |
template<typename T> void NPTxyz<T>::calcVelScale(void) { |
93 |
> |
void NPTxyz::calcVelScale(void) { |
94 |
|
int i,j; |
95 |
|
|
96 |
|
for (i = 0; i < 3; i++ ) { |
97 |
|
for (j = 0; j < 3; j++ ) { |
98 |
< |
vScale[i][j] = eta[i][j]; |
98 |
> |
vScale(i, j) = eta(i, j); |
99 |
|
|
100 |
|
if (i == j) { |
101 |
< |
vScale[i][j] += chi; |
101 |
> |
vScale(i, j) += chi; |
102 |
|
} |
103 |
|
} |
104 |
|
} |
105 |
|
} |
106 |
|
|
134 |
– |
template<typename T> void NPTxyz<T>::getVelScaleA(double sc[3], double vel[3]) { |
135 |
– |
matVecMul3( vScale, vel, sc ); |
136 |
– |
} |
107 |
|
|
108 |
< |
template<typename T> void NPTxyz<T>::getVelScaleB(double sc[3], int index ){ |
109 |
< |
int j; |
140 |
< |
double myVel[3]; |
141 |
< |
|
142 |
< |
for (j = 0; j < 3; j++) |
143 |
< |
myVel[j] = oldVel[3*index + j]; |
144 |
< |
|
145 |
< |
matVecMul3( vScale, myVel, sc ); |
108 |
> |
void NPTxyz::getVelScaleA(Vector3d& sc, const Vector3d& vel) { |
109 |
> |
sc = vScale * vel; |
110 |
|
} |
111 |
|
|
112 |
< |
template<typename T> void NPTxyz<T>::getPosScale(double pos[3], double COM[3], |
113 |
< |
int index, double sc[3]){ |
150 |
< |
int j; |
151 |
< |
double rj[3]; |
152 |
< |
|
153 |
< |
for(j=0; j<3; j++) |
154 |
< |
rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; |
155 |
< |
|
156 |
< |
matVecMul3( eta, rj, sc ); |
112 |
> |
void NPTxyz::getVelScaleB(Vector3d& sc, int index ) { |
113 |
> |
sc = vScale * oldVel[index]; |
114 |
|
} |
158 |
– |
|
159 |
– |
template<typename T> void NPTxyz<T>::scaleSimBox( void ){ |
160 |
– |
|
161 |
– |
int i,j,k; |
162 |
– |
double scaleMat[3][3]; |
163 |
– |
double eta2ij, scaleFactor; |
164 |
– |
double bigScale, smallScale, offDiagMax; |
165 |
– |
double hm[3][3], hmnew[3][3]; |
166 |
– |
|
167 |
– |
|
168 |
– |
|
169 |
– |
// Scale the box after all the positions have been moved: |
170 |
– |
|
171 |
– |
// Use a taylor expansion for eta products: Hmat = Hmat . exp(dt * etaMat) |
172 |
– |
// Hmat = Hmat . ( Ident + dt * etaMat + dt^2 * etaMat*etaMat / 2) |
173 |
– |
|
174 |
– |
bigScale = 1.0; |
175 |
– |
smallScale = 1.0; |
176 |
– |
offDiagMax = 0.0; |
177 |
– |
|
178 |
– |
for(i=0; i<3; i++){ |
179 |
– |
for(j=0; j<3; j++){ |
180 |
– |
scaleMat[i][j] = 0.0; |
181 |
– |
if(i==j) scaleMat[i][j] = 1.0; |
182 |
– |
} |
183 |
– |
} |
184 |
– |
|
185 |
– |
for(i=0;i<3;i++){ |
115 |
|
|
116 |
< |
// calculate the scaleFactors |
117 |
< |
|
118 |
< |
scaleFactor = exp(dt*eta[i][i]); |
119 |
< |
|
120 |
< |
scaleMat[i][i] = scaleFactor; |
192 |
< |
|
193 |
< |
if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i]; |
194 |
< |
if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i]; |
195 |
< |
} |
196 |
< |
|
197 |
< |
// for(i=0; i<3; i++){ |
198 |
< |
// for(j=0; j<3; j++){ |
199 |
< |
|
200 |
< |
// // Calculate the matrix Product of the eta array (we only need |
201 |
< |
// // the ij element right now): |
202 |
< |
|
203 |
< |
// eta2ij = 0.0; |
204 |
< |
// for(k=0; k<3; k++){ |
205 |
< |
// eta2ij += eta[i][k] * eta[k][j]; |
206 |
< |
// } |
207 |
< |
|
208 |
< |
// scaleMat[i][j] = 0.0; |
209 |
< |
// // identity matrix (see above): |
210 |
< |
// if (i == j) scaleMat[i][j] = 1.0; |
211 |
< |
// // Taylor expansion for the exponential truncated at second order: |
212 |
< |
// scaleMat[i][j] += dt*eta[i][j] + 0.5*dt*dt*eta2ij; |
213 |
< |
|
214 |
< |
// if (i != j) |
215 |
< |
// if (fabs(scaleMat[i][j]) > offDiagMax) |
216 |
< |
// offDiagMax = fabs(scaleMat[i][j]); |
217 |
< |
// } |
218 |
< |
|
219 |
< |
// if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i]; |
220 |
< |
// if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i]; |
221 |
< |
// } |
222 |
< |
|
223 |
< |
if ((bigScale > 1.1) || (smallScale < 0.9)) { |
224 |
< |
sprintf( painCave.errMsg, |
225 |
< |
"NPTxyz error: Attempting a Box scaling of more than 10 percent.\n" |
226 |
< |
" Check your tauBarostat, as it is probably too small!\n\n" |
227 |
< |
" scaleMat = [%lf\t%lf\t%lf]\n" |
228 |
< |
" [%lf\t%lf\t%lf]\n" |
229 |
< |
" [%lf\t%lf\t%lf]\n", |
230 |
< |
scaleMat[0][0],scaleMat[0][1],scaleMat[0][2], |
231 |
< |
scaleMat[1][0],scaleMat[1][1],scaleMat[1][2], |
232 |
< |
scaleMat[2][0],scaleMat[2][1],scaleMat[2][2]); |
233 |
< |
painCave.isFatal = 1; |
234 |
< |
simError(); |
235 |
< |
} else { |
236 |
< |
info->getBoxM(hm); |
237 |
< |
matMul3(hm, scaleMat, hmnew); |
238 |
< |
info->setBoxM(hmnew); |
239 |
< |
} |
116 |
> |
void NPTxyz::getPosScale(const Vector3d& pos, const Vector3d& COM, |
117 |
> |
int index, Vector3d& sc) { |
118 |
> |
|
119 |
> |
Vector3d rj = (oldPos[index] + pos[j])/2.0 -COM; |
120 |
> |
sc = eta * rj; |
121 |
|
} |
122 |
|
|
123 |
< |
template<typename T> bool NPTxyz<T>::etaConverged() { |
123 |
> |
bool NPTxyz::etaConverged() { |
124 |
|
int i; |
125 |
|
double diffEta, sumEta; |
126 |
|
|
127 |
|
sumEta = 0; |
128 |
|
for(i = 0; i < 3; i++) |
129 |
< |
sumEta += pow(prevEta[i][i] - eta[i][i], 2); |
129 |
> |
sumEta += pow(prevEta(i, i) - eta(i, i), 2); |
130 |
|
|
131 |
|
diffEta = sqrt( sumEta / 3.0 ); |
132 |
|
|
133 |
|
return ( diffEta <= etaTolerance ); |
134 |
|
} |
135 |
|
|
136 |
< |
template<typename T> double NPTxyz<T>::getConservedQuantity(void){ |
136 |
> |
*/ |
137 |
> |
|
138 |
> |
double NPTxyz::calcConservedQuantity(){ |
139 |
|
|
140 |
|
double conservedQuantity; |
141 |
|
double totalEnergy; |
144 |
|
double barostat_kinetic; |
145 |
|
double barostat_potential; |
146 |
|
double trEta; |
147 |
< |
double a[3][3], b[3][3]; |
147 |
> |
Mat3x3d a; |
148 |
> |
Mat3x3d b; |
149 |
|
|
150 |
|
totalEnergy = tStats->getTotalE(); |
151 |
|
|
167 |
|
conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential + |
168 |
|
barostat_kinetic + barostat_potential; |
169 |
|
|
286 |
– |
// cout.width(8); |
287 |
– |
// cout.precision(8); |
170 |
|
|
289 |
– |
// cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << |
290 |
– |
// "\t" << thermostat_potential << "\t" << barostat_kinetic << |
291 |
– |
// "\t" << barostat_potential << "\t" << conservedQuantity << endl; |
292 |
– |
|
171 |
|
return conservedQuantity; |
172 |
|
|
173 |
|
} |
174 |
|
|
175 |
< |
template<typename T> string NPTxyz<T>::getAdditionalParameters(void){ |
176 |
< |
string parameters; |
299 |
< |
const int BUFFERSIZE = 2000; // size of the read buffer |
300 |
< |
char buffer[BUFFERSIZE]; |
175 |
> |
|
176 |
> |
void NPTxyz::scaleSimBox(){ |
177 |
|
|
178 |
< |
sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt); |
179 |
< |
parameters += buffer; |
178 |
> |
int i,j,k; |
179 |
> |
Mat3x3d scaleMat; |
180 |
> |
double eta2ij, scaleFactor; |
181 |
> |
double bigScale, smallScale, offDiagMax; |
182 |
> |
Mat3x3d hm; |
183 |
> |
Mat3x3d hmnew; |
184 |
|
|
185 |
< |
for(int i = 0; i < 3; i++){ |
186 |
< |
sprintf(buffer,"\t%G\t%G\t%G;", eta[i][0], eta[i][1], eta[i][2]); |
187 |
< |
parameters += buffer; |
185 |
> |
|
186 |
> |
|
187 |
> |
// Scale the box after all the positions have been moved: |
188 |
> |
|
189 |
> |
// Use a taylor expansion for eta products: Hmat = Hmat . exp(dt * etaMat) |
190 |
> |
// Hmat = Hmat . ( Ident + dt * etaMat + dt^2 * etaMat*etaMat / 2) |
191 |
> |
|
192 |
> |
bigScale = 1.0; |
193 |
> |
smallScale = 1.0; |
194 |
> |
offDiagMax = 0.0; |
195 |
> |
|
196 |
> |
for(i=0; i<3; i++){ |
197 |
> |
for(j=0; j<3; j++){ |
198 |
> |
scaleMat(i, j) = 0.0; |
199 |
> |
if(i==j) scaleMat(i, j) = 1.0; |
200 |
> |
} |
201 |
|
} |
202 |
|
|
203 |
< |
return parameters; |
203 |
> |
for(i=0;i<3;i++){ |
204 |
|
|
205 |
+ |
// calculate the scaleFactors |
206 |
+ |
|
207 |
+ |
scaleFactor = exp(dt*eta(i, i)); |
208 |
+ |
|
209 |
+ |
scaleMat(i, i) = scaleFactor; |
210 |
+ |
|
211 |
+ |
if (scaleMat(i, i) > bigScale) bigScale = scaleMat(i, i); |
212 |
+ |
if (scaleMat(i, i) < smallScale) smallScale = scaleMat(i, i); |
213 |
+ |
} |
214 |
+ |
|
215 |
+ |
if ((bigScale > 1.1) || (smallScale < 0.9)) { |
216 |
+ |
sprintf( painCave.errMsg, |
217 |
+ |
"NPTxyz error: Attempting a Box scaling of more than 10 percent.\n" |
218 |
+ |
" Check your tauBarostat, as it is probably too small!\n\n" |
219 |
+ |
" scaleMat = [%lf\t%lf\t%lf]\n" |
220 |
+ |
" [%lf\t%lf\t%lf]\n" |
221 |
+ |
" [%lf\t%lf\t%lf]\n", |
222 |
+ |
scaleMat(0, 0),scaleMat(0, 1),scaleMat(0, 2), |
223 |
+ |
scaleMat(1, 0),scaleMat(1, 1),scaleMat(1, 2), |
224 |
+ |
scaleMat(2, 0),scaleMat(2, 1),scaleMat(2, 2)); |
225 |
+ |
painCave.isFatal = 1; |
226 |
+ |
simError(); |
227 |
+ |
} else { |
228 |
+ |
info->getBoxM(hm); |
229 |
+ |
matMul3(hm, scaleMat, hmnew); |
230 |
+ |
info->setBoxM(hmnew); |
231 |
+ |
} |
232 |
|
} |
233 |
+ |
|
234 |
+ |
} |