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