ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new-templateless/OOPSE/libmdtools/NPT.cpp
Revision: 850
Committed: Mon Nov 3 22:07:17 2003 UTC (20 years, 9 months ago) by mmeineke
File size: 7715 byte(s)
Log Message:
begun work on removing templates and most of standard template library from OOPSE.

File Contents

# Content
1 #include <stdlib.h>
2 #include <math.h>
3
4 #include "Atom.hpp"
5 #include "SRI.hpp"
6 #include "AbstractClasses.hpp"
7 #include "SimInfo.hpp"
8 #include "ForceFields.hpp"
9 #include "Thermo.hpp"
10 #include "ReadWrite.hpp"
11 #include "Integrator.hpp"
12 #include "simError.h"
13
14 #ifdef IS_MPI
15 #include "mpiSimulation.hpp"
16 #endif
17
18
19 // Basic isotropic thermostating and barostating via the Melchionna
20 // modification of the Hoover algorithm:
21 //
22 // Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
23 // Molec. Phys., 78, 533.
24 //
25 // and
26 //
27 // Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
28
29 NPT::NPT ( SimInfo *theInfo, ForceFields* the_ff):
30 Integrator( theInfo, the_ff )
31 {
32 GenericData* data;
33
34 chi = 0.0;
35 integralOfChidt = 0.0;
36 have_tau_thermostat = 0;
37 have_tau_barostat = 0;
38 have_target_temp = 0;
39 have_target_pressure = 0;
40 have_chi_tolerance = 0;
41 have_eta_tolerance = 0;
42 have_pos_iter_tolerance = 0;
43
44 // retrieve chi and integralOfChidt from simInfo
45 data = info->getProperty(CHIVALUE_ID);
46 if(data != NULL ){
47 chi = data->getDval();
48 }
49
50 data = info->getProperty(INTEGRALOFCHIDT_ID);
51 if(data != NULL ){
52 integralOfChidt = data->getDval();
53 }
54
55 oldPos = new double[3*nAtoms];
56 oldVel = new double[3*nAtoms];
57 oldJi = new double[3*nAtoms];
58 #ifdef IS_MPI
59 Nparticles = mpiSim->getTotAtoms();
60 #else
61 Nparticles = theInfo->n_atoms;
62 #endif
63
64 }
65
66 NPT::~NPT() {
67 delete[] oldPos;
68 delete[] oldVel;
69 delete[] oldJi;
70 }
71
72 void NPT::moveA() {
73
74 //new version of NPT
75 int i, j, k;
76 DirectionalAtom* dAtom;
77 double Tb[3], ji[3];
78 double mass;
79 double vel[3], pos[3], frc[3];
80 double sc[3];
81 double COM[3];
82
83 instaTemp = tStats->getTemperature();
84 tStats->getPressureTensor( press );
85 instaPress = p_convert * (press[0][0] + press[1][1] + press[2][2]) / 3.0;
86 instaVol = tStats->getVolume();
87
88 tStats->getCOM(COM);
89
90 //evolve velocity half step
91 for( i=0; i<nAtoms; i++ ){
92
93 atoms[i]->getVel( vel );
94 atoms[i]->getFrc( frc );
95
96 mass = atoms[i]->getMass();
97
98 getVelScaleA( sc, vel );
99
100 for (j=0; j < 3; j++) {
101
102 // velocity half step (use chi from previous step here):
103 vel[j] += dt2 * ((frc[j] / mass ) * eConvert - sc[j]);
104
105 }
106
107 atoms[i]->setVel( vel );
108
109 if( atoms[i]->isDirectional() ){
110
111 dAtom = (DirectionalAtom *)atoms[i];
112
113 // get and convert the torque to body frame
114
115 dAtom->getTrq( Tb );
116 dAtom->lab2Body( Tb );
117
118 // get the angular momentum, and propagate a half step
119
120 dAtom->getJ( ji );
121
122 for (j=0; j < 3; j++)
123 ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
124
125 this->rotationPropagation( dAtom, ji );
126
127 dAtom->setJ( ji );
128 }
129 }
130
131 // evolve chi and eta half step
132
133 evolveChiA();
134 evolveEtaA();
135
136 //calculate the integral of chidt
137 integralOfChidt += dt2*chi;
138
139 //save the old positions
140 for(i = 0; i < nAtoms; i++){
141 atoms[i]->getPos(pos);
142 for(j = 0; j < 3; j++)
143 oldPos[i*3 + j] = pos[j];
144 }
145
146 //the first estimation of r(t+dt) is equal to r(t)
147
148 for(k = 0; k < 5; k ++){
149
150 for(i =0 ; i < nAtoms; i++){
151
152 atoms[i]->getVel(vel);
153 atoms[i]->getPos(pos);
154
155 this->getPosScale( pos, COM, i, sc );
156
157 for(j = 0; j < 3; j++)
158 pos[j] = oldPos[i*3 + j] + dt*(vel[j] + sc[j]);
159
160 atoms[i]->setPos( pos );
161 }
162
163 if (nConstrained){
164 constrainA();
165 }
166 }
167
168
169 // Scale the box after all the positions have been moved:
170
171 this->scaleSimBox();
172 }
173
174 void NPT::moveB( void ){
175
176 //new version of NPT
177 int i, j, k;
178 DirectionalAtom* dAtom;
179 double Tb[3], ji[3], sc[3];
180 double vel[3], frc[3];
181 double mass;
182
183 // Set things up for the iteration:
184
185 for( i=0; i<nAtoms; i++ ){
186
187 atoms[i]->getVel( vel );
188
189 for (j=0; j < 3; j++)
190 oldVel[3*i + j] = vel[j];
191
192 if( atoms[i]->isDirectional() ){
193
194 dAtom = (DirectionalAtom *)atoms[i];
195
196 dAtom->getJ( ji );
197
198 for (j=0; j < 3; j++)
199 oldJi[3*i + j] = ji[j];
200
201 }
202 }
203
204 // do the iteration:
205
206 instaVol = tStats->getVolume();
207
208 for (k=0; k < 4; k++) {
209
210 instaTemp = tStats->getTemperature();
211 instaPress = tStats->getPressure();
212
213 // evolve chi another half step using the temperature at t + dt/2
214
215 this->evolveChiB();
216 this->evolveEtaB();
217
218 for( i=0; i<nAtoms; i++ ){
219
220 atoms[i]->getFrc( frc );
221 atoms[i]->getVel(vel);
222
223 mass = atoms[i]->getMass();
224
225 getVelScaleB( sc, i );
226
227 // velocity half step
228 for (j=0; j < 3; j++)
229 vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - sc[j]);
230
231 atoms[i]->setVel( vel );
232
233 if( atoms[i]->isDirectional() ){
234
235 dAtom = (DirectionalAtom *)atoms[i];
236
237 // get and convert the torque to body frame
238
239 dAtom->getTrq( Tb );
240 dAtom->lab2Body( Tb );
241
242 for (j=0; j < 3; j++)
243 ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi);
244
245 dAtom->setJ( ji );
246 }
247 }
248
249 if (nConstrained){
250 constrainB();
251 }
252
253 if ( this->chiConverged() && this->etaConverged() ) break;
254 }
255
256 //calculate integral of chida
257 integralOfChidt += dt2*chi;
258
259
260 }
261
262 void NPT::resetIntegrator() {
263 chi = 0.0;
264 Integrator::resetIntegrator();
265 }
266
267 void NPT::evolveChiA() {
268 chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
269 oldChi = chi;
270 }
271
272 void NPT::evolveChiB() {
273
274 prevChi = chi;
275 chi = oldChi + dt2 * ( instaTemp / targetTemp - 1.0) / tt2;
276 }
277
278 bool NPT::chiConverged() {
279
280 return ( fabs( prevChi - chi ) <= chiTolerance );
281 }
282
283 int NPT::readyCheck() {
284
285 //check parent's readyCheck() first
286 if (Integrator::readyCheck() == -1)
287 return -1;
288
289 // First check to see if we have a target temperature.
290 // Not having one is fatal.
291
292 if (!have_target_temp) {
293 sprintf( painCave.errMsg,
294 "NPT error: You can't use the NPT integrator\n"
295 " without a targetTemp!\n"
296 );
297 painCave.isFatal = 1;
298 simError();
299 return -1;
300 }
301
302 if (!have_target_pressure) {
303 sprintf( painCave.errMsg,
304 "NPT error: You can't use the NPT integrator\n"
305 " without a targetPressure!\n"
306 );
307 painCave.isFatal = 1;
308 simError();
309 return -1;
310 }
311
312 // We must set tauThermostat.
313
314 if (!have_tau_thermostat) {
315 sprintf( painCave.errMsg,
316 "NPT error: If you use the NPT\n"
317 " integrator, you must set tauThermostat.\n");
318 painCave.isFatal = 1;
319 simError();
320 return -1;
321 }
322
323 // We must set tauBarostat.
324
325 if (!have_tau_barostat) {
326 sprintf( painCave.errMsg,
327 "NPT error: If you use the NPT\n"
328 " integrator, you must set tauBarostat.\n");
329 painCave.isFatal = 1;
330 simError();
331 return -1;
332 }
333
334 if (!have_chi_tolerance) {
335 sprintf( painCave.errMsg,
336 "NPT warning: setting chi tolerance to 1e-6\n");
337 chiTolerance = 1e-6;
338 have_chi_tolerance = 1;
339 painCave.isFatal = 0;
340 simError();
341 }
342
343 if (!have_eta_tolerance) {
344 sprintf( painCave.errMsg,
345 "NPT warning: setting eta tolerance to 1e-6\n");
346 etaTolerance = 1e-6;
347 have_eta_tolerance = 1;
348 painCave.isFatal = 0;
349 simError();
350 }
351
352 // We need NkBT a lot, so just set it here: This is the RAW number
353 // of particles, so no subtraction or addition of constraints or
354 // orientational degrees of freedom:
355
356 NkBT = (double)Nparticles * kB * targetTemp;
357
358 // fkBT is used because the thermostat operates on more degrees of freedom
359 // than the barostat (when there are particles with orientational degrees
360 // of freedom). ndf = 3 * (n_atoms + n_oriented -1) - n_constraint - nZcons
361
362 fkBT = (double)info->ndf * kB * targetTemp;
363
364 tt2 = tauThermostat * tauThermostat;
365 tb2 = tauBarostat * tauBarostat;
366
367 return 1;
368 }