ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Thermo.cpp
Revision: 475
Committed: Tue Apr 8 12:44:18 2003 UTC (21 years, 3 months ago) by gezelter
File size: 7194 byte(s)
Log Message:
Changes to integrate the NVT and NPT ensembles

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 402
20 mmeineke 377 #define BASE_SEED 123456789
21    
22     Thermo::Thermo( SimInfo* the_entry_plug ) {
23     entry_plug = the_entry_plug;
24     int baseSeed = BASE_SEED;
25    
26     gaussStream = new gaussianSPRNG( baseSeed );
27     }
28    
29     Thermo::~Thermo(){
30     delete gaussStream;
31     }
32    
33     double Thermo::getKinetic(){
34    
35     const double e_convert = 4.184E-4; // convert kcal/mol -> (amu A^2)/fs^2
36     double vx2, vy2, vz2;
37     double kinetic, v_sqr;
38     int kl;
39     double jx2, jy2, jz2; // the square of the angular momentums
40    
41     DirectionalAtom *dAtom;
42    
43     int n_atoms;
44     double kinetic_global;
45     Atom** atoms;
46    
47    
48     n_atoms = entry_plug->n_atoms;
49     atoms = entry_plug->atoms;
50    
51     kinetic = 0.0;
52     kinetic_global = 0.0;
53     for( kl=0; kl < n_atoms; kl++ ){
54    
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    
62     if( atoms[kl]->isDirectional() ){
63    
64     dAtom = (DirectionalAtom *)atoms[kl];
65    
66     jx2 = dAtom->getJx() * dAtom->getJx();
67     jy2 = dAtom->getJy() * dAtom->getJy();
68     jz2 = dAtom->getJz() * dAtom->getJz();
69    
70     kinetic += (jx2 / dAtom->getIxx()) + (jy2 / dAtom->getIyy())
71     + (jz2 / dAtom->getIzz());
72     }
73     }
74     #ifdef IS_MPI
75 mmeineke 447 MPI_Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE,
76     MPI_SUM, MPI_COMM_WORLD);
77 mmeineke 377 kinetic = kinetic_global;
78     #endif //is_mpi
79    
80     kinetic = kinetic * 0.5 / e_convert;
81    
82     return kinetic;
83     }
84    
85     double Thermo::getPotential(){
86    
87 chuckv 401 double potential_local;
88 mmeineke 377 double potential;
89     int el, nSRI;
90 mmeineke 428 Molecule* molecules;
91 mmeineke 377
92 mmeineke 428 molecules = entry_plug->molecules;
93 mmeineke 377 nSRI = entry_plug->n_SRI;
94    
95 chuckv 401 potential_local = 0.0;
96 chuckv 438 potential = 0.0;
97 chuckv 401 potential_local += entry_plug->lrPot;
98 mmeineke 377
99 mmeineke 423 for( el=0; el<entry_plug->n_mol; el++ ){
100 mmeineke 428 potential_local += molecules[el].getPotential();
101 mmeineke 377 }
102    
103     // Get total potential for entire system from MPI.
104     #ifdef IS_MPI
105 mmeineke 447 MPI_Allreduce(&potential_local,&potential,1,MPI_DOUBLE,
106     MPI_SUM, MPI_COMM_WORLD);
107 chuckv 401 #else
108     potential = potential_local;
109 mmeineke 377 #endif // is_mpi
110    
111 chuckv 438 #ifdef IS_MPI
112     /*
113     std::cerr << "node " << worldRank << ": after pot = " << potential << "\n";
114     */
115     #endif
116    
117 mmeineke 377 return potential;
118     }
119    
120     double Thermo::getTotalE(){
121    
122     double total;
123    
124     total = this->getKinetic() + this->getPotential();
125     return total;
126     }
127    
128 gezelter 454 double Thermo::getTemperature(){
129    
130     const double kb = 1.9872179E-3; // boltzman's constant in kcal/(mol K)
131     double temperature;
132    
133 gezelter 458 temperature = ( 2.0 * this->getKinetic() ) / ((double)entry_plug->ndf * kb );
134 mmeineke 377 return temperature;
135     }
136    
137     double Thermo::getPressure(){
138 gezelter 445 // returns pressure in units amu*fs^-2*Ang^-1
139     // routine derived via viral theorem description in:
140     // Paci, E. and Marchi, M. J.Phys.Chem. 1996, 100, 4314-4322
141 mmeineke 377
142 gezelter 468 const double convert = 4.184e-4;
143 gezelter 475 double molmass;
144 gezelter 468 double vcom[3];
145     double p_local, p_sum, p_mol, virial;
146     double theBox[3];
147     double* tau;
148     int i, nMols;
149     Molecule* molecules;
150    
151     nMols = entry_plug->n_mol;
152     molecules = entry_plug->molecules;
153     tau = entry_plug->tau;
154    
155     // use velocities of molecular centers of mass and molecular masses:
156     p_local = 0.0;
157 gezelter 475
158 gezelter 468 for (i=0; i < nMols; i++) {
159 gezelter 475 molmass = molecules[i].getCOMvel(vcom);
160     p_local += (vcom[0]*vcom[0] + vcom[1]*vcom[1] + vcom[2]*vcom[2]) * molmass;
161 gezelter 468 }
162    
163     // Get total for entire system from MPI.
164     #ifdef IS_MPI
165     MPI_Allreduce(&p_local,&p_sum,1,MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
166     #else
167     p_sum = p_local;
168     #endif // is_mpi
169    
170     virial = tau[0] + tau[4] + tau[8];
171     entry_plug->getBox(theBox);
172    
173     p_mol = (p_sum - virial*convert) / (3.0 * theBox[0] * theBox[1]* theBox[2]);
174    
175     return p_mol;
176 mmeineke 377 }
177    
178     void Thermo::velocitize() {
179    
180     double x,y;
181     double vx, vy, vz;
182     double jx, jy, jz;
183     int i, vr, vd; // velocity randomizer loop counters
184 chuckv 403 double vdrift[3];
185 mmeineke 377 double vbar;
186     const double kb = 8.31451e-7; // kb in amu, angstroms, fs, etc.
187     double av2;
188     double kebar;
189     int n_atoms;
190     Atom** atoms;
191     DirectionalAtom* dAtom;
192     double temperature;
193     int n_oriented;
194     int n_constraints;
195    
196     atoms = entry_plug->atoms;
197     n_atoms = entry_plug->n_atoms;
198     temperature = entry_plug->target_temp;
199     n_oriented = entry_plug->n_oriented;
200     n_constraints = entry_plug->n_constraints;
201    
202 gezelter 458 kebar = kb * temperature * (double)entry_plug->ndf /
203     ( 2.0 * (double)entry_plug->ndfRaw );
204 chuckv 403
205 mmeineke 377 for(vr = 0; vr < n_atoms; vr++){
206    
207     // uses equipartition theory to solve for vbar in angstrom/fs
208    
209     av2 = 2.0 * kebar / atoms[vr]->getMass();
210     vbar = sqrt( av2 );
211 gezelter 444
212 mmeineke 377 // vbar = sqrt( 8.31451e-7 * temperature / atoms[vr]->getMass() );
213    
214     // picks random velocities from a gaussian distribution
215     // centered on vbar
216    
217     vx = vbar * gaussStream->getGaussian();
218     vy = vbar * gaussStream->getGaussian();
219     vz = vbar * gaussStream->getGaussian();
220    
221     atoms[vr]->set_vx( vx );
222     atoms[vr]->set_vy( vy );
223     atoms[vr]->set_vz( vz );
224     }
225 chuckv 401
226     // Get the Center of Mass drift velocity.
227    
228 chuckv 403 getCOMVel(vdrift);
229 mmeineke 377
230     // Corrects for the center of mass drift.
231     // sums all the momentum and divides by total mass.
232    
233     for(vd = 0; vd < n_atoms; vd++){
234    
235     vx = atoms[vd]->get_vx();
236     vy = atoms[vd]->get_vy();
237     vz = atoms[vd]->get_vz();
238 chuckv 401
239 mmeineke 377 vx -= vdrift[0];
240     vy -= vdrift[1];
241     vz -= vdrift[2];
242    
243     atoms[vd]->set_vx(vx);
244     atoms[vd]->set_vy(vy);
245     atoms[vd]->set_vz(vz);
246     }
247     if( n_oriented ){
248    
249     for( i=0; i<n_atoms; i++ ){
250    
251     if( atoms[i]->isDirectional() ){
252    
253     dAtom = (DirectionalAtom *)atoms[i];
254    
255     vbar = sqrt( 2.0 * kebar * dAtom->getIxx() );
256     jx = vbar * gaussStream->getGaussian();
257    
258     vbar = sqrt( 2.0 * kebar * dAtom->getIyy() );
259     jy = vbar * gaussStream->getGaussian();
260 gezelter 454
261 mmeineke 377 vbar = sqrt( 2.0 * kebar * dAtom->getIzz() );
262     jz = vbar * gaussStream->getGaussian();
263    
264     dAtom->setJx( jx );
265     dAtom->setJy( jy );
266     dAtom->setJz( jz );
267     }
268     }
269     }
270     }
271 chuckv 401
272 chuckv 403 void Thermo::getCOMVel(double vdrift[3]){
273 chuckv 401
274     double mtot, mtot_local;
275     double vdrift_local[3];
276     int vd, n_atoms;
277     Atom** atoms;
278    
279     // We are very careless here with the distinction between n_atoms and n_local
280     // We should really fix this before someone pokes an eye out.
281    
282     n_atoms = entry_plug->n_atoms;
283     atoms = entry_plug->atoms;
284    
285     mtot_local = 0.0;
286     vdrift_local[0] = 0.0;
287     vdrift_local[1] = 0.0;
288     vdrift_local[2] = 0.0;
289    
290     for(vd = 0; vd < n_atoms; vd++){
291    
292     vdrift_local[0] += atoms[vd]->get_vx() * atoms[vd]->getMass();
293     vdrift_local[1] += atoms[vd]->get_vy() * atoms[vd]->getMass();
294     vdrift_local[2] += atoms[vd]->get_vz() * atoms[vd]->getMass();
295    
296     mtot_local += atoms[vd]->getMass();
297     }
298    
299     #ifdef IS_MPI
300 mmeineke 447 MPI_Allreduce(&mtot_local,&mtot,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
301     MPI_Allreduce(vdrift_local,vdrift,3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
302 chuckv 401 #else
303     mtot = mtot_local;
304     for(vd = 0; vd < 3; vd++) {
305     vdrift[vd] = vdrift_local[vd];
306     }
307     #endif
308    
309     for (vd = 0; vd < 3; vd++) {
310     vdrift[vd] = vdrift[vd] / mtot;
311     }
312    
313     }
314