--- trunk/OOPSE/libmdtools/NPTi.cpp 2003/07/09 02:15:29 578 +++ trunk/OOPSE/libmdtools/NPTi.cpp 2003/11/06 22:01:37 855 @@ -1,4 +1,4 @@ -#include +#include #include "Atom.hpp" #include "SRI.hpp" #include "AbstractClasses.hpp" @@ -7,240 +7,170 @@ #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,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 +} - tt2 = tauThermostat * tauThermostat; - tb2 = tauBarostat * tauBarostat; +template void NPTi::resetIntegrator() { + eta = 0.0; + T::resetIntegrator(); +} - 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) / (NkBT*tb2)); +template void NPTi::evolveEtaA() { + eta += dt2 * ( instaVol * (instaPress - targetPressure) / + (p_convert*NkBT*tb2)); + oldEta = eta; +} - for( i=0; igetMass())*eConvert - - vel[j]*(chi+eta)); +template void NPTi::evolveEtaB() { - // position whole step + prevEta = eta; + eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) / + (p_convert*NkBT*tb2)); +} - rj[0] = pos[atomIndex]; - rj[1] = pos[atomIndex+1]; - rj[2] = pos[atomIndex+2]; - - info->wrapVector(rj); +template void NPTi::getVelScaleA(double sc[3], double vel[3]) { + 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] = vel[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 +template void NPTi::getVelScaleB(double sc[3], int index ){ + int i; - 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: + for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta ); +} - info->scaleBox(exp(dt*eta)); +template void NPTi::getPosScale(double pos[3], double COM[3], + int index, double sc[3]){ + int j; + + for(j=0; j<3; j++) + sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; + + 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) / (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 Energy; + double thermostat_kinetic; + double thermostat_potential; + double barostat_kinetic; + double barostat_potential; + + Energy = tStats->getTotalE(); + + thermostat_kinetic = fkBT* tt2 * chi * chi / + (2.0 * eConvert); + + thermostat_potential = fkBT* integralOfChidt / eConvert; + + + barostat_kinetic = 3.0 * NkBT * tb2 * 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; } + +template string NPTi::getAdditionalParameters(void){ + string parameters; + const int BUFFERSIZE = 2000; // size of the read buffer + char buffer[BUFFERSIZE]; + + sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt); + parameters += buffer; + + sprintf(buffer,"\t%G\t0\t0;", eta); + parameters += buffer; + + sprintf(buffer,"\t0\t%G\t0;", eta); + parameters += buffer; + + sprintf(buffer,"\t0\t0\t%G;", eta); + parameters += buffer; + + return parameters; + +}