ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/Thermo.cpp
Revision: 252
Committed: Tue Jan 28 22:16:55 2003 UTC (21 years, 5 months ago) by chuckv
File size: 7292 byte(s)
Log Message:
Force loops seems to work, velocitize never being called....

File Contents

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