| [b70721] | 1 | /* | 
|---|
|  | 2 | * bondgraph.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Oct 29, 2009 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #include <iostream> | 
|---|
|  | 9 |  | 
|---|
|  | 10 | #include "atom.hpp" | 
|---|
| [1cbf47] | 11 | #include "bond.hpp" | 
|---|
| [b70721] | 12 | #include "bondgraph.hpp" | 
|---|
|  | 13 | #include "element.hpp" | 
|---|
| [244a84] | 14 | #include "info.hpp" | 
|---|
| [e138de] | 15 | #include "log.hpp" | 
|---|
| [b70721] | 16 | #include "molecule.hpp" | 
|---|
|  | 17 | #include "parser.hpp" | 
|---|
| [ae38fb] | 18 | #include "periodentafel.hpp" | 
|---|
| [b70721] | 19 | #include "vector.hpp" | 
|---|
|  | 20 |  | 
|---|
|  | 21 | /** Constructor of class BondGraph. | 
|---|
|  | 22 | * This classes contains typical bond lengths and thus may be used to construct a bond graph for a given molecule. | 
|---|
|  | 23 | */ | 
|---|
| [ae38fb] | 24 | BondGraph::BondGraph(bool IsA) : BondLengthMatrix(NULL), max_distance(0), IsAngstroem(IsA) | 
|---|
| [b70721] | 25 | { | 
|---|
|  | 26 | }; | 
|---|
|  | 27 |  | 
|---|
|  | 28 | /** Destructor of class BondGraph. | 
|---|
|  | 29 | */ | 
|---|
|  | 30 | BondGraph::~BondGraph() | 
|---|
|  | 31 | { | 
|---|
|  | 32 | if (BondLengthMatrix != NULL) { | 
|---|
|  | 33 | delete(BondLengthMatrix); | 
|---|
|  | 34 | } | 
|---|
|  | 35 | }; | 
|---|
|  | 36 |  | 
|---|
|  | 37 | /** Parses the bond lengths in a given file and puts them int a matrix form. | 
|---|
| [34e0013] | 38 | * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(), | 
|---|
| [b998c3] | 39 | * but only if parsing is successful. Otherwise variable is left as NULL. | 
|---|
| [b70721] | 40 | * \param *out output stream for debugging | 
|---|
|  | 41 | * \param filename file with bond lengths to parse | 
|---|
|  | 42 | * \return true - success in parsing file, false - failed to parse the file | 
|---|
|  | 43 | */ | 
|---|
| [e138de] | 44 | bool BondGraph::LoadBondLengthTable(const string &filename) | 
|---|
| [b70721] | 45 | { | 
|---|
| [244a84] | 46 | Info FunctionInfo(__func__); | 
|---|
| [b70721] | 47 | bool status = true; | 
|---|
| [34e0013] | 48 | MatrixContainer *TempContainer = NULL; | 
|---|
| [b70721] | 49 |  | 
|---|
|  | 50 | // allocate MatrixContainer | 
|---|
|  | 51 | if (BondLengthMatrix != NULL) { | 
|---|
| [e138de] | 52 | Log() << Verbose(1) << "MatrixContainer for Bond length already present, removing." << endl; | 
|---|
| [b70721] | 53 | delete(BondLengthMatrix); | 
|---|
|  | 54 | } | 
|---|
| [34e0013] | 55 | TempContainer = new MatrixContainer; | 
|---|
| [b70721] | 56 |  | 
|---|
|  | 57 | // parse in matrix | 
|---|
| [244a84] | 58 | if (status = TempContainer->ParseMatrix(filename.c_str(), 0, 1, 0)) { | 
|---|
|  | 59 | Log() << Verbose(1) << "Parsing bond length matrix successful." << endl; | 
|---|
|  | 60 | } else { | 
|---|
| [58ed4a] | 61 | DoeLog(1) && (eLog()<< Verbose(1) << "Parsing bond length matrix failed." << endl); | 
|---|
| [244a84] | 62 | } | 
|---|
| [b70721] | 63 |  | 
|---|
|  | 64 | // find greatest distance | 
|---|
|  | 65 | max_distance=0; | 
|---|
| [34e0013] | 66 | if (status) { | 
|---|
|  | 67 | for(int i=0;i<TempContainer->RowCounter[0];i++) | 
|---|
|  | 68 | for(int j=i;j<TempContainer->ColumnCounter[0];j++) | 
|---|
|  | 69 | if (TempContainer->Matrix[0][i][j] > max_distance) | 
|---|
|  | 70 | max_distance = TempContainer->Matrix[0][i][j]; | 
|---|
|  | 71 | } | 
|---|
| [b70721] | 72 |  | 
|---|
| [34e0013] | 73 | if (status) // set to not NULL only if matrix was parsed | 
|---|
|  | 74 | BondLengthMatrix = TempContainer; | 
|---|
|  | 75 | else { | 
|---|
|  | 76 | BondLengthMatrix = NULL; | 
|---|
|  | 77 | delete(TempContainer); | 
|---|
|  | 78 | } | 
|---|
| [b70721] | 79 | return status; | 
|---|
|  | 80 | }; | 
|---|
|  | 81 |  | 
|---|
|  | 82 | /** Parses the bond lengths in a given file and puts them int a matrix form. | 
|---|
|  | 83 | * \param *out output stream for debugging | 
|---|
|  | 84 | * \param *mol molecule with atoms | 
|---|
|  | 85 | * \return true - success, false - failed to construct bond structure | 
|---|
|  | 86 | */ | 
|---|
| [e138de] | 87 | bool BondGraph::ConstructBondGraph(molecule * const mol) | 
|---|
| [b70721] | 88 | { | 
|---|
| [1cbf47] | 89 | Info FunctionInfo(__func__); | 
|---|
|  | 90 | bool status = true; | 
|---|
| [b70721] | 91 |  | 
|---|
| [34e0013] | 92 | if (mol->start->next == mol->end) // only construct if molecule is not empty | 
|---|
|  | 93 | return false; | 
|---|
|  | 94 |  | 
|---|
| [3c349b] | 95 | if (BondLengthMatrix == NULL) { // no bond length matrix parsed? | 
|---|
| [e138de] | 96 | SetMaxDistanceToMaxOfCovalentRadii(mol); | 
|---|
|  | 97 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::CovalentMinMaxDistance, this); | 
|---|
| [3c349b] | 98 | } else | 
|---|
| [e138de] | 99 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::BondLengthMatrixMinMaxDistance, this); | 
|---|
| [b70721] | 100 |  | 
|---|
|  | 101 | return status; | 
|---|
|  | 102 | }; | 
|---|
|  | 103 |  | 
|---|
|  | 104 | /** Returns the entry for a given index pair. | 
|---|
|  | 105 | * \param firstelement index/atom number of first element (row index) | 
|---|
|  | 106 | * \param secondelement index/atom number of second element (column index) | 
|---|
|  | 107 | * \note matrix is of course symmetric. | 
|---|
|  | 108 | */ | 
|---|
|  | 109 | double BondGraph::GetBondLength(int firstZ, int secondZ) | 
|---|
|  | 110 | { | 
|---|
| [34e0013] | 111 | if (BondLengthMatrix == NULL) | 
|---|
|  | 112 | return( -1. ); | 
|---|
|  | 113 | else | 
|---|
|  | 114 | return (BondLengthMatrix->Matrix[0][firstZ][secondZ]); | 
|---|
| [b70721] | 115 | }; | 
|---|
|  | 116 |  | 
|---|
| [3c349b] | 117 | /** Determines the maximum of all element::CovalentRadius for elements present in \a *mol. | 
|---|
| [ae38fb] | 118 | * \param *out output stream for debugging | 
|---|
| [3c349b] | 119 | * \param *mol molecule with all atoms and their respective elements. | 
|---|
| [ae38fb] | 120 | */ | 
|---|
| [e138de] | 121 | double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(const molecule * const mol) | 
|---|
| [ae38fb] | 122 | { | 
|---|
| [1cbf47] | 123 | Info FunctionInfo(__func__); | 
|---|
| [ae38fb] | 124 | max_distance = 0.; | 
|---|
|  | 125 |  | 
|---|
| [3c349b] | 126 | atom *Runner = mol->start; | 
|---|
|  | 127 | while (Runner->next != mol->end) { | 
|---|
| [ae38fb] | 128 | Runner = Runner->next; | 
|---|
| [3c349b] | 129 | if (Runner->type->CovalentRadius > max_distance) | 
|---|
|  | 130 | max_distance = Runner->type->CovalentRadius; | 
|---|
| [ae38fb] | 131 | } | 
|---|
|  | 132 | max_distance *= 2.; | 
|---|
|  | 133 |  | 
|---|
|  | 134 | return max_distance; | 
|---|
|  | 135 | }; | 
|---|
|  | 136 |  | 
|---|
| [b70721] | 137 | /** Returns bond criterion for given pair based on covalent radius. | 
|---|
|  | 138 | * \param *Walker first BondedParticle | 
|---|
|  | 139 | * \param *OtherWalker second BondedParticle | 
|---|
|  | 140 | * \param &MinDistance lower bond bound on return | 
|---|
|  | 141 | * \param &MaxDistance upper bond bound on return | 
|---|
|  | 142 | * \param IsAngstroem whether units are in angstroem or bohr radii | 
|---|
|  | 143 | */ | 
|---|
|  | 144 | void BondGraph::CovalentMinMaxDistance(BondedParticle * const Walker, BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
|  | 145 | { | 
|---|
|  | 146 | MinDistance = OtherWalker->type->CovalentRadius + Walker->type->CovalentRadius; | 
|---|
|  | 147 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
|  | 148 | MaxDistance = MinDistance + BONDTHRESHOLD; | 
|---|
|  | 149 | MinDistance -= BONDTHRESHOLD; | 
|---|
|  | 150 | }; | 
|---|
|  | 151 |  | 
|---|
|  | 152 | /** Returns bond criterion for given pair based on a bond length matrix. | 
|---|
|  | 153 | * The matrix should be contained in \a this BondGraph and contain an element- | 
|---|
|  | 154 | * to-element length. | 
|---|
|  | 155 | * \param *Walker first BondedParticle | 
|---|
|  | 156 | * \param *OtherWalker second BondedParticle | 
|---|
|  | 157 | * \param &MinDistance lower bond bound on return | 
|---|
|  | 158 | * \param &MaxDistance upper bond bound on return | 
|---|
|  | 159 | * \param IsAngstroem whether units are in angstroem or bohr radii | 
|---|
|  | 160 | */ | 
|---|
|  | 161 | void BondGraph::BondLengthMatrixMinMaxDistance(BondedParticle * const Walker, BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
|  | 162 | { | 
|---|
| [34e0013] | 163 | if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet | 
|---|
| [58ed4a] | 164 | DoeLog(2) && (eLog()<< Verbose(2) << "BondLengthMatrixMinMaxDistance() called without having parsed the bond length matrix yet!" << endl); | 
|---|
| [b21a64] | 165 | CovalentMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem); | 
|---|
|  | 166 | } else { | 
|---|
|  | 167 | MinDistance = GetBondLength(Walker->type->Z-1, OtherWalker->type->Z-1); | 
|---|
|  | 168 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
|  | 169 | MaxDistance = MinDistance + BONDTHRESHOLD; | 
|---|
|  | 170 | MinDistance -= BONDTHRESHOLD; | 
|---|
|  | 171 | } | 
|---|
| [b70721] | 172 | }; | 
|---|
|  | 173 |  | 
|---|
| [1cbf47] | 174 | /** Counts the number of hydrogen bridge bonds. | 
|---|
|  | 175 | * With \a *InterfaceElement an extra element can be specified that identifies some boundary. | 
|---|
|  | 176 | * Then, counting is for the h-bridges that connect to interface only. | 
|---|
|  | 177 | * \param *molecules molecules to count bonds | 
|---|
|  | 178 | * \param *InterfaceElement or NULL | 
|---|
|  | 179 | */ | 
|---|
|  | 180 | int CountHydrogenBridgeBonds(MoleculeListClass *molecules, element * InterfaceElement = NULL) | 
|---|
|  | 181 | { | 
|---|
|  | 182 | Info FunctionInfo(__func__); | 
|---|
|  | 183 | atom *Walker = NULL; | 
|---|
|  | 184 | atom *Runner = NULL; | 
|---|
|  | 185 | atom *Hydrogen = NULL; | 
|---|
|  | 186 | atom *OtherHydrogen = NULL; | 
|---|
|  | 187 | Vector OHBond; | 
|---|
|  | 188 | Vector OOBond; | 
|---|
|  | 189 | int count = 0; | 
|---|
|  | 190 | bool HydrogenFlag = false; | 
|---|
|  | 191 | bool OtherHydrogenFlag = false; | 
|---|
|  | 192 | bool InterfaceFlag = false; | 
|---|
|  | 193 | for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin();MolWalker != molecules->ListOfMolecules.end(); MolWalker++) { | 
|---|
|  | 194 | Walker = (*MolWalker)->start; | 
|---|
|  | 195 | while (Walker->next != (*MolWalker)->end) { | 
|---|
|  | 196 | Walker = Walker->next; | 
|---|
|  | 197 | for (MoleculeList::const_iterator MolRunner = molecules->ListOfMolecules.begin();MolRunner != molecules->ListOfMolecules.end(); MolRunner++) { | 
|---|
|  | 198 | Runner = (*MolRunner)->start; | 
|---|
|  | 199 | while (Runner->next != (*MolRunner)->end) { | 
|---|
|  | 200 | Runner = Runner->next; | 
|---|
|  | 201 | if ((Runner != Walker) && (Walker->type->Z  == 8) && (Runner->type->Z  == 8)) { | 
|---|
|  | 202 | // check distance | 
|---|
|  | 203 | const double distance = Runner->x.DistanceSquared(&Walker->x); | 
|---|
|  | 204 | if (distance < HBRIDGEDISTANCE*HBRIDGEDISTANCE) { | 
|---|
|  | 205 | // get hydrogen, check for InterfaceElement | 
|---|
|  | 206 | HydrogenFlag = false; | 
|---|
|  | 207 | OtherHydrogenFlag = false; | 
|---|
|  | 208 | InterfaceFlag = (InterfaceElement == NULL); | 
|---|
|  | 209 | // on other atom(Runner) we check for bond to interface element | 
|---|
|  | 210 | for (BondList::const_iterator BondRunner = Runner->ListOfBonds.begin(); BondRunner != Runner->ListOfBonds.end(); BondRunner++) { | 
|---|
|  | 211 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(Walker); | 
|---|
|  | 212 | if (!OtherHydrogenFlag && (OtherAtom->type->Z == 1)) { | 
|---|
|  | 213 | OtherHydrogen = OtherAtom; | 
|---|
|  | 214 | OtherHydrogen = true; | 
|---|
|  | 215 | } | 
|---|
|  | 216 | InterfaceFlag = InterfaceFlag || (OtherAtom->type == InterfaceElement); | 
|---|
|  | 217 | } | 
|---|
|  | 218 | // on this element (Walker) we check for bond to hydrogen, i.e. part of water molecule | 
|---|
|  | 219 | for (BondList::const_iterator BondRunner = Walker->ListOfBonds.begin(); BondRunner != Walker->ListOfBonds.end(); BondRunner++) { | 
|---|
|  | 220 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(Walker); | 
|---|
|  | 221 | if (!HydrogenFlag && (OtherAtom->type->Z == 1)) { | 
|---|
|  | 222 | Hydrogen = OtherAtom; | 
|---|
|  | 223 | HydrogenFlag = true; | 
|---|
|  | 224 | } | 
|---|
|  | 225 | } | 
|---|
|  | 226 | if (InterfaceFlag && HydrogenFlag) { | 
|---|
|  | 227 | if ((Walker->nr < Runner->nr) || (!OtherHydrogenFlag)) { | 
|---|
|  | 228 | // check angle | 
|---|
|  | 229 | OHBond.CopyVector(&Walker->x); | 
|---|
|  | 230 | OHBond.SubtractVector(&Hydrogen->x); | 
|---|
|  | 231 | OOBond.CopyVector(&Runner->x); | 
|---|
|  | 232 | OOBond.SubtractVector(&Walker->x); | 
|---|
|  | 233 | const double angle = OHBond.Angle(&OOBond); | 
|---|
|  | 234 | if (angle < M_PI*(30./180.)) { | 
|---|
|  | 235 | DoLog(1) && (Log() << Verbose(1) << Walker->Name << ", " << Hydrogen->Name << " and " << Runner->Name << " have a hydrogen bridge bond with " << sqrt(distance) << " and at angle " << (180./M_PI)*angle << " degrees." << endl); | 
|---|
|  | 236 | count++; | 
|---|
|  | 237 | } | 
|---|
|  | 238 | } | 
|---|
|  | 239 | } | 
|---|
|  | 240 | } | 
|---|
|  | 241 | } | 
|---|
|  | 242 | } | 
|---|
|  | 243 | } | 
|---|
|  | 244 | } | 
|---|
|  | 245 | } | 
|---|
|  | 246 | return count; | 
|---|
|  | 247 | } | 
|---|