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