| [b70721] | 1 | /*
 | 
|---|
 | 2 |  * bondgraph.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Oct 29, 2009
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [112b09] | 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 9 | 
 | 
|---|
| [b70721] | 10 | #include <iostream>
 | 
|---|
 | 11 | 
 | 
|---|
 | 12 | #include "atom.hpp"
 | 
|---|
| [1cbf47] | 13 | #include "bond.hpp"
 | 
|---|
| [b70721] | 14 | #include "bondgraph.hpp"
 | 
|---|
 | 15 | #include "element.hpp"
 | 
|---|
| [952f38] | 16 | #include "Helpers/Info.hpp"
 | 
|---|
 | 17 | #include "Helpers/Verbose.hpp"
 | 
|---|
 | 18 | #include "Helpers/Log.hpp"
 | 
|---|
| [b70721] | 19 | #include "molecule.hpp"
 | 
|---|
 | 20 | #include "parser.hpp"
 | 
|---|
| [ae38fb] | 21 | #include "periodentafel.hpp"
 | 
|---|
| [57f243] | 22 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [b70721] | 23 | 
 | 
|---|
| [88b400] | 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 | 
 | 
|---|
| [b70721] | 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 |  */
 | 
|---|
| [97b825] | 29 | BondGraph::BondGraph(bool IsA) :
 | 
|---|
 | 30 |     BondLengthMatrix(NULL),
 | 
|---|
 | 31 |     max_distance(0),
 | 
|---|
 | 32 |     IsAngstroem(IsA)
 | 
|---|
 | 33 | {};
 | 
|---|
| [b70721] | 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.
 | 
|---|
| [34e0013] | 45 |  * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(),
 | 
|---|
| [b998c3] | 46 |  * but only if parsing is successful. Otherwise variable is left as NULL.
 | 
|---|
| [b70721] | 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 |  */
 | 
|---|
| [e138de] | 51 | bool BondGraph::LoadBondLengthTable(const string &filename)
 | 
|---|
| [b70721] | 52 | {
 | 
|---|
| [244a84] | 53 |   Info FunctionInfo(__func__);
 | 
|---|
| [b70721] | 54 |   bool status = true;
 | 
|---|
| [34e0013] | 55 |   MatrixContainer *TempContainer = NULL;
 | 
|---|
| [b70721] | 56 | 
 | 
|---|
 | 57 |   // allocate MatrixContainer
 | 
|---|
 | 58 |   if (BondLengthMatrix != NULL) {
 | 
|---|
| [a67d19] | 59 |     DoLog(1) && (Log() << Verbose(1) << "MatrixContainer for Bond length already present, removing." << endl);
 | 
|---|
| [b70721] | 60 |     delete(BondLengthMatrix);
 | 
|---|
 | 61 |   }
 | 
|---|
| [34e0013] | 62 |   TempContainer = new MatrixContainer;
 | 
|---|
| [b70721] | 63 | 
 | 
|---|
 | 64 |   // parse in matrix
 | 
|---|
| [a26ca5] | 65 |   if ((status = TempContainer->ParseMatrix(filename.c_str(), 0, 1, 0))) {
 | 
|---|
| [a67d19] | 66 |     DoLog(1) && (Log() << Verbose(1) << "Parsing bond length matrix successful." << endl);
 | 
|---|
| [244a84] | 67 |   } else {
 | 
|---|
| [58ed4a] | 68 |     DoeLog(1) && (eLog()<< Verbose(1) << "Parsing bond length matrix failed." << endl);
 | 
|---|
| [244a84] | 69 |   }
 | 
|---|
| [b70721] | 70 | 
 | 
|---|
 | 71 |   // find greatest distance
 | 
|---|
 | 72 |   max_distance=0;
 | 
|---|
| [34e0013] | 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 |   }
 | 
|---|
| [b70721] | 79 | 
 | 
|---|
| [34e0013] | 80 |   if (status) // set to not NULL only if matrix was parsed
 | 
|---|
 | 81 |     BondLengthMatrix = TempContainer;
 | 
|---|
 | 82 |   else {
 | 
|---|
 | 83 |     BondLengthMatrix = NULL;
 | 
|---|
 | 84 |     delete(TempContainer);
 | 
|---|
 | 85 |   }
 | 
|---|
| [b70721] | 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 |  */
 | 
|---|
| [e138de] | 94 | bool BondGraph::ConstructBondGraph(molecule * const mol)
 | 
|---|
| [b70721] | 95 | {
 | 
|---|
| [1cbf47] | 96 |   Info FunctionInfo(__func__);
 | 
|---|
| [bd6bfa] | 97 |   bool status = true;
 | 
|---|
| [b70721] | 98 | 
 | 
|---|
| [9879f6] | 99 |   if (mol->empty()) // only construct if molecule is not empty
 | 
|---|
| [34e0013] | 100 |     return false;
 | 
|---|
 | 101 | 
 | 
|---|
| [3c349b] | 102 |   if (BondLengthMatrix == NULL) { // no bond length matrix parsed?
 | 
|---|
| [e138de] | 103 |     SetMaxDistanceToMaxOfCovalentRadii(mol);
 | 
|---|
 | 104 |     mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::CovalentMinMaxDistance, this);
 | 
|---|
| [3c349b] | 105 |   } else
 | 
|---|
| [e138de] | 106 |     mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::BondLengthMatrixMinMaxDistance, this);
 | 
|---|
| [b70721] | 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 | {
 | 
|---|
| [34e0013] | 118 |   if (BondLengthMatrix == NULL)
 | 
|---|
 | 119 |     return( -1. );
 | 
|---|
 | 120 |   else
 | 
|---|
 | 121 |     return (BondLengthMatrix->Matrix[0][firstZ][secondZ]);
 | 
|---|
| [b70721] | 122 | };
 | 
|---|
 | 123 | 
 | 
|---|
| [3c349b] | 124 | /** Determines the maximum of all element::CovalentRadius for elements present in \a *mol.
 | 
|---|
| [ae38fb] | 125 |  * \param *out output stream for debugging
 | 
|---|
| [3c349b] | 126 |  * \param *mol molecule with all atoms and their respective elements.
 | 
|---|
| [ae38fb] | 127 |  */
 | 
|---|
| [e138de] | 128 | double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(const molecule * const mol)
 | 
|---|
| [ae38fb] | 129 | {
 | 
|---|
| [1cbf47] | 130 |   Info FunctionInfo(__func__);
 | 
|---|
| [ae38fb] | 131 |   max_distance = 0.;
 | 
|---|
 | 132 | 
 | 
|---|
| [9879f6] | 133 |   for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
 | 
|---|
| [d74077] | 134 |     if ((*iter)->getType()->CovalentRadius > max_distance)
 | 
|---|
 | 135 |       max_distance = (*iter)->getType()->CovalentRadius;
 | 
|---|
| [ae38fb] | 136 |   }
 | 
|---|
 | 137 |   max_distance *= 2.;
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |   return max_distance;
 | 
|---|
 | 140 | };
 | 
|---|
 | 141 | 
 | 
|---|
| [b70721] | 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 | {
 | 
|---|
| [d74077] | 151 |   MinDistance = OtherWalker->getType()->CovalentRadius + Walker->getType()->CovalentRadius;
 | 
|---|
| [b70721] | 152 |   MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
 | 
|---|
| [88b400] | 153 |   MaxDistance = MinDistance + BondThreshold;
 | 
|---|
 | 154 |   MinDistance -= BondThreshold;
 | 
|---|
| [b70721] | 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 | {
 | 
|---|
| [34e0013] | 168 |   if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet
 | 
|---|
| [58ed4a] | 169 |     DoeLog(2) && (eLog()<< Verbose(2) << "BondLengthMatrixMinMaxDistance() called without having parsed the bond length matrix yet!" << endl);
 | 
|---|
| [b21a64] | 170 |     CovalentMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem);
 | 
|---|
 | 171 |   } else {
 | 
|---|
| [d74077] | 172 |     MinDistance = GetBondLength(Walker->getType()->Z-1, OtherWalker->getType()->Z-1);
 | 
|---|
| [b21a64] | 173 |     MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
 | 
|---|
| [88b400] | 174 |     MaxDistance = MinDistance + BondThreshold;
 | 
|---|
 | 175 |     MinDistance -= BondThreshold;
 | 
|---|
| [b21a64] | 176 |   }
 | 
|---|
| [b70721] | 177 | };
 | 
|---|