--- trunk/OOPSE/libmdtools/NPTi.cpp 2003/07/15 19:56:08 617 +++ trunk/OOPSE/libmdtools/NPTi.cpp 2003/11/07 17:09:48 857 @@ -1,4 +1,4 @@ -#include +#include #include "Atom.hpp" #include "SRI.hpp" #include "AbstractClasses.hpp" @@ -7,127 +7,101 @@ #include "Thermo.hpp" #include "ReadWrite.hpp" #include "Integrator.hpp" -#include "simError.h" +#include "simError.h" +#ifdef IS_MPI +#include "mpiSimulation.hpp" +#endif // Basic isotropic thermostating and barostating via the Melchionna // modification of the Hoover algorithm: // // Melchionna, S., Ciccotti, G., and Holian, B. L., 1993, -// Molec. Phys., 78, 533. +// Molec. Phys., 78, 533. // // and -// +// // 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; + GenericData* data; + DoubleArrayData * etaValue; + vector etaArray; + eta = 0.0; - have_tau_thermostat = 0; - have_tau_barostat = 0; - have_target_temp = 0; - have_target_pressure = 0; + oldEta = 0.0; + + if( theInfo->useInitXSstate ){ + // retrieve eta from simInfo if + data = info->getProperty(ETAVALUE_ID); + if(data){ + etaValue = dynamic_cast(data); + + if(etaValue){ + etaArray = etaValue->getData(); + eta = etaArray[0]; + oldEta = eta; + } + } + } } -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; - - instaTemp = tStats->getTemperature(); - instaPress = tStats->getPressure(); - instaVol = tStats->getVolume(); - - // first evolve chi a half step - - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / +template void NPTi::evolveEtaA() { + eta += dt2 * ( instaVol * (instaPress - targetPressure) / (p_convert*NkBT*tb2)); + oldEta = eta; +} - for( i=0; igetVel( vel ); - atoms[i]->getPos( pos ); - atoms[i]->getFrc( frc ); +template void NPTi::evolveEtaB() { - mass = atoms[i]->getMass(); + prevEta = eta; + eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) / + (p_convert*NkBT*tb2)); +} - for (j=0; j < 3; j++) { - vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*(chi+eta)); - rj[j] = pos[j]; - } +template void NPTi::calcVelScale(void) { + vScale = chi + eta; +} - atoms[i]->setVel( vel ); +template void NPTi::getVelScaleA(double sc[3], double vel[3]) { + int i; - info->wrapVector(rj); + for(i=0; i<3; i++) sc[i] = vel[i] * vScale; +} - for (j = 0; j < 3; j++) - pos[j] += dt * (vel[j] + eta*rj[j]); +template void NPTi::getVelScaleB(double sc[3], int index ){ + int i; - atoms[i]->setPos( pos ); + for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * vScale; +} - 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 +template void NPTi::getPosScale(double pos[3], double COM[3], + int index, double sc[3]){ + int j; - dAtom->getJ( ji ); + for(j=0; j<3; j++) + sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; - 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 + for(j=0; j<3; j++) + sc[j] *= eta; +} - 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 ); +template void NPTi::scaleSimBox( void ){ - // 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 ); - } + double scaleFactor; - } - - // Scale the box after all the positions have been moved: - scaleFactor = exp(dt*eta); if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) { @@ -138,118 +112,69 @@ void NPTi::moveA() { ); painCave.isFatal = 1; simError(); - } else { - info->scaleBox(exp(dt*eta)); + } else { + info->scaleBox(scaleFactor); } } -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(); + double conservedQuantity; + double Energy; + double thermostat_kinetic; + double thermostat_potential; + double barostat_kinetic; + double barostat_potential; - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / - (p_convert*NkBT*tb2)); - - 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() ){ + barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta / + (2.0 * eConvert); - dAtom = (DirectionalAtom *)atoms[i]; + barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / + eConvert; - // get and convert the torque to body frame + conservedQuantity = Energy + thermostat_kinetic + thermostat_potential + + barostat_kinetic + barostat_potential; - dAtom->getTrq( Tb ); - dAtom->lab2Body( Tb ); +// cout.width(8); +// cout.precision(8); - // 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 ); - } - } +// cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << +// "\t" << thermostat_potential << "\t" << barostat_kinetic << +// "\t" << barostat_potential << "\t" << conservedQuantity << endl; + return conservedQuantity; } -int NPTi::readyCheck() { - - // 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" - " without a targetTemp!\n" - ); - painCave.isFatal = 1; - simError(); - return -1; - } +template string NPTi::getAdditionalParameters(void){ + string parameters; + const int BUFFERSIZE = 2000; // size of the read buffer + char buffer[BUFFERSIZE]; - 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; - } + sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt); + parameters += buffer; - // 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; - } + sprintf(buffer,"\t%G\t0\t0;", eta); + parameters += buffer; - // We need NkBT a lot, so just set it here: + sprintf(buffer,"\t0\t%G\t0;", eta); + parameters += buffer; - NkBT = (double)info->ndf * kB * targetTemp; + sprintf(buffer,"\t0\t0\t%G;", eta); + parameters += buffer; - return 1; + return parameters; + }