--- trunk/OOPSE/libmdtools/NPTi.cpp 2003/07/09 01:41:11 577 +++ trunk/OOPSE/libmdtools/NPTi.cpp 2003/07/31 15:35:07 658 @@ -1,3 +1,4 @@ +#include #include "Atom.hpp" #include "SRI.hpp" #include "AbstractClasses.hpp" @@ -19,8 +20,8 @@ NPTi::NPTi ( SimInfo *theInfo, ForceFields* the_ff): // // Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. -NPTi::NPTi ( SimInfo *theInfo, ForceFields* the_ff): - Integrator( theInfo, the_ff ) +template NPTi::NPTi ( SimInfo *theInfo, ForceFields* the_ff): + T( theInfo, the_ff ) { chi = 0.0; eta = 0.0; @@ -30,17 +31,18 @@ void NPTi::moveA() { have_target_pressure = 0; } -void NPTi::moveA() { +template void NPTi::moveA() { - int i,j,k; - int atomIndex, aMatIndex; + int i, j; DirectionalAtom* dAtom; - double Tb[3]; - double ji[3]; + double Tb[3], ji[3]; + double A[3][3], I[3][3]; + double angle, mass; + double vel[3], pos[3], frc[3]; + double rj[3]; double instaTemp, instaPress, instaVol; - double tt2, tb2; - double angle; + double tt2, tb2, scaleFactor; tt2 = tauThermostat * tauThermostat; tb2 = tauBarostat * tauBarostat; @@ -49,95 +51,107 @@ void NPTi::moveA() { instaPress = tStats->getPressure(); instaVol = tStats->getVolume(); - // first evolve chi a half step + // first evolve chi a half step chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); + eta += dt2 * ( instaVol * (instaPress - targetPressure) / + (p_convert*NkBT*tb2)); for( i=0; igetMass())*eConvert - - vel[j]*(chi+eta)); + atoms[i]->getVel( vel ); + atoms[i]->getPos( pos ); + atoms[i]->getFrc( frc ); - // position whole step + mass = atoms[i]->getMass(); - rj[0] = pos[atomIndex]; - rj[1] = pos[atomIndex+1]; - rj[2] = pos[atomIndex+2]; - + for (j=0; j < 3; j++) { + vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta)); + rj[j] = pos[j]; + } + + atoms[i]->setVel( vel ); + info->wrapVector(rj); - pos[atomIndex] += dt * (vel[atomIndex] + eta*rj[0]); - pos[atomIndex+1] += dt * (vel[atomIndex+1] + eta*rj[1]); - pos[atomIndex+2] += dt * (vel[atomIndex+2] + eta*rj[2]); - + for (j = 0; j < 3; j++) + pos[j] += dt * (vel[j] + eta*rj[j]); + + atoms[i]->setPos( pos ); + 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->getTrq( Tb ); dAtom->lab2Body( Tb ); // get the angular momentum, and propagate a half step - ji[0] = dAtom->getJx(); - ji[1] = dAtom->getJy(); - ji[2] = dAtom->getJz(); + dAtom->getJ( ji ); + + for (j=0; j < 3; j++) + ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); - 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 - + + dAtom->getA(A); + dAtom->getI(I); + // rotate about the x-axis - angle = dt2 * ji[0] / dAtom->getIxx(); - this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); - + angle = dt2 * ji[0] / I[0][0]; + this->rotate( 1, 2, angle, ji, A ); + // rotate about the y-axis - angle = dt2 * ji[1] / dAtom->getIyy(); - this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); + angle = dt2 * ji[1] / I[1][1]; + this->rotate( 2, 0, angle, ji, A ); // rotate about the z-axis - angle = dt * ji[2] / dAtom->getIzz(); - this->rotate( 0, 1, angle, ji, &Amat[aMatIndex] ); + angle = dt * ji[2] / I[2][2]; + this->rotate( 0, 1, angle, ji, A); // rotate about the y-axis - angle = dt2 * ji[1] / dAtom->getIyy(); - this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); + angle = dt2 * ji[1] / I[1][1]; + this->rotate( 2, 0, angle, ji, A ); // rotate about the x-axis - angle = dt2 * ji[0] / dAtom->getIxx(); - this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); + angle = dt2 * ji[0] / I[0][0]; + this->rotate( 1, 2, angle, ji, A ); - dAtom->setJx( ji[0] ); - dAtom->setJy( ji[1] ); - dAtom->setJz( ji[2] ); - } - + dAtom->setJ( ji ); + dAtom->setA( A ); + } + } + // Scale the box after all the positions have been moved: + + scaleFactor = exp(dt*eta); - info->scaleBox(exp(dt*eta)); + if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) { + sprintf( painCave.errMsg, + "NPTi error: Attempting a Box scaling of more than 10 percent" + " check your tauBarostat, as it is probably too small!\n" + " eta = %lf, scaleFactor = %lf\n", eta, scaleFactor + ); + painCave.isFatal = 1; + simError(); + } else { + info->scaleBox(exp(dt*eta)); + } } -void NPTi::moveB( void ){ - int i,j,k; - int atomIndex; +template void NPTi::moveB( void ){ + + int i, j; DirectionalAtom* dAtom; - double Tb[3]; - double ji[3]; + double Tb[3], ji[3]; + double vel[3], frc[3]; + double mass; + double instaTemp, instaPress, instaVol; double tt2, tb2; @@ -149,48 +163,48 @@ void NPTi::moveB( void ){ instaVol = tStats->getVolume(); chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); + eta += dt2 * ( instaVol * (instaPress - targetPressure) / + (p_convert*NkBT*tb2)); for( i=0; igetVel( vel ); + atoms[i]->getFrc( frc ); + + mass = atoms[i]->getMass(); + // velocity half step - for( j=atomIndex; j<(atomIndex+3); j++ ) - for( j=atomIndex; j<(atomIndex+3); j++ ) - vel[j] += dt2 * ((frc[j]/atoms[i]->getMass())*eConvert - - vel[j]*(chi+eta)); + for (j=0; j < 3; j++) + vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta)); + atoms[i]->setVel( vel ); + 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(); - + + // get and convert the torque to body frame + + dAtom->getTrq( Tb ); 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] ); + + // get the angular momentum, and propagate a half step + + dAtom->getJ( ji ); + + for (j=0; j < 3; j++) + ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); + + dAtom->setJ( ji ); } } } -int NPTi::readyCheck() { +template int NPTi::readyCheck() { + + //check parent's readyCheck() first + if (T::readyCheck() == -1) + return -1; // First check to see if we have a target temperature. // Not having one is fatal.