| 1 | #include <cmath> | 
| 2 | #include <iostream> | 
| 3 | using namespace std; | 
| 4 |  | 
| 5 | #ifdef IS_MPI | 
| 6 | #include <mpi.h> | 
| 7 | #include <mpi++.h> | 
| 8 | #endif //is_mpi | 
| 9 |  | 
| 10 | #include "Thermo.hpp" | 
| 11 | #include "SRI.hpp" | 
| 12 | #include "Integrator.hpp" | 
| 13 | #include "simError.h" | 
| 14 |  | 
| 15 | #ifdef IS_MPI | 
| 16 | #define __C | 
| 17 | #include "mpiSimulation.hpp" | 
| 18 | #endif // is_mpi | 
| 19 |  | 
| 20 |  | 
| 21 | #define BASE_SEED 123456789 | 
| 22 |  | 
| 23 | Thermo::Thermo( SimInfo* the_entry_plug ) { | 
| 24 | entry_plug = the_entry_plug; | 
| 25 | int baseSeed = BASE_SEED; | 
| 26 |  | 
| 27 | gaussStream = new gaussianSPRNG( baseSeed ); | 
| 28 | } | 
| 29 |  | 
| 30 | Thermo::~Thermo(){ | 
| 31 | delete gaussStream; | 
| 32 | } | 
| 33 |  | 
| 34 | double Thermo::getKinetic(){ | 
| 35 |  | 
| 36 | const double e_convert = 4.184E-4; // convert kcal/mol -> (amu A^2)/fs^2 | 
| 37 | double vx2, vy2, vz2; | 
| 38 | double kinetic, v_sqr; | 
| 39 | int kl; | 
| 40 | double jx2, jy2, jz2; // the square of the angular momentums | 
| 41 |  | 
| 42 | DirectionalAtom *dAtom; | 
| 43 |  | 
| 44 | int n_atoms; | 
| 45 | double kinetic_global; | 
| 46 | Atom** atoms; | 
| 47 |  | 
| 48 |  | 
| 49 | n_atoms = entry_plug->n_atoms; | 
| 50 | atoms = entry_plug->atoms; | 
| 51 |  | 
| 52 | kinetic = 0.0; | 
| 53 | kinetic_global = 0.0; | 
| 54 | for( kl=0; kl < n_atoms; kl++ ){ | 
| 55 |  | 
| 56 | vx2 = atoms[kl]->get_vx() * atoms[kl]->get_vx(); | 
| 57 | vy2 = atoms[kl]->get_vy() * atoms[kl]->get_vy(); | 
| 58 | vz2 = atoms[kl]->get_vz() * atoms[kl]->get_vz(); | 
| 59 |  | 
| 60 | v_sqr = vx2 + vy2 + vz2; | 
| 61 | kinetic += atoms[kl]->getMass() * v_sqr; | 
| 62 |  | 
| 63 | if( atoms[kl]->isDirectional() ){ | 
| 64 |  | 
| 65 | dAtom = (DirectionalAtom *)atoms[kl]; | 
| 66 |  | 
| 67 | jx2 = dAtom->getJx() * dAtom->getJx(); | 
| 68 | jy2 = dAtom->getJy() * dAtom->getJy(); | 
| 69 | jz2 = dAtom->getJz() * dAtom->getJz(); | 
| 70 |  | 
| 71 | kinetic += (jx2 / dAtom->getIxx()) + (jy2 / dAtom->getIyy()) | 
| 72 | + (jz2 / dAtom->getIzz()); | 
| 73 | } | 
| 74 | } | 
| 75 | #ifdef IS_MPI | 
| 76 | MPI::COMM_WORLD.Allreduce(&kinetic,&kinetic_global,1,MPI_DOUBLE,MPI_SUM); | 
| 77 | kinetic = kinetic_global; | 
| 78 | #endif //is_mpi | 
| 79 |  | 
| 80 | kinetic = kinetic * 0.5 / e_convert; | 
| 81 |  | 
| 82 | return kinetic; | 
| 83 | } | 
| 84 |  | 
| 85 | double Thermo::getPotential(){ | 
| 86 |  | 
| 87 | double potential_local; | 
| 88 | double potential; | 
| 89 | int el, nSRI; | 
| 90 | Molecule* molecules; | 
| 91 |  | 
| 92 | molecules = entry_plug->molecules; | 
| 93 | nSRI = entry_plug->n_SRI; | 
| 94 |  | 
| 95 | potential_local = 0.0; | 
| 96 | potential = 0.0; | 
| 97 | potential_local += entry_plug->lrPot; | 
| 98 |  | 
| 99 | for( el=0; el<entry_plug->n_mol; el++ ){ | 
| 100 | potential_local += molecules[el].getPotential(); | 
| 101 | } | 
| 102 |  | 
| 103 | #ifdef IS_MPI | 
| 104 | /* | 
| 105 | std::cerr << "node " << worldRank << ": before LONG RANGE pot = " << entry_plug->lrPot | 
| 106 | << "; pot_local = " << potential_local | 
| 107 | << "; pot = " << potential << "\n"; | 
| 108 | */ | 
| 109 | #endif | 
| 110 |  | 
| 111 | // Get total potential for entire system from MPI. | 
| 112 | #ifdef IS_MPI | 
| 113 | MPI::COMM_WORLD.Allreduce(&potential_local,&potential,1,MPI_DOUBLE,MPI_SUM); | 
| 114 | #else | 
| 115 | potential = potential_local; | 
| 116 | #endif // is_mpi | 
| 117 |  | 
| 118 | #ifdef IS_MPI | 
| 119 | /* | 
| 120 | std::cerr << "node " << worldRank << ": after pot = " << potential << "\n"; | 
| 121 | */ | 
| 122 | #endif | 
| 123 |  | 
| 124 | return potential; | 
| 125 | } | 
| 126 |  | 
| 127 | double Thermo::getTotalE(){ | 
| 128 |  | 
| 129 | double total; | 
| 130 |  | 
| 131 | total = this->getKinetic() + this->getPotential(); | 
| 132 | return total; | 
| 133 | } | 
| 134 |  | 
| 135 | double Thermo::getTemperature(){ | 
| 136 |  | 
| 137 | const double kb = 1.9872179E-3; // boltzman's constant in kcal/(mol K) | 
| 138 | double temperature; | 
| 139 | int ndf_local, ndf; | 
| 140 |  | 
| 141 | ndf_local = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented | 
| 142 | - entry_plug->n_constraints; | 
| 143 |  | 
| 144 | #ifdef IS_MPI | 
| 145 | MPI::COMM_WORLD.Allreduce(&ndf_local,&ndf,1,MPI_INT,MPI_SUM); | 
| 146 | #else | 
| 147 | ndf = ndf_local; | 
| 148 | #endif | 
| 149 |  | 
| 150 | ndf = ndf - 3; | 
| 151 |  | 
| 152 | temperature = ( 2.0 * this->getKinetic() ) / ( ndf * kb ); | 
| 153 | return temperature; | 
| 154 | } | 
| 155 |  | 
| 156 | double Thermo::getPressure(){ | 
| 157 |  | 
| 158 | //  const double conv_Pa_atm = 9.901E-6; // convert Pa -> atm | 
| 159 | // const double conv_internal_Pa = 1.661E-7; //convert amu/(fs^2 A) -> Pa | 
| 160 | //  const double conv_A_m = 1.0E-10; //convert A -> m | 
| 161 |  | 
| 162 | return 0.0; | 
| 163 | } | 
| 164 |  | 
| 165 | void Thermo::velocitize() { | 
| 166 |  | 
| 167 | double x,y; | 
| 168 | double vx, vy, vz; | 
| 169 | double jx, jy, jz; | 
| 170 | int i, vr, vd; // velocity randomizer loop counters | 
| 171 | double vdrift[3]; | 
| 172 | double vbar; | 
| 173 | const double kb = 8.31451e-7; // kb in amu, angstroms, fs, etc. | 
| 174 | double av2; | 
| 175 | double kebar; | 
| 176 | int ndf, ndf_local; // number of degrees of freedom | 
| 177 | int ndfRaw, ndfRaw_local; // the raw number of degrees of freedom | 
| 178 | int n_atoms; | 
| 179 | Atom** atoms; | 
| 180 | DirectionalAtom* dAtom; | 
| 181 | double temperature; | 
| 182 | int n_oriented; | 
| 183 | int n_constraints; | 
| 184 |  | 
| 185 | atoms         = entry_plug->atoms; | 
| 186 | n_atoms       = entry_plug->n_atoms; | 
| 187 | temperature   = entry_plug->target_temp; | 
| 188 | n_oriented    = entry_plug->n_oriented; | 
| 189 | n_constraints = entry_plug->n_constraints; | 
| 190 |  | 
| 191 | // Raw degrees of freedom that we have to set | 
| 192 | ndfRaw_local = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented; | 
| 193 |  | 
| 194 | // Degrees of freedom that can contain kinetic energy | 
| 195 | ndf_local = 3 * entry_plug->n_atoms + 3 * entry_plug->n_oriented | 
| 196 | - entry_plug->n_constraints; | 
| 197 |  | 
| 198 | #ifdef IS_MPI | 
| 199 | MPI::COMM_WORLD.Allreduce(&ndf_local,&ndf,1,MPI_INT,MPI_SUM); | 
| 200 | MPI::COMM_WORLD.Allreduce(&ndfRaw_local,&ndfRaw,1,MPI_INT,MPI_SUM); | 
| 201 | #else | 
| 202 | ndfRaw = ndfRaw_local; | 
| 203 | ndf = ndf_local; | 
| 204 | #endif | 
| 205 | ndf = ndf - 3; | 
| 206 |  | 
| 207 | kebar = kb * temperature * (double)ndf / ( 2.0 * (double)ndfRaw ); | 
| 208 |  | 
| 209 | for(vr = 0; vr < n_atoms; vr++){ | 
| 210 |  | 
| 211 | // uses equipartition theory to solve for vbar in angstrom/fs | 
| 212 |  | 
| 213 | av2 = 2.0 * kebar / atoms[vr]->getMass(); | 
| 214 | vbar = sqrt( av2 ); | 
| 215 |  | 
| 216 | //     vbar = sqrt( 8.31451e-7 * temperature / atoms[vr]->getMass() ); | 
| 217 |  | 
| 218 | // picks random velocities from a gaussian distribution | 
| 219 | // centered on vbar | 
| 220 |  | 
| 221 | vx = vbar * gaussStream->getGaussian(); | 
| 222 | vy = vbar * gaussStream->getGaussian(); | 
| 223 | vz = vbar * gaussStream->getGaussian(); | 
| 224 |  | 
| 225 | atoms[vr]->set_vx( vx ); | 
| 226 | atoms[vr]->set_vy( vy ); | 
| 227 | atoms[vr]->set_vz( vz ); | 
| 228 | } | 
| 229 |  | 
| 230 | // Get the Center of Mass drift velocity. | 
| 231 |  | 
| 232 | getCOMVel(vdrift); | 
| 233 |  | 
| 234 | //  Corrects for the center of mass drift. | 
| 235 | // sums all the momentum and divides by total mass. | 
| 236 |  | 
| 237 | for(vd = 0; vd < n_atoms; vd++){ | 
| 238 |  | 
| 239 | vx = atoms[vd]->get_vx(); | 
| 240 | vy = atoms[vd]->get_vy(); | 
| 241 | vz = atoms[vd]->get_vz(); | 
| 242 |  | 
| 243 | vx -= vdrift[0]; | 
| 244 | vy -= vdrift[1]; | 
| 245 | vz -= vdrift[2]; | 
| 246 |  | 
| 247 | atoms[vd]->set_vx(vx); | 
| 248 | atoms[vd]->set_vy(vy); | 
| 249 | atoms[vd]->set_vz(vz); | 
| 250 | } | 
| 251 | if( n_oriented ){ | 
| 252 |  | 
| 253 | for( i=0; i<n_atoms; i++ ){ | 
| 254 |  | 
| 255 | if( atoms[i]->isDirectional() ){ | 
| 256 |  | 
| 257 | dAtom = (DirectionalAtom *)atoms[i]; | 
| 258 |  | 
| 259 | vbar = sqrt( 2.0 * kebar * dAtom->getIxx() ); | 
| 260 | jx = vbar * gaussStream->getGaussian(); | 
| 261 |  | 
| 262 | vbar = sqrt( 2.0 * kebar * dAtom->getIyy() ); | 
| 263 | jy = vbar * gaussStream->getGaussian(); | 
| 264 |  | 
| 265 | vbar = sqrt( 2.0 * kebar * dAtom->getIzz() ); | 
| 266 | jz = vbar * gaussStream->getGaussian(); | 
| 267 |  | 
| 268 | dAtom->setJx( jx ); | 
| 269 | dAtom->setJy( jy ); | 
| 270 | dAtom->setJz( jz ); | 
| 271 | } | 
| 272 | } | 
| 273 | } | 
| 274 | } | 
| 275 |  | 
| 276 | void Thermo::getCOMVel(double vdrift[3]){ | 
| 277 |  | 
| 278 | double mtot, mtot_local; | 
| 279 | double vdrift_local[3]; | 
| 280 | int vd, n_atoms; | 
| 281 | Atom** atoms; | 
| 282 |  | 
| 283 | // We are very careless here with the distinction between n_atoms and n_local | 
| 284 | // We should really fix this before someone pokes an eye out. | 
| 285 |  | 
| 286 | n_atoms = entry_plug->n_atoms; | 
| 287 | atoms   = entry_plug->atoms; | 
| 288 |  | 
| 289 | mtot_local = 0.0; | 
| 290 | vdrift_local[0] = 0.0; | 
| 291 | vdrift_local[1] = 0.0; | 
| 292 | vdrift_local[2] = 0.0; | 
| 293 |  | 
| 294 | for(vd = 0; vd < n_atoms; vd++){ | 
| 295 |  | 
| 296 | vdrift_local[0] += atoms[vd]->get_vx() * atoms[vd]->getMass(); | 
| 297 | vdrift_local[1] += atoms[vd]->get_vy() * atoms[vd]->getMass(); | 
| 298 | vdrift_local[2] += atoms[vd]->get_vz() * atoms[vd]->getMass(); | 
| 299 |  | 
| 300 | mtot_local += atoms[vd]->getMass(); | 
| 301 | } | 
| 302 |  | 
| 303 | #ifdef IS_MPI | 
| 304 | MPI::COMM_WORLD.Allreduce(&mtot_local,&mtot,1,MPI_DOUBLE,MPI_SUM); | 
| 305 | MPI::COMM_WORLD.Allreduce(vdrift_local,vdrift,3,MPI_DOUBLE,MPI_SUM); | 
| 306 | #else | 
| 307 | mtot = mtot_local; | 
| 308 | for(vd = 0; vd < 3; vd++) { | 
| 309 | vdrift[vd] = vdrift_local[vd]; | 
| 310 | } | 
| 311 | #endif | 
| 312 |  | 
| 313 | for (vd = 0; vd < 3; vd++) { | 
| 314 | vdrift[vd] = vdrift[vd] / mtot; | 
| 315 | } | 
| 316 |  | 
| 317 | } | 
| 318 |  |