| 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 "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 | max_distance(0),
|
|---|
| 45 | IsAngstroem(IsA)
|
|---|
| 46 | {}
|
|---|
| 47 |
|
|---|
| 48 | BondGraph::~BondGraph()
|
|---|
| 49 | {
|
|---|
| 50 | if (BondLengthMatrix != NULL) {
|
|---|
| 51 | delete(BondLengthMatrix);
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | bool BondGraph::LoadBondLengthTable(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 | // find greatest distance
|
|---|
| 77 | max_distance=0;
|
|---|
| 78 | if (status) {
|
|---|
| 79 | for(int i=0;i<TempContainer->RowCounter[0];i++)
|
|---|
| 80 | for(int j=i;j<TempContainer->ColumnCounter[0];j++)
|
|---|
| 81 | if (TempContainer->Matrix[0][i][j] > max_distance)
|
|---|
| 82 | max_distance = TempContainer->Matrix[0][i][j];
|
|---|
| 83 | }
|
|---|
| 84 | max_distance += BondThreshold;
|
|---|
| 85 |
|
|---|
| 86 | if (status) // set to not NULL only if matrix was parsed
|
|---|
| 87 | BondLengthMatrix = TempContainer;
|
|---|
| 88 | else {
|
|---|
| 89 | BondLengthMatrix = NULL;
|
|---|
| 90 | delete(TempContainer);
|
|---|
| 91 | }
|
|---|
| 92 | return status;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | double BondGraph::GetBondLength(int firstZ, int secondZ)
|
|---|
| 96 | {
|
|---|
| 97 | std::cout << "Request for length between " << firstZ << " and " << secondZ << ": ";
|
|---|
| 98 | if (BondLengthMatrix == NULL) {
|
|---|
| 99 | std::cout << "-1." << std::endl;
|
|---|
| 100 | return( -1. );
|
|---|
| 101 | } else {
|
|---|
| 102 | std::cout << BondLengthMatrix->Matrix[0][firstZ][secondZ] << std::endl;
|
|---|
| 103 | return (BondLengthMatrix->Matrix[0][firstZ][secondZ]);
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | double BondGraph::getMaxDistance() const
|
|---|
| 108 | {
|
|---|
| 109 | return max_distance;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | void BondGraph::CovalentMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem)
|
|---|
| 114 | {
|
|---|
| 115 | MinDistance = OtherWalker->getType()->getCovalentRadius() + Walker->getType()->getCovalentRadius();
|
|---|
| 116 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
|
|---|
| 117 | MaxDistance = MinDistance + BondThreshold;
|
|---|
| 118 | MinDistance -= BondThreshold;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void BondGraph::BondLengthMatrixMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem)
|
|---|
| 122 | {
|
|---|
| 123 | ASSERT(BondLengthMatrix != NULL,
|
|---|
| 124 | "BondGraph::BondLengthMatrixMinMaxDistance() called without NULL BondLengthMatrix.");
|
|---|
| 125 | MinDistance = GetBondLength(Walker->getType()->getAtomicNumber()-1, OtherWalker->getType()->getAtomicNumber()-1);
|
|---|
| 126 | MinDistance *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
|
|---|
| 127 | MaxDistance = MinDistance + BondThreshold;
|
|---|
| 128 | MinDistance -= BondThreshold;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | void BondGraph::getMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem)
|
|---|
| 132 | {
|
|---|
| 133 | if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet
|
|---|
| 134 | LOG(2, "INFO: Using Covalent radii criterion for [min,max] distances.");
|
|---|
| 135 | CovalentMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem);
|
|---|
| 136 | } else {
|
|---|
| 137 | LOG(2, "INFO: Using Covalent radii criterion for [min,max] distances.");
|
|---|
| 138 | BondLengthMatrixMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem);
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | void BondGraph::CreateAdjacency(LinkedCell &LC)
|
|---|
| 143 | {
|
|---|
| 144 | atom *Walker = NULL;
|
|---|
| 145 | atom *OtherWalker = NULL;
|
|---|
| 146 | int n[NDIM];
|
|---|
| 147 | double MinDistance, MaxDistance;
|
|---|
| 148 | Box &domain = World::getInstance().getDomain();
|
|---|
| 149 |
|
|---|
| 150 | unsigned int BondCount = 0;
|
|---|
| 151 | // 3a. go through every cell
|
|---|
| 152 | LOG(3, "INFO: Celling ... ");
|
|---|
| 153 | for (LC.n[0] = 0; LC.n[0] < LC.N[0]; LC.n[0]++)
|
|---|
| 154 | for (LC.n[1] = 0; LC.n[1] < LC.N[1]; LC.n[1]++)
|
|---|
| 155 | for (LC.n[2] = 0; LC.n[2] < LC.N[2]; LC.n[2]++) {
|
|---|
| 156 | const TesselPointSTLList *List = LC.GetCurrentCell();
|
|---|
| 157 | LOG(2, "Current cell is " << LC.n[0] << ", " << LC.n[1] << ", " << LC.n[2] << " with No. " << LC.index << " containing " << List->size() << " points.");
|
|---|
| 158 | if (List != NULL) {
|
|---|
| 159 | for (TesselPointSTLList::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
|
|---|
| 160 | Walker = dynamic_cast<atom*>(*Runner);
|
|---|
| 161 | ASSERT(Walker != NULL,
|
|---|
| 162 | "BondGraph::CreateAdjacency() - Tesselpoint that was not an atom retrieved from LinkedNode");
|
|---|
| 163 | LOG(2, "INFO: Current Atom is " << *Walker << ".");
|
|---|
| 164 | // 3c. check for possible bond between each atom in this and every one in the 27 cells
|
|---|
| 165 | for (n[0] = -1; n[0] <= 1; n[0]++)
|
|---|
| 166 | for (n[1] = -1; n[1] <= 1; n[1]++)
|
|---|
| 167 | for (n[2] = -1; n[2] <= 1; n[2]++) {
|
|---|
| 168 | const TesselPointSTLList *OtherList = LC.GetRelativeToCurrentCell(n);
|
|---|
| 169 | if (OtherList != NULL) {
|
|---|
| 170 | LOG(3, "INFO: Current relative cell is " << LC.n[0] << ", " << LC.n[1] << ", " << LC.n[2] << " with No. " << LC.index << " containing " << List->size() << " points.");
|
|---|
| 171 | for (TesselPointSTLList::const_iterator OtherRunner = OtherList->begin(); OtherRunner != OtherList->end(); OtherRunner++) {
|
|---|
| 172 | if ((*OtherRunner) > Walker) { // just to not add bonds from both sides
|
|---|
| 173 | OtherWalker = dynamic_cast<atom*>(*OtherRunner);
|
|---|
| 174 | ASSERT(OtherWalker != NULL,
|
|---|
| 175 | "BondGraph::CreateAdjacency() - TesselPoint that was not an atom retrieved from LinkedNode");
|
|---|
| 176 | getMinMaxDistance(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem);
|
|---|
| 177 | const double distance = domain.periodicDistanceSquared(OtherWalker->getPosition(),Walker->getPosition());
|
|---|
| 178 | LOG(2, "INFO: Checking distance " << distance << " against typical bond length of [" << MinDistance * MinDistance << "," << MaxDistance * MaxDistance << "].");
|
|---|
| 179 | const bool status = (distance <= MaxDistance * MaxDistance) && (distance >= MinDistance * MinDistance);
|
|---|
| 180 | LOG(3, "INFO: MinDistance is " << MinDistance << " and MaxDistance is " << MaxDistance << ".");
|
|---|
| 181 | if (OtherWalker->father > Walker->father ) { // just to not add bonds from both sides
|
|---|
| 182 | if (status) { // create bond if distance is smaller
|
|---|
| 183 | LOG(1, "ACCEPT: Adding Bond between " << *Walker << " and " << *OtherWalker << " in distance " << sqrt(distance) << ".");
|
|---|
| 184 | bond * Binder = new bond(Walker->father, OtherWalker->father, 1);
|
|---|
| 185 | Walker->father->RegisterBond(WorldTime::getTime(), Binder);
|
|---|
| 186 | OtherWalker->father->RegisterBond(WorldTime::getTime(), Binder);
|
|---|
| 187 | BondCount++;
|
|---|
| 188 | } else {
|
|---|
| 189 | LOG(1, "REJECT: Squared distance "
|
|---|
| 190 | << distance << " is out of covalent bounds ["
|
|---|
| 191 | << MinDistance*MinDistance << ","
|
|---|
| 192 | << MaxDistance * MaxDistance << "].");
|
|---|
| 193 | }
|
|---|
| 194 | } else {
|
|---|
| 195 | LOG(5, "REJECT: Not Adding: Wrong order of father's.");
|
|---|
| 196 | }
|
|---|
| 197 | } else {
|
|---|
| 198 | LOG(5, "REJECT: Not Adding: Wrong order.");
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | LOG(1, "I detected " << BondCount << " bonds in the molecule.");
|
|---|
| 207 | }
|
|---|