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 378 by mmeineke, Fri Mar 21 17:42:12 2003 UTC vs.
Revision 1118 by tim, Mon Apr 19 03:52:27 2004 UTC

# Line 1 | Line 1
1 < #include <cmath>
1 > #include <math.h>
2   #include <iostream>
3   using namespace std;
4  
5   #ifdef IS_MPI
6   #include <mpi.h>
7 #include <mpi++.h>
7   #endif //is_mpi
8  
9   #include "Thermo.hpp"
10   #include "SRI.hpp"
11   #include "Integrator.hpp"
12 + #include "simError.h"
13  
14 < #define BASE_SEED 123456789
14 > #ifdef IS_MPI
15 > #define __C
16 > #include "mpiSimulation.hpp"
17 > #endif // is_mpi
18  
19 < Thermo::Thermo( SimInfo* the_entry_plug ) {
20 <  entry_plug = the_entry_plug;
21 <  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 27 | 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  
35  DirectionalAtom *dAtom;
36
37  int n_atoms;
38    double kinetic_global;
39 <  Atom** atoms;
40 <
39 >  vector<StuntDouble *> integrableObjects = info->integrableObjects;
40    
42  n_atoms = entry_plug->n_atoms;
43  atoms = entry_plug->atoms;
44
41    kinetic = 0.0;
42    kinetic_global = 0.0;
47  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());
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::COMM_WORLD.Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE,MPI_SUM);
68 >  MPI_Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE,
69 >                MPI_SUM, MPI_COMM_WORLD);
70    kinetic = kinetic_global;
71   #endif //is_mpi
72  
# Line 77 | Line 77 | double Thermo::getPotential(){
77  
78   double Thermo::getPotential(){
79    
80 +  double potential_local;
81    double potential;
81  double potential_global;
82    int el, nSRI;
83 <  SRI** sris;
83 >  Molecule* molecules;
84  
85 <  sris = entry_plug->sr_interactions;
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_global = 0.0;
90 <  potential += entry_plug->lrPot;
90 >  potential_local += info->lrPot;
91  
92 <  for( el=0; el<nSRI; el++ ){
93 <    
94 <    potential += sris[el]->get_potential();
92 >  for( el=0; el<info->n_mol; el++ ){    
93 >    potential_local += molecules[el].getPotential();
94    }
95  
96    // Get total potential for entire system from MPI.
97   #ifdef IS_MPI
98 <  MPI::COMM_WORLD.Allreduce(&potential,&potential_global,1,MPI_DOUBLE,MPI_SUM);
99 <  potential = potential_global;
100 <
98 >  MPI_Allreduce(&potential_local,&potential,1,MPI_DOUBLE,
99 >                MPI_SUM, MPI_COMM_WORLD);
100 > #else
101 >  potential = potential_local;
102   #endif // is_mpi
103  
104    return potential;
# Line 114 | 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  int ndf = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented
121    - entry_plug->n_constraints - 3;
119  
120 <  temperature = ( 2.0 * this->getKinetic() ) / ( ndf * kb );
120 >  temperature = ( 2.0 * this->getKinetic() ) / ((double)info->ndf * kb );
121    return temperature;
122   }
123  
124 < double Thermo::getPressure(){
124 > double Thermo::getVolume() {
125  
126 < //  const double conv_Pa_atm = 9.901E-6; // convert Pa -> atm
127 < // const double conv_internal_Pa = 1.661E-7; //convert amu/(fs^2 A) -> Pa
131 < //  const double conv_A_m = 1.0E-10; //convert A -> m
126 >  return info->boxVol;
127 > }
128  
129 <  return 0.0;
129 > double Thermo::getPressure() {
130 >
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 >  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::getPressureX() {
145 >
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::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 > 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
194 >
195 >  const double e_convert = 4.184e-4;
196 >
197 >  double molmass, volume;
198 >  double vcom[3];
199 >  double p_local[9], p_global[9];
200 >  int i, j, k, nMols;
201 >  Molecule* molecules;
202 >
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++) {    
209 >    p_local[i] = 0.0;
210 >    p_global[i] = 0.0;
211 >  }
212 >
213 >  for (i=0; i < nMols; i++) {
214 >    molmass = molecules[i].getCOMvel(vcom);
215 >
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 >  }
226 >
227 >  // Get total for entire system from MPI.
228 >
229 > #ifdef IS_MPI
230 >  MPI_Allreduce(p_local,p_global,9,MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
231 > #else
232 >  for (i=0; i<9; i++) {
233 >    p_global[i] = p_local[i];
234 >  }
235 > #endif // is_mpi
236 >
237 >  volume = this->getVolume();
238 >
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;
140 <  double jx, jy, jz;
141 <  int i, vr, vd; // velocity randomizer loop counters
250 >  double aVel[3], aJ[3], I[3][3];
251 >  int i, j, vr, vd; // velocity randomizer loop counters
252    double vdrift[3];
143  double mtot = 0.0;
253    double vbar;
254    const double kb = 8.31451e-7; // kb in amu, angstroms, fs, etc.
255    double av2;
256    double kebar;
148  int ndf; // number of degrees of freedom
149  int ndfRaw; // the raw number of degrees of freedom
257    int n_atoms;
258    Atom** atoms;
259    DirectionalAtom* dAtom;
# Line 154 | Line 261 | void Thermo::velocitize() {
261    int n_oriented;
262    int n_constraints;
263  
264 <  atoms         = entry_plug->atoms;
265 <  n_atoms       = entry_plug->n_atoms;
266 <  temperature   = entry_plug->target_temp;
267 <  n_oriented    = entry_plug->n_oriented;
268 <  n_constraints = entry_plug->n_constraints;
264 >  atoms         = info->atoms;
265 >  n_atoms       = info->n_atoms;
266 >  temperature   = info->target_temp;
267 >  n_oriented    = info->n_oriented;
268 >  n_constraints = info->n_constraints;
269    
270 <
271 <  ndfRaw = 3 * n_atoms + 3 * n_oriented;
165 <  ndf = ndfRaw - n_constraints - 3;
166 <  kebar = kb * temperature * (double)ndf / ( 2.0 * (double)ndfRaw );
270 >  kebar = kb * temperature * (double)info->ndfRaw /
271 >    ( 2.0 * (double)info->ndf );
272    
273    for(vr = 0; vr < n_atoms; vr++){
274      
# Line 172 | Line 277 | void Thermo::velocitize() {
277      av2 = 2.0 * kebar / atoms[vr]->getMass();
278      vbar = sqrt( av2 );
279  
175 //     vbar = sqrt( 8.31451e-7 * temperature / atoms[vr]->getMass() );
176    
280      // picks random velocities from a gaussian distribution
281      // centered on vbar
282  
283 <    vx = vbar * gaussStream->getGaussian();
284 <    vy = vbar * gaussStream->getGaussian();
285 <    vz = vbar * gaussStream->getGaussian();
283 >    for (j=0; j<3; j++)
284 >      aVel[j] = vbar * gaussStream->getGaussian();
285 >    
286 >    atoms[vr]->setVel( aVel );
287  
184    atoms[vr]->set_vx( vx );
185    atoms[vr]->set_vy( vy );
186    atoms[vr]->set_vz( vz );
288    }
289 +
290 +  // Get the Center of Mass drift velocity.
291 +
292 +  getCOMVel(vdrift);
293    
294    //  Corrects for the center of mass drift.
295    // sums all the momentum and divides by total mass.
191  
192  mtot = 0.0;
193  vdrift[0] = 0.0;
194  vdrift[1] = 0.0;
195  vdrift[2] = 0.0;
196  for(vd = 0; vd < n_atoms; vd++){
197    
198    vdrift[0] += atoms[vd]->get_vx() * atoms[vd]->getMass();
199    vdrift[1] += atoms[vd]->get_vy() * atoms[vd]->getMass();
200    vdrift[2] += atoms[vd]->get_vz() * atoms[vd]->getMass();
201    
202    mtot += atoms[vd]->getMass();
203  }
204  
205  for (vd = 0; vd < 3; vd++) {
206    vdrift[vd] = vdrift[vd] / mtot;
207  }
208  
296  
297    for(vd = 0; vd < n_atoms; vd++){
298      
299 <    vx = atoms[vd]->get_vx();
213 <    vy = atoms[vd]->get_vy();
214 <    vz = atoms[vd]->get_vz();
299 >    atoms[vd]->getVel(aVel);
300      
301 <    
302 <    vx -= vdrift[0];
303 <    vy -= vdrift[1];
304 <    vz -= vdrift[2];
220 <    
221 <    atoms[vd]->set_vx(vx);
222 <    atoms[vd]->set_vy(vy);
223 <    atoms[vd]->set_vz(vz);
301 >    for (j=0; j < 3; j++)
302 >      aVel[j] -= vdrift[j];
303 >        
304 >    atoms[vd]->setVel( aVel );
305    }
306    if( n_oriented ){
307    
# Line 229 | Line 310 | void Thermo::velocitize() {
310        if( atoms[i]->isDirectional() ){
311          
312          dAtom = (DirectionalAtom *)atoms[i];
313 +        dAtom->getI( I );
314 +        
315 +        for (j = 0 ; j < 3; j++) {
316  
317 <        vbar = sqrt( 2.0 * kebar * dAtom->getIxx() );
318 <        jx = vbar * gaussStream->getGaussian();
317 >          vbar = sqrt( 2.0 * kebar * I[j][j] );
318 >          aJ[j] = vbar * gaussStream->getGaussian();
319  
320 <        vbar = sqrt( 2.0 * kebar * dAtom->getIyy() );
237 <        jy = vbar * gaussStream->getGaussian();
320 >        }      
321  
322 <        vbar = sqrt( 2.0 * kebar * dAtom->getIzz() );
323 <        jz = vbar * gaussStream->getGaussian();
241 <        
242 <        dAtom->setJx( jx );
243 <        dAtom->setJy( jy );
244 <        dAtom->setJz( jz );
322 >        dAtom->setJ( aJ );
323 >
324        }
325      }  
326    }
327   }
328 +
329 + void Thermo::getCOMVel(double vdrift[3]){
330 +
331 +  double mtot, mtot_local;
332 +  double aVel[3], amass;
333 +  double vdrift_local[3];
334 +  int vd, n_atoms, j;
335 +  Atom** atoms;
336 +
337 +  // We are very careless here with the distinction between n_atoms and n_local
338 +  // We should really fix this before someone pokes an eye out.
339 +
340 +  n_atoms = info->n_atoms;  
341 +  atoms   = info->atoms;
342 +
343 +  mtot_local = 0.0;
344 +  vdrift_local[0] = 0.0;
345 +  vdrift_local[1] = 0.0;
346 +  vdrift_local[2] = 0.0;
347 +  
348 +  for(vd = 0; vd < n_atoms; vd++){
349 +    
350 +    amass = atoms[vd]->getMass();
351 +    atoms[vd]->getVel( aVel );
352 +
353 +    for(j = 0; j < 3; j++)
354 +      vdrift_local[j] += aVel[j] * amass;
355 +    
356 +    mtot_local += amass;
357 +  }
358 +
359 + #ifdef IS_MPI
360 +  MPI_Allreduce(&mtot_local,&mtot,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
361 +  MPI_Allreduce(vdrift_local,vdrift,3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
362 + #else
363 +  mtot = mtot_local;
364 +  for(vd = 0; vd < 3; vd++) {
365 +    vdrift[vd] = vdrift_local[vd];
366 +  }
367 + #endif
368 +    
369 +  for (vd = 0; vd < 3; vd++) {
370 +    vdrift[vd] = vdrift[vd] / mtot;
371 +  }
372 +  
373 + }
374 +
375 + void Thermo::getCOM(double COM[3]){
376 +
377 +  double mtot, mtot_local;
378 +  double aPos[3], amass;
379 +  double COM_local[3];
380 +  int i, n_atoms, j;
381 +  Atom** atoms;
382 +
383 +  // We are very careless here with the distinction between n_atoms and n_local
384 +  // We should really fix this before someone pokes an eye out.
385 +
386 +  n_atoms = info->n_atoms;  
387 +  atoms   = info->atoms;
388 +
389 +  mtot_local = 0.0;
390 +  COM_local[0] = 0.0;
391 +  COM_local[1] = 0.0;
392 +  COM_local[2] = 0.0;
393 +  
394 +  for(i = 0; i < n_atoms; i++){
395 +    
396 +    amass = atoms[i]->getMass();
397 +    atoms[i]->getPos( aPos );
398 +
399 +    for(j = 0; j < 3; j++)
400 +      COM_local[j] += aPos[j] * amass;
401 +    
402 +    mtot_local += amass;
403 +  }
404 +
405 + #ifdef IS_MPI
406 +  MPI_Allreduce(&mtot_local,&mtot,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
407 +  MPI_Allreduce(COM_local,COM,3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
408 + #else
409 +  mtot = mtot_local;
410 +  for(i = 0; i < 3; i++) {
411 +    COM[i] = COM_local[i];
412 +  }
413 + #endif
414 +    
415 +  for (i = 0; i < 3; i++) {
416 +    COM[i] = COM[i] / mtot;
417 +  }
418 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines