--- branches/mmeineke/OOPSE/libmdtools/Thermo.cpp 2003/03/21 17:42:12 377 +++ trunk/OOPSE/libmdtools/Thermo.cpp 2003/04/10 16:22:00 486 @@ -4,13 +4,19 @@ using namespace std; #ifdef IS_MPI #include -#include #endif //is_mpi #include "Thermo.hpp" #include "SRI.hpp" #include "Integrator.hpp" +#include "simError.h" +#ifdef IS_MPI +#define __C +#include "mpiSimulation.hpp" +#endif // is_mpi + + #define BASE_SEED 123456789 Thermo::Thermo( SimInfo* the_entry_plug ) { @@ -66,7 +72,8 @@ double Thermo::getKinetic(){ } } #ifdef IS_MPI - MPI::COMM_WORLD.Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE,MPI_SUM); + MPI_Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE, + MPI_SUM, MPI_COMM_WORLD); kinetic = kinetic_global; #endif //is_mpi @@ -77,30 +84,36 @@ double Thermo::getPotential(){ double Thermo::getPotential(){ + double potential_local; double potential; - double potential_global; int el, nSRI; - SRI** sris; + Molecule* molecules; - sris = entry_plug->sr_interactions; + molecules = entry_plug->molecules; nSRI = entry_plug->n_SRI; + potential_local = 0.0; potential = 0.0; - potential_global = 0.0; - potential += entry_plug->lrPot; + potential_local += entry_plug->lrPot; - for( el=0; elget_potential(); + for( el=0; eln_mol; el++ ){ + potential_local += molecules[el].getPotential(); } // Get total potential for entire system from MPI. #ifdef IS_MPI - MPI::COMM_WORLD.Allreduce(&potential,&potential_global,1,MPI_DOUBLE,MPI_SUM); - potential = potential_global; - + MPI_Allreduce(&potential_local,&potential,1,MPI_DOUBLE, + MPI_SUM, MPI_COMM_WORLD); +#else + potential = potential_local; #endif // is_mpi +#ifdef IS_MPI + /* + std::cerr << "node " << worldRank << ": after pot = " << potential << "\n"; + */ +#endif + return potential; } @@ -117,20 +130,105 @@ double Thermo::getTemperature(){ const double kb = 1.9872179E-3; // boltzman's constant in kcal/(mol K) double temperature; - int ndf = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented - - entry_plug->n_constraints - 3; - - temperature = ( 2.0 * this->getKinetic() ) / ( ndf * kb ); + temperature = ( 2.0 * this->getKinetic() ) / ((double)entry_plug->ndf * kb ); return temperature; } -double Thermo::getPressure(){ +double Thermo::getEnthalpy() { -// const double conv_Pa_atm = 9.901E-6; // convert Pa -> atm -// const double conv_internal_Pa = 1.661E-7; //convert amu/(fs^2 A) -> Pa -// const double conv_A_m = 1.0E-10; //convert A -> m + const double e_convert = 4.184E-4; // convert kcal/mol -> (amu A^2)/fs^2 + double u, p, v; + double press[9]; - return 0.0; + u = this->getTotalE(); + + this->getPressureTensor(press); + p = (press[0] + press[4] + press[8]) / 3.0; + + v = this->getVolume(); + + return (u + (p*v)/e_convert); +} + +double Thermo::getVolume() { + double theBox[3]; + + entry_plug->getBox(theBox); + return (theBox[0] * theBox[1] * theBox[2]); +} + +double Thermo::getPressure() { + // returns the pressure in units of atm + // Relies on the calculation of the full molecular pressure tensor + + const double p_convert = 1.63882576e8; + double press[9]; + double pressure; + + this->getPressureTensor(press); + + pressure = p_convert * (press[0] + press[4] + press[8]) / 3.0; + + return pressure; +} + + +void Thermo::getPressureTensor(double press[9]){ + // returns pressure tensor in units amu*fs^-2*Ang^-1 + // routine derived via viral theorem description in: + // Paci, E. and Marchi, M. J.Phys.Chem. 1996, 100, 4314-4322 + + const double e_convert = 4.184e-4; + + double molmass, volume; + double vcom[3]; + double p_local[9], p_global[9]; + double theBox[3]; + //double* tau; + int i, nMols; + Molecule* molecules; + + nMols = entry_plug->n_mol; + molecules = entry_plug->molecules; + //tau = entry_plug->tau; + + // use velocities of molecular centers of mass and molecular masses: + for (i=0; i < 9; i++) { + p_local[i] = 0.0; + p_global[i] = 0.0; + } + + for (i=0; i < nMols; i++) { + molmass = molecules[i].getCOMvel(vcom); + + p_local[0] += molmass * (vcom[0] * vcom[0]); + p_local[1] += molmass * (vcom[0] * vcom[1]); + p_local[2] += molmass * (vcom[0] * vcom[2]); + p_local[3] += molmass * (vcom[1] * vcom[0]); + p_local[4] += molmass * (vcom[1] * vcom[1]); + p_local[5] += molmass * (vcom[1] * vcom[2]); + p_local[6] += molmass * (vcom[2] * vcom[0]); + p_local[7] += molmass * (vcom[2] * vcom[1]); + p_local[8] += molmass * (vcom[2] * vcom[2]); + } + + // Get total for entire system from MPI. + +#ifdef IS_MPI + MPI_Allreduce(p_local,p_global,9,MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); +#else + for (i=0; i<9; i++) { + p_global[i] = p_local[i]; + } +#endif // is_mpi + + entry_plug->getBox(theBox); + + volume = theBox[0] * theBox[1] * theBox[2]; + + for(i=0; i<9; i++) { + press[i] = (p_global[i] - entry_plug->tau[i]*e_convert) / volume; + } } void Thermo::velocitize() { @@ -140,13 +238,10 @@ void Thermo::velocitize() { double jx, jy, jz; int i, vr, vd; // velocity randomizer loop counters double vdrift[3]; - double mtot = 0.0; double vbar; const double kb = 8.31451e-7; // kb in amu, angstroms, fs, etc. double av2; double kebar; - int ndf; // number of degrees of freedom - int ndfRaw; // the raw number of degrees of freedom int n_atoms; Atom** atoms; DirectionalAtom* dAtom; @@ -160,10 +255,8 @@ void Thermo::velocitize() { n_oriented = entry_plug->n_oriented; n_constraints = entry_plug->n_constraints; - - ndfRaw = 3 * n_atoms + 3 * n_oriented; - ndf = ndfRaw - n_constraints - 3; - kebar = kb * temperature * (double)ndf / ( 2.0 * (double)ndfRaw ); + kebar = kb * temperature * (double)entry_plug->ndf / + ( 2.0 * (double)entry_plug->ndfRaw ); for(vr = 0; vr < n_atoms; vr++){ @@ -171,7 +264,7 @@ void Thermo::velocitize() { av2 = 2.0 * kebar / atoms[vr]->getMass(); vbar = sqrt( av2 ); - + // vbar = sqrt( 8.31451e-7 * temperature / atoms[vr]->getMass() ); // picks random velocities from a gaussian distribution @@ -185,35 +278,20 @@ void Thermo::velocitize() { atoms[vr]->set_vy( vy ); atoms[vr]->set_vz( vz ); } + + // Get the Center of Mass drift velocity. + + getCOMVel(vdrift); // Corrects for the center of mass drift. // sums all the momentum and divides by total mass. - - mtot = 0.0; - vdrift[0] = 0.0; - vdrift[1] = 0.0; - vdrift[2] = 0.0; - for(vd = 0; vd < n_atoms; vd++){ - - vdrift[0] += atoms[vd]->get_vx() * atoms[vd]->getMass(); - vdrift[1] += atoms[vd]->get_vy() * atoms[vd]->getMass(); - vdrift[2] += atoms[vd]->get_vz() * atoms[vd]->getMass(); - - mtot += atoms[vd]->getMass(); - } - - for (vd = 0; vd < 3; vd++) { - vdrift[vd] = vdrift[vd] / mtot; - } - for(vd = 0; vd < n_atoms; vd++){ vx = atoms[vd]->get_vx(); vy = atoms[vd]->get_vy(); vz = atoms[vd]->get_vz(); - - + vx -= vdrift[0]; vy -= vdrift[1]; vz -= vdrift[2]; @@ -235,7 +313,7 @@ void Thermo::velocitize() { vbar = sqrt( 2.0 * kebar * dAtom->getIyy() ); jy = vbar * gaussStream->getGaussian(); - + vbar = sqrt( 2.0 * kebar * dAtom->getIzz() ); jz = vbar * gaussStream->getGaussian(); @@ -246,3 +324,47 @@ void Thermo::velocitize() { } } } + +void Thermo::getCOMVel(double vdrift[3]){ + + double mtot, mtot_local; + double vdrift_local[3]; + int vd, n_atoms; + Atom** atoms; + + // We are very careless here with the distinction between n_atoms and n_local + // We should really fix this before someone pokes an eye out. + + n_atoms = entry_plug->n_atoms; + atoms = entry_plug->atoms; + + mtot_local = 0.0; + vdrift_local[0] = 0.0; + vdrift_local[1] = 0.0; + vdrift_local[2] = 0.0; + + for(vd = 0; vd < n_atoms; vd++){ + + vdrift_local[0] += atoms[vd]->get_vx() * atoms[vd]->getMass(); + vdrift_local[1] += atoms[vd]->get_vy() * atoms[vd]->getMass(); + vdrift_local[2] += atoms[vd]->get_vz() * atoms[vd]->getMass(); + + mtot_local += atoms[vd]->getMass(); + } + +#ifdef IS_MPI + MPI_Allreduce(&mtot_local,&mtot,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(vdrift_local,vdrift,3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); +#else + mtot = mtot_local; + for(vd = 0; vd < 3; vd++) { + vdrift[vd] = vdrift_local[vd]; + } +#endif + + for (vd = 0; vd < 3; vd++) { + vdrift[vd] = vdrift[vd] / mtot; + } + +} +