| 1 | gezelter | 2049 | /* | 
| 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. Redistributions of source code must retain the above copyright | 
| 10 |  |  | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  |  | * | 
| 12 |  |  | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 |  |  | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  |  | *    documentation and/or other materials provided with the | 
| 15 |  |  | *    distribution. | 
| 16 |  |  | * | 
| 17 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 18 |  |  | * kind. All express or implied conditions, representations and | 
| 19 |  |  | * warranties, including any implied warranty of merchantability, | 
| 20 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 23 |  |  | * using, modifying or distributing the software or its | 
| 24 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 25 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 27 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 28 |  |  | * arising out of the use of or inability to use software, even if the | 
| 29 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 30 |  |  | * such damages. | 
| 31 |  |  | * | 
| 32 |  |  | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 |  |  | * research, please cite the appropriate papers when you publish your | 
| 34 |  |  | * work.  Good starting points are: | 
| 35 |  |  | * | 
| 36 |  |  | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 |  |  | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 |  |  | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 |  |  | * [4] Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 |  |  | * [4] , Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 |  |  | */ | 
| 42 |  |  |  | 
| 43 |  |  | #include "applications/staticProps/HBondGeometric.hpp" | 
| 44 |  |  | #include "utils/simError.h" | 
| 45 |  |  | #include "io/DumpReader.hpp" | 
| 46 |  |  | #include "primitives/Molecule.hpp" | 
| 47 |  |  | #include "utils/NumericConstant.hpp" | 
| 48 |  |  |  | 
| 49 |  |  | #include <vector> | 
| 50 |  |  |  | 
| 51 |  |  | namespace OpenMD { | 
| 52 |  |  |  | 
| 53 |  |  | HBondGeometric::HBondGeometric(SimInfo* info, | 
| 54 |  |  | const std::string& filename, | 
| 55 |  |  | const std::string& sele1, | 
| 56 |  |  | const std::string& sele2, | 
| 57 |  |  | double rCut, double thetaCut, int nbins) : | 
| 58 |  |  | StaticAnalyser(info, filename), | 
| 59 | gezelter | 2071 | selectionScript1_(sele1), seleMan1_(info), evaluator1_(info), | 
| 60 |  |  | selectionScript2_(sele2), seleMan2_(info), evaluator2_(info) { | 
| 61 | gezelter | 2049 |  | 
| 62 |  |  | setOutputName(getPrefix(filename) + ".hbg"); | 
| 63 |  |  |  | 
| 64 |  |  | ff_ = info_->getForceField(); | 
| 65 |  |  |  | 
| 66 |  |  | evaluator1_.loadScriptString(sele1); | 
| 67 |  |  | if (!evaluator1_.isDynamic()) { | 
| 68 |  |  | seleMan1_.setSelectionSet(evaluator1_.evaluate()); | 
| 69 |  |  | } | 
| 70 |  |  | evaluator2_.loadScriptString(sele2); | 
| 71 |  |  | if (!evaluator2_.isDynamic()) { | 
| 72 |  |  | seleMan2_.setSelectionSet(evaluator2_.evaluate()); | 
| 73 |  |  | } | 
| 74 |  |  |  | 
| 75 |  |  | // Set up cutoff values: | 
| 76 |  |  |  | 
| 77 |  |  | rCut_ = rCut; | 
| 78 |  |  | thetaCut_ = thetaCut; | 
| 79 |  |  | nBins_ = nbins; | 
| 80 |  |  |  | 
| 81 |  |  | nHBonds_.resize(nBins_); | 
| 82 |  |  | nDonor_.resize(nBins_); | 
| 83 |  |  | nAcceptor_.resize(nBins_); | 
| 84 | gezelter | 2050 |  | 
| 85 |  |  | initializeHistogram(); | 
| 86 | gezelter | 2049 | } | 
| 87 |  |  |  | 
| 88 |  |  | HBondGeometric::~HBondGeometric() { | 
| 89 |  |  | nHBonds_.clear(); | 
| 90 |  |  | nDonor_.clear(); | 
| 91 |  |  | nAcceptor_.clear(); | 
| 92 |  |  | } | 
| 93 |  |  |  | 
| 94 |  |  | void HBondGeometric::initializeHistogram() { | 
| 95 |  |  | std::fill(nHBonds_.begin(),   nHBonds_.end(),   0); | 
| 96 |  |  | std::fill(nDonor_.begin(),    nDonor_.end(),    0); | 
| 97 |  |  | std::fill(nAcceptor_.begin(), nAcceptor_.end(), 0); | 
| 98 |  |  | nSelected_ = 0; | 
| 99 |  |  | } | 
| 100 |  |  |  | 
| 101 |  |  | void HBondGeometric::process() { | 
| 102 | gezelter | 2052 | Molecule* mol1; | 
| 103 |  |  | Molecule* mol2; | 
| 104 | gezelter | 2049 | RigidBody* rb1; | 
| 105 | gezelter | 2052 | Molecule::HBondDonor* hbd1; | 
| 106 |  |  | Molecule::HBondDonor* hbd2; | 
| 107 |  |  | std::vector<Molecule::HBondDonor*>::iterator hbdi; | 
| 108 |  |  | std::vector<Molecule::HBondDonor*>::iterator hbdj; | 
| 109 |  |  | std::vector<Atom*>::iterator hbai; | 
| 110 |  |  | std::vector<Atom*>::iterator hbaj; | 
| 111 |  |  | Atom* hba1; | 
| 112 |  |  | Atom* hba2; | 
| 113 | gezelter | 2049 | SimInfo::MoleculeIterator mi; | 
| 114 |  |  | Molecule::RigidBodyIterator rbIter; | 
| 115 | gezelter | 2052 | Vector3d dPos; | 
| 116 |  |  | Vector3d aPos; | 
| 117 |  |  | Vector3d hPos; | 
| 118 |  |  | Vector3d DH; | 
| 119 |  |  | Vector3d DA; | 
| 120 |  |  | RealType DAdist, DHdist, theta, ctheta; | 
| 121 | gezelter | 2049 | int ii, jj; | 
| 122 |  |  | int nHB, nA, nD; | 
| 123 |  |  |  | 
| 124 |  |  | DumpReader reader(info_, dumpFilename_); | 
| 125 |  |  | int nFrames = reader.getNFrames(); | 
| 126 |  |  | frameCounter_ = 0; | 
| 127 |  |  |  | 
| 128 |  |  | for (int istep = 0; istep < nFrames; istep += step_) { | 
| 129 |  |  | reader.readFrame(istep); | 
| 130 |  |  | frameCounter_++; | 
| 131 |  |  | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 132 |  |  |  | 
| 133 |  |  | // update the positions of atoms which belong to the rigidbodies | 
| 134 |  |  |  | 
| 135 | gezelter | 2052 | for (mol1 = info_->beginMolecule(mi); mol1 != NULL; | 
| 136 |  |  | mol1 = info_->nextMolecule(mi)) { | 
| 137 |  |  | for (rb1 = mol1->beginRigidBody(rbIter); rb1 != NULL; | 
| 138 |  |  | rb1 = mol1->nextRigidBody(rbIter)) { | 
| 139 | gezelter | 2049 | rb1->updateAtoms(); | 
| 140 |  |  | } | 
| 141 |  |  | } | 
| 142 |  |  |  | 
| 143 |  |  | if  (evaluator1_.isDynamic()) { | 
| 144 |  |  | seleMan1_.setSelectionSet(evaluator1_.evaluate()); | 
| 145 |  |  | } | 
| 146 |  |  | if  (evaluator2_.isDynamic()) { | 
| 147 |  |  | seleMan2_.setSelectionSet(evaluator2_.evaluate()); | 
| 148 |  |  | } | 
| 149 |  |  |  | 
| 150 | gezelter | 2052 | for (mol1 = seleMan1_.beginSelectedMolecule(ii); | 
| 151 |  |  | mol1 != NULL; mol1 = seleMan1_.nextSelectedMolecule(ii)) { | 
| 152 | gezelter | 2049 |  | 
| 153 | gezelter | 2052 | // We're collecting statistics on the molecules in selection 1: | 
| 154 | gezelter | 2049 | nHB = 0; | 
| 155 |  |  | nA = 0; | 
| 156 |  |  | nD = 0; | 
| 157 |  |  |  | 
| 158 | gezelter | 2052 | for (mol2 = seleMan2_.beginSelectedMolecule(jj); | 
| 159 |  |  | mol2 != NULL; mol2 = seleMan2_.nextSelectedMolecule(jj)) { | 
| 160 |  |  |  | 
| 161 |  |  | // loop over the possible donors in molecule 1: | 
| 162 |  |  | for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL; | 
| 163 |  |  | hbd1 = mol1->nextHBondDonor(hbdi)) { | 
| 164 |  |  | dPos = hbd1->donorAtom->getPos(); | 
| 165 |  |  | hPos = hbd1->donatedHydrogen->getPos(); | 
| 166 |  |  | DH = hPos - dPos; | 
| 167 |  |  | currentSnapshot_->wrapVector(DH); | 
| 168 |  |  | DHdist = DH.length(); | 
| 169 | gezelter | 2049 |  | 
| 170 | gezelter | 2052 | // loop over the possible acceptors in molecule 2: | 
| 171 |  |  | for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL; | 
| 172 |  |  | hba2 = mol2->nextHBondAcceptor(hbaj)) { | 
| 173 |  |  | aPos = hba2->getPos(); | 
| 174 |  |  | DA = aPos - dPos; | 
| 175 |  |  | currentSnapshot_->wrapVector(DA); | 
| 176 |  |  | DAdist = DA.length(); | 
| 177 | gezelter | 2049 |  | 
| 178 | gezelter | 2052 | // Distance criteria: are the donor and acceptor atoms | 
| 179 |  |  | // close enough? | 
| 180 |  |  | if (DAdist < rCut_) { | 
| 181 |  |  |  | 
| 182 |  |  | ctheta = dot(DH, DA) / (DHdist * DAdist); | 
| 183 |  |  | theta = acos(ctheta) * 180.0 / M_PI; | 
| 184 |  |  |  | 
| 185 |  |  | // Angle criteria: are the D-H and D-A and vectors close? | 
| 186 |  |  | if (theta < thetaCut_) { | 
| 187 |  |  | // molecule 1 is a Hbond donor: | 
| 188 |  |  | nHB++; | 
| 189 |  |  | nD++; | 
| 190 | gezelter | 2049 | } | 
| 191 | gezelter | 2052 | } | 
| 192 |  |  | } | 
| 193 |  |  | } | 
| 194 |  |  |  | 
| 195 |  |  | // now loop over the possible acceptors in molecule 1: | 
| 196 |  |  | for (hba1 = mol1->beginHBondAcceptor(hbai); hba1 != NULL; | 
| 197 |  |  | hba1 = mol1->nextHBondAcceptor(hbai)) { | 
| 198 |  |  | aPos = hba1->getPos(); | 
| 199 | gezelter | 2049 |  | 
| 200 | gezelter | 2052 | // loop over the possible donors in molecule 2: | 
| 201 |  |  | for (hbd2 = mol2->beginHBondDonor(hbdj); hbd2 != NULL; | 
| 202 |  |  | hbd2 = mol2->nextHBondDonor(hbdj)) { | 
| 203 |  |  | dPos = hbd2->donorAtom->getPos(); | 
| 204 | gezelter | 2049 |  | 
| 205 | gezelter | 2052 | DA = aPos - dPos; | 
| 206 |  |  | currentSnapshot_->wrapVector(DA); | 
| 207 |  |  | DAdist = DA.length(); | 
| 208 |  |  |  | 
| 209 |  |  | // Distance criteria: are the donor and acceptor atoms | 
| 210 |  |  | // close enough? | 
| 211 |  |  | if (DAdist < rCut_) { | 
| 212 |  |  | hPos = hbd2->donatedHydrogen->getPos(); | 
| 213 |  |  | DH = hPos - dPos; | 
| 214 |  |  | currentSnapshot_->wrapVector(DH); | 
| 215 |  |  | DHdist = DH.length(); | 
| 216 |  |  | ctheta = dot(DH, DA) / (DHdist * DAdist); | 
| 217 |  |  | theta = acos(ctheta) * 180.0 / M_PI; | 
| 218 |  |  | // Angle criteria: are the D-H and D-A and vectors close? | 
| 219 |  |  | if (theta < thetaCut_) { | 
| 220 |  |  | // molecule 1 is a Hbond acceptor: | 
| 221 |  |  | nHB++; | 
| 222 |  |  | nA++; | 
| 223 |  |  | } | 
| 224 | gezelter | 2049 | } | 
| 225 | gezelter | 2052 | } | 
| 226 | gezelter | 2049 | } | 
| 227 | gezelter | 2052 | } | 
| 228 | gezelter | 2049 | collectHistogram(nHB, nA, nD); | 
| 229 |  |  | } | 
| 230 |  |  | } | 
| 231 |  |  | writeHistogram(); | 
| 232 |  |  | } | 
| 233 |  |  |  | 
| 234 |  |  |  | 
| 235 |  |  | void HBondGeometric::collectHistogram(int nHB, int nA, int nD) { | 
| 236 |  |  | nHBonds_[nHB] += 1; | 
| 237 |  |  | nAcceptor_[nA] += 1; | 
| 238 |  |  | nDonor_[nD] += 1; | 
| 239 |  |  | nSelected_++; | 
| 240 |  |  | } | 
| 241 |  |  |  | 
| 242 |  |  |  | 
| 243 |  |  | void HBondGeometric::writeHistogram() { | 
| 244 |  |  |  | 
| 245 |  |  | std::ofstream osq(getOutputFileName().c_str()); | 
| 246 |  |  |  | 
| 247 |  |  | if (osq.is_open()) { | 
| 248 |  |  |  | 
| 249 |  |  | osq << "# HydrogenBonding Statistics\n"; | 
| 250 |  |  | osq << "# selection1: (" << selectionScript1_ << ")" | 
| 251 |  |  | << "\tselection2: (" << selectionScript2_ <<  ")\n"; | 
| 252 | gezelter | 2052 | osq << "# molecules in selection1: " << nSelected_ << "\n"; | 
| 253 |  |  | osq << "# nHBonds\tnAcceptor\tnDonor\tp(nHBonds)\tp(nAcceptor)\tp(nDonor)\n"; | 
| 254 | gezelter | 2049 | // Normalize by number of frames and write it out: | 
| 255 |  |  | for (int i = 0; i < nBins_; ++i) { | 
| 256 |  |  | osq << i; | 
| 257 | gezelter | 2052 | osq << "\t" << nHBonds_[i]; | 
| 258 |  |  | osq << "\t" << nAcceptor_[i]; | 
| 259 |  |  | osq << "\t" << nDonor_[i]; | 
| 260 | gezelter | 2049 | osq << "\t" << (RealType) (nHBonds_[i]) / nSelected_; | 
| 261 |  |  | osq << "\t" << (RealType) (nAcceptor_[i]) / nSelected_; | 
| 262 |  |  | osq << "\t" << (RealType) (nDonor_[i]) / nSelected_; | 
| 263 |  |  | osq << "\n"; | 
| 264 |  |  | } | 
| 265 |  |  | osq.close(); | 
| 266 |  |  |  | 
| 267 |  |  | } else { | 
| 268 |  |  | sprintf(painCave.errMsg, "HBondGeometric: unable to open %s\n", | 
| 269 |  |  | (getOutputFileName() + "q").c_str()); | 
| 270 |  |  | painCave.isFatal = 1; | 
| 271 |  |  | simError(); | 
| 272 |  |  | } | 
| 273 |  |  | } | 
| 274 |  |  | } | 
| 275 |  |  |  | 
| 276 |  |  |  | 
| 277 |  |  |  | 
| 278 |  |  |  |