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