| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [b70721] | 8 | /* | 
|---|
|  | 9 | * bondgraph.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Oct 29, 2009 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [112b09] | 20 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 21 |  | 
|---|
| [b70721] | 22 | #include <iostream> | 
|---|
|  | 23 |  | 
|---|
|  | 24 | #include "atom.hpp" | 
|---|
| [1cbf47] | 25 | #include "bond.hpp" | 
|---|
| [b70721] | 26 | #include "bondgraph.hpp" | 
|---|
|  | 27 | #include "element.hpp" | 
|---|
| [952f38] | 28 | #include "Helpers/Info.hpp" | 
|---|
|  | 29 | #include "Helpers/Verbose.hpp" | 
|---|
|  | 30 | #include "Helpers/Log.hpp" | 
|---|
| [b70721] | 31 | #include "molecule.hpp" | 
|---|
|  | 32 | #include "parser.hpp" | 
|---|
| [ae38fb] | 33 | #include "periodentafel.hpp" | 
|---|
| [57f243] | 34 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [b70721] | 35 |  | 
|---|
| [88b400] | 36 | const double BondGraph::BondThreshold = 0.4;   //!< CSD threshold in bond check which is the width of the interval whose center is the sum of the covalent radii | 
|---|
|  | 37 |  | 
|---|
| [b70721] | 38 | /** Constructor of class BondGraph. | 
|---|
|  | 39 | * This classes contains typical bond lengths and thus may be used to construct a bond graph for a given molecule. | 
|---|
|  | 40 | */ | 
|---|
| [97b825] | 41 | BondGraph::BondGraph(bool IsA) : | 
|---|
|  | 42 | BondLengthMatrix(NULL), | 
|---|
|  | 43 | max_distance(0), | 
|---|
|  | 44 | IsAngstroem(IsA) | 
|---|
|  | 45 | {}; | 
|---|
| [b70721] | 46 |  | 
|---|
|  | 47 | /** Destructor of class BondGraph. | 
|---|
|  | 48 | */ | 
|---|
|  | 49 | BondGraph::~BondGraph() | 
|---|
|  | 50 | { | 
|---|
|  | 51 | if (BondLengthMatrix != NULL) { | 
|---|
|  | 52 | delete(BondLengthMatrix); | 
|---|
|  | 53 | } | 
|---|
|  | 54 | }; | 
|---|
|  | 55 |  | 
|---|
|  | 56 | /** Parses the bond lengths in a given file and puts them int a matrix form. | 
|---|
| [34e0013] | 57 | * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(), | 
|---|
| [b998c3] | 58 | * but only if parsing is successful. Otherwise variable is left as NULL. | 
|---|
| [b70721] | 59 | * \param *out output stream for debugging | 
|---|
|  | 60 | * \param filename file with bond lengths to parse | 
|---|
|  | 61 | * \return true - success in parsing file, false - failed to parse the file | 
|---|
|  | 62 | */ | 
|---|
| [e138de] | 63 | bool BondGraph::LoadBondLengthTable(const string &filename) | 
|---|
| [b70721] | 64 | { | 
|---|
| [244a84] | 65 | Info FunctionInfo(__func__); | 
|---|
| [b70721] | 66 | bool status = true; | 
|---|
| [34e0013] | 67 | MatrixContainer *TempContainer = NULL; | 
|---|
| [b70721] | 68 |  | 
|---|
|  | 69 | // allocate MatrixContainer | 
|---|
|  | 70 | if (BondLengthMatrix != NULL) { | 
|---|
| [a67d19] | 71 | DoLog(1) && (Log() << Verbose(1) << "MatrixContainer for Bond length already present, removing." << endl); | 
|---|
| [b70721] | 72 | delete(BondLengthMatrix); | 
|---|
|  | 73 | } | 
|---|
| [34e0013] | 74 | TempContainer = new MatrixContainer; | 
|---|
| [b70721] | 75 |  | 
|---|
|  | 76 | // parse in matrix | 
|---|
| [a26ca5] | 77 | if ((status = TempContainer->ParseMatrix(filename.c_str(), 0, 1, 0))) { | 
|---|
| [a67d19] | 78 | DoLog(1) && (Log() << Verbose(1) << "Parsing bond length matrix successful." << endl); | 
|---|
| [244a84] | 79 | } else { | 
|---|
| [58ed4a] | 80 | DoeLog(1) && (eLog()<< Verbose(1) << "Parsing bond length matrix failed." << endl); | 
|---|
| [244a84] | 81 | } | 
|---|
| [b70721] | 82 |  | 
|---|
|  | 83 | // find greatest distance | 
|---|
|  | 84 | max_distance=0; | 
|---|
| [34e0013] | 85 | if (status) { | 
|---|
|  | 86 | for(int i=0;i<TempContainer->RowCounter[0];i++) | 
|---|
|  | 87 | for(int j=i;j<TempContainer->ColumnCounter[0];j++) | 
|---|
|  | 88 | if (TempContainer->Matrix[0][i][j] > max_distance) | 
|---|
|  | 89 | max_distance = TempContainer->Matrix[0][i][j]; | 
|---|
|  | 90 | } | 
|---|
| [b70721] | 91 |  | 
|---|
| [34e0013] | 92 | if (status) // set to not NULL only if matrix was parsed | 
|---|
|  | 93 | BondLengthMatrix = TempContainer; | 
|---|
|  | 94 | else { | 
|---|
|  | 95 | BondLengthMatrix = NULL; | 
|---|
|  | 96 | delete(TempContainer); | 
|---|
|  | 97 | } | 
|---|
| [b70721] | 98 | return status; | 
|---|
|  | 99 | }; | 
|---|
|  | 100 |  | 
|---|
|  | 101 | /** Parses the bond lengths in a given file and puts them int a matrix form. | 
|---|
|  | 102 | * \param *out output stream for debugging | 
|---|
|  | 103 | * \param *mol molecule with atoms | 
|---|
|  | 104 | * \return true - success, false - failed to construct bond structure | 
|---|
|  | 105 | */ | 
|---|
| [e138de] | 106 | bool BondGraph::ConstructBondGraph(molecule * const mol) | 
|---|
| [b70721] | 107 | { | 
|---|
| [1cbf47] | 108 | Info FunctionInfo(__func__); | 
|---|
| [bd6bfa] | 109 | bool status = true; | 
|---|
| [b70721] | 110 |  | 
|---|
| [9879f6] | 111 | if (mol->empty()) // only construct if molecule is not empty | 
|---|
| [34e0013] | 112 | return false; | 
|---|
|  | 113 |  | 
|---|
| [3c349b] | 114 | if (BondLengthMatrix == NULL) { // no bond length matrix parsed? | 
|---|
| [e138de] | 115 | SetMaxDistanceToMaxOfCovalentRadii(mol); | 
|---|
|  | 116 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::CovalentMinMaxDistance, this); | 
|---|
| [3c349b] | 117 | } else | 
|---|
| [e138de] | 118 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::BondLengthMatrixMinMaxDistance, this); | 
|---|
| [b70721] | 119 |  | 
|---|
|  | 120 | return status; | 
|---|
|  | 121 | }; | 
|---|
|  | 122 |  | 
|---|
|  | 123 | /** Returns the entry for a given index pair. | 
|---|
|  | 124 | * \param firstelement index/atom number of first element (row index) | 
|---|
|  | 125 | * \param secondelement index/atom number of second element (column index) | 
|---|
|  | 126 | * \note matrix is of course symmetric. | 
|---|
|  | 127 | */ | 
|---|
|  | 128 | double BondGraph::GetBondLength(int firstZ, int secondZ) | 
|---|
|  | 129 | { | 
|---|
| [34e0013] | 130 | if (BondLengthMatrix == NULL) | 
|---|
|  | 131 | return( -1. ); | 
|---|
|  | 132 | else | 
|---|
|  | 133 | return (BondLengthMatrix->Matrix[0][firstZ][secondZ]); | 
|---|
| [b70721] | 134 | }; | 
|---|
|  | 135 |  | 
|---|
| [3c349b] | 136 | /** Determines the maximum of all element::CovalentRadius for elements present in \a *mol. | 
|---|
| [ae38fb] | 137 | * \param *out output stream for debugging | 
|---|
| [3c349b] | 138 | * \param *mol molecule with all atoms and their respective elements. | 
|---|
| [ae38fb] | 139 | */ | 
|---|
| [e138de] | 140 | double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(const molecule * const mol) | 
|---|
| [ae38fb] | 141 | { | 
|---|
| [1cbf47] | 142 | Info FunctionInfo(__func__); | 
|---|
| [ae38fb] | 143 | max_distance = 0.; | 
|---|
|  | 144 |  | 
|---|
| [9879f6] | 145 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) { | 
|---|
| [83f176] | 146 | if ((*iter)->getType()->getCovalentRadius() > max_distance) | 
|---|
|  | 147 | max_distance = (*iter)->getType()->getCovalentRadius(); | 
|---|
| [ae38fb] | 148 | } | 
|---|
|  | 149 | max_distance *= 2.; | 
|---|
|  | 150 |  | 
|---|
|  | 151 | return max_distance; | 
|---|
|  | 152 | }; | 
|---|
|  | 153 |  | 
|---|
| [b70721] | 154 | /** Returns bond criterion for given pair based on covalent radius. | 
|---|
|  | 155 | * \param *Walker first BondedParticle | 
|---|
|  | 156 | * \param *OtherWalker second BondedParticle | 
|---|
|  | 157 | * \param &MinDistance lower bond bound on return | 
|---|
|  | 158 | * \param &MaxDistance upper bond bound on return | 
|---|
|  | 159 | * \param IsAngstroem whether units are in angstroem or bohr radii | 
|---|
|  | 160 | */ | 
|---|
|  | 161 | void BondGraph::CovalentMinMaxDistance(BondedParticle * const Walker, BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
|  | 162 | { | 
|---|
| [83f176] | 163 | MinDistance = OtherWalker->getType()->getCovalentRadius() + Walker->getType()->getCovalentRadius(); | 
|---|
| [b70721] | 164 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
| [88b400] | 165 | MaxDistance = MinDistance + BondThreshold; | 
|---|
|  | 166 | MinDistance -= BondThreshold; | 
|---|
| [b70721] | 167 | }; | 
|---|
|  | 168 |  | 
|---|
|  | 169 | /** Returns bond criterion for given pair based on a bond length matrix. | 
|---|
|  | 170 | * The matrix should be contained in \a this BondGraph and contain an element- | 
|---|
|  | 171 | * to-element length. | 
|---|
|  | 172 | * \param *Walker first BondedParticle | 
|---|
|  | 173 | * \param *OtherWalker second BondedParticle | 
|---|
|  | 174 | * \param &MinDistance lower bond bound on return | 
|---|
|  | 175 | * \param &MaxDistance upper bond bound on return | 
|---|
|  | 176 | * \param IsAngstroem whether units are in angstroem or bohr radii | 
|---|
|  | 177 | */ | 
|---|
|  | 178 | void BondGraph::BondLengthMatrixMinMaxDistance(BondedParticle * const Walker, BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
|  | 179 | { | 
|---|
| [34e0013] | 180 | if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet | 
|---|
| [58ed4a] | 181 | DoeLog(2) && (eLog()<< Verbose(2) << "BondLengthMatrixMinMaxDistance() called without having parsed the bond length matrix yet!" << endl); | 
|---|
| [b21a64] | 182 | CovalentMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem); | 
|---|
|  | 183 | } else { | 
|---|
| [83f176] | 184 | MinDistance = GetBondLength(Walker->getType()->getAtomicNumber()-1, OtherWalker->getType()->getAtomicNumber()-1); | 
|---|
| [b21a64] | 185 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
| [88b400] | 186 | MaxDistance = MinDistance + BondThreshold; | 
|---|
|  | 187 | MinDistance -= BondThreshold; | 
|---|
| [b21a64] | 188 | } | 
|---|
| [b70721] | 189 | }; | 
|---|