| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
| [b70721] | 23 | /*
 | 
|---|
 | 24 |  * bondgraph.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Oct 29, 2009
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 36 | 
 | 
|---|
| [b70721] | 37 | #include <iostream>
 | 
|---|
 | 38 | 
 | 
|---|
| [6f0841] | 39 | #include "Atom/atom.hpp"
 | 
|---|
| [129204] | 40 | #include "Bond/bond.hpp"
 | 
|---|
| [632508] | 41 | #include "Graph/BondGraph.hpp"
 | 
|---|
| [3738f0] | 42 | #include "Box.hpp"
 | 
|---|
| [3bdb6d] | 43 | #include "Element/element.hpp"
 | 
|---|
| [ad011c] | 44 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 45 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [3738f0] | 46 | #include "CodePatterns/Range.hpp"
 | 
|---|
 | 47 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [b70721] | 48 | #include "molecule.hpp"
 | 
|---|
| [3bdb6d] | 49 | #include "Element/periodentafel.hpp"
 | 
|---|
| [a9b86d] | 50 | #include "Fragmentation/MatrixContainer.hpp"
 | 
|---|
| [57f243] | 51 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [3738f0] | 52 | #include "World.hpp"
 | 
|---|
 | 53 | #include "WorldTime.hpp"
 | 
|---|
| [b70721] | 54 | 
 | 
|---|
| [88b400] | 55 | 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
 | 
|---|
 | 56 | 
 | 
|---|
| [f007a1] | 57 | BondGraph::BondGraph() :
 | 
|---|
 | 58 |     BondLengthMatrix(NULL),
 | 
|---|
 | 59 |     IsAngstroem(true)
 | 
|---|
 | 60 | {}
 | 
|---|
 | 61 | 
 | 
|---|
| [97b825] | 62 | BondGraph::BondGraph(bool IsA) :
 | 
|---|
 | 63 |     BondLengthMatrix(NULL),
 | 
|---|
 | 64 |     IsAngstroem(IsA)
 | 
|---|
| [3738f0] | 65 | {}
 | 
|---|
| [b70721] | 66 | 
 | 
|---|
 | 67 | BondGraph::~BondGraph()
 | 
|---|
| [829761] | 68 | {
 | 
|---|
 | 69 |   CleanupBondLengthTable();
 | 
|---|
 | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | void BondGraph::CleanupBondLengthTable()
 | 
|---|
| [b70721] | 73 | {
 | 
|---|
 | 74 |   if (BondLengthMatrix != NULL) {
 | 
|---|
 | 75 |     delete(BondLengthMatrix);
 | 
|---|
 | 76 |   }
 | 
|---|
| [3738f0] | 77 | }
 | 
|---|
| [b70721] | 78 | 
 | 
|---|
| [111f4a] | 79 | bool BondGraph::LoadBondLengthTable(
 | 
|---|
 | 80 |     std::istream &input)
 | 
|---|
| [b70721] | 81 | {
 | 
|---|
| [244a84] | 82 |   Info FunctionInfo(__func__);
 | 
|---|
| [b70721] | 83 |   bool status = true;
 | 
|---|
| [34e0013] | 84 |   MatrixContainer *TempContainer = NULL;
 | 
|---|
| [b70721] | 85 | 
 | 
|---|
 | 86 |   // allocate MatrixContainer
 | 
|---|
 | 87 |   if (BondLengthMatrix != NULL) {
 | 
|---|
| [3738f0] | 88 |     LOG(1, "MatrixContainer for Bond length already present, removing.");
 | 
|---|
| [b70721] | 89 |     delete(BondLengthMatrix);
 | 
|---|
| [829761] | 90 |     BondLengthMatrix = NULL;
 | 
|---|
| [b70721] | 91 |   }
 | 
|---|
| [34e0013] | 92 |   TempContainer = new MatrixContainer;
 | 
|---|
| [b70721] | 93 | 
 | 
|---|
 | 94 |   // parse in matrix
 | 
|---|
| [4e855e] | 95 |   if ((input.good()) && (status = TempContainer->ParseMatrix(input, 0, 1, 0))) {
 | 
|---|
| [3738f0] | 96 |     LOG(1, "Parsing bond length matrix successful.");
 | 
|---|
| [244a84] | 97 |   } else {
 | 
|---|
| [47d041] | 98 |     ELOG(1, "Parsing bond length matrix failed.");
 | 
|---|
| [4e855e] | 99 |     status = false;
 | 
|---|
| [244a84] | 100 |   }
 | 
|---|
| [b70721] | 101 | 
 | 
|---|
| [34e0013] | 102 |   if (status) // set to not NULL only if matrix was parsed
 | 
|---|
 | 103 |     BondLengthMatrix = TempContainer;
 | 
|---|
 | 104 |   else {
 | 
|---|
 | 105 |     BondLengthMatrix = NULL;
 | 
|---|
 | 106 |     delete(TempContainer);
 | 
|---|
 | 107 |   }
 | 
|---|
| [b70721] | 108 |   return status;
 | 
|---|
| [3738f0] | 109 | }
 | 
|---|
| [b70721] | 110 | 
 | 
|---|
| [300220] | 111 | double BondGraph::GetBondLength(
 | 
|---|
 | 112 |     int firstZ,
 | 
|---|
 | 113 |     int secondZ) const
 | 
|---|
| [b70721] | 114 | {
 | 
|---|
| [826e8c] | 115 |   double return_length;
 | 
|---|
 | 116 |   if ((firstZ < 0) || (firstZ >= (int)BondLengthMatrix->Matrix[0].size()))
 | 
|---|
 | 117 |     return -1.;
 | 
|---|
 | 118 |   if ((secondZ < 0) || (secondZ >= (int)BondLengthMatrix->Matrix[0][firstZ].size()))
 | 
|---|
 | 119 |     return -1.;
 | 
|---|
| [4e855e] | 120 |   if (BondLengthMatrix == NULL) {
 | 
|---|
| [826e8c] | 121 |     return_length = -1.;
 | 
|---|
| [4e855e] | 122 |   } else {
 | 
|---|
| [826e8c] | 123 |     return_length = BondLengthMatrix->Matrix[0][firstZ][secondZ];
 | 
|---|
| [4e855e] | 124 |   }
 | 
|---|
| [826e8c] | 125 |   LOG(4, "INFO: Request for length between " << firstZ << " and "
 | 
|---|
 | 126 |       << secondZ << ": " << return_length << ".");
 | 
|---|
 | 127 |   return return_length;
 | 
|---|
| [3738f0] | 128 | }
 | 
|---|
| [ae38fb] | 129 | 
 | 
|---|
| [607eab] | 130 | range<double> BondGraph::CovalentMinMaxDistance(
 | 
|---|
| [300220] | 131 |     const element * const Walker,
 | 
|---|
| [607eab] | 132 |     const element * const OtherWalker) const
 | 
|---|
| [b70721] | 133 | {
 | 
|---|
| [607eab] | 134 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| [300220] | 135 |   MinMaxDistance.first = OtherWalker->getCovalentRadius() + Walker->getCovalentRadius();
 | 
|---|
 | 136 |   MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
 | 
|---|
 | 137 |   MinMaxDistance.last = MinMaxDistance.first + BondThreshold;
 | 
|---|
 | 138 |   MinMaxDistance.first -= BondThreshold;
 | 
|---|
| [607eab] | 139 |   return MinMaxDistance;
 | 
|---|
| [3738f0] | 140 | }
 | 
|---|
| [b70721] | 141 | 
 | 
|---|
| [607eab] | 142 | range<double> BondGraph::BondLengthMatrixMinMaxDistance(
 | 
|---|
| [300220] | 143 |     const element * const Walker,
 | 
|---|
| [607eab] | 144 |     const element * const OtherWalker) const
 | 
|---|
| [72d90e] | 145 | {
 | 
|---|
| [300220] | 146 |   ASSERT(BondLengthMatrix, "BondGraph::BondLengthMatrixMinMaxDistance() called without NULL BondLengthMatrix.");
 | 
|---|
 | 147 |   ASSERT(Walker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal element given.");
 | 
|---|
 | 148 |   ASSERT(OtherWalker, "BondGraph::BondLengthMatrixMinMaxDistance() - illegal other element given.");
 | 
|---|
| [607eab] | 149 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| [300220] | 150 |   MinMaxDistance.first = GetBondLength(Walker->getAtomicNumber()-1, OtherWalker->getAtomicNumber()-1);
 | 
|---|
 | 151 |   MinMaxDistance.first *= (IsAngstroem) ? 1. : 1. / AtomicLengthToAngstroem;
 | 
|---|
 | 152 |   MinMaxDistance.last = MinMaxDistance.first + BondThreshold;
 | 
|---|
 | 153 |   MinMaxDistance.first -= BondThreshold;
 | 
|---|
| [607eab] | 154 |   return MinMaxDistance;
 | 
|---|
| [3738f0] | 155 | }
 | 
|---|
| [72d90e] | 156 | 
 | 
|---|
| [607eab] | 157 | range<double> BondGraph::getMinMaxDistance(
 | 
|---|
| [300220] | 158 |     const element * const Walker,
 | 
|---|
| [607eab] | 159 |     const element * const OtherWalker) const
 | 
|---|
| [b70721] | 160 | {
 | 
|---|
| [607eab] | 161 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| [34e0013] | 162 |   if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet
 | 
|---|
| [8ac66b4] | 163 |     LOG(5, "DEBUG: Using Covalent radii criterion for [min,max) distances.");
 | 
|---|
| [607eab] | 164 |     MinMaxDistance = CovalentMinMaxDistance(Walker, OtherWalker);
 | 
|---|
| [b21a64] | 165 |   } else {
 | 
|---|
| [8ac66b4] | 166 |     LOG(5, "DEBUG: Using tabulated bond table criterion for [min,max) distances.");
 | 
|---|
| [607eab] | 167 |     MinMaxDistance = BondLengthMatrixMinMaxDistance(Walker, OtherWalker);
 | 
|---|
| [b21a64] | 168 |   }
 | 
|---|
| [607eab] | 169 |   return MinMaxDistance;
 | 
|---|
| [72d90e] | 170 | }
 | 
|---|
| [3738f0] | 171 | 
 | 
|---|
| [607eab] | 172 | range<double> BondGraph::getMinMaxDistance(
 | 
|---|
| [300220] | 173 |     const BondedParticle * const Walker,
 | 
|---|
| [607eab] | 174 |     const BondedParticle * const OtherWalker) const
 | 
|---|
| [300220] | 175 | {
 | 
|---|
| [607eab] | 176 |   return getMinMaxDistance(Walker->getType(), OtherWalker->getType());
 | 
|---|
| [300220] | 177 | }
 | 
|---|
 | 178 | 
 | 
|---|
| [607eab] | 179 | range<double> BondGraph::getMinMaxDistanceSquared(
 | 
|---|
| [300220] | 180 |     const BondedParticle * const Walker,
 | 
|---|
| [607eab] | 181 |     const BondedParticle * const OtherWalker) const
 | 
|---|
| [300220] | 182 | {
 | 
|---|
 | 183 |   // use non-squared version
 | 
|---|
| [607eab] | 184 |   range<double> MinMaxDistance(getMinMaxDistance(Walker, OtherWalker));
 | 
|---|
| [300220] | 185 |   // and square
 | 
|---|
 | 186 |   MinMaxDistance.first *= MinMaxDistance.first;
 | 
|---|
 | 187 |   MinMaxDistance.last *= MinMaxDistance.last;
 | 
|---|
| [607eab] | 188 |   return MinMaxDistance;
 | 
|---|
| [300220] | 189 | }
 | 
|---|
 | 190 | 
 | 
|---|
| [826e8c] | 191 | LinkedCell::LinkedCell_View BondGraph::getLinkedCell(const double max_distance) const
 | 
|---|
| [3738f0] | 192 | {
 | 
|---|
| [826e8c] | 193 |   return World::getInstance().getLinkedCell(max_distance);
 | 
|---|
 | 194 | }
 | 
|---|
 | 195 | 
 | 
|---|
| [108968] | 196 | std::set< const element *> BondGraph::getElementSetFromNumbers(const std::set<atomicNumber_t> &Set) const
 | 
|---|
 | 197 | {
 | 
|---|
 | 198 |   std::set< const element *> PresentElements;
 | 
|---|
 | 199 |   for(std::set<atomicNumber_t>::const_iterator iter = Set.begin(); iter != Set.end(); ++iter)
 | 
|---|
 | 200 |     PresentElements.insert( World::getInstance().getPeriode()->FindElement(*iter) );
 | 
|---|
 | 201 |   return PresentElements;
 | 
|---|
 | 202 | }
 | 
|---|
 | 203 | 
 | 
|---|
| [826e8c] | 204 | Box &BondGraph::getDomain() const
 | 
|---|
 | 205 | {
 | 
|---|
 | 206 |   return World::getInstance().getDomain();
 | 
|---|
 | 207 | }
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 | unsigned int BondGraph::getTime() const
 | 
|---|
 | 210 | {
 | 
|---|
 | 211 |   return WorldTime::getTime();
 | 
|---|
| [3738f0] | 212 | }
 | 
|---|
| [0cbad2] | 213 | 
 | 
|---|
| [9b6663] | 214 | bool BondGraph::operator==(const BondGraph &other) const
 | 
|---|
 | 215 | {
 | 
|---|
 | 216 |   if (IsAngstroem != other.IsAngstroem)
 | 
|---|
 | 217 |     return false;
 | 
|---|
 | 218 |   if (((BondLengthMatrix != NULL) && (other.BondLengthMatrix == NULL))
 | 
|---|
 | 219 |       || ((BondLengthMatrix == NULL) && (other.BondLengthMatrix != NULL)))
 | 
|---|
 | 220 |     return false;
 | 
|---|
 | 221 |   if ((BondLengthMatrix != NULL) && (other.BondLengthMatrix != NULL))
 | 
|---|
 | 222 |     if (*BondLengthMatrix != *other.BondLengthMatrix)
 | 
|---|
 | 223 |       return false;
 | 
|---|
 | 224 |   return true;
 | 
|---|
 | 225 | }
 | 
|---|