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