--- trunk/OOPSE/libmdtools/NPTf.cpp 2003/07/09 02:15:29 578 +++ trunk/OOPSE/libmdtools/NPTf.cpp 2003/09/04 21:48:35 746 @@ -1,3 +1,4 @@ +#include #include "Atom.hpp" #include "SRI.hpp" #include "AbstractClasses.hpp" @@ -19,97 +20,98 @@ NPTf::NPTf ( SimInfo *theInfo, ForceFields* the_ff): // // 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; + int i, j; chi = 0.0; - for(i = 0; i < 9; i++) eta[i] = 0.0; + + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + eta[i][j] = 0.0; + have_tau_thermostat = 0; have_tau_barostat = 0; have_target_temp = 0; have_target_pressure = 0; } -void NPTf::moveA() { +template void NPTf::moveA() { - int i,j,k; - int atomIndex, aMatIndex; + int i, j, k; 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 ident[3][3], eta1[3][3], eta2[3][3], hmnew[3][3]; - double hm[9]; - double vx, vy, vz; - double scx, scy, scz; double instaTemp, instaPress, instaVol; double tt2, tb2; - double angle; - double press[9]; - const double p_convert = 1.63882576e8; + double sc[3]; + double eta2ij; + double press[3][3], vScale[3][3], hm[3][3], hmnew[3][3], scaleMat[3][3]; + double bigScale, smallScale, offDiagMax; tt2 = tauThermostat * tauThermostat; tb2 = tauBarostat * tauBarostat; instaTemp = tStats->getTemperature(); tStats->getPressureTensor(press); - - for (i=0; i < 9; i++) press[i] *= p_convert; - 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; 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); + + vScale[i][j] = eta[i][j] + chi; + + } else { + + eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2); + + vScale[i][j] = eta[i][j]; + + } + } + } + for( i=0; igetVel( vel ); + atoms[i]->getPos( pos ); + atoms[i]->getFrc( frc ); + + mass = atoms[i]->getMass(); // velocity half step + + info->matVecMul3( vScale, vel, sc ); - vx = vel[atomIndex]; - vy = vel[atomIndex+1]; - vz = vel[atomIndex+2]; - - scx = (chi + eta[0])*vx + eta[1]*vy + eta[2]*vz; - scy = eta[3]*vx + (chi + eta[4])*vy + eta[5]*vz; - scz = eta[6]*vx + eta[7]*vy + (chi + eta[8])*vz; - - vx += dt2 * ((frc[atomIndex] /atoms[i]->getMass())*eConvert - scx); - vy += dt2 * ((frc[atomIndex+1]/atoms[i]->getMass())*eConvert - scy); - vz += dt2 * ((frc[atomIndex+2]/atoms[i]->getMass())*eConvert - scz); + for (j = 0; j < 3; j++) { + vel[j] += dt2 * ((frc[j] / mass) * eConvert - sc[j]); + rj[j] = pos[j]; + } - vel[atomIndex] = vx; - vel[atomIndex+1] = vy; - vel[atomIndex+2] = vz; + atoms[i]->setVel( vel ); // position whole step - rj[0] = pos[atomIndex]; - rj[1] = pos[atomIndex+1]; - rj[2] = pos[atomIndex+2]; - info->wrapVector(rj); - 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]; + info->matVecMul3( eta, rj, sc ); - pos[atomIndex] += dt * (vel[atomIndex] + scx); - pos[atomIndex+1] += dt * (vel[atomIndex+1] + scy); - pos[atomIndex+2] += dt * (vel[atomIndex+2] + scz); + for (j = 0; j < 3; j++ ) + pos[j] += dt * (vel[j] + sc[j]); + + atoms[i]->setPos( pos ); if( atoms[i]->isDirectional() ){ @@ -117,192 +119,220 @@ void NPTf::moveA() { // 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: - + // 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++){ - ident[i][j] = 0.0; - eta1[i][j] = eta[3*i+j]; - eta2[i][j] = 0.0; + + // 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++){ - eta2[i][j] += eta[3*i+k] * eta[3*k+j]; + 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]); + } - ident[i][i] = 1.0; - } - - info->getBoxM(hm); - - for(i=0; i<3; i++){ - for(j=0; j<3; j++){ - hmnew[i][j] = 0.0; - for(k=0; k<3; k++){ - // remember that hmat has transpose ordering for Fortran compat: - hmnew[i][j] += hm[3*k+i] * (ident[k][j] - + dt * eta1[k][j] - + 0.5 * dt * dt * eta2[k][j]); - } - } + if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i]; + if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i]; } - for (i = 0; i < 3; i++) { - for (j = 0; j < 3; j++) { - // remember that hmat has transpose ordering for Fortran compat: - hm[3*j + 1] = hmnew[i][j]; - } + if ((bigScale > 1.1) || (smallScale < 0.9)) { + sprintf( painCave.errMsg, + "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(); + } else if (offDiagMax > 0.1) { + sprintf( painCave.errMsg, + "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(); + } else { + info->getBoxM(hm); + info->matMul3(hm, scaleMat, hmnew); + info->setBoxM(hmnew); } - - info->setBoxM(hm); } -void NPTf::moveB( void ){ - int i,j,k; - int atomIndex; +template void NPTf::moveB( void ){ + + int i, j; DirectionalAtom* dAtom; - double Tb[3]; - double ji[3]; - double press[9]; - double instaTemp, instaVol; + double Tb[3], ji[3]; + double vel[3], frc[3]; + double mass; + + double instaTemp, instaPress, instaVol; double tt2, tb2; - double vx, vy, vz; - double scx, scy, scz; - const double p_convert = 1.63882576e8; + double sc[3]; + double press[3][3], vScale[3][3]; tt2 = tauThermostat * tauThermostat; tb2 = tauBarostat * tauBarostat; instaTemp = tStats->getTemperature(); tStats->getPressureTensor(press); - - for (i=0; i < 9; i++) press[i] *= p_convert; - 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; 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); + + vScale[i][j] = eta[i][j] + chi; + + } else { + + eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2); + + vScale[i][j] = eta[i][j]; + + } + } + } + for( i=0; igetVel( vel ); + atoms[i]->getFrc( frc ); + + mass = atoms[i]->getMass(); + // velocity half step + + info->matVecMul3( vScale, vel, sc ); - vx = vel[atomIndex]; - vy = vel[atomIndex+1]; - vz = vel[atomIndex+2]; - - scx = (chi + eta[0])*vx + eta[1]*vy + eta[2]*vz; - scy = eta[3]*vx + (chi + eta[4])*vy + eta[5]*vz; - scz = eta[6]*vx + eta[7]*vy + (chi + eta[8])*vz; - - vx += dt2 * ((frc[atomIndex] /atoms[i]->getMass())*eConvert - scx); - vy += dt2 * ((frc[atomIndex+1]/atoms[i]->getMass())*eConvert - scy); - vz += dt2 * ((frc[atomIndex+2]/atoms[i]->getMass())*eConvert - scz); + for (j = 0; j < 3; j++) { + vel[j] += dt2 * ((frc[j] / mass) * eConvert - sc[j]); + } - vel[atomIndex] = vx; - vel[atomIndex+1] = vy; - vel[atomIndex+2] = vz; + 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(); - + dAtom->getTrq( Tb ); dAtom->lab2Body( Tb ); - // get the angular momentum, and complete the angular momentum - // half step + // get the angular momentum, and propagate a half step - ji[0] = dAtom->getJx(); - ji[1] = dAtom->getJy(); - ji[2] = dAtom->getJz(); + dAtom->getJ( ji ); - 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); + for (j=0; j < 3; j++) + ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); - dAtom->setJx( ji[0] ); - dAtom->setJy( ji[1] ); - dAtom->setJz( ji[2] ); - } + dAtom->setJ( ji ); + + } } } -int NPTi::readyCheck() { +template void NPTf::resetIntegrator() { + int i,j; + + chi = 0.0; + + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + eta[i][j] = 0.0; + +} + +template int NPTf::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. if (!have_target_temp) { sprintf( painCave.errMsg, - "NPTi error: You can't use the NPTi integrator\n" + "NPTf error: You can't use the NPTf integrator\n" " without a targetTemp!\n" ); painCave.isFatal = 1; @@ -312,7 +342,7 @@ int NPTi::readyCheck() { if (!have_target_pressure) { sprintf( painCave.errMsg, - "NPTi error: You can't use the NPTi integrator\n" + "NPTf error: You can't use the NPTf integrator\n" " without a targetPressure!\n" ); painCave.isFatal = 1; @@ -324,7 +354,7 @@ int NPTi::readyCheck() { if (!have_tau_thermostat) { sprintf( painCave.errMsg, - "NPTi error: If you use the NPTi\n" + "NPTf error: If you use the NPTf\n" " integrator, you must set tauThermostat.\n"); painCave.isFatal = 1; simError(); @@ -335,7 +365,7 @@ int NPTi::readyCheck() { if (!have_tau_barostat) { sprintf( painCave.errMsg, - "NPTi error: If you use the NPTi\n" + "NPTf error: If you use the NPTf\n" " integrator, you must set tauBarostat.\n"); painCave.isFatal = 1; simError();