| 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 | BondGraph::BondGraph(bool IsA) : | 
|---|
| 39 | BondLengthMatrix(NULL), | 
|---|
| 40 | max_distance(0), | 
|---|
| 41 | IsAngstroem(IsA) | 
|---|
| 42 | {}; | 
|---|
| 43 |  | 
|---|
| 44 | BondGraph::~BondGraph() | 
|---|
| 45 | { | 
|---|
| 46 | if (BondLengthMatrix != NULL) { | 
|---|
| 47 | delete(BondLengthMatrix); | 
|---|
| 48 | } | 
|---|
| 49 | }; | 
|---|
| 50 |  | 
|---|
| 51 | bool BondGraph::LoadBondLengthTable(std::istream &input) | 
|---|
| 52 | { | 
|---|
| 53 | Info FunctionInfo(__func__); | 
|---|
| 54 | bool status = true; | 
|---|
| 55 | MatrixContainer *TempContainer = NULL; | 
|---|
| 56 |  | 
|---|
| 57 | // allocate MatrixContainer | 
|---|
| 58 | if (BondLengthMatrix != NULL) { | 
|---|
| 59 | DoLog(1) && (Log() << Verbose(1) << "MatrixContainer for Bond length already present, removing." << endl); | 
|---|
| 60 | delete(BondLengthMatrix); | 
|---|
| 61 | } | 
|---|
| 62 | TempContainer = new MatrixContainer; | 
|---|
| 63 |  | 
|---|
| 64 | // parse in matrix | 
|---|
| 65 | if ((input.good()) && (status = TempContainer->ParseMatrix(input, 0, 1, 0))) { | 
|---|
| 66 | DoLog(1) && (Log() << Verbose(1) << "Parsing bond length matrix successful." << endl); | 
|---|
| 67 | } else { | 
|---|
| 68 | DoeLog(1) && (eLog()<< Verbose(1) << "Parsing bond length matrix failed." << endl); | 
|---|
| 69 | status = false; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | // find greatest distance | 
|---|
| 73 | max_distance=0; | 
|---|
| 74 | if (status) { | 
|---|
| 75 | for(int i=0;i<TempContainer->RowCounter[0];i++) | 
|---|
| 76 | for(int j=i;j<TempContainer->ColumnCounter[0];j++) | 
|---|
| 77 | if (TempContainer->Matrix[0][i][j] > max_distance) | 
|---|
| 78 | max_distance = TempContainer->Matrix[0][i][j]; | 
|---|
| 79 | } | 
|---|
| 80 | max_distance += BondThreshold; | 
|---|
| 81 |  | 
|---|
| 82 | if (status) // set to not NULL only if matrix was parsed | 
|---|
| 83 | BondLengthMatrix = TempContainer; | 
|---|
| 84 | else { | 
|---|
| 85 | BondLengthMatrix = NULL; | 
|---|
| 86 | delete(TempContainer); | 
|---|
| 87 | } | 
|---|
| 88 | return status; | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 | bool BondGraph::CreateAdjacencyForMolecule(molecule * const mol) | 
|---|
| 92 | { | 
|---|
| 93 | Info FunctionInfo(__func__); | 
|---|
| 94 | bool status = true; | 
|---|
| 95 |  | 
|---|
| 96 | if (mol->empty()) // only construct if molecule is not empty | 
|---|
| 97 | return false; | 
|---|
| 98 |  | 
|---|
| 99 | SetMaxDistanceToMaxOfCovalentRadii(mol); | 
|---|
| 100 | mol->CreateAdjacencyList(max_distance, IsAngstroem, &BondGraph::getMinMaxDistance, this); | 
|---|
| 101 |  | 
|---|
| 102 | return status; | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | double BondGraph::GetBondLength(int firstZ, int secondZ) | 
|---|
| 106 | { | 
|---|
| 107 | std::cout << "Request for length between " << firstZ << " and " << secondZ << ": "; | 
|---|
| 108 | if (BondLengthMatrix == NULL) { | 
|---|
| 109 | std::cout << "-1." << std::endl; | 
|---|
| 110 | return( -1. ); | 
|---|
| 111 | } else { | 
|---|
| 112 | std::cout << BondLengthMatrix->Matrix[0][firstZ][secondZ] << std::endl; | 
|---|
| 113 | return (BondLengthMatrix->Matrix[0][firstZ][secondZ]); | 
|---|
| 114 | } | 
|---|
| 115 | }; | 
|---|
| 116 |  | 
|---|
| 117 | double BondGraph::SetMaxDistanceToMaxOfCovalentRadii(const molecule * const mol) | 
|---|
| 118 | { | 
|---|
| 119 | Info FunctionInfo(__func__); | 
|---|
| 120 | max_distance = 0.; | 
|---|
| 121 |  | 
|---|
| 122 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) { | 
|---|
| 123 | if ((*iter)->getType()->getCovalentRadius() > max_distance) | 
|---|
| 124 | max_distance = (*iter)->getType()->getCovalentRadius(); | 
|---|
| 125 | } | 
|---|
| 126 | max_distance *= 2.; | 
|---|
| 127 | max_distance += BondThreshold; | 
|---|
| 128 |  | 
|---|
| 129 | return max_distance; | 
|---|
| 130 | }; | 
|---|
| 131 |  | 
|---|
| 132 | double BondGraph::getMaxDistance() const | 
|---|
| 133 | { | 
|---|
| 134 | return max_distance; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 | void BondGraph::CovalentMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
| 139 | { | 
|---|
| 140 | MinDistance = OtherWalker->getType()->getCovalentRadius() + Walker->getType()->getCovalentRadius(); | 
|---|
| 141 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
| 142 | MaxDistance = MinDistance + BondThreshold; | 
|---|
| 143 | MinDistance -= BondThreshold; | 
|---|
| 144 | }; | 
|---|
| 145 |  | 
|---|
| 146 | void BondGraph::BondLengthMatrixMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
| 147 | { | 
|---|
| 148 | ASSERT(BondLengthMatrix != NULL, | 
|---|
| 149 | "BondGraph::BondLengthMatrixMinMaxDistance() called without NULL BondLengthMatrix."); | 
|---|
| 150 | MinDistance = GetBondLength(Walker->getType()->getAtomicNumber()-1, OtherWalker->getType()->getAtomicNumber()-1); | 
|---|
| 151 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
| 152 | MaxDistance = MinDistance + BondThreshold; | 
|---|
| 153 | MinDistance -= BondThreshold; | 
|---|
| 154 | }; | 
|---|
| 155 |  | 
|---|
| 156 | void BondGraph::getMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem) | 
|---|
| 157 | { | 
|---|
| 158 | if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet | 
|---|
| 159 | LOG(2, "INFO: Using Covalent radii criterion for [min,max] distances."); | 
|---|
| 160 | CovalentMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem); | 
|---|
| 161 | } else { | 
|---|
| 162 | LOG(2, "INFO: Using Covalent radii criterion for [min,max] distances."); | 
|---|
| 163 | BondLengthMatrixMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem); | 
|---|
| 164 | } | 
|---|
| 165 | } | 
|---|