ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Thermo.cpp
Revision: 787
Committed: Thu Sep 25 19:27:15 2003 UTC (20 years, 9 months ago) by mmeineke
File size: 9393 byte(s)
Log Message:
cleaned things with gcc -Wall and g++ -Wall

File Contents

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