| 1 | #include <math.h> | 
| 2 | #include "Atom.hpp" | 
| 3 | #include "SRI.hpp" | 
| 4 | #include "AbstractClasses.hpp" | 
| 5 | #include "SimInfo.hpp" | 
| 6 | #include "ForceFields.hpp" | 
| 7 | #include "Thermo.hpp" | 
| 8 | #include "ReadWrite.hpp" | 
| 9 | #include "Integrator.hpp" | 
| 10 | #include "simError.h" | 
| 11 |  | 
| 12 | #ifdef IS_MPI | 
| 13 | #include "mpiSimulation.hpp" | 
| 14 | #endif | 
| 15 |  | 
| 16 |  | 
| 17 | // Basic isotropic thermostating and barostating via the Melchionna | 
| 18 | // modification of the Hoover algorithm: | 
| 19 | // | 
| 20 | //    Melchionna, S., Ciccotti, G., and Holian, B. L., 1993, | 
| 21 | //       Molec. Phys., 78, 533. | 
| 22 | // | 
| 23 | //           and | 
| 24 | // | 
| 25 | //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. | 
| 26 |  | 
| 27 | NPT::NPT ( SimInfo *theInfo, ForceFields* the_ff): | 
| 28 | Integrator( theInfo, the_ff ) | 
| 29 | { | 
| 30 | GenericData* data; | 
| 31 | DoubleData * chiValue; | 
| 32 | DoubleData * integralOfChidtValue; | 
| 33 |  | 
| 34 | chiValue = NULL; | 
| 35 | integralOfChidtValue = NULL; | 
| 36 |  | 
| 37 | chi = 0.0; | 
| 38 | integralOfChidt = 0.0; | 
| 39 | have_tau_thermostat = 0; | 
| 40 | have_tau_barostat = 0; | 
| 41 | have_target_temp = 0; | 
| 42 | have_target_pressure = 0; | 
| 43 | have_chi_tolerance = 0; | 
| 44 | have_eta_tolerance = 0; | 
| 45 | have_pos_iter_tolerance = 0; | 
| 46 |  | 
| 47 | // retrieve chi and integralOfChidt from simInfo | 
| 48 | data = info->getProperty(CHIVALUE_ID); | 
| 49 | if(data){ | 
| 50 | chiValue = dynamic_cast<DoubleData*>(data); | 
| 51 | } | 
| 52 |  | 
| 53 | data = info->getProperty(INTEGRALOFCHIDT_ID); | 
| 54 | if(data){ | 
| 55 | integralOfChidtValue = dynamic_cast<DoubleData*>(data); | 
| 56 | } | 
| 57 |  | 
| 58 | // chi and integralOfChidt should appear by pair | 
| 59 | if(chiValue && integralOfChidtValue){ | 
| 60 | chi = chiValue->getData(); | 
| 61 | integralOfChidt = integralOfChidtValue->getData(); | 
| 62 | } | 
| 63 |  | 
| 64 | oldPos = new double[3*nAtoms]; | 
| 65 | oldVel = new double[3*nAtoms]; | 
| 66 | oldJi = new double[3*nAtoms]; | 
| 67 | #ifdef IS_MPI | 
| 68 | Nparticles = mpiSim->getTotAtoms(); | 
| 69 | #else | 
| 70 | Nparticles = theInfo->n_atoms; | 
| 71 | #endif | 
| 72 |  | 
| 73 | } | 
| 74 |  | 
| 75 | NPT::~NPT() { | 
| 76 | delete[] oldPos; | 
| 77 | delete[] oldVel; | 
| 78 | delete[] oldJi; | 
| 79 | } | 
| 80 |  | 
| 81 | void NPT::moveA() { | 
| 82 |  | 
| 83 | //new version of NPT | 
| 84 | int i, j, k; | 
| 85 | DirectionalAtom* dAtom; | 
| 86 | double Tb[3], ji[3]; | 
| 87 | double mass; | 
| 88 | double vel[3], pos[3], frc[3]; | 
| 89 | double sc[3]; | 
| 90 | double COM[3]; | 
| 91 |  | 
| 92 | instaTemp = tStats->getTemperature(); | 
| 93 | tStats->getPressureTensor( press ); | 
| 94 | instaPress = p_convert * (press[0][0] + press[1][1] + press[2][2]) / 3.0; | 
| 95 | instaVol = tStats->getVolume(); | 
| 96 |  | 
| 97 | tStats->getCOM(COM); | 
| 98 |  | 
| 99 | //evolve velocity half step | 
| 100 | for( i=0; i<nAtoms; i++ ){ | 
| 101 |  | 
| 102 | atoms[i]->getVel( vel ); | 
| 103 | atoms[i]->getFrc( frc ); | 
| 104 |  | 
| 105 | mass = atoms[i]->getMass(); | 
| 106 |  | 
| 107 | getVelScaleA( sc, vel ); | 
| 108 |  | 
| 109 | for (j=0; j < 3; j++) { | 
| 110 |  | 
| 111 | // velocity half step  (use chi from previous step here): | 
| 112 | vel[j] += dt2 * ((frc[j] / mass ) * eConvert - sc[j]); | 
| 113 |  | 
| 114 | } | 
| 115 |  | 
| 116 | atoms[i]->setVel( vel ); | 
| 117 |  | 
| 118 | if( atoms[i]->isDirectional() ){ | 
| 119 |  | 
| 120 | dAtom = (DirectionalAtom *)atoms[i]; | 
| 121 |  | 
| 122 | // get and convert the torque to body frame | 
| 123 |  | 
| 124 | dAtom->getTrq( Tb ); | 
| 125 | dAtom->lab2Body( Tb ); | 
| 126 |  | 
| 127 | // get the angular momentum, and propagate a half step | 
| 128 |  | 
| 129 | dAtom->getJ( ji ); | 
| 130 |  | 
| 131 | for (j=0; j < 3; j++) | 
| 132 | ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi); | 
| 133 |  | 
| 134 | this->rotationPropagation( dAtom, ji ); | 
| 135 |  | 
| 136 | dAtom->setJ( ji ); | 
| 137 | } | 
| 138 | } | 
| 139 |  | 
| 140 | // evolve chi and eta  half step | 
| 141 |  | 
| 142 | evolveChiA(); | 
| 143 | evolveEtaA(); | 
| 144 |  | 
| 145 | //calculate the integral of chidt | 
| 146 | integralOfChidt += dt2*chi; | 
| 147 |  | 
| 148 | //save the old positions | 
| 149 | for(i = 0; i < nAtoms; i++){ | 
| 150 | atoms[i]->getPos(pos); | 
| 151 | for(j = 0; j < 3; j++) | 
| 152 | oldPos[i*3 + j] = pos[j]; | 
| 153 | } | 
| 154 |  | 
| 155 | //the first estimation of r(t+dt) is equal to  r(t) | 
| 156 |  | 
| 157 | for(k = 0; k < 5; k ++){ | 
| 158 |  | 
| 159 | for(i =0 ; i < nAtoms; i++){ | 
| 160 |  | 
| 161 | atoms[i]->getVel(vel); | 
| 162 | atoms[i]->getPos(pos); | 
| 163 |  | 
| 164 | this->getPosScale( pos, COM, i, sc ); | 
| 165 |  | 
| 166 | for(j = 0; j < 3; j++) | 
| 167 | pos[j] = oldPos[i*3 + j] + dt*(vel[j] + sc[j]); | 
| 168 |  | 
| 169 | atoms[i]->setPos( pos ); | 
| 170 | } | 
| 171 |  | 
| 172 | if (nConstrained){ | 
| 173 | constrainA(); | 
| 174 | } | 
| 175 | } | 
| 176 |  | 
| 177 |  | 
| 178 | // Scale the box after all the positions have been moved: | 
| 179 |  | 
| 180 | this->scaleSimBox(); | 
| 181 | } | 
| 182 |  | 
| 183 | void NPT::moveB( void ){ | 
| 184 |  | 
| 185 | //new version of NPT | 
| 186 | int i, j, k; | 
| 187 | DirectionalAtom* dAtom; | 
| 188 | double Tb[3], ji[3], sc[3]; | 
| 189 | double vel[3], frc[3]; | 
| 190 | double mass; | 
| 191 |  | 
| 192 | // Set things up for the iteration: | 
| 193 |  | 
| 194 | for( i=0; i<nAtoms; i++ ){ | 
| 195 |  | 
| 196 | atoms[i]->getVel( vel ); | 
| 197 |  | 
| 198 | for (j=0; j < 3; j++) | 
| 199 | oldVel[3*i + j]  = vel[j]; | 
| 200 |  | 
| 201 | if( atoms[i]->isDirectional() ){ | 
| 202 |  | 
| 203 | dAtom = (DirectionalAtom *)atoms[i]; | 
| 204 |  | 
| 205 | dAtom->getJ( ji ); | 
| 206 |  | 
| 207 | for (j=0; j < 3; j++) | 
| 208 | oldJi[3*i + j] = ji[j]; | 
| 209 |  | 
| 210 | } | 
| 211 | } | 
| 212 |  | 
| 213 | // do the iteration: | 
| 214 |  | 
| 215 | instaVol = tStats->getVolume(); | 
| 216 |  | 
| 217 | for (k=0; k < 4; k++) { | 
| 218 |  | 
| 219 | instaTemp = tStats->getTemperature(); | 
| 220 | instaPress = tStats->getPressure(); | 
| 221 |  | 
| 222 | // evolve chi another half step using the temperature at t + dt/2 | 
| 223 |  | 
| 224 | this->evolveChiB(); | 
| 225 | this->evolveEtaB(); | 
| 226 |  | 
| 227 | for( i=0; i<nAtoms; i++ ){ | 
| 228 |  | 
| 229 | atoms[i]->getFrc( frc ); | 
| 230 | atoms[i]->getVel(vel); | 
| 231 |  | 
| 232 | mass = atoms[i]->getMass(); | 
| 233 |  | 
| 234 | getVelScaleB( sc, i ); | 
| 235 |  | 
| 236 | // velocity half step | 
| 237 | for (j=0; j < 3; j++) | 
| 238 | vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - sc[j]); | 
| 239 |  | 
| 240 | atoms[i]->setVel( vel ); | 
| 241 |  | 
| 242 | if( atoms[i]->isDirectional() ){ | 
| 243 |  | 
| 244 | dAtom = (DirectionalAtom *)atoms[i]; | 
| 245 |  | 
| 246 | // get and convert the torque to body frame | 
| 247 |  | 
| 248 | dAtom->getTrq( Tb ); | 
| 249 | dAtom->lab2Body( Tb ); | 
| 250 |  | 
| 251 | for (j=0; j < 3; j++) | 
| 252 | ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi); | 
| 253 |  | 
| 254 | dAtom->setJ( ji ); | 
| 255 | } | 
| 256 | } | 
| 257 |  | 
| 258 | if (nConstrained){ | 
| 259 | constrainB(); | 
| 260 | } | 
| 261 |  | 
| 262 | if ( this->chiConverged() && this->etaConverged() ) break; | 
| 263 | } | 
| 264 |  | 
| 265 | //calculate integral of chida | 
| 266 | integralOfChidt += dt2*chi; | 
| 267 |  | 
| 268 |  | 
| 269 | } | 
| 270 |  | 
| 271 | void NPT::resetIntegrator() { | 
| 272 | chi = 0.0; | 
| 273 | Integrator::resetIntegrator(); | 
| 274 | } | 
| 275 |  | 
| 276 | void NPT::evolveChiA() { | 
| 277 | chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; | 
| 278 | oldChi = chi; | 
| 279 | } | 
| 280 |  | 
| 281 | void NPT::evolveChiB() { | 
| 282 |  | 
| 283 | prevChi = chi; | 
| 284 | chi = oldChi + dt2 * ( instaTemp / targetTemp - 1.0) / tt2; | 
| 285 | } | 
| 286 |  | 
| 287 | bool NPT::chiConverged() { | 
| 288 |  | 
| 289 | return ( fabs( prevChi - chi ) <= chiTolerance ); | 
| 290 | } | 
| 291 |  | 
| 292 | int NPT::readyCheck() { | 
| 293 |  | 
| 294 | //check parent's readyCheck() first | 
| 295 | if (Integrator::readyCheck() == -1) | 
| 296 | return -1; | 
| 297 |  | 
| 298 | // First check to see if we have a target temperature. | 
| 299 | // Not having one is fatal. | 
| 300 |  | 
| 301 | if (!have_target_temp) { | 
| 302 | sprintf( painCave.errMsg, | 
| 303 | "NPT error: You can't use the NPT integrator\n" | 
| 304 | "   without a targetTemp!\n" | 
| 305 | ); | 
| 306 | painCave.isFatal = 1; | 
| 307 | simError(); | 
| 308 | return -1; | 
| 309 | } | 
| 310 |  | 
| 311 | if (!have_target_pressure) { | 
| 312 | sprintf( painCave.errMsg, | 
| 313 | "NPT error: You can't use the NPT integrator\n" | 
| 314 | "   without a targetPressure!\n" | 
| 315 | ); | 
| 316 | painCave.isFatal = 1; | 
| 317 | simError(); | 
| 318 | return -1; | 
| 319 | } | 
| 320 |  | 
| 321 | // We must set tauThermostat. | 
| 322 |  | 
| 323 | if (!have_tau_thermostat) { | 
| 324 | sprintf( painCave.errMsg, | 
| 325 | "NPT error: If you use the NPT\n" | 
| 326 | "   integrator, you must set tauThermostat.\n"); | 
| 327 | painCave.isFatal = 1; | 
| 328 | simError(); | 
| 329 | return -1; | 
| 330 | } | 
| 331 |  | 
| 332 | // We must set tauBarostat. | 
| 333 |  | 
| 334 | if (!have_tau_barostat) { | 
| 335 | sprintf( painCave.errMsg, | 
| 336 | "NPT error: If you use the NPT\n" | 
| 337 | "   integrator, you must set tauBarostat.\n"); | 
| 338 | painCave.isFatal = 1; | 
| 339 | simError(); | 
| 340 | return -1; | 
| 341 | } | 
| 342 |  | 
| 343 | if (!have_chi_tolerance) { | 
| 344 | sprintf( painCave.errMsg, | 
| 345 | "NPT warning: setting chi tolerance to 1e-6\n"); | 
| 346 | chiTolerance = 1e-6; | 
| 347 | have_chi_tolerance = 1; | 
| 348 | painCave.isFatal = 0; | 
| 349 | simError(); | 
| 350 | } | 
| 351 |  | 
| 352 | if (!have_eta_tolerance) { | 
| 353 | sprintf( painCave.errMsg, | 
| 354 | "NPT warning: setting eta tolerance to 1e-6\n"); | 
| 355 | etaTolerance = 1e-6; | 
| 356 | have_eta_tolerance = 1; | 
| 357 | painCave.isFatal = 0; | 
| 358 | simError(); | 
| 359 | } | 
| 360 |  | 
| 361 | // We need NkBT a lot, so just set it here: This is the RAW number | 
| 362 | // of particles, so no subtraction or addition of constraints or | 
| 363 | // orientational degrees of freedom: | 
| 364 |  | 
| 365 | NkBT = (double)Nparticles * kB * targetTemp; | 
| 366 |  | 
| 367 | // fkBT is used because the thermostat operates on more degrees of freedom | 
| 368 | // than the barostat (when there are particles with orientational degrees | 
| 369 | // of freedom).  ndf = 3 * (n_atoms + n_oriented -1) - n_constraint - nZcons | 
| 370 |  | 
| 371 | fkBT = (double)info->ndf * kB * targetTemp; | 
| 372 |  | 
| 373 | tt2 = tauThermostat * tauThermostat; | 
| 374 | tb2 = tauBarostat * tauBarostat; | 
| 375 |  | 
| 376 | return 1; | 
| 377 | } |