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