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