9 |
|
#include "Integrator.hpp" |
10 |
|
#include "simError.h" |
11 |
|
|
12 |
+ |
#ifdef IS_MPI |
13 |
+ |
#include "mpiSimulation.hpp" |
14 |
+ |
#endif |
15 |
|
|
16 |
|
// Basic isotropic thermostating and barostating via the Melchionna |
17 |
|
// modification of the Hoover algorithm: |
23 |
|
// |
24 |
|
// Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. |
25 |
|
|
26 |
< |
NPTi::NPTi ( SimInfo *theInfo, ForceFields* the_ff): |
27 |
< |
Integrator( theInfo, the_ff ) |
26 |
> |
template<typename T> NPTi<T>::NPTi ( SimInfo *theInfo, ForceFields* the_ff): |
27 |
> |
T( theInfo, the_ff ) |
28 |
|
{ |
26 |
– |
chi = 0.0; |
29 |
|
eta = 0.0; |
30 |
< |
have_tau_thermostat = 0; |
29 |
< |
have_tau_barostat = 0; |
30 |
< |
have_target_temp = 0; |
31 |
< |
have_target_pressure = 0; |
30 |
> |
oldEta = 0.0; |
31 |
|
} |
32 |
|
|
33 |
< |
void NPTi::moveA() { |
34 |
< |
|
35 |
< |
int i,j,k; |
37 |
< |
int atomIndex, aMatIndex; |
38 |
< |
DirectionalAtom* dAtom; |
39 |
< |
double Tb[3]; |
40 |
< |
double ji[3]; |
41 |
< |
double rj[3]; |
42 |
< |
double instaTemp, instaPress, instaVol; |
43 |
< |
double tt2, tb2; |
44 |
< |
double angle; |
33 |
> |
template<typename T> NPTi<T>::~NPTi() { |
34 |
> |
//nothing for now |
35 |
> |
} |
36 |
|
|
37 |
< |
tt2 = tauThermostat * tauThermostat; |
38 |
< |
tb2 = tauBarostat * tauBarostat; |
37 |
> |
template<typename T> void NPTi<T>::resetIntegrator() { |
38 |
> |
eta = 0.0; |
39 |
> |
T::resetIntegrator(); |
40 |
> |
} |
41 |
|
|
42 |
< |
instaTemp = tStats->getTemperature(); |
43 |
< |
instaPress = tStats->getPressure(); |
44 |
< |
instaVol = tStats->getVolume(); |
45 |
< |
|
46 |
< |
// first evolve chi a half step |
42 |
> |
template<typename T> void NPTi<T>::evolveEtaA() { |
43 |
> |
eta += dt2 * ( instaVol * (instaPress - targetPressure) / |
44 |
> |
(p_convert*NkBT*tb2)); |
45 |
> |
oldEta = eta; |
46 |
> |
} |
47 |
> |
|
48 |
> |
template<typename T> void NPTi<T>::evolveEtaB() { |
49 |
|
|
50 |
< |
chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
51 |
< |
eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); |
50 |
> |
prevEta = eta; |
51 |
> |
eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) / |
52 |
> |
(p_convert*NkBT*tb2)); |
53 |
> |
} |
54 |
|
|
55 |
< |
for( i=0; i<nAtoms; i++ ){ |
56 |
< |
atomIndex = i * 3; |
60 |
< |
aMatIndex = i * 9; |
61 |
< |
|
62 |
< |
// velocity half step |
63 |
< |
for( j=atomIndex; j<(atomIndex+3); j++ ) |
64 |
< |
vel[j] += dt2 * ((frc[j]/atoms[i]->getMass())*eConvert |
65 |
< |
- vel[j]*(chi+eta)); |
55 |
> |
template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) { |
56 |
> |
int i; |
57 |
|
|
58 |
< |
// position whole step |
58 |
> |
for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta ); |
59 |
> |
} |
60 |
|
|
61 |
< |
rj[0] = pos[atomIndex]; |
62 |
< |
rj[1] = pos[atomIndex+1]; |
71 |
< |
rj[2] = pos[atomIndex+2]; |
72 |
< |
|
73 |
< |
info->wrapVector(rj); |
61 |
> |
template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){ |
62 |
> |
int i; |
63 |
|
|
64 |
< |
pos[atomIndex] += dt * (vel[atomIndex] + eta*rj[0]); |
65 |
< |
pos[atomIndex+1] += dt * (vel[atomIndex+1] + eta*rj[1]); |
77 |
< |
pos[atomIndex+2] += dt * (vel[atomIndex+2] + eta*rj[2]); |
78 |
< |
|
79 |
< |
if( atoms[i]->isDirectional() ){ |
64 |
> |
for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta ); |
65 |
> |
} |
66 |
|
|
81 |
– |
dAtom = (DirectionalAtom *)atoms[i]; |
82 |
– |
|
83 |
– |
// get and convert the torque to body frame |
84 |
– |
|
85 |
– |
Tb[0] = dAtom->getTx(); |
86 |
– |
Tb[1] = dAtom->getTy(); |
87 |
– |
Tb[2] = dAtom->getTz(); |
88 |
– |
|
89 |
– |
dAtom->lab2Body( Tb ); |
90 |
– |
|
91 |
– |
// get the angular momentum, and propagate a half step |
67 |
|
|
68 |
< |
ji[0] = dAtom->getJx(); |
69 |
< |
ji[1] = dAtom->getJy(); |
70 |
< |
ji[2] = dAtom->getJz(); |
96 |
< |
|
97 |
< |
ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi); |
98 |
< |
ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi); |
99 |
< |
ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi); |
100 |
< |
|
101 |
< |
// use the angular velocities to propagate the rotation matrix a |
102 |
< |
// full time step |
103 |
< |
|
104 |
< |
// rotate about the x-axis |
105 |
< |
angle = dt2 * ji[0] / dAtom->getIxx(); |
106 |
< |
this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); |
107 |
< |
|
108 |
< |
// rotate about the y-axis |
109 |
< |
angle = dt2 * ji[1] / dAtom->getIyy(); |
110 |
< |
this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); |
111 |
< |
|
112 |
< |
// rotate about the z-axis |
113 |
< |
angle = dt * ji[2] / dAtom->getIzz(); |
114 |
< |
this->rotate( 0, 1, angle, ji, &Amat[aMatIndex] ); |
115 |
< |
|
116 |
< |
// rotate about the y-axis |
117 |
< |
angle = dt2 * ji[1] / dAtom->getIyy(); |
118 |
< |
this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); |
119 |
< |
|
120 |
< |
// rotate about the x-axis |
121 |
< |
angle = dt2 * ji[0] / dAtom->getIxx(); |
122 |
< |
this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); |
123 |
< |
|
124 |
< |
dAtom->setJx( ji[0] ); |
125 |
< |
dAtom->setJy( ji[1] ); |
126 |
< |
dAtom->setJz( ji[2] ); |
127 |
< |
} |
128 |
< |
|
129 |
< |
} |
130 |
< |
// Scale the box after all the positions have been moved: |
68 |
> |
template<typename T> void NPTi<T>::getPosScale(double pos[3], double COM[3], |
69 |
> |
int index, double sc[3]){ |
70 |
> |
int j; |
71 |
|
|
72 |
< |
info->scaleBox(exp(dt*eta)); |
72 |
> |
for(j=0; j<3; j++) |
73 |
> |
sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; |
74 |
|
|
75 |
+ |
for(j=0; j<3; j++) |
76 |
+ |
sc[j] *= eta; |
77 |
|
} |
78 |
|
|
79 |
< |
void NPTi::moveB( void ){ |
137 |
< |
int i,j,k; |
138 |
< |
int atomIndex; |
139 |
< |
DirectionalAtom* dAtom; |
140 |
< |
double Tb[3]; |
141 |
< |
double ji[3]; |
142 |
< |
double instaTemp, instaPress, instaVol; |
143 |
< |
double tt2, tb2; |
144 |
< |
|
145 |
< |
tt2 = tauThermostat * tauThermostat; |
146 |
< |
tb2 = tauBarostat * tauBarostat; |
79 |
> |
template<typename T> void NPTi<T>::scaleSimBox( void ){ |
80 |
|
|
81 |
< |
instaTemp = tStats->getTemperature(); |
149 |
< |
instaPress = tStats->getPressure(); |
150 |
< |
instaVol = tStats->getVolume(); |
81 |
> |
double scaleFactor; |
82 |
|
|
83 |
< |
chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
153 |
< |
eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); |
154 |
< |
|
155 |
< |
for( i=0; i<nAtoms; i++ ){ |
156 |
< |
atomIndex = i * 3; |
157 |
< |
|
158 |
< |
// velocity half step |
159 |
< |
for( j=atomIndex; j<(atomIndex+3); j++ ) |
160 |
< |
for( j=atomIndex; j<(atomIndex+3); j++ ) |
161 |
< |
vel[j] += dt2 * ((frc[j]/atoms[i]->getMass())*eConvert |
162 |
< |
- vel[j]*(chi+eta)); |
163 |
< |
|
164 |
< |
if( atoms[i]->isDirectional() ){ |
165 |
< |
|
166 |
< |
dAtom = (DirectionalAtom *)atoms[i]; |
167 |
< |
|
168 |
< |
// get and convert the torque to body frame |
169 |
< |
|
170 |
< |
Tb[0] = dAtom->getTx(); |
171 |
< |
Tb[1] = dAtom->getTy(); |
172 |
< |
Tb[2] = dAtom->getTz(); |
173 |
< |
|
174 |
< |
dAtom->lab2Body( Tb ); |
175 |
< |
|
176 |
< |
// get the angular momentum, and complete the angular momentum |
177 |
< |
// half step |
178 |
< |
|
179 |
< |
ji[0] = dAtom->getJx(); |
180 |
< |
ji[1] = dAtom->getJy(); |
181 |
< |
ji[2] = dAtom->getJz(); |
182 |
< |
|
183 |
< |
ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi); |
184 |
< |
ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi); |
185 |
< |
ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi); |
186 |
< |
|
187 |
< |
dAtom->setJx( ji[0] ); |
188 |
< |
dAtom->setJy( ji[1] ); |
189 |
< |
dAtom->setJz( ji[2] ); |
190 |
< |
} |
191 |
< |
} |
192 |
< |
} |
83 |
> |
scaleFactor = exp(dt*eta); |
84 |
|
|
85 |
< |
int NPTi::readyCheck() { |
195 |
< |
|
196 |
< |
// First check to see if we have a target temperature. |
197 |
< |
// Not having one is fatal. |
198 |
< |
|
199 |
< |
if (!have_target_temp) { |
85 |
> |
if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) { |
86 |
|
sprintf( painCave.errMsg, |
87 |
< |
"NPTi error: You can't use the NPTi integrator\n" |
88 |
< |
" without a targetTemp!\n" |
87 |
> |
"NPTi error: Attempting a Box scaling of more than 10 percent" |
88 |
> |
" check your tauBarostat, as it is probably too small!\n" |
89 |
> |
" eta = %lf, scaleFactor = %lf\n", eta, scaleFactor |
90 |
|
); |
91 |
|
painCave.isFatal = 1; |
92 |
|
simError(); |
93 |
< |
return -1; |
94 |
< |
} |
93 |
> |
} else { |
94 |
> |
info->scaleBox(scaleFactor); |
95 |
> |
} |
96 |
|
|
97 |
< |
if (!have_target_pressure) { |
210 |
< |
sprintf( painCave.errMsg, |
211 |
< |
"NPTi error: You can't use the NPTi integrator\n" |
212 |
< |
" without a targetPressure!\n" |
213 |
< |
); |
214 |
< |
painCave.isFatal = 1; |
215 |
< |
simError(); |
216 |
< |
return -1; |
217 |
< |
} |
218 |
< |
|
219 |
< |
// We must set tauThermostat. |
220 |
< |
|
221 |
< |
if (!have_tau_thermostat) { |
222 |
< |
sprintf( painCave.errMsg, |
223 |
< |
"NPTi error: If you use the NPTi\n" |
224 |
< |
" integrator, you must set tauThermostat.\n"); |
225 |
< |
painCave.isFatal = 1; |
226 |
< |
simError(); |
227 |
< |
return -1; |
228 |
< |
} |
97 |
> |
} |
98 |
|
|
99 |
< |
// We must set tauBarostat. |
231 |
< |
|
232 |
< |
if (!have_tau_barostat) { |
233 |
< |
sprintf( painCave.errMsg, |
234 |
< |
"NPTi error: If you use the NPTi\n" |
235 |
< |
" integrator, you must set tauBarostat.\n"); |
236 |
< |
painCave.isFatal = 1; |
237 |
< |
simError(); |
238 |
< |
return -1; |
239 |
< |
} |
99 |
> |
template<typename T> bool NPTi<T>::etaConverged() { |
100 |
|
|
101 |
< |
// We need NkBT a lot, so just set it here: |
101 |
> |
return ( fabs(prevEta - eta) <= etaTolerance ); |
102 |
> |
} |
103 |
|
|
104 |
< |
NkBT = (double)info->ndf * kB * targetTemp; |
104 |
> |
template<typename T> double NPTi<T>::getConservedQuantity(void){ |
105 |
|
|
106 |
< |
return 1; |
106 |
> |
double conservedQuantity; |
107 |
> |
double Three_NkBT; |
108 |
> |
double Energy; |
109 |
> |
double thermostat_kinetic; |
110 |
> |
double thermostat_potential; |
111 |
> |
double barostat_kinetic; |
112 |
> |
double barostat_potential; |
113 |
> |
double tb2; |
114 |
> |
double eta2; |
115 |
> |
|
116 |
> |
Energy = tStats->getTotalE(); |
117 |
> |
|
118 |
> |
thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi / |
119 |
> |
(2.0 * eConvert); |
120 |
> |
|
121 |
> |
thermostat_potential = fkBT* integralOfChidt / eConvert; |
122 |
> |
|
123 |
> |
|
124 |
> |
barostat_kinetic = 3.0 * NkBT * tauBarostat * tauBarostat * eta * eta / |
125 |
> |
(2.0 * eConvert); |
126 |
> |
|
127 |
> |
barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / |
128 |
> |
eConvert; |
129 |
> |
|
130 |
> |
conservedQuantity = Energy + thermostat_kinetic + thermostat_potential + |
131 |
> |
barostat_kinetic + barostat_potential; |
132 |
> |
|
133 |
> |
// cout.width(8); |
134 |
> |
// cout.precision(8); |
135 |
> |
|
136 |
> |
// cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << |
137 |
> |
// "\t" << thermostat_potential << "\t" << barostat_kinetic << |
138 |
> |
// "\t" << barostat_potential << "\t" << conservedQuantity << endl; |
139 |
> |
return conservedQuantity; |
140 |
|
} |