| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 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/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 |   if (BondLengthMatrix == NULL) {
 | 
|---|
| 101 |     std::cout << "-1." << std::endl;
 | 
|---|
| 102 |     return( -1. );
 | 
|---|
| 103 |   } else {
 | 
|---|
| 104 |     std::cout << BondLengthMatrix->Matrix[0][firstZ][secondZ] << std::endl;
 | 
|---|
| 105 |     return (BondLengthMatrix->Matrix[0][firstZ][secondZ]);
 | 
|---|
| 106 |   }
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | range<double> BondGraph::CovalentMinMaxDistance(
 | 
|---|
| 110 |     const element * const Walker,
 | 
|---|
| 111 |     const element * const OtherWalker) const
 | 
|---|
| 112 | {
 | 
|---|
| 113 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 114 |   MinMaxDistance.first = OtherWalker->getCovalentRadius() + Walker->getCovalentRadius();
 | 
|---|
| 115 |   MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
 | 
|---|
| 116 |   MinMaxDistance.last = MinMaxDistance.first + BondThreshold;
 | 
|---|
| 117 |   MinMaxDistance.first -= BondThreshold;
 | 
|---|
| 118 |   return MinMaxDistance;
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | range<double> BondGraph::BondLengthMatrixMinMaxDistance(
 | 
|---|
| 122 |     const element * const Walker,
 | 
|---|
| 123 |     const element * const OtherWalker) const
 | 
|---|
| 124 | {
 | 
|---|
| 125 |   ASSERT(BondLengthMatrix, "BondGraph::BondLengthMatrixMinMaxDistance() called without NULL BondLengthMatrix.");
 | 
|---|
| 126 |   ASSERT(Walker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal element given.");
 | 
|---|
| 127 |   ASSERT(OtherWalker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal other element given.");
 | 
|---|
| 128 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 129 |   MinMaxDistance.first = GetBondLength(Walker->getAtomicNumber()-1, OtherWalker->getAtomicNumber()-1);
 | 
|---|
| 130 |   MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
 | 
|---|
| 131 |   MinMaxDistance.last = MinMaxDistance.first + BondThreshold;
 | 
|---|
| 132 |   MinMaxDistance.first -= BondThreshold;
 | 
|---|
| 133 |   return MinMaxDistance;
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | range<double> BondGraph::getMinMaxDistance(
 | 
|---|
| 137 |     const element * const Walker,
 | 
|---|
| 138 |     const element * const OtherWalker) const
 | 
|---|
| 139 | {
 | 
|---|
| 140 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 141 |   if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet
 | 
|---|
| 142 |     LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances.");
 | 
|---|
| 143 |     MinMaxDistance = CovalentMinMaxDistance(Walker, OtherWalker);
 | 
|---|
| 144 |   } else {
 | 
|---|
| 145 |     LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances.");
 | 
|---|
| 146 |     MinMaxDistance = BondLengthMatrixMinMaxDistance(Walker, OtherWalker);
 | 
|---|
| 147 |   }
 | 
|---|
| 148 |   return MinMaxDistance;
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | range<double> BondGraph::getMinMaxDistance(
 | 
|---|
| 152 |     const BondedParticle * const Walker,
 | 
|---|
| 153 |     const BondedParticle * const OtherWalker) const
 | 
|---|
| 154 | {
 | 
|---|
| 155 |   return getMinMaxDistance(Walker->getType(), OtherWalker->getType());
 | 
|---|
| 156 | }
 | 
|---|
| 157 | 
 | 
|---|
| 158 | range<double> BondGraph::getMinMaxDistanceSquared(
 | 
|---|
| 159 |     const BondedParticle * const Walker,
 | 
|---|
| 160 |     const BondedParticle * const OtherWalker) const
 | 
|---|
| 161 | {
 | 
|---|
| 162 |   // use non-squared version
 | 
|---|
| 163 |   range<double> MinMaxDistance(getMinMaxDistance(Walker, OtherWalker));
 | 
|---|
| 164 |   // and square
 | 
|---|
| 165 |   MinMaxDistance.first *= MinMaxDistance.first;
 | 
|---|
| 166 |   MinMaxDistance.last *= MinMaxDistance.last;
 | 
|---|
| 167 |   return MinMaxDistance;
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | void BondGraph::CreateAdjacency(LinkedCell_deprecated &LC) const
 | 
|---|
| 171 | {
 | 
|---|
| 172 |   atom *Walker = NULL;
 | 
|---|
| 173 |   atom *OtherWalker = NULL;
 | 
|---|
| 174 |   int n[NDIM];
 | 
|---|
| 175 |   Box &domain = World::getInstance().getDomain();
 | 
|---|
| 176 |   size_t CurrentTime = WorldTime::getTime();
 | 
|---|
| 177 | 
 | 
|---|
| 178 |   unsigned int BondCount = 0;
 | 
|---|
| 179 |   // 3a. go through every cell
 | 
|---|
| 180 |   LOG(3, "INFO: Celling ... ");
 | 
|---|
| 181 |   for (LC.n[0] = 0; LC.n[0] < LC.N[0]; LC.n[0]++)
 | 
|---|
| 182 |     for (LC.n[1] = 0; LC.n[1] < LC.N[1]; LC.n[1]++)
 | 
|---|
| 183 |       for (LC.n[2] = 0; LC.n[2] < LC.N[2]; LC.n[2]++) {
 | 
|---|
| 184 |         const TesselPointSTLList *List = LC.GetCurrentCell();
 | 
|---|
| 185 |         LOG(2, "Current cell is " << LC.n[0] << ", " << LC.n[1] << ", " << LC.n[2] << " with No. " << LC.index << " containing " << List->size() << " points.");
 | 
|---|
| 186 |         if (List != NULL) {
 | 
|---|
| 187 |           for (TesselPointSTLList::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
| 188 |             Walker = dynamic_cast<atom*>(*Runner);
 | 
|---|
| 189 |             ASSERT(Walker != NULL,
 | 
|---|
| 190 |                 "BondGraph::CreateAdjacency() - Tesselpoint that was not an atom retrieved from LinkedNode");
 | 
|---|
| 191 |             LOG(2, "INFO: Current Atom is " << *Walker << ".");
 | 
|---|
| 192 |             // 3c. check for possible bond between each atom in this and every one in the 27 cells
 | 
|---|
| 193 |             for (n[0] = -1; n[0] <= 1; n[0]++)
 | 
|---|
| 194 |               for (n[1] = -1; n[1] <= 1; n[1]++)
 | 
|---|
| 195 |                 for (n[2] = -1; n[2] <= 1; n[2]++) {
 | 
|---|
| 196 |                   const TesselPointSTLList *OtherList = LC.GetRelativeToCurrentCell(n);
 | 
|---|
| 197 |                   if (OtherList != NULL) {
 | 
|---|
| 198 |                     LOG(3, "INFO: Current relative cell is " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << LC.index << " containing " << OtherList->size() << " points.");
 | 
|---|
| 199 |                     for (TesselPointSTLList::const_iterator OtherRunner = OtherList->begin(); OtherRunner != OtherList->end(); OtherRunner++) {
 | 
|---|
| 200 |                       if ((*OtherRunner) > Walker) {  // just to not add bonds from both sides
 | 
|---|
| 201 |                         OtherWalker = dynamic_cast<atom*>(*OtherRunner);
 | 
|---|
| 202 |                         ASSERT(OtherWalker != NULL,
 | 
|---|
| 203 |                             "BondGraph::CreateAdjacency() - TesselPoint that was not an atom retrieved from LinkedNode");
 | 
|---|
| 204 |                         LOG(3, "INFO: Current other atom is " << *OtherWalker << ".");
 | 
|---|
| 205 |                         if (OtherWalker->father > Walker->father ) { // just to not add bonds from both sides
 | 
|---|
| 206 |                           const range<double> MinMaxDistanceSquared(
 | 
|---|
| 207 |                               getMinMaxDistanceSquared(Walker, OtherWalker));
 | 
|---|
| 208 |                           const double distance = domain.periodicDistanceSquared(OtherWalker->getPosition(),Walker->getPosition());
 | 
|---|
| 209 |                           LOG(3, "INFO: Checking squared distance " << distance << " against typical bond length of " << MinMaxDistanceSquared << ".");
 | 
|---|
| 210 |                           const bool status = MinMaxDistanceSquared.isInRange(distance);
 | 
|---|
| 211 |                           if (status) { // create bond if distance is smaller
 | 
|---|
| 212 |                             LOG(1, "ACCEPT: Adding Bond between " << *Walker << " and " << *OtherWalker << " in distance " << sqrt(distance) << ".");
 | 
|---|
| 213 |                             //const bond * Binder =
 | 
|---|
| 214 |                               Walker->father->addBond(CurrentTime, OtherWalker->father);
 | 
|---|
| 215 |                             BondCount++;
 | 
|---|
| 216 |                           } else {
 | 
|---|
| 217 |                             LOG(2, "REJECT: Squared distance "
 | 
|---|
| 218 |                                 << distance << " is out of squared covalent bounds "
 | 
|---|
| 219 |                                 << MinMaxDistanceSquared << ".");
 | 
|---|
| 220 |                           }
 | 
|---|
| 221 |                         } else {
 | 
|---|
| 222 |                           LOG(5, "REJECT: Not Adding: Wrong order of father's.");
 | 
|---|
| 223 |                         }
 | 
|---|
| 224 |                       } else {
 | 
|---|
| 225 |                         LOG(5, "REJECT: Not Adding: Wrong order.");
 | 
|---|
| 226 |                       }
 | 
|---|
| 227 |                     }
 | 
|---|
| 228 |                   }
 | 
|---|
| 229 |                 }
 | 
|---|
| 230 |           }
 | 
|---|
| 231 |         }
 | 
|---|
| 232 |       }
 | 
|---|
| 233 |     LOG(1, "I detected " << BondCount << " bonds in the molecule.");
 | 
|---|
| 234 | }
 | 
|---|
| 235 | 
 | 
|---|
| 236 | bool BondGraph::operator==(const BondGraph &other) const
 | 
|---|
| 237 | {
 | 
|---|
| 238 |   if (IsAngstroem != other.IsAngstroem)
 | 
|---|
| 239 |     return false;
 | 
|---|
| 240 |   if (((BondLengthMatrix != NULL) && (other.BondLengthMatrix == NULL))
 | 
|---|
| 241 |       || ((BondLengthMatrix == NULL) && (other.BondLengthMatrix != NULL)))
 | 
|---|
| 242 |     return false;
 | 
|---|
| 243 |   if ((BondLengthMatrix != NULL) && (other.BondLengthMatrix != NULL))
 | 
|---|
| 244 |     if (*BondLengthMatrix != *other.BondLengthMatrix)
 | 
|---|
| 245 |       return false;
 | 
|---|
| 246 |   return true;
 | 
|---|
| 247 | }
 | 
|---|