--- trunk/OOPSE/libmdtools/NPTf.cpp 2003/07/09 01:41:11 577 +++ trunk/OOPSE/libmdtools/NPTf.cpp 2003/09/23 20:34:31 782 @@ -1,3 +1,4 @@ +#include #include "Atom.hpp" #include "SRI.hpp" #include "AbstractClasses.hpp" @@ -8,8 +9,11 @@ #include "Integrator.hpp" #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, @@ -19,272 +23,252 @@ // // 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; + + int i,j; + + for(i = 0; i < 3; i++){ + for (j = 0; j < 3; j++){ + + eta[i][j] = 0.0; + oldEta[i][j] = 0.0; + } + } } -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; +template NPTf::~NPTf() { - tt2 = tauThermostat * tauThermostat; - tb2 = tauBarostat * tauBarostat; + // empty for now +} - instaTemp = tStats->getTemperature(); - tStats->getPressureTensor(press); +template void NPTf::resetIntegrator() { + + int i, j; + + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + eta[i][j] = 0.0; + + T::resetIntegrator(); +} - for (i=0; i < 9; i++) press[i] *= p_convert; - - instaVol = tStats->getVolume(); - - // first evolve chi a half step +template void NPTf::evolveEtaA() { - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; + int i, j; - 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; 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); + } + } - 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); + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + oldEta[i][j] = eta[i][j]; +} - vel[atomIndex] = vx; - vel[atomIndex+1] = vy; - vel[atomIndex+2] = vz; +template void NPTf::evolveEtaB() { + + int i,j; - // position whole step + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + prevEta[i][j] = eta[i][j]; - rj[0] = pos[atomIndex]; - rj[1] = pos[atomIndex+1]; - rj[2] = pos[atomIndex+2]; + 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); + } + } + } +} - info->wrapVector(rj); +template void NPTf::getVelScaleA(double sc[3], double vel[3]) { + int i,j; + double vScale[3][3]; - 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]; - - 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() ){ - - dAtom = (DirectionalAtom *)atoms[i]; - - // get and convert the torque to body frame + for (i = 0; i < 3; i++ ) { + for (j = 0; j < 3; j++ ) { + vScale[i][j] = eta[i][j]; - Tb[0] = dAtom->getTx(); - Tb[1] = dAtom->getTy(); - Tb[2] = dAtom->getTz(); - - dAtom->lab2Body( Tb ); - - // get the angular momentum, and propagate a 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); - - // 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] ); + if (i == j) { + vScale[i][j] += chi; + } } - } - - // Scale the box after all the positions have been moved: + info->matVecMul3( vScale, vel, sc ); +} +template void NPTf::getVelScaleB(double sc[3], int index ){ + int i,j; + double myVel[3]; + double vScale[3][3]; - // Use a taylor expansion for eta products - - info->getBoxM(hm); + 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 ); +} +template void NPTf::getPosScale(double pos[3], double COM[3], + int index, double sc[3]){ + int j; + double rj[3]; - - - info->scaleBox(exp(dt*eta)); - + for(j=0; j<3; j++) + rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; + info->matVecMul3( eta, rj, sc ); } -void NPTi::moveB( void ){ +template void NPTf::scaleSimBox( void ){ + int i,j,k; - int atomIndex; - DirectionalAtom* dAtom; - double Tb[3]; - double ji[3]; - double instaTemp, instaPress, instaVol; - double tt2, tb2; + double scaleMat[3][3]; + double eta2ij; + double bigScale, smallScale, offDiagMax; + double hm[3][3], hmnew[3][3]; - tt2 = tauThermostat * tauThermostat; - tb2 = tauBarostat * tauBarostat; - instaTemp = tStats->getTemperature(); - instaPress = tStats->getPressure(); - instaVol = tStats->getVolume(); - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); + // Scale the box after all the positions have been moved: - for( i=0; igetMass())*eConvert - - vel[j]*(chi+eta)); - - if( atoms[i]->isDirectional() ){ + // 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++){ - dAtom = (DirectionalAtom *)atoms[i]; + // Calculate the matrix Product of the eta array (we only need + // the ij element right now): - // get and convert the torque to body frame + eta2ij = 0.0; + for(k=0; k<3; k++){ + eta2ij += eta[i][k] * eta[k][j]; + } - 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] ); + 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]); } - } -} -int NPTi::readyCheck() { - - // First check to see if we have a target temperature. - // Not having one is fatal. + if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i]; + if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i]; + } - if (!have_target_temp) { + if ((bigScale > 1.1) || (smallScale < 0.9)) { sprintf( painCave.errMsg, - "NPTi error: You can't use the NPTi integrator\n" - " without a targetTemp!\n" - ); + "NPTf error: Attempting a Box scaling of more than 10 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; - } - - if (!have_target_pressure) { + } else if (offDiagMax > 0.1) { sprintf( painCave.errMsg, - "NPTi error: You can't use the NPTi integrator\n" - " without a targetPressure!\n" - ); + "NPTf error: Attempting an off-diagonal Box scaling of more than 10 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); } +} + +template bool NPTf::etaConverged() { + int i; + double diffEta, sumEta; + + sumEta = 0; + for(i = 0; i < 3; i++) + sumEta += pow(prevEta[i][i] - eta[i][i], 2); - // We must set tauThermostat. - - if (!have_tau_thermostat) { - sprintf( painCave.errMsg, - "NPTi error: If you use the NPTi\n" - " integrator, you must set tauThermostat.\n"); - painCave.isFatal = 1; - simError(); - return -1; - } + diffEta = sqrt( sumEta / 3.0 ); + + return ( diffEta <= etaTolerance ); +} - // 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 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]; - // We need NkBT a lot, so just set it here: + totalEnergy = tStats->getTotalE(); - NkBT = (double)info->ndf * kB * targetTemp; + thermostat_kinetic = fkBT* tt2 * chi * chi / + (2.0 * eConvert); - return 1; + 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; + +// cout.width(8); +// cout.precision(8); + +// cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << +// "\t" << thermostat_potential << "\t" << barostat_kinetic << +// "\t" << barostat_potential << "\t" << conservedQuantity << endl; + + return conservedQuantity; + }