| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * analysis_bonds.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Nov 7, 2009
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "analysis_bonds.hpp"
 | 
|---|
| 23 | #include "atom.hpp"
 | 
|---|
| 24 | #include "bond.hpp"
 | 
|---|
| 25 | #include "element.hpp"
 | 
|---|
| 26 | #include "Helpers/Info.hpp"
 | 
|---|
| 27 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 28 | #include "Helpers/Log.hpp"
 | 
|---|
| 29 | #include "molecule.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | /** Calculates the min, mean and maximum bond counts for the given molecule.
 | 
|---|
| 32 |  * \param *mol molecule with atoms and atom::ListOfBonds
 | 
|---|
| 33 |  * \param &Min minimum count on return
 | 
|---|
| 34 |  * \param &Mean mean count on return
 | 
|---|
| 35 |  * \param &Max maximum count on return
 | 
|---|
| 36 |  */
 | 
|---|
| 37 | void GetMaxMinMeanBondCount(const molecule * const mol, double &Min, double &Mean, double &Max)
 | 
|---|
| 38 | {
 | 
|---|
| 39 |   Min = 2e+6;
 | 
|---|
| 40 |   Max = -2e+5;
 | 
|---|
| 41 |   Mean = 0.;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   int AtomCount = 0;
 | 
|---|
| 44 |   for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
 | 
|---|
| 45 |     const int count = (*iter)->ListOfBonds.size();
 | 
|---|
| 46 |     if (Max < count)
 | 
|---|
| 47 |       Max = count;
 | 
|---|
| 48 |     if (Min > count)
 | 
|---|
| 49 |       Min = count;
 | 
|---|
| 50 |     Mean += count;
 | 
|---|
| 51 |     AtomCount++;
 | 
|---|
| 52 |   }
 | 
|---|
| 53 |   if (((int)Mean % 2) != 0)
 | 
|---|
| 54 |     DoeLog(1) && (eLog()<< Verbose(1) << "Something is wrong with the bond structure, the number of bonds is not even!" << endl);
 | 
|---|
| 55 |   Mean /= (double)AtomCount;
 | 
|---|
| 56 | };
 | 
|---|
| 57 | 
 | 
|---|
| 58 | /** Calculates the min and max bond distance of all atoms of two given elements.
 | 
|---|
| 59 |  * \param *mol molecule with atoms
 | 
|---|
| 60 |  * \param *type1 one element
 | 
|---|
| 61 |  * \param *type2 other element
 | 
|---|
| 62 |  * \param &Min minimum distance on return, 0 if no bond between the two elements
 | 
|---|
| 63 |  * \param &Mean mean distance (i.e. sum of distance for matching element pairs, divided by number) on return, 0 if no bond between the two elements
 | 
|---|
| 64 |  * \param &Max maximum distance on return, 0 if no bond between the two elements
 | 
|---|
| 65 |  */
 | 
|---|
| 66 | void MinMeanMaxBondDistanceBetweenElements(const molecule *mol, const element *type1, const element *type2, double &Min, double &Mean, double &Max)
 | 
|---|
| 67 | {
 | 
|---|
| 68 |   Min = 2e+6;
 | 
|---|
| 69 |   Mean = 0.;
 | 
|---|
| 70 |   Max = -2e+6;
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   int AtomNo = 0;
 | 
|---|
| 73 |   for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
 | 
|---|
| 74 |     if ((*iter)->getType() == type1)
 | 
|---|
| 75 |       for (BondList::const_iterator BondRunner = (*iter)->ListOfBonds.begin(); BondRunner != (*iter)->ListOfBonds.end(); BondRunner++)
 | 
|---|
| 76 |         if ((*BondRunner)->GetOtherAtom((*iter))->getType() == type2) {
 | 
|---|
| 77 |           const double distance = (*BondRunner)->GetDistanceSquared();
 | 
|---|
| 78 |           if (Min > distance)
 | 
|---|
| 79 |             Min = distance;
 | 
|---|
| 80 |           if (Max < distance)
 | 
|---|
| 81 |             Max = distance;
 | 
|---|
| 82 |           Mean += sqrt(distance);
 | 
|---|
| 83 |           AtomNo++;
 | 
|---|
| 84 |         }
 | 
|---|
| 85 |   }
 | 
|---|
| 86 |   if (Max < 0) {
 | 
|---|
| 87 |     Max = Min = 0.;
 | 
|---|
| 88 |   } else {
 | 
|---|
| 89 |     Max = sqrt(Max);
 | 
|---|
| 90 |     Min = sqrt(Min);
 | 
|---|
| 91 |     Mean = Mean/(double)AtomNo;
 | 
|---|
| 92 |   }
 | 
|---|
| 93 | };
 | 
|---|
| 94 | 
 | 
|---|
| 95 | /** Calculate the angle between \a *first and \a *origin and \a *second and \a *origin.
 | 
|---|
| 96 |  * \param *first first Vector
 | 
|---|
| 97 |  * \param *origin origin of angle taking
 | 
|---|
| 98 |  * \param *second second Vector
 | 
|---|
| 99 |  * \return angle between \a *first and \a *second, both relative to origin at \a *origin.
 | 
|---|
| 100 |  */
 | 
|---|
| 101 | double CalculateAngle(const Vector &first, const Vector ¢ral, const Vector &second)
 | 
|---|
| 102 | {
 | 
|---|
| 103 |   Vector OHBond;
 | 
|---|
| 104 |   Vector OOBond;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   OHBond = first - central;
 | 
|---|
| 107 |   OOBond = second - central;
 | 
|---|
| 108 |   const double angle = OHBond.Angle(OOBond);
 | 
|---|
| 109 |   return angle;
 | 
|---|
| 110 | };
 | 
|---|
| 111 | 
 | 
|---|
| 112 | /** Checks whether the angle between \a *Oxygen and \a *Hydrogen and \a *Oxygen and \a *OtherOxygen is less than 30 degrees.
 | 
|---|
| 113 |  * Note that distance criterion is not checked.
 | 
|---|
| 114 |  * \param *Oxygen first oxygen atom, bonded to \a *Hydrogen
 | 
|---|
| 115 |  * \param *Hydrogen hydrogen bonded to \a *Oxygen
 | 
|---|
| 116 |  * \param *OtherOxygen other oxygen atom
 | 
|---|
| 117 |  * \return true - angle criteria fulfilled, false - criteria not fulfilled, angle greater than 30 degrees.
 | 
|---|
| 118 |  */
 | 
|---|
| 119 | bool CheckHydrogenBridgeBondAngle(atom *Oxygen, atom *Hydrogen, atom *OtherOxygen)
 | 
|---|
| 120 | {
 | 
|---|
| 121 |   Info FunctionInfo(__func__);
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   // check angle
 | 
|---|
| 124 |   if (CalculateAngle(Hydrogen->getPosition(), Oxygen->getPosition(), OtherOxygen->getPosition()) < M_PI*(30./180.)) {
 | 
|---|
| 125 |     return true;
 | 
|---|
| 126 |   } else {
 | 
|---|
| 127 |     return false;
 | 
|---|
| 128 |   }
 | 
|---|
| 129 | };
 | 
|---|
| 130 | 
 | 
|---|
| 131 | /** Counts the number of hydrogen bridge bonds.
 | 
|---|
| 132 |  * With \a *InterfaceElement an extra element can be specified that identifies some boundary.
 | 
|---|
| 133 |  * Then, counting is for the h-bridges that connect to interface only.
 | 
|---|
| 134 |  * \param *molecules molecules to count bonds
 | 
|---|
| 135 |  * \param *InterfaceElement or NULL
 | 
|---|
| 136 |  * \param *Interface2Element or NULL
 | 
|---|
| 137 |  */
 | 
|---|
| 138 | int CountHydrogenBridgeBonds(MoleculeListClass *molecules, const element * InterfaceElement = NULL, const element * Interface2Element = NULL)
 | 
|---|
| 139 | {
 | 
|---|
| 140 |   int count = 0;
 | 
|---|
| 141 |   int OtherHydrogens = 0;
 | 
|---|
| 142 |   double Otherangle = 0.;
 | 
|---|
| 143 |   bool InterfaceFlag = false;
 | 
|---|
| 144 |   bool Interface2Flag = false;
 | 
|---|
| 145 |   bool OtherHydrogenFlag = true;
 | 
|---|
| 146 |   for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin();MolWalker != molecules->ListOfMolecules.end(); ++MolWalker) {
 | 
|---|
| 147 |     molecule::iterator Walker = (*MolWalker)->begin();
 | 
|---|
| 148 |     for(;Walker!=(*MolWalker)->end();++Walker){
 | 
|---|
| 149 |       for (MoleculeList::const_iterator MolRunner = molecules->ListOfMolecules.begin();MolRunner != molecules->ListOfMolecules.end(); ++MolRunner) {
 | 
|---|
| 150 |         molecule::iterator Runner = (*MolRunner)->begin();
 | 
|---|
| 151 |         for(;Runner!=(*MolRunner)->end();++Runner){
 | 
|---|
| 152 |           if (((*Walker)->getType()->getAtomicNumber()  == 8) && ((*Runner)->getType()->getAtomicNumber()  == 8)) {
 | 
|---|
| 153 |             // check distance
 | 
|---|
| 154 |             const double distance = (*Runner)->DistanceSquared(*(*Walker));
 | 
|---|
| 155 |             if ((distance > MYEPSILON) && (distance < HBRIDGEDISTANCE*HBRIDGEDISTANCE)) { // distance >0 means  different atoms
 | 
|---|
| 156 |               // on other atom(Runner) we check for bond to interface element and
 | 
|---|
| 157 |               // check that O-O line is not in between the shanks of the two connected hydrogens (Otherangle > 104.5)
 | 
|---|
| 158 |               OtherHydrogenFlag = true;
 | 
|---|
| 159 |               Otherangle = 0.;
 | 
|---|
| 160 |               OtherHydrogens = 0;
 | 
|---|
| 161 |               InterfaceFlag = (InterfaceElement == NULL);
 | 
|---|
| 162 |               Interface2Flag = (Interface2Element == NULL);
 | 
|---|
| 163 |               for (BondList::const_iterator BondRunner = (*Runner)->ListOfBonds.begin(); BondRunner != (*Runner)->ListOfBonds.end(); BondRunner++) {
 | 
|---|
| 164 |                 atom * const OtherAtom = (*BondRunner)->GetOtherAtom(*Runner);
 | 
|---|
| 165 |                 // if hydrogen, check angle to be greater(!) than 30 degrees
 | 
|---|
| 166 |                 if (OtherAtom->getType()->getAtomicNumber() == 1) {
 | 
|---|
| 167 |                   const double angle = CalculateAngle(OtherAtom->getPosition(), (*Runner)->getPosition(), (*Walker)->getPosition());
 | 
|---|
| 168 |                   OtherHydrogenFlag = OtherHydrogenFlag && (angle > M_PI*(30./180.) + MYEPSILON);
 | 
|---|
| 169 |                   Otherangle += angle;
 | 
|---|
| 170 |                   OtherHydrogens++;
 | 
|---|
| 171 |                 }
 | 
|---|
| 172 |                 InterfaceFlag = InterfaceFlag || (OtherAtom->getType() == InterfaceElement);
 | 
|---|
| 173 |                 Interface2Flag = Interface2Flag || (OtherAtom->getType() == Interface2Element);
 | 
|---|
| 174 |               }
 | 
|---|
| 175 |               DoLog(1) && (Log() << Verbose(1) << "Otherangle is " << Otherangle << " for " << OtherHydrogens << " hydrogens." << endl);
 | 
|---|
| 176 |               switch (OtherHydrogens) {
 | 
|---|
| 177 |                 case 0:
 | 
|---|
| 178 |                 case 1:
 | 
|---|
| 179 |                   break;
 | 
|---|
| 180 |                 case 2:
 | 
|---|
| 181 |                   OtherHydrogenFlag = OtherHydrogenFlag && (Otherangle > M_PI*(104.5/180.) + MYEPSILON);
 | 
|---|
| 182 |                   break;
 | 
|---|
| 183 |                 default: // 3 or more hydrogens ...
 | 
|---|
| 184 |                   OtherHydrogenFlag = false;
 | 
|---|
| 185 |                   break;
 | 
|---|
| 186 |               }
 | 
|---|
| 187 |               if (InterfaceFlag && Interface2Flag && OtherHydrogenFlag) {
 | 
|---|
| 188 |                 // on this element (Walker) we check for bond to hydrogen, i.e. part of water molecule
 | 
|---|
| 189 |                 for (BondList::const_iterator BondRunner = (*Walker)->ListOfBonds.begin(); BondRunner != (*Walker)->ListOfBonds.end(); BondRunner++) {
 | 
|---|
| 190 |                   atom * const OtherAtom = (*BondRunner)->GetOtherAtom(*Walker);
 | 
|---|
| 191 |                   if (OtherAtom->getType()->getAtomicNumber() == 1) {
 | 
|---|
| 192 |                     // check angle
 | 
|---|
| 193 |                     if (CheckHydrogenBridgeBondAngle(*Walker, OtherAtom, *Runner)) {
 | 
|---|
| 194 |                       DoLog(1) && (Log() << Verbose(1) << (*Walker)->getName() << ", " << OtherAtom->getName() << " and " << (*Runner)->getName() << " has a hydrogen bridge bond with distance " << sqrt(distance) << " and angle " << CalculateAngle(OtherAtom->getPosition(), (*Walker)->getPosition(), (*Runner)->getPosition())*(180./M_PI) << "." << endl);
 | 
|---|
| 195 |                       count++;
 | 
|---|
| 196 |                       break;
 | 
|---|
| 197 |                     }
 | 
|---|
| 198 |                   }
 | 
|---|
| 199 |                 }
 | 
|---|
| 200 |               }
 | 
|---|
| 201 |             }
 | 
|---|
| 202 |           }
 | 
|---|
| 203 |         }
 | 
|---|
| 204 |       }
 | 
|---|
| 205 |     }
 | 
|---|
| 206 |   }
 | 
|---|
| 207 |   return count;
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|
| 210 | /** Counts the number of bonds between two given elements.
 | 
|---|
| 211 |  * \param *molecules list of molecules with all atoms
 | 
|---|
| 212 |  * \param *first pointer to first element
 | 
|---|
| 213 |  * \param *second pointer to second element
 | 
|---|
| 214 |  * \return number of found bonds (\a *first-\a *second)
 | 
|---|
| 215 |  */
 | 
|---|
| 216 | int CountBondsOfTwo(MoleculeListClass * const molecules, const element * const first, const element * const second)
 | 
|---|
| 217 | {
 | 
|---|
| 218 |   int count = 0;
 | 
|---|
| 219 | 
 | 
|---|
| 220 |   for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin();MolWalker != molecules->ListOfMolecules.end(); MolWalker++) {
 | 
|---|
| 221 |     molecule::iterator Walker = (*MolWalker)->begin();
 | 
|---|
| 222 |     for(;Walker!=(*MolWalker)->end();++Walker){
 | 
|---|
| 223 |       atom * theAtom = *Walker;
 | 
|---|
| 224 |       if ((theAtom->getType() == first) || (theAtom->getType() == second)) {  // first element matches
 | 
|---|
| 225 |         for (BondList::const_iterator BondRunner = theAtom->ListOfBonds.begin(); BondRunner != theAtom->ListOfBonds.end(); BondRunner++) {
 | 
|---|
| 226 |           atom * const OtherAtom = (*BondRunner)->GetOtherAtom(theAtom);
 | 
|---|
| 227 |           if (((OtherAtom->getType() == first) || (OtherAtom->getType() == second)) && (theAtom->nr < OtherAtom->nr)) {
 | 
|---|
| 228 |             count++;
 | 
|---|
| 229 |             DoLog(1) && (Log() << Verbose(1) << *first << "-" << *second << " bond found between " << *Walker << " and " << *OtherAtom << "." << endl);
 | 
|---|
| 230 |           }
 | 
|---|
| 231 |         }
 | 
|---|
| 232 |       }
 | 
|---|
| 233 |     }
 | 
|---|
| 234 |   }
 | 
|---|
| 235 |   return count;
 | 
|---|
| 236 | };
 | 
|---|
| 237 | 
 | 
|---|
| 238 | /** Counts the number of bonds between three given elements.
 | 
|---|
| 239 |  * Note that we do not look for arbitrary sequence of given bonds, but \a *second will be the central atom and we check
 | 
|---|
| 240 |  * whether it has bonds to both \a *first and \a *third.
 | 
|---|
| 241 |  * \param *molecules list of molecules with all atoms
 | 
|---|
| 242 |  * \param *first pointer to first element
 | 
|---|
| 243 |  * \param *second pointer to second element
 | 
|---|
| 244 |  * \param *third pointer to third element
 | 
|---|
| 245 |  * \return number of found bonds (\a *first-\a *second-\a *third, \a *third-\a *second-\a *first, respectively)
 | 
|---|
| 246 |  */
 | 
|---|
| 247 | int CountBondsOfThree(MoleculeListClass * const molecules, const element * const first, const element * const second, const element * const third)
 | 
|---|
| 248 | {
 | 
|---|
| 249 |   int count = 0;
 | 
|---|
| 250 |   bool MatchFlag[2];
 | 
|---|
| 251 |   bool result = false;
 | 
|---|
| 252 |   const element * ElementArray[2];
 | 
|---|
| 253 |   ElementArray[0] = first;
 | 
|---|
| 254 |   ElementArray[1] = third;
 | 
|---|
| 255 | 
 | 
|---|
| 256 |   for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin();MolWalker != molecules->ListOfMolecules.end(); MolWalker++) {
 | 
|---|
| 257 |     molecule::iterator Walker = (*MolWalker)->begin();
 | 
|---|
| 258 |     for(;Walker!=(*MolWalker)->end();++Walker){
 | 
|---|
| 259 |       atom *theAtom = *Walker;
 | 
|---|
| 260 |       if (theAtom->getType() == second) {  // first element matches
 | 
|---|
| 261 |         for (int i=0;i<2;i++)
 | 
|---|
| 262 |           MatchFlag[i] = false;
 | 
|---|
| 263 |         for (BondList::const_iterator BondRunner = theAtom->ListOfBonds.begin(); BondRunner != theAtom->ListOfBonds.end(); BondRunner++) {
 | 
|---|
| 264 |           atom * const OtherAtom = (*BondRunner)->GetOtherAtom(theAtom);
 | 
|---|
| 265 |           for (int i=0;i<2;i++)
 | 
|---|
| 266 |             if ((!MatchFlag[i]) && (OtherAtom->getType() == ElementArray[i])) {
 | 
|---|
| 267 |               MatchFlag[i] = true;
 | 
|---|
| 268 |               break;  // each bonding atom can match at most one element we are looking for
 | 
|---|
| 269 |             }
 | 
|---|
| 270 |         }
 | 
|---|
| 271 |         result = true;
 | 
|---|
| 272 |         for (int i=0;i<2;i++) // gather results
 | 
|---|
| 273 |           result = result && MatchFlag[i];
 | 
|---|
| 274 |         if (result) { // check results
 | 
|---|
| 275 |           count++;
 | 
|---|
| 276 |           DoLog(1) && (Log() << Verbose(1) << *first << "-" << *second << "-" << *third << " bond found at " << *Walker << "." << endl);
 | 
|---|
| 277 |         }
 | 
|---|
| 278 |       }
 | 
|---|
| 279 |     }
 | 
|---|
| 280 |   }
 | 
|---|
| 281 |   return count;
 | 
|---|
| 282 | };
 | 
|---|