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 447 by mmeineke, Thu Apr 3 20:21:54 2003 UTC vs.
Revision 853 by mmeineke, Thu Nov 6 19:11:38 2003 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 j, kl;
37  
38    DirectionalAtom *dAtom;
39  
# Line 45 | Line 42 | double Thermo::getKinetic(){
42    Atom** atoms;
43  
44    
45 <  n_atoms = entry_plug->n_atoms;
46 <  atoms = entry_plug->atoms;
45 >  n_atoms = info->n_atoms;
46 >  atoms = info->atoms;
47  
48    kinetic = 0.0;
49    kinetic_global = 0.0;
50    for( kl=0; kl < n_atoms; kl++ ){
51 +    
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  
55    vx2 = atoms[kl]->get_vx() * atoms[kl]->get_vx();
56    vy2 = atoms[kl]->get_vy() * atoms[kl]->get_vy();
57    vz2 = atoms[kl]->get_vz() * atoms[kl]->get_vz();
58
59    v_sqr = vx2 + vy2 + vz2;
60    kinetic += atoms[kl]->getMass() * v_sqr;
61
58      if( atoms[kl]->isDirectional() ){
59              
60        dAtom = (DirectionalAtom *)atoms[kl];
61 +
62 +      dAtom->getJ( aJ );
63 +      dAtom->getI( I );
64        
65 <      jx2 = dAtom->getJx() * dAtom->getJx();    
66 <      jy2 = dAtom->getJy() * dAtom->getJy();
68 <      jz2 = dAtom->getJz() * dAtom->getJz();
65 >      for (j=0; j<3; j++)
66 >        kinetic += aJ[j]*aJ[j] / I[j][j];
67        
70      kinetic += (jx2 / dAtom->getIxx()) + (jy2 / dAtom->getIyy())
71        + (jz2 / dAtom->getIzz());
68      }
69    }
70   #ifdef IS_MPI
# Line 89 | Line 85 | double Thermo::getPotential(){
85    int el, nSRI;
86    Molecule* molecules;
87  
88 <  molecules = entry_plug->molecules;
89 <  nSRI = entry_plug->n_SRI;
88 >  molecules = info->molecules;
89 >  nSRI = info->n_SRI;
90  
91    potential_local = 0.0;
92    potential = 0.0;
93 <  potential_local += entry_plug->lrPot;
93 >  potential_local += info->lrPot;
94  
95 <  for( el=0; el<entry_plug->n_mol; el++ ){    
95 >  for( el=0; el<info->n_mol; el++ ){    
96      potential_local += molecules[el].getPotential();
97    }
98  
# Line 108 | Line 104 | double Thermo::getPotential(){
104    potential = potential_local;
105   #endif // is_mpi
106  
111 #ifdef IS_MPI
112  /*
113  std::cerr << "node " << worldRank << ": after pot = " << potential << "\n";
114  */
115 #endif
116
107    return potential;
108   }
109  
# Line 127 | Line 117 | double Thermo::getTemperature(){
117  
118   double Thermo::getTemperature(){
119  
120 <  const double kb = 1.9872179E-3; // boltzman's constant in kcal/(mol K)
120 >  const double kb = 1.9872156E-3; // boltzman's constant in kcal/(mol K)
121    double temperature;
132  int ndf_local, ndf;
122    
123 <  ndf_local = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented
124 <    - entry_plug->n_constraints;
123 >  temperature = ( 2.0 * this->getKinetic() ) / ((double)info->ndf * kb );
124 >  return temperature;
125 > }
126  
127 < #ifdef IS_MPI
138 <  MPI_Allreduce(&ndf_local,&ndf,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
139 < #else
140 <  ndf = ndf_local;
141 < #endif
127 > double Thermo::getVolume() {
128  
129 <  ndf = ndf - 3;
129 >  return info->boxVol;
130 > }
131 >
132 > double Thermo::getPressure() {
133 >
134 >  // Relies on the calculation of the full molecular pressure tensor
135    
136 <  temperature = ( 2.0 * this->getKinetic() ) / ( ndf * kb );
137 <  return temperature;
136 >  const double p_convert = 1.63882576e8;
137 >  double press[3][3];
138 >  double pressure;
139 >
140 >  this->getPressureTensor(press);
141 >
142 >  pressure = p_convert * (press[0][0] + press[1][1] + press[2][2]) / 3.0;
143 >
144 >  return pressure;
145   }
146  
147 < double Thermo::getPressure(){
148 <  // returns pressure in units amu*fs^-2*Ang^-1
147 > double Thermo::getPressureX() {
148 >
149 >  // Relies on the calculation of the full molecular pressure tensor
150 >  
151 >  const double p_convert = 1.63882576e8;
152 >  double press[3][3];
153 >  double pressureX;
154 >
155 >  this->getPressureTensor(press);
156 >
157 >  pressureX = p_convert * press[0][0];
158 >
159 >  return pressureX;
160 > }
161 >
162 > double Thermo::getPressureY() {
163 >
164 >  // Relies on the calculation of the full molecular pressure tensor
165 >  
166 >  const double p_convert = 1.63882576e8;
167 >  double press[3][3];
168 >  double pressureY;
169 >
170 >  this->getPressureTensor(press);
171 >
172 >  pressureY = p_convert * press[1][1];
173 >
174 >  return pressureY;
175 > }
176 >
177 > double Thermo::getPressureZ() {
178 >
179 >  // Relies on the calculation of the full molecular pressure tensor
180 >  
181 >  const double p_convert = 1.63882576e8;
182 >  double press[3][3];
183 >  double pressureZ;
184 >
185 >  this->getPressureTensor(press);
186 >
187 >  pressureZ = p_convert * press[2][2];
188 >
189 >  return pressureZ;
190 > }
191 >
192 >
193 > void Thermo::getPressureTensor(double press[3][3]){
194 >  // returns pressure tensor in units amu*fs^-2*Ang^-1
195    // routine derived via viral theorem description in:
196    // Paci, E. and Marchi, M. J.Phys.Chem. 1996, 100, 4314-4322
197  
198 <  return 0.0;
198 >  const double e_convert = 4.184e-4;
199 >
200 >  double molmass, volume;
201 >  double vcom[3];
202 >  double p_local[9], p_global[9];
203 >  int i, j, k, nMols;
204 >  Molecule* molecules;
205 >
206 >  nMols = info->n_mol;
207 >  molecules = info->molecules;
208 >  //tau = info->tau;
209 >
210 >  // use velocities of molecular centers of mass and molecular masses:
211 >  for (i=0; i < 9; i++) {    
212 >    p_local[i] = 0.0;
213 >    p_global[i] = 0.0;
214 >  }
215 >
216 >  for (i=0; i < nMols; i++) {
217 >    molmass = molecules[i].getCOMvel(vcom);
218 >
219 >    p_local[0] += molmass * (vcom[0] * vcom[0]);
220 >    p_local[1] += molmass * (vcom[0] * vcom[1]);
221 >    p_local[2] += molmass * (vcom[0] * vcom[2]);
222 >    p_local[3] += molmass * (vcom[1] * vcom[0]);
223 >    p_local[4] += molmass * (vcom[1] * vcom[1]);
224 >    p_local[5] += molmass * (vcom[1] * vcom[2]);
225 >    p_local[6] += molmass * (vcom[2] * vcom[0]);
226 >    p_local[7] += molmass * (vcom[2] * vcom[1]);
227 >    p_local[8] += molmass * (vcom[2] * vcom[2]);
228 >  }
229 >
230 >  // Get total for entire system from MPI.
231 >
232 > #ifdef IS_MPI
233 >  MPI_Allreduce(p_local,p_global,9,MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
234 > #else
235 >  for (i=0; i<9; i++) {
236 >    p_global[i] = p_local[i];
237 >  }
238 > #endif // is_mpi
239 >
240 >  volume = this->getVolume();
241 >
242 >  for(i = 0; i < 3; i++) {
243 >    for (j = 0; j < 3; j++) {
244 >      k = 3*i + j;
245 >      press[i][j] = (p_global[k] + info->tau[k]*e_convert) / volume;
246 >
247 >    }
248 >  }
249   }
250  
251   void Thermo::velocitize() {
252    
253 <  double x,y;
254 <  double vx, vy, vz;
161 <  double jx, jy, jz;
162 <  int i, vr, vd; // velocity randomizer loop counters
253 >  double aVel[3], aJ[3], I[3][3];
254 >  int i, j, vr, vd; // velocity randomizer loop counters
255    double vdrift[3];
256    double vbar;
257    const double kb = 8.31451e-7; // kb in amu, angstroms, fs, etc.
258    double av2;
259    double kebar;
168  int ndf, ndf_local; // number of degrees of freedom
169  int ndfRaw, ndfRaw_local; // the raw number of degrees of freedom
260    int n_atoms;
261    Atom** atoms;
262    DirectionalAtom* dAtom;
# Line 174 | Line 264 | void Thermo::velocitize() {
264    int n_oriented;
265    int n_constraints;
266  
267 <  atoms         = entry_plug->atoms;
268 <  n_atoms       = entry_plug->n_atoms;
269 <  temperature   = entry_plug->target_temp;
270 <  n_oriented    = entry_plug->n_oriented;
271 <  n_constraints = entry_plug->n_constraints;
267 >  atoms         = info->atoms;
268 >  n_atoms       = info->n_atoms;
269 >  temperature   = info->target_temp;
270 >  n_oriented    = info->n_oriented;
271 >  n_constraints = info->n_constraints;
272    
273 <  // Raw degrees of freedom that we have to set
274 <  ndfRaw_local = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented;
185 <
186 <  // Degrees of freedom that can contain kinetic energy
187 <  ndf_local = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented
188 <    - entry_plug->n_constraints;
273 >  kebar = kb * temperature * (double)info->ndfRaw /
274 >    ( 2.0 * (double)info->ndf );
275    
190 #ifdef IS_MPI
191  MPI_Allreduce(&ndf_local,&ndf,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
192  MPI_Allreduce(&ndfRaw_local,&ndfRaw,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
193 #else
194  ndfRaw = ndfRaw_local;
195  ndf = ndf_local;
196 #endif
197  ndf = ndf - 3;
198
199  kebar = kb * temperature * (double)ndf / ( 2.0 * (double)ndfRaw );
200  
276    for(vr = 0; vr < n_atoms; vr++){
277      
278      // uses equipartition theory to solve for vbar in angstrom/fs
279  
280      av2 = 2.0 * kebar / atoms[vr]->getMass();
281      vbar = sqrt( av2 );
282 <
208 < //     vbar = sqrt( 8.31451e-7 * temperature / atoms[vr]->getMass() );
209 <    
282 >
283      // picks random velocities from a gaussian distribution
284      // centered on vbar
285  
286 <    vx = vbar * gaussStream->getGaussian();
287 <    vy = vbar * gaussStream->getGaussian();
288 <    vz = vbar * gaussStream->getGaussian();
286 >    for (j=0; j<3; j++)
287 >      aVel[j] = vbar * gaussStream->getGaussian();
288 >    
289 >    atoms[vr]->setVel( aVel );
290  
217    atoms[vr]->set_vx( vx );
218    atoms[vr]->set_vy( vy );
219    atoms[vr]->set_vz( vz );
291    }
292  
293    // Get the Center of Mass drift velocity.
# Line 228 | Line 299 | void Thermo::velocitize() {
299  
300    for(vd = 0; vd < n_atoms; vd++){
301      
302 <    vx = atoms[vd]->get_vx();
232 <    vy = atoms[vd]->get_vy();
233 <    vz = atoms[vd]->get_vz();
234 <        
235 <    vx -= vdrift[0];
236 <    vy -= vdrift[1];
237 <    vz -= vdrift[2];
302 >    atoms[vd]->getVel(aVel);
303      
304 <    atoms[vd]->set_vx(vx);
305 <    atoms[vd]->set_vy(vy);
306 <    atoms[vd]->set_vz(vz);
304 >    for (j=0; j < 3; j++)
305 >      aVel[j] -= vdrift[j];
306 >        
307 >    atoms[vd]->setVel( aVel );
308    }
309    if( n_oriented ){
310    
# Line 247 | Line 313 | void Thermo::velocitize() {
313        if( atoms[i]->isDirectional() ){
314          
315          dAtom = (DirectionalAtom *)atoms[i];
316 +        dAtom->getI( I );
317 +        
318 +        for (j = 0 ; j < 3; j++) {
319  
320 <        vbar = sqrt( 2.0 * kebar * dAtom->getIxx() );
321 <        jx = vbar * gaussStream->getGaussian();
320 >          vbar = sqrt( 2.0 * kebar * I[j][j] );
321 >          aJ[j] = vbar * gaussStream->getGaussian();
322  
323 <        vbar = sqrt( 2.0 * kebar * dAtom->getIyy() );
255 <        jy = vbar * gaussStream->getGaussian();
323 >        }      
324  
325 <        vbar = sqrt( 2.0 * kebar * dAtom->getIzz() );
326 <        jz = vbar * gaussStream->getGaussian();
259 <        
260 <        dAtom->setJx( jx );
261 <        dAtom->setJy( jy );
262 <        dAtom->setJz( jz );
325 >        dAtom->setJ( aJ );
326 >
327        }
328      }  
329    }
# Line 268 | Line 332 | void Thermo::getCOMVel(double vdrift[3]){
332   void Thermo::getCOMVel(double vdrift[3]){
333  
334    double mtot, mtot_local;
335 +  double aVel[3], amass;
336    double vdrift_local[3];
337 <  int vd, n_atoms;
337 >  int vd, n_atoms, j;
338    Atom** atoms;
339  
340    // We are very careless here with the distinction between n_atoms and n_local
341    // We should really fix this before someone pokes an eye out.
342  
343 <  n_atoms = entry_plug->n_atoms;  
344 <  atoms   = entry_plug->atoms;
343 >  n_atoms = info->n_atoms;  
344 >  atoms   = info->atoms;
345  
346    mtot_local = 0.0;
347    vdrift_local[0] = 0.0;
# Line 285 | Line 350 | void Thermo::getCOMVel(double vdrift[3]){
350    
351    for(vd = 0; vd < n_atoms; vd++){
352      
353 <    vdrift_local[0] += atoms[vd]->get_vx() * atoms[vd]->getMass();
354 <    vdrift_local[1] += atoms[vd]->get_vy() * atoms[vd]->getMass();
355 <    vdrift_local[2] += atoms[vd]->get_vz() * atoms[vd]->getMass();
353 >    amass = atoms[vd]->getMass();
354 >    atoms[vd]->getVel( aVel );
355 >
356 >    for(j = 0; j < 3; j++)
357 >      vdrift_local[j] += aVel[j] * amass;
358      
359 <    mtot_local += atoms[vd]->getMass();
359 >    mtot_local += amass;
360    }
361  
362   #ifdef IS_MPI
# Line 308 | Line 375 | void Thermo::getCOMVel(double vdrift[3]){
375    
376   }
377  
378 + void Thermo::getCOM(double COM[3]){
379 +
380 +  double mtot, mtot_local;
381 +  double aPos[3], amass;
382 +  double COM_local[3];
383 +  int i, n_atoms, j;
384 +  Atom** atoms;
385 +
386 +  // We are very careless here with the distinction between n_atoms and n_local
387 +  // We should really fix this before someone pokes an eye out.
388 +
389 +  n_atoms = info->n_atoms;  
390 +  atoms   = info->atoms;
391 +
392 +  mtot_local = 0.0;
393 +  COM_local[0] = 0.0;
394 +  COM_local[1] = 0.0;
395 +  COM_local[2] = 0.0;
396 +  
397 +  for(i = 0; i < n_atoms; i++){
398 +    
399 +    amass = atoms[i]->getMass();
400 +    atoms[i]->getPos( aPos );
401 +
402 +    for(j = 0; j < 3; j++)
403 +      COM_local[j] += aPos[j] * amass;
404 +    
405 +    mtot_local += amass;
406 +  }
407 +
408 + #ifdef IS_MPI
409 +  MPI_Allreduce(&mtot_local,&mtot,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
410 +  MPI_Allreduce(COM_local,COM,3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
411 + #else
412 +  mtot = mtot_local;
413 +  for(i = 0; i < 3; i++) {
414 +    COM[i] = COM_local[i];
415 +  }
416 + #endif
417 +    
418 +  for (i = 0; i < 3; i++) {
419 +    COM[i] = COM[i] / mtot;
420 +  }
421 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines