| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [d103d3] | 4 |  * Copyright (C)  2010-2011 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 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 | 
 | 
|---|
| [6f0841] | 28 | #include "Atom/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"
 | 
|---|
| [3bdb6d] | 34 | #include "Element/element.hpp"
 | 
|---|
| [53c7fc] | 35 | #include "Element/periodentafel.hpp"
 | 
|---|
| [129204] | 36 | #include "Graph/BondGraph.hpp"
 | 
|---|
| [783e88] | 37 | #include "LinearAlgebra/Exceptions.hpp"
 | 
|---|
| [13d150] | 38 | #include "LinearAlgebra/leastsquaremin.hpp"
 | 
|---|
| [9d83b6] | 39 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
 | 40 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
 | 41 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [53c7fc] | 42 | #include "LinkedCell/linkedcell.hpp"
 | 
|---|
| [cee0b57] | 43 | #include "molecule.hpp"
 | 
|---|
| [d127c8] | 44 | #include "Tesselation/tesselation.hpp"
 | 
|---|
| [b34306] | 45 | #include "World.hpp"
 | 
|---|
| [9d83b6] | 46 | #include "WorldTime.hpp"
 | 
|---|
| [14de469] | 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 |  */
 | 
|---|
| [cd5047] | 54 | molecule::molecule(const periodentafel * const teil) :
 | 
|---|
 | 55 |   Observable("molecule"),
 | 
|---|
| [458c31] | 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)
 | 
|---|
| [69eb71] | 66 | {
 | 
|---|
| [fa649a] | 67 | 
 | 
|---|
| [387b36] | 68 |   strcpy(name,World::getInstance().getDefaultName().c_str());
 | 
|---|
| [14de469] | 69 | };
 | 
|---|
 | 70 | 
 | 
|---|
| [cbc5fb] | 71 | molecule *NewMolecule(){
 | 
|---|
| [23b547] | 72 |   return new molecule(World::getInstance().getPeriode());
 | 
|---|
| [cbc5fb] | 73 | }
 | 
|---|
 | 74 | 
 | 
|---|
| [14de469] | 75 | /** Destructor of class molecule.
 | 
|---|
 | 76 |  * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
 | 
|---|
 | 77 |  */
 | 
|---|
| [69eb71] | 78 | molecule::~molecule()
 | 
|---|
| [14de469] | 79 | {
 | 
|---|
| [042f82] | 80 |   CleanupMolecule();
 | 
|---|
| [14de469] | 81 | };
 | 
|---|
 | 82 | 
 | 
|---|
| [357fba] | 83 | 
 | 
|---|
| [cbc5fb] | 84 | void DeleteMolecule(molecule *mol){
 | 
|---|
 | 85 |   delete mol;
 | 
|---|
 | 86 | }
 | 
|---|
 | 87 | 
 | 
|---|
| [520c8b] | 88 | // getter and setter
 | 
|---|
| [73a857] | 89 | const std::string molecule::getName() const{
 | 
|---|
| [520c8b] | 90 |   return std::string(name);
 | 
|---|
 | 91 | }
 | 
|---|
 | 92 | 
 | 
|---|
| [ea7176] | 93 | int molecule::getAtomCount() const{
 | 
|---|
 | 94 |   return *AtomCount;
 | 
|---|
 | 95 | }
 | 
|---|
 | 96 | 
 | 
|---|
| [458c31] | 97 | int molecule::getBondCount() const{
 | 
|---|
 | 98 |   return *BondCount;
 | 
|---|
 | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
| [520c8b] | 101 | void molecule::setName(const std::string _name){
 | 
|---|
| [2ba827] | 102 |   OBSERVE;
 | 
|---|
| [35b698] | 103 |   cout << "Set name of molecule " << getId() << " to " << _name << endl;
 | 
|---|
| [520c8b] | 104 |   strncpy(name,_name.c_str(),MAXSTRINGSIZE);
 | 
|---|
 | 105 | }
 | 
|---|
 | 106 | 
 | 
|---|
| [a7a087] | 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 | 
 | 
|---|
| [73a857] | 120 | moleculeId_t molecule::getId() const {
 | 
|---|
| [cbc5fb] | 121 |   return id;
 | 
|---|
 | 122 | }
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 | void molecule::setId(moleculeId_t _id){
 | 
|---|
 | 125 |   id =_id;
 | 
|---|
 | 126 | }
 | 
|---|
 | 127 | 
 | 
|---|
| [73a857] | 128 | const Formula &molecule::getFormula() const {
 | 
|---|
| [f17e1c] | 129 |   return formula;
 | 
|---|
| [ac9b56] | 130 | }
 | 
|---|
 | 131 | 
 | 
|---|
| [73a857] | 132 | unsigned int molecule::getElementCount() const{
 | 
|---|
| [389cc8] | 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 | 
 | 
|---|
| [bd58fb] | 148 | /************************** Access to the List of Atoms ****************/
 | 
|---|
 | 149 | 
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 | molecule::iterator molecule::begin(){
 | 
|---|
| [30c753] | 152 |   return iterator(atomIds.begin(), FromIdToAtom());
 | 
|---|
| [bd58fb] | 153 | }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | molecule::const_iterator molecule::begin() const{
 | 
|---|
| [30c753] | 156 |   return const_iterator(atomIds.begin(), FromIdToAtom());
 | 
|---|
| [bd58fb] | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
| [9879f6] | 159 | molecule::iterator molecule::end(){
 | 
|---|
| [30c753] | 160 |   return iterator(atomIds.end(), FromIdToAtom());
 | 
|---|
| [bd58fb] | 161 | }
 | 
|---|
 | 162 | 
 | 
|---|
| [9879f6] | 163 | molecule::const_iterator molecule::end() const{
 | 
|---|
| [30c753] | 164 |   return const_iterator(atomIds.end(), FromIdToAtom());
 | 
|---|
| [bd58fb] | 165 | }
 | 
|---|
| [520c8b] | 166 | 
 | 
|---|
| [9879f6] | 167 | bool molecule::empty() const
 | 
|---|
 | 168 | {
 | 
|---|
| [30c753] | 169 |   return (atomIds.empty());
 | 
|---|
| [9879f6] | 170 | }
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 | size_t molecule::size() const
 | 
|---|
 | 173 | {
 | 
|---|
 | 174 |   size_t counter = 0;
 | 
|---|
| [59fff1] | 175 |   for (const_iterator iter = begin(); iter != end (); ++iter)
 | 
|---|
| [9879f6] | 176 |     counter++;
 | 
|---|
 | 177 |   return counter;
 | 
|---|
 | 178 | }
 | 
|---|
 | 179 | 
 | 
|---|
 | 180 | molecule::const_iterator molecule::erase( const_iterator loc )
 | 
|---|
 | 181 | {
 | 
|---|
| [bf8e20] | 182 |   OBSERVE;
 | 
|---|
| [59fff1] | 183 |   const_iterator iter = loc;
 | 
|---|
| [30c753] | 184 |   ++iter;
 | 
|---|
| [59fff1] | 185 |   atom * const _atom = const_cast<atom *>(*loc);
 | 
|---|
 | 186 |   atomIds.erase( _atom->getId() );
 | 
|---|
 | 187 |   formula-=_atom->getType();
 | 
|---|
 | 188 |   _atom->removeFromMolecule();
 | 
|---|
| [9879f6] | 189 |   return iter;
 | 
|---|
 | 190 | }
 | 
|---|
 | 191 | 
 | 
|---|
| [6cfa36] | 192 | molecule::const_iterator molecule::erase( atom * key )
 | 
|---|
| [9879f6] | 193 | {
 | 
|---|
| [bf8e20] | 194 |   OBSERVE;
 | 
|---|
| [59fff1] | 195 |   const_iterator iter = find(key);
 | 
|---|
| [a7b761b] | 196 |   if (iter != end()){
 | 
|---|
| [30c753] | 197 |     ++iter;
 | 
|---|
| [274d45] | 198 |     atomIds.erase( key->getId() );
 | 
|---|
| [8f4df1] | 199 |     formula-=key->getType();
 | 
|---|
| [6cfa36] | 200 |     key->removeFromMolecule();
 | 
|---|
| [a7b761b] | 201 |   }
 | 
|---|
 | 202 |   return iter;
 | 
|---|
| [9879f6] | 203 | }
 | 
|---|
 | 204 | 
 | 
|---|
| [6cfa36] | 205 | molecule::const_iterator molecule::find ( atom * key ) const
 | 
|---|
| [9879f6] | 206 | {
 | 
|---|
| [30c753] | 207 |   return const_iterator(atomIds.find(key->getId()), FromIdToAtom());
 | 
|---|
| [9879f6] | 208 | }
 | 
|---|
 | 209 | 
 | 
|---|
 | 210 | pair<molecule::iterator,bool> molecule::insert ( atom * const key )
 | 
|---|
 | 211 | {
 | 
|---|
| [bf8e20] | 212 |   OBSERVE;
 | 
|---|
| [274d45] | 213 |   pair<atomIdSet::iterator,bool> res = atomIds.insert(key->getId());
 | 
|---|
 | 214 |   if (res.second) { // push atom if went well
 | 
|---|
| [8f4df1] | 215 |     formula+=key->getType();
 | 
|---|
| [30c753] | 216 |     return pair<iterator,bool>(iterator(res.first, FromIdToAtom()),res.second);
 | 
|---|
| [274d45] | 217 |   } else {
 | 
|---|
| [30c753] | 218 |     return pair<iterator,bool>(end(),res.second);
 | 
|---|
| [274d45] | 219 |   }
 | 
|---|
| [9879f6] | 220 | }
 | 
|---|
| [520c8b] | 221 | 
 | 
|---|
| [6cfa36] | 222 | bool molecule::containsAtom(atom* key){
 | 
|---|
| [274d45] | 223 |   return (find(key) != end());
 | 
|---|
| [6cfa36] | 224 | }
 | 
|---|
 | 225 | 
 | 
|---|
| [9317be] | 226 | World::AtomComposite molecule::getAtomSet() const
 | 
|---|
| [3738f0] | 227 | {
 | 
|---|
| [9317be] | 228 |   World::AtomComposite vector_of_atoms;
 | 
|---|
| [30c753] | 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)
 | 
|---|
| [59fff1] | 235 |   for (molecule::iterator iter = begin(); iter != end(); ++iter)
 | 
|---|
| [30c753] | 236 |     vector_of_atoms.push_back(*iter);
 | 
|---|
| [3738f0] | 237 |   return vector_of_atoms;
 | 
|---|
 | 238 | }
 | 
|---|
 | 239 | 
 | 
|---|
| [14de469] | 240 | /** Adds given atom \a *pointer from molecule list.
 | 
|---|
| [69eb71] | 241 |  * Increases molecule::last_atom and gives last number to added atom and names it according to its element::abbrev and molecule::AtomCount
 | 
|---|
| [14de469] | 242 |  * \param *pointer allocated and set atom
 | 
|---|
 | 243 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
 | 244 |  */
 | 
|---|
 | 245 | bool molecule::AddAtom(atom *pointer)
 | 
|---|
| [69eb71] | 246 | {
 | 
|---|
| [2ba827] | 247 |   OBSERVE;
 | 
|---|
| [042f82] | 248 |   if (pointer != NULL) {
 | 
|---|
| [d74077] | 249 |     if (pointer->getType() != NULL) {
 | 
|---|
| [83f176] | 250 |       if (pointer->getType()->getAtomicNumber() != 1)
 | 
|---|
| [042f82] | 251 |         NoNonHydrogen++;
 | 
|---|
| [68f03d] | 252 |       if(pointer->getName() == "Unknown"){
 | 
|---|
 | 253 |         stringstream sstr;
 | 
|---|
| [735b1c] | 254 |         sstr << pointer->getType()->getSymbol() << pointer->getNr()+1;
 | 
|---|
| [68f03d] | 255 |         pointer->setName(sstr.str());
 | 
|---|
| [042f82] | 256 |       }
 | 
|---|
 | 257 |     }
 | 
|---|
| [9879f6] | 258 |     insert(pointer);
 | 
|---|
| [6cfa36] | 259 |     pointer->setMolecule(this);
 | 
|---|
| [f721c6] | 260 |   }
 | 
|---|
| [9879f6] | 261 |   return true;
 | 
|---|
| [14de469] | 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
 | 
|---|
| [89c8b2] | 267 |  * \return pointer to the newly added atom
 | 
|---|
| [14de469] | 268 |  */
 | 
|---|
 | 269 | atom * molecule::AddCopyAtom(atom *pointer)
 | 
|---|
| [69eb71] | 270 | {
 | 
|---|
| [f721c6] | 271 |   atom *retval = NULL;
 | 
|---|
| [2ba827] | 272 |   OBSERVE;
 | 
|---|
| [042f82] | 273 |   if (pointer != NULL) {
 | 
|---|
| [46d958] | 274 |     atom *walker = pointer->clone();
 | 
|---|
| [a7b761b] | 275 |     walker->setName(pointer->getName());
 | 
|---|
| [a479fa] | 276 |     walker->setNr(last_atom++);  // increase number within molecule
 | 
|---|
| [9879f6] | 277 |     insert(walker);
 | 
|---|
| [83f176] | 278 |     if ((pointer->getType() != NULL) && (pointer->getType()->getAtomicNumber() != 1))
 | 
|---|
| [042f82] | 279 |       NoNonHydrogen++;
 | 
|---|
| [e8926e] | 280 |     walker->setMolecule(this);
 | 
|---|
| [f721c6] | 281 |     retval=walker;
 | 
|---|
 | 282 |   }
 | 
|---|
 | 283 |   return retval;
 | 
|---|
| [14de469] | 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
 | 
|---|
| [042f82] | 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.
 | 
|---|
| [14de469] | 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
 | 
|---|
| [042f82] | 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$.
 | 
|---|
| [69eb71] | 310 |  *
 | 
|---|
| [14de469] | 311 |  * \param *out output stream for debugging
 | 
|---|
| [69eb71] | 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)
 | 
|---|
| [14de469] | 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 |  */
 | 
|---|
| [e138de] | 320 | bool molecule::AddHydrogenReplacementAtom(bond *TopBond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem)
 | 
|---|
| [14de469] | 321 | {
 | 
|---|
| [47d041] | 322 | //  Info info(__func__);
 | 
|---|
| [f721c6] | 323 |   bool AllWentWell = true;    // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
 | 
|---|
| [2ba827] | 324 |   OBSERVE;
 | 
|---|
| [042f82] | 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
 | 
|---|
| [cca9ef] | 333 |   const RealSpaceMatrix &matrix =  World::getInstance().getDomain().getM();
 | 
|---|
| [266237] | 334 |   bond *Binder = NULL;
 | 
|---|
| [042f82] | 335 | 
 | 
|---|
 | 336 |   // create vector in direction of bond
 | 
|---|
| [d74077] | 337 |   InBondvector = TopReplacement->getPosition() - TopOrigin->getPosition();
 | 
|---|
| [042f82] | 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!
 | 
|---|
| [300220] | 343 |   const BondGraph * const BG = World::getInstance().getBondGraph();
 | 
|---|
| [607eab] | 344 |   const range<double> MinMaxBondDistance(
 | 
|---|
 | 345 |       BG->getMinMaxDistance(TopOrigin,TopReplacement));
 | 
|---|
| [300220] | 346 |   if (!MinMaxBondDistance.isInRange(bondlength)) {
 | 
|---|
| [47d041] | 347 | //    LOG(4, "InBondvector is: " << InBondvector << ".");
 | 
|---|
| [042f82] | 348 |     Orthovector1.Zero();
 | 
|---|
 | 349 |     for (int i=NDIM;i--;) {
 | 
|---|
| [d74077] | 350 |       l = TopReplacement->at(i) - TopOrigin->at(i);
 | 
|---|
| [300220] | 351 |       if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here)
 | 
|---|
| [0a4f7f] | 352 |         Orthovector1[i] = (l < 0) ? -1. : +1.;
 | 
|---|
| [042f82] | 353 |       } // (signs are correct, was tested!)
 | 
|---|
 | 354 |     }
 | 
|---|
| [5108e1] | 355 |     Orthovector1 *= matrix;
 | 
|---|
| [1bd79e] | 356 |     InBondvector -= Orthovector1; // subtract just the additional translation
 | 
|---|
| [042f82] | 357 |     bondlength = InBondvector.Norm();
 | 
|---|
| [47d041] | 358 | //    LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << ".");
 | 
|---|
| [042f82] | 359 |   } // periodic correction finished
 | 
|---|
 | 360 | 
 | 
|---|
 | 361 |   InBondvector.Normalize();
 | 
|---|
 | 362 |   // get typical bond length and store as scale factor for later
 | 
|---|
| [d74077] | 363 |   ASSERT(TopOrigin->getType() != NULL, "AddHydrogenReplacementAtom: element of TopOrigin is not given.");
 | 
|---|
| [83f176] | 364 |   BondRescale = TopOrigin->getType()->getHBondDistance(TopBond->BondDegree-1);
 | 
|---|
| [042f82] | 365 |   if (BondRescale == -1) {
 | 
|---|
| [47d041] | 366 |     ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->getName() << "<->" << TopReplacement->getName() << ") of degree " << TopBond->BondDegree << "!");
 | 
|---|
| [2ba827] | 367 |     return false;
 | 
|---|
| [042f82] | 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:
 | 
|---|
| [23b547] | 377 |       FirstOtherAtom = World::getInstance().createAtom();    // new atom
 | 
|---|
| [d74077] | 378 |       FirstOtherAtom->setType(1);  // element is Hydrogen
 | 
|---|
| [bce72c] | 379 |       FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 380 |       FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [83f176] | 381 |       if (TopReplacement->getType()->getAtomicNumber() == 1) { // neither rescale nor replace if it's already hydrogen
 | 
|---|
| [042f82] | 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 |       }
 | 
|---|
| [1bd79e] | 387 |       InBondvector *= BondRescale;   // rescale the distance vector to Hydrogen bond length
 | 
|---|
| [d74077] | 388 |       FirstOtherAtom->setPosition(TopOrigin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom
 | 
|---|
| [042f82] | 389 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
| [47d041] | 390 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
 | 
|---|
| [042f82] | 391 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 392 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 393 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 394 |       break;
 | 
|---|
 | 395 |     case 2:
 | 
|---|
| [9d83b6] | 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 {
 | 
|---|
| [47d041] | 410 |               ELOG(2, "Detected more than four bonds for atom " << TopOrigin->getName());
 | 
|---|
| [9d83b6] | 411 |             }
 | 
|---|
| [042f82] | 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
 | 
|---|
| [47d041] | 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.");
 | 
|---|
| [042f82] | 421 | 
 | 
|---|
 | 422 |         // determine the plane of these two with the *origin
 | 
|---|
| [0a4f7f] | 423 |         try {
 | 
|---|
| [783e88] | 424 |           Orthovector1 = Plane(TopOrigin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal();
 | 
|---|
| [0a4f7f] | 425 |         }
 | 
|---|
 | 426 |         catch(LinearDependenceException &excp){
 | 
|---|
| [47d041] | 427 |           LOG(0, boost::diagnostic_information(excp));
 | 
|---|
| [0a4f7f] | 428 |           // TODO: figure out what to do with the Orthovector in this case
 | 
|---|
 | 429 |           AllWentWell = false;
 | 
|---|
 | 430 |         }
 | 
|---|
| [042f82] | 431 |       } else {
 | 
|---|
| [273382] | 432 |         Orthovector1.GetOneNormalVector(InBondvector);
 | 
|---|
| [042f82] | 433 |       }
 | 
|---|
| [47d041] | 434 |       //LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
 | 
|---|
| [042f82] | 435 |       // orthogonal vector and bond vector between origin and replacement form the new plane
 | 
|---|
| [0a4f7f] | 436 |       Orthovector1.MakeNormalTo(InBondvector);
 | 
|---|
| [042f82] | 437 |       Orthovector1.Normalize();
 | 
|---|
| [47d041] | 438 |       //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << ".");
 | 
|---|
| [042f82] | 439 | 
 | 
|---|
 | 440 |       // create the two Hydrogens ...
 | 
|---|
| [23b547] | 441 |       FirstOtherAtom = World::getInstance().createAtom();
 | 
|---|
 | 442 |       SecondOtherAtom = World::getInstance().createAtom();
 | 
|---|
| [d74077] | 443 |       FirstOtherAtom->setType(1);
 | 
|---|
 | 444 |       SecondOtherAtom->setType(1);
 | 
|---|
| [bce72c] | 445 |       FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 446 |       FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [bce72c] | 447 |       SecondOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 448 |       SecondOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [042f82] | 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
 | 
|---|
| [83f176] | 451 |       bondangle = TopOrigin->getType()->getHBondAngle(1);
 | 
|---|
| [042f82] | 452 |       if (bondangle == -1) {
 | 
|---|
| [47d041] | 453 |         ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->getName() << "<->" << TopReplacement->getName() << ") of degree " << TopBond->BondDegree << "!");
 | 
|---|
| [2ba827] | 454 |         return false;
 | 
|---|
| [042f82] | 455 |         bondangle = 0;
 | 
|---|
 | 456 |       }
 | 
|---|
 | 457 |       bondangle *= M_PI/180./2.;
 | 
|---|
| [47d041] | 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));
 | 
|---|
| [d74077] | 460 |       FirstOtherAtom->Zero();
 | 
|---|
 | 461 |       SecondOtherAtom->Zero();
 | 
|---|
| [042f82] | 462 |       for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
 | 
|---|
| [d74077] | 463 |         FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle)));
 | 
|---|
 | 464 |         SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle)));
 | 
|---|
| [042f82] | 465 |       }
 | 
|---|
| [d74077] | 466 |       FirstOtherAtom->Scale(BondRescale);  // rescale by correct BondDistance
 | 
|---|
 | 467 |       SecondOtherAtom->Scale(BondRescale);
 | 
|---|
| [47d041] | 468 |       //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << ".");
 | 
|---|
| [d74077] | 469 |       *FirstOtherAtom += TopOrigin->getPosition();
 | 
|---|
 | 470 |       *SecondOtherAtom += TopOrigin->getPosition();
 | 
|---|
| [042f82] | 471 |       // ... and add to molecule
 | 
|---|
 | 472 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
 | 473 |       AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
 | 
|---|
| [47d041] | 474 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
 | 
|---|
 | 475 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
 | 
|---|
| [042f82] | 476 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 477 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 478 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 479 |       Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
 | 
|---|
 | 480 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 481 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 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)
 | 
|---|
| [23b547] | 485 |       FirstOtherAtom = World::getInstance().createAtom();
 | 
|---|
 | 486 |       SecondOtherAtom = World::getInstance().createAtom();
 | 
|---|
 | 487 |       ThirdOtherAtom = World::getInstance().createAtom();
 | 
|---|
| [d74077] | 488 |       FirstOtherAtom->setType(1);
 | 
|---|
 | 489 |       SecondOtherAtom->setType(1);
 | 
|---|
 | 490 |       ThirdOtherAtom->setType(1);
 | 
|---|
| [bce72c] | 491 |       FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 492 |       FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [bce72c] | 493 |       SecondOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 494 |       SecondOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [bce72c] | 495 |       ThirdOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
 | 
|---|
| [6625c3] | 496 |       ThirdOtherAtom->setFixedIon(TopReplacement->getFixedIon());
 | 
|---|
| [042f82] | 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
 | 
|---|
| [273382] | 502 |       AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector);
 | 
|---|
| [47d041] | 503 | //      LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
 | 
|---|
| [0a4f7f] | 504 |       try{
 | 
|---|
 | 505 |         Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal();
 | 
|---|
 | 506 |       }
 | 
|---|
 | 507 |       catch(LinearDependenceException &excp) {
 | 
|---|
| [47d041] | 508 |         LOG(0, boost::diagnostic_information(excp));
 | 
|---|
| [0a4f7f] | 509 |         AllWentWell = false;
 | 
|---|
 | 510 |       }
 | 
|---|
| [47d041] | 511 | //      LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".")
 | 
|---|
| [042f82] | 512 | 
 | 
|---|
 | 513 |       // create correct coordination for the three atoms
 | 
|---|
| [83f176] | 514 |       alpha = (TopOrigin->getType()->getHBondAngle(2))/180.*M_PI/2.;  // retrieve triple bond angle from database
 | 
|---|
| [042f82] | 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
 | 
|---|
| [47d041] | 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));
 | 
|---|
| [042f82] | 522 |       factors[0] = d;
 | 
|---|
 | 523 |       factors[1] = f;
 | 
|---|
 | 524 |       factors[2] = 0.;
 | 
|---|
| [d74077] | 525 |       FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
| [042f82] | 526 |       factors[1] = -0.5*f;
 | 
|---|
 | 527 |       factors[2] = g;
 | 
|---|
| [d74077] | 528 |       SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
| [042f82] | 529 |       factors[2] = -g;
 | 
|---|
| [d74077] | 530 |       ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
| [042f82] | 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
 | 
|---|
| [d74077] | 538 |       *FirstOtherAtom += TopOrigin->getPosition();
 | 
|---|
 | 539 |       *SecondOtherAtom += TopOrigin->getPosition();
 | 
|---|
 | 540 |       *ThirdOtherAtom += TopOrigin->getPosition();
 | 
|---|
| [042f82] | 541 | 
 | 
|---|
 | 542 |       // ... and add to molecule
 | 
|---|
 | 543 |       AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
 | 
|---|
 | 544 |       AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
 | 
|---|
 | 545 |       AllWentWell = AllWentWell && AddAtom(ThirdOtherAtom);
 | 
|---|
| [47d041] | 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 << ".");
 | 
|---|
| [042f82] | 549 |       Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
 | 
|---|
 | 550 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 551 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 552 |       Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
 | 
|---|
 | 553 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 554 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 555 |       Binder = AddBond(BottomOrigin, ThirdOtherAtom, 1);
 | 
|---|
 | 556 |       Binder->Cyclic = false;
 | 
|---|
| [129204] | 557 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| [042f82] | 558 |       break;
 | 
|---|
 | 559 |     default:
 | 
|---|
| [47d041] | 560 |       ELOG(1, "BondDegree does not state single, double or triple bond!");
 | 
|---|
| [042f82] | 561 |       AllWentWell = false;
 | 
|---|
 | 562 |       break;
 | 
|---|
 | 563 |   }
 | 
|---|
 | 564 | 
 | 
|---|
 | 565 |   return AllWentWell;
 | 
|---|
| [14de469] | 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)
 | 
|---|
| [69eb71] | 574 | {
 | 
|---|
| [f721c6] | 575 | 
 | 
|---|
| [042f82] | 576 |   istringstream *input = NULL;
 | 
|---|
 | 577 |   int NumberOfAtoms = 0; // atom number in xyz read
 | 
|---|
| [6625c3] | 578 |   int i; // loop variables
 | 
|---|
| [042f82] | 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 | 
 | 
|---|
| [2ba827] | 589 |   OBSERVE;
 | 
|---|
| [042f82] | 590 |   getline(xyzfile,line,'\n'); // Read numer of atoms in file
 | 
|---|
 | 591 |   input = new istringstream(line);
 | 
|---|
 | 592 |   *input >> NumberOfAtoms;
 | 
|---|
| [47d041] | 593 |   LOG(0, "Parsing " << NumberOfAtoms << " atoms in file.");
 | 
|---|
| [042f82] | 594 |   getline(xyzfile,line,'\n'); // Read comment
 | 
|---|
| [47d041] | 595 |   LOG(1, "Comment: " << line);
 | 
|---|
| [042f82] | 596 | 
 | 
|---|
 | 597 |   if (MDSteps == 0) // no atoms yet present
 | 
|---|
 | 598 |     MDSteps++;
 | 
|---|
 | 599 |   for(i=0;i<NumberOfAtoms;i++){
 | 
|---|
| [23b547] | 600 |     Walker = World::getInstance().createAtom();
 | 
|---|
| [042f82] | 601 |     getline(xyzfile,line,'\n');
 | 
|---|
 | 602 |     istringstream *item = new istringstream(line);
 | 
|---|
 | 603 |     //istringstream input(line);
 | 
|---|
| [47d041] | 604 |     //LOG(1, "Reading: " << line);
 | 
|---|
| [042f82] | 605 |     *item >> shorthand;
 | 
|---|
 | 606 |     *item >> x[0];
 | 
|---|
 | 607 |     *item >> x[1];
 | 
|---|
 | 608 |     *item >> x[2];
 | 
|---|
| [d74077] | 609 |     Walker->setType(elemente->FindElement(shorthand));
 | 
|---|
 | 610 |     if (Walker->getType() == NULL) {
 | 
|---|
| [47d041] | 611 |       ELOG(1, "Could not parse the element at line: '" << line << "', setting to H.");
 | 
|---|
| [d74077] | 612 |       Walker->setType(1);
 | 
|---|
| [042f82] | 613 |     }
 | 
|---|
| [056e70] | 614 | 
 | 
|---|
| [d74077] | 615 |     Walker->setPosition(Vector(x));
 | 
|---|
| [056e70] | 616 |     Walker->setPositionAtStep(MDSteps-1, Vector(x));
 | 
|---|
 | 617 |     Walker->setAtomicVelocityAtStep(MDSteps-1, zeroVec);
 | 
|---|
 | 618 |     Walker->setAtomicForceAtStep(MDSteps-1, zeroVec);
 | 
|---|
| [042f82] | 619 |     AddAtom(Walker);  // add to molecule
 | 
|---|
 | 620 |     delete(item);
 | 
|---|
 | 621 |   }
 | 
|---|
 | 622 |   xyzfile.close();
 | 
|---|
 | 623 |   delete(input);
 | 
|---|
 | 624 |   return true;
 | 
|---|
| [14de469] | 625 | };
 | 
|---|
 | 626 | 
 | 
|---|
 | 627 | /** Creates a copy of this molecule.
 | 
|---|
 | 628 |  * \return copy of molecule
 | 
|---|
 | 629 |  */
 | 
|---|
| [e4afb4] | 630 | molecule *molecule::CopyMolecule() const
 | 
|---|
| [14de469] | 631 | {
 | 
|---|
| [5f612ee] | 632 |   molecule *copy = World::getInstance().createMolecule();
 | 
|---|
| [042f82] | 633 | 
 | 
|---|
 | 634 |   // copy all atoms
 | 
|---|
| [30c753] | 635 |   std::map< const atom *, atom *> FatherFinder;
 | 
|---|
| [59fff1] | 636 |   for (iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
 | 637 |     atom * const copy_atom = copy->AddCopyAtom(*iter);
 | 
|---|
| [30c753] | 638 |     FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
 | 
|---|
 | 639 |   }
 | 
|---|
| [042f82] | 640 | 
 | 
|---|
 | 641 |   // copy all bonds
 | 
|---|
| [30c753] | 642 |   for(const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
 | 
|---|
| [9d83b6] | 643 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
 | 644 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
 | 645 |         BondRunner != ListOfBonds.end();
 | 
|---|
 | 646 |         ++BondRunner)
 | 
|---|
| [e08c46] | 647 |       if ((*BondRunner)->leftatom == *AtomRunner) {
 | 
|---|
| [0cc92b] | 648 |         bond *Binder = (*BondRunner);
 | 
|---|
| [e08c46] | 649 |         // get the pendant atoms of current bond in the copy molecule
 | 
|---|
| [30c753] | 650 |         ASSERT(FatherFinder.count(Binder->leftatom),
 | 
|---|
| [59fff1] | 651 |             "molecule::CopyMolecule() - No copy of original left atom "
 | 
|---|
 | 652 |             +toString(Binder->leftatom)+" for bond copy found");
 | 
|---|
| [30c753] | 653 |         ASSERT(FatherFinder.count(Binder->rightatom),
 | 
|---|
| [59fff1] | 654 |             "molecule::CopyMolecule() - No copy of original right atom "
 | 
|---|
 | 655 |             +toString(Binder->rightatom)+"  for bond copy found");
 | 
|---|
| [30c753] | 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);
 | 
|---|
| [e08c46] | 660 |         NewBond->Cyclic = Binder->Cyclic;
 | 
|---|
 | 661 |         if (Binder->Cyclic)
 | 
|---|
 | 662 |           copy->NoCyclicBonds++;
 | 
|---|
 | 663 |         NewBond->Type = Binder->Type;
 | 
|---|
 | 664 |       }
 | 
|---|
| [9d83b6] | 665 |   }
 | 
|---|
| [042f82] | 666 |   // correct fathers
 | 
|---|
| [30c753] | 667 |   //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
 | 
|---|
| [cee0b57] | 668 | 
 | 
|---|
| [042f82] | 669 |   return copy;
 | 
|---|
| [14de469] | 670 | };
 | 
|---|
 | 671 | 
 | 
|---|
| [89c8b2] | 672 | 
 | 
|---|
| [9df680] | 673 | /** Destroys all atoms inside this molecule.
 | 
|---|
 | 674 |  */
 | 
|---|
 | 675 | void molecule::removeAtomsinMolecule()
 | 
|---|
 | 676 | {
 | 
|---|
 | 677 |   // remove each atom from world
 | 
|---|
| [59fff1] | 678 |   for(iterator AtomRunner = begin(); !empty(); AtomRunner = begin())
 | 
|---|
| [9df680] | 679 |     World::getInstance().destroyAtom(*AtomRunner);
 | 
|---|
 | 680 | };
 | 
|---|
 | 681 | 
 | 
|---|
 | 682 | 
 | 
|---|
| [89c8b2] | 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 |  */
 | 
|---|
| [c550dd] | 689 | molecule* molecule::CopyMoleculeFromSubRegion(const Shape ®ion) const {
 | 
|---|
| [5f612ee] | 690 |   molecule *copy = World::getInstance().createMolecule();
 | 
|---|
| [89c8b2] | 691 | 
 | 
|---|
| [30c753] | 692 |   // copy all atoms
 | 
|---|
 | 693 |   std::map< const atom *, atom *> FatherFinder;
 | 
|---|
| [59fff1] | 694 |   for (iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
| [30c753] | 695 |     if((*iter)->IsInShape(region)){
 | 
|---|
| [59fff1] | 696 |       atom * const copy_atom = copy->AddCopyAtom(*iter);
 | 
|---|
| [30c753] | 697 |       FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
 | 
|---|
| [9df5c6] | 698 |     }
 | 
|---|
 | 699 |   }
 | 
|---|
| [89c8b2] | 700 | 
 | 
|---|
| [30c753] | 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 | 
 | 
|---|
| [e138de] | 726 |   //TODO: copy->BuildInducedSubgraph(this);
 | 
|---|
| [89c8b2] | 727 | 
 | 
|---|
 | 728 |   return copy;
 | 
|---|
 | 729 | }
 | 
|---|
 | 730 | 
 | 
|---|
| [14de469] | 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 |  */
 | 
|---|
| [cee0b57] | 737 | bond * molecule::AddBond(atom *atom1, atom *atom2, int degree)
 | 
|---|
| [14de469] | 738 | {
 | 
|---|
| [f8e486] | 739 |   OBSERVE;
 | 
|---|
| [042f82] | 740 |   bond *Binder = NULL;
 | 
|---|
| [05a97c] | 741 | 
 | 
|---|
 | 742 |   // some checks to make sure we are able to create the bond
 | 
|---|
| [59fff1] | 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");
 | 
|---|
| [05a97c] | 755 | 
 | 
|---|
| [efe516] | 756 |   Binder = new bond(atom1, atom2, degree);
 | 
|---|
| [073a9e4] | 757 |   atom1->RegisterBond(WorldTime::getTime(), Binder);
 | 
|---|
 | 758 |   atom2->RegisterBond(WorldTime::getTime(), Binder);
 | 
|---|
| [59fff1] | 759 |   if ((atom1->getType() != NULL)
 | 
|---|
 | 760 |       && (atom1->getType()->getAtomicNumber() != 1)
 | 
|---|
 | 761 |       && (atom2->getType() != NULL)
 | 
|---|
 | 762 |       && (atom2->getType()->getAtomicNumber() != 1))
 | 
|---|
| [05a97c] | 763 |     NoNonBonds++;
 | 
|---|
 | 764 | 
 | 
|---|
| [042f82] | 765 |   return Binder;
 | 
|---|
| [14de469] | 766 | };
 | 
|---|
 | 767 | 
 | 
|---|
| [fa649a] | 768 | /** Remove bond from bond chain list and from the both atom::ListOfBonds.
 | 
|---|
| [073a9e4] | 769 |  * Bond::~Bond takes care of bond removal
 | 
|---|
| [14de469] | 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 | {
 | 
|---|
| [47d041] | 775 |   //ELOG(1, "molecule::RemoveBond: Function not implemented yet.");
 | 
|---|
| [e08c46] | 776 |   delete(pointer);
 | 
|---|
| [042f82] | 777 |   return true;
 | 
|---|
| [14de469] | 778 | };
 | 
|---|
 | 779 | 
 | 
|---|
 | 780 | /** Remove every bond from bond chain list that atom \a *BondPartner is a constituent of.
 | 
|---|
| [69eb71] | 781 |  * \todo Function not implemented yet
 | 
|---|
| [14de469] | 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 | {
 | 
|---|
| [47d041] | 787 |   //ELOG(1, "molecule::RemoveBond: Function not implemented yet.");
 | 
|---|
| [5e2f80] | 788 |   BondPartner->removeAllBonds();
 | 
|---|
| [042f82] | 789 |   return false;
 | 
|---|
| [14de469] | 790 | };
 | 
|---|
 | 791 | 
 | 
|---|
| [1907a7] | 792 | /** Set molecule::name from the basename without suffix in the given \a *filename.
 | 
|---|
 | 793 |  * \param *filename filename
 | 
|---|
 | 794 |  */
 | 
|---|
| [d67150] | 795 | void molecule::SetNameFromFilename(const char *filename)
 | 
|---|
| [1907a7] | 796 | {
 | 
|---|
 | 797 |   int length = 0;
 | 
|---|
| [f7f7a4] | 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
 | 
|---|
| [49e1ae] | 803 |   const char *endname = strchr(molname, '.');
 | 
|---|
| [1907a7] | 804 |   if ((endname == NULL) || (endname < molname))
 | 
|---|
 | 805 |     length = strlen(molname);
 | 
|---|
 | 806 |   else
 | 
|---|
 | 807 |     length = strlen(molname) - strlen(endname);
 | 
|---|
| [35b698] | 808 |   cout << "Set name of molecule " << getId() << " to " << molname << endl;
 | 
|---|
| [1907a7] | 809 |   strncpy(name, molname, length);
 | 
|---|
| [d67150] | 810 |   name[length]='\0';
 | 
|---|
| [1907a7] | 811 | };
 | 
|---|
 | 812 | 
 | 
|---|
| [14de469] | 813 | /** Sets the molecule::cell_size to the components of \a *dim (rectangular box)
 | 
|---|
 | 814 |  * \param *dim vector class
 | 
|---|
 | 815 |  */
 | 
|---|
| [e9b8bb] | 816 | void molecule::SetBoxDimension(Vector *dim)
 | 
|---|
| [14de469] | 817 | {
 | 
|---|
| [cca9ef] | 818 |   RealSpaceMatrix domain;
 | 
|---|
| [84c494] | 819 |   for(int i =0; i<NDIM;++i)
 | 
|---|
 | 820 |     domain.at(i,i) = dim->at(i);
 | 
|---|
 | 821 |   World::getInstance().setDomain(domain);
 | 
|---|
| [14de469] | 822 | };
 | 
|---|
 | 823 | 
 | 
|---|
| [fa7989] | 824 | /** Removes atom from molecule list and removes all of its bonds.
 | 
|---|
| [cee0b57] | 825 |  * \param *pointer atom to be removed
 | 
|---|
 | 826 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
| [a9d254] | 827 |  */
 | 
|---|
| [cee0b57] | 828 | bool molecule::RemoveAtom(atom *pointer)
 | 
|---|
| [a9d254] | 829 | {
 | 
|---|
| [a7b761b] | 830 |   ASSERT(pointer, "Null pointer passed to molecule::RemoveAtom().");
 | 
|---|
| [ea7176] | 831 |   OBSERVE;
 | 
|---|
| [266237] | 832 |   RemoveBonds(pointer);
 | 
|---|
| [2e4105] | 833 |   pointer->removeFromMolecule();
 | 
|---|
| [9879f6] | 834 |   return true;
 | 
|---|
| [a9d254] | 835 | };
 | 
|---|
 | 836 | 
 | 
|---|
| [cee0b57] | 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
 | 
|---|
| [f3278b] | 840 |  */
 | 
|---|
| [cee0b57] | 841 | bool molecule::UnlinkAtom(atom *pointer)
 | 
|---|
| [f3278b] | 842 | {
 | 
|---|
| [cee0b57] | 843 |   if (pointer == NULL)
 | 
|---|
 | 844 |     return false;
 | 
|---|
| [2e4105] | 845 |   pointer->removeFromMolecule();
 | 
|---|
| [cee0b57] | 846 |   return true;
 | 
|---|
| [f3278b] | 847 | };
 | 
|---|
 | 848 | 
 | 
|---|
| [cee0b57] | 849 | /** Removes every atom from molecule list.
 | 
|---|
 | 850 |  * \return true - succeeded, false - atom not found in list
 | 
|---|
| [14de469] | 851 |  */
 | 
|---|
| [cee0b57] | 852 | bool molecule::CleanupMolecule()
 | 
|---|
| [14de469] | 853 | {
 | 
|---|
| [9879f6] | 854 |   for (molecule::iterator iter = begin(); !empty(); iter = begin())
 | 
|---|
| [2e4105] | 855 |     (*iter)->removeFromMolecule();
 | 
|---|
| [274d45] | 856 |   return empty();
 | 
|---|
| [69eb71] | 857 | };
 | 
|---|
| [14de469] | 858 | 
 | 
|---|
| [cee0b57] | 859 | /** Finds an atom specified by its continuous number.
 | 
|---|
 | 860 |  * \param Nr number of atom withim molecule
 | 
|---|
 | 861 |  * \return pointer to atom or NULL
 | 
|---|
| [14de469] | 862 |  */
 | 
|---|
| [9879f6] | 863 | atom * molecule::FindAtom(int Nr)  const
 | 
|---|
 | 864 | {
 | 
|---|
| [59fff1] | 865 |   molecule::iterator iter = begin();
 | 
|---|
| [9879f6] | 866 |   for (; iter != end(); ++iter)
 | 
|---|
| [59fff1] | 867 |   if ((*iter)->getNr() == Nr)
 | 
|---|
 | 868 |     break;
 | 
|---|
| [9879f6] | 869 |   if (iter != end()) {
 | 
|---|
| [47d041] | 870 |     //LOG(0, "Found Atom Nr. " << walker->getNr());
 | 
|---|
| [9879f6] | 871 |     return (*iter);
 | 
|---|
| [cee0b57] | 872 |   } else {
 | 
|---|
| [59fff1] | 873 |     ELOG(1, "Atom not found in molecule " << getName() << "'s list.");
 | 
|---|
| [cee0b57] | 874 |     return NULL;
 | 
|---|
| [042f82] | 875 |   }
 | 
|---|
| [59fff1] | 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 | }
 | 
|---|
| [14de469] | 893 | 
 | 
|---|
| [cee0b57] | 894 | /** Asks for atom number, and checks whether in list.
 | 
|---|
 | 895 |  * \param *text question before entering
 | 
|---|
| [a6b7fb] | 896 |  */
 | 
|---|
| [cee0b57] | 897 | atom * molecule::AskAtom(string text)
 | 
|---|
| [a6b7fb] | 898 | {
 | 
|---|
| [cee0b57] | 899 |   int No;
 | 
|---|
 | 900 |   atom *ion = NULL;
 | 
|---|
 | 901 |   do {
 | 
|---|
| [47d041] | 902 |     //std::cout << "============Atom list==========================" << std::endl;
 | 
|---|
| [cee0b57] | 903 |     //mol->Output((ofstream *)&cout);
 | 
|---|
| [47d041] | 904 |     //std::cout << "===============================================" << std::endl;
 | 
|---|
 | 905 |     std::cout << text;
 | 
|---|
| [cee0b57] | 906 |     cin >> No;
 | 
|---|
 | 907 |     ion = this->FindAtom(No);
 | 
|---|
 | 908 |   } while (ion == NULL);
 | 
|---|
 | 909 |   return ion;
 | 
|---|
| [a6b7fb] | 910 | };
 | 
|---|
 | 911 | 
 | 
|---|
| [cee0b57] | 912 | /** Checks if given coordinates are within cell volume.
 | 
|---|
 | 913 |  * \param *x array of coordinates
 | 
|---|
 | 914 |  * \return true - is within, false - out of cell
 | 
|---|
| [14de469] | 915 |  */
 | 
|---|
| [cee0b57] | 916 | bool molecule::CheckBounds(const Vector *x) const
 | 
|---|
| [14de469] | 917 | {
 | 
|---|
| [cca9ef] | 918 |   const RealSpaceMatrix &domain = World::getInstance().getDomain().getM();
 | 
|---|
| [cee0b57] | 919 |   bool result = true;
 | 
|---|
 | 920 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| [84c494] | 921 |     result = result && ((x->at(i) >= 0) && (x->at(i) < domain.at(i,i)));
 | 
|---|
| [042f82] | 922 |   }
 | 
|---|
| [cee0b57] | 923 |   //return result;
 | 
|---|
 | 924 |   return true; /// probably not gonna use the check no more
 | 
|---|
| [69eb71] | 925 | };
 | 
|---|
| [14de469] | 926 | 
 | 
|---|
| [cee0b57] | 927 | /** Prints molecule to *out.
 | 
|---|
 | 928 |  * \param *out output stream
 | 
|---|
| [14de469] | 929 |  */
 | 
|---|
| [e4afb4] | 930 | bool molecule::Output(ostream * const output) const
 | 
|---|
| [14de469] | 931 | {
 | 
|---|
| [e138de] | 932 |   if (output == NULL) {
 | 
|---|
| [cee0b57] | 933 |     return false;
 | 
|---|
 | 934 |   } else {
 | 
|---|
| [0ba410] | 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;
 | 
|---|
| [30c753] | 939 |     for_each(begin(),end(),boost::bind(&atom::OutputArrayIndexed,_1,output,elementLookup,AtomNo,(const char*)0));
 | 
|---|
| [cee0b57] | 940 |     return true;
 | 
|---|
| [042f82] | 941 |   }
 | 
|---|
| [14de469] | 942 | };
 | 
|---|
 | 943 | 
 | 
|---|
| [cee0b57] | 944 | /** Prints molecule with all atomic trajectory positions to *out.
 | 
|---|
 | 945 |  * \param *out output stream
 | 
|---|
| [21c017] | 946 |  */
 | 
|---|
| [e4afb4] | 947 | bool molecule::OutputTrajectories(ofstream * const output) const
 | 
|---|
| [21c017] | 948 | {
 | 
|---|
| [e138de] | 949 |   if (output == NULL) {
 | 
|---|
| [cee0b57] | 950 |     return false;
 | 
|---|
 | 951 |   } else {
 | 
|---|
 | 952 |     for (int step = 0; step < MDSteps; step++) {
 | 
|---|
 | 953 |       if (step == 0) {
 | 
|---|
| [e138de] | 954 |         *output << "#Ion_TypeNr._Nr.R[0]    R[1]    R[2]    MoveType (0 MoveIon, 1 FixedIon)" << endl;
 | 
|---|
| [205ccd] | 955 |       } else {
 | 
|---|
| [e138de] | 956 |         *output << "# ====== MD step " << step << " =========" << endl;
 | 
|---|
| [cee0b57] | 957 |       }
 | 
|---|
| [882a8a] | 958 |       int AtomNo[MAX_ELEMENTS];
 | 
|---|
 | 959 |       memset(AtomNo,0,(MAX_ELEMENTS-1)*sizeof(*AtomNo));
 | 
|---|
 | 960 |       enumeration<const element*> elementLookup = formula.enumerateElements();
 | 
|---|
| [30c753] | 961 |       for_each(begin(),end(),boost::bind(&atom::OutputTrajectory,_1,output,elementLookup, AtomNo, (const int)step));
 | 
|---|
| [21c017] | 962 |     }
 | 
|---|
| [cee0b57] | 963 |     return true;
 | 
|---|
| [21c017] | 964 |   }
 | 
|---|
 | 965 | };
 | 
|---|
 | 966 | 
 | 
|---|
| [266237] | 967 | /** Outputs contents of each atom::ListOfBonds.
 | 
|---|
| [cee0b57] | 968 |  * \param *out output stream
 | 
|---|
| [14de469] | 969 |  */
 | 
|---|
| [e138de] | 970 | void molecule::OutputListOfBonds() const
 | 
|---|
| [14de469] | 971 | {
 | 
|---|
| [4b5cf8] | 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 | }
 | 
|---|
| [14de469] | 982 | 
 | 
|---|
| [cee0b57] | 983 | /** Output of element before the actual coordination list.
 | 
|---|
 | 984 |  * \param *out stream pointer
 | 
|---|
| [14de469] | 985 |  */
 | 
|---|
| [e138de] | 986 | bool molecule::Checkout(ofstream * const output)  const
 | 
|---|
| [14de469] | 987 | {
 | 
|---|
| [389cc8] | 988 |   return formula.checkOut(output);
 | 
|---|
| [6e9353] | 989 | };
 | 
|---|
 | 990 | 
 | 
|---|
| [cee0b57] | 991 | /** Prints molecule with all its trajectories to *out as xyz file.
 | 
|---|
 | 992 |  * \param *out output stream
 | 
|---|
| [d7e30c] | 993 |  */
 | 
|---|
| [e138de] | 994 | bool molecule::OutputTrajectoriesXYZ(ofstream * const output)
 | 
|---|
| [d7e30c] | 995 | {
 | 
|---|
| [cee0b57] | 996 |   time_t now;
 | 
|---|
| [042f82] | 997 | 
 | 
|---|
| [e138de] | 998 |   if (output != NULL) {
 | 
|---|
| [681a8a] | 999 |     now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| [cee0b57] | 1000 |     for (int step=0;step<MDSteps;step++) {
 | 
|---|
| [ea7176] | 1001 |       *output << getAtomCount() << "\n\tCreated by molecuilder, step " << step << ", on " << ctime(&now);
 | 
|---|
| [30c753] | 1002 |       for_each(begin(),end(),boost::bind(&atom::OutputTrajectoryXYZ,_1,output,step));
 | 
|---|
| [042f82] | 1003 |     }
 | 
|---|
| [cee0b57] | 1004 |     return true;
 | 
|---|
 | 1005 |   } else
 | 
|---|
 | 1006 |     return false;
 | 
|---|
| [14de469] | 1007 | };
 | 
|---|
 | 1008 | 
 | 
|---|
| [cee0b57] | 1009 | /** Prints molecule to *out as xyz file.
 | 
|---|
 | 1010 | * \param *out output stream
 | 
|---|
| [69eb71] | 1011 |  */
 | 
|---|
| [e138de] | 1012 | bool molecule::OutputXYZ(ofstream * const output) const
 | 
|---|
| [4aa03a] | 1013 | {
 | 
|---|
| [cee0b57] | 1014 |   time_t now;
 | 
|---|
| [042f82] | 1015 | 
 | 
|---|
| [e138de] | 1016 |   if (output != NULL) {
 | 
|---|
| [23b830] | 1017 |     now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| [ea7176] | 1018 |     *output << getAtomCount() << "\n\tCreated by molecuilder on " << ctime(&now);
 | 
|---|
| [30c753] | 1019 |     for_each(begin(),end(),bind2nd(mem_fun(&atom::OutputXYZLine),output));
 | 
|---|
| [042f82] | 1020 |     return true;
 | 
|---|
| [cee0b57] | 1021 |   } else
 | 
|---|
 | 1022 |     return false;
 | 
|---|
 | 1023 | };
 | 
|---|
| [4aa03a] | 1024 | 
 | 
|---|
| [cee0b57] | 1025 | /** Brings molecule::AtomCount and atom::*Name up-to-date.
 | 
|---|
| [14de469] | 1026 |  * \param *out output stream for debugging
 | 
|---|
 | 1027 |  */
 | 
|---|
| [ea7176] | 1028 | int molecule::doCountAtoms()
 | 
|---|
| [14de469] | 1029 | {
 | 
|---|
| [ea7176] | 1030 |   int res = size();
 | 
|---|
| [cee0b57] | 1031 |   int i = 0;
 | 
|---|
| [ea7176] | 1032 |   NoNonHydrogen = 0;
 | 
|---|
| [59fff1] | 1033 |   for (molecule::iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
| [a479fa] | 1034 |     (*iter)->setNr(i);   // update number in molecule (for easier referencing in FragmentMolecule lateron)
 | 
|---|
| [83f176] | 1035 |     if ((*iter)->getType()->getAtomicNumber() != 1) // count non-hydrogen atoms whilst at it
 | 
|---|
| [ea7176] | 1036 |       NoNonHydrogen++;
 | 
|---|
| [a7b761b] | 1037 |     stringstream sstr;
 | 
|---|
| [735b1c] | 1038 |     sstr << (*iter)->getType()->getSymbol() << (*iter)->getNr()+1;
 | 
|---|
| [a7b761b] | 1039 |     (*iter)->setName(sstr.str());
 | 
|---|
| [47d041] | 1040 |     LOG(3, "Naming atom nr. " << (*iter)->getNr() << " " << (*iter)->getName() << ".");
 | 
|---|
| [cee0b57] | 1041 |     i++;
 | 
|---|
 | 1042 |   }
 | 
|---|
| [ea7176] | 1043 |   return res;
 | 
|---|
| [cee0b57] | 1044 | };
 | 
|---|
| [042f82] | 1045 | 
 | 
|---|
| [458c31] | 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 | 
 | 
|---|
| [14de469] | 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 |  */
 | 
|---|
| [e138de] | 1071 | int * molecule::GetFatherSonAtomicMap(molecule *OtherMolecule)
 | 
|---|
| [14de469] | 1072 | {
 | 
|---|
| [47d041] | 1073 |   LOG(3, "Begin of GetFatherAtomicMap.");
 | 
|---|
| [1024cb] | 1074 |   int *AtomicMap = new int[getAtomCount()];
 | 
|---|
| [ea7176] | 1075 |   for (int i=getAtomCount();i--;)
 | 
|---|
| [042f82] | 1076 |     AtomicMap[i] = -1;
 | 
|---|
 | 1077 |   if (OtherMolecule == this) {  // same molecule
 | 
|---|
| [ea7176] | 1078 |     for (int i=getAtomCount();i--;) // no need as -1 means already that there is trivial correspondence
 | 
|---|
| [042f82] | 1079 |       AtomicMap[i] = i;
 | 
|---|
| [47d041] | 1080 |     LOG(4, "Map is trivial.");
 | 
|---|
| [042f82] | 1081 |   } else {
 | 
|---|
| [47d041] | 1082 |     std::stringstream output;
 | 
|---|
 | 1083 |     output << "Map is ";
 | 
|---|
| [9879f6] | 1084 |     for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
 | 1085 |       if ((*iter)->father == NULL) {
 | 
|---|
| [735b1c] | 1086 |         AtomicMap[(*iter)->getNr()] = -2;
 | 
|---|
| [042f82] | 1087 |       } else {
 | 
|---|
| [9879f6] | 1088 |         for (molecule::const_iterator runner = OtherMolecule->begin(); runner != OtherMolecule->end(); ++runner) {
 | 
|---|
| [042f82] | 1089 |       //for (int i=0;i<AtomCount;i++) { // search atom
 | 
|---|
| [1024cb] | 1090 |         //for (int j=0;j<OtherMolecule->getAtomCount();j++) {
 | 
|---|
| [47d041] | 1091 |           //LOG(4, "Comparing father " << (*iter)->father << " with the other one " << (*runner)->father << ".");
 | 
|---|
| [9879f6] | 1092 |           if ((*iter)->father == (*runner))
 | 
|---|
| [735b1c] | 1093 |             AtomicMap[(*iter)->getNr()] = (*runner)->getNr();
 | 
|---|
| [042f82] | 1094 |         }
 | 
|---|
 | 1095 |       }
 | 
|---|
| [47d041] | 1096 |       output << AtomicMap[(*iter)->getNr()] << "\t";
 | 
|---|
| [042f82] | 1097 |     }
 | 
|---|
| [47d041] | 1098 |     LOG(4, output.str());
 | 
|---|
| [042f82] | 1099 |   }
 | 
|---|
| [47d041] | 1100 |   LOG(3, "End of GetFatherAtomicMap.");
 | 
|---|
| [042f82] | 1101 |   return AtomicMap;
 | 
|---|
| [14de469] | 1102 | };
 | 
|---|
 | 1103 | 
 | 
|---|
| [4a7776a] | 1104 | 
 | 
|---|
| [c68025] | 1105 | void molecule::flipActiveFlag(){
 | 
|---|
 | 1106 |   ActiveFlag = !ActiveFlag;
 | 
|---|
 | 1107 | }
 | 
|---|