| 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 |   double return_length;
 | 
|---|
| 101 |   if ((firstZ < 0) || (firstZ >= (int)BondLengthMatrix->Matrix[0].size()))
 | 
|---|
| 102 |     return -1.;
 | 
|---|
| 103 |   if ((secondZ < 0) || (secondZ >= (int)BondLengthMatrix->Matrix[0][firstZ].size()))
 | 
|---|
| 104 |     return -1.;
 | 
|---|
| 105 |   if (BondLengthMatrix == NULL) {
 | 
|---|
| 106 |     return_length = -1.;
 | 
|---|
| 107 |   } else {
 | 
|---|
| 108 |     return_length = BondLengthMatrix->Matrix[0][firstZ][secondZ];
 | 
|---|
| 109 |   }
 | 
|---|
| 110 |   LOG(4, "INFO: Request for length between " << firstZ << " and "
 | 
|---|
| 111 |       << secondZ << ": " << return_length << ".");
 | 
|---|
| 112 |   return return_length;
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | range<double> BondGraph::CovalentMinMaxDistance(
 | 
|---|
| 116 |     const element * const Walker,
 | 
|---|
| 117 |     const element * const OtherWalker) const
 | 
|---|
| 118 | {
 | 
|---|
| 119 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 120 |   MinMaxDistance.first = OtherWalker->getCovalentRadius() + Walker->getCovalentRadius();
 | 
|---|
| 121 |   MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
 | 
|---|
| 122 |   MinMaxDistance.last = MinMaxDistance.first + BondThreshold;
 | 
|---|
| 123 |   MinMaxDistance.first -= BondThreshold;
 | 
|---|
| 124 |   return MinMaxDistance;
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | range<double> BondGraph::BondLengthMatrixMinMaxDistance(
 | 
|---|
| 128 |     const element * const Walker,
 | 
|---|
| 129 |     const element * const OtherWalker) const
 | 
|---|
| 130 | {
 | 
|---|
| 131 |   ASSERT(BondLengthMatrix, "BondGraph::BondLengthMatrixMinMaxDistance() called without NULL BondLengthMatrix.");
 | 
|---|
| 132 |   ASSERT(Walker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal element given.");
 | 
|---|
| 133 |   ASSERT(OtherWalker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal other element given.");
 | 
|---|
| 134 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 135 |   MinMaxDistance.first = GetBondLength(Walker->getAtomicNumber()-1, OtherWalker->getAtomicNumber()-1);
 | 
|---|
| 136 |   MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
 | 
|---|
| 137 |   MinMaxDistance.last = MinMaxDistance.first + BondThreshold;
 | 
|---|
| 138 |   MinMaxDistance.first -= BondThreshold;
 | 
|---|
| 139 |   return MinMaxDistance;
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | range<double> BondGraph::getMinMaxDistance(
 | 
|---|
| 143 |     const element * const Walker,
 | 
|---|
| 144 |     const element * const OtherWalker) const
 | 
|---|
| 145 | {
 | 
|---|
| 146 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 147 |   if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet
 | 
|---|
| 148 |     LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances.");
 | 
|---|
| 149 |     MinMaxDistance = CovalentMinMaxDistance(Walker, OtherWalker);
 | 
|---|
| 150 |   } else {
 | 
|---|
| 151 |     LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances.");
 | 
|---|
| 152 |     MinMaxDistance = BondLengthMatrixMinMaxDistance(Walker, OtherWalker);
 | 
|---|
| 153 |   }
 | 
|---|
| 154 |   return MinMaxDistance;
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | range<double> BondGraph::getMinMaxDistance(
 | 
|---|
| 158 |     const BondedParticle * const Walker,
 | 
|---|
| 159 |     const BondedParticle * const OtherWalker) const
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   return getMinMaxDistance(Walker->getType(), OtherWalker->getType());
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | range<double> BondGraph::getMinMaxDistanceSquared(
 | 
|---|
| 165 |     const BondedParticle * const Walker,
 | 
|---|
| 166 |     const BondedParticle * const OtherWalker) const
 | 
|---|
| 167 | {
 | 
|---|
| 168 |   // use non-squared version
 | 
|---|
| 169 |   range<double> MinMaxDistance(getMinMaxDistance(Walker, OtherWalker));
 | 
|---|
| 170 |   // and square
 | 
|---|
| 171 |   MinMaxDistance.first *= MinMaxDistance.first;
 | 
|---|
| 172 |   MinMaxDistance.last *= MinMaxDistance.last;
 | 
|---|
| 173 |   return MinMaxDistance;
 | 
|---|
| 174 | }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | LinkedCell::LinkedCell_View BondGraph::getLinkedCell(const double max_distance) const
 | 
|---|
| 177 | {
 | 
|---|
| 178 |   return World::getInstance().getLinkedCell(max_distance);
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | std::set< const element *> BondGraph::getElementSetFromNumbers(const std::set<atomicNumber_t> &Set) const
 | 
|---|
| 182 | {
 | 
|---|
| 183 |   std::set< const element *> PresentElements;
 | 
|---|
| 184 |   for(std::set<atomicNumber_t>::const_iterator iter = Set.begin(); iter != Set.end(); ++iter)
 | 
|---|
| 185 |     PresentElements.insert( World::getInstance().getPeriode()->FindElement(*iter) );
 | 
|---|
| 186 |   return PresentElements;
 | 
|---|
| 187 | }
 | 
|---|
| 188 | 
 | 
|---|
| 189 | Box &BondGraph::getDomain() const
 | 
|---|
| 190 | {
 | 
|---|
| 191 |   return World::getInstance().getDomain();
 | 
|---|
| 192 | }
 | 
|---|
| 193 | 
 | 
|---|
| 194 | unsigned int BondGraph::getTime() const
 | 
|---|
| 195 | {
 | 
|---|
| 196 |   return WorldTime::getTime();
 | 
|---|
| 197 | }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | bool BondGraph::operator==(const BondGraph &other) const
 | 
|---|
| 200 | {
 | 
|---|
| 201 |   if (IsAngstroem != other.IsAngstroem)
 | 
|---|
| 202 |     return false;
 | 
|---|
| 203 |   if (((BondLengthMatrix != NULL) && (other.BondLengthMatrix == NULL))
 | 
|---|
| 204 |       || ((BondLengthMatrix == NULL) && (other.BondLengthMatrix != NULL)))
 | 
|---|
| 205 |     return false;
 | 
|---|
| 206 |   if ((BondLengthMatrix != NULL) && (other.BondLengthMatrix != NULL))
 | 
|---|
| 207 |     if (*BondLengthMatrix != *other.BondLengthMatrix)
 | 
|---|
| 208 |       return false;
 | 
|---|
| 209 |   return true;
 | 
|---|
| 210 | }
 | 
|---|