| [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"
 | 
|---|
| [e138de] | 13 | #include "log.hpp"
 | 
|---|
| [b70721] | 14 | #include "molecule.hpp"
 | 
|---|
 | 15 | #include "parser.hpp"
 | 
|---|
| [ae38fb] | 16 | #include "periodentafel.hpp"
 | 
|---|
| [b70721] | 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 |  */
 | 
|---|
| [ae38fb] | 22 | BondGraph::BondGraph(bool IsA) : BondLengthMatrix(NULL), max_distance(0), IsAngstroem(IsA)
 | 
|---|
| [b70721] | 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.
 | 
|---|
| [34e0013] | 36 |  * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(),
 | 
|---|
| [b998c3] | 37 |  * but only if parsing is successful. Otherwise variable is left as NULL.
 | 
|---|
| [b70721] | 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 |  */
 | 
|---|
| [e138de] | 42 | bool BondGraph::LoadBondLengthTable(const string &filename)
 | 
|---|
| [b70721] | 43 | {
 | 
|---|
 | 44 |   bool status = true;
 | 
|---|
| [34e0013] | 45 |   MatrixContainer *TempContainer = NULL;
 | 
|---|
| [b70721] | 46 | 
 | 
|---|
 | 47 |   // allocate MatrixContainer
 | 
|---|
 | 48 |   if (BondLengthMatrix != NULL) {
 | 
|---|
| [e138de] | 49 |     Log() << Verbose(1) << "MatrixContainer for Bond length already present, removing." << endl;
 | 
|---|
| [b70721] | 50 |     delete(BondLengthMatrix);
 | 
|---|
 | 51 |   }
 | 
|---|
| [34e0013] | 52 |   TempContainer = new MatrixContainer;
 | 
|---|
| [b70721] | 53 | 
 | 
|---|
 | 54 |   // parse in matrix
 | 
|---|
| [34e0013] | 55 |   status = TempContainer->ParseMatrix(filename.c_str(), 0, 1, 0);
 | 
|---|
| [b70721] | 56 | 
 | 
|---|
 | 57 |   // find greatest distance
 | 
|---|
 | 58 |   max_distance=0;
 | 
|---|
| [34e0013] | 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 |   }
 | 
|---|
| [b70721] | 65 | 
 | 
|---|
| [34e0013] | 66 |   if (status) // set to not NULL only if matrix was parsed
 | 
|---|
 | 67 |     BondLengthMatrix = TempContainer;
 | 
|---|
 | 68 |   else {
 | 
|---|
 | 69 |     BondLengthMatrix = NULL;
 | 
|---|
 | 70 |     delete(TempContainer);
 | 
|---|
 | 71 |   }
 | 
|---|
| [b70721] | 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 |  */
 | 
|---|
| [e138de] | 80 | bool BondGraph::ConstructBondGraph(molecule * const mol)
 | 
|---|
| [b70721] | 81 | {
 | 
|---|
 | 82 |   bool status = true;
 | 
|---|
 | 83 | 
 | 
|---|
| [34e0013] | 84 |   if (mol->start->next == mol->end) // only construct if molecule is not empty
 | 
|---|
 | 85 |     return false;
 | 
|---|
 | 86 | 
 | 
|---|
| [3c349b] | 87 |   if (BondLengthMatrix == NULL) { // no bond length matrix parsed?
 | 
|---|
| [e138de] | 88 |     SetMaxDistanceToMaxOfCovalentRadii(mol);
 | 
|---|
 | 89 |     mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::CovalentMinMaxDistance, this);
 | 
|---|
| [3c349b] | 90 |   } else
 | 
|---|
| [e138de] | 91 |     mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::BondLengthMatrixMinMaxDistance, this);
 | 
|---|
| [b70721] | 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 | {
 | 
|---|
| [34e0013] | 103 |   if (BondLengthMatrix == NULL)
 | 
|---|
 | 104 |     return( -1. );
 | 
|---|
 | 105 |   else
 | 
|---|
 | 106 |     return (BondLengthMatrix->Matrix[0][firstZ][secondZ]);
 | 
|---|
| [b70721] | 107 | };
 | 
|---|
 | 108 | 
 | 
|---|
| [3c349b] | 109 | /** Determines the maximum of all element::CovalentRadius for elements present in \a *mol.
 | 
|---|
| [ae38fb] | 110 |  * \param *out output stream for debugging
 | 
|---|
| [3c349b] | 111 |  * \param *mol molecule with all atoms and their respective elements.
 | 
|---|
| [ae38fb] | 112 |  */
 | 
|---|
| [e138de] | 113 | double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(const molecule * const mol)
 | 
|---|
| [ae38fb] | 114 | {
 | 
|---|
 | 115 |   max_distance = 0.;
 | 
|---|
 | 116 | 
 | 
|---|
| [3c349b] | 117 |   atom *Runner = mol->start;
 | 
|---|
 | 118 |   while (Runner->next != mol->end) {
 | 
|---|
| [ae38fb] | 119 |     Runner = Runner->next;
 | 
|---|
| [3c349b] | 120 |     if (Runner->type->CovalentRadius > max_distance)
 | 
|---|
 | 121 |       max_distance = Runner->type->CovalentRadius;
 | 
|---|
| [ae38fb] | 122 |   }
 | 
|---|
 | 123 |   max_distance *= 2.;
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |   return max_distance;
 | 
|---|
 | 126 | };
 | 
|---|
 | 127 | 
 | 
|---|
| [b70721] | 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 | {
 | 
|---|
| [34e0013] | 154 |   if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet
 | 
|---|
| [717e0c] | 155 |     eLog() << Verbose(2) << "BondLengthMatrixMinMaxDistance() called without having parsed the bond length matrix yet!" << endl;
 | 
|---|
| [b21a64] | 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 |   }
 | 
|---|
| [b70721] | 163 | };
 | 
|---|
 | 164 | 
 | 
|---|