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