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