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