| 1 | /* | 
|---|
| 2 | * bondgraph.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Oct 29, 2009 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Helpers/MemDebug.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | #include <iostream> | 
|---|
| 11 |  | 
|---|
| 12 | #include "atom.hpp" | 
|---|
| 13 | #include "bond.hpp" | 
|---|
| 14 | #include "bondgraph.hpp" | 
|---|
| 15 | #include "element.hpp" | 
|---|
| 16 | #include "Helpers/Info.hpp" | 
|---|
| 17 | #include "Helpers/Verbose.hpp" | 
|---|
| 18 | #include "Helpers/Log.hpp" | 
|---|
| 19 | #include "molecule.hpp" | 
|---|
| 20 | #include "parser.hpp" | 
|---|
| 21 | #include "periodentafel.hpp" | 
|---|
| 22 | #include "LinearAlgebra/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 | */ | 
|---|
| 27 | BondGraph::BondGraph(bool IsA) : BondLengthMatrix(NULL), max_distance(0), IsAngstroem(IsA) | 
|---|
| 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. | 
|---|
| 41 | * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(), | 
|---|
| 42 | * but only if parsing is successful. Otherwise variable is left as NULL. | 
|---|
| 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 | */ | 
|---|
| 47 | bool BondGraph::LoadBondLengthTable(const string &filename) | 
|---|
| 48 | { | 
|---|
| 49 | Info FunctionInfo(__func__); | 
|---|
| 50 | bool status = true; | 
|---|
| 51 | MatrixContainer *TempContainer = NULL; | 
|---|
| 52 |  | 
|---|
| 53 | // allocate MatrixContainer | 
|---|
| 54 | if (BondLengthMatrix != NULL) { | 
|---|
| 55 | DoLog(1) && (Log() << Verbose(1) << "MatrixContainer for Bond length already present, removing." << endl); | 
|---|
| 56 | delete(BondLengthMatrix); | 
|---|
| 57 | } | 
|---|
| 58 | TempContainer = new MatrixContainer; | 
|---|
| 59 |  | 
|---|
| 60 | // parse in matrix | 
|---|
| 61 | if ((status = TempContainer->ParseMatrix(filename.c_str(), 0, 1, 0))) { | 
|---|
| 62 | DoLog(1) && (Log() << Verbose(1) << "Parsing bond length matrix successful." << endl); | 
|---|
| 63 | } else { | 
|---|
| 64 | DoeLog(1) && (eLog()<< Verbose(1) << "Parsing bond length matrix failed." << endl); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | // find greatest distance | 
|---|
| 68 | max_distance=0; | 
|---|
| 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 | } | 
|---|
| 75 |  | 
|---|
| 76 | if (status) // set to not NULL only if matrix was parsed | 
|---|
| 77 | BondLengthMatrix = TempContainer; | 
|---|
| 78 | else { | 
|---|
| 79 | BondLengthMatrix = NULL; | 
|---|
| 80 | delete(TempContainer); | 
|---|
| 81 | } | 
|---|
| 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 | */ | 
|---|
| 90 | bool BondGraph::ConstructBondGraph(molecule * const mol) | 
|---|
| 91 | { | 
|---|
| 92 | Info FunctionInfo(__func__); | 
|---|
| 93 | bool status = true; | 
|---|
| 94 |  | 
|---|
| 95 | if (mol->empty()) // only construct if molecule is not empty | 
|---|
| 96 | return false; | 
|---|
| 97 |  | 
|---|
| 98 | if (BondLengthMatrix == NULL) { // no bond length matrix parsed? | 
|---|
| 99 | SetMaxDistanceToMaxOfCovalentRadii(mol); | 
|---|
| 100 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::CovalentMinMaxDistance, this); | 
|---|
| 101 | } else | 
|---|
| 102 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::BondLengthMatrixMinMaxDistance, this); | 
|---|
| 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 | { | 
|---|
| 114 | if (BondLengthMatrix == NULL) | 
|---|
| 115 | return( -1. ); | 
|---|
| 116 | else | 
|---|
| 117 | return (BondLengthMatrix->Matrix[0][firstZ][secondZ]); | 
|---|
| 118 | }; | 
|---|
| 119 |  | 
|---|
| 120 | /** Determines the maximum of all element::CovalentRadius for elements present in \a *mol. | 
|---|
| 121 | * \param *out output stream for debugging | 
|---|
| 122 | * \param *mol molecule with all atoms and their respective elements. | 
|---|
| 123 | */ | 
|---|
| 124 | double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(const molecule * const mol) | 
|---|
| 125 | { | 
|---|
| 126 | Info FunctionInfo(__func__); | 
|---|
| 127 | max_distance = 0.; | 
|---|
| 128 |  | 
|---|
| 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; | 
|---|
| 132 | } | 
|---|
| 133 | max_distance *= 2.; | 
|---|
| 134 |  | 
|---|
| 135 | return max_distance; | 
|---|
| 136 | }; | 
|---|
| 137 |  | 
|---|
| 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 | { | 
|---|
| 164 | if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet | 
|---|
| 165 | DoeLog(2) && (eLog()<< Verbose(2) << "BondLengthMatrixMinMaxDistance() called without having parsed the bond length matrix yet!" << endl); | 
|---|
| 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 | } | 
|---|
| 173 | }; | 
|---|