ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/NPTxyz.cpp
Revision: 857
Committed: Fri Nov 7 17:09:48 2003 UTC (20 years, 8 months ago) by mmeineke
File size: 7251 byte(s)
Log Message:
moved the velocity scale matrix calculation outside of the atom loop in the NPT family of integrators.

File Contents

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