| 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; |
| 37 |
< |
DirectionalAtom* dAtom; |
| 38 |
< |
double Tb[3], ji[3]; |
| 39 |
< |
double A[3][3], I[3][3]; |
| 40 |
< |
double angle, mass; |
| 41 |
< |
double vel[3], pos[3], frc[3]; |
| 33 |
> |
template<typename T> NPTi<T>::~NPTi() { |
| 34 |
> |
//nothing for now |
| 35 |
> |
} |
| 36 |
|
|
| 37 |
< |
double rj[3]; |
| 38 |
< |
double instaTemp, instaPress, instaVol; |
| 39 |
< |
double tt2, tb2, scaleFactor; |
| 37 |
> |
template<typename T> void NPTi<T>::resetIntegrator() { |
| 38 |
> |
eta = 0.0; |
| 39 |
> |
T::resetIntegrator(); |
| 40 |
> |
} |
| 41 |
|
|
| 42 |
< |
tt2 = tauThermostat * tauThermostat; |
| 43 |
< |
tb2 = tauBarostat * tauBarostat; |
| 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 |
< |
instaTemp = tStats->getTemperature(); |
| 51 |
< |
instaPress = tStats->getPressure(); |
| 52 |
< |
instaVol = tStats->getVolume(); |
| 53 |
< |
|
| 54 |
< |
// first evolve chi a half step |
| 48 |
> |
template<typename T> void NPTi<T>::evolveEtaB() { |
| 49 |
|
|
| 50 |
< |
chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
| 51 |
< |
eta += dt2 * ( instaVol * (instaPress - targetPressure) / |
| 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 |
< |
atoms[i]->getVel( vel ); |
| 62 |
< |
atoms[i]->getPos( pos ); |
| 63 |
< |
atoms[i]->getFrc( frc ); |
| 55 |
> |
template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) { |
| 56 |
> |
int i; |
| 57 |
|
|
| 58 |
< |
mass = atoms[i]->getMass(); |
| 58 |
> |
for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta ); |
| 59 |
> |
} |
| 60 |
|
|
| 61 |
< |
for (j=0; j < 3; j++) { |
| 62 |
< |
vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta)); |
| 69 |
< |
rj[j] = pos[j]; |
| 70 |
< |
} |
| 61 |
> |
template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){ |
| 62 |
> |
int i; |
| 63 |
|
|
| 64 |
< |
atoms[i]->setVel( vel ); |
| 64 |
> |
for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta ); |
| 65 |
> |
} |
| 66 |
|
|
| 74 |
– |
info->wrapVector(rj); |
| 67 |
|
|
| 68 |
< |
for (j = 0; j < 3; j++) |
| 69 |
< |
pos[j] += dt * (vel[j] + eta*rj[j]); |
| 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 |
+ |
for(j=0; j<3; j++) |
| 73 |
+ |
sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; |
| 74 |
|
|
| 75 |
< |
atoms[i]->setPos( pos ); |
| 75 |
> |
for(j=0; j<3; j++) |
| 76 |
> |
sc[j] *= eta; |
| 77 |
> |
} |
| 78 |
|
|
| 79 |
+ |
template<typename T> void NPTi<T>::scaleSimBox( void ){ |
| 80 |
|
|
| 81 |
< |
if( atoms[i]->isDirectional() ){ |
| 81 |
> |
double scaleFactor; |
| 82 |
|
|
| 85 |
– |
dAtom = (DirectionalAtom *)atoms[i]; |
| 86 |
– |
|
| 87 |
– |
// get and convert the torque to body frame |
| 88 |
– |
|
| 89 |
– |
dAtom->getTrq( Tb ); |
| 90 |
– |
dAtom->lab2Body( Tb ); |
| 91 |
– |
|
| 92 |
– |
// get the angular momentum, and propagate a half step |
| 93 |
– |
|
| 94 |
– |
dAtom->getJ( ji ); |
| 95 |
– |
|
| 96 |
– |
for (j=0; j < 3; j++) |
| 97 |
– |
ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); |
| 98 |
– |
|
| 99 |
– |
// use the angular velocities to propagate the rotation matrix a |
| 100 |
– |
// full time step |
| 101 |
– |
|
| 102 |
– |
dAtom->getA(A); |
| 103 |
– |
dAtom->getI(I); |
| 104 |
– |
|
| 105 |
– |
// rotate about the x-axis |
| 106 |
– |
angle = dt2 * ji[0] / I[0][0]; |
| 107 |
– |
this->rotate( 1, 2, angle, ji, A ); |
| 108 |
– |
|
| 109 |
– |
// rotate about the y-axis |
| 110 |
– |
angle = dt2 * ji[1] / I[1][1]; |
| 111 |
– |
this->rotate( 2, 0, angle, ji, A ); |
| 112 |
– |
|
| 113 |
– |
// rotate about the z-axis |
| 114 |
– |
angle = dt * ji[2] / I[2][2]; |
| 115 |
– |
this->rotate( 0, 1, angle, ji, A); |
| 116 |
– |
|
| 117 |
– |
// rotate about the y-axis |
| 118 |
– |
angle = dt2 * ji[1] / I[1][1]; |
| 119 |
– |
this->rotate( 2, 0, angle, ji, A ); |
| 120 |
– |
|
| 121 |
– |
// rotate about the x-axis |
| 122 |
– |
angle = dt2 * ji[0] / I[0][0]; |
| 123 |
– |
this->rotate( 1, 2, angle, ji, A ); |
| 124 |
– |
|
| 125 |
– |
dAtom->setJ( ji ); |
| 126 |
– |
dAtom->setA( A ); |
| 127 |
– |
} |
| 128 |
– |
|
| 129 |
– |
} |
| 130 |
– |
|
| 131 |
– |
// Scale the box after all the positions have been moved: |
| 132 |
– |
|
| 83 |
|
scaleFactor = exp(dt*eta); |
| 84 |
|
|
| 85 |
< |
if (scaleFactor > 1.1 || scaleFactor < 0.9) { |
| 85 |
> |
if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) { |
| 86 |
|
sprintf( painCave.errMsg, |
| 87 |
|
"NPTi error: Attempting a Box scaling of more than 10 percent" |
| 88 |
|
" check your tauBarostat, as it is probably too small!\n" |
| 91 |
|
painCave.isFatal = 1; |
| 92 |
|
simError(); |
| 93 |
|
} else { |
| 94 |
< |
info->scaleBox(exp(dt*eta)); |
| 95 |
< |
} |
| 94 |
> |
info->scaleBox(scaleFactor); |
| 95 |
> |
} |
| 96 |
> |
|
| 97 |
|
} |
| 98 |
|
|
| 99 |
< |
void NPTi::moveB( void ){ |
| 99 |
> |
template<typename T> bool NPTi<T>::etaConverged() { |
| 100 |
|
|
| 101 |
< |
int i, j; |
| 102 |
< |
DirectionalAtom* dAtom; |
| 152 |
< |
double Tb[3], ji[3]; |
| 153 |
< |
double vel[3], frc[3]; |
| 154 |
< |
double mass; |
| 101 |
> |
return ( fabs(prevEta - eta) <= etaTolerance ); |
| 102 |
> |
} |
| 103 |
|
|
| 104 |
< |
double instaTemp, instaPress, instaVol; |
| 157 |
< |
double tt2, tb2; |
| 158 |
< |
|
| 159 |
< |
tt2 = tauThermostat * tauThermostat; |
| 160 |
< |
tb2 = tauBarostat * tauBarostat; |
| 104 |
> |
template<typename T> double NPTi<T>::getConservedQuantity(void){ |
| 105 |
|
|
| 106 |
< |
instaTemp = tStats->getTemperature(); |
| 107 |
< |
instaPress = tStats->getPressure(); |
| 108 |
< |
instaVol = tStats->getVolume(); |
| 109 |
< |
|
| 110 |
< |
chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
| 111 |
< |
eta += dt2 * ( instaVol * (instaPress - targetPressure) / |
| 168 |
< |
(p_convert*NkBT*tb2)); |
| 106 |
> |
double conservedQuantity; |
| 107 |
> |
double Energy; |
| 108 |
> |
double thermostat_kinetic; |
| 109 |
> |
double thermostat_potential; |
| 110 |
> |
double barostat_kinetic; |
| 111 |
> |
double barostat_potential; |
| 112 |
|
|
| 113 |
< |
for( i=0; i<nAtoms; i++ ){ |
| 113 |
> |
Energy = tStats->getTotalE(); |
| 114 |
|
|
| 115 |
< |
atoms[i]->getVel( vel ); |
| 116 |
< |
atoms[i]->getFrc( frc ); |
| 115 |
> |
thermostat_kinetic = fkBT* tt2 * chi * chi / |
| 116 |
> |
(2.0 * eConvert); |
| 117 |
|
|
| 118 |
< |
mass = atoms[i]->getMass(); |
| 118 |
> |
thermostat_potential = fkBT* integralOfChidt / eConvert; |
| 119 |
|
|
| 177 |
– |
// velocity half step |
| 178 |
– |
for (j=0; j < 3; j++) |
| 179 |
– |
vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta)); |
| 180 |
– |
|
| 181 |
– |
atoms[i]->setVel( vel ); |
| 120 |
|
|
| 121 |
< |
if( atoms[i]->isDirectional() ){ |
| 122 |
< |
|
| 185 |
< |
dAtom = (DirectionalAtom *)atoms[i]; |
| 186 |
< |
|
| 187 |
< |
// get and convert the torque to body frame |
| 188 |
< |
|
| 189 |
< |
dAtom->getTrq( Tb ); |
| 190 |
< |
dAtom->lab2Body( Tb ); |
| 191 |
< |
|
| 192 |
< |
// get the angular momentum, and propagate a half step |
| 193 |
< |
|
| 194 |
< |
dAtom->getJ( ji ); |
| 195 |
< |
|
| 196 |
< |
for (j=0; j < 3; j++) |
| 197 |
< |
ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); |
| 198 |
< |
|
| 199 |
< |
dAtom->setJ( ji ); |
| 200 |
< |
} |
| 201 |
< |
} |
| 202 |
< |
} |
| 203 |
< |
|
| 204 |
< |
int NPTi::readyCheck() { |
| 205 |
< |
|
| 206 |
< |
// First check to see if we have a target temperature. |
| 207 |
< |
// Not having one is fatal. |
| 121 |
> |
barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta / |
| 122 |
> |
(2.0 * eConvert); |
| 123 |
|
|
| 124 |
< |
if (!have_target_temp) { |
| 125 |
< |
sprintf( painCave.errMsg, |
| 211 |
< |
"NPTi error: You can't use the NPTi integrator\n" |
| 212 |
< |
" without a targetTemp!\n" |
| 213 |
< |
); |
| 214 |
< |
painCave.isFatal = 1; |
| 215 |
< |
simError(); |
| 216 |
< |
return -1; |
| 217 |
< |
} |
| 124 |
> |
barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / |
| 125 |
> |
eConvert; |
| 126 |
|
|
| 127 |
< |
if (!have_target_pressure) { |
| 128 |
< |
sprintf( painCave.errMsg, |
| 221 |
< |
"NPTi error: You can't use the NPTi integrator\n" |
| 222 |
< |
" without a targetPressure!\n" |
| 223 |
< |
); |
| 224 |
< |
painCave.isFatal = 1; |
| 225 |
< |
simError(); |
| 226 |
< |
return -1; |
| 227 |
< |
} |
| 127 |
> |
conservedQuantity = Energy + thermostat_kinetic + thermostat_potential + |
| 128 |
> |
barostat_kinetic + barostat_potential; |
| 129 |
|
|
| 130 |
< |
// We must set tauThermostat. |
| 131 |
< |
|
| 231 |
< |
if (!have_tau_thermostat) { |
| 232 |
< |
sprintf( painCave.errMsg, |
| 233 |
< |
"NPTi error: If you use the NPTi\n" |
| 234 |
< |
" integrator, you must set tauThermostat.\n"); |
| 235 |
< |
painCave.isFatal = 1; |
| 236 |
< |
simError(); |
| 237 |
< |
return -1; |
| 238 |
< |
} |
| 130 |
> |
// cout.width(8); |
| 131 |
> |
// cout.precision(8); |
| 132 |
|
|
| 133 |
< |
// We must set tauBarostat. |
| 134 |
< |
|
| 135 |
< |
if (!have_tau_barostat) { |
| 136 |
< |
sprintf( painCave.errMsg, |
| 244 |
< |
"NPTi error: If you use the NPTi\n" |
| 245 |
< |
" integrator, you must set tauBarostat.\n"); |
| 246 |
< |
painCave.isFatal = 1; |
| 247 |
< |
simError(); |
| 248 |
< |
return -1; |
| 249 |
< |
} |
| 250 |
< |
|
| 251 |
< |
// We need NkBT a lot, so just set it here: |
| 252 |
< |
|
| 253 |
< |
NkBT = (double)info->ndf * kB * targetTemp; |
| 254 |
< |
|
| 255 |
< |
return 1; |
| 133 |
> |
// cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << |
| 134 |
> |
// "\t" << thermostat_potential << "\t" << barostat_kinetic << |
| 135 |
> |
// "\t" << barostat_potential << "\t" << conservedQuantity << endl; |
| 136 |
> |
return conservedQuantity; |
| 137 |
|
} |