1 |
#include "Atom.hpp" |
2 |
#include "SRI.hpp" |
3 |
#include "AbstractClasses.hpp" |
4 |
#include "SimInfo.hpp" |
5 |
#include "ForceFields.hpp" |
6 |
#include "Thermo.hpp" |
7 |
#include "ReadWrite.hpp" |
8 |
#include "Integrator.hpp" |
9 |
#include "simError.h" |
10 |
|
11 |
|
12 |
// Basic thermostating via Hoover, Phys.Rev.A, 1985, Vol. 31 (5) 1695-1697 |
13 |
|
14 |
template<typename T> NVT<T>::NVT ( SimInfo *theInfo, ForceFields* the_ff): |
15 |
T( theInfo, the_ff ) |
16 |
{ |
17 |
chi = 0.0; |
18 |
have_tau_thermostat = 0; |
19 |
have_target_temp = 0; |
20 |
have_chi_tolerance = 0; |
21 |
integralOfChidt = 0.0; |
22 |
|
23 |
oldVel = new double[3*nAtoms]; |
24 |
oldJi = new double[3*nAtoms]; |
25 |
} |
26 |
|
27 |
template<typename T> NVT<T>::~NVT() { |
28 |
delete[] oldVel; |
29 |
delete[] oldJi; |
30 |
} |
31 |
|
32 |
template<typename T> void NVT<T>::moveA() { |
33 |
|
34 |
int i, j; |
35 |
DirectionalAtom* dAtom; |
36 |
double Tb[3], ji[3]; |
37 |
double mass; |
38 |
double vel[3], pos[3], frc[3]; |
39 |
|
40 |
double instTemp; |
41 |
|
42 |
// We need the temperature at time = t for the chi update below: |
43 |
|
44 |
instTemp = tStats->getTemperature(); |
45 |
|
46 |
for( i=0; i<nAtoms; i++ ){ |
47 |
|
48 |
atoms[i]->getVel( vel ); |
49 |
atoms[i]->getPos( pos ); |
50 |
atoms[i]->getFrc( frc ); |
51 |
|
52 |
mass = atoms[i]->getMass(); |
53 |
|
54 |
for (j=0; j < 3; j++) { |
55 |
// velocity half step (use chi from previous step here): |
56 |
vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi); |
57 |
// position whole step |
58 |
pos[j] += dt * vel[j]; |
59 |
} |
60 |
|
61 |
atoms[i]->setVel( vel ); |
62 |
atoms[i]->setPos( pos ); |
63 |
|
64 |
if( atoms[i]->isDirectional() ){ |
65 |
|
66 |
dAtom = (DirectionalAtom *)atoms[i]; |
67 |
|
68 |
// get and convert the torque to body frame |
69 |
|
70 |
dAtom->getTrq( Tb ); |
71 |
dAtom->lab2Body( Tb ); |
72 |
|
73 |
// get the angular momentum, and propagate a half step |
74 |
|
75 |
dAtom->getJ( ji ); |
76 |
|
77 |
for (j=0; j < 3; j++) |
78 |
ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); |
79 |
|
80 |
this->rotationPropagation( dAtom, ji ); |
81 |
|
82 |
dAtom->setJ( ji ); |
83 |
} |
84 |
} |
85 |
|
86 |
if (nConstrained){ |
87 |
constrainA(); |
88 |
} |
89 |
|
90 |
// Finally, evolve chi a half step (just like a velocity) using |
91 |
// temperature at time t, not time t+dt/2 |
92 |
|
93 |
chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat); |
94 |
integralOfChidt += chi*dt2; |
95 |
|
96 |
} |
97 |
|
98 |
template<typename T> void NVT<T>::moveB( void ){ |
99 |
int i, j, k; |
100 |
DirectionalAtom* dAtom; |
101 |
double Tb[3], ji[3]; |
102 |
double vel[3], frc[3]; |
103 |
double mass; |
104 |
double instTemp; |
105 |
double oldChi, prevChi; |
106 |
|
107 |
// Set things up for the iteration: |
108 |
|
109 |
oldChi = chi; |
110 |
|
111 |
for( i=0; i<nAtoms; i++ ){ |
112 |
|
113 |
atoms[i]->getVel( vel ); |
114 |
|
115 |
for (j=0; j < 3; j++) |
116 |
oldVel[3*i + j] = vel[j]; |
117 |
|
118 |
if( atoms[i]->isDirectional() ){ |
119 |
|
120 |
dAtom = (DirectionalAtom *)atoms[i]; |
121 |
|
122 |
dAtom->getJ( ji ); |
123 |
|
124 |
for (j=0; j < 3; j++) |
125 |
oldJi[3*i + j] = ji[j]; |
126 |
|
127 |
} |
128 |
} |
129 |
|
130 |
// do the iteration: |
131 |
|
132 |
for (k=0; k < 4; k++) { |
133 |
|
134 |
instTemp = tStats->getTemperature(); |
135 |
|
136 |
// evolve chi another half step using the temperature at t + dt/2 |
137 |
|
138 |
prevChi = chi; |
139 |
chi = oldChi + dt2 * ( instTemp / targetTemp - 1.0) / |
140 |
(tauThermostat*tauThermostat); |
141 |
|
142 |
for( i=0; i<nAtoms; i++ ){ |
143 |
|
144 |
atoms[i]->getFrc( frc ); |
145 |
atoms[i]->getVel(vel); |
146 |
|
147 |
mass = atoms[i]->getMass(); |
148 |
|
149 |
// velocity half step |
150 |
for (j=0; j < 3; j++) |
151 |
vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel[3*i + j]*chi); |
152 |
|
153 |
atoms[i]->setVel( vel ); |
154 |
|
155 |
if( atoms[i]->isDirectional() ){ |
156 |
|
157 |
dAtom = (DirectionalAtom *)atoms[i]; |
158 |
|
159 |
// get and convert the torque to body frame |
160 |
|
161 |
dAtom->getTrq( Tb ); |
162 |
dAtom->lab2Body( Tb ); |
163 |
|
164 |
for (j=0; j < 3; j++) |
165 |
ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi); |
166 |
|
167 |
dAtom->setJ( ji ); |
168 |
} |
169 |
} |
170 |
|
171 |
if (nConstrained){ |
172 |
constrainB(); |
173 |
} |
174 |
|
175 |
if (fabs(prevChi - chi) <= chiTolerance) break; |
176 |
} |
177 |
|
178 |
integralOfChidt += dt2*chi; |
179 |
} |
180 |
|
181 |
template<typename T> void NVT<T>::resetIntegrator( void ){ |
182 |
|
183 |
chi = 0.0; |
184 |
integralOfChidt = 0.0; |
185 |
} |
186 |
|
187 |
template<typename T> int NVT<T>::readyCheck() { |
188 |
|
189 |
//check parent's readyCheck() first |
190 |
if (T::readyCheck() == -1) |
191 |
return -1; |
192 |
|
193 |
// First check to see if we have a target temperature. |
194 |
// Not having one is fatal. |
195 |
|
196 |
if (!have_target_temp) { |
197 |
sprintf( painCave.errMsg, |
198 |
"NVT error: You can't use the NVT integrator without a targetTemp!\n" |
199 |
); |
200 |
painCave.isFatal = 1; |
201 |
simError(); |
202 |
return -1; |
203 |
} |
204 |
|
205 |
// We must set tauThermostat. |
206 |
|
207 |
if (!have_tau_thermostat) { |
208 |
sprintf( painCave.errMsg, |
209 |
"NVT error: If you use the constant temperature\n" |
210 |
" integrator, you must set tauThermostat.\n"); |
211 |
painCave.isFatal = 1; |
212 |
simError(); |
213 |
return -1; |
214 |
} |
215 |
|
216 |
if (!have_chi_tolerance) { |
217 |
sprintf( painCave.errMsg, |
218 |
"NVT warning: setting chi tolerance to 1e-6\n"); |
219 |
chiTolerance = 1e-6; |
220 |
have_chi_tolerance = 1; |
221 |
painCave.isFatal = 0; |
222 |
simError(); |
223 |
} |
224 |
|
225 |
return 1; |
226 |
|
227 |
} |
228 |
|
229 |
template<typename T> double NVT<T>::getConservedQuantity(void){ |
230 |
|
231 |
double conservedQuantity; |
232 |
double fkBT; |
233 |
double Energy; |
234 |
double thermostat_kinetic; |
235 |
double thermostat_potential; |
236 |
|
237 |
fkBT = (double)(info->getNDF() ) * kB * targetTemp; |
238 |
|
239 |
Energy = tStats->getTotalE(); |
240 |
|
241 |
thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi / |
242 |
(2.0 * eConvert); |
243 |
|
244 |
thermostat_potential = fkBT * integralOfChidt / eConvert; |
245 |
|
246 |
conservedQuantity = Energy + thermostat_kinetic + thermostat_potential; |
247 |
|
248 |
cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << |
249 |
"\t" << thermostat_potential << "\t" << conservedQuantity << endl; |
250 |
|
251 |
return conservedQuantity; |
252 |
} |