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