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