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