| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2011 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/bond.hpp" | 
|---|
| 26 | #include "Graph/BondGraph.hpp" | 
|---|
| 27 | #include "Box.hpp" | 
|---|
| 28 | #include "Element/element.hpp" | 
|---|
| 29 | #include "CodePatterns/Info.hpp" | 
|---|
| 30 | #include "CodePatterns/Log.hpp" | 
|---|
| 31 | #include "CodePatterns/Range.hpp" | 
|---|
| 32 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 33 | #include "molecule.hpp" | 
|---|
| 34 | #include "Element/periodentafel.hpp" | 
|---|
| 35 | #include "Fragmentation/MatrixContainer.hpp" | 
|---|
| 36 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 37 | #include "World.hpp" | 
|---|
| 38 | #include "WorldTime.hpp" | 
|---|
| 39 |  | 
|---|
| 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 |  | 
|---|
| 42 | BondGraph::BondGraph() : | 
|---|
| 43 | BondLengthMatrix(NULL), | 
|---|
| 44 | IsAngstroem(true) | 
|---|
| 45 | {} | 
|---|
| 46 |  | 
|---|
| 47 | BondGraph::BondGraph(bool IsA) : | 
|---|
| 48 | BondLengthMatrix(NULL), | 
|---|
| 49 | IsAngstroem(IsA) | 
|---|
| 50 | {} | 
|---|
| 51 |  | 
|---|
| 52 | BondGraph::~BondGraph() | 
|---|
| 53 | { | 
|---|
| 54 | CleanupBondLengthTable(); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | void BondGraph::CleanupBondLengthTable() | 
|---|
| 58 | { | 
|---|
| 59 | if (BondLengthMatrix != NULL) { | 
|---|
| 60 | delete(BondLengthMatrix); | 
|---|
| 61 | } | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | bool BondGraph::LoadBondLengthTable( | 
|---|
| 65 | std::istream &input) | 
|---|
| 66 | { | 
|---|
| 67 | Info FunctionInfo(__func__); | 
|---|
| 68 | bool status = true; | 
|---|
| 69 | MatrixContainer *TempContainer = NULL; | 
|---|
| 70 |  | 
|---|
| 71 | // allocate MatrixContainer | 
|---|
| 72 | if (BondLengthMatrix != NULL) { | 
|---|
| 73 | LOG(1, "MatrixContainer for Bond length already present, removing."); | 
|---|
| 74 | delete(BondLengthMatrix); | 
|---|
| 75 | BondLengthMatrix = NULL; | 
|---|
| 76 | } | 
|---|
| 77 | TempContainer = new MatrixContainer; | 
|---|
| 78 |  | 
|---|
| 79 | // parse in matrix | 
|---|
| 80 | if ((input.good()) && (status = TempContainer->ParseMatrix(input, 0, 1, 0))) { | 
|---|
| 81 | LOG(1, "Parsing bond length matrix successful."); | 
|---|
| 82 | } else { | 
|---|
| 83 | ELOG(1, "Parsing bond length matrix failed."); | 
|---|
| 84 | status = false; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | if (status) // set to not NULL only if matrix was parsed | 
|---|
| 88 | BondLengthMatrix = TempContainer; | 
|---|
| 89 | else { | 
|---|
| 90 | BondLengthMatrix = NULL; | 
|---|
| 91 | delete(TempContainer); | 
|---|
| 92 | } | 
|---|
| 93 | return status; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | double BondGraph::GetBondLength( | 
|---|
| 97 | int firstZ, | 
|---|
| 98 | int secondZ) const | 
|---|
| 99 | { | 
|---|
| 100 | std::cout << "Request for length between " << firstZ << " and " << secondZ << ": "; | 
|---|
| 101 | if (BondLengthMatrix == NULL) { | 
|---|
| 102 | std::cout << "-1." << std::endl; | 
|---|
| 103 | return( -1. ); | 
|---|
| 104 | } else { | 
|---|
| 105 | std::cout << BondLengthMatrix->Matrix[0][firstZ][secondZ] << std::endl; | 
|---|
| 106 | return (BondLengthMatrix->Matrix[0][firstZ][secondZ]); | 
|---|
| 107 | } | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | range<double> BondGraph::CovalentMinMaxDistance( | 
|---|
| 111 | const element * const Walker, | 
|---|
| 112 | const element * const OtherWalker) const | 
|---|
| 113 | { | 
|---|
| 114 | range<double> MinMaxDistance(0.,0.); | 
|---|
| 115 | MinMaxDistance.first = OtherWalker->getCovalentRadius() + Walker->getCovalentRadius(); | 
|---|
| 116 | MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
| 117 | MinMaxDistance.last = MinMaxDistance.first + BondThreshold; | 
|---|
| 118 | MinMaxDistance.first -= BondThreshold; | 
|---|
| 119 | return MinMaxDistance; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | range<double> BondGraph::BondLengthMatrixMinMaxDistance( | 
|---|
| 123 | const element * const Walker, | 
|---|
| 124 | const element * const OtherWalker) const | 
|---|
| 125 | { | 
|---|
| 126 | ASSERT(BondLengthMatrix, "BondGraph::BondLengthMatrixMinMaxDistance() called without NULL BondLengthMatrix."); | 
|---|
| 127 | ASSERT(Walker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal element given."); | 
|---|
| 128 | ASSERT(OtherWalker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal other element given."); | 
|---|
| 129 | range<double> MinMaxDistance(0.,0.); | 
|---|
| 130 | MinMaxDistance.first = GetBondLength(Walker->getAtomicNumber()-1, OtherWalker->getAtomicNumber()-1); | 
|---|
| 131 | MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem; | 
|---|
| 132 | MinMaxDistance.last = MinMaxDistance.first + BondThreshold; | 
|---|
| 133 | MinMaxDistance.first -= BondThreshold; | 
|---|
| 134 | return MinMaxDistance; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | range<double> BondGraph::getMinMaxDistance( | 
|---|
| 138 | const element * const Walker, | 
|---|
| 139 | const element * const OtherWalker) const | 
|---|
| 140 | { | 
|---|
| 141 | range<double> MinMaxDistance(0.,0.); | 
|---|
| 142 | if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet | 
|---|
| 143 | LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances."); | 
|---|
| 144 | MinMaxDistance = CovalentMinMaxDistance(Walker, OtherWalker); | 
|---|
| 145 | } else { | 
|---|
| 146 | LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances."); | 
|---|
| 147 | MinMaxDistance = BondLengthMatrixMinMaxDistance(Walker, OtherWalker); | 
|---|
| 148 | } | 
|---|
| 149 | return MinMaxDistance; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | range<double> BondGraph::getMinMaxDistance( | 
|---|
| 153 | const BondedParticle * const Walker, | 
|---|
| 154 | const BondedParticle * const OtherWalker) const | 
|---|
| 155 | { | 
|---|
| 156 | return getMinMaxDistance(Walker->getType(), OtherWalker->getType()); | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | range<double> BondGraph::getMinMaxDistanceSquared( | 
|---|
| 160 | const BondedParticle * const Walker, | 
|---|
| 161 | const BondedParticle * const OtherWalker) const | 
|---|
| 162 | { | 
|---|
| 163 | // use non-squared version | 
|---|
| 164 | range<double> MinMaxDistance(getMinMaxDistance(Walker, OtherWalker)); | 
|---|
| 165 | // and square | 
|---|
| 166 | MinMaxDistance.first *= MinMaxDistance.first; | 
|---|
| 167 | MinMaxDistance.last *= MinMaxDistance.last; | 
|---|
| 168 | return MinMaxDistance; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | void BondGraph::CreateAdjacency(LinkedCell &LC) const | 
|---|
| 172 | { | 
|---|
| 173 | atom *Walker = NULL; | 
|---|
| 174 | atom *OtherWalker = NULL; | 
|---|
| 175 | int n[NDIM]; | 
|---|
| 176 | Box &domain = World::getInstance().getDomain(); | 
|---|
| 177 | size_t CurrentTime = WorldTime::getTime(); | 
|---|
| 178 |  | 
|---|
| 179 | unsigned int BondCount = 0; | 
|---|
| 180 | // 3a. go through every cell | 
|---|
| 181 | LOG(3, "INFO: Celling ... "); | 
|---|
| 182 | for (LC.n[0] = 0; LC.n[0] < LC.N[0]; LC.n[0]++) | 
|---|
| 183 | for (LC.n[1] = 0; LC.n[1] < LC.N[1]; LC.n[1]++) | 
|---|
| 184 | for (LC.n[2] = 0; LC.n[2] < LC.N[2]; LC.n[2]++) { | 
|---|
| 185 | const TesselPointSTLList *List = LC.GetCurrentCell(); | 
|---|
| 186 | LOG(2, "Current cell is " << LC.n[0] << ", " << LC.n[1] << ", " << LC.n[2] << " with No. " << LC.index << " containing " << List->size() << " points."); | 
|---|
| 187 | if (List != NULL) { | 
|---|
| 188 | for (TesselPointSTLList::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 189 | Walker = dynamic_cast<atom*>(*Runner); | 
|---|
| 190 | ASSERT(Walker != NULL, | 
|---|
| 191 | "BondGraph::CreateAdjacency() - Tesselpoint that was not an atom retrieved from LinkedNode"); | 
|---|
| 192 | LOG(2, "INFO: Current Atom is " << *Walker << "."); | 
|---|
| 193 | // 3c. check for possible bond between each atom in this and every one in the 27 cells | 
|---|
| 194 | for (n[0] = -1; n[0] <= 1; n[0]++) | 
|---|
| 195 | for (n[1] = -1; n[1] <= 1; n[1]++) | 
|---|
| 196 | for (n[2] = -1; n[2] <= 1; n[2]++) { | 
|---|
| 197 | const TesselPointSTLList *OtherList = LC.GetRelativeToCurrentCell(n); | 
|---|
| 198 | if (OtherList != NULL) { | 
|---|
| 199 | LOG(3, "INFO: Current relative cell is " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << LC.index << " containing " << List->size() << " points."); | 
|---|
| 200 | for (TesselPointSTLList::const_iterator OtherRunner = OtherList->begin(); OtherRunner != OtherList->end(); OtherRunner++) { | 
|---|
| 201 | if ((*OtherRunner) > Walker) {  // just to not add bonds from both sides | 
|---|
| 202 | OtherWalker = dynamic_cast<atom*>(*OtherRunner); | 
|---|
| 203 | ASSERT(OtherWalker != NULL, | 
|---|
| 204 | "BondGraph::CreateAdjacency() - TesselPoint that was not an atom retrieved from LinkedNode"); | 
|---|
| 205 | LOG(3, "INFO: Current other atom is " << *OtherWalker << "."); | 
|---|
| 206 | if (OtherWalker->father > Walker->father ) { // just to not add bonds from both sides | 
|---|
| 207 | const range<double> MinMaxDistanceSquared( | 
|---|
| 208 | getMinMaxDistanceSquared(Walker, OtherWalker)); | 
|---|
| 209 | const double distance = domain.periodicDistanceSquared(OtherWalker->getPosition(),Walker->getPosition()); | 
|---|
| 210 | LOG(3, "INFO: Checking squared distance " << distance << " against typical bond length of " << MinMaxDistanceSquared << "."); | 
|---|
| 211 | const bool status = MinMaxDistanceSquared.isInRange(distance); | 
|---|
| 212 | if (status) { // create bond if distance is smaller | 
|---|
| 213 | LOG(1, "ACCEPT: Adding Bond between " << *Walker << " and " << *OtherWalker << " in distance " << sqrt(distance) << "."); | 
|---|
| 214 | //const bond * Binder = | 
|---|
| 215 | Walker->father->addBond(CurrentTime, OtherWalker->father); | 
|---|
| 216 | BondCount++; | 
|---|
| 217 | } else { | 
|---|
| 218 | LOG(2, "REJECT: Squared distance " | 
|---|
| 219 | << distance << " is out of squared covalent bounds " | 
|---|
| 220 | << MinMaxDistanceSquared << "."); | 
|---|
| 221 | } | 
|---|
| 222 | } else { | 
|---|
| 223 | LOG(5, "REJECT: Not Adding: Wrong order of father's."); | 
|---|
| 224 | } | 
|---|
| 225 | } else { | 
|---|
| 226 | LOG(5, "REJECT: Not Adding: Wrong order."); | 
|---|
| 227 | } | 
|---|
| 228 | } | 
|---|
| 229 | } | 
|---|
| 230 | } | 
|---|
| 231 | } | 
|---|
| 232 | } | 
|---|
| 233 | } | 
|---|
| 234 | LOG(1, "I detected " << BondCount << " bonds in the molecule."); | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | bool BondGraph::operator==(const BondGraph &other) const | 
|---|
| 238 | { | 
|---|
| 239 | if (IsAngstroem != other.IsAngstroem) | 
|---|
| 240 | return false; | 
|---|
| 241 | if (((BondLengthMatrix != NULL) && (other.BondLengthMatrix == NULL)) | 
|---|
| 242 | || ((BondLengthMatrix == NULL) && (other.BondLengthMatrix != NULL))) | 
|---|
| 243 | return false; | 
|---|
| 244 | if ((BondLengthMatrix != NULL) && (other.BondLengthMatrix != NULL)) | 
|---|
| 245 | if (*BondLengthMatrix != *other.BondLengthMatrix) | 
|---|
| 246 | return false; | 
|---|
| 247 | return true; | 
|---|
| 248 | } | 
|---|