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