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