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