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