--- trunk/OOPSE/libmdtools/NPTf.cpp 2003/09/19 20:00:27 778 +++ trunk/OOPSE/libmdtools/NPTf.cpp 2003/09/22 21:23:25 780 @@ -26,69 +26,77 @@ template NPTf::NPTf ( SimInfo *theInfo, template NPTf::NPTf ( SimInfo *theInfo, ForceFields* the_ff): T( theInfo, the_ff ) { - int i, j; - chi = 0.0; - integralOfChidt = 0.0; - - for(i = 0; i < 3; i++) - for (j = 0; j < 3; j++) + + int i,j; + + for(i = 0; i < 3; i++){ + for (j = 0; j < 3; j++){ + eta[i][j] = 0.0; + oldEta[i][j] = 0.0; + } + } +} - have_tau_thermostat = 0; - have_tau_barostat = 0; - have_target_temp = 0; - have_target_pressure = 0; +template NPTf::~NPTf() { - have_chi_tolerance = 0; - have_eta_tolerance = 0; - have_pos_iter_tolerance = 0; + // empty for now +} - 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 - +template void NPTf::resetIntegrator() { + + int i, j; + + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + eta[i][j] = 0.0; + + T::resetIntegrator(); } -template NPTf::~NPTf() { - delete[] oldPos; - delete[] oldVel; - delete[] oldJi; +template void NPTf::evolveEtaA() { + + int i, j; + + 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); + } + } + + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + oldEta[i][j] = eta[i][j]; } -template void NPTf::moveA() { +template void NPTf::evolveEtaB() { + + int i,j; - // new version of NPTf - int i, j, k; - DirectionalAtom* dAtom; - double Tb[3], ji[3]; + for(i = 0; i < 3; i++) + for (j = 0; j < 3; j++) + prevEta[i][j] = eta[i][j]; - double mass; - double vel[3], pos[3], frc[3]; + 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); + } else { + eta[i][j] = oldEta[i][j] + dt2 * instaVol * press[i][j] / (NkBT*tb2); + } + } + } +} - double rj[3]; - double instaTemp, instaPress, instaVol; - double tt2, tb2; - 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]; +template void NPTf::getVelScaleA(double sc[3], double vel[3]) { + int i,j; + double vScale[3][3]; - tt2 = tauThermostat * tauThermostat; - tb2 = tauBarostat * tauBarostat; - - instaTemp = tStats->getTemperature(); - tStats->getPressureTensor(press); - instaVol = tStats->getVolume(); - - 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]; @@ -99,96 +107,51 @@ template void NPTf::moveA() { } } - //evolve velocity half step - for( i=0; imatVecMul3( vScale, vel, sc ); +} - atoms[i]->getVel( vel ); - atoms[i]->getFrc( frc ); +template void NPTf::getVelScaleB(double sc[3], int index ){ + int i,j; + double myVel[3]; + double vScale[3][3]; - mass = atoms[i]->getMass(); - - info->matVecMul3( vScale, vel, sc ); - - for (j=0; j < 3; j++) { - // velocity half step - vel[j] += dt2 * ((frc[j] / mass) * eConvert - sc[j]); - } - - atoms[i]->setVel( vel ); - - if( atoms[i]->isDirectional() ){ - - dAtom = (DirectionalAtom *)atoms[i]; - - // get and convert the torque to body frame + for (i = 0; i < 3; i++ ) { + for (j = 0; j < 3; j++ ) { + vScale[i][j] = eta[i][j]; - 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); - - this->rotationPropagation( dAtom, ji ); - - dAtom->setJ( ji ); - } - } - - // 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); + if (i == j) { + vScale[i][j] += chi; + } } - - //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]; } - //the first estimation of r(t+dt) is equal to r(t) - - for(k = 0; k < 4; k ++){ + for (j = 0; j < 3; j++) + myVel[j] = oldVel[3*index + j]; - for(i =0 ; i < nAtoms; i++){ + info->matVecMul3( vScale, myVel, sc ); +} - atoms[i]->getVel(vel); - atoms[i]->getPos(pos); +template void NPTf::getPosScale(double pos[3], double COM[3], + int index, double sc[3]){ + int j; + double rj[3]; - 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]); + for(j=0; j<3; j++) + rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; - atoms[i]->setPos( pos ); + info->matVecMul3( eta, rj, sc ); +} - } +template void NPTf::scaleSimBox( void ){ - if (nConstrained) { - constrainA(); - } - } + int i,j,k; + double scaleMat[3][3]; + double eta2ij; + double bigScale, smallScale, offDiagMax; + double hm[3][3], hmnew[3][3]; + - + // Scale the box after all the positions have been moved: // Use a taylor expansion for eta products: Hmat = Hmat . exp(dt * etaMat) @@ -253,220 +216,23 @@ template void NPTf::moveA() { info->matMul3(hm, scaleMat, hmnew); info->setBoxM(hmnew); } - } -template void NPTf::moveB( void ){ - - //new version of NPTf - int i, j, k; - DirectionalAtom* dAtom; - double Tb[3], ji[3]; - double vel[3], myVel[3], frc[3]; - double mass; +template bool NPTf::etaConverged() { + int i; + double diffEta, sumEta; - 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; - - // Set things up for the iteration: - - oldChi = chi; - + sumEta = 0; 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(); + sumEta += pow(prevEta[i][i] - eta[i][i], 2); - for (k=0; k < 4; k++) { - - 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; - - 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); - - mass = atoms[i]->getMass(); - - for (j = 0; j < 3; j++) - myVel[j] = oldVel[3*i + j]; - - info->matVecMul3( vScale, myVel, sc ); - - // 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]); - } - - atoms[i]->setVel( vel ); - - if( atoms[i]->isDirectional() ){ - - dAtom = (DirectionalAtom *)atoms[i]; + diffEta = sqrt( sumEta / 3.0 ); - // 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); - - 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; - + return ( diffEta <= etaTolerance ); } -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, - "NPTf error: You can't use the NPTf integrator\n" - " without a targetTemp!\n" - ); - painCave.isFatal = 1; - simError(); - return -1; - } - - if (!have_target_pressure) { - sprintf( painCave.errMsg, - "NPTf error: You can't use the NPTf integrator\n" - " without a targetPressure!\n" - ); - painCave.isFatal = 1; - simError(); - return -1; - } - - // We must set tauThermostat. - - if (!have_tau_thermostat) { - sprintf( painCave.errMsg, - "NPTf error: If you use the NPTf\n" - " integrator, you must set tauThermostat.\n"); - painCave.isFatal = 1; - simError(); - return -1; - } - - // We must set tauBarostat. - - if (!have_tau_barostat) { - sprintf( painCave.errMsg, - "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: 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; - - return 1; -} - template double NPTf::getConservedQuantity(void){ - + double conservedQuantity; double Energy; double thermostat_kinetic; @@ -478,7 +244,7 @@ template double NPTf::getConservedQuant Energy = tStats->getTotalE(); - thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi / + thermostat_kinetic = fkBT* tt2 * chi * chi / (2.0 * eConvert); thermostat_potential = fkBT* integralOfChidt / eConvert; @@ -487,7 +253,7 @@ template double NPTf::getConservedQuant info->matMul3(a, eta, b); trEta = info->matTrace3(b); - barostat_kinetic = NkBT * tauBarostat * tauBarostat * trEta / + barostat_kinetic = NkBT * tb2 * trEta / (2.0 * eConvert); barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / @@ -496,12 +262,13 @@ template double NPTf::getConservedQuant conservedQuantity = Energy + thermostat_kinetic + thermostat_potential + barostat_kinetic + barostat_potential; - cout.width(8); - cout.precision(8); +// 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; +// cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << +// "\t" << thermostat_potential << "\t" << barostat_kinetic << +// "\t" << barostat_potential << "\t" << conservedQuantity << endl; return conservedQuantity; + }