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