ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/Thermo.cpp
Revision: 249
Committed: Mon Jan 27 21:28:19 2003 UTC (21 years, 5 months ago) by chuckv
File size: 7187 byte(s)
Log Message:
For some unknown reason the Single processor builds. Has not been tested!

File Contents

# User Rev Content
1 mmeineke 10 #include <cmath>
2 chuckv 249
3     #ifdef IS_MPI
4 chuckv 218 #include <mpi++.h>
5 chuckv 249 #endif //is_mpi
6 mmeineke 10
7     #include "Thermo.hpp"
8     #include "SRI.hpp"
9     #include "LRI.hpp"
10     #include "Integrator.hpp"
11    
12 chuckv 223 #define BASE_SEED 123456789
13 mmeineke 10
14 chuckv 223 Thermo::Thermo( SimInfo* the_entry_plug ) {
15     entry_plug = the_entry_plug;
16 chuckv 249 int baseSeed = BASE_SEED;
17 chuckv 223 gaussStream = new gaussianSPRNG( baseSeed );
18     }
19    
20     Thermo::~Thermo(){
21     delete gaussStream;
22     }
23    
24 mmeineke 10 double Thermo::getKinetic(){
25    
26     const double e_convert = 4.184E-4; // convert kcal/mol -> (amu A^2)/fs^2
27     double vx2, vy2, vz2;
28     double kinetic, v_sqr;
29     int kl;
30     double jx2, jy2, jz2; // the square of the angular momentums
31    
32     DirectionalAtom *dAtom;
33    
34     int n_atoms;
35 chuckv 218 double kinetic_global;
36 mmeineke 10 Atom** atoms;
37 chuckv 218
38 mmeineke 10
39     n_atoms = entry_plug->n_atoms;
40     atoms = entry_plug->atoms;
41    
42     kinetic = 0.0;
43 chuckv 218 kinetic_global = 0.0;
44 mmeineke 10 for( kl=0; kl < n_atoms; kl++ ){
45    
46     vx2 = atoms[kl]->get_vx() * atoms[kl]->get_vx();
47     vy2 = atoms[kl]->get_vy() * atoms[kl]->get_vy();
48     vz2 = atoms[kl]->get_vz() * atoms[kl]->get_vz();
49    
50     v_sqr = vx2 + vy2 + vz2;
51     kinetic += atoms[kl]->getMass() * v_sqr;
52    
53     if( atoms[kl]->isDirectional() ){
54    
55     dAtom = (DirectionalAtom *)atoms[kl];
56    
57     jx2 = dAtom->getJx() * dAtom->getJx();
58     jy2 = dAtom->getJy() * dAtom->getJy();
59     jz2 = dAtom->getJz() * dAtom->getJz();
60    
61     kinetic += (jx2 / dAtom->getIxx()) + (jy2 / dAtom->getIyy())
62     + (jz2 / dAtom->getIzz());
63     }
64     }
65 chuckv 218 #ifdef IS_MPI
66     MPI_COMM_WORLD.Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE,MPI_SUM);
67     kinetic = kinetic_global;
68 chuckv 249 #endif //is_mpi
69 chuckv 218
70 mmeineke 10 kinetic = kinetic * 0.5 / e_convert;
71    
72     return kinetic;
73     }
74    
75     double Thermo::getPotential(){
76    
77     double potential;
78 chuckv 218 double potential_global;
79 mmeineke 10 int el, nSRI;
80     SRI** sris;
81    
82     sris = entry_plug->sr_interactions;
83     nSRI = entry_plug->n_SRI;
84    
85     potential = 0.0;
86 chuckv 218 potential_global = 0.0;
87 chuckv 249 potential += entry_plug->lrPot;
88 mmeineke 10
89     // std::cerr << "long range potential: " << potential << "\n";
90     for( el=0; el<nSRI; el++ ){
91    
92     potential += sris[el]->get_potential();
93     }
94    
95 chuckv 218 // Get total potential for entire system from MPI.
96     #ifdef IS_MPI
97     MPI_COMM_WORLD.Allreduce(&potential,&potential_global,1,MPI_DOUBLE,MPI_SUM);
98     potential = potential_global;
99 chuckv 249 #endif // is_mpi
100 chuckv 218
101 mmeineke 10 return potential;
102     }
103    
104     double Thermo::getTotalE(){
105    
106     double total;
107    
108     total = this->getKinetic() + this->getPotential();
109     return total;
110     }
111    
112     double Thermo::getTemperature(){
113    
114 mmeineke 25 const double kb = 1.9872179E-3; // boltzman's constant in kcal/(mol K)
115 mmeineke 10 double temperature;
116    
117     int ndf = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented
118     - entry_plug->n_constraints - 3;
119    
120     temperature = ( 2.0 * this->getKinetic() ) / ( ndf * kb );
121     return temperature;
122     }
123    
124     double Thermo::getPressure(){
125    
126 mmeineke 117 // 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
128     // const double conv_A_m = 1.0E-10; //convert A -> m
129 mmeineke 10
130     return 0.0;
131     }
132    
133     void Thermo::velocitize() {
134    
135     double x,y;
136     double vx, vy, vz;
137     double jx, jy, jz;
138     int i, vr, vd; // velocity randomizer loop counters
139     double vdrift[3];
140     double mtot = 0.0;
141     double vbar;
142     const double kb = 8.31451e-7; // kb in amu, angstroms, fs, etc.
143     double av2;
144     double kebar;
145     int ndf; // number of degrees of freedom
146     int ndfRaw; // the raw number of degrees of freedom
147     int n_atoms;
148     Atom** atoms;
149     DirectionalAtom* dAtom;
150     double temperature;
151     int n_oriented;
152     int n_constraints;
153    
154     atoms = entry_plug->atoms;
155     n_atoms = entry_plug->n_atoms;
156     temperature = entry_plug->target_temp;
157     n_oriented = entry_plug->n_oriented;
158     n_constraints = entry_plug->n_constraints;
159    
160    
161     ndfRaw = 3 * n_atoms + 3 * n_oriented;
162     ndf = ndfRaw - n_constraints - 3;
163     kebar = kb * temperature * (double)ndf / ( 2.0 * (double)ndfRaw );
164    
165     for(vr = 0; vr < n_atoms; vr++){
166    
167     // uses equipartition theory to solve for vbar in angstrom/fs
168    
169     av2 = 2.0 * kebar / atoms[vr]->getMass();
170     vbar = sqrt( av2 );
171    
172     // vbar = sqrt( 8.31451e-7 * temperature / atoms[vr]->getMass() );
173    
174     // picks random velocities from a gaussian distribution
175     // centered on vbar
176 chuckv 221 #ifndef USE_SPRNG
177     /* If we are using mpi, we need to use the SPRNG random
178     generator. The non drand48 generator will just repeat
179     the same numbers for every node creating a non-gaussian
180     distribution for the simulation. drand48 is fine for the
181     single processor version of the code, but SPRNG should
182     still be preferred for consistency.
183     */
184 chuckv 249
185 chuckv 221 #ifdef IS_MPI
186     #error "SPRNG random number generator must be used for MPI"
187     #else
188     #warning "Using drand48 for random number generation"
189 chuckv 249 #endif // is_mpi
190    
191 mmeineke 10 x = drand48();
192     y = drand48();
193     vx = vbar * sqrt( -2.0 * log(x)) * cos(2 * M_PI * y);
194    
195     x = drand48();
196     y = drand48();
197     vy = vbar * sqrt( -2.0 * log(x)) * cos(2 * M_PI * y);
198    
199     x = drand48();
200     y = drand48();
201     vz = vbar * sqrt( -2.0 * log(x)) * cos(2 * M_PI * y);
202 chuckv 249 #endif // use_spring
203 chuckv 221
204     #ifdef USE_SPRNG
205 chuckv 223 vx = vbar * gaussStream->getGaussian();
206     vy = vbar * gaussStream->getGaussian();
207     vz = vbar * gaussStream->getGaussian();
208 chuckv 249 #endif // use_spring
209 chuckv 221
210 mmeineke 10 atoms[vr]->set_vx( vx );
211     atoms[vr]->set_vy( vy );
212     atoms[vr]->set_vz( vz );
213     }
214    
215     // Corrects for the center of mass drift.
216     // sums all the momentum and divides by total mass.
217    
218     mtot = 0.0;
219     vdrift[0] = 0.0;
220     vdrift[1] = 0.0;
221     vdrift[2] = 0.0;
222     for(vd = 0; vd < n_atoms; vd++){
223    
224     vdrift[0] += atoms[vd]->get_vx() * atoms[vd]->getMass();
225     vdrift[1] += atoms[vd]->get_vy() * atoms[vd]->getMass();
226     vdrift[2] += atoms[vd]->get_vz() * atoms[vd]->getMass();
227    
228     mtot = mtot + atoms[vd]->getMass();
229     }
230    
231     for (vd = 0; vd < 3; vd++) {
232     vdrift[vd] = vdrift[vd] / mtot;
233     }
234    
235     for(vd = 0; vd < n_atoms; vd++){
236    
237     vx = atoms[vd]->get_vx();
238     vy = atoms[vd]->get_vy();
239     vz = atoms[vd]->get_vz();
240    
241    
242     vx -= vdrift[0];
243     vy -= vdrift[1];
244     vz -= vdrift[2];
245    
246     atoms[vd]->set_vx(vx);
247     atoms[vd]->set_vy(vy);
248     atoms[vd]->set_vz(vz);
249     }
250     if( n_oriented ){
251    
252     for( i=0; i<n_atoms; i++ ){
253    
254     if( atoms[i]->isDirectional() ){
255    
256     dAtom = (DirectionalAtom *)atoms[i];
257 chuckv 249
258     #ifndef USE_SPRNG
259    
260 chuckv 221 #ifdef IS_MPI
261     #error "SPRNG random number generator must be used for MPI"
262 chuckv 249 #else // is_mpi
263 chuckv 221 #warning "Using drand48 for random number generation"
264 chuckv 249 #endif // is_MPI
265 mmeineke 10
266     vbar = sqrt( 2.0 * kebar * dAtom->getIxx() );
267     x = drand48();
268     y = drand48();
269     jx = vbar * sqrt( -2.0 * log(x)) * cos(2 * M_PI * y);
270    
271     vbar = sqrt( 2.0 * kebar * dAtom->getIyy() );
272     x = drand48();
273     y = drand48();
274     jy = vbar * sqrt( -2.0 * log(x)) * cos(2 * M_PI * y);
275    
276     vbar = sqrt( 2.0 * kebar * dAtom->getIzz() );
277     x = drand48();
278     y = drand48();
279     jz = vbar * sqrt( -2.0 * log(x)) * cos(2 * M_PI * y);
280 chuckv 249
281     #else //use_sprng
282    
283 chuckv 221 vbar = sqrt( 2.0 * kebar * dAtom->getIxx() );
284 chuckv 223 jx = vbar * gaussStream->getGaussian();
285 chuckv 221
286     vbar = sqrt( 2.0 * kebar * dAtom->getIyy() );
287 chuckv 223 jy = vbar * gaussStream->getGaussian();
288 chuckv 221
289     vbar = sqrt( 2.0 * kebar * dAtom->getIzz() );
290 chuckv 223 jz = vbar * gaussStream->getGaussian();
291 chuckv 249 #endif //use_sprng
292 mmeineke 10
293     dAtom->setJx( jx );
294     dAtom->setJy( jy );
295     dAtom->setJz( jz );
296     }
297     }
298     }
299     }