| 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. 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 | 
    selectionScript1_(sele1), seleMan1_(info), evaluator1_(info), | 
| 60 | 
    selectionScript2_(sele2), seleMan2_(info), evaluator2_(info) { | 
| 61 | 
     | 
| 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 | 
 | 
| 85 | 
    initializeHistogram(); | 
| 86 | 
  } | 
| 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 | 
    Molecule* mol1; | 
| 103 | 
    Molecule* mol2; | 
| 104 | 
    RigidBody* rb1; | 
| 105 | 
    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 | 
    SimInfo::MoleculeIterator mi; | 
| 114 | 
    Molecule::RigidBodyIterator rbIter; | 
| 115 | 
    Molecule::IntegrableObjectIterator ioi; | 
| 116 | 
    Vector3d dPos; | 
| 117 | 
    Vector3d aPos; | 
| 118 | 
    Vector3d hPos; | 
| 119 | 
    Vector3d DH; | 
| 120 | 
    Vector3d DA; | 
| 121 | 
    RealType DAdist, DHdist, theta, ctheta; | 
| 122 | 
    int ii, jj; | 
| 123 | 
    int nHB, nA, nD; | 
| 124 | 
 | 
| 125 | 
    DumpReader reader(info_, dumpFilename_);     | 
| 126 | 
    int nFrames = reader.getNFrames(); | 
| 127 | 
    frameCounter_ = 0; | 
| 128 | 
 | 
| 129 | 
    for (int istep = 0; istep < nFrames; istep += step_) { | 
| 130 | 
      reader.readFrame(istep); | 
| 131 | 
      frameCounter_++; | 
| 132 | 
      currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 133 | 
      | 
| 134 | 
      // update the positions of atoms which belong to the rigidbodies | 
| 135 | 
       | 
| 136 | 
      for (mol1 = info_->beginMolecule(mi); mol1 != NULL;  | 
| 137 | 
           mol1 = info_->nextMolecule(mi)) { | 
| 138 | 
        for (rb1 = mol1->beginRigidBody(rbIter); rb1 != NULL;  | 
| 139 | 
             rb1 = mol1->nextRigidBody(rbIter)) { | 
| 140 | 
          rb1->updateAtoms(); | 
| 141 | 
        }         | 
| 142 | 
      }            | 
| 143 | 
       | 
| 144 | 
      if  (evaluator1_.isDynamic()) { | 
| 145 | 
        seleMan1_.setSelectionSet(evaluator1_.evaluate()); | 
| 146 | 
      } | 
| 147 | 
      if  (evaluator2_.isDynamic()) { | 
| 148 | 
        seleMan2_.setSelectionSet(evaluator2_.evaluate()); | 
| 149 | 
      } | 
| 150 | 
       | 
| 151 | 
      for (mol1 = seleMan1_.beginSelectedMolecule(ii); | 
| 152 | 
           mol1 != NULL; mol1 = seleMan1_.nextSelectedMolecule(ii)) { | 
| 153 | 
 | 
| 154 | 
        // We're collecting statistics on the molecules in selection 1: | 
| 155 | 
        nHB = 0; | 
| 156 | 
        nA = 0; | 
| 157 | 
        nD = 0; | 
| 158 | 
         | 
| 159 | 
        for (mol2 = seleMan2_.beginSelectedMolecule(jj); | 
| 160 | 
             mol2 != NULL; mol2 = seleMan2_.nextSelectedMolecule(jj)) { | 
| 161 | 
           | 
| 162 | 
          // loop over the possible donors in molecule 1: | 
| 163 | 
          for (hbd1 = mol1->beginHBondDonor(hbdi); hbd1 != NULL; | 
| 164 | 
               hbd1 = mol1->nextHBondDonor(hbdi)) { | 
| 165 | 
            dPos = hbd1->donorAtom->getPos(); | 
| 166 | 
            hPos = hbd1->donatedHydrogen->getPos(); | 
| 167 | 
            DH = hPos - dPos;  | 
| 168 | 
            currentSnapshot_->wrapVector(DH); | 
| 169 | 
            DHdist = DH.length(); | 
| 170 | 
 | 
| 171 | 
            // loop over the possible acceptors in molecule 2: | 
| 172 | 
            for (hba2 = mol2->beginHBondAcceptor(hbaj); hba2 != NULL; | 
| 173 | 
                 hba2 = mol2->nextHBondAcceptor(hbaj)) { | 
| 174 | 
              aPos = hba2->getPos(); | 
| 175 | 
              DA = aPos - dPos;               | 
| 176 | 
              currentSnapshot_->wrapVector(DA); | 
| 177 | 
              DAdist = DA.length(); | 
| 178 | 
 | 
| 179 | 
              // Distance criteria: are the donor and acceptor atoms | 
| 180 | 
              // close enough? | 
| 181 | 
              if (DAdist < rCut_) { | 
| 182 | 
 | 
| 183 | 
                ctheta = dot(DH, DA) / (DHdist * DAdist); | 
| 184 | 
                theta = acos(ctheta) * 180.0 / M_PI; | 
| 185 | 
 | 
| 186 | 
                // Angle criteria: are the D-H and D-A and vectors close? | 
| 187 | 
                if (theta < thetaCut_) { | 
| 188 | 
                  // molecule 1 is a Hbond donor: | 
| 189 | 
                  nHB++; | 
| 190 | 
                  nD++; | 
| 191 | 
                } | 
| 192 | 
              }             | 
| 193 | 
            }             | 
| 194 | 
          } | 
| 195 | 
 | 
| 196 | 
          // now loop over the possible acceptors in molecule 1: | 
| 197 | 
          for (hba1 = mol1->beginHBondAcceptor(hbai); hba1 != NULL; | 
| 198 | 
               hba1 = mol1->nextHBondAcceptor(hbai)) { | 
| 199 | 
            aPos = hba1->getPos(); | 
| 200 | 
             | 
| 201 | 
            // loop over the possible donors in molecule 2: | 
| 202 | 
            for (hbd2 = mol2->beginHBondDonor(hbdj); hbd2 != NULL; | 
| 203 | 
               hbd2 = mol2->nextHBondDonor(hbdj)) { | 
| 204 | 
              dPos = hbd2->donorAtom->getPos(); | 
| 205 | 
 | 
| 206 | 
              DA = aPos - dPos; | 
| 207 | 
              currentSnapshot_->wrapVector(DA); | 
| 208 | 
              DAdist = DA.length(); | 
| 209 | 
               | 
| 210 | 
              // Distance criteria: are the donor and acceptor atoms | 
| 211 | 
              // close enough? | 
| 212 | 
              if (DAdist < rCut_) { | 
| 213 | 
                hPos = hbd2->donatedHydrogen->getPos(); | 
| 214 | 
                DH = hPos - dPos;  | 
| 215 | 
                currentSnapshot_->wrapVector(DH); | 
| 216 | 
                DHdist = DH.length(); | 
| 217 | 
                ctheta = dot(DH, DA) / (DHdist * DAdist); | 
| 218 | 
                theta = acos(ctheta) * 180.0 / M_PI; | 
| 219 | 
                // Angle criteria: are the D-H and D-A and vectors close? | 
| 220 | 
                if (theta < thetaCut_) { | 
| 221 | 
                  // molecule 1 is a Hbond acceptor: | 
| 222 | 
                  nHB++; | 
| 223 | 
                  nA++; | 
| 224 | 
                }                 | 
| 225 | 
              } | 
| 226 | 
            } | 
| 227 | 
          } | 
| 228 | 
        }                  | 
| 229 | 
        collectHistogram(nHB, nA, nD); | 
| 230 | 
      } | 
| 231 | 
    } | 
| 232 | 
    writeHistogram(); | 
| 233 | 
  } | 
| 234 | 
  | 
| 235 | 
         | 
| 236 | 
  void HBondGeometric::collectHistogram(int nHB, int nA, int nD) { | 
| 237 | 
    nHBonds_[nHB] += 1; | 
| 238 | 
    nAcceptor_[nA] += 1; | 
| 239 | 
    nDonor_[nD] += 1; | 
| 240 | 
    nSelected_++; | 
| 241 | 
  } | 
| 242 | 
 | 
| 243 | 
 | 
| 244 | 
  void HBondGeometric::writeHistogram() { | 
| 245 | 
         | 
| 246 | 
    std::ofstream osq(getOutputFileName().c_str()); | 
| 247 | 
 | 
| 248 | 
    if (osq.is_open()) { | 
| 249 | 
       | 
| 250 | 
      osq << "# HydrogenBonding Statistics\n"; | 
| 251 | 
      osq << "# selection1: (" << selectionScript1_ << ")" | 
| 252 | 
          << "\tselection2: (" << selectionScript2_ <<  ")\n"; | 
| 253 | 
      osq << "# molecules in selection1: " << nSelected_ << "\n"; | 
| 254 | 
      osq << "# nHBonds\tnAcceptor\tnDonor\tp(nHBonds)\tp(nAcceptor)\tp(nDonor)\n"; | 
| 255 | 
      // Normalize by number of frames and write it out: | 
| 256 | 
      for (int i = 0; i < nBins_; ++i) { | 
| 257 | 
        osq << i; | 
| 258 | 
        osq << "\t" << nHBonds_[i]; | 
| 259 | 
        osq << "\t" << nAcceptor_[i]; | 
| 260 | 
        osq << "\t" << nDonor_[i]; | 
| 261 | 
        osq << "\t" << (RealType) (nHBonds_[i]) / nSelected_; | 
| 262 | 
        osq << "\t" << (RealType) (nAcceptor_[i]) / nSelected_; | 
| 263 | 
        osq << "\t" << (RealType) (nDonor_[i]) / nSelected_; | 
| 264 | 
        osq << "\n"; | 
| 265 | 
      } | 
| 266 | 
      osq.close(); | 
| 267 | 
       | 
| 268 | 
    } else { | 
| 269 | 
      sprintf(painCave.errMsg, "HBondGeometric: unable to open %s\n",  | 
| 270 | 
              (getOutputFileName() + "q").c_str()); | 
| 271 | 
      painCave.isFatal = 1; | 
| 272 | 
      simError();   | 
| 273 | 
    } | 
| 274 | 
  } | 
| 275 | 
} | 
| 276 | 
 | 
| 277 | 
       | 
| 278 | 
 | 
| 279 | 
 |