--- trunk/OOPSE/libmdtools/NPTi.cpp 2003/07/31 15:35:07 658 +++ trunk/OOPSE/libmdtools/NPTi.cpp 2003/09/25 19:27:15 787 @@ -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: @@ -23,111 +26,60 @@ template NPTi::NPTi ( SimInfo *theInfo, 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; } -template void NPTi::moveA() { - - int i, j; - DirectionalAtom* dAtom; - double Tb[3], ji[3]; - double A[3][3], I[3][3]; - double angle, mass; - double vel[3], pos[3], frc[3]; +template NPTi::~NPTi() { + //nothing for now +} - double rj[3]; - double instaTemp, instaPress, instaVol; - double tt2, tb2, scaleFactor; +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; igetVel( vel ); - atoms[i]->getPos( pos ); - atoms[i]->getFrc( frc ); +template void NPTi::getVelScaleA(double sc[3], double vel[3]) { + int i; - mass = atoms[i]->getMass(); + for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta ); +} - for (j=0; j < 3; j++) { - vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta)); - rj[j] = pos[j]; - } +template void NPTi::getVelScaleB(double sc[3], int index ){ + int i; - atoms[i]->setVel( vel ); + for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta ); +} - info->wrapVector(rj); - for (j = 0; j < 3; j++) - pos[j] += dt * (vel[j] + eta*rj[j]); +template void NPTi::getPosScale(double pos[3], double COM[3], + int index, double sc[3]){ + int j; - atoms[i]->setPos( pos ); + for(j=0; j<3; j++) + sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; - if( atoms[i]->isDirectional() ){ + for(j=0; j<3; j++) + sc[j] *= eta; +} - dAtom = (DirectionalAtom *)atoms[i]; - - // get and convert the torque to body frame - - dAtom->getTrq( Tb ); - dAtom->lab2Body( Tb ); - - // get the angular momentum, and propagate a half step +template void NPTi::scaleSimBox( void ){ - dAtom->getJ( ji ); + double scaleFactor; - for (j=0; j < 3; j++) - ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*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] / I[0][0]; - this->rotate( 1, 2, angle, ji, A ); - - // rotate about the y-axis - angle = dt2 * ji[1] / I[1][1]; - this->rotate( 2, 0, angle, ji, A ); - - // rotate about the z-axis - angle = dt * ji[2] / I[2][2]; - this->rotate( 0, 1, angle, ji, A); - - // rotate about the y-axis - angle = dt2 * ji[1] / I[1][1]; - this->rotate( 2, 0, angle, ji, A ); - - // rotate about the x-axis - angle = dt2 * ji[0] / I[0][0]; - this->rotate( 1, 2, angle, ji, A ); - - dAtom->setJ( ji ); - dAtom->setA( A ); - } - - } - - // Scale the box after all the positions have been moved: - scaleFactor = exp(dt*eta); if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) { @@ -139,121 +91,47 @@ template void NPTi::moveA() { painCave.isFatal = 1; simError(); } else { - info->scaleBox(exp(dt*eta)); - } + info->scaleBox(scaleFactor); + } } -template void NPTi::moveB( void ){ +template bool NPTi::etaConverged() { - int i, j; - DirectionalAtom* dAtom; - double Tb[3], ji[3]; - double vel[3], frc[3]; - double mass; + return ( fabs(prevEta - eta) <= etaTolerance ); +} - double instaTemp, instaPress, instaVol; - double tt2, tb2; - - tt2 = tauThermostat * tauThermostat; - tb2 = tauBarostat * tauBarostat; +template double NPTi::getConservedQuantity(void){ - instaTemp = tStats->getTemperature(); - instaPress = tStats->getPressure(); - instaVol = tStats->getVolume(); - - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / - (p_convert*NkBT*tb2)); + double conservedQuantity; + double Energy; + double thermostat_kinetic; + double thermostat_potential; + double barostat_kinetic; + double barostat_potential; - for( i=0; igetTotalE(); - atoms[i]->getVel( vel ); - atoms[i]->getFrc( frc ); + thermostat_kinetic = fkBT* tt2 * chi * chi / + (2.0 * eConvert); - mass = atoms[i]->getMass(); + thermostat_potential = fkBT* integralOfChidt / eConvert; - // velocity half step - 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 - - dAtom->getTrq( Tb ); - dAtom->lab2Body( Tb ); - - // 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 ); - } - } -} - -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. + barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta / + (2.0 * eConvert); - 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; - } + barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / + eConvert; - 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; - } + conservedQuantity = Energy + thermostat_kinetic + thermostat_potential + + barostat_kinetic + barostat_potential; - // 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; - } +// cout.width(8); +// cout.precision(8); - // 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; - } - - // We need NkBT a lot, so just set it here: - - NkBT = (double)info->ndf * kB * targetTemp; - - return 1; +// cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << +// "\t" << thermostat_potential << "\t" << barostat_kinetic << +// "\t" << barostat_potential << "\t" << conservedQuantity << endl; + return conservedQuantity; }