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