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