| [b70721] | 1 | /* | 
|---|
|  | 2 | * bondgraph.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Oct 29, 2009 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [bf3817] | 8 | // include config.h | 
|---|
|  | 9 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 10 | #include <config.h> | 
|---|
|  | 11 | #endif | 
|---|
|  | 12 |  | 
|---|
| [112b09] | 13 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 14 |  | 
|---|
| [b70721] | 15 | #include <iostream> | 
|---|
|  | 16 |  | 
|---|
|  | 17 | #include "atom.hpp" | 
|---|
| [1cbf47] | 18 | #include "bond.hpp" | 
|---|
| [b70721] | 19 | #include "bondgraph.hpp" | 
|---|
|  | 20 | #include "element.hpp" | 
|---|
| [952f38] | 21 | #include "Helpers/Info.hpp" | 
|---|
|  | 22 | #include "Helpers/Verbose.hpp" | 
|---|
|  | 23 | #include "Helpers/Log.hpp" | 
|---|
| [b70721] | 24 | #include "molecule.hpp" | 
|---|
|  | 25 | #include "parser.hpp" | 
|---|
| [ae38fb] | 26 | #include "periodentafel.hpp" | 
|---|
| [57f243] | 27 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [b70721] | 28 |  | 
|---|
| [88b400] | 29 | const double BondGraph::BondThreshold = 0.4;   //!< CSD threshold in bond check which is the width of the interval whose center is the sum of the covalent radii | 
|---|
|  | 30 |  | 
|---|
| [b70721] | 31 | /** Constructor of class BondGraph. | 
|---|
|  | 32 | * This classes contains typical bond lengths and thus may be used to construct a bond graph for a given molecule. | 
|---|
|  | 33 | */ | 
|---|
| [97b825] | 34 | BondGraph::BondGraph(bool IsA) : | 
|---|
|  | 35 | BondLengthMatrix(NULL), | 
|---|
|  | 36 | max_distance(0), | 
|---|
|  | 37 | IsAngstroem(IsA) | 
|---|
|  | 38 | {}; | 
|---|
| [b70721] | 39 |  | 
|---|
|  | 40 | /** Destructor of class BondGraph. | 
|---|
|  | 41 | */ | 
|---|
|  | 42 | BondGraph::~BondGraph() | 
|---|
|  | 43 | { | 
|---|
|  | 44 | if (BondLengthMatrix != NULL) { | 
|---|
|  | 45 | delete(BondLengthMatrix); | 
|---|
|  | 46 | } | 
|---|
|  | 47 | }; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | /** Parses the bond lengths in a given file and puts them int a matrix form. | 
|---|
| [34e0013] | 50 | * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(), | 
|---|
| [b998c3] | 51 | * but only if parsing is successful. Otherwise variable is left as NULL. | 
|---|
| [b70721] | 52 | * \param *out output stream for debugging | 
|---|
|  | 53 | * \param filename file with bond lengths to parse | 
|---|
|  | 54 | * \return true - success in parsing file, false - failed to parse the file | 
|---|
|  | 55 | */ | 
|---|
| [e138de] | 56 | bool BondGraph::LoadBondLengthTable(const string &filename) | 
|---|
| [b70721] | 57 | { | 
|---|
| [244a84] | 58 | Info FunctionInfo(__func__); | 
|---|
| [b70721] | 59 | bool status = true; | 
|---|
| [34e0013] | 60 | MatrixContainer *TempContainer = NULL; | 
|---|
| [b70721] | 61 |  | 
|---|
|  | 62 | // allocate MatrixContainer | 
|---|
|  | 63 | if (BondLengthMatrix != NULL) { | 
|---|
| [a67d19] | 64 | DoLog(1) && (Log() << Verbose(1) << "MatrixContainer for Bond length already present, removing." << endl); | 
|---|
| [b70721] | 65 | delete(BondLengthMatrix); | 
|---|
|  | 66 | } | 
|---|
| [34e0013] | 67 | TempContainer = new MatrixContainer; | 
|---|
| [b70721] | 68 |  | 
|---|
|  | 69 | // parse in matrix | 
|---|
| [a26ca5] | 70 | if ((status = TempContainer->ParseMatrix(filename.c_str(), 0, 1, 0))) { | 
|---|
| [a67d19] | 71 | DoLog(1) && (Log() << Verbose(1) << "Parsing bond length matrix successful." << endl); | 
|---|
| [244a84] | 72 | } else { | 
|---|
| [58ed4a] | 73 | DoeLog(1) && (eLog()<< Verbose(1) << "Parsing bond length matrix failed." << endl); | 
|---|
| [244a84] | 74 | } | 
|---|
| [b70721] | 75 |  | 
|---|
|  | 76 | // find greatest distance | 
|---|
|  | 77 | max_distance=0; | 
|---|
| [34e0013] | 78 | if (status) { | 
|---|
|  | 79 | for(int i=0;i<TempContainer->RowCounter[0];i++) | 
|---|
|  | 80 | for(int j=i;j<TempContainer->ColumnCounter[0];j++) | 
|---|
|  | 81 | if (TempContainer->Matrix[0][i][j] > max_distance) | 
|---|
|  | 82 | max_distance = TempContainer->Matrix[0][i][j]; | 
|---|
|  | 83 | } | 
|---|
| [b70721] | 84 |  | 
|---|
| [34e0013] | 85 | if (status) // set to not NULL only if matrix was parsed | 
|---|
|  | 86 | BondLengthMatrix = TempContainer; | 
|---|
|  | 87 | else { | 
|---|
|  | 88 | BondLengthMatrix = NULL; | 
|---|
|  | 89 | delete(TempContainer); | 
|---|
|  | 90 | } | 
|---|
| [b70721] | 91 | return status; | 
|---|
|  | 92 | }; | 
|---|
|  | 93 |  | 
|---|
|  | 94 | /** Parses the bond lengths in a given file and puts them int a matrix form. | 
|---|
|  | 95 | * \param *out output stream for debugging | 
|---|
|  | 96 | * \param *mol molecule with atoms | 
|---|
|  | 97 | * \return true - success, false - failed to construct bond structure | 
|---|
|  | 98 | */ | 
|---|
| [e138de] | 99 | bool BondGraph::ConstructBondGraph(molecule * const mol) | 
|---|
| [b70721] | 100 | { | 
|---|
| [1cbf47] | 101 | Info FunctionInfo(__func__); | 
|---|
| [bd6bfa] | 102 | bool status = true; | 
|---|
| [b70721] | 103 |  | 
|---|
| [9879f6] | 104 | if (mol->empty()) // only construct if molecule is not empty | 
|---|
| [34e0013] | 105 | return false; | 
|---|
|  | 106 |  | 
|---|
| [3c349b] | 107 | if (BondLengthMatrix == NULL) { // no bond length matrix parsed? | 
|---|
| [e138de] | 108 | SetMaxDistanceToMaxOfCovalentRadii(mol); | 
|---|
|  | 109 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::CovalentMinMaxDistance, this); | 
|---|
| [3c349b] | 110 | } else | 
|---|
| [e138de] | 111 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::BondLengthMatrixMinMaxDistance, this); | 
|---|
| [b70721] | 112 |  | 
|---|
|  | 113 | return status; | 
|---|
|  | 114 | }; | 
|---|
|  | 115 |  | 
|---|
|  | 116 | /** Returns the entry for a given index pair. | 
|---|
|  | 117 | * \param firstelement index/atom number of first element (row index) | 
|---|
|  | 118 | * \param secondelement index/atom number of second element (column index) | 
|---|
|  | 119 | * \note matrix is of course symmetric. | 
|---|
|  | 120 | */ | 
|---|
|  | 121 | double BondGraph::GetBondLength(int firstZ, int secondZ) | 
|---|
|  | 122 | { | 
|---|
| [34e0013] | 123 | if (BondLengthMatrix == NULL) | 
|---|
|  | 124 | return( -1. ); | 
|---|
|  | 125 | else | 
|---|
|  | 126 | return (BondLengthMatrix->Matrix[0][firstZ][secondZ]); | 
|---|
| [b70721] | 127 | }; | 
|---|
|  | 128 |  | 
|---|
| [3c349b] | 129 | /** Determines the maximum of all element::CovalentRadius for elements present in \a *mol. | 
|---|
| [ae38fb] | 130 | * \param *out output stream for debugging | 
|---|
| [3c349b] | 131 | * \param *mol molecule with all atoms and their respective elements. | 
|---|
| [ae38fb] | 132 | */ | 
|---|
| [e138de] | 133 | double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(const molecule * const mol) | 
|---|
| [ae38fb] | 134 | { | 
|---|
| [1cbf47] | 135 | Info FunctionInfo(__func__); | 
|---|
| [ae38fb] | 136 | max_distance = 0.; | 
|---|
|  | 137 |  | 
|---|
| [9879f6] | 138 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) { | 
|---|
| [d74077] | 139 | if ((*iter)->getType()->CovalentRadius > max_distance) | 
|---|
|  | 140 | max_distance = (*iter)->getType()->CovalentRadius; | 
|---|
| [ae38fb] | 141 | } | 
|---|
|  | 142 | max_distance *= 2.; | 
|---|
|  | 143 |  | 
|---|
|  | 144 | return max_distance; | 
|---|
|  | 145 | }; | 
|---|
|  | 146 |  | 
|---|
| [b70721] | 147 | /** Returns bond criterion for given pair based on covalent radius. | 
|---|
|  | 148 | * \param *Walker first BondedParticle | 
|---|
|  | 149 | * \param *OtherWalker second BondedParticle | 
|---|
|  | 150 | * \param &MinDistance lower bond bound on return | 
|---|
|  | 151 | * \param &MaxDistance upper bond bound on return | 
|---|
|  | 152 | * \param IsAngstroem whether units are in angstroem or bohr radii | 
|---|
|  | 153 | */ | 
|---|
|  | 154 | void BondGraph::CovalentMinMaxDistance(BondedParticle * const Walker, BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
|  | 155 | { | 
|---|
| [d74077] | 156 | MinDistance = OtherWalker->getType()->CovalentRadius + Walker->getType()->CovalentRadius; | 
|---|
| [b70721] | 157 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
| [88b400] | 158 | MaxDistance = MinDistance + BondThreshold; | 
|---|
|  | 159 | MinDistance -= BondThreshold; | 
|---|
| [b70721] | 160 | }; | 
|---|
|  | 161 |  | 
|---|
|  | 162 | /** Returns bond criterion for given pair based on a bond length matrix. | 
|---|
|  | 163 | * The matrix should be contained in \a this BondGraph and contain an element- | 
|---|
|  | 164 | * to-element length. | 
|---|
|  | 165 | * \param *Walker first BondedParticle | 
|---|
|  | 166 | * \param *OtherWalker second BondedParticle | 
|---|
|  | 167 | * \param &MinDistance lower bond bound on return | 
|---|
|  | 168 | * \param &MaxDistance upper bond bound on return | 
|---|
|  | 169 | * \param IsAngstroem whether units are in angstroem or bohr radii | 
|---|
|  | 170 | */ | 
|---|
|  | 171 | void BondGraph::BondLengthMatrixMinMaxDistance(BondedParticle * const Walker, BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
|  | 172 | { | 
|---|
| [34e0013] | 173 | if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet | 
|---|
| [58ed4a] | 174 | DoeLog(2) && (eLog()<< Verbose(2) << "BondLengthMatrixMinMaxDistance() called without having parsed the bond length matrix yet!" << endl); | 
|---|
| [b21a64] | 175 | CovalentMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem); | 
|---|
|  | 176 | } else { | 
|---|
| [d74077] | 177 | MinDistance = GetBondLength(Walker->getType()->Z-1, OtherWalker->getType()->Z-1); | 
|---|
| [b21a64] | 178 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
| [88b400] | 179 | MaxDistance = MinDistance + BondThreshold; | 
|---|
|  | 180 | MinDistance -= BondThreshold; | 
|---|
| [b21a64] | 181 | } | 
|---|
| [b70721] | 182 | }; | 
|---|