| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * bondgraph.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Oct 29, 2009
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <iostream>
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include "Atom/atom.hpp"
 | 
|---|
| 40 | #include "Bond/bond.hpp"
 | 
|---|
| 41 | #include "Graph/BondGraph.hpp"
 | 
|---|
| 42 | #include "Box.hpp"
 | 
|---|
| 43 | #include "Element/element.hpp"
 | 
|---|
| 44 | #include "CodePatterns/Info.hpp"
 | 
|---|
| 45 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 46 | #include "CodePatterns/Range.hpp"
 | 
|---|
| 47 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 48 | #include "molecule.hpp"
 | 
|---|
| 49 | #include "Element/periodentafel.hpp"
 | 
|---|
| 50 | #include "Fragmentation/MatrixContainer.hpp"
 | 
|---|
| 51 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 52 | #include "World.hpp"
 | 
|---|
| 53 | #include "WorldTime.hpp"
 | 
|---|
| 54 | 
 | 
|---|
| 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 | 
 | 
|---|
| 57 | BondGraph::BondGraph() :
 | 
|---|
| 58 |     BondLengthMatrix(NULL),
 | 
|---|
| 59 |     IsAngstroem(true)
 | 
|---|
| 60 | {}
 | 
|---|
| 61 | 
 | 
|---|
| 62 | BondGraph::BondGraph(bool IsA) :
 | 
|---|
| 63 |     BondLengthMatrix(NULL),
 | 
|---|
| 64 |     IsAngstroem(IsA)
 | 
|---|
| 65 | {}
 | 
|---|
| 66 | 
 | 
|---|
| 67 | BondGraph::~BondGraph()
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   CleanupBondLengthTable();
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | void BondGraph::CleanupBondLengthTable()
 | 
|---|
| 73 | {
 | 
|---|
| 74 |   if (BondLengthMatrix != NULL) {
 | 
|---|
| 75 |     delete(BondLengthMatrix);
 | 
|---|
| 76 |   }
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | bool BondGraph::LoadBondLengthTable(
 | 
|---|
| 80 |     std::istream &input)
 | 
|---|
| 81 | {
 | 
|---|
| 82 |   Info FunctionInfo(__func__);
 | 
|---|
| 83 |   bool status = true;
 | 
|---|
| 84 |   MatrixContainer *TempContainer = NULL;
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   // allocate MatrixContainer
 | 
|---|
| 87 |   if (BondLengthMatrix != NULL) {
 | 
|---|
| 88 |     LOG(1, "MatrixContainer for Bond length already present, removing.");
 | 
|---|
| 89 |     delete(BondLengthMatrix);
 | 
|---|
| 90 |     BondLengthMatrix = NULL;
 | 
|---|
| 91 |   }
 | 
|---|
| 92 |   TempContainer = new MatrixContainer;
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   // parse in matrix
 | 
|---|
| 95 |   if ((input.good()) && (status = TempContainer->ParseMatrix(input, 0, 1, 0))) {
 | 
|---|
| 96 |     LOG(1, "Parsing bond length matrix successful.");
 | 
|---|
| 97 |   } else {
 | 
|---|
| 98 |     ELOG(1, "Parsing bond length matrix failed.");
 | 
|---|
| 99 |     status = false;
 | 
|---|
| 100 |   }
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   if (status) // set to not NULL only if matrix was parsed
 | 
|---|
| 103 |     BondLengthMatrix = TempContainer;
 | 
|---|
| 104 |   else {
 | 
|---|
| 105 |     BondLengthMatrix = NULL;
 | 
|---|
| 106 |     delete(TempContainer);
 | 
|---|
| 107 |   }
 | 
|---|
| 108 |   return status;
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | double BondGraph::GetBondLength(
 | 
|---|
| 112 |     int firstZ,
 | 
|---|
| 113 |     int secondZ) const
 | 
|---|
| 114 | {
 | 
|---|
| 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.;
 | 
|---|
| 120 |   if (BondLengthMatrix == NULL) {
 | 
|---|
| 121 |     return_length = -1.;
 | 
|---|
| 122 |   } else {
 | 
|---|
| 123 |     return_length = BondLengthMatrix->Matrix[0][firstZ][secondZ];
 | 
|---|
| 124 |   }
 | 
|---|
| 125 |   LOG(4, "INFO: Request for length between " << firstZ << " and "
 | 
|---|
| 126 |       << secondZ << ": " << return_length << ".");
 | 
|---|
| 127 |   return return_length;
 | 
|---|
| 128 | }
 | 
|---|
| 129 | 
 | 
|---|
| 130 | range<double> BondGraph::CovalentMinMaxDistance(
 | 
|---|
| 131 |     const element * const Walker,
 | 
|---|
| 132 |     const element * const OtherWalker) const
 | 
|---|
| 133 | {
 | 
|---|
| 134 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 135 |   MinMaxDistance.first = OtherWalker->getCovalentRadius() + Walker->getCovalentRadius();
 | 
|---|
| 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::BondLengthMatrixMinMaxDistance(
 | 
|---|
| 143 |     const element * const Walker,
 | 
|---|
| 144 |     const element * const OtherWalker) const
 | 
|---|
| 145 | {
 | 
|---|
| 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.");
 | 
|---|
| 149 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 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;
 | 
|---|
| 154 |   return MinMaxDistance;
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | range<double> BondGraph::getMinMaxDistance(
 | 
|---|
| 158 |     const element * const Walker,
 | 
|---|
| 159 |     const element * const OtherWalker) const
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   range<double> MinMaxDistance(0.,0.);
 | 
|---|
| 162 |   if (BondLengthMatrix == NULL) {// safety measure if no matrix has been parsed yet
 | 
|---|
| 163 |     LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances.");
 | 
|---|
| 164 |     MinMaxDistance = CovalentMinMaxDistance(Walker, OtherWalker);
 | 
|---|
| 165 |   } else {
 | 
|---|
| 166 |     LOG(2, "INFO: Using Covalent radii criterion for [min,max) distances.");
 | 
|---|
| 167 |     MinMaxDistance = BondLengthMatrixMinMaxDistance(Walker, OtherWalker);
 | 
|---|
| 168 |   }
 | 
|---|
| 169 |   return MinMaxDistance;
 | 
|---|
| 170 | }
 | 
|---|
| 171 | 
 | 
|---|
| 172 | range<double> BondGraph::getMinMaxDistance(
 | 
|---|
| 173 |     const BondedParticle * const Walker,
 | 
|---|
| 174 |     const BondedParticle * const OtherWalker) const
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   return getMinMaxDistance(Walker->getType(), OtherWalker->getType());
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | range<double> BondGraph::getMinMaxDistanceSquared(
 | 
|---|
| 180 |     const BondedParticle * const Walker,
 | 
|---|
| 181 |     const BondedParticle * const OtherWalker) const
 | 
|---|
| 182 | {
 | 
|---|
| 183 |   // use non-squared version
 | 
|---|
| 184 |   range<double> MinMaxDistance(getMinMaxDistance(Walker, OtherWalker));
 | 
|---|
| 185 |   // and square
 | 
|---|
| 186 |   MinMaxDistance.first *= MinMaxDistance.first;
 | 
|---|
| 187 |   MinMaxDistance.last *= MinMaxDistance.last;
 | 
|---|
| 188 |   return MinMaxDistance;
 | 
|---|
| 189 | }
 | 
|---|
| 190 | 
 | 
|---|
| 191 | LinkedCell::LinkedCell_View BondGraph::getLinkedCell(const double max_distance) const
 | 
|---|
| 192 | {
 | 
|---|
| 193 |   return World::getInstance().getLinkedCell(max_distance);
 | 
|---|
| 194 | }
 | 
|---|
| 195 | 
 | 
|---|
| 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 | 
 | 
|---|
| 204 | Box &BondGraph::getDomain() const
 | 
|---|
| 205 | {
 | 
|---|
| 206 |   return World::getInstance().getDomain();
 | 
|---|
| 207 | }
 | 
|---|
| 208 | 
 | 
|---|
| 209 | unsigned int BondGraph::getTime() const
 | 
|---|
| 210 | {
 | 
|---|
| 211 |   return WorldTime::getTime();
 | 
|---|
| 212 | }
 | 
|---|
| 213 | 
 | 
|---|
| 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 | }
 | 
|---|