| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [b70721] | 8 | /* | 
|---|
|  | 9 | * bondgraph.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Oct 29, 2009 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 21 |  | 
|---|
| [b70721] | 22 | #include <iostream> | 
|---|
|  | 23 |  | 
|---|
| [6f0841] | 24 | #include "Atom/atom.hpp" | 
|---|
| [129204] | 25 | #include "Bond/bond.hpp" | 
|---|
| [632508] | 26 | #include "Graph/BondGraph.hpp" | 
|---|
| [3738f0] | 27 | #include "Box.hpp" | 
|---|
| [3bdb6d] | 28 | #include "Element/element.hpp" | 
|---|
| [ad011c] | 29 | #include "CodePatterns/Info.hpp" | 
|---|
|  | 30 | #include "CodePatterns/Log.hpp" | 
|---|
| [3738f0] | 31 | #include "CodePatterns/Range.hpp" | 
|---|
|  | 32 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [b70721] | 33 | #include "molecule.hpp" | 
|---|
| [3bdb6d] | 34 | #include "Element/periodentafel.hpp" | 
|---|
| [a9b86d] | 35 | #include "Fragmentation/MatrixContainer.hpp" | 
|---|
| [57f243] | 36 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [3738f0] | 37 | #include "World.hpp" | 
|---|
|  | 38 | #include "WorldTime.hpp" | 
|---|
| [b70721] | 39 |  | 
|---|
| [88b400] | 40 | 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 | 
|---|
|  | 41 |  | 
|---|
| [f007a1] | 42 | BondGraph::BondGraph() : | 
|---|
|  | 43 | BondLengthMatrix(NULL), | 
|---|
|  | 44 | IsAngstroem(true) | 
|---|
|  | 45 | {} | 
|---|
|  | 46 |  | 
|---|
| [97b825] | 47 | BondGraph::BondGraph(bool IsA) : | 
|---|
|  | 48 | BondLengthMatrix(NULL), | 
|---|
|  | 49 | IsAngstroem(IsA) | 
|---|
| [3738f0] | 50 | {} | 
|---|
| [b70721] | 51 |  | 
|---|
|  | 52 | BondGraph::~BondGraph() | 
|---|
| [829761] | 53 | { | 
|---|
|  | 54 | CleanupBondLengthTable(); | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | void BondGraph::CleanupBondLengthTable() | 
|---|
| [b70721] | 58 | { | 
|---|
|  | 59 | if (BondLengthMatrix != NULL) { | 
|---|
|  | 60 | delete(BondLengthMatrix); | 
|---|
|  | 61 | } | 
|---|
| [3738f0] | 62 | } | 
|---|
| [b70721] | 63 |  | 
|---|
| [111f4a] | 64 | bool BondGraph::LoadBondLengthTable( | 
|---|
|  | 65 | std::istream &input) | 
|---|
| [b70721] | 66 | { | 
|---|
| [244a84] | 67 | Info FunctionInfo(__func__); | 
|---|
| [b70721] | 68 | bool status = true; | 
|---|
| [34e0013] | 69 | MatrixContainer *TempContainer = NULL; | 
|---|
| [b70721] | 70 |  | 
|---|
|  | 71 | // allocate MatrixContainer | 
|---|
|  | 72 | if (BondLengthMatrix != NULL) { | 
|---|
| [3738f0] | 73 | LOG(1, "MatrixContainer for Bond length already present, removing."); | 
|---|
| [b70721] | 74 | delete(BondLengthMatrix); | 
|---|
| [829761] | 75 | BondLengthMatrix = NULL; | 
|---|
| [b70721] | 76 | } | 
|---|
| [34e0013] | 77 | TempContainer = new MatrixContainer; | 
|---|
| [b70721] | 78 |  | 
|---|
|  | 79 | // parse in matrix | 
|---|
| [4e855e] | 80 | if ((input.good()) && (status = TempContainer->ParseMatrix(input, 0, 1, 0))) { | 
|---|
| [3738f0] | 81 | LOG(1, "Parsing bond length matrix successful."); | 
|---|
| [244a84] | 82 | } else { | 
|---|
| [47d041] | 83 | ELOG(1, "Parsing bond length matrix failed."); | 
|---|
| [4e855e] | 84 | status = false; | 
|---|
| [244a84] | 85 | } | 
|---|
| [b70721] | 86 |  | 
|---|
| [34e0013] | 87 | if (status) // set to not NULL only if matrix was parsed | 
|---|
|  | 88 | BondLengthMatrix = TempContainer; | 
|---|
|  | 89 | else { | 
|---|
|  | 90 | BondLengthMatrix = NULL; | 
|---|
|  | 91 | delete(TempContainer); | 
|---|
|  | 92 | } | 
|---|
| [b70721] | 93 | return status; | 
|---|
| [3738f0] | 94 | } | 
|---|
| [b70721] | 95 |  | 
|---|
| [300220] | 96 | double BondGraph::GetBondLength( | 
|---|
|  | 97 | int firstZ, | 
|---|
|  | 98 | int secondZ) const | 
|---|
| [b70721] | 99 | { | 
|---|
| [826e8c] | 100 | double return_length; | 
|---|
|  | 101 | if ((firstZ < 0) || (firstZ >= (int)BondLengthMatrix->Matrix[0].size())) | 
|---|
|  | 102 | return -1.; | 
|---|
|  | 103 | if ((secondZ < 0) || (secondZ >= (int)BondLengthMatrix->Matrix[0][firstZ].size())) | 
|---|
|  | 104 | return -1.; | 
|---|
| [4e855e] | 105 | if (BondLengthMatrix == NULL) { | 
|---|
| [826e8c] | 106 | return_length = -1.; | 
|---|
| [4e855e] | 107 | } else { | 
|---|
| [826e8c] | 108 | return_length = BondLengthMatrix->Matrix[0][firstZ][secondZ]; | 
|---|
| [4e855e] | 109 | } | 
|---|
| [826e8c] | 110 | LOG(4, "INFO: Request for length between " << firstZ << " and " | 
|---|
|  | 111 | << secondZ << ": " << return_length << "."); | 
|---|
|  | 112 | return return_length; | 
|---|
| [3738f0] | 113 | } | 
|---|
| [ae38fb] | 114 |  | 
|---|
| [607eab] | 115 | range<double> BondGraph::CovalentMinMaxDistance( | 
|---|
| [300220] | 116 | const element * const Walker, | 
|---|
| [607eab] | 117 | const element * const OtherWalker) const | 
|---|
| [b70721] | 118 | { | 
|---|
| [607eab] | 119 | range<double> MinMaxDistance(0.,0.); | 
|---|
| [300220] | 120 | MinMaxDistance.first = OtherWalker->getCovalentRadius() + Walker->getCovalentRadius(); | 
|---|
|  | 121 | MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
|  | 122 | MinMaxDistance.last = MinMaxDistance.first + BondThreshold; | 
|---|
|  | 123 | MinMaxDistance.first -= BondThreshold; | 
|---|
| [607eab] | 124 | return MinMaxDistance; | 
|---|
| [3738f0] | 125 | } | 
|---|
| [b70721] | 126 |  | 
|---|
| [607eab] | 127 | range<double> BondGraph::BondLengthMatrixMinMaxDistance( | 
|---|
| [300220] | 128 | const element * const Walker, | 
|---|
| [607eab] | 129 | const element * const OtherWalker) const | 
|---|
| [72d90e] | 130 | { | 
|---|
| [300220] | 131 | ASSERT(BondLengthMatrix, "BondGraph::BondLengthMatrixMinMaxDistance() called without NULL BondLengthMatrix."); | 
|---|
|  | 132 | ASSERT(Walker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal element given."); | 
|---|
|  | 133 | ASSERT(OtherWalker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal other element given."); | 
|---|
| [607eab] | 134 | range<double> MinMaxDistance(0.,0.); | 
|---|
| [300220] | 135 | MinMaxDistance.first = GetBondLength(Walker->getAtomicNumber()-1, OtherWalker->getAtomicNumber()-1); | 
|---|
|  | 136 | MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
|  | 137 | MinMaxDistance.last = MinMaxDistance.first + BondThreshold; | 
|---|
|  | 138 | MinMaxDistance.first -= BondThreshold; | 
|---|
| [607eab] | 139 | return MinMaxDistance; | 
|---|
| [3738f0] | 140 | } | 
|---|
| [72d90e] | 141 |  | 
|---|
| [607eab] | 142 | range<double> BondGraph::getMinMaxDistance( | 
|---|
| [300220] | 143 | const element * const Walker, | 
|---|
| [607eab] | 144 | const element * const OtherWalker) const | 
|---|
| [b70721] | 145 | { | 
|---|
| [607eab] | 146 | range<double> MinMaxDistance(0.,0.); | 
|---|
| [34e0013] | 147 | if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet | 
|---|
| [300220] | 148 | LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances."); | 
|---|
| [607eab] | 149 | MinMaxDistance = CovalentMinMaxDistance(Walker, OtherWalker); | 
|---|
| [b21a64] | 150 | } else { | 
|---|
| [300220] | 151 | LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances."); | 
|---|
| [607eab] | 152 | MinMaxDistance = BondLengthMatrixMinMaxDistance(Walker, OtherWalker); | 
|---|
| [b21a64] | 153 | } | 
|---|
| [607eab] | 154 | return MinMaxDistance; | 
|---|
| [72d90e] | 155 | } | 
|---|
| [3738f0] | 156 |  | 
|---|
| [607eab] | 157 | range<double> BondGraph::getMinMaxDistance( | 
|---|
| [300220] | 158 | const BondedParticle * const Walker, | 
|---|
| [607eab] | 159 | const BondedParticle * const OtherWalker) const | 
|---|
| [300220] | 160 | { | 
|---|
| [607eab] | 161 | return getMinMaxDistance(Walker->getType(), OtherWalker->getType()); | 
|---|
| [300220] | 162 | } | 
|---|
|  | 163 |  | 
|---|
| [607eab] | 164 | range<double> BondGraph::getMinMaxDistanceSquared( | 
|---|
| [300220] | 165 | const BondedParticle * const Walker, | 
|---|
| [607eab] | 166 | const BondedParticle * const OtherWalker) const | 
|---|
| [300220] | 167 | { | 
|---|
|  | 168 | // use non-squared version | 
|---|
| [607eab] | 169 | range<double> MinMaxDistance(getMinMaxDistance(Walker, OtherWalker)); | 
|---|
| [300220] | 170 | // and square | 
|---|
|  | 171 | MinMaxDistance.first *= MinMaxDistance.first; | 
|---|
|  | 172 | MinMaxDistance.last *= MinMaxDistance.last; | 
|---|
| [607eab] | 173 | return MinMaxDistance; | 
|---|
| [300220] | 174 | } | 
|---|
|  | 175 |  | 
|---|
| [826e8c] | 176 | LinkedCell::LinkedCell_View BondGraph::getLinkedCell(const double max_distance) const | 
|---|
| [3738f0] | 177 | { | 
|---|
| [826e8c] | 178 | return World::getInstance().getLinkedCell(max_distance); | 
|---|
|  | 179 | } | 
|---|
|  | 180 |  | 
|---|
|  | 181 | Box &BondGraph::getDomain() const | 
|---|
|  | 182 | { | 
|---|
|  | 183 | return World::getInstance().getDomain(); | 
|---|
|  | 184 | } | 
|---|
|  | 185 |  | 
|---|
|  | 186 | unsigned int BondGraph::getTime() const | 
|---|
|  | 187 | { | 
|---|
|  | 188 | return WorldTime::getTime(); | 
|---|
| [3738f0] | 189 | } | 
|---|
| [0cbad2] | 190 |  | 
|---|
| [9b6663] | 191 | bool BondGraph::operator==(const BondGraph &other) const | 
|---|
|  | 192 | { | 
|---|
|  | 193 | if (IsAngstroem != other.IsAngstroem) | 
|---|
|  | 194 | return false; | 
|---|
|  | 195 | if (((BondLengthMatrix != NULL) && (other.BondLengthMatrix == NULL)) | 
|---|
|  | 196 | || ((BondLengthMatrix == NULL) && (other.BondLengthMatrix != NULL))) | 
|---|
|  | 197 | return false; | 
|---|
|  | 198 | if ((BondLengthMatrix != NULL) && (other.BondLengthMatrix != NULL)) | 
|---|
|  | 199 | if (*BondLengthMatrix != *other.BondLengthMatrix) | 
|---|
|  | 200 | return false; | 
|---|
|  | 201 | return true; | 
|---|
|  | 202 | } | 
|---|