| 1 | /* | 
| 2 | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 | * | 
| 4 | * The University of Notre Dame grants you ("Licensee") a | 
| 5 | * non-exclusive, royalty free, license to use, modify and | 
| 6 | * redistribute this software in source and binary code form, provided | 
| 7 | * that the following conditions are met: | 
| 8 | * | 
| 9 | * 1. Acknowledgement of the program authors must be made in any | 
| 10 | *    publication of scientific results based in part on use of the | 
| 11 | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 | *    the article in which the program was described (Matthew | 
| 13 | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 | * | 
| 18 | * 2. Redistributions of source code must retain the above copyright | 
| 19 | *    notice, this list of conditions and the following disclaimer. | 
| 20 | * | 
| 21 | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 | *    notice, this list of conditions and the following disclaimer in the | 
| 23 | *    documentation and/or other materials provided with the | 
| 24 | *    distribution. | 
| 25 | * | 
| 26 | * This software is provided "AS IS," without a warranty of any | 
| 27 | * kind. All express or implied conditions, representations and | 
| 28 | * warranties, including any implied warranty of merchantability, | 
| 29 | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 | * be liable for any damages suffered by licensee as a result of | 
| 32 | * using, modifying or distributing the software or its | 
| 33 | * derivatives. In no event will the University of Notre Dame or its | 
| 34 | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 | * direct, indirect, special, consequential, incidental or punitive | 
| 36 | * damages, however caused and regardless of the theory of liability, | 
| 37 | * arising out of the use of or inability to use software, even if the | 
| 38 | * University of Notre Dame has been advised of the possibility of | 
| 39 | * such damages. | 
| 40 | */ | 
| 41 |  | 
| 42 |  | 
| 43 | /* Creates orientational bond order parameters as outlined by | 
| 44 | *     Bond-orientaional order in liquids and glasses, Steinhart,Nelson,Ronchetti | 
| 45 | *     Phys Rev B, 28,784,1983 | 
| 46 | * | 
| 47 | */ | 
| 48 |  | 
| 49 | #include "applications/staticProps/BondOrderParameter.hpp" | 
| 50 | #include "utils/simError.h" | 
| 51 | #include "io/DumpReader.hpp" | 
| 52 | #include "primitives/Molecule.hpp" | 
| 53 | #include "utils/NumericConstant.hpp" | 
| 54 |  | 
| 55 | namespace oopse { | 
| 56 |  | 
| 57 | BondOrderParameter::BondOrderParameter(SimInfo* info, | 
| 58 | const std::string& filename, | 
| 59 | const std::string& sele, | 
| 60 | double rCut, int nbins) : StaticAnalyser(info, filename), selectionScript_(sele), evaluator_(info), seleMan_(info){ | 
| 61 |  | 
| 62 | setOutputName(getPrefix(filename) + ".bo"); | 
| 63 |  | 
| 64 | evaluator_.loadScriptString(sele); | 
| 65 | if (!evaluator_.isDynamic()) { | 
| 66 | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 67 | } | 
| 68 |  | 
| 69 | // Set up cutoff radius and order of the Legendre Polynomial: | 
| 70 |  | 
| 71 | rCut_ = rCut; | 
| 72 | nBins_ = nbins; | 
| 73 | Qcount_.resize(lMax_+1); | 
| 74 | Wcount_.resize(lMax_+1); | 
| 75 |  | 
| 76 | // Q can take values from 0 to 1 | 
| 77 |  | 
| 78 | MinQ_ = 0.0; | 
| 79 | MaxQ_ = 1.1; | 
| 80 | deltaQ_ = (MaxQ_ - MinQ_) / nbins; | 
| 81 |  | 
| 82 | // W_6 for icosahedral clusters is 11 / sqrt(4199) = 0.169754, so we'll | 
| 83 | // use values for MinW_ and MaxW_ that are slightly larger than this: | 
| 84 |  | 
| 85 | MinW_ = -0.25; | 
| 86 | MaxW_ = 0.25; | 
| 87 | deltaW_ = (MaxW_ - MinW_) / nbins; | 
| 88 |  | 
| 89 | // Make arrays for Wigner3jm | 
| 90 | double* THRCOF = new double[2*lMax_+1]; | 
| 91 | // Variables for Wigner routine | 
| 92 | double lPass, m1Pass, m2m, m2M; | 
| 93 | int error, mSize; | 
| 94 | mSize = 2*lMax_+1; | 
| 95 |  | 
| 96 | for (int l = 0; l <= lMax_; l++) { | 
| 97 | lPass = (double)l; | 
| 98 | for (int m1 = -l; m1 <= l; m1++) { | 
| 99 | m1Pass = (double)m1; | 
| 100 |  | 
| 101 | std::pair<int,int> lm = std::make_pair(l, m1); | 
| 102 |  | 
| 103 | // Zero work array | 
| 104 | for (int ii = 0; ii < 2*l + 1; ii++){ | 
| 105 | THRCOF[ii] = 0.0; | 
| 106 | } | 
| 107 |  | 
| 108 | // Get Wigner coefficients | 
| 109 | Wigner3jm(&lPass, &lPass, &lPass, | 
| 110 | &m1Pass, &m2m, &m2M, | 
| 111 | THRCOF, &mSize, &error); | 
| 112 |  | 
| 113 | m2Min[lm] = (int)floor(m2m); | 
| 114 | m2Max[lm] = (int)floor(m2M); | 
| 115 |  | 
| 116 | for (int mmm = 0; mmm < (int)(m2M - m2m); mmm++) { | 
| 117 | w3j[lm].push_back(THRCOF[mmm]); | 
| 118 | } | 
| 119 | } | 
| 120 | } | 
| 121 | } | 
| 122 |  | 
| 123 | BondOrderParameter::~BondOrderParameter() { | 
| 124 | Q_histogram_.clear(); | 
| 125 | W_histogram_.clear(); | 
| 126 | } | 
| 127 |  | 
| 128 | void BondOrderParameter::initalizeHistogram() { | 
| 129 | for (int bin = 0; bin < nBins_; bin++) { | 
| 130 | for (int l = 0; l <= lMax_; l++) { | 
| 131 | Q_histogram_[std::make_pair(bin,l)] = 0; | 
| 132 | W_histogram_[std::make_pair(bin,l)] = 0; | 
| 133 | } | 
| 134 | } | 
| 135 | } | 
| 136 |  | 
| 137 | void BondOrderParameter::process() { | 
| 138 | Molecule* mol; | 
| 139 | Atom* atom; | 
| 140 | RigidBody* rb; | 
| 141 | int myIndex; | 
| 142 | SimInfo::MoleculeIterator mi; | 
| 143 | Molecule::RigidBodyIterator rbIter; | 
| 144 | Molecule::AtomIterator ai; | 
| 145 | StuntDouble* sd; | 
| 146 | Vector3d vec; | 
| 147 | RealType costheta; | 
| 148 | RealType phi; | 
| 149 | RealType r; | 
| 150 | RealType dist; | 
| 151 | std::map<std::pair<int,int>,ComplexType> q; | 
| 152 | std::vector<RealType> q_l; | 
| 153 | std::vector<RealType> q2; | 
| 154 | std::vector<ComplexType> w; | 
| 155 | std::vector<ComplexType> w_hat; | 
| 156 | std::map<std::pair<int,int>,ComplexType> QBar; | 
| 157 | std::vector<RealType> Q2; | 
| 158 | std::vector<RealType> Q; | 
| 159 | std::vector<ComplexType> W; | 
| 160 | std::vector<ComplexType> W_hat; | 
| 161 | int nBonds, Nbonds; | 
| 162 | SphericalHarmonic sphericalHarmonic; | 
| 163 | int i, j; | 
| 164 |  | 
| 165 | DumpReader reader(info_, dumpFilename_); | 
| 166 | int nFrames = reader.getNFrames(); | 
| 167 | frameCounter_ = 0; | 
| 168 |  | 
| 169 | q_l.resize(lMax_+1); | 
| 170 | q2.resize(lMax_+1); | 
| 171 | w.resize(lMax_+1); | 
| 172 | w_hat.resize(lMax_+1); | 
| 173 |  | 
| 174 | Q2.resize(lMax_+1); | 
| 175 | Q.resize(lMax_+1); | 
| 176 | W.resize(lMax_+1); | 
| 177 | W_hat.resize(lMax_+1); | 
| 178 |  | 
| 179 | for (int istep = 0; istep < nFrames; istep += step_) { | 
| 180 | reader.readFrame(istep); | 
| 181 | frameCounter_++; | 
| 182 | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 183 |  | 
| 184 | if (evaluator_.isDynamic()) { | 
| 185 | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 186 | } | 
| 187 |  | 
| 188 | // update the positions of atoms which belong to the rigidbodies | 
| 189 |  | 
| 190 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 191 | mol = info_->nextMolecule(mi)) { | 
| 192 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; | 
| 193 | rb = mol->nextRigidBody(rbIter)) { | 
| 194 | rb->updateAtoms(); | 
| 195 | } | 
| 196 | } | 
| 197 |  | 
| 198 | // outer loop is over the selected StuntDoubles: | 
| 199 |  | 
| 200 | for (sd = seleMan_.beginSelected(i); sd != NULL; | 
| 201 | sd = seleMan_.nextSelected(i)) { | 
| 202 |  | 
| 203 | myIndex = sd->getGlobalIndex(); | 
| 204 | nBonds = 0; | 
| 205 |  | 
| 206 | for (int l = 0; l <= lMax_; l++) { | 
| 207 | for (int m = -l; m <= l; m++) { | 
| 208 | q[std::make_pair(l,m)] = 0.0; | 
| 209 | } | 
| 210 | } | 
| 211 |  | 
| 212 | // inner loop is over all other atoms in the system: | 
| 213 |  | 
| 214 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 215 | mol = info_->nextMolecule(mi)) { | 
| 216 | for (atom = mol->beginAtom(ai); atom != NULL; | 
| 217 | atom = mol->nextAtom(ai)) { | 
| 218 |  | 
| 219 | if (atom->getGlobalIndex() != myIndex) { | 
| 220 |  | 
| 221 | vec = sd->getPos() - atom->getPos(); | 
| 222 | currentSnapshot_->wrapVector(vec); | 
| 223 |  | 
| 224 | // Calculate "bonds" and build Q_lm(r) where | 
| 225 | //      Q_lm = Y_lm(theta(r),phi(r)) | 
| 226 | // The spherical harmonics are wrt any arbitrary coordinate | 
| 227 | // system, we choose standard spherical coordinates | 
| 228 |  | 
| 229 | r = vec.length(); | 
| 230 |  | 
| 231 | // Check to see if neighbor is in bond cutoff | 
| 232 |  | 
| 233 | if (r < rCut_) { | 
| 234 | costheta = vec.z() / r; | 
| 235 | phi = atan2(vec.y(), vec.x()); | 
| 236 |  | 
| 237 | for (int l = 0; l <= lMax_; l++) { | 
| 238 | sphericalHarmonic.setL(l); | 
| 239 | for(int m = -l; m <= l; m++){ | 
| 240 | sphericalHarmonic.setM(m); | 
| 241 | q[std::make_pair(l,m)] += sphericalHarmonic.getValueAt(costheta, phi); | 
| 242 | } | 
| 243 | } | 
| 244 | nBonds++; | 
| 245 | } | 
| 246 | } | 
| 247 | } | 
| 248 | } | 
| 249 |  | 
| 250 |  | 
| 251 | for (int l = 0; l <= lMax_; l++) { | 
| 252 | q_l[l] = 0.0; | 
| 253 | for(int m = -l; m <= l; m++) { | 
| 254 | q_l[l] += norm(q[std::make_pair(l,m)]); | 
| 255 | } | 
| 256 | q_l[l] *= 4.0*NumericConstant::PI/(RealType)(2*l + 1); | 
| 257 | q_l[l] = sqrt(q_l[l])/(RealType)nBonds; | 
| 258 | } | 
| 259 |  | 
| 260 | // Find second order invariant Q_l | 
| 261 |  | 
| 262 | for (int l = 0; l <= lMax_; l++) { | 
| 263 | q2[l] = 0.0; | 
| 264 | for (int m = -l; m <= l; m++){ | 
| 265 | q2[l] += norm(q[std::make_pair(l,m)]); | 
| 266 | } | 
| 267 | q_l[l] = sqrt(q2[l] * 4.0 * NumericConstant::PI / | 
| 268 | (RealType)(2*l + 1))/(RealType)nBonds; | 
| 269 | } | 
| 270 |  | 
| 271 | // Find Third Order Invariant W_l | 
| 272 |  | 
| 273 | for (int l = 0; l <= lMax_; l++) { | 
| 274 | w[l] = 0.0; | 
| 275 | for (int m1 = -l; m1 <= l; m1++) { | 
| 276 | std::pair<int,int> lm = std::make_pair(l, m1); | 
| 277 | for (int mmm = 0; mmm < (m2Max[lm] - m2Min[lm]); mmm++) { | 
| 278 | int m2 = m2Min[lm] + mmm; | 
| 279 | int m3 = -m1-m2; | 
| 280 | w[l] += w3j[lm][mmm] * q[lm] * | 
| 281 | q[std::make_pair(l,m2)] *  q[std::make_pair(l,m3)]; | 
| 282 | } | 
| 283 | } | 
| 284 |  | 
| 285 | w_hat[l] = w[l] / pow(q2[l], 1.5); | 
| 286 | } | 
| 287 |  | 
| 288 | collectHistogram(q_l, w_hat); | 
| 289 |  | 
| 290 | Nbonds += nBonds; | 
| 291 | for (int l = 0; l <= lMax_;  l++) { | 
| 292 | for (int m = -l; m <= l; m++) { | 
| 293 | QBar[std::make_pair(l,m)] += q[std::make_pair(l,m)]; | 
| 294 | } | 
| 295 | } | 
| 296 | } | 
| 297 | } | 
| 298 |  | 
| 299 | // Normalize Qbar2 | 
| 300 | for (int l = 0; l <= lMax_; l++) { | 
| 301 | for (int m = -l; m <= l; m++){ | 
| 302 | QBar[std::make_pair(l,m)] /= Nbonds; | 
| 303 | } | 
| 304 | } | 
| 305 |  | 
| 306 | // Find second order invariant Q_l | 
| 307 |  | 
| 308 | for (int l = 0; l <= lMax_; l++) { | 
| 309 | Q2[l] = 0.0; | 
| 310 | for (int m = -l; m <= l; m++){ | 
| 311 | Q2[l] += norm(QBar[std::make_pair(l,m)]); | 
| 312 | } | 
| 313 | Q[l] = sqrt(Q2[l] * 4.0 * NumericConstant::PI / (RealType)(2*l + 1)); | 
| 314 | } | 
| 315 |  | 
| 316 | // Find Third Order Invariant W_l | 
| 317 |  | 
| 318 | for (int l = 0; l <= lMax_; l++) { | 
| 319 | W[l] = 0.0; | 
| 320 | for (int m1 = -l; m1 <= l; m1++) { | 
| 321 | std::pair<int,int> lm = std::make_pair(l, m1); | 
| 322 | for (int mmm = 0; mmm < (m2Max[lm] - m2Min[lm]); mmm++) { | 
| 323 | int m2 = m2Min[lm] + mmm; | 
| 324 | int m3 = -m1-m2; | 
| 325 | W[l] += w3j[lm][mmm] * QBar[lm] * | 
| 326 | QBar[std::make_pair(l,m2)] * QBar[std::make_pair(l,m3)]; | 
| 327 | } | 
| 328 | } | 
| 329 |  | 
| 330 | W_hat[l] = W[l] / pow(Q2[l], 1.5); | 
| 331 | } | 
| 332 |  | 
| 333 | writeOrderParameter(Q, W_hat); | 
| 334 | } | 
| 335 |  | 
| 336 | void BondOrderParameter::collectHistogram(std::vector<RealType> q, | 
| 337 | std::vector<ComplexType> what) { | 
| 338 |  | 
| 339 | for (int l = 0; l <= lMax_; l++) { | 
| 340 | if (q[l] >= MinQ_ && q[l] < MaxQ_) { | 
| 341 | int qbin = (q[l] - MinQ_) / deltaQ_; | 
| 342 | Q_histogram_[std::make_pair(qbin,l)] += 1; | 
| 343 | Qcount_[l]++; | 
| 344 | } else { | 
| 345 | sprintf( painCave.errMsg, | 
| 346 | "q_l value outside reasonable range\n"); | 
| 347 | painCave.severity = OOPSE_ERROR; | 
| 348 | painCave.isFatal = 1; | 
| 349 | simError(); | 
| 350 | } | 
| 351 | } | 
| 352 |  | 
| 353 | for (int l = 0; l <= lMax_; l++) { | 
| 354 | if (real(what[l]) >= MinW_ && real(what[l]) < MaxW_) { | 
| 355 | int wbin = (real(what[l]) - MinW_) / deltaW_; | 
| 356 | W_histogram_[std::make_pair(wbin,l)] += 1; | 
| 357 | Wcount_[l]++; | 
| 358 | } else { | 
| 359 | sprintf( painCave.errMsg, | 
| 360 | "Re[w_hat] value outside reasonable range\n"); | 
| 361 | painCave.severity = OOPSE_ERROR; | 
| 362 | painCave.isFatal = 1; | 
| 363 | simError(); | 
| 364 | } | 
| 365 | } | 
| 366 |  | 
| 367 | } | 
| 368 |  | 
| 369 |  | 
| 370 | void BondOrderParameter::writeOrderParameter(std::vector<RealType> Q, | 
| 371 | std::vector<ComplexType> What) { | 
| 372 |  | 
| 373 | std::ofstream osq((getOutputFileName() + "q").c_str()); | 
| 374 |  | 
| 375 | if (osq.is_open()) { | 
| 376 |  | 
| 377 | osq << "# Bond Order Parameters\n"; | 
| 378 | osq << "# selection: (" << selectionScript_ << ")\n"; | 
| 379 | osq << "# \n"; | 
| 380 | for (int l = 0; l <= lMax_; l++) { | 
| 381 | osq << "# <Q_" << l << ">: " << Q[l] << "\n"; | 
| 382 | } | 
| 383 | // Normalize by number of frames and write it out: | 
| 384 | for (int i = 0; i < nBins_; ++i) { | 
| 385 | RealType Qval = MinQ_ + (i + 0.5) * deltaQ_; | 
| 386 | osq << Qval; | 
| 387 | for (int l = 0; l <= lMax_; l++) { | 
| 388 | osq << "\t" << (RealType)Q_histogram_[std::make_pair(i,l)] / (RealType)Qcount_[l]; | 
| 389 | } | 
| 390 | osq << "\n"; | 
| 391 | } | 
| 392 |  | 
| 393 | osq.close(); | 
| 394 |  | 
| 395 | } else { | 
| 396 | sprintf(painCave.errMsg, "BondOrderParameter: unable to open %s\n", | 
| 397 | (getOutputFileName() + "q").c_str()); | 
| 398 | painCave.isFatal = 1; | 
| 399 | simError(); | 
| 400 | } | 
| 401 |  | 
| 402 | std::ofstream osw((getOutputFileName() + "w").c_str()); | 
| 403 |  | 
| 404 | if (osw.is_open()) { | 
| 405 | osw << "# Bond Order Parameters\n"; | 
| 406 | osw << "# selection: (" << selectionScript_ << ")\n"; | 
| 407 | osw << "# \n"; | 
| 408 | for (int l = 0; l <= lMax_; l++) { | 
| 409 | osw << "# <W_" << l << ">: " << real(What[l]) << "\n"; | 
| 410 | } | 
| 411 | // Normalize by number of frames and write it out: | 
| 412 | for (int i = 0; i < nBins_; ++i) { | 
| 413 | RealType Wval = MinW_ + (i + 0.5) * deltaW_; | 
| 414 | osw << Wval; | 
| 415 | for (int l = 0; l <= lMax_; l++) { | 
| 416 | osw << "\t" << (RealType)W_histogram_[std::make_pair(i,l)] / (RealType)Wcount_[l]; | 
| 417 | } | 
| 418 | osw << "\n"; | 
| 419 | } | 
| 420 |  | 
| 421 | osw.close(); | 
| 422 | } else { | 
| 423 | sprintf(painCave.errMsg, "BondOrderParameter: unable to open %s\n", | 
| 424 | (getOutputFileName() + "w").c_str()); | 
| 425 | painCave.isFatal = 1; | 
| 426 | simError(); | 
| 427 | } | 
| 428 |  | 
| 429 | } | 
| 430 | } |