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