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