ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Thermo.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/Thermo.cpp (file contents):
Revision 582 by mmeineke, Wed Jul 9 15:33:46 2003 UTC vs.
Revision 1127 by tim, Tue Apr 20 16:56:40 2004 UTC

# Line 1 | Line 1
1 < #include <cmath>
1 > #include <math.h>
2   #include <iostream>
3   using namespace std;
4  
# Line 16 | Line 16 | using namespace std;
16   #include "mpiSimulation.hpp"
17   #endif // is_mpi
18  
19 <
20 < #define BASE_SEED 123456789
21 <
22 < Thermo::Thermo( SimInfo* the_entry_plug ) {
23 <  entry_plug = the_entry_plug;
24 <  int baseSeed = BASE_SEED;
19 > Thermo::Thermo( SimInfo* the_info ) {
20 >  info = the_info;
21 >  int baseSeed = the_info->getSeed();
22    
23    gaussStream = new gaussianSPRNG( baseSeed );
24   }
# Line 33 | Line 30 | double Thermo::getKinetic(){
30   double Thermo::getKinetic(){
31  
32    const double e_convert = 4.184E-4; // convert kcal/mol -> (amu A^2)/fs^2
33 <  double vx2, vy2, vz2;
34 <  double kinetic, v_sqr;
35 <  int kl;
36 <  double jx2, jy2, jz2; // the square of the angular momentums
33 >  double kinetic;
34 >  double amass;
35 >  double aVel[3], aJ[3], I[3][3];
36 >  int i, j, k, kl;
37  
41  DirectionalAtom *dAtom;
42
43  int n_atoms;
38    double kinetic_global;
39 <  Atom** atoms;
46 <
39 >  vector<StuntDouble *> integrableObjects = info->integrableObjects;
40    
48  n_atoms = entry_plug->n_atoms;
49  atoms = entry_plug->atoms;
50
41    kinetic = 0.0;
42    kinetic_global = 0.0;
53  for( kl=0; kl < n_atoms; kl++ ){
43  
44 <    vx2 = atoms[kl]->get_vx() * atoms[kl]->get_vx();
45 <    vy2 = atoms[kl]->get_vy() * atoms[kl]->get_vy();
46 <    vz2 = atoms[kl]->get_vz() * atoms[kl]->get_vz();
44 >  for (kl=0; kl<integrableObjects.size(); kl++) {
45 >    integrableObjects[kl]->getVel(aVel);
46 >    amass = integrableObjects[kl]->getMass();
47  
48 <    v_sqr = vx2 + vy2 + vz2;
49 <    kinetic += atoms[kl]->getMass() * v_sqr;
48 >   for(j=0; j<3; j++)
49 >      kinetic += amass*aVel[j]*aVel[j];
50  
51 <    if( atoms[kl]->isDirectional() ){
52 <            
53 <      dAtom = (DirectionalAtom *)atoms[kl];
54 <      
55 <      jx2 = dAtom->getJx() * dAtom->getJx();    
56 <      jy2 = dAtom->getJy() * dAtom->getJy();
57 <      jz2 = dAtom->getJz() * dAtom->getJz();
58 <      
59 <      kinetic += (jx2 / dAtom->getIxx()) + (jy2 / dAtom->getIyy())
60 <        + (jz2 / dAtom->getIzz());
61 <    }
51 >   if (integrableObjects[kl]->isDirectional()){
52 >
53 >      integrableObjects[kl]->getJ( aJ );
54 >      integrableObjects[kl]->getI( I );
55 >
56 >      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 >        for (j=0; j<3; j++)
63 >          kinetic += aJ[j]*aJ[j] / I[j][j];
64 >      }
65 >   }
66    }
67   #ifdef IS_MPI
68    MPI_Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE,
69                  MPI_SUM, MPI_COMM_WORLD);
70    kinetic = kinetic_global;
71   #endif //is_mpi
72 <
72 >  
73    kinetic = kinetic * 0.5 / e_convert;
74  
75    return kinetic;
# Line 89 | Line 82 | double Thermo::getPotential(){
82    int el, nSRI;
83    Molecule* molecules;
84  
85 <  molecules = entry_plug->molecules;
86 <  nSRI = entry_plug->n_SRI;
85 >  molecules = info->molecules;
86 >  nSRI = info->n_SRI;
87  
88    potential_local = 0.0;
89    potential = 0.0;
90 <  potential_local += entry_plug->lrPot;
90 >  potential_local += info->lrPot;
91  
92 <  for( el=0; el<entry_plug->n_mol; el++ ){    
92 >  for( el=0; el<info->n_mol; el++ ){    
93      potential_local += molecules[el].getPotential();
94    }
95  
# Line 108 | Line 101 | double Thermo::getPotential(){
101    potential = potential_local;
102   #endif // is_mpi
103  
111 #ifdef IS_MPI
112  /*
113  std::cerr << "node " << worldRank << ": after pot = " << potential << "\n";
114  */
115 #endif
116
104    return potential;
105   }
106  
# Line 127 | Line 114 | double Thermo::getTemperature(){
114  
115   double Thermo::getTemperature(){
116  
117 <  const double kb = 1.9872179E-3; // boltzman's constant in kcal/(mol K)
117 >  const double kb = 1.9872156E-3; // boltzman's constant in kcal/(mol K)
118    double temperature;
119 <  
120 <  temperature = ( 2.0 * this->getKinetic() ) / ((double)entry_plug->ndf * kb );
119 >
120 >  temperature = ( 2.0 * this->getKinetic() ) / ((double)info->ndf * kb );
121    return temperature;
122   }
123  
124 < double Thermo::getEnthalpy() {
124 > double Thermo::getVolume() {
125  
126 <  const double e_convert = 4.184E-4; // convert kcal/mol -> (amu A^2)/fs^2
127 <  double u, p, v;
141 <  double press[9];
126 >  return info->boxVol;
127 > }
128  
129 <  u = this->getTotalE();
129 > double Thermo::getPressure() {
130  
131 <  this->getPressureTensor(press);
132 <  p = (press[0] + press[4] + press[8]) / 3.0;
133 <
134 <  v = this->getVolume();
131 >  // Relies on the calculation of the full molecular pressure tensor
132 >  
133 >  const double p_convert = 1.63882576e8;
134 >  double press[3][3];
135 >  double pressure;
136  
137 <  return (u + (p*v)/e_convert);
137 >  this->getPressureTensor(press);
138 >
139 >  pressure = p_convert * (press[0][0] + press[1][1] + press[2][2]) / 3.0;
140 >
141 >  return pressure;
142   }
143  
144 < double Thermo::getVolume() {
144 > double Thermo::getPressureX() {
145  
146 <  return entry_plug->boxVol;
146 >  // 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::getPressure() {
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[9];
165 <  double pressure;
164 >  double press[3][3];
165 >  double pressureY;
166  
167    this->getPressureTensor(press);
168  
169 <  pressure = p_convert * (press[0] + press[4] + press[8]) / 3.0;
169 >  pressureY = p_convert * press[1][1];
170  
171 <  return pressure;
171 >  return pressureY;
172   }
173  
174 + double Thermo::getPressureZ() {
175  
176 < void Thermo::getPressureTensor(double press[9]){
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 > void Thermo::getPressureTensor(double press[3][3]){
191    // returns pressure tensor in units amu*fs^-2*Ang^-1
192    // routine derived via viral theorem description in:
193    // Paci, E. and Marchi, M. J.Phys.Chem. 1996, 100, 4314-4322
# Line 181 | Line 197 | void Thermo::getPressureTensor(double press[9]){
197    double molmass, volume;
198    double vcom[3];
199    double p_local[9], p_global[9];
200 <  double theBox[3];
185 <  //double* tau;
186 <  int i, nMols;
200 >  int i, j, k, nMols;
201    Molecule* molecules;
202  
203 <  nMols = entry_plug->n_mol;
204 <  molecules = entry_plug->molecules;
205 <  //tau = entry_plug->tau;
203 >  nMols = info->n_mol;
204 >  molecules = info->molecules;
205 >  //tau = info->tau;
206  
207    // use velocities of molecular centers of mass and molecular masses:
208    for (i=0; i < 9; i++) {    
# Line 220 | Line 234 | void Thermo::getPressureTensor(double press[9]){
234    }
235   #endif // is_mpi
236  
237 <  volume = entry_plug->boxVol;
237 >  volume = this->getVolume();
238  
239 <  for(i=0; i<9; i++) {
240 <    press[i] = (p_global[i] - entry_plug->tau[i]*e_convert) / volume;
239 >  for(i = 0; i < 3; i++) {
240 >    for (j = 0; j < 3; j++) {
241 >      k = 3*i + j;
242 >      press[i][j] = (p_global[k] + info->tau[k]*e_convert) / volume;
243 >
244 >    }
245    }
246   }
247  
248   void Thermo::velocitize() {
249    
250 <  double x,y;
251 <  double vx, vy, vz;
234 <  double jx, jy, jz;
235 <  int i, vr, vd; // velocity randomizer loop counters
250 >  double aVel[3], aJ[3], I[3][3];
251 >  int i, j, l, m, n, vr, vd; // velocity randomizer loop counters
252    double vdrift[3];
253    double vbar;
254    const double kb = 8.31451e-7; // kb in amu, angstroms, fs, etc.
255    double av2;
256    double kebar;
241  int n_atoms;
242  Atom** atoms;
243  DirectionalAtom* dAtom;
257    double temperature;
258 <  int n_oriented;
246 <  int n_constraints;
258 >  int nobj;
259  
260 <  atoms         = entry_plug->atoms;
249 <  n_atoms       = entry_plug->n_atoms;
250 <  temperature   = entry_plug->target_temp;
251 <  n_oriented    = entry_plug->n_oriented;
252 <  n_constraints = entry_plug->n_constraints;
253 <  
254 <  kebar = kb * temperature * (double)entry_plug->ndf /
255 <    ( 2.0 * (double)entry_plug->ndfRaw );
260 >  nobj = info->integrableObjects.size();
261    
262 <  for(vr = 0; vr < n_atoms; vr++){
262 >  temperature   = info->target_temp;
263 >  
264 >  kebar = kb * temperature * (double)info->ndfRaw /
265 >    ( 2.0 * (double)info->ndf );
266 >  
267 >  for(vr = 0; vr < nobj; vr++){
268      
269      // uses equipartition theory to solve for vbar in angstrom/fs
270  
271 <    av2 = 2.0 * kebar / atoms[vr]->getMass();
271 >    av2 = 2.0 * kebar / info->integrableObjects[vr]->getMass();
272      vbar = sqrt( av2 );
273 <
264 < //     vbar = sqrt( 8.31451e-7 * temperature / atoms[vr]->getMass() );
265 <    
273 >
274      // picks random velocities from a gaussian distribution
275      // centered on vbar
276  
277 <    vx = vbar * gaussStream->getGaussian();
278 <    vy = vbar * gaussStream->getGaussian();
279 <    vz = vbar * gaussStream->getGaussian();
277 >    for (j=0; j<3; j++)
278 >      aVel[j] = vbar * gaussStream->getGaussian();
279 >    
280 >    info->integrableObjects[vr]->setVel( aVel );
281 >    
282 >    if(info->integrableObjects[vr]->isDirectional()){
283  
284 <    atoms[vr]->set_vx( vx );
285 <    atoms[vr]->set_vy( vy );
286 <    atoms[vr]->set_vz( vz );
284 >      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    }
310  
311    // Get the Center of Mass drift velocity.
# Line 282 | Line 315 | void Thermo::velocitize() {
315    //  Corrects for the center of mass drift.
316    // sums all the momentum and divides by total mass.
317  
318 <  for(vd = 0; vd < n_atoms; vd++){
318 >  for(vd = 0; vd < nobj; vd++){
319      
320 <    vx = atoms[vd]->get_vx();
288 <    vy = atoms[vd]->get_vy();
289 <    vz = atoms[vd]->get_vz();
290 <        
291 <    vx -= vdrift[0];
292 <    vy -= vdrift[1];
293 <    vz -= vdrift[2];
320 >    info->integrableObjects[vd]->getVel(aVel);
321      
322 <    atoms[vd]->set_vx(vx);
323 <    atoms[vd]->set_vy(vy);
297 <    atoms[vd]->set_vz(vz);
298 <  }
299 <  if( n_oriented ){
300 <  
301 <    for( i=0; i<n_atoms; i++ ){
302 <      
303 <      if( atoms[i]->isDirectional() ){
304 <        
305 <        dAtom = (DirectionalAtom *)atoms[i];
306 <
307 <        vbar = sqrt( 2.0 * kebar * dAtom->getIxx() );
308 <        jx = vbar * gaussStream->getGaussian();
309 <
310 <        vbar = sqrt( 2.0 * kebar * dAtom->getIyy() );
311 <        jy = vbar * gaussStream->getGaussian();
322 >    for (j=0; j < 3; j++)
323 >      aVel[j] -= vdrift[j];
324          
325 <        vbar = sqrt( 2.0 * kebar * dAtom->getIzz() );
314 <        jz = vbar * gaussStream->getGaussian();
315 <        
316 <        dAtom->setJx( jx );
317 <        dAtom->setJy( jy );
318 <        dAtom->setJz( jz );
319 <      }
320 <    }  
325 >    info->integrableObjects[vd]->setVel( aVel );
326    }
327 +
328   }
329  
330   void Thermo::getCOMVel(double vdrift[3]){
331  
332    double mtot, mtot_local;
333 +  double aVel[3], amass;
334    double vdrift_local[3];
335 <  int vd, n_atoms;
336 <  Atom** atoms;
335 >  int vd, j;
336 >  int nobj;
337  
338 <  // We are very careless here with the distinction between n_atoms and n_local
332 <  // We should really fix this before someone pokes an eye out.
338 >  nobj   = info->integrableObjects.size();
339  
334  n_atoms = entry_plug->n_atoms;  
335  atoms   = entry_plug->atoms;
336
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 <  for(vd = 0; vd < n_atoms; vd++){
345 >  for(vd = 0; vd < nobj; vd++){
346      
347 <    vdrift_local[0] += atoms[vd]->get_vx() * atoms[vd]->getMass();
348 <    vdrift_local[1] += atoms[vd]->get_vy() * atoms[vd]->getMass();
349 <    vdrift_local[2] += atoms[vd]->get_vz() * atoms[vd]->getMass();
347 >    amass = info->integrableObjects[vd]->getMass();
348 >    info->integrableObjects[vd]->getVel( aVel );
349 >
350 >    for(j = 0; j < 3; j++)
351 >      vdrift_local[j] += aVel[j] * amass;
352      
353 <    mtot_local += atoms[vd]->getMass();
353 >    mtot_local += amass;
354    }
355  
356   #ifdef IS_MPI
# Line 364 | Line 369 | void Thermo::getCOMVel(double vdrift[3]){
369    
370   }
371  
372 + void Thermo::getCOM(double COM[3]){
373 +
374 +  double mtot, mtot_local;
375 +  double aPos[3], amass;
376 +  double COM_local[3];
377 +  int i, j;
378 +  int nobj;
379 +
380 +  mtot_local = 0.0;
381 +  COM_local[0] = 0.0;
382 +  COM_local[1] = 0.0;
383 +  COM_local[2] = 0.0;
384 +
385 +  nobj = info->integrableObjects.size();
386 +  for(i = 0; i < nobj; i++){
387 +    
388 +    amass = info->integrableObjects[i]->getMass();
389 +    info->integrableObjects[i]->getPos( aPos );
390 +
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 +
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 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines