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