--- trunk/OOPSE/libmdtools/NPTf.cpp 2003/07/09 01:41:11 577 +++ trunk/OOPSE/libmdtools/NPTf.cpp 2003/11/06 22:01:37 855 @@ -1,3 +1,4 @@ +#include #include "Atom.hpp" #include "SRI.hpp" #include "AbstractClasses.hpp" @@ -6,285 +7,301 @@ #include "Thermo.hpp" #include "ReadWrite.hpp" #include "Integrator.hpp" -#include "simError.h" +#include "simError.h" +#ifdef IS_MPI +#include "mpiSimulation.hpp" +#endif -// Basic isotropic thermostating and barostating via the Melchionna +// Basic non-isotropic thermostating and barostating via the Melchionna // modification of the Hoover algorithm: // // Melchionna, S., Ciccotti, G., and Holian, B. L., 1993, -// Molec. Phys., 78, 533. +// Molec. Phys., 78, 533. // // and -// +// // Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. -NPTf::NPTf ( SimInfo *theInfo, ForceFields* the_ff): - Integrator( theInfo, the_ff ) +template NPTf::NPTf ( SimInfo *theInfo, ForceFields* the_ff): + T( theInfo, the_ff ) { - int i; - chi = 0.0; - for(i = 0; i < 9; i++) eta[i] = 0.0; - have_tau_thermostat = 0; - have_tau_barostat = 0; - have_target_temp = 0; - have_target_pressure = 0; -} + GenericData* data; + DoubleArrayData * etaValue; + vector etaArray; + int i,j; -void NPTf::moveA() { - - int i,j,k; - int atomIndex, aMatIndex; - DirectionalAtom* dAtom; - double Tb[3]; - double ji[3]; - double rj[3]; - double instaTemp, instaPress, instaVol; - double tt2, tb2; - double angle; - double press[9]; - const double p_convert = 1.63882576e8; + for(i = 0; i < 3; i++){ + for (j = 0; j < 3; j++){ - tt2 = tauThermostat * tauThermostat; - tb2 = tauBarostat * tauBarostat; + eta[i][j] = 0.0; + oldEta[i][j] = 0.0; + } + } - instaTemp = tStats->getTemperature(); - tStats->getPressureTensor(press); - for (i=0; i < 9; i++) press[i] *= p_convert; + if( theInfo->useInitXSstate ){ + // retrieve eta array from simInfo if it exists + data = info->getProperty(ETAVALUE_ID); + if(data){ + etaValue = dynamic_cast(data); + + if(etaValue){ + etaArray = etaValue->getData(); + + for(i = 0; i < 3; i++){ + for (j = 0; j < 3; j++){ + eta[i][j] = etaArray[3*i+j]; + oldEta[i][j] = eta[i][j]; + } + } + } + } + } - instaVol = tStats->getVolume(); - - // first evolve chi a half step - - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - - eta[0] += dt2 * instaVol * (press[0] - targetPressure) / (NkBT*tb2); - eta[1] += dt2 * instaVol * press[1] / (NkBT*tb2); - eta[2] += dt2 * instaVol * press[2] / (NkBT*tb2); - eta[3] += dt2 * instaVol * press[3] / (NkBT*tb2); - eta[4] += dt2 * instaVol * (press[4] - targetPressure) / (NkBT*tb2); - eta[5] += dt2 * instaVol * press[5] / (NkBT*tb2); - eta[6] += dt2 * instaVol * press[6] / (NkBT*tb2); - eta[7] += dt2 * instaVol * press[7] / (NkBT*tb2); - eta[8] += dt2 * instaVol * (press[8] - targetPressure) / (NkBT*tb2); - - for( i=0; igetMass())*eConvert - scx); - vy += dt2 * ((frc[atomIndex+1]/atoms[i]->getMass())*eConvert - scy); - vz += dt2 * ((frc[atomIndex+2]/atoms[i]->getMass())*eConvert - scz); +} - vel[atomIndex] = vx; - vel[atomIndex+1] = vy; - vel[atomIndex+2] = vz; +template NPTf::~NPTf() { - // position whole step + // empty for now +} - rj[0] = pos[atomIndex]; - rj[1] = pos[atomIndex+1]; - rj[2] = pos[atomIndex+2]; +template void NPTf::resetIntegrator() { - info->wrapVector(rj); + int i, j; - scx = eta[0]*rj[0] + eta[1]*rj[1] + eta[2]*rj[2]; - scy = eta[3]*rj[0] + eta[4]*rj[1] + eta[5]*rj[2]; - scz = eta[6]*rj[0] + eta[7]*rj[1] + eta[8]*rj[2]; + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + eta[i][j] = 0.0; - pos[atomIndex] += dt * (vel[atomIndex] + scx); - pos[atomIndex+1] += dt * (vel[atomIndex+1] + scy); - pos[atomIndex+2] += dt * (vel[atomIndex+2] + scz); - - if( atoms[i]->isDirectional() ){ + T::resetIntegrator(); +} - dAtom = (DirectionalAtom *)atoms[i]; - - // get and convert the torque to body frame - - Tb[0] = dAtom->getTx(); - Tb[1] = dAtom->getTy(); - Tb[2] = dAtom->getTz(); - - dAtom->lab2Body( Tb ); - - // get the angular momentum, and propagate a half step +template void NPTf::evolveEtaA() { - ji[0] = dAtom->getJx(); - ji[1] = dAtom->getJy(); - ji[2] = dAtom->getJz(); - - ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi); - ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi); - ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi); - - // use the angular velocities to propagate the rotation matrix a - // full time step - - // rotate about the x-axis - angle = dt2 * ji[0] / dAtom->getIxx(); - this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); - - // rotate about the y-axis - angle = dt2 * ji[1] / dAtom->getIyy(); - this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); - - // rotate about the z-axis - angle = dt * ji[2] / dAtom->getIzz(); - this->rotate( 0, 1, angle, ji, &Amat[aMatIndex] ); - - // rotate about the y-axis - angle = dt2 * ji[1] / dAtom->getIyy(); - this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); - - // rotate about the x-axis - angle = dt2 * ji[0] / dAtom->getIxx(); - this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); - - dAtom->setJx( ji[0] ); - dAtom->setJy( ji[1] ); - dAtom->setJz( ji[2] ); + int i, j; + + for(i = 0; i < 3; i ++){ + for(j = 0; j < 3; j++){ + if( i == j) + eta[i][j] += dt2 * instaVol * + (press[i][j] - targetPressure/p_convert) / (NkBT*tb2); + else + eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2); } - } - // Scale the box after all the positions have been moved: - + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + oldEta[i][j] = eta[i][j]; +} +template void NPTf::evolveEtaB() { - // Use a taylor expansion for eta products - - info->getBoxM(hm); - + int i,j; + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + prevEta[i][j] = eta[i][j]; + for(i = 0; i < 3; i ++){ + for(j = 0; j < 3; j++){ + if( i == j) { + eta[i][j] = oldEta[i][j] + dt2 * instaVol * + (press[i][j] - targetPressure/p_convert) / (NkBT*tb2); + } else { + eta[i][j] = oldEta[i][j] + dt2 * instaVol * press[i][j] / (NkBT*tb2); + } + } + } +} +template void NPTf::getVelScaleA(double sc[3], double vel[3]) { + int i,j; + double vScale[3][3]; + for (i = 0; i < 3; i++ ) { + for (j = 0; j < 3; j++ ) { + vScale[i][j] = eta[i][j]; - info->scaleBox(exp(dt*eta)); + if (i == j) { + vScale[i][j] += chi; + } + } + } - + info->matVecMul3( vScale, vel, sc ); } -void NPTi::moveB( void ){ - int i,j,k; - int atomIndex; - DirectionalAtom* dAtom; - double Tb[3]; - double ji[3]; - double instaTemp, instaPress, instaVol; - double tt2, tb2; - - tt2 = tauThermostat * tauThermostat; - tb2 = tauBarostat * tauBarostat; - - instaTemp = tStats->getTemperature(); - instaPress = tStats->getPressure(); - instaVol = tStats->getVolume(); +template void NPTf::getVelScaleB(double sc[3], int index ){ + int i,j; + double myVel[3]; + double vScale[3][3]; - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); - - for( i=0; igetMass())*eConvert - - vel[j]*(chi+eta)); - - if( atoms[i]->isDirectional() ){ - - dAtom = (DirectionalAtom *)atoms[i]; - - // get and convert the torque to body frame - - Tb[0] = dAtom->getTx(); - Tb[1] = dAtom->getTy(); - Tb[2] = dAtom->getTz(); - - dAtom->lab2Body( Tb ); - - // get the angular momentum, and complete the angular momentum - // half step - - ji[0] = dAtom->getJx(); - ji[1] = dAtom->getJy(); - ji[2] = dAtom->getJz(); - - ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi); - ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi); - ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi); - - dAtom->setJx( ji[0] ); - dAtom->setJy( ji[1] ); - dAtom->setJz( ji[2] ); + for (i = 0; i < 3; i++ ) { + for (j = 0; j < 3; j++ ) { + vScale[i][j] = eta[i][j]; + + if (i == j) { + vScale[i][j] += chi; + } } } + + for (j = 0; j < 3; j++) + myVel[j] = oldVel[3*index + j]; + + info->matVecMul3( vScale, myVel, sc ); } -int NPTi::readyCheck() { - - // First check to see if we have a target temperature. - // Not having one is fatal. - - if (!have_target_temp) { - sprintf( painCave.errMsg, - "NPTi error: You can't use the NPTi integrator\n" - " without a targetTemp!\n" - ); - painCave.isFatal = 1; - simError(); - return -1; +template void NPTf::getPosScale(double pos[3], double COM[3], + int index, double sc[3]){ + int j; + double rj[3]; + + for(j=0; j<3; j++) + rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; + + info->matVecMul3( eta, rj, sc ); +} + +template void NPTf::scaleSimBox( void ){ + + int i,j,k; + double scaleMat[3][3]; + double eta2ij; + double bigScale, smallScale, offDiagMax; + double hm[3][3], hmnew[3][3]; + + + + // Scale the box after all the positions have been moved: + + // Use a taylor expansion for eta products: Hmat = Hmat . exp(dt * etaMat) + // Hmat = Hmat . ( Ident + dt * etaMat + dt^2 * etaMat*etaMat / 2) + + bigScale = 1.0; + smallScale = 1.0; + offDiagMax = 0.0; + + for(i=0; i<3; i++){ + for(j=0; j<3; j++){ + + // Calculate the matrix Product of the eta array (we only need + // the ij element right now): + + eta2ij = 0.0; + for(k=0; k<3; k++){ + eta2ij += eta[i][k] * eta[k][j]; + } + + scaleMat[i][j] = 0.0; + // identity matrix (see above): + if (i == j) scaleMat[i][j] = 1.0; + // Taylor expansion for the exponential truncated at second order: + scaleMat[i][j] += dt*eta[i][j] + 0.5*dt*dt*eta2ij; + + if (i != j) + if (fabs(scaleMat[i][j]) > offDiagMax) + offDiagMax = fabs(scaleMat[i][j]); + } + + if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i]; + if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i]; } - if (!have_target_pressure) { + if ((bigScale > 1.01) || (smallScale < 0.99)) { sprintf( painCave.errMsg, - "NPTi error: You can't use the NPTi integrator\n" - " without a targetPressure!\n" - ); + "NPTf error: Attempting a Box scaling of more than 1 percent.\n" + " Check your tauBarostat, as it is probably too small!\n\n" + " scaleMat = [%lf\t%lf\t%lf]\n" + " [%lf\t%lf\t%lf]\n" + " [%lf\t%lf\t%lf]\n", + scaleMat[0][0],scaleMat[0][1],scaleMat[0][2], + scaleMat[1][0],scaleMat[1][1],scaleMat[1][2], + scaleMat[2][0],scaleMat[2][1],scaleMat[2][2]); painCave.isFatal = 1; simError(); - return -1; - } - - // We must set tauThermostat. - - if (!have_tau_thermostat) { + } else if (offDiagMax > 0.01) { sprintf( painCave.errMsg, - "NPTi error: If you use the NPTi\n" - " integrator, you must set tauThermostat.\n"); + "NPTf error: Attempting an off-diagonal Box scaling of more than 1 percent.\n" + " Check your tauBarostat, as it is probably too small!\n\n" + " scaleMat = [%lf\t%lf\t%lf]\n" + " [%lf\t%lf\t%lf]\n" + " [%lf\t%lf\t%lf]\n", + scaleMat[0][0],scaleMat[0][1],scaleMat[0][2], + scaleMat[1][0],scaleMat[1][1],scaleMat[1][2], + scaleMat[2][0],scaleMat[2][1],scaleMat[2][2]); painCave.isFatal = 1; simError(); - return -1; - } + } else { + info->getBoxM(hm); + info->matMul3(hm, scaleMat, hmnew); + info->setBoxM(hmnew); + } +} - // We must set tauBarostat. - - if (!have_tau_barostat) { - sprintf( painCave.errMsg, - "NPTi error: If you use the NPTi\n" - " integrator, you must set tauBarostat.\n"); - painCave.isFatal = 1; - simError(); - return -1; - } +template bool NPTf::etaConverged() { + int i; + double diffEta, sumEta; - // We need NkBT a lot, so just set it here: + sumEta = 0; + for(i = 0; i < 3; i++) + sumEta += pow(prevEta[i][i] - eta[i][i], 2); - NkBT = (double)info->ndf * kB * targetTemp; + diffEta = sqrt( sumEta / 3.0 ); - return 1; + return ( diffEta <= etaTolerance ); } + +template double NPTf::getConservedQuantity(void){ + + double conservedQuantity; + double totalEnergy; + double thermostat_kinetic; + double thermostat_potential; + double barostat_kinetic; + double barostat_potential; + double trEta; + double a[3][3], b[3][3]; + + totalEnergy = tStats->getTotalE(); + + thermostat_kinetic = fkBT * tt2 * chi * chi / + (2.0 * eConvert); + + thermostat_potential = fkBT* integralOfChidt / eConvert; + + info->transposeMat3(eta, a); + info->matMul3(a, eta, b); + trEta = info->matTrace3(b); + + barostat_kinetic = NkBT * tb2 * trEta / + (2.0 * eConvert); + + barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / + eConvert; + + conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential + + barostat_kinetic + barostat_potential; + + return conservedQuantity; + +} + +template string NPTf::getAdditionalParameters(void){ + string parameters; + const int BUFFERSIZE = 2000; // size of the read buffer + char buffer[BUFFERSIZE]; + + sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt); + parameters += buffer; + + for(int i = 0; i < 3; i++){ + sprintf(buffer,"\t%G\t%G\t%G;", eta[i][0], eta[i][1], eta[i][2]); + parameters += buffer; + } + + return parameters; + +}