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