--- trunk/OOPSE/libmdtools/NPTi.cpp 2003/07/09 22:14:06 586 +++ trunk/OOPSE/libmdtools/NPTi.cpp 2003/09/19 20:00:27 778 @@ -9,6 +9,9 @@ #include "Integrator.hpp" #include "simError.h" +#ifdef IS_MPI +#include "mpiSimulation.hpp" +#endif // Basic isotropic thermostating and barostating via the Melchionna // modification of the Hoover algorithm: @@ -20,233 +23,118 @@ 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; - have_tau_thermostat = 0; - have_tau_barostat = 0; - have_target_temp = 0; - have_target_pressure = 0; + oldEta = 0.0; } -void NPTi::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; +template NPTi::~NPTi() { + //nothing for now +} +template void NPTi::resetIntegrator() { + eta = 0.0; + T::resetIntegrator(); +} - tt2 = tauThermostat * tauThermostat; - tb2 = tauBarostat * tauBarostat; +template void NPTi::evolveEtaA() { + eta += dt2 * ( instaVol * (instaPress - targetPressure) / + (p_convert*NkBT*tb2)); + oldEta = eta; +} - instaTemp = tStats->getTemperature(); - instaPress = tStats->getPressure(); - instaVol = tStats->getVolume(); - - // first evolve chi a half step +template void NPTi::evolveEtaB() { - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / + prevEta = eta; + eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) / (p_convert*NkBT*tb2)); +} - for( i=0; igetMass())*eConvert - - vel[j]*(chi+eta)); +template void NPTi::getVelScaleA(double sc[3], double vel[3]) { + int i; - // position whole step + for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta ); +} - rj[0] = pos[atomIndex]; - rj[1] = pos[atomIndex+1]; - rj[2] = pos[atomIndex+2]; - - info->wrapVector(rj); +template void NPTi::getVelScaleB(double sc[3], int index ){ + int i; - 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]); - - if( atoms[i]->isDirectional() ){ + for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta ); +} - 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 - 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] ); - } - - } - // Scale the box after all the positions have been moved: +template void NPTi::getPosScale(double pos[3], double COM[3], + int index, double sc[3]){ + int j; - cerr << "eta = " << eta - << "; exp(dt*eta) = " << exp(eta*dt) << "\n"; + for(j=0; j<3; j++) + sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; - info->scaleBox(exp(dt*eta)); - + for(j=0; j<3; j++) + sc[j] *= eta; } -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; +template void NPTi::scaleSimBox( void ){ - instaTemp = tStats->getTemperature(); - instaPress = tStats->getPressure(); - instaVol = tStats->getVolume(); + double scaleFactor; - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / - (p_convert*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] ); - } - } -} + scaleFactor = exp(dt*eta); -int NPTi::readyCheck() { - - // First check to see if we have a target temperature. - // Not having one is fatal. - - if (!have_target_temp) { + if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) { sprintf( painCave.errMsg, - "NPTi error: You can't use the NPTi integrator\n" - " without a targetTemp!\n" + "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(); - return -1; - } + } else { + info->scaleBox(scaleFactor); + } - if (!have_target_pressure) { - sprintf( painCave.errMsg, - "NPTi error: You can't use the NPTi integrator\n" - " without a targetPressure!\n" - ); - painCave.isFatal = 1; - simError(); - return -1; - } - - // 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; - } +} - // 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 NPTi::etaConverged() { - // We need NkBT a lot, so just set it here: + return ( fabs(prevEta - eta) <= etaTolerance ); +} - NkBT = (double)info->ndf * kB * targetTemp; +template double NPTi::getConservedQuantity(void){ - return 1; + double conservedQuantity; + double Three_NkBT; + double Energy; + double thermostat_kinetic; + double thermostat_potential; + double barostat_kinetic; + double barostat_potential; + double tb2; + double eta2; + + Energy = tStats->getTotalE(); + + thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi / + (2.0 * eConvert); + + thermostat_potential = fkBT* integralOfChidt / eConvert; + + + barostat_kinetic = 3.0 * NkBT * tauBarostat * tauBarostat * eta * eta / + (2.0 * eConvert); + + barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / + eConvert; + + conservedQuantity = Energy + 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; }