| [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"
 | 
|---|
| [244a84] | 16 | #include "info.hpp"
 | 
|---|
| [e138de] | 17 | #include "log.hpp"
 | 
|---|
| [b70721] | 18 | #include "molecule.hpp"
 | 
|---|
 | 19 | #include "parser.hpp"
 | 
|---|
| [ae38fb] | 20 | #include "periodentafel.hpp"
 | 
|---|
| [b70721] | 21 | #include "vector.hpp"
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /** Constructor of class BondGraph.
 | 
|---|
 | 24 |  * This classes contains typical bond lengths and thus may be used to construct a bond graph for a given molecule.
 | 
|---|
 | 25 |  */
 | 
|---|
| [ae38fb] | 26 | BondGraph::BondGraph(bool IsA) : BondLengthMatrix(NULL), max_distance(0), IsAngstroem(IsA)
 | 
|---|
| [b70721] | 27 | {
 | 
|---|
 | 28 | };
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | /** Destructor of class BondGraph.
 | 
|---|
 | 31 |  */
 | 
|---|
 | 32 | BondGraph::~BondGraph()
 | 
|---|
 | 33 | {
 | 
|---|
 | 34 |   if (BondLengthMatrix != NULL) {
 | 
|---|
 | 35 |     delete(BondLengthMatrix);
 | 
|---|
 | 36 |   }
 | 
|---|
 | 37 | };
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | /** Parses the bond lengths in a given file and puts them int a matrix form.
 | 
|---|
| [34e0013] | 40 |  * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(),
 | 
|---|
| [b998c3] | 41 |  * but only if parsing is successful. Otherwise variable is left as NULL.
 | 
|---|
| [b70721] | 42 |  * \param *out output stream for debugging
 | 
|---|
 | 43 |  * \param filename file with bond lengths to parse
 | 
|---|
 | 44 |  * \return true - success in parsing file, false - failed to parse the file
 | 
|---|
 | 45 |  */
 | 
|---|
| [e138de] | 46 | bool BondGraph::LoadBondLengthTable(const string &filename)
 | 
|---|
| [b70721] | 47 | {
 | 
|---|
| [244a84] | 48 |   Info FunctionInfo(__func__);
 | 
|---|
| [b70721] | 49 |   bool status = true;
 | 
|---|
| [34e0013] | 50 |   MatrixContainer *TempContainer = NULL;
 | 
|---|
| [b70721] | 51 | 
 | 
|---|
 | 52 |   // allocate MatrixContainer
 | 
|---|
 | 53 |   if (BondLengthMatrix != NULL) {
 | 
|---|
| [a67d19] | 54 |     DoLog(1) && (Log() << Verbose(1) << "MatrixContainer for Bond length already present, removing." << endl);
 | 
|---|
| [b70721] | 55 |     delete(BondLengthMatrix);
 | 
|---|
 | 56 |   }
 | 
|---|
| [34e0013] | 57 |   TempContainer = new MatrixContainer;
 | 
|---|
| [b70721] | 58 | 
 | 
|---|
 | 59 |   // parse in matrix
 | 
|---|
| [a26ca5] | 60 |   if ((status = TempContainer->ParseMatrix(filename.c_str(), 0, 1, 0))) {
 | 
|---|
| [a67d19] | 61 |     DoLog(1) && (Log() << Verbose(1) << "Parsing bond length matrix successful." << endl);
 | 
|---|
| [244a84] | 62 |   } else {
 | 
|---|
| [58ed4a] | 63 |     DoeLog(1) && (eLog()<< Verbose(1) << "Parsing bond length matrix failed." << endl);
 | 
|---|
| [244a84] | 64 |   }
 | 
|---|
| [b70721] | 65 | 
 | 
|---|
 | 66 |   // find greatest distance
 | 
|---|
 | 67 |   max_distance=0;
 | 
|---|
| [34e0013] | 68 |   if (status) {
 | 
|---|
 | 69 |     for(int i=0;i<TempContainer->RowCounter[0];i++)
 | 
|---|
 | 70 |       for(int j=i;j<TempContainer->ColumnCounter[0];j++)
 | 
|---|
 | 71 |         if (TempContainer->Matrix[0][i][j] > max_distance)
 | 
|---|
 | 72 |           max_distance = TempContainer->Matrix[0][i][j];
 | 
|---|
 | 73 |   }
 | 
|---|
| [b70721] | 74 | 
 | 
|---|
| [34e0013] | 75 |   if (status) // set to not NULL only if matrix was parsed
 | 
|---|
 | 76 |     BondLengthMatrix = TempContainer;
 | 
|---|
 | 77 |   else {
 | 
|---|
 | 78 |     BondLengthMatrix = NULL;
 | 
|---|
 | 79 |     delete(TempContainer);
 | 
|---|
 | 80 |   }
 | 
|---|
| [b70721] | 81 |   return status;
 | 
|---|
 | 82 | };
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | /** Parses the bond lengths in a given file and puts them int a matrix form.
 | 
|---|
 | 85 |  * \param *out output stream for debugging
 | 
|---|
 | 86 |  * \param *mol molecule with atoms
 | 
|---|
 | 87 |  * \return true - success, false - failed to construct bond structure
 | 
|---|
 | 88 |  */
 | 
|---|
| [e138de] | 89 | bool BondGraph::ConstructBondGraph(molecule * const mol)
 | 
|---|
| [b70721] | 90 | {
 | 
|---|
| [1cbf47] | 91 |   Info FunctionInfo(__func__);
 | 
|---|
| [bd6bfa] | 92 |   bool status = true;
 | 
|---|
| [b70721] | 93 | 
 | 
|---|
| [9879f6] | 94 |   if (mol->empty()) // only construct if molecule is not empty
 | 
|---|
| [34e0013] | 95 |     return false;
 | 
|---|
 | 96 | 
 | 
|---|
| [3c349b] | 97 |   if (BondLengthMatrix == NULL) { // no bond length matrix parsed?
 | 
|---|
| [e138de] | 98 |     SetMaxDistanceToMaxOfCovalentRadii(mol);
 | 
|---|
 | 99 |     mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::CovalentMinMaxDistance, this);
 | 
|---|
| [3c349b] | 100 |   } else
 | 
|---|
| [e138de] | 101 |     mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::BondLengthMatrixMinMaxDistance, this);
 | 
|---|
| [b70721] | 102 | 
 | 
|---|
 | 103 |   return status;
 | 
|---|
 | 104 | };
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | /** Returns the entry for a given index pair.
 | 
|---|
 | 107 |  * \param firstelement index/atom number of first element (row index)
 | 
|---|
 | 108 |  * \param secondelement index/atom number of second element (column index)
 | 
|---|
 | 109 |  * \note matrix is of course symmetric.
 | 
|---|
 | 110 |  */
 | 
|---|
 | 111 | double BondGraph::GetBondLength(int firstZ, int secondZ)
 | 
|---|
 | 112 | {
 | 
|---|
| [34e0013] | 113 |   if (BondLengthMatrix == NULL)
 | 
|---|
 | 114 |     return( -1. );
 | 
|---|
 | 115 |   else
 | 
|---|
 | 116 |     return (BondLengthMatrix->Matrix[0][firstZ][secondZ]);
 | 
|---|
| [b70721] | 117 | };
 | 
|---|
 | 118 | 
 | 
|---|
| [3c349b] | 119 | /** Determines the maximum of all element::CovalentRadius for elements present in \a *mol.
 | 
|---|
| [ae38fb] | 120 |  * \param *out output stream for debugging
 | 
|---|
| [3c349b] | 121 |  * \param *mol molecule with all atoms and their respective elements.
 | 
|---|
| [ae38fb] | 122 |  */
 | 
|---|
| [e138de] | 123 | double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(const molecule * const mol)
 | 
|---|
| [ae38fb] | 124 | {
 | 
|---|
| [1cbf47] | 125 |   Info FunctionInfo(__func__);
 | 
|---|
| [ae38fb] | 126 |   max_distance = 0.;
 | 
|---|
 | 127 | 
 | 
|---|
| [9879f6] | 128 |   for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
 | 
|---|
 | 129 |     if ((*iter)->type->CovalentRadius > max_distance)
 | 
|---|
 | 130 |       max_distance = (*iter)->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 | };
 | 
|---|