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