--- trunk/OOPSE/libmdtools/NPTf.cpp 2003/07/09 01:41:11 577 +++ trunk/OOPSE/libmdtools/NPTf.cpp 2003/09/19 16:01:07 772 @@ -1,3 +1,4 @@ +#include #include "Atom.hpp" #include "SRI.hpp" #include "AbstractClasses.hpp" @@ -8,8 +9,11 @@ #include "Integrator.hpp" #include "simError.h" +#ifdef IS_MPI +#include "mpiSimulation.hpp" +#endif -// Basic isotropic thermostating and barostating via the Melchionna +// Basic non-isotropic thermostating and barostating via the Melchionna // modification of the Hoover algorithm: // // Melchionna, S., Ciccotti, G., and Holian, B. L., 1993, @@ -19,230 +23,418 @@ // // 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; + integralOfChidt = 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; + + have_chi_tolerance = 0; + have_eta_tolerance = 0; + have_pos_iter_tolerance = 0; + + oldPos = new double[3*nAtoms]; + oldVel = new double[3*nAtoms]; + oldJi = new double[3*nAtoms]; +#ifdef IS_MPI + Nparticles = mpiSim->getTotAtoms(); +#else + Nparticles = theInfo->n_atoms; +#endif + } -void NPTf::moveA() { - - int i,j,k; - int atomIndex, aMatIndex; +template NPTf::~NPTf() { + delete[] oldPos; + delete[] oldVel; + delete[] oldJi; +} + +template void NPTf::moveA() { + + // new version of NPTf + 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 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; + double COM[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; + tStats->getCOM(COM); + + //calculate scale factor of veloity + for (i = 0; i < 3; i++ ) { + for (j = 0; j < 3; j++ ) { + vScale[i][j] = eta[i][j]; + + if (i == j) { + vScale[i][j] += chi; + } + } + } - 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); - + //evolve velocity half step for( i=0; igetMass())*eConvert - scx); - vy += dt2 * ((frc[atomIndex+1]/atoms[i]->getMass())*eConvert - scy); - vz += dt2 * ((frc[atomIndex+2]/atoms[i]->getMass())*eConvert - scz); - vel[atomIndex] = vx; - vel[atomIndex+1] = vy; - vel[atomIndex+2] = vz; + atoms[i]->getVel( vel ); + atoms[i]->getFrc( frc ); - // position whole step + mass = atoms[i]->getMass(); + + info->matVecMul3( vScale, vel, sc ); - rj[0] = pos[atomIndex]; - rj[1] = pos[atomIndex+1]; - rj[2] = pos[atomIndex+2]; + for (j=0; j < 3; j++) { + // velocity half step + vel[j] += dt2 * ((frc[j] / mass) * eConvert - sc[j]); + } - 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]; - - pos[atomIndex] += dt * (vel[atomIndex] + scx); - pos[atomIndex+1] += dt * (vel[atomIndex+1] + scy); - pos[atomIndex+2] += dt * (vel[atomIndex+2] + scz); + 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 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 ); + } + } + + // advance chi half step + chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; + + // calculate the integral of chidt + integralOfChidt += dt2*chi; + + // advance eta half step + + 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); + else + eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2); } + //save the old positions + for(i = 0; i < nAtoms; i++){ + atoms[i]->getPos(pos); + for(j = 0; j < 3; j++) + oldPos[i*3 + j] = pos[j]; } - - // Scale the box after all the positions have been moved: + //the first estimation of r(t+dt) is equal to r(t) + + for(k = 0; k < 4; k ++){ + for(i =0 ; i < nAtoms; i++){ - // Use a taylor expansion for eta products - - info->getBoxM(hm); - + atoms[i]->getVel(vel); + atoms[i]->getPos(pos); + for(j = 0; j < 3; j++) + rj[j] = (oldPos[i*3 + j] + pos[j])/2 - COM[j]; + + info->matVecMul3( eta, rj, sc ); + + for(j = 0; j < 3; j++) + pos[j] = oldPos[i*3 + j] + dt*(vel[j] + sc[j]); + atoms[i]->setPos( pos ); + } + if (nConstrained) { + constrainA(); + } + } - info->scaleBox(exp(dt*eta)); + + // 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++){ + + // 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++){ + 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]); + } + if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i]; + if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i]; + } + + 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); + } + } -void NPTi::moveB( void ){ - int i,j,k; - int atomIndex; +template void NPTf::moveB( void ){ + + //new version of NPTf + int i, j, k; DirectionalAtom* dAtom; - double Tb[3]; - double ji[3]; + double Tb[3], ji[3]; + double vel[3], myVel[3], frc[3]; + double mass; + double instaTemp, instaPress, instaVol; double tt2, tb2; + double sc[3]; + double press[3][3], vScale[3][3]; + double oldChi, prevChi; + double oldEta[3][3], prevEta[3][3], diffEta; tt2 = tauThermostat * tauThermostat; tb2 = tauBarostat * tauBarostat; - instaTemp = tStats->getTemperature(); - instaPress = tStats->getPressure(); - instaVol = tStats->getVolume(); + // Set things up for the iteration: - chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); + oldChi = chi; + for(i = 0; i < 3; i++) + for(j = 0; j < 3; j++) + oldEta[i][j] = eta[i][j]; + for( i=0; igetVel( vel ); + + for (j=0; j < 3; j++) + oldVel[3*i + j] = vel[j]; + + if( atoms[i]->isDirectional() ){ + + dAtom = (DirectionalAtom *)atoms[i]; + + dAtom->getJ( ji ); + + for (j=0; j < 3; j++) + oldJi[3*i + j] = ji[j]; + + } + } + + // do the iteration: + + instaVol = tStats->getVolume(); + + for (k=0; k < 4; k++) { - // velocity half step - for( j=atomIndex; j<(atomIndex+3); j++ ) - for( j=atomIndex; j<(atomIndex+3); j++ ) - vel[j] += dt2 * ((frc[j]/atoms[i]->getMass())*eConvert - - vel[j]*(chi+eta)); + instaTemp = tStats->getTemperature(); + tStats->getPressureTensor(press); + + // evolve chi another half step using the temperature at t + dt/2 + + prevChi = chi; + chi = oldChi + dt2 * ( instaTemp / targetTemp - 1.0) / tt2; - if( atoms[i]->isDirectional() ){ + for(i = 0; i < 3; i++) + for(j = 0; j < 3; j++) + prevEta[i][j] = eta[i][j]; + + //advance eta half step and calculate scale factor for velocity + + for(i = 0; i < 3; i ++) + for(j = 0; j < 3; j++){ + if( i == j) { + eta[i][j] = oldEta[i][j] + dt2 * instaVol * + (press[i][j] - targetPressure/p_convert) / (NkBT*tb2); + vScale[i][j] = eta[i][j] + chi; + } else { + eta[i][j] = oldEta[i][j] + dt2 * instaVol * press[i][j] / (NkBT*tb2); + vScale[i][j] = eta[i][j]; + } + } + + for( i=0; igetFrc( frc ); + atoms[i]->getVel(vel); - dAtom = (DirectionalAtom *)atoms[i]; + mass = atoms[i]->getMass(); + + for (j = 0; j < 3; j++) + myVel[j] = oldVel[3*i + j]; - // get and convert the torque to body frame + info->matVecMul3( vScale, myVel, sc ); - Tb[0] = dAtom->getTx(); - Tb[1] = dAtom->getTy(); - Tb[2] = dAtom->getTz(); + // velocity half step + for (j=0; j < 3; j++) { + // velocity half step (use chi from previous step here): + vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass) * eConvert - sc[j]); + } - dAtom->lab2Body( Tb ); + atoms[i]->setVel( vel ); - // get the angular momentum, and complete the angular momentum - // half step + if( atoms[i]->isDirectional() ){ + + dAtom = (DirectionalAtom *)atoms[i]; + + // get and convert the torque to body frame + + dAtom->getTrq( Tb ); + dAtom->lab2Body( Tb ); + + for (j=0; j < 3; j++) + ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi); - 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] ); + dAtom->setJ( ji ); + } } + + if (nConstrained) { + constrainB(); + } + + diffEta = 0; + for(i = 0; i < 3; i++) + diffEta += pow(prevEta[i][i] - eta[i][i], 2); + + if (fabs(prevChi - chi) <= chiTolerance && sqrt(diffEta / 3) <= etaTolerance) + break; } + + //calculate integral of chidt + integralOfChidt += dt2*chi; + } -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; @@ -252,7 +444,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; @@ -264,7 +456,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(); @@ -275,16 +467,66 @@ 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(); return -1; } - // We need NkBT a lot, so just set it here: + + // We need NkBT a lot, so just set it here: This is the RAW number + // of particles, so no subtraction or addition of constraints or + // orientational degrees of freedom: + + NkBT = (double)Nparticles * kB * targetTemp; + + // fkBT is used because the thermostat operates on more degrees of freedom + // than the barostat (when there are particles with orientational degrees + // of freedom). ndf = 3 * (n_atoms + n_oriented -1) - n_constraint - nZcons + + fkBT = (double)info->ndf * kB * targetTemp; - NkBT = (double)info->ndf * kB * targetTemp; - return 1; } + +template double NPTf::getConservedQuantity(void){ + + double conservedQuantity; + double Energy; + double thermostat_kinetic; + double thermostat_potential; + double barostat_kinetic; + double barostat_potential; + double trEta; + double a[3][3], b[3][3]; + + Energy = tStats->getTotalE(); + + thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi / + (2.0 * eConvert); + + thermostat_potential = fkBT* integralOfChidt / eConvert; + + info->transposeMat3(eta, a); + info->matMul3(a, eta, b); + trEta = info->matTrace3(b); + + barostat_kinetic = NkBT * tauBarostat * tauBarostat * trEta / + (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; +}