| 1 | gezelter | 2 | #include <math.h> | 
| 2 |  |  | #include "MatVec3.h" | 
| 3 |  |  | #include "Atom.hpp" | 
| 4 |  |  | #include "SRI.hpp" | 
| 5 |  |  | #include "AbstractClasses.hpp" | 
| 6 |  |  | #include "SimInfo.hpp" | 
| 7 |  |  | #include "ForceFields.hpp" | 
| 8 |  |  | #include "Thermo.hpp" | 
| 9 |  |  | #include "ReadWrite.hpp" | 
| 10 |  |  | #include "Integrator.hpp" | 
| 11 |  |  | #include "simError.h" | 
| 12 |  |  |  | 
| 13 |  |  | #ifdef IS_MPI | 
| 14 |  |  | #include "mpiSimulation.hpp" | 
| 15 |  |  | #endif | 
| 16 |  |  |  | 
| 17 |  |  | // Basic non-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 |  |  | template<typename T> NPTxyz<T>::NPTxyz ( SimInfo *theInfo, ForceFields* the_ff): | 
| 28 |  |  | T( theInfo, the_ff ) | 
| 29 |  |  | { | 
| 30 |  |  | GenericData* data; | 
| 31 |  |  | DoubleArrayData * etaValue; | 
| 32 |  |  | vector<double> etaArray; | 
| 33 |  |  | int i,j; | 
| 34 |  |  |  | 
| 35 |  |  | for(i = 0; i < 3; i++){ | 
| 36 |  |  | for (j = 0; j < 3; j++){ | 
| 37 |  |  |  | 
| 38 |  |  | eta[i][j] = 0.0; | 
| 39 |  |  | oldEta[i][j] = 0.0; | 
| 40 |  |  | } | 
| 41 |  |  | } | 
| 42 |  |  |  | 
| 43 |  |  |  | 
| 44 |  |  | if( theInfo->useInitXSstate ){ | 
| 45 |  |  |  | 
| 46 |  |  | // retrieve eta array from simInfo if it exists | 
| 47 |  |  | data = info->getProperty(ETAVALUE_ID); | 
| 48 |  |  | if(data){ | 
| 49 |  |  | etaValue = dynamic_cast<DoubleArrayData*>(data); | 
| 50 |  |  |  | 
| 51 |  |  | if(etaValue){ | 
| 52 |  |  | etaArray = etaValue->getData(); | 
| 53 |  |  |  | 
| 54 |  |  | for(i = 0; i < 3; i++){ | 
| 55 |  |  | for (j = 0; j < 3; j++){ | 
| 56 |  |  | eta[i][j] = etaArray[3*i+j]; | 
| 57 |  |  | oldEta[i][j] = eta[i][j]; | 
| 58 |  |  | } | 
| 59 |  |  | } | 
| 60 |  |  | } | 
| 61 |  |  | } | 
| 62 |  |  | } | 
| 63 |  |  | } | 
| 64 |  |  |  | 
| 65 |  |  | template<typename T> NPTxyz<T>::~NPTxyz() { | 
| 66 |  |  |  | 
| 67 |  |  | // empty for now | 
| 68 |  |  | } | 
| 69 |  |  |  | 
| 70 |  |  | template<typename T> void NPTxyz<T>::resetIntegrator() { | 
| 71 |  |  |  | 
| 72 |  |  | int i, j; | 
| 73 |  |  |  | 
| 74 |  |  | for(i = 0; i < 3; i++) | 
| 75 |  |  | for (j = 0; j < 3; j++) | 
| 76 |  |  | eta[i][j] = 0.0; | 
| 77 |  |  |  | 
| 78 |  |  | T::resetIntegrator(); | 
| 79 |  |  | } | 
| 80 |  |  |  | 
| 81 |  |  | template<typename T> void NPTxyz<T>::evolveEtaA() { | 
| 82 |  |  |  | 
| 83 |  |  | int i, j; | 
| 84 |  |  |  | 
| 85 |  |  | for(i = 0; i < 3; i ++){ | 
| 86 |  |  | for(j = 0; j < 3; j++){ | 
| 87 |  |  | if( i == j) | 
| 88 |  |  | eta[i][j] += dt2 *  instaVol * | 
| 89 |  |  | (press[i][j] - targetPressure/p_convert) / (NkBT*tb2); | 
| 90 |  |  | else | 
| 91 |  |  | eta[i][j] = 0.0; | 
| 92 |  |  | } | 
| 93 |  |  | } | 
| 94 |  |  |  | 
| 95 |  |  | for(i = 0; i < 3; i++) | 
| 96 |  |  | for (j = 0; j < 3; j++) | 
| 97 |  |  | oldEta[i][j] = eta[i][j]; | 
| 98 |  |  | } | 
| 99 |  |  |  | 
| 100 |  |  | template<typename T> void NPTxyz<T>::evolveEtaB() { | 
| 101 |  |  |  | 
| 102 |  |  | int i,j; | 
| 103 |  |  |  | 
| 104 |  |  | for(i = 0; i < 3; i++) | 
| 105 |  |  | for (j = 0; j < 3; j++) | 
| 106 |  |  | prevEta[i][j] = eta[i][j]; | 
| 107 |  |  |  | 
| 108 |  |  | for(i = 0; i < 3; i ++){ | 
| 109 |  |  | for(j = 0; j < 3; j++){ | 
| 110 |  |  | if( i == j) { | 
| 111 |  |  | eta[i][j] = oldEta[i][j] + dt2 *  instaVol * | 
| 112 |  |  | (press[i][j] - targetPressure/p_convert) / (NkBT*tb2); | 
| 113 |  |  | } else { | 
| 114 |  |  | eta[i][j] = 0.0; | 
| 115 |  |  | } | 
| 116 |  |  | } | 
| 117 |  |  | } | 
| 118 |  |  | } | 
| 119 |  |  |  | 
| 120 |  |  | template<typename T> void NPTxyz<T>::calcVelScale(void) { | 
| 121 |  |  | int i,j; | 
| 122 |  |  |  | 
| 123 |  |  | for (i = 0; i < 3; i++ ) { | 
| 124 |  |  | for (j = 0; j < 3; j++ ) { | 
| 125 |  |  | vScale[i][j] = eta[i][j]; | 
| 126 |  |  |  | 
| 127 |  |  | if (i == j) { | 
| 128 |  |  | vScale[i][j] += chi; | 
| 129 |  |  | } | 
| 130 |  |  | } | 
| 131 |  |  | } | 
| 132 |  |  | } | 
| 133 |  |  |  | 
| 134 |  |  | template<typename T> void NPTxyz<T>::getVelScaleA(double sc[3], double vel[3]) { | 
| 135 |  |  | matVecMul3( vScale, vel, sc ); | 
| 136 |  |  | } | 
| 137 |  |  |  | 
| 138 |  |  | template<typename T> void NPTxyz<T>::getVelScaleB(double sc[3], int index ){ | 
| 139 |  |  | int j; | 
| 140 |  |  | double myVel[3]; | 
| 141 |  |  |  | 
| 142 |  |  | for (j = 0; j < 3; j++) | 
| 143 |  |  | myVel[j] = oldVel[3*index + j]; | 
| 144 |  |  |  | 
| 145 |  |  | matVecMul3( vScale, myVel, sc ); | 
| 146 |  |  | } | 
| 147 |  |  |  | 
| 148 |  |  | template<typename T> void NPTxyz<T>::getPosScale(double pos[3], double COM[3], | 
| 149 |  |  | int index, double sc[3]){ | 
| 150 |  |  | int j; | 
| 151 |  |  | double rj[3]; | 
| 152 |  |  |  | 
| 153 |  |  | for(j=0; j<3; j++) | 
| 154 |  |  | rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; | 
| 155 |  |  |  | 
| 156 |  |  | matVecMul3( eta, rj, sc ); | 
| 157 |  |  | } | 
| 158 |  |  |  | 
| 159 |  |  | template<typename T> void NPTxyz<T>::scaleSimBox( void ){ | 
| 160 |  |  |  | 
| 161 |  |  | int i,j,k; | 
| 162 |  |  | double scaleMat[3][3]; | 
| 163 |  |  | double eta2ij, scaleFactor; | 
| 164 |  |  | double bigScale, smallScale, offDiagMax; | 
| 165 |  |  | double hm[3][3], hmnew[3][3]; | 
| 166 |  |  |  | 
| 167 |  |  |  | 
| 168 |  |  |  | 
| 169 |  |  | // Scale the box after all the positions have been moved: | 
| 170 |  |  |  | 
| 171 |  |  | // Use a taylor expansion for eta products:  Hmat = Hmat . exp(dt * etaMat) | 
| 172 |  |  | //  Hmat = Hmat . ( Ident + dt * etaMat  + dt^2 * etaMat*etaMat / 2) | 
| 173 |  |  |  | 
| 174 |  |  | bigScale = 1.0; | 
| 175 |  |  | smallScale = 1.0; | 
| 176 |  |  | offDiagMax = 0.0; | 
| 177 |  |  |  | 
| 178 |  |  | for(i=0; i<3; i++){ | 
| 179 |  |  | for(j=0; j<3; j++){ | 
| 180 |  |  | scaleMat[i][j] = 0.0; | 
| 181 |  |  | if(i==j) scaleMat[i][j] = 1.0; | 
| 182 |  |  | } | 
| 183 |  |  | } | 
| 184 |  |  |  | 
| 185 |  |  | for(i=0;i<3;i++){ | 
| 186 |  |  |  | 
| 187 |  |  | // calculate the scaleFactors | 
| 188 |  |  |  | 
| 189 |  |  | scaleFactor = exp(dt*eta[i][i]); | 
| 190 |  |  |  | 
| 191 |  |  | scaleMat[i][i] = scaleFactor; | 
| 192 |  |  |  | 
| 193 |  |  | if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i]; | 
| 194 |  |  | if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i]; | 
| 195 |  |  | } | 
| 196 |  |  |  | 
| 197 |  |  | //   for(i=0; i<3; i++){ | 
| 198 |  |  | //     for(j=0; j<3; j++){ | 
| 199 |  |  |  | 
| 200 |  |  | //       // Calculate the matrix Product of the eta array (we only need | 
| 201 |  |  | //       // the ij element right now): | 
| 202 |  |  |  | 
| 203 |  |  | //       eta2ij = 0.0; | 
| 204 |  |  | //       for(k=0; k<3; k++){ | 
| 205 |  |  | //         eta2ij += eta[i][k] * eta[k][j]; | 
| 206 |  |  | //       } | 
| 207 |  |  |  | 
| 208 |  |  | //       scaleMat[i][j] = 0.0; | 
| 209 |  |  | //       // identity matrix (see above): | 
| 210 |  |  | //       if (i == j) scaleMat[i][j] = 1.0; | 
| 211 |  |  | //       // Taylor expansion for the exponential truncated at second order: | 
| 212 |  |  | //       scaleMat[i][j] += dt*eta[i][j]  + 0.5*dt*dt*eta2ij; | 
| 213 |  |  |  | 
| 214 |  |  | //       if (i != j) | 
| 215 |  |  | //         if (fabs(scaleMat[i][j]) > offDiagMax) | 
| 216 |  |  | //           offDiagMax = fabs(scaleMat[i][j]); | 
| 217 |  |  | //     } | 
| 218 |  |  |  | 
| 219 |  |  | //     if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i]; | 
| 220 |  |  | //     if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i]; | 
| 221 |  |  | //   } | 
| 222 |  |  |  | 
| 223 |  |  | if ((bigScale > 1.1) || (smallScale < 0.9)) { | 
| 224 |  |  | sprintf( painCave.errMsg, | 
| 225 |  |  | "NPTxyz error: Attempting a Box scaling of more than 10 percent.\n" | 
| 226 |  |  | " Check your tauBarostat, as it is probably too small!\n\n" | 
| 227 |  |  | " scaleMat = [%lf\t%lf\t%lf]\n" | 
| 228 |  |  | "            [%lf\t%lf\t%lf]\n" | 
| 229 |  |  | "            [%lf\t%lf\t%lf]\n", | 
| 230 |  |  | scaleMat[0][0],scaleMat[0][1],scaleMat[0][2], | 
| 231 |  |  | scaleMat[1][0],scaleMat[1][1],scaleMat[1][2], | 
| 232 |  |  | scaleMat[2][0],scaleMat[2][1],scaleMat[2][2]); | 
| 233 |  |  | painCave.isFatal = 1; | 
| 234 |  |  | simError(); | 
| 235 |  |  | } else { | 
| 236 |  |  | info->getBoxM(hm); | 
| 237 |  |  | matMul3(hm, scaleMat, hmnew); | 
| 238 |  |  | info->setBoxM(hmnew); | 
| 239 |  |  | } | 
| 240 |  |  | } | 
| 241 |  |  |  | 
| 242 |  |  | template<typename T> bool NPTxyz<T>::etaConverged() { | 
| 243 |  |  | int i; | 
| 244 |  |  | double diffEta, sumEta; | 
| 245 |  |  |  | 
| 246 |  |  | sumEta = 0; | 
| 247 |  |  | for(i = 0; i < 3; i++) | 
| 248 |  |  | sumEta += pow(prevEta[i][i] - eta[i][i], 2); | 
| 249 |  |  |  | 
| 250 |  |  | diffEta = sqrt( sumEta / 3.0 ); | 
| 251 |  |  |  | 
| 252 |  |  | return ( diffEta <= etaTolerance ); | 
| 253 |  |  | } | 
| 254 |  |  |  | 
| 255 |  |  | template<typename T> double NPTxyz<T>::getConservedQuantity(void){ | 
| 256 |  |  |  | 
| 257 |  |  | double conservedQuantity; | 
| 258 |  |  | double totalEnergy; | 
| 259 |  |  | double thermostat_kinetic; | 
| 260 |  |  | double thermostat_potential; | 
| 261 |  |  | double barostat_kinetic; | 
| 262 |  |  | double barostat_potential; | 
| 263 |  |  | double trEta; | 
| 264 |  |  | double a[3][3], b[3][3]; | 
| 265 |  |  |  | 
| 266 |  |  | totalEnergy = tStats->getTotalE(); | 
| 267 |  |  |  | 
| 268 |  |  | thermostat_kinetic = fkBT * tt2 * chi * chi / | 
| 269 |  |  | (2.0 * eConvert); | 
| 270 |  |  |  | 
| 271 |  |  | thermostat_potential = fkBT* integralOfChidt / eConvert; | 
| 272 |  |  |  | 
| 273 |  |  | transposeMat3(eta, a); | 
| 274 |  |  | matMul3(a, eta, b); | 
| 275 |  |  | trEta = matTrace3(b); | 
| 276 |  |  |  | 
| 277 |  |  | barostat_kinetic = NkBT * tb2 * trEta / | 
| 278 |  |  | (2.0 * eConvert); | 
| 279 |  |  |  | 
| 280 |  |  | barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / | 
| 281 |  |  | eConvert; | 
| 282 |  |  |  | 
| 283 |  |  | conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential + | 
| 284 |  |  | barostat_kinetic + barostat_potential; | 
| 285 |  |  |  | 
| 286 |  |  | //   cout.width(8); | 
| 287 |  |  | //   cout.precision(8); | 
| 288 |  |  |  | 
| 289 |  |  | //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << | 
| 290 |  |  | //       "\t" << thermostat_potential << "\t" << barostat_kinetic << | 
| 291 |  |  | //       "\t" << barostat_potential << "\t" << conservedQuantity << endl; | 
| 292 |  |  |  | 
| 293 |  |  | return conservedQuantity; | 
| 294 |  |  |  | 
| 295 |  |  | } | 
| 296 |  |  |  | 
| 297 |  |  | template<typename T> string NPTxyz<T>::getAdditionalParameters(void){ | 
| 298 |  |  | string parameters; | 
| 299 |  |  | const int BUFFERSIZE = 2000; // size of the read buffer | 
| 300 |  |  | char buffer[BUFFERSIZE]; | 
| 301 |  |  |  | 
| 302 |  |  | sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt); | 
| 303 |  |  | parameters += buffer; | 
| 304 |  |  |  | 
| 305 |  |  | for(int i = 0; i < 3; i++){ | 
| 306 |  |  | sprintf(buffer,"\t%G\t%G\t%G;", eta[i][0], eta[i][1], eta[i][2]); | 
| 307 |  |  | parameters += buffer; | 
| 308 |  |  | } | 
| 309 |  |  |  | 
| 310 |  |  | return parameters; | 
| 311 |  |  |  | 
| 312 |  |  | } |