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