| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [94d5ac6] | 6 |  * 
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 13 |  *    (at your option) any later version.
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 18 |  *    GNU General Public License for more details.
 | 
|---|
 | 19 |  *
 | 
|---|
 | 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
| [14de469] | 24 | /** \file molecules.cpp
 | 
|---|
| [69eb71] | 25 |  *
 | 
|---|
| [14de469] | 26 |  * Functions for the class molecule.
 | 
|---|
| [69eb71] | 27 |  *
 | 
|---|
| [14de469] | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
| [aafd77] | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 36 | 
 | 
|---|
| [49e1ae] | 37 | #include <cstring>
 | 
|---|
| [ac9b56] | 38 | #include <boost/bind.hpp>
 | 
|---|
| [9df5c6] | 39 | #include <boost/foreach.hpp>
 | 
|---|
| [49e1ae] | 40 | 
 | 
|---|
| [aafd77] | 41 | #include <gsl/gsl_inline.h>
 | 
|---|
 | 42 | #include <gsl/gsl_heapsort.h>
 | 
|---|
 | 43 | 
 | 
|---|
| [560bbe] | 44 | #include "molecule.hpp"
 | 
|---|
 | 45 | 
 | 
|---|
| [6f0841] | 46 | #include "Atom/atom.hpp"
 | 
|---|
| [129204] | 47 | #include "Bond/bond.hpp"
 | 
|---|
| [9d83b6] | 48 | #include "Box.hpp"
 | 
|---|
 | 49 | #include "CodePatterns/enumeration.hpp"
 | 
|---|
 | 50 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [a80fbdf] | 51 | #include "config.hpp"
 | 
|---|
| [560bbe] | 52 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [3bdb6d] | 53 | #include "Element/element.hpp"
 | 
|---|
| [129204] | 54 | #include "Graph/BondGraph.hpp"
 | 
|---|
| [783e88] | 55 | #include "LinearAlgebra/Exceptions.hpp"
 | 
|---|
| [13d150] | 56 | #include "LinearAlgebra/leastsquaremin.hpp"
 | 
|---|
| [9d83b6] | 57 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
 | 58 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
 | 59 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [53c7fc] | 60 | #include "LinkedCell/linkedcell.hpp"
 | 
|---|
| [560bbe] | 61 | #include "IdPool_impl.hpp"
 | 
|---|
| [c67ff9] | 62 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| [d127c8] | 63 | #include "Tesselation/tesselation.hpp"
 | 
|---|
| [b34306] | 64 | #include "World.hpp"
 | 
|---|
| [9d83b6] | 65 | #include "WorldTime.hpp"
 | 
|---|
| [14de469] | 66 | 
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 | /************************************* Functions for class molecule *********************************/
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 | /** Constructor of class molecule.
 | 
|---|
 | 71 |  * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
 | 
|---|
 | 72 |  */
 | 
|---|
| [4d2b33] | 73 | molecule::molecule() :
 | 
|---|
| [cd5047] | 74 |   Observable("molecule"),
 | 
|---|
| [458c31] | 75 |   MDSteps(0),
 | 
|---|
 | 76 |   NoNonBonds(0),
 | 
|---|
 | 77 |   NoCyclicBonds(0),
 | 
|---|
 | 78 |   ActiveFlag(false),
 | 
|---|
 | 79 |   IndexNr(-1),
 | 
|---|
| [e791dc] | 80 |   NoNonHydrogen(this,boost::bind(&molecule::doCountNoNonHydrogen,this),"NoNonHydrogen"),
 | 
|---|
| [458c31] | 81 |   BondCount(this,boost::bind(&molecule::doCountBonds,this),"BondCount"),
 | 
|---|
| [52ed5b] | 82 |   atomIdPool(1, 20, 100),
 | 
|---|
| [458c31] | 83 |   last_atom(0)
 | 
|---|
| [69eb71] | 84 | {
 | 
|---|
| [6a3c83] | 85 |   // add specific channels
 | 
|---|
 | 86 |   Channels *OurChannel = new Channels;
 | 
|---|
| [708277] | 87 |   NotificationChannels.insert( std::make_pair( static_cast<Observable *>(this), OurChannel) );
 | 
|---|
| [6a3c83] | 88 |   for (size_t type = 0; type < (size_t)NotificationType_MAX; ++type)
 | 
|---|
 | 89 |     OurChannel->addChannel(type);
 | 
|---|
| [fa649a] | 90 | 
 | 
|---|
| [387b36] | 91 |   strcpy(name,World::getInstance().getDefaultName().c_str());
 | 
|---|
| [14de469] | 92 | };
 | 
|---|
 | 93 | 
 | 
|---|
| [cbc5fb] | 94 | molecule *NewMolecule(){
 | 
|---|
| [4d2b33] | 95 |   return new molecule();
 | 
|---|
| [cbc5fb] | 96 | }
 | 
|---|
 | 97 | 
 | 
|---|
| [14de469] | 98 | /** Destructor of class molecule.
 | 
|---|
 | 99 |  * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
 | 
|---|
 | 100 |  */
 | 
|---|
| [69eb71] | 101 | molecule::~molecule()
 | 
|---|
| [14de469] | 102 | {
 | 
|---|
| [042f82] | 103 |   CleanupMolecule();
 | 
|---|
| [14de469] | 104 | };
 | 
|---|
 | 105 | 
 | 
|---|
| [357fba] | 106 | 
 | 
|---|
| [cbc5fb] | 107 | void DeleteMolecule(molecule *mol){
 | 
|---|
 | 108 |   delete mol;
 | 
|---|
 | 109 | }
 | 
|---|
 | 110 | 
 | 
|---|
| [520c8b] | 111 | // getter and setter
 | 
|---|
| [73a857] | 112 | const std::string molecule::getName() const{
 | 
|---|
| [520c8b] | 113 |   return std::string(name);
 | 
|---|
 | 114 | }
 | 
|---|
 | 115 | 
 | 
|---|
| [ea7176] | 116 | int molecule::getAtomCount() const{
 | 
|---|
| [e791dc] | 117 |   return atomIds.size();
 | 
|---|
 | 118 | }
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 | size_t molecule::getNoNonHydrogen() const{
 | 
|---|
 | 121 |   return *NoNonHydrogen;
 | 
|---|
| [ea7176] | 122 | }
 | 
|---|
 | 123 | 
 | 
|---|
| [458c31] | 124 | int molecule::getBondCount() const{
 | 
|---|
 | 125 |   return *BondCount;
 | 
|---|
 | 126 | }
 | 
|---|
 | 127 | 
 | 
|---|
| [520c8b] | 128 | void molecule::setName(const std::string _name){
 | 
|---|
| [2ba827] | 129 |   OBSERVE;
 | 
|---|
| [6a3c83] | 130 |   NOTIFY(MoleculeNameChanged);
 | 
|---|
| [35b698] | 131 |   cout << "Set name of molecule " << getId() << " to " << _name << endl;
 | 
|---|
| [520c8b] | 132 |   strncpy(name,_name.c_str(),MAXSTRINGSIZE);
 | 
|---|
 | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
| [c6ab91] | 135 | void molecule::InsertLocalToGlobalId(atom * const pointer)
 | 
|---|
 | 136 | {
 | 
|---|
 | 137 | #ifndef NDEBUG
 | 
|---|
 | 138 |   std::pair< LocalToGlobalId_t::iterator, bool > inserter =
 | 
|---|
 | 139 | #endif
 | 
|---|
 | 140 |       LocalToGlobalId.insert( std::make_pair(pointer->getNr(), pointer) );
 | 
|---|
 | 141 |   ASSERT( inserter.second,
 | 
|---|
 | 142 |       "molecule::AddAtom() - local number "+toString(pointer->getNr())+" appears twice.");
 | 
|---|
 | 143 | }
 | 
|---|
 | 144 | 
 | 
|---|
| [560bbe] | 145 | bool molecule::changeAtomNr(int oldNr, int newNr, atom* target){
 | 
|---|
 | 146 |   OBSERVE;
 | 
|---|
 | 147 |   if(atomIdPool.reserveId(newNr)){
 | 
|---|
| [6a3c83] | 148 |     NOTIFY(AtomNrChanged);
 | 
|---|
| [560bbe] | 149 |     if (oldNr != -1)  // -1 is reserved and indicates no number
 | 
|---|
 | 150 |       atomIdPool.releaseId(oldNr);
 | 
|---|
| [c6ab91] | 151 |     LocalToGlobalId.erase(oldNr);
 | 
|---|
| [560bbe] | 152 |     ASSERT (target,
 | 
|---|
 | 153 |         "molecule::changeAtomNr() - given target is NULL, cannot set Nr or name.");
 | 
|---|
 | 154 |     target->setNr(newNr);
 | 
|---|
| [c6ab91] | 155 |     InsertLocalToGlobalId(target);
 | 
|---|
| [560bbe] | 156 |     setAtomName(target);
 | 
|---|
 | 157 |     return true;
 | 
|---|
 | 158 |   } else{
 | 
|---|
 | 159 |     return false;
 | 
|---|
 | 160 |   }
 | 
|---|
 | 161 | }
 | 
|---|
 | 162 | 
 | 
|---|
| [a7a087] | 163 | bool molecule::changeId(moleculeId_t newId){
 | 
|---|
 | 164 |   // first we move ourselves in the world
 | 
|---|
 | 165 |   // the world lets us know if that succeeded
 | 
|---|
 | 166 |   if(World::getInstance().changeMoleculeId(id,newId,this)){
 | 
|---|
 | 167 |     id = newId;
 | 
|---|
 | 168 |     return true;
 | 
|---|
 | 169 |   }
 | 
|---|
 | 170 |   else{
 | 
|---|
 | 171 |     return false;
 | 
|---|
 | 172 |   }
 | 
|---|
 | 173 | }
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 | 
 | 
|---|
| [73a857] | 176 | moleculeId_t molecule::getId() const {
 | 
|---|
| [cbc5fb] | 177 |   return id;
 | 
|---|
 | 178 | }
 | 
|---|
 | 179 | 
 | 
|---|
 | 180 | void molecule::setId(moleculeId_t _id){
 | 
|---|
 | 181 |   id =_id;
 | 
|---|
 | 182 | }
 | 
|---|
 | 183 | 
 | 
|---|
| [73a857] | 184 | const Formula &molecule::getFormula() const {
 | 
|---|
| [f17e1c] | 185 |   return formula;
 | 
|---|
| [ac9b56] | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|
| [73a857] | 188 | unsigned int molecule::getElementCount() const{
 | 
|---|
| [389cc8] | 189 |   return formula.getElementCount();
 | 
|---|
 | 190 | }
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 | bool molecule::hasElement(const element *element) const{
 | 
|---|
 | 193 |   return formula.hasElement(element);
 | 
|---|
 | 194 | }
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 | bool molecule::hasElement(atomicNumber_t Z) const{
 | 
|---|
 | 197 |   return formula.hasElement(Z);
 | 
|---|
 | 198 | }
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 | bool molecule::hasElement(const string &shorthand) const{
 | 
|---|
 | 201 |   return formula.hasElement(shorthand);
 | 
|---|
 | 202 | }
 | 
|---|
 | 203 | 
 | 
|---|
| [bd58fb] | 204 | /************************** Access to the List of Atoms ****************/
 | 
|---|
 | 205 | 
 | 
|---|
| [9879f6] | 206 | molecule::const_iterator molecule::erase( const_iterator loc )
 | 
|---|
 | 207 | {
 | 
|---|
| [bf8e20] | 208 |   OBSERVE;
 | 
|---|
| [6a3c83] | 209 |   NOTIFY(AtomRemoved);
 | 
|---|
| [59fff1] | 210 |   const_iterator iter = loc;
 | 
|---|
| [30c753] | 211 |   ++iter;
 | 
|---|
| [59fff1] | 212 |   atom * const _atom = const_cast<atom *>(*loc);
 | 
|---|
 | 213 |   atomIds.erase( _atom->getId() );
 | 
|---|
| [6a3c83] | 214 |   {
 | 
|---|
 | 215 |     NOTIFY(AtomNrChanged);
 | 
|---|
 | 216 |     atomIdPool.releaseId(_atom->getNr());
 | 
|---|
| [c6ab91] | 217 |     LocalToGlobalId.erase(_atom->getNr());
 | 
|---|
| [6a3c83] | 218 |     _atom->setNr(-1);
 | 
|---|
 | 219 |   }
 | 
|---|
| [59fff1] | 220 |   formula-=_atom->getType();
 | 
|---|
 | 221 |   _atom->removeFromMolecule();
 | 
|---|
| [9879f6] | 222 |   return iter;
 | 
|---|
 | 223 | }
 | 
|---|
 | 224 | 
 | 
|---|
| [6cfa36] | 225 | molecule::const_iterator molecule::erase( atom * key )
 | 
|---|
| [9879f6] | 226 | {
 | 
|---|
| [bf8e20] | 227 |   OBSERVE;
 | 
|---|
| [6a3c83] | 228 |   NOTIFY(AtomRemoved);
 | 
|---|
| [59fff1] | 229 |   const_iterator iter = find(key);
 | 
|---|
| [a7b761b] | 230 |   if (iter != end()){
 | 
|---|
| [30c753] | 231 |     ++iter;
 | 
|---|
| [274d45] | 232 |     atomIds.erase( key->getId() );
 | 
|---|
| [6a3c83] | 233 |     {
 | 
|---|
 | 234 |       NOTIFY(AtomNrChanged);
 | 
|---|
 | 235 |       atomIdPool.releaseId(key->getNr());
 | 
|---|
| [c6ab91] | 236 |       LocalToGlobalId.erase(key->getNr());
 | 
|---|
| [6a3c83] | 237 |       key->setNr(-1);
 | 
|---|
 | 238 |     }
 | 
|---|
| [8f4df1] | 239 |     formula-=key->getType();
 | 
|---|
| [6cfa36] | 240 |     key->removeFromMolecule();
 | 
|---|
| [a7b761b] | 241 |   }
 | 
|---|
 | 242 |   return iter;
 | 
|---|
| [9879f6] | 243 | }
 | 
|---|
 | 244 | 
 | 
|---|
 | 245 | pair<molecule::iterator,bool> molecule::insert ( atom * const key )
 | 
|---|
 | 246 | {
 | 
|---|
| [bf8e20] | 247 |   OBSERVE;
 | 
|---|
| [6a3c83] | 248 |   NOTIFY(AtomInserted);
 | 
|---|
| [8e1f901] | 249 |   std::pair<iterator,bool> res = atomIds.insert(key->getId());
 | 
|---|
| [274d45] | 250 |   if (res.second) { // push atom if went well
 | 
|---|
| [6a3c83] | 251 |     NOTIFY(AtomNrChanged);
 | 
|---|
| [560bbe] | 252 |     key->setNr(atomIdPool.getNextId());
 | 
|---|
| [c6ab91] | 253 |     InsertLocalToGlobalId(key);
 | 
|---|
| [560bbe] | 254 |     setAtomName(key);
 | 
|---|
| [8f4df1] | 255 |     formula+=key->getType();
 | 
|---|
| [8e1f901] | 256 |     return res;
 | 
|---|
| [274d45] | 257 |   } else {
 | 
|---|
| [30c753] | 258 |     return pair<iterator,bool>(end(),res.second);
 | 
|---|
| [274d45] | 259 |   }
 | 
|---|
| [9879f6] | 260 | }
 | 
|---|
| [520c8b] | 261 | 
 | 
|---|
| [560bbe] | 262 | void molecule::setAtomName(atom *_atom) const
 | 
|---|
 | 263 | {
 | 
|---|
 | 264 |   std::stringstream sstr;
 | 
|---|
| [52ed5b] | 265 |   sstr << _atom->getType()->getSymbol() << _atom->getNr();
 | 
|---|
| [560bbe] | 266 |   _atom->setName(sstr.str());
 | 
|---|
 | 267 | }
 | 
|---|
 | 268 | 
 | 
|---|
| [9317be] | 269 | World::AtomComposite molecule::getAtomSet() const
 | 
|---|
| [3738f0] | 270 | {
 | 
|---|
| [9317be] | 271 |   World::AtomComposite vector_of_atoms;
 | 
|---|
| [59fff1] | 272 |   for (molecule::iterator iter = begin(); iter != end(); ++iter)
 | 
|---|
| [30c753] | 273 |     vector_of_atoms.push_back(*iter);
 | 
|---|
| [3738f0] | 274 |   return vector_of_atoms;
 | 
|---|
 | 275 | }
 | 
|---|
 | 276 | 
 | 
|---|
| [14de469] | 277 | /** Adds given atom \a *pointer from molecule list.
 | 
|---|
| [69eb71] | 278 |  * Increases molecule::last_atom and gives last number to added atom and names it according to its element::abbrev and molecule::AtomCount
 | 
|---|
| [14de469] | 279 |  * \param *pointer allocated and set atom
 | 
|---|
 | 280 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
 | 281 |  */
 | 
|---|
 | 282 | bool molecule::AddAtom(atom *pointer)
 | 
|---|
| [69eb71] | 283 | {
 | 
|---|
| [042f82] | 284 |   if (pointer != NULL) {
 | 
|---|
| [356ae4] | 285 |     // molecule::insert() is called by setMolecule()
 | 
|---|
| [6cfa36] | 286 |     pointer->setMolecule(this);
 | 
|---|
| [f721c6] | 287 |   }
 | 
|---|
| [9879f6] | 288 |   return true;
 | 
|---|
| [14de469] | 289 | };
 | 
|---|
 | 290 | 
 | 
|---|
 | 291 | /** Adds a copy of the given atom \a *pointer from molecule list.
 | 
|---|
 | 292 |  * Increases molecule::last_atom and gives last number to added atom.
 | 
|---|
 | 293 |  * \param *pointer allocated and set atom
 | 
|---|
| [89c8b2] | 294 |  * \return pointer to the newly added atom
 | 
|---|
| [14de469] | 295 |  */
 | 
|---|
 | 296 | atom * molecule::AddCopyAtom(atom *pointer)
 | 
|---|
| [69eb71] | 297 | {
 | 
|---|
| [f721c6] | 298 |   atom *retval = NULL;
 | 
|---|
| [042f82] | 299 |   if (pointer != NULL) {
 | 
|---|
| [46d958] | 300 |     atom *walker = pointer->clone();
 | 
|---|
| [c6ab91] | 301 |     AddAtom(walker);
 | 
|---|
| [f721c6] | 302 |     retval=walker;
 | 
|---|
 | 303 |   }
 | 
|---|
 | 304 |   return retval;
 | 
|---|
| [14de469] | 305 | };
 | 
|---|
 | 306 | 
 | 
|---|
 | 307 | /** Adds a Hydrogen atom in replacement for the given atom \a *partner in bond with a *origin.
 | 
|---|
 | 308 |  * Here, we have to distinguish between single, double or triple bonds as stated by \a BondDegree, that each demand
 | 
|---|
 | 309 |  * a different scheme when adding \a *replacement atom for the given one.
 | 
|---|
 | 310 |  * -# Single Bond: Simply add new atom with bond distance rescaled to typical hydrogen one
 | 
|---|
 | 311 |  * -# Double Bond: Here, we need the **BondList of the \a *origin atom, by scanning for the other bonds instead of
 | 
|---|
| [042f82] | 312 |  *    *Bond, we use the through these connected atoms to determine the plane they lie in, vector::MakeNormalvector().
 | 
|---|
 | 313 |  *    The orthonormal vector to this plane along with the vector in *Bond direction determines the plane the two
 | 
|---|
 | 314 |  *    replacing hydrogens shall lie in. Now, all remains to do is take the usual hydrogen double bond angle for the
 | 
|---|
 | 315 |  *    element of *origin and form the sin/cos admixture of both plane vectors for the new coordinates of the two
 | 
|---|
 | 316 |  *    hydrogens forming this angle with *origin.
 | 
|---|
| [14de469] | 317 |  * -# Triple Bond: The idea is to set up a tetraoid (C1-H1-H2-H3) (however the lengths \f$b\f$ of the sides of the base
 | 
|---|
| [042f82] | 318 |  *    triangle formed by the to be added hydrogens are not equal to the typical bond distance \f$l\f$ but have to be
 | 
|---|
 | 319 |  *    determined from the typical angle \f$\alpha\f$ for a hydrogen triple connected to the element of *origin):
 | 
|---|
 | 320 |  *    We have the height \f$d\f$ as the vector in *Bond direction (from triangle C1-H1-H2).
 | 
|---|
 | 321 |  *    \f[ h = l \cdot \cos{\left (\frac{\alpha}{2} \right )} \qquad b = 2l \cdot \sin{\left (\frac{\alpha}{2} \right)} \quad \rightarrow \quad d = l \cdot \sqrt{\cos^2{\left (\frac{\alpha}{2} \right)}-\frac{1}{3}\cdot\sin^2{\left (\frac{\alpha}{2}\right )}}
 | 
|---|
 | 322 |  *    \f]
 | 
|---|
 | 323 |  *    vector::GetNormalvector() creates one orthonormal vector from this *Bond vector and vector::MakeNormalvector creates
 | 
|---|
 | 324 |  *    the third one from the former two vectors. The latter ones form the plane of the base triangle mentioned above.
 | 
|---|
 | 325 |  *    The lengths for these are \f$f\f$ and \f$g\f$ (from triangle H1-H2-(center of H1-H2-H3)) with knowledge that
 | 
|---|
 | 326 |  *    the median lines in an isosceles triangle meet in the center point with a ratio 2:1.
 | 
|---|
 | 327 |  *    \f[ f = \frac{b}{\sqrt{3}} \qquad g = \frac{b}{2}
 | 
|---|
 | 328 |  *    \f]
 | 
|---|
 | 329 |  *    as the coordination of all three atoms in the coordinate system of these three vectors:
 | 
|---|
 | 330 |  *    \f$\pmatrix{d & f & 0}\f$, \f$\pmatrix{d & -0.5 \cdot f & g}\f$ and \f$\pmatrix{d & -0.5 \cdot f & -g}\f$.
 | 
|---|
| [69eb71] | 331 |  *
 | 
|---|
| [14de469] | 332 |  * \param *out output stream for debugging
 | 
|---|
| [69eb71] | 333 |  * \param *Bond pointer to bond between \a *origin and \a *replacement
 | 
|---|
 | 334 |  * \param *TopOrigin son of \a *origin of upper level molecule (the atom added to this molecule as a copy of \a *origin)
 | 
|---|
| [14de469] | 335 |  * \param *origin pointer to atom which acts as the origin for scaling the added hydrogen to correct bond length
 | 
|---|
 | 336 |  * \param *replacement pointer to the atom which shall be copied as a hydrogen atom in this molecule
 | 
|---|
 | 337 |  * \param isAngstroem whether the coordination of the given atoms is in AtomicLength (false) or Angstrom(true)
 | 
|---|
 | 338 |  * \return number of atoms added, if < bond::BondDegree then something went wrong
 | 
|---|
 | 339 |  * \todo double and triple bonds splitting (always use the tetraeder angle!)
 | 
|---|
 | 340 |  */
 | 
|---|
| [88c8ec] | 341 | bool molecule::AddHydrogenReplacementAtom(bond::ptr TopBond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem)
 | 
|---|
| [14de469] | 342 | {
 | 
|---|
| [47d041] | 343 | //  Info info(__func__);
 | 
|---|
| [f721c6] | 344 |   bool AllWentWell = true;    // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
 | 
|---|
| [042f82] | 345 |   double bondlength;  // bond length of the bond to be replaced/cut
 | 
|---|
 | 346 |   double bondangle;  // bond angle of the bond to be replaced/cut
 | 
|---|
 | 347 |   double BondRescale;   // rescale value for the hydrogen bond length
 | 
|---|
| [7d82a5] | 348 |   bond::ptr FirstBond;
 | 
|---|
 | 349 |   bond::ptr SecondBond; // Other bonds in double bond case to determine "other" plane
 | 
|---|
| [042f82] | 350 |   atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
 | 
|---|
 | 351 |   double b,l,d,f,g, alpha, factors[NDIM];    // hold temporary values in triple bond case for coordination determination
 | 
|---|
 | 352 |   Vector Orthovector1, Orthovector2;  // temporary vectors in coordination construction
 | 
|---|
 | 353 |   Vector InBondvector;    // vector in direction of *Bond
 | 
|---|
| [cca9ef] | 354 |   const RealSpaceMatrix &matrix =  World::getInstance().getDomain().getM();
 | 
|---|
| [7d82a5] | 355 |   bond::ptr Binder;
 | 
|---|
| [042f82] | 356 | 
 | 
|---|
 | 357 |   // create vector in direction of bond
 | 
|---|
| [d74077] | 358 |   InBondvector = TopReplacement->getPosition() - TopOrigin->getPosition();
 | 
|---|
| [042f82] | 359 |   bondlength = InBondvector.Norm();
 | 
|---|
 | 360 | 
 | 
|---|
 | 361 |    // is greater than typical bond distance? Then we have to correct periodically
 | 
|---|
 | 362 |    // the problem is not the H being out of the box, but InBondvector have the wrong direction
 | 
|---|
 | 363 |    // due to TopReplacement or Origin being on the wrong side!
 | 
|---|
| [300220] | 364 |   const BondGraph * const BG = World::getInstance().getBondGraph();
 | 
|---|
| [607eab] | 365 |   const range<double> MinMaxBondDistance(
 | 
|---|
 | 366 |       BG->getMinMaxDistance(TopOrigin,TopReplacement));
 | 
|---|
| [300220] | 367 |   if (!MinMaxBondDistance.isInRange(bondlength)) {
 | 
|---|
| [47d041] | 368 | //    LOG(4, "InBondvector is: " << InBondvector << ".");
 | 
|---|
| [042f82] | 369 |     Orthovector1.Zero();
 | 
|---|
 | 370 |     for (int i=NDIM;i--;) {
 | 
|---|
| [d74077] | 371 |       l = TopReplacement->at(i) - TopOrigin->at(i);
 | 
|---|
| [300220] | 372 |       if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here)
 | 
|---|
| [0a4f7f] | 373 |         Orthovector1[i] = (l < 0) ? -1. : +1.;
 | 
|---|
| [042f82] | 374 |       } // (signs are correct, was tested!)
 | 
|---|
 | 375 |     }
 | 
|---|
| [5108e1] | 376 |     Orthovector1 *= matrix;
 | 
|---|
| [1bd79e] | 377 |     InBondvector -= Orthovector1; // subtract just the additional translation
 | 
|---|
| [042f82] | 378 |     bondlength = InBondvector.Norm();
 | 
|---|
| [47d041] | 379 | //    LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << ".");
 | 
|---|
| [042f82] | 380 |   } // periodic correction finished
 | 
|---|
 | 381 | 
 | 
|---|
 | 382 |   InBondvector.Normalize();
 | 
|---|
 | 383 |   // get typical bond length and store as scale factor for later
 | 
|---|
| [d74077] | 384 |   ASSERT(TopOrigin->getType() != NULL, "AddHydrogenReplacementAtom: element of TopOrigin is not given.");
 | 
|---|
| [1f693d] | 385 |   BondRescale = TopOrigin->getType()->getHBondDistance(TopBond->getDegree()-1);
 | 
|---|
| [042f82] | 386 |   if (BondRescale == -1) {
 | 
|---|
| [1f693d] | 387 |     ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->getName() << "<->" << TopReplacement->getName() << ") of degree " << TopBond->getDegree() << "!");
 | 
|---|
| [2ba827] | 388 |     return false;
 | 
|---|
| [042f82] | 389 |     BondRescale = bondlength;
 | 
|---|
 | 390 |   } else {
 | 
|---|
 | 391 |     if (!IsAngstroem)
 | 
|---|
 | 392 |       BondRescale /= (1.*AtomicLengthToAngstroem);
 | 
|---|
 | 393 |   }
 | 
|---|
 | 394 | 
 | 
|---|
 | 395 |   // discern single, double and triple bonds
 | 
|---|
| [1f693d] | 396 |   switch(TopBond->getDegree()) {
 | 
|---|
| [042f82] | 397 |     case 1:
 | 
|---|
| [23b547] | 398 |       FirstOtherAtom = World::getInstance().createAtom();    // new atom
 | 
|---|
| [d74077] | 399 |       FirstOtherAtom->setType(1);  // element is Hydrogen
 | 
|---|
| [bce72c] | 400 |       FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 401 |       FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [83f176] | 402 |       if (TopReplacement->getType()->getAtomicNumber() == 1) { // neither rescale nor replace if it's already hydrogen
 | 
|---|
| [042f82] | 403 |         FirstOtherAtom->father = TopReplacement;
 | 
|---|
 | 404 |         BondRescale = bondlength;
 | 
|---|
 | 405 |       } else {
 | 
|---|
 | 406 |         FirstOtherAtom->father = NULL;  // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
 | 
|---|
 | 407 |       }
 | 
|---|
| [1bd79e] | 408 |       InBondvector *= BondRescale;   // rescale the distance vector to Hydrogen bond length
 | 
|---|
| [d74077] | 409 |       FirstOtherAtom->setPosition(TopOrigin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom
 | 
|---|
| [042f82] | 410 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
| [47d041] | 411 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
 | 
|---|
| [042f82] | 412 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 413 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 414 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 415 |       break;
 | 
|---|
 | 416 |     case 2:
 | 
|---|
| [9d83b6] | 417 |       {
 | 
|---|
 | 418 |         // determine two other bonds (warning if there are more than two other) plus valence sanity check
 | 
|---|
 | 419 |         const BondList& ListOfBonds = TopOrigin->getListOfBonds();
 | 
|---|
 | 420 |         for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
 | 421 |             Runner != ListOfBonds.end();
 | 
|---|
 | 422 |             ++Runner) {
 | 
|---|
 | 423 |           if ((*Runner) != TopBond) {
 | 
|---|
 | 424 |             if (FirstBond == NULL) {
 | 
|---|
 | 425 |               FirstBond = (*Runner);
 | 
|---|
 | 426 |               FirstOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
 | 
|---|
 | 427 |             } else if (SecondBond == NULL) {
 | 
|---|
 | 428 |               SecondBond = (*Runner);
 | 
|---|
 | 429 |               SecondOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
 | 
|---|
 | 430 |             } else {
 | 
|---|
| [47d041] | 431 |               ELOG(2, "Detected more than four bonds for atom " << TopOrigin->getName());
 | 
|---|
| [9d83b6] | 432 |             }
 | 
|---|
| [042f82] | 433 |           }
 | 
|---|
 | 434 |         }
 | 
|---|
 | 435 |       }
 | 
|---|
 | 436 |       if (SecondOtherAtom == NULL) {  // then we have an atom with valence four, but only 3 bonds: one to replace and one which is TopBond (third is FirstBond)
 | 
|---|
 | 437 |         SecondBond = TopBond;
 | 
|---|
 | 438 |         SecondOtherAtom = TopReplacement;
 | 
|---|
 | 439 |       }
 | 
|---|
 | 440 |       if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all
 | 
|---|
| [47d041] | 441 | //        LOG(3, "Regarding the double bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") to be constructed: Taking " << FirstOtherAtom->Name << " and " << SecondOtherAtom->Name << " along with " << TopOrigin->Name << " to determine orthogonal plane.");
 | 
|---|
| [042f82] | 442 | 
 | 
|---|
 | 443 |         // determine the plane of these two with the *origin
 | 
|---|
| [0a4f7f] | 444 |         try {
 | 
|---|
| [783e88] | 445 |           Orthovector1 = Plane(TopOrigin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal();
 | 
|---|
| [0a4f7f] | 446 |         }
 | 
|---|
 | 447 |         catch(LinearDependenceException &excp){
 | 
|---|
| [47d041] | 448 |           LOG(0, boost::diagnostic_information(excp));
 | 
|---|
| [0a4f7f] | 449 |           // TODO: figure out what to do with the Orthovector in this case
 | 
|---|
 | 450 |           AllWentWell = false;
 | 
|---|
 | 451 |         }
 | 
|---|
| [042f82] | 452 |       } else {
 | 
|---|
| [273382] | 453 |         Orthovector1.GetOneNormalVector(InBondvector);
 | 
|---|
| [042f82] | 454 |       }
 | 
|---|
| [47d041] | 455 |       //LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
 | 
|---|
| [042f82] | 456 |       // orthogonal vector and bond vector between origin and replacement form the new plane
 | 
|---|
| [0a4f7f] | 457 |       Orthovector1.MakeNormalTo(InBondvector);
 | 
|---|
| [042f82] | 458 |       Orthovector1.Normalize();
 | 
|---|
| [47d041] | 459 |       //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << ".");
 | 
|---|
| [042f82] | 460 | 
 | 
|---|
 | 461 |       // create the two Hydrogens ...
 | 
|---|
| [23b547] | 462 |       FirstOtherAtom = World::getInstance().createAtom();
 | 
|---|
 | 463 |       SecondOtherAtom = World::getInstance().createAtom();
 | 
|---|
| [d74077] | 464 |       FirstOtherAtom->setType(1);
 | 
|---|
 | 465 |       SecondOtherAtom->setType(1);
 | 
|---|
| [bce72c] | 466 |       FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 467 |       FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [bce72c] | 468 |       SecondOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 469 |       SecondOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [042f82] | 470 |       FirstOtherAtom->father = NULL;  // we are just an added hydrogen with no father
 | 
|---|
 | 471 |       SecondOtherAtom->father = NULL;  //  we are just an added hydrogen with no father
 | 
|---|
| [83f176] | 472 |       bondangle = TopOrigin->getType()->getHBondAngle(1);
 | 
|---|
| [042f82] | 473 |       if (bondangle == -1) {
 | 
|---|
| [1f693d] | 474 |         ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->getName() << "<->" << TopReplacement->getName() << ") of degree " << TopBond->getDegree() << "!");
 | 
|---|
| [2ba827] | 475 |         return false;
 | 
|---|
| [042f82] | 476 |         bondangle = 0;
 | 
|---|
 | 477 |       }
 | 
|---|
 | 478 |       bondangle *= M_PI/180./2.;
 | 
|---|
| [47d041] | 479 | //      LOG(3, "INFO: ReScaleCheck: InBondvector " << InBondvector << ", " << Orthovector1 << ".");
 | 
|---|
 | 480 | //      LOG(3, "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle));
 | 
|---|
| [d74077] | 481 |       FirstOtherAtom->Zero();
 | 
|---|
 | 482 |       SecondOtherAtom->Zero();
 | 
|---|
| [042f82] | 483 |       for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
 | 
|---|
| [d74077] | 484 |         FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle)));
 | 
|---|
 | 485 |         SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle)));
 | 
|---|
| [042f82] | 486 |       }
 | 
|---|
| [d74077] | 487 |       FirstOtherAtom->Scale(BondRescale);  // rescale by correct BondDistance
 | 
|---|
 | 488 |       SecondOtherAtom->Scale(BondRescale);
 | 
|---|
| [47d041] | 489 |       //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << ".");
 | 
|---|
| [d74077] | 490 |       *FirstOtherAtom += TopOrigin->getPosition();
 | 
|---|
 | 491 |       *SecondOtherAtom += TopOrigin->getPosition();
 | 
|---|
| [042f82] | 492 |       // ... and add to molecule
 | 
|---|
 | 493 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
 | 494 |       AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
 | 
|---|
| [47d041] | 495 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
 | 
|---|
 | 496 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
 | 
|---|
| [042f82] | 497 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 498 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 499 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 500 |       Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
 | 
|---|
 | 501 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 502 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 503 |       break;
 | 
|---|
 | 504 |     case 3:
 | 
|---|
 | 505 |       // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
 | 
|---|
| [23b547] | 506 |       FirstOtherAtom = World::getInstance().createAtom();
 | 
|---|
 | 507 |       SecondOtherAtom = World::getInstance().createAtom();
 | 
|---|
 | 508 |       ThirdOtherAtom = World::getInstance().createAtom();
 | 
|---|
| [d74077] | 509 |       FirstOtherAtom->setType(1);
 | 
|---|
 | 510 |       SecondOtherAtom->setType(1);
 | 
|---|
 | 511 |       ThirdOtherAtom->setType(1);
 | 
|---|
| [bce72c] | 512 |       FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 513 |       FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [bce72c] | 514 |       SecondOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 515 |       SecondOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [bce72c] | 516 |       ThirdOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 517 |       ThirdOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [042f82] | 518 |       FirstOtherAtom->father = NULL;  //  we are just an added hydrogen with no father
 | 
|---|
 | 519 |       SecondOtherAtom->father = NULL;  //  we are just an added hydrogen with no father
 | 
|---|
 | 520 |       ThirdOtherAtom->father = NULL;  //  we are just an added hydrogen with no father
 | 
|---|
 | 521 | 
 | 
|---|
 | 522 |       // we need to vectors orthonormal the InBondvector
 | 
|---|
| [273382] | 523 |       AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector);
 | 
|---|
| [47d041] | 524 | //      LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
 | 
|---|
| [0a4f7f] | 525 |       try{
 | 
|---|
 | 526 |         Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal();
 | 
|---|
 | 527 |       }
 | 
|---|
 | 528 |       catch(LinearDependenceException &excp) {
 | 
|---|
| [47d041] | 529 |         LOG(0, boost::diagnostic_information(excp));
 | 
|---|
| [0a4f7f] | 530 |         AllWentWell = false;
 | 
|---|
 | 531 |       }
 | 
|---|
| [47d041] | 532 | //      LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".")
 | 
|---|
| [042f82] | 533 | 
 | 
|---|
 | 534 |       // create correct coordination for the three atoms
 | 
|---|
| [83f176] | 535 |       alpha = (TopOrigin->getType()->getHBondAngle(2))/180.*M_PI/2.;  // retrieve triple bond angle from database
 | 
|---|
| [042f82] | 536 |       l = BondRescale;        // desired bond length
 | 
|---|
 | 537 |       b = 2.*l*sin(alpha);    // base length of isosceles triangle
 | 
|---|
 | 538 |       d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.);   // length for InBondvector
 | 
|---|
 | 539 |       f = b/sqrt(3.);   // length for Orthvector1
 | 
|---|
 | 540 |       g = b/2.;         // length for Orthvector2
 | 
|---|
| [47d041] | 541 | //      LOG(3, "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", ");
 | 
|---|
 | 542 | //      LOG(3, "The three Bond lengths: " << sqrt(d*d+f*f) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g) << ", "  << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g));
 | 
|---|
| [042f82] | 543 |       factors[0] = d;
 | 
|---|
 | 544 |       factors[1] = f;
 | 
|---|
 | 545 |       factors[2] = 0.;
 | 
|---|
| [d74077] | 546 |       FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
| [042f82] | 547 |       factors[1] = -0.5*f;
 | 
|---|
 | 548 |       factors[2] = g;
 | 
|---|
| [d74077] | 549 |       SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
| [042f82] | 550 |       factors[2] = -g;
 | 
|---|
| [d74077] | 551 |       ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
| [042f82] | 552 | 
 | 
|---|
 | 553 |       // rescale each to correct BondDistance
 | 
|---|
 | 554 | //      FirstOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 555 | //      SecondOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 556 | //      ThirdOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 557 | 
 | 
|---|
 | 558 |       // and relative to *origin atom
 | 
|---|
| [d74077] | 559 |       *FirstOtherAtom += TopOrigin->getPosition();
 | 
|---|
 | 560 |       *SecondOtherAtom += TopOrigin->getPosition();
 | 
|---|
 | 561 |       *ThirdOtherAtom += TopOrigin->getPosition();
 | 
|---|
| [042f82] | 562 | 
 | 
|---|
 | 563 |       // ... and add to molecule
 | 
|---|
 | 564 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
 | 565 |       AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
 | 
|---|
 | 566 |       AllWentWell = AllWentWell && AddAtom(ThirdOtherAtom);
 | 
|---|
| [47d041] | 567 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
 | 
|---|
 | 568 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
 | 
|---|
 | 569 | //      LOG(4, "INFO: Added " << *ThirdOtherAtom << " at: " << ThirdOtherAtom->x << ".");
 | 
|---|
| [042f82] | 570 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 571 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 572 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 573 |       Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
 | 
|---|
 | 574 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 575 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 576 |       Binder = AddBond(BottomOrigin, ThirdOtherAtom, 1);
 | 
|---|
 | 577 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 578 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 579 |       break;
 | 
|---|
 | 580 |     default:
 | 
|---|
| [47d041] | 581 |       ELOG(1, "BondDegree does not state single, double or triple bond!");
 | 
|---|
| [042f82] | 582 |       AllWentWell = false;
 | 
|---|
 | 583 |       break;
 | 
|---|
 | 584 |   }
 | 
|---|
 | 585 | 
 | 
|---|
 | 586 |   return AllWentWell;
 | 
|---|
| [14de469] | 587 | };
 | 
|---|
 | 588 | 
 | 
|---|
 | 589 | /** Creates a copy of this molecule.
 | 
|---|
| [c67ff9] | 590 |  * \param offset translation Vector for the new molecule relative to old one
 | 
|---|
| [14de469] | 591 |  * \return copy of molecule
 | 
|---|
 | 592 |  */
 | 
|---|
| [c67ff9] | 593 | molecule *molecule::CopyMolecule(const Vector &offset) const
 | 
|---|
| [14de469] | 594 | {
 | 
|---|
| [5f612ee] | 595 |   molecule *copy = World::getInstance().createMolecule();
 | 
|---|
| [042f82] | 596 | 
 | 
|---|
 | 597 |   // copy all atoms
 | 
|---|
| [30c753] | 598 |   std::map< const atom *, atom *> FatherFinder;
 | 
|---|
| [59fff1] | 599 |   for (iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
 | 600 |     atom * const copy_atom = copy->AddCopyAtom(*iter);
 | 
|---|
| [c67ff9] | 601 |     copy_atom->setPosition(copy_atom->getPosition() + offset);
 | 
|---|
| [30c753] | 602 |     FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
 | 
|---|
 | 603 |   }
 | 
|---|
| [042f82] | 604 | 
 | 
|---|
 | 605 |   // copy all bonds
 | 
|---|
| [30c753] | 606 |   for(const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
 | 
|---|
| [9d83b6] | 607 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
 | 608 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
 | 609 |         BondRunner != ListOfBonds.end();
 | 
|---|
 | 610 |         ++BondRunner)
 | 
|---|
| [e08c46] | 611 |       if ((*BondRunner)->leftatom == *AtomRunner) {
 | 
|---|
| [88c8ec] | 612 |         bond::ptr Binder = (*BondRunner);
 | 
|---|
| [e08c46] | 613 |         // get the pendant atoms of current bond in the copy molecule
 | 
|---|
| [30c753] | 614 |         ASSERT(FatherFinder.count(Binder->leftatom),
 | 
|---|
| [59fff1] | 615 |             "molecule::CopyMolecule() - No copy of original left atom "
 | 
|---|
 | 616 |             +toString(Binder->leftatom)+" for bond copy found");
 | 
|---|
| [30c753] | 617 |         ASSERT(FatherFinder.count(Binder->rightatom),
 | 
|---|
| [59fff1] | 618 |             "molecule::CopyMolecule() - No copy of original right atom "
 | 
|---|
 | 619 |             +toString(Binder->rightatom)+"  for bond copy found");
 | 
|---|
| [30c753] | 620 |         atom * const LeftAtom = FatherFinder[Binder->leftatom];
 | 
|---|
 | 621 |         atom * const RightAtom = FatherFinder[Binder->rightatom];
 | 
|---|
 | 622 | 
 | 
|---|
| [1f693d] | 623 |         bond::ptr const NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->getDegree());
 | 
|---|
| [e08c46] | 624 |         NewBond->Cyclic = Binder->Cyclic;
 | 
|---|
 | 625 |         if (Binder->Cyclic)
 | 
|---|
 | 626 |           copy->NoCyclicBonds++;
 | 
|---|
 | 627 |         NewBond->Type = Binder->Type;
 | 
|---|
 | 628 |       }
 | 
|---|
| [9d83b6] | 629 |   }
 | 
|---|
| [042f82] | 630 |   // correct fathers
 | 
|---|
| [30c753] | 631 |   //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
 | 
|---|
| [cee0b57] | 632 | 
 | 
|---|
| [042f82] | 633 |   return copy;
 | 
|---|
| [14de469] | 634 | };
 | 
|---|
 | 635 | 
 | 
|---|
| [89c8b2] | 636 | 
 | 
|---|
| [9df680] | 637 | /** Destroys all atoms inside this molecule.
 | 
|---|
 | 638 |  */
 | 
|---|
 | 639 | void molecule::removeAtomsinMolecule()
 | 
|---|
 | 640 | {
 | 
|---|
 | 641 |   // remove each atom from world
 | 
|---|
| [59fff1] | 642 |   for(iterator AtomRunner = begin(); !empty(); AtomRunner = begin())
 | 
|---|
| [9df680] | 643 |     World::getInstance().destroyAtom(*AtomRunner);
 | 
|---|
 | 644 | };
 | 
|---|
 | 645 | 
 | 
|---|
 | 646 | 
 | 
|---|
| [89c8b2] | 647 | /**
 | 
|---|
 | 648 |  * Copies all atoms of a molecule which are within the defined parallelepiped.
 | 
|---|
 | 649 |  *
 | 
|---|
 | 650 |  * @param offest for the origin of the parallelepiped
 | 
|---|
 | 651 |  * @param three vectors forming the matrix that defines the shape of the parallelpiped
 | 
|---|
 | 652 |  */
 | 
|---|
| [c550dd] | 653 | molecule* molecule::CopyMoleculeFromSubRegion(const Shape ®ion) const {
 | 
|---|
| [5f612ee] | 654 |   molecule *copy = World::getInstance().createMolecule();
 | 
|---|
| [89c8b2] | 655 | 
 | 
|---|
| [30c753] | 656 |   // copy all atoms
 | 
|---|
 | 657 |   std::map< const atom *, atom *> FatherFinder;
 | 
|---|
| [59fff1] | 658 |   for (iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
| [4e904b] | 659 |     if (region.isInside((*iter)->getPosition())) {
 | 
|---|
| [59fff1] | 660 |       atom * const copy_atom = copy->AddCopyAtom(*iter);
 | 
|---|
| [30c753] | 661 |       FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
 | 
|---|
| [9df5c6] | 662 |     }
 | 
|---|
 | 663 |   }
 | 
|---|
| [89c8b2] | 664 | 
 | 
|---|
| [30c753] | 665 |   // copy all bonds
 | 
|---|
 | 666 |   for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
 | 
|---|
 | 667 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
 | 668 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
 | 669 |         BondRunner != ListOfBonds.end();
 | 
|---|
 | 670 |         ++BondRunner)
 | 
|---|
 | 671 |       if ((*BondRunner)->leftatom == *AtomRunner) {
 | 
|---|
| [88c8ec] | 672 |         bond::ptr Binder = (*BondRunner);
 | 
|---|
| [30c753] | 673 |         if ((FatherFinder.count(Binder->leftatom))
 | 
|---|
 | 674 |             && (FatherFinder.count(Binder->rightatom))) {
 | 
|---|
 | 675 |           // if copy present, then it must be from subregion
 | 
|---|
 | 676 |           atom * const LeftAtom = FatherFinder[Binder->leftatom];
 | 
|---|
 | 677 |           atom * const RightAtom = FatherFinder[Binder->rightatom];
 | 
|---|
 | 678 | 
 | 
|---|
| [1f693d] | 679 |           bond::ptr const NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->getDegree());
 | 
|---|
| [30c753] | 680 |           NewBond->Cyclic = Binder->Cyclic;
 | 
|---|
 | 681 |           if (Binder->Cyclic)
 | 
|---|
 | 682 |             copy->NoCyclicBonds++;
 | 
|---|
 | 683 |           NewBond->Type = Binder->Type;
 | 
|---|
 | 684 |         }
 | 
|---|
 | 685 |       }
 | 
|---|
 | 686 |   }
 | 
|---|
 | 687 |   // correct fathers
 | 
|---|
 | 688 |   //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
 | 
|---|
 | 689 | 
 | 
|---|
| [e138de] | 690 |   //TODO: copy->BuildInducedSubgraph(this);
 | 
|---|
| [89c8b2] | 691 | 
 | 
|---|
 | 692 |   return copy;
 | 
|---|
 | 693 | }
 | 
|---|
 | 694 | 
 | 
|---|
| [14de469] | 695 | /** Adds a bond to a the molecule specified by two atoms, \a *first and \a *second.
 | 
|---|
 | 696 |  * Also updates molecule::BondCount and molecule::NoNonBonds.
 | 
|---|
 | 697 |  * \param *first first atom in bond
 | 
|---|
 | 698 |  * \param *second atom in bond
 | 
|---|
 | 699 |  * \return pointer to bond or NULL on failure
 | 
|---|
 | 700 |  */
 | 
|---|
| [88c8ec] | 701 | bond::ptr molecule::AddBond(atom *atom1, atom *atom2, int degree)
 | 
|---|
| [14de469] | 702 | {
 | 
|---|
| [7d82a5] | 703 |   bond::ptr Binder;
 | 
|---|
| [05a97c] | 704 | 
 | 
|---|
 | 705 |   // some checks to make sure we are able to create the bond
 | 
|---|
| [59fff1] | 706 |   ASSERT(atom1,
 | 
|---|
 | 707 |       "molecule::AddBond() - First atom "+toString(atom1)
 | 
|---|
 | 708 |       +" is not a invalid pointer");
 | 
|---|
 | 709 |   ASSERT(atom2,
 | 
|---|
 | 710 |       "molecule::AddBond() - Second atom "+toString(atom2)
 | 
|---|
 | 711 |       +" is not a invalid pointer");
 | 
|---|
 | 712 |   ASSERT(isInMolecule(atom1),
 | 
|---|
 | 713 |       "molecule::AddBond() - First atom "+toString(atom1)
 | 
|---|
 | 714 |       +" is not part of molecule");
 | 
|---|
 | 715 |   ASSERT(isInMolecule(atom2),
 | 
|---|
 | 716 |       "molecule::AddBond() - Second atom "+toString(atom2)
 | 
|---|
 | 717 |       +" is not part of molecule");
 | 
|---|
| [05a97c] | 718 | 
 | 
|---|
| [7d82a5] | 719 |   Binder.reset(new bond(atom1, atom2, degree));
 | 
|---|
| [073a9e4] | 720 |   atom1->RegisterBond(WorldTime::getTime(), Binder);
 | 
|---|
 | 721 |   atom2->RegisterBond(WorldTime::getTime(), Binder);
 | 
|---|
| [59fff1] | 722 |   if ((atom1->getType() != NULL)
 | 
|---|
 | 723 |       && (atom1->getType()->getAtomicNumber() != 1)
 | 
|---|
 | 724 |       && (atom2->getType() != NULL)
 | 
|---|
 | 725 |       && (atom2->getType()->getAtomicNumber() != 1))
 | 
|---|
| [05a97c] | 726 |     NoNonBonds++;
 | 
|---|
 | 727 | 
 | 
|---|
| [042f82] | 728 |   return Binder;
 | 
|---|
| [14de469] | 729 | };
 | 
|---|
 | 730 | 
 | 
|---|
| [1907a7] | 731 | /** Set molecule::name from the basename without suffix in the given \a *filename.
 | 
|---|
 | 732 |  * \param *filename filename
 | 
|---|
 | 733 |  */
 | 
|---|
| [d67150] | 734 | void molecule::SetNameFromFilename(const char *filename)
 | 
|---|
| [1907a7] | 735 | {
 | 
|---|
| [575343] | 736 |   OBSERVE;
 | 
|---|
| [1907a7] | 737 |   int length = 0;
 | 
|---|
| [f7f7a4] | 738 |   const char *molname = strrchr(filename, '/');
 | 
|---|
 | 739 |   if (molname != NULL)
 | 
|---|
 | 740 |     molname += sizeof(char);  // search for filename without dirs
 | 
|---|
 | 741 |   else
 | 
|---|
 | 742 |     molname = filename; // contains no slashes
 | 
|---|
| [49e1ae] | 743 |   const char *endname = strchr(molname, '.');
 | 
|---|
| [1907a7] | 744 |   if ((endname == NULL) || (endname < molname))
 | 
|---|
 | 745 |     length = strlen(molname);
 | 
|---|
 | 746 |   else
 | 
|---|
 | 747 |     length = strlen(molname) - strlen(endname);
 | 
|---|
| [35b698] | 748 |   cout << "Set name of molecule " << getId() << " to " << molname << endl;
 | 
|---|
| [1907a7] | 749 |   strncpy(name, molname, length);
 | 
|---|
| [d67150] | 750 |   name[length]='\0';
 | 
|---|
| [1907a7] | 751 | };
 | 
|---|
 | 752 | 
 | 
|---|
| [14de469] | 753 | /** Sets the molecule::cell_size to the components of \a *dim (rectangular box)
 | 
|---|
 | 754 |  * \param *dim vector class
 | 
|---|
 | 755 |  */
 | 
|---|
| [e9b8bb] | 756 | void molecule::SetBoxDimension(Vector *dim)
 | 
|---|
| [14de469] | 757 | {
 | 
|---|
| [cca9ef] | 758 |   RealSpaceMatrix domain;
 | 
|---|
| [84c494] | 759 |   for(int i =0; i<NDIM;++i)
 | 
|---|
 | 760 |     domain.at(i,i) = dim->at(i);
 | 
|---|
 | 761 |   World::getInstance().setDomain(domain);
 | 
|---|
| [14de469] | 762 | };
 | 
|---|
 | 763 | 
 | 
|---|
| [cee0b57] | 764 | /** Removes atom from molecule list, but does not delete it.
 | 
|---|
 | 765 |  * \param *pointer atom to be removed
 | 
|---|
 | 766 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
| [f3278b] | 767 |  */
 | 
|---|
| [cee0b57] | 768 | bool molecule::UnlinkAtom(atom *pointer)
 | 
|---|
| [f3278b] | 769 | {
 | 
|---|
| [cee0b57] | 770 |   if (pointer == NULL)
 | 
|---|
 | 771 |     return false;
 | 
|---|
| [2e4105] | 772 |   pointer->removeFromMolecule();
 | 
|---|
| [cee0b57] | 773 |   return true;
 | 
|---|
| [f3278b] | 774 | };
 | 
|---|
 | 775 | 
 | 
|---|
| [cee0b57] | 776 | /** Removes every atom from molecule list.
 | 
|---|
 | 777 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
| [14de469] | 778 |  */
 | 
|---|
| [cee0b57] | 779 | bool molecule::CleanupMolecule()
 | 
|---|
| [14de469] | 780 | {
 | 
|---|
| [9879f6] | 781 |   for (molecule::iterator iter = begin(); !empty(); iter = begin())
 | 
|---|
| [2e4105] | 782 |     (*iter)->removeFromMolecule();
 | 
|---|
| [274d45] | 783 |   return empty();
 | 
|---|
| [69eb71] | 784 | };
 | 
|---|
| [14de469] | 785 | 
 | 
|---|
| [cee0b57] | 786 | /** Finds an atom specified by its continuous number.
 | 
|---|
 | 787 |  * \param Nr number of atom withim molecule
 | 
|---|
 | 788 |  * \return pointer to atom or NULL
 | 
|---|
| [14de469] | 789 |  */
 | 
|---|
| [9879f6] | 790 | atom * molecule::FindAtom(int Nr)  const
 | 
|---|
 | 791 | {
 | 
|---|
| [c6ab91] | 792 |   LocalToGlobalId_t::const_iterator iter = LocalToGlobalId.find(Nr);
 | 
|---|
 | 793 |   if (iter != LocalToGlobalId.end()) {
 | 
|---|
| [47d041] | 794 |     //LOG(0, "Found Atom Nr. " << walker->getNr());
 | 
|---|
| [c6ab91] | 795 |     return iter->second;
 | 
|---|
| [cee0b57] | 796 |   } else {
 | 
|---|
| [ca8bea] | 797 |     ELOG(1, "Atom with Nr " << Nr << " not found in molecule " << getName() << "'s list.");
 | 
|---|
| [cee0b57] | 798 |     return NULL;
 | 
|---|
| [042f82] | 799 |   }
 | 
|---|
| [59fff1] | 800 | }
 | 
|---|
 | 801 | 
 | 
|---|
 | 802 | /** Checks whether the given atom is a member of this molecule.
 | 
|---|
 | 803 |  *
 | 
|---|
 | 804 |  *  We make use here of molecule::atomIds to get a result on
 | 
|---|
 | 805 |  *
 | 
|---|
 | 806 |  * @param _atom atom to check
 | 
|---|
 | 807 |  * @return true - is member, false - is not
 | 
|---|
 | 808 |  */
 | 
|---|
 | 809 | bool molecule::isInMolecule(const atom * const _atom)
 | 
|---|
 | 810 | {
 | 
|---|
 | 811 |   ASSERT(_atom->getMolecule() == this,
 | 
|---|
 | 812 |       "molecule::isInMolecule() - atom is not designated to be in molecule '"
 | 
|---|
 | 813 |       +toString(this->getName())+"'.");
 | 
|---|
| [8e1f901] | 814 |   molecule::const_iterator iter = atomIds.find(_atom->getId());
 | 
|---|
| [59fff1] | 815 |   return (iter != atomIds.end());
 | 
|---|
 | 816 | }
 | 
|---|
| [14de469] | 817 | 
 | 
|---|
| [cee0b57] | 818 | /** Asks for atom number, and checks whether in list.
 | 
|---|
 | 819 |  * \param *text question before entering
 | 
|---|
| [a6b7fb] | 820 |  */
 | 
|---|
| [955b91] | 821 | atom * molecule::AskAtom(std::string text)
 | 
|---|
| [a6b7fb] | 822 | {
 | 
|---|
| [cee0b57] | 823 |   int No;
 | 
|---|
 | 824 |   atom *ion = NULL;
 | 
|---|
 | 825 |   do {
 | 
|---|
| [47d041] | 826 |     //std::cout << "============Atom list==========================" << std::endl;
 | 
|---|
| [cee0b57] | 827 |     //mol->Output((ofstream *)&cout);
 | 
|---|
| [47d041] | 828 |     //std::cout << "===============================================" << std::endl;
 | 
|---|
 | 829 |     std::cout << text;
 | 
|---|
| [cee0b57] | 830 |     cin >> No;
 | 
|---|
 | 831 |     ion = this->FindAtom(No);
 | 
|---|
 | 832 |   } while (ion == NULL);
 | 
|---|
 | 833 |   return ion;
 | 
|---|
| [a6b7fb] | 834 | };
 | 
|---|
 | 835 | 
 | 
|---|
| [cee0b57] | 836 | /** Checks if given coordinates are within cell volume.
 | 
|---|
 | 837 |  * \param *x array of coordinates
 | 
|---|
 | 838 |  * \return true - is within, false - out of cell
 | 
|---|
| [14de469] | 839 |  */
 | 
|---|
| [cee0b57] | 840 | bool molecule::CheckBounds(const Vector *x) const
 | 
|---|
| [14de469] | 841 | {
 | 
|---|
| [cca9ef] | 842 |   const RealSpaceMatrix &domain = World::getInstance().getDomain().getM();
 | 
|---|
| [cee0b57] | 843 |   bool result = true;
 | 
|---|
 | 844 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| [84c494] | 845 |     result = result && ((x->at(i) >= 0) && (x->at(i) < domain.at(i,i)));
 | 
|---|
| [042f82] | 846 |   }
 | 
|---|
| [cee0b57] | 847 |   //return result;
 | 
|---|
 | 848 |   return true; /// probably not gonna use the check no more
 | 
|---|
| [69eb71] | 849 | };
 | 
|---|
| [14de469] | 850 | 
 | 
|---|
| [cee0b57] | 851 | /** Prints molecule to *out.
 | 
|---|
 | 852 |  * \param *out output stream
 | 
|---|
| [14de469] | 853 |  */
 | 
|---|
| [e4afb4] | 854 | bool molecule::Output(ostream * const output) const
 | 
|---|
| [14de469] | 855 | {
 | 
|---|
| [e138de] | 856 |   if (output == NULL) {
 | 
|---|
| [cee0b57] | 857 |     return false;
 | 
|---|
 | 858 |   } else {
 | 
|---|
| [0ba410] | 859 |     int AtomNo[MAX_ELEMENTS];
 | 
|---|
 | 860 |     memset(AtomNo,0,(MAX_ELEMENTS-1)*sizeof(*AtomNo));
 | 
|---|
 | 861 |     enumeration<const element*> elementLookup = formula.enumerateElements();
 | 
|---|
 | 862 |     *output << "#Ion_TypeNr._Nr.R[0]    R[1]    R[2]    MoveType (0 MoveIon, 1 FixedIon)" << endl;
 | 
|---|
| [30c753] | 863 |     for_each(begin(),end(),boost::bind(&atom::OutputArrayIndexed,_1,output,elementLookup,AtomNo,(const char*)0));
 | 
|---|
| [cee0b57] | 864 |     return true;
 | 
|---|
| [042f82] | 865 |   }
 | 
|---|
| [14de469] | 866 | };
 | 
|---|
 | 867 | 
 | 
|---|
| [266237] | 868 | /** Outputs contents of each atom::ListOfBonds.
 | 
|---|
| [cee0b57] | 869 |  * \param *out output stream
 | 
|---|
| [14de469] | 870 |  */
 | 
|---|
| [e138de] | 871 | void molecule::OutputListOfBonds() const
 | 
|---|
| [14de469] | 872 | {
 | 
|---|
| [4b5cf8] | 873 |   std::stringstream output;
 | 
|---|
 | 874 |   LOG(2, "From Contents of ListOfBonds, all atoms:");
 | 
|---|
 | 875 |   for (molecule::const_iterator iter = begin();
 | 
|---|
 | 876 |       iter != end();
 | 
|---|
 | 877 |       ++iter) {
 | 
|---|
 | 878 |     (*iter)->OutputBondOfAtom(output);
 | 
|---|
 | 879 |     output << std::endl << "\t\t";
 | 
|---|
 | 880 |   }
 | 
|---|
 | 881 |   LOG(2, output.str());
 | 
|---|
 | 882 | }
 | 
|---|
| [14de469] | 883 | 
 | 
|---|
| [cee0b57] | 884 | /** Brings molecule::AtomCount and atom::*Name up-to-date.
 | 
|---|
| [14de469] | 885 |  * \param *out output stream for debugging
 | 
|---|
 | 886 |  */
 | 
|---|
| [e791dc] | 887 | size_t molecule::doCountNoNonHydrogen() const
 | 
|---|
| [14de469] | 888 | {
 | 
|---|
| [e791dc] | 889 |   int temp = 0;
 | 
|---|
| [560bbe] | 890 |   // go through atoms and look for new ones
 | 
|---|
 | 891 |   for (molecule::const_iterator iter = begin(); iter != end(); ++iter)
 | 
|---|
| [83f176] | 892 |     if ((*iter)->getType()->getAtomicNumber() != 1) // count non-hydrogen atoms whilst at it
 | 
|---|
| [e791dc] | 893 |       ++temp;
 | 
|---|
 | 894 |   return temp;
 | 
|---|
| [cee0b57] | 895 | };
 | 
|---|
| [042f82] | 896 | 
 | 
|---|
| [458c31] | 897 | /** Counts the number of present bonds.
 | 
|---|
 | 898 |  * \return number of bonds
 | 
|---|
 | 899 |  */
 | 
|---|
 | 900 | int molecule::doCountBonds() const
 | 
|---|
 | 901 | {
 | 
|---|
 | 902 |   unsigned int counter = 0;
 | 
|---|
 | 903 |   for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
 | 
|---|
 | 904 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
 | 905 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
 | 906 |         BondRunner != ListOfBonds.end();
 | 
|---|
 | 907 |         ++BondRunner)
 | 
|---|
 | 908 |       if ((*BondRunner)->leftatom == *AtomRunner)
 | 
|---|
 | 909 |         counter++;
 | 
|---|
 | 910 |   }
 | 
|---|
 | 911 |   return counter;
 | 
|---|
 | 912 | }
 | 
|---|
 | 913 | 
 | 
|---|
 | 914 | 
 | 
|---|
| [14de469] | 915 | /** Returns an index map for two father-son-molecules.
 | 
|---|
 | 916 |  * The map tells which atom in this molecule corresponds to which one in the other molecul with their fathers.
 | 
|---|
 | 917 |  * \param *out output stream for debugging
 | 
|---|
 | 918 |  * \param *OtherMolecule corresponding molecule with fathers
 | 
|---|
 | 919 |  * \return allocated map of size molecule::AtomCount with map
 | 
|---|
 | 920 |  * \todo make this with a good sort O(n), not O(n^2)
 | 
|---|
 | 921 |  */
 | 
|---|
| [e138de] | 922 | int * molecule::GetFatherSonAtomicMap(molecule *OtherMolecule)
 | 
|---|
| [14de469] | 923 | {
 | 
|---|
| [47d041] | 924 |   LOG(3, "Begin of GetFatherAtomicMap.");
 | 
|---|
| [1024cb] | 925 |   int *AtomicMap = new int[getAtomCount()];
 | 
|---|
| [ea7176] | 926 |   for (int i=getAtomCount();i--;)
 | 
|---|
| [042f82] | 927 |     AtomicMap[i] = -1;
 | 
|---|
 | 928 |   if (OtherMolecule == this) {  // same molecule
 | 
|---|
| [ea7176] | 929 |     for (int i=getAtomCount();i--;) // no need as -1 means already that there is trivial correspondence
 | 
|---|
| [042f82] | 930 |       AtomicMap[i] = i;
 | 
|---|
| [47d041] | 931 |     LOG(4, "Map is trivial.");
 | 
|---|
| [042f82] | 932 |   } else {
 | 
|---|
| [47d041] | 933 |     std::stringstream output;
 | 
|---|
 | 934 |     output << "Map is ";
 | 
|---|
| [9879f6] | 935 |     for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
 | 936 |       if ((*iter)->father == NULL) {
 | 
|---|
| [735b1c] | 937 |         AtomicMap[(*iter)->getNr()] = -2;
 | 
|---|
| [042f82] | 938 |       } else {
 | 
|---|
| [9879f6] | 939 |         for (molecule::const_iterator runner = OtherMolecule->begin(); runner != OtherMolecule->end(); ++runner) {
 | 
|---|
| [042f82] | 940 |       //for (int i=0;i<AtomCount;i++) { // search atom
 | 
|---|
| [1024cb] | 941 |         //for (int j=0;j<OtherMolecule->getAtomCount();j++) {
 | 
|---|
| [47d041] | 942 |           //LOG(4, "Comparing father " << (*iter)->father << " with the other one " << (*runner)->father << ".");
 | 
|---|
| [9879f6] | 943 |           if ((*iter)->father == (*runner))
 | 
|---|
| [735b1c] | 944 |             AtomicMap[(*iter)->getNr()] = (*runner)->getNr();
 | 
|---|
| [042f82] | 945 |         }
 | 
|---|
 | 946 |       }
 | 
|---|
| [47d041] | 947 |       output << AtomicMap[(*iter)->getNr()] << "\t";
 | 
|---|
| [042f82] | 948 |     }
 | 
|---|
| [47d041] | 949 |     LOG(4, output.str());
 | 
|---|
| [042f82] | 950 |   }
 | 
|---|
| [47d041] | 951 |   LOG(3, "End of GetFatherAtomicMap.");
 | 
|---|
| [042f82] | 952 |   return AtomicMap;
 | 
|---|
| [14de469] | 953 | };
 | 
|---|
 | 954 | 
 | 
|---|
| [4a7776a] | 955 | 
 | 
|---|
| [c68025] | 956 | void molecule::flipActiveFlag(){
 | 
|---|
 | 957 |   ActiveFlag = !ActiveFlag;
 | 
|---|
 | 958 | }
 | 
|---|
| [560bbe] | 959 | 
 | 
|---|
| [55feea1] | 960 | Shape molecule::getBoundingShape(const double boundary) const
 | 
|---|
| [c67ff9] | 961 | {
 | 
|---|
 | 962 |   // get center and radius
 | 
|---|
 | 963 |   Vector center;
 | 
|---|
 | 964 |   double radius = 0.;
 | 
|---|
 | 965 |   {
 | 
|---|
 | 966 |     center.Zero();
 | 
|---|
 | 967 |     for(const_iterator iter = begin(); iter != end(); ++iter)
 | 
|---|
 | 968 |       center += (*iter)->getPosition();
 | 
|---|
 | 969 |     center *= 1./(double)size();
 | 
|---|
 | 970 |     for(const_iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
 | 971 |       const Vector &position = (*iter)->getPosition();
 | 
|---|
 | 972 |       const double temp_distance = position.DistanceSquared(center);
 | 
|---|
 | 973 |       if (temp_distance > radius)
 | 
|---|
 | 974 |         radius = temp_distance;
 | 
|---|
 | 975 |     }
 | 
|---|
 | 976 |   }
 | 
|---|
 | 977 |   // convert radius to true value and add some small boundary
 | 
|---|
| [55feea1] | 978 |   radius = sqrt(radius) + boundary + 1e+6*std::numeric_limits<double>::epsilon();
 | 
|---|
| [c67ff9] | 979 |   LOG(1, "INFO: The " << size() << " atoms of the molecule are contained in a sphere at "
 | 
|---|
 | 980 |       << center << " with radius " << radius << ".");
 | 
|---|
 | 981 | 
 | 
|---|
| [f24af7] | 982 |   // TODO: When we do not use a Sphere here anymore, then FillRegularGridAction will
 | 
|---|
 | 983 |   // will not work as it expects a sphere due to possible random rotations.
 | 
|---|
| [c67ff9] | 984 |   Shape BoundingShape(Sphere(center, radius));
 | 
|---|
 | 985 |   LOG(1, "INFO: Created sphere at " << BoundingShape.getCenter() << " and radius "
 | 
|---|
 | 986 |       << BoundingShape.getRadius() << ".");
 | 
|---|
 | 987 |   return BoundingShape;
 | 
|---|
 | 988 | }
 | 
|---|
 | 989 | 
 | 
|---|
| [560bbe] | 990 | // construct idpool
 | 
|---|
 | 991 | CONSTRUCT_IDPOOL(atomId_t, continuousId)
 | 
|---|
| [c67ff9] | 992 | 
 | 
|---|