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