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