| [bcf653] | 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 | 
 | 
|---|
| [96c961] | 8 | /*
 | 
|---|
 | 9 |  * analysis_bonds.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Nov 7, 2009
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [112b09] | 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
| [220cf37] | 22 | #include "analysis_bonds.hpp"
 | 
|---|
 | 23 | #include "atom.hpp"
 | 
|---|
 | 24 | #include "bond.hpp"
 | 
|---|
| [388049] | 25 | #include "element.hpp"
 | 
|---|
| [952f38] | 26 | #include "Helpers/Info.hpp"
 | 
|---|
 | 27 | #include "Helpers/Verbose.hpp"
 | 
|---|
 | 28 | #include "Helpers/Log.hpp"
 | 
|---|
| [220cf37] | 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;
 | 
|---|
| [9879f6] | 44 |   for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
 | 
|---|
 | 45 |     const int count = (*iter)->ListOfBonds.size();
 | 
|---|
| [220cf37] | 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)
 | 
|---|
| [58ed4a] | 54 |     DoeLog(1) && (eLog()<< Verbose(1) << "Something is wrong with the bond structure, the number of bonds is not even!" << endl);
 | 
|---|
| [220cf37] | 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 |  */
 | 
|---|
| [4eb4fe] | 66 | void MinMeanMaxBondDistanceBetweenElements(const molecule *mol, const element *type1, const element *type2, double &Min, double &Mean, double &Max)
 | 
|---|
| [220cf37] | 67 | {
 | 
|---|
 | 68 |   Min = 2e+6;
 | 
|---|
 | 69 |   Mean = 0.;
 | 
|---|
 | 70 |   Max = -2e+6;
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 |   int AtomNo = 0;
 | 
|---|
| [9879f6] | 73 |   for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
 | 
|---|
| [d74077] | 74 |     if ((*iter)->getType() == type1)
 | 
|---|
| [9879f6] | 75 |       for (BondList::const_iterator BondRunner = (*iter)->ListOfBonds.begin(); BondRunner != (*iter)->ListOfBonds.end(); BondRunner++)
 | 
|---|
| [d74077] | 76 |         if ((*BondRunner)->GetOtherAtom((*iter))->getType() == type2) {
 | 
|---|
| [220cf37] | 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 | };
 | 
|---|
| [388049] | 94 | 
 | 
|---|
| [fe238c] | 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 |  */
 | 
|---|
| [d74077] | 101 | double CalculateAngle(const Vector &first, const Vector ¢ral, const Vector &second)
 | 
|---|
| [fe238c] | 102 | {
 | 
|---|
 | 103 |   Vector OHBond;
 | 
|---|
 | 104 |   Vector OOBond;
 | 
|---|
 | 105 | 
 | 
|---|
| [d74077] | 106 |   OHBond = first - central;
 | 
|---|
 | 107 |   OOBond = second - central;
 | 
|---|
| [8cbb97] | 108 |   const double angle = OHBond.Angle(OOBond);
 | 
|---|
| [fe238c] | 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
 | 
|---|
| [d74077] | 124 |   if (CalculateAngle(Hydrogen->getPosition(), Oxygen->getPosition(), OtherOxygen->getPosition()) < M_PI*(30./180.)) {
 | 
|---|
| [fe238c] | 125 |     return true;
 | 
|---|
 | 126 |   } else {
 | 
|---|
 | 127 |     return false;
 | 
|---|
 | 128 |   }
 | 
|---|
 | 129 | };
 | 
|---|
| [388049] | 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
 | 
|---|
| [bfd839] | 136 |  * \param *Interface2Element or NULL
 | 
|---|
| [388049] | 137 |  */
 | 
|---|
| [bfd839] | 138 | int CountHydrogenBridgeBonds(MoleculeListClass *molecules, const element * InterfaceElement = NULL, const element * Interface2Element = NULL)
 | 
|---|
| [388049] | 139 | {
 | 
|---|
 | 140 |   int count = 0;
 | 
|---|
| [fe238c] | 141 |   int OtherHydrogens = 0;
 | 
|---|
 | 142 |   double Otherangle = 0.;
 | 
|---|
| [388049] | 143 |   bool InterfaceFlag = false;
 | 
|---|
| [bfd839] | 144 |   bool Interface2Flag = false;
 | 
|---|
| [fe238c] | 145 |   bool OtherHydrogenFlag = true;
 | 
|---|
| [a7b761b] | 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){
 | 
|---|
| [83f176] | 152 |           if (((*Walker)->getType()->getAtomicNumber()  == 8) && ((*Runner)->getType()->getAtomicNumber()  == 8)) {
 | 
|---|
| [388049] | 153 |             // check distance
 | 
|---|
| [d74077] | 154 |             const double distance = (*Runner)->DistanceSquared(*(*Walker));
 | 
|---|
| [388049] | 155 |             if ((distance > MYEPSILON) && (distance < HBRIDGEDISTANCE*HBRIDGEDISTANCE)) { // distance >0 means  different atoms
 | 
|---|
| [fe238c] | 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;
 | 
|---|
| [388049] | 161 |               InterfaceFlag = (InterfaceElement == NULL);
 | 
|---|
| [bfd839] | 162 |               Interface2Flag = (Interface2Element == NULL);
 | 
|---|
| [a7b761b] | 163 |               for (BondList::const_iterator BondRunner = (*Runner)->ListOfBonds.begin(); BondRunner != (*Runner)->ListOfBonds.end(); BondRunner++) {
 | 
|---|
 | 164 |                 atom * const OtherAtom = (*BondRunner)->GetOtherAtom(*Runner);
 | 
|---|
| [fe238c] | 165 |                 // if hydrogen, check angle to be greater(!) than 30 degrees
 | 
|---|
| [83f176] | 166 |                 if (OtherAtom->getType()->getAtomicNumber() == 1) {
 | 
|---|
| [d74077] | 167 |                   const double angle = CalculateAngle(OtherAtom->getPosition(), (*Runner)->getPosition(), (*Walker)->getPosition());
 | 
|---|
| [fe238c] | 168 |                   OtherHydrogenFlag = OtherHydrogenFlag && (angle > M_PI*(30./180.) + MYEPSILON);
 | 
|---|
 | 169 |                   Otherangle += angle;
 | 
|---|
 | 170 |                   OtherHydrogens++;
 | 
|---|
 | 171 |                 }
 | 
|---|
| [d74077] | 172 |                 InterfaceFlag = InterfaceFlag || (OtherAtom->getType() == InterfaceElement);
 | 
|---|
 | 173 |                 Interface2Flag = Interface2Flag || (OtherAtom->getType() == Interface2Element);
 | 
|---|
| [388049] | 174 |               }
 | 
|---|
| [fe238c] | 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 |               }
 | 
|---|
| [bfd839] | 187 |               if (InterfaceFlag && Interface2Flag && OtherHydrogenFlag) {
 | 
|---|
| [388049] | 188 |                 // on this element (Walker) we check for bond to hydrogen, i.e. part of water molecule
 | 
|---|
| [a7b761b] | 189 |                 for (BondList::const_iterator BondRunner = (*Walker)->ListOfBonds.begin(); BondRunner != (*Walker)->ListOfBonds.end(); BondRunner++) {
 | 
|---|
 | 190 |                   atom * const OtherAtom = (*BondRunner)->GetOtherAtom(*Walker);
 | 
|---|
| [83f176] | 191 |                   if (OtherAtom->getType()->getAtomicNumber() == 1) {
 | 
|---|
| [388049] | 192 |                     // check angle
 | 
|---|
| [a7b761b] | 193 |                     if (CheckHydrogenBridgeBondAngle(*Walker, OtherAtom, *Runner)) {
 | 
|---|
| [d74077] | 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);
 | 
|---|
| [388049] | 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++) {
 | 
|---|
| [a7b761b] | 221 |     molecule::iterator Walker = (*MolWalker)->begin();
 | 
|---|
 | 222 |     for(;Walker!=(*MolWalker)->end();++Walker){
 | 
|---|
 | 223 |       atom * theAtom = *Walker;
 | 
|---|
| [d74077] | 224 |       if ((theAtom->getType() == first) || (theAtom->getType() == second)) {  // first element matches
 | 
|---|
| [a7b761b] | 225 |         for (BondList::const_iterator BondRunner = theAtom->ListOfBonds.begin(); BondRunner != theAtom->ListOfBonds.end(); BondRunner++) {
 | 
|---|
 | 226 |           atom * const OtherAtom = (*BondRunner)->GetOtherAtom(theAtom);
 | 
|---|
| [d74077] | 227 |           if (((OtherAtom->getType() == first) || (OtherAtom->getType() == second)) && (theAtom->nr < OtherAtom->nr)) {
 | 
|---|
| [388049] | 228 |             count++;
 | 
|---|
| [2fe971] | 229 |             DoLog(1) && (Log() << Verbose(1) << *first << "-" << *second << " bond found between " << *Walker << " and " << *OtherAtom << "." << endl);
 | 
|---|
| [388049] | 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++) {
 | 
|---|
| [a7b761b] | 257 |     molecule::iterator Walker = (*MolWalker)->begin();
 | 
|---|
 | 258 |     for(;Walker!=(*MolWalker)->end();++Walker){
 | 
|---|
 | 259 |       atom *theAtom = *Walker;
 | 
|---|
| [d74077] | 260 |       if (theAtom->getType() == second) {  // first element matches
 | 
|---|
| [388049] | 261 |         for (int i=0;i<2;i++)
 | 
|---|
 | 262 |           MatchFlag[i] = false;
 | 
|---|
| [a7b761b] | 263 |         for (BondList::const_iterator BondRunner = theAtom->ListOfBonds.begin(); BondRunner != theAtom->ListOfBonds.end(); BondRunner++) {
 | 
|---|
 | 264 |           atom * const OtherAtom = (*BondRunner)->GetOtherAtom(theAtom);
 | 
|---|
| [388049] | 265 |           for (int i=0;i<2;i++)
 | 
|---|
| [d74077] | 266 |             if ((!MatchFlag[i]) && (OtherAtom->getType() == ElementArray[i])) {
 | 
|---|
| [388049] | 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++;
 | 
|---|
| [2fe971] | 276 |           DoLog(1) && (Log() << Verbose(1) << *first << "-" << *second << "-" << *third << " bond found at " << *Walker << "." << endl);
 | 
|---|
| [388049] | 277 |         }
 | 
|---|
 | 278 |       }
 | 
|---|
 | 279 |     }
 | 
|---|
 | 280 |   }
 | 
|---|
 | 281 |   return count;
 | 
|---|
 | 282 | };
 | 
|---|