ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Thermo.cpp
Revision: 1127
Committed: Tue Apr 20 16:56:40 2004 UTC (20 years, 2 months ago) by tim
File size: 9563 byte(s)
Log Message:
fixed getCOMVel  and velocitize at thermo

File Contents

# User Rev Content
1 gezelter 829 #include <math.h>
2 mmeineke 377 #include <iostream>
3     using namespace std;
4    
5     #ifdef IS_MPI
6     #include <mpi.h>
7     #endif //is_mpi
8    
9     #include "Thermo.hpp"
10     #include "SRI.hpp"
11     #include "Integrator.hpp"
12 chuckv 438 #include "simError.h"
13 mmeineke 402
14     #ifdef IS_MPI
15 chuckv 401 #define __C
16 mmeineke 402 #include "mpiSimulation.hpp"
17     #endif // is_mpi
18 mmeineke 377
19 mmeineke 614 Thermo::Thermo( SimInfo* the_info ) {
20     info = the_info;
21 tim 708 int baseSeed = the_info->getSeed();
22 mmeineke 377
23     gaussStream = new gaussianSPRNG( baseSeed );
24     }
25    
26     Thermo::~Thermo(){
27     delete gaussStream;
28     }
29    
30     double Thermo::getKinetic(){
31    
32     const double e_convert = 4.184E-4; // convert kcal/mol -> (amu A^2)/fs^2
33 gezelter 608 double kinetic;
34     double amass;
35     double aVel[3], aJ[3], I[3][3];
36 tim 1118 int i, j, k, kl;
37 mmeineke 377
38     double kinetic_global;
39 tim 1113 vector<StuntDouble *> integrableObjects = info->integrableObjects;
40 mmeineke 377
41     kinetic = 0.0;
42     kinetic_global = 0.0;
43    
44 tim 1113 for (kl=0; kl<integrableObjects.size(); kl++) {
45     integrableObjects[kl]->getVel(aVel);
46     amass = integrableObjects[kl]->getMass();
47 gezelter 608
48 tim 1113 for(j=0; j<3; j++)
49     kinetic += amass*aVel[j]*aVel[j];
50    
51     if (integrableObjects[kl]->isDirectional()){
52    
53     integrableObjects[kl]->getJ( aJ );
54     integrableObjects[kl]->getI( I );
55    
56 tim 1118 if (integrableObjects[kl]->isLinear()) {
57     i = integrableObjects[kl]->linearAxis();
58     j = (i+1)%3;
59     k = (i+2)%3;
60     kinetic += aJ[j]*aJ[j]/I[j][j] + aJ[k]*aJ[k]/I[k][k];
61     } else {
62 gezelter 1125 for (j=0; j<3; j++)
63     kinetic += aJ[j]*aJ[j] / I[j][j];
64 tim 1118 }
65 gezelter 1125 }
66 mmeineke 377 }
67     #ifdef IS_MPI
68 mmeineke 447 MPI_Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE,
69     MPI_SUM, MPI_COMM_WORLD);
70 mmeineke 377 kinetic = kinetic_global;
71     #endif //is_mpi
72 gezelter 1125
73 mmeineke 377 kinetic = kinetic * 0.5 / e_convert;
74    
75     return kinetic;
76     }
77    
78     double Thermo::getPotential(){
79    
80 chuckv 401 double potential_local;
81 mmeineke 377 double potential;
82     int el, nSRI;
83 mmeineke 428 Molecule* molecules;
84 mmeineke 377
85 mmeineke 614 molecules = info->molecules;
86     nSRI = info->n_SRI;
87 mmeineke 377
88 chuckv 401 potential_local = 0.0;
89 chuckv 438 potential = 0.0;
90 mmeineke 614 potential_local += info->lrPot;
91 mmeineke 377
92 mmeineke 614 for( el=0; el<info->n_mol; el++ ){
93 mmeineke 428 potential_local += molecules[el].getPotential();
94 mmeineke 377 }
95    
96     // Get total potential for entire system from MPI.
97     #ifdef IS_MPI
98 mmeineke 447 MPI_Allreduce(&potential_local,&potential,1,MPI_DOUBLE,
99     MPI_SUM, MPI_COMM_WORLD);
100 chuckv 401 #else
101     potential = potential_local;
102 mmeineke 377 #endif // is_mpi
103    
104     return potential;
105     }
106    
107     double Thermo::getTotalE(){
108    
109     double total;
110    
111     total = this->getKinetic() + this->getPotential();
112     return total;
113     }
114    
115 gezelter 454 double Thermo::getTemperature(){
116    
117 tim 763 const double kb = 1.9872156E-3; // boltzman's constant in kcal/(mol K)
118 gezelter 454 double temperature;
119 tim 1113
120 mmeineke 614 temperature = ( 2.0 * this->getKinetic() ) / ((double)info->ndf * kb );
121 mmeineke 377 return temperature;
122     }
123    
124 gezelter 484 double Thermo::getVolume() {
125 gezelter 574
126 mmeineke 614 return info->boxVol;
127 gezelter 484 }
128    
129 gezelter 483 double Thermo::getPressure() {
130 gezelter 574
131 gezelter 483 // Relies on the calculation of the full molecular pressure tensor
132    
133     const double p_convert = 1.63882576e8;
134 gezelter 588 double press[3][3];
135 gezelter 483 double pressure;
136    
137     this->getPressureTensor(press);
138    
139 gezelter 588 pressure = p_convert * (press[0][0] + press[1][1] + press[2][2]) / 3.0;
140 gezelter 483
141     return pressure;
142     }
143    
144 mmeineke 755 double Thermo::getPressureX() {
145 gezelter 483
146 mmeineke 755 // Relies on the calculation of the full molecular pressure tensor
147    
148     const double p_convert = 1.63882576e8;
149     double press[3][3];
150     double pressureX;
151    
152     this->getPressureTensor(press);
153    
154     pressureX = p_convert * press[0][0];
155    
156     return pressureX;
157     }
158    
159     double Thermo::getPressureY() {
160    
161     // Relies on the calculation of the full molecular pressure tensor
162    
163     const double p_convert = 1.63882576e8;
164     double press[3][3];
165     double pressureY;
166    
167     this->getPressureTensor(press);
168    
169     pressureY = p_convert * press[1][1];
170    
171     return pressureY;
172     }
173    
174     double Thermo::getPressureZ() {
175    
176     // Relies on the calculation of the full molecular pressure tensor
177    
178     const double p_convert = 1.63882576e8;
179     double press[3][3];
180     double pressureZ;
181    
182     this->getPressureTensor(press);
183    
184     pressureZ = p_convert * press[2][2];
185    
186     return pressureZ;
187     }
188    
189    
190 gezelter 588 void Thermo::getPressureTensor(double press[3][3]){
191 gezelter 483 // returns pressure tensor in units amu*fs^-2*Ang^-1
192 gezelter 445 // routine derived via viral theorem description in:
193     // Paci, E. and Marchi, M. J.Phys.Chem. 1996, 100, 4314-4322
194 mmeineke 377
195 gezelter 477 const double e_convert = 4.184e-4;
196 gezelter 483
197     double molmass, volume;
198 gezelter 468 double vcom[3];
199 gezelter 483 double p_local[9], p_global[9];
200 mmeineke 614 int i, j, k, nMols;
201 gezelter 468 Molecule* molecules;
202    
203 mmeineke 614 nMols = info->n_mol;
204     molecules = info->molecules;
205     //tau = info->tau;
206 gezelter 468
207     // use velocities of molecular centers of mass and molecular masses:
208 gezelter 483 for (i=0; i < 9; i++) {
209     p_local[i] = 0.0;
210     p_global[i] = 0.0;
211     }
212 gezelter 475
213 gezelter 468 for (i=0; i < nMols; i++) {
214 gezelter 475 molmass = molecules[i].getCOMvel(vcom);
215 gezelter 483
216     p_local[0] += molmass * (vcom[0] * vcom[0]);
217     p_local[1] += molmass * (vcom[0] * vcom[1]);
218     p_local[2] += molmass * (vcom[0] * vcom[2]);
219     p_local[3] += molmass * (vcom[1] * vcom[0]);
220     p_local[4] += molmass * (vcom[1] * vcom[1]);
221     p_local[5] += molmass * (vcom[1] * vcom[2]);
222     p_local[6] += molmass * (vcom[2] * vcom[0]);
223     p_local[7] += molmass * (vcom[2] * vcom[1]);
224     p_local[8] += molmass * (vcom[2] * vcom[2]);
225 gezelter 468 }
226    
227     // Get total for entire system from MPI.
228 chuckv 479
229 gezelter 468 #ifdef IS_MPI
230 gezelter 483 MPI_Allreduce(p_local,p_global,9,MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
231 gezelter 468 #else
232 gezelter 483 for (i=0; i<9; i++) {
233     p_global[i] = p_local[i];
234     }
235 gezelter 468 #endif // is_mpi
236    
237 gezelter 611 volume = this->getVolume();
238 gezelter 468
239 gezelter 588 for(i = 0; i < 3; i++) {
240     for (j = 0; j < 3; j++) {
241     k = 3*i + j;
242 mmeineke 614 press[i][j] = (p_global[k] + info->tau[k]*e_convert) / volume;
243    
244 gezelter 588 }
245 gezelter 483 }
246 mmeineke 377 }
247    
248     void Thermo::velocitize() {
249    
250 gezelter 608 double aVel[3], aJ[3], I[3][3];
251 tim 1127 int i, j, l, m, n, vr, vd; // velocity randomizer loop counters
252 chuckv 403 double vdrift[3];
253 mmeineke 377 double vbar;
254     const double kb = 8.31451e-7; // kb in amu, angstroms, fs, etc.
255     double av2;
256     double kebar;
257     double temperature;
258 tim 1127 int nobj;
259 mmeineke 377
260 tim 1127 nobj = info->integrableObjects.size();
261    
262 mmeineke 614 temperature = info->target_temp;
263 mmeineke 377
264 tim 763 kebar = kb * temperature * (double)info->ndfRaw /
265     ( 2.0 * (double)info->ndf );
266 chuckv 403
267 tim 1127 for(vr = 0; vr < nobj; vr++){
268 mmeineke 377
269     // uses equipartition theory to solve for vbar in angstrom/fs
270    
271 tim 1127 av2 = 2.0 * kebar / info->integrableObjects[vr]->getMass();
272 mmeineke 377 vbar = sqrt( av2 );
273 mmeineke 853
274 mmeineke 377 // picks random velocities from a gaussian distribution
275     // centered on vbar
276    
277 gezelter 608 for (j=0; j<3; j++)
278     aVel[j] = vbar * gaussStream->getGaussian();
279    
280 tim 1127 info->integrableObjects[vr]->setVel( aVel );
281    
282     if(info->integrableObjects[vr]->isDirectional()){
283 mmeineke 377
284 tim 1127 info->integrableObjects[vr]->getI( I );
285    
286     if (info->integrableObjects[vr]->isLinear()) {
287    
288     l= info->integrableObjects[vr]->linearAxis();
289     m = (l+1)%3;
290     n = (l+2)%3;
291    
292     aJ[l] = 0.0;
293     vbar = sqrt( 2.0 * kebar * I[m][m] );
294     aJ[m] = vbar * gaussStream->getGaussian();
295     vbar = sqrt( 2.0 * kebar * I[n][n] );
296     aJ[n] = vbar * gaussStream->getGaussian();
297    
298     } else {
299     for (j = 0 ; j < 3; j++) {
300     vbar = sqrt( 2.0 * kebar * I[j][j] );
301     aJ[j] = vbar * gaussStream->getGaussian();
302     }
303     } // else isLinear
304    
305     info->integrableObjects[vr]->setJ( aJ );
306    
307     }//isDirectional
308    
309 mmeineke 377 }
310 chuckv 401
311     // Get the Center of Mass drift velocity.
312    
313 chuckv 403 getCOMVel(vdrift);
314 mmeineke 377
315     // Corrects for the center of mass drift.
316     // sums all the momentum and divides by total mass.
317    
318 tim 1127 for(vd = 0; vd < nobj; vd++){
319 mmeineke 377
320 tim 1127 info->integrableObjects[vd]->getVel(aVel);
321 gezelter 608
322     for (j=0; j < 3; j++)
323     aVel[j] -= vdrift[j];
324 chuckv 401
325 tim 1127 info->integrableObjects[vd]->setVel( aVel );
326 mmeineke 377 }
327    
328     }
329 chuckv 401
330 chuckv 403 void Thermo::getCOMVel(double vdrift[3]){
331 chuckv 401
332     double mtot, mtot_local;
333 gezelter 608 double aVel[3], amass;
334 chuckv 401 double vdrift_local[3];
335 tim 1127 int vd, j;
336     int nobj;
337 chuckv 401
338 tim 1127 nobj = info->integrableObjects.size();
339 chuckv 401
340     mtot_local = 0.0;
341     vdrift_local[0] = 0.0;
342     vdrift_local[1] = 0.0;
343     vdrift_local[2] = 0.0;
344    
345 tim 1127 for(vd = 0; vd < nobj; vd++){
346 chuckv 401
347 tim 1127 amass = info->integrableObjects[vd]->getMass();
348     info->integrableObjects[vd]->getVel( aVel );
349 gezelter 608
350     for(j = 0; j < 3; j++)
351     vdrift_local[j] += aVel[j] * amass;
352 chuckv 401
353 gezelter 608 mtot_local += amass;
354 chuckv 401 }
355    
356     #ifdef IS_MPI
357 mmeineke 447 MPI_Allreduce(&mtot_local,&mtot,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
358     MPI_Allreduce(vdrift_local,vdrift,3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
359 chuckv 401 #else
360     mtot = mtot_local;
361     for(vd = 0; vd < 3; vd++) {
362     vdrift[vd] = vdrift_local[vd];
363     }
364     #endif
365    
366     for (vd = 0; vd < 3; vd++) {
367     vdrift[vd] = vdrift[vd] / mtot;
368     }
369    
370     }
371    
372 tim 763 void Thermo::getCOM(double COM[3]){
373    
374     double mtot, mtot_local;
375     double aPos[3], amass;
376     double COM_local[3];
377 tim 1127 int i, j;
378     int nobj;
379 tim 763
380     mtot_local = 0.0;
381     COM_local[0] = 0.0;
382     COM_local[1] = 0.0;
383     COM_local[2] = 0.0;
384 tim 1127
385     nobj = info->integrableObjects.size();
386     for(i = 0; i < nobj; i++){
387 tim 763
388 tim 1127 amass = info->integrableObjects[i]->getMass();
389     info->integrableObjects[i]->getPos( aPos );
390 tim 763
391     for(j = 0; j < 3; j++)
392     COM_local[j] += aPos[j] * amass;
393    
394     mtot_local += amass;
395     }
396    
397     #ifdef IS_MPI
398     MPI_Allreduce(&mtot_local,&mtot,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
399     MPI_Allreduce(COM_local,COM,3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
400     #else
401     mtot = mtot_local;
402     for(i = 0; i < 3; i++) {
403     COM[i] = COM_local[i];
404     }
405     #endif
406    
407     for (i = 0; i < 3; i++) {
408     COM[i] = COM[i] / mtot;
409     }
410     }
411 tim 1127
412     void Thermo::removeCOMdrift() {
413     double vdrift[3], aVel[3];
414     int vd, j, nobj;
415    
416     nobj = info->integrableObjects.size();
417    
418     // Get the Center of Mass drift velocity.
419    
420     getCOMVel(vdrift);
421    
422     // Corrects for the center of mass drift.
423     // sums all the momentum and divides by total mass.
424    
425     for(vd = 0; vd < nobj; vd++){
426    
427     info->integrableObjects[vd]->getVel(aVel);
428    
429     for (j=0; j < 3; j++)
430     aVel[j] -= vdrift[j];
431    
432     info->integrableObjects[vd]->setVel( aVel );
433     }
434     }