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; |
40 |
|
|
41 |
|
double instTemp; |
42 |
|
|
43 |
< |
instTemp = tStats->getTemperature(); |
43 |
> |
// We need the temperature at time = t for the chi update below: |
44 |
|
|
45 |
< |
// first evolve chi a half step |
45 |
> |
instTemp = tStats->getTemperature(); |
46 |
|
|
37 |
– |
chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat); |
38 |
– |
|
47 |
|
for( i=0; i<nAtoms; i++ ){ |
48 |
|
|
49 |
|
atoms[i]->getVel( vel ); |
53 |
|
mass = atoms[i]->getMass(); |
54 |
|
|
55 |
|
for (j=0; j < 3; j++) { |
56 |
< |
// velocity half step |
56 |
> |
// velocity half step (use chi from previous step here): |
57 |
|
vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi); |
58 |
|
// position whole step |
59 |
|
pos[j] += dt * vel[j]; |
108 |
|
dAtom->setA( A ); |
109 |
|
} |
110 |
|
} |
111 |
+ |
|
112 |
+ |
if (nConstrained){ |
113 |
+ |
constrainA(); |
114 |
+ |
} |
115 |
+ |
|
116 |
+ |
// Finally, evolve chi a half step (just like a velocity) using |
117 |
+ |
// temperature at time t, not time t+dt/2 |
118 |
+ |
|
119 |
+ |
chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat); |
120 |
+ |
integralOfChidt += chi*dt2; |
121 |
+ |
|
122 |
|
} |
123 |
|
|
124 |
|
template<typename T> void NVT<T>::moveB( void ){ |
125 |
< |
int i, j; |
125 |
> |
int i, j, k; |
126 |
|
DirectionalAtom* dAtom; |
127 |
|
double Tb[3], ji[3]; |
128 |
|
double vel[3], frc[3]; |
129 |
|
double mass; |
111 |
– |
|
130 |
|
double instTemp; |
131 |
< |
|
132 |
< |
instTemp = tStats->getTemperature(); |
133 |
< |
chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat); |
134 |
< |
|
131 |
> |
double oldChi, prevChi; |
132 |
> |
|
133 |
> |
// Set things up for the iteration: |
134 |
> |
|
135 |
> |
oldChi = chi; |
136 |
> |
|
137 |
|
for( i=0; i<nAtoms; i++ ){ |
138 |
|
|
139 |
|
atoms[i]->getVel( vel ); |
120 |
– |
atoms[i]->getFrc( frc ); |
140 |
|
|
141 |
< |
mass = atoms[i]->getMass(); |
141 |
> |
for (j=0; j < 3; j++) |
142 |
> |
oldVel[3*i + j] = vel[j]; |
143 |
|
|
124 |
– |
// velocity half step |
125 |
– |
for (j=0; j < 3; j++) |
126 |
– |
vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi); |
127 |
– |
|
128 |
– |
atoms[i]->setVel( vel ); |
129 |
– |
|
144 |
|
if( atoms[i]->isDirectional() ){ |
145 |
|
|
146 |
|
dAtom = (DirectionalAtom *)atoms[i]; |
147 |
|
|
148 |
< |
// get and convert the torque to body frame |
148 |
> |
dAtom->getJ( ji ); |
149 |
|
|
150 |
< |
dAtom->getTrq( Tb ); |
151 |
< |
dAtom->lab2Body( Tb ); |
150 |
> |
for (j=0; j < 3; j++) |
151 |
> |
oldJi[3*i + j] = ji[j]; |
152 |
|
|
153 |
< |
// get the angular momentum, and propagate a half step |
153 |
> |
} |
154 |
> |
} |
155 |
|
|
156 |
< |
dAtom->getJ( ji ); |
156 |
> |
// do the iteration: |
157 |
|
|
158 |
+ |
for (k=0; k < 4; k++) { |
159 |
+ |
|
160 |
+ |
instTemp = tStats->getTemperature(); |
161 |
+ |
|
162 |
+ |
// evolve chi another half step using the temperature at t + dt/2 |
163 |
+ |
|
164 |
+ |
prevChi = chi; |
165 |
+ |
chi = oldChi + dt2 * ( instTemp / targetTemp - 1.0) / |
166 |
+ |
(tauThermostat*tauThermostat); |
167 |
+ |
|
168 |
+ |
for( i=0; i<nAtoms; i++ ){ |
169 |
+ |
|
170 |
+ |
atoms[i]->getFrc( frc ); |
171 |
+ |
atoms[i]->getVel(vel); |
172 |
+ |
|
173 |
+ |
mass = atoms[i]->getMass(); |
174 |
+ |
|
175 |
+ |
// velocity half step |
176 |
|
for (j=0; j < 3; j++) |
177 |
< |
ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); |
177 |
> |
vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel[3*i + j]*chi); |
178 |
|
|
179 |
+ |
atoms[i]->setVel( vel ); |
180 |
+ |
|
181 |
+ |
if( atoms[i]->isDirectional() ){ |
182 |
+ |
|
183 |
+ |
dAtom = (DirectionalAtom *)atoms[i]; |
184 |
+ |
|
185 |
+ |
// get and convert the torque to body frame |
186 |
+ |
|
187 |
+ |
dAtom->getTrq( Tb ); |
188 |
+ |
dAtom->lab2Body( Tb ); |
189 |
+ |
|
190 |
+ |
for (j=0; j < 3; j++) |
191 |
+ |
ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi); |
192 |
+ |
|
193 |
+ |
dAtom->setJ( ji ); |
194 |
+ |
} |
195 |
+ |
} |
196 |
|
|
197 |
< |
dAtom->setJ( ji ); |
197 |
> |
if (nConstrained){ |
198 |
> |
constrainB(); |
199 |
|
} |
200 |
+ |
|
201 |
+ |
if (fabs(prevChi - chi) <= chiTolerance) break; |
202 |
|
} |
203 |
+ |
|
204 |
+ |
integralOfChidt += dt2*chi; |
205 |
|
} |
206 |
|
|
207 |
|
template<typename T> void NVT<T>::resetIntegrator( void ){ |
208 |
|
|
209 |
|
chi = 0.0; |
210 |
+ |
integralOfChidt = 0.0; |
211 |
|
} |
212 |
|
|
213 |
|
template<typename T> int NVT<T>::readyCheck() { |
238 |
|
simError(); |
239 |
|
return -1; |
240 |
|
} |
241 |
< |
return 1; |
241 |
> |
|
242 |
> |
if (!have_chi_tolerance) { |
243 |
> |
sprintf( painCave.errMsg, |
244 |
> |
"NVT warning: setting chi tolerance to 1e-6\n"); |
245 |
> |
chiTolerance = 1e-6; |
246 |
> |
have_chi_tolerance = 1; |
247 |
> |
painCave.isFatal = 0; |
248 |
> |
simError(); |
249 |
> |
} |
250 |
> |
|
251 |
> |
return 1; |
252 |
> |
|
253 |
|
} |
254 |
+ |
|
255 |
+ |
template<typename T> double NVT<T>::getConservedQuantity(void){ |
256 |
+ |
|
257 |
+ |
double conservedQuantity; |
258 |
+ |
double fkBT; |
259 |
+ |
double Energy; |
260 |
+ |
double thermostat_kinetic; |
261 |
+ |
double thermostat_potential; |
262 |
+ |
|
263 |
+ |
fkBT = (double)(info->getNDF() ) * kB * targetTemp; |
264 |
+ |
|
265 |
+ |
Energy = tStats->getTotalE(); |
266 |
+ |
|
267 |
+ |
thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi / |
268 |
+ |
(2.0 * eConvert); |
269 |
+ |
|
270 |
+ |
thermostat_potential = fkBT * integralOfChidt / eConvert; |
271 |
+ |
|
272 |
+ |
conservedQuantity = Energy + thermostat_kinetic + thermostat_potential; |
273 |
+ |
|
274 |
+ |
cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << |
275 |
+ |
"\t" << thermostat_potential << "\t" << conservedQuantity << endl; |
276 |
+ |
|
277 |
+ |
return conservedQuantity; |
278 |
+ |
} |