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