| [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 atom.cpp
 | 
|---|
| [1907a7] | 25 |  *
 | 
|---|
| [14de469] | 26 |  * Function implementations for the class atom.
 | 
|---|
| [1907a7] | 27 |  *
 | 
|---|
| [14de469] | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 36 | 
 | 
|---|
| [357fba] | 37 | #include "atom.hpp"
 | 
|---|
| [708277] | 38 | #include "AtomObserver.hpp"
 | 
|---|
| [129204] | 39 | #include "Bond/bond.hpp"
 | 
|---|
| [e2373df] | 40 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [4a7776a] | 41 | #include "config.hpp"
 | 
|---|
| [3bdb6d] | 42 | #include "Element/element.hpp"
 | 
|---|
| [57f243] | 43 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [d346b6] | 44 | #include "World.hpp"
 | 
|---|
| [11f0fa] | 45 | #include "WorldTime.hpp"
 | 
|---|
| [6cfa36] | 46 | #include "molecule.hpp"
 | 
|---|
| [c550dd] | 47 | #include "Shapes/Shape.hpp"
 | 
|---|
| [a0064e] | 48 | 
 | 
|---|
| [36166d] | 49 | #include <iomanip>
 | 
|---|
| [0ba410] | 50 | #include <iostream>
 | 
|---|
| [36166d] | 51 | 
 | 
|---|
| [14de469] | 52 | /************************************* Functions for class atom *************************************/
 | 
|---|
 | 53 | 
 | 
|---|
| [70ff32] | 54 | 
 | 
|---|
| [46d958] | 55 | atom::atom() :
 | 
|---|
| [97b825] | 56 |   father(this),
 | 
|---|
| [5309ba] | 57 |   sort(&Nr),
 | 
|---|
| [97b825] | 58 |   mol(0)
 | 
|---|
| [708277] | 59 | {
 | 
|---|
 | 60 |   // sign on to global atom change tracker
 | 
|---|
 | 61 |   AtomObserver::getInstance().AtomInserted(this);
 | 
|---|
 | 62 | }
 | 
|---|
| [14de469] | 63 | 
 | 
|---|
| [ea0c8b] | 64 | atom::atom(atom *pointer) :
 | 
|---|
 | 65 |     ParticleInfo(*pointer),
 | 
|---|
| [c742fe] | 66 |     AtomInfo(*pointer),
 | 
|---|
| [97b825] | 67 |     father(pointer),
 | 
|---|
| [5309ba] | 68 |     sort(&Nr),
 | 
|---|
| [9df680] | 69 |     mol(0)
 | 
|---|
| [2319ed] | 70 | {
 | 
|---|
| [443547] | 71 |   AtomicPosition = pointer->AtomicPosition; // copy trajectory of coordination
 | 
|---|
 | 72 |   AtomicVelocity = pointer->AtomicVelocity; // copy trajectory of velocity
 | 
|---|
 | 73 |   AtomicForce = pointer->AtomicForce;
 | 
|---|
| [708277] | 74 |   // sign on to global atom change tracker
 | 
|---|
 | 75 |   AtomObserver::getInstance().AtomInserted(this);
 | 
|---|
| [b453f9] | 76 | };
 | 
|---|
| [2319ed] | 77 | 
 | 
|---|
| [46d958] | 78 | atom *atom::clone(){
 | 
|---|
| [68f03d] | 79 |   atom *res = new atom(this);
 | 
|---|
| [23b547] | 80 |   World::getInstance().registerAtom(res);
 | 
|---|
| [46d958] | 81 |   return res;
 | 
|---|
 | 82 | }
 | 
|---|
 | 83 | 
 | 
|---|
| [2319ed] | 84 | 
 | 
|---|
| [14de469] | 85 | /** Destructor of class atom.
 | 
|---|
 | 86 |  */
 | 
|---|
| [1907a7] | 87 | atom::~atom()
 | 
|---|
| [14de469] | 88 | {
 | 
|---|
| [6cfa36] | 89 |   removeFromMolecule();
 | 
|---|
| [708277] | 90 |   // sign off from global atom change tracker
 | 
|---|
 | 91 |   AtomObserver::getInstance().AtomRemoved(this);
 | 
|---|
 | 92 | }
 | 
|---|
| [e2373df] | 93 | 
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | void atom::UpdateSteps()
 | 
|---|
 | 96 | {
 | 
|---|
 | 97 |   LOG(4,"atom::UpdateSteps() called.");
 | 
|---|
 | 98 |   // append to position, velocity and force vector
 | 
|---|
 | 99 |   AtomInfo::AppendTrajectoryStep();
 | 
|---|
| [1e6249] | 100 |   // append to ListOfBonds vector
 | 
|---|
 | 101 |   BondedParticleInfo::AppendTrajectoryStep();
 | 
|---|
| [e2373df] | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
| [59fff1] | 104 | atom *atom::GetTrueFather()
 | 
|---|
 | 105 | {
 | 
|---|
 | 106 |   const atom *father = const_cast<const atom *>(this)->GetTrueFather();
 | 
|---|
 | 107 |   return const_cast<atom *>(father);
 | 
|---|
 | 108 | }
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 | const atom *atom::GetTrueFather() const
 | 
|---|
| [14de469] | 111 | {
 | 
|---|
| [215df0] | 112 |   if(father == this){ // top most father is the one that points on itself
 | 
|---|
 | 113 |     return this;
 | 
|---|
 | 114 |   }
 | 
|---|
 | 115 |   else if(!father) {
 | 
|---|
 | 116 |     return 0;
 | 
|---|
 | 117 |   }
 | 
|---|
 | 118 |   else {
 | 
|---|
 | 119 |     return father->GetTrueFather();
 | 
|---|
 | 120 |   }
 | 
|---|
| [14de469] | 121 | };
 | 
|---|
 | 122 | 
 | 
|---|
| [e65246] | 123 | /** Sets father to itself or its father in case of copying a molecule.
 | 
|---|
 | 124 |  */
 | 
|---|
 | 125 | void atom::CorrectFather()
 | 
|---|
 | 126 | {
 | 
|---|
| [2e352f] | 127 |   if (father->father != father)   // same atom in copy's father points to itself
 | 
|---|
 | 128 | //    father = this;  // set father to itself (copy of a whole molecule)
 | 
|---|
 | 129 | //  else
 | 
|---|
| [e65246] | 130 |    father = father->father;  // set father to original's father
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | };
 | 
|---|
 | 133 | 
 | 
|---|
| [b453f9] | 134 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
 | 
|---|
| [e65246] | 135 | {
 | 
|---|
 | 136 |   if ( ptr == father )
 | 
|---|
 | 137 |     *res = this;
 | 
|---|
 | 138 | };
 | 
|---|
 | 139 | 
 | 
|---|
| [00abfc] | 140 | bool atom::isFather(const atom *ptr){
 | 
|---|
 | 141 |   return ptr==father;
 | 
|---|
 | 142 | }
 | 
|---|
 | 143 | 
 | 
|---|
| [e138de] | 144 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
 | 
|---|
| [14de469] | 145 | {
 | 
|---|
 | 146 |   if (out != NULL) {
 | 
|---|
 | 147 |     *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"  << fixed << setprecision(9) << showpoint;
 | 
|---|
| [d74077] | 148 |     *out << at(0) << "\t" << at(1) << "\t" << at(2);
 | 
|---|
| [6625c3] | 149 |     *out << "\t" << (int)(getFixedIon());
 | 
|---|
| [bce72c] | 150 |     if (getAtomicVelocity().Norm() > MYEPSILON)
 | 
|---|
 | 151 |       *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
 | 
|---|
| [437922] | 152 |     if (comment != NULL)
 | 
|---|
 | 153 |       *out << " # " << comment << endl;
 | 
|---|
| [e9f8f9] | 154 |     else
 | 
|---|
| [735b1c] | 155 |       *out << " # molecule nr " << getNr() << endl;
 | 
|---|
| [e9f8f9] | 156 |     return true;
 | 
|---|
 | 157 |   } else
 | 
|---|
 | 158 |     return false;
 | 
|---|
 | 159 | };
 | 
|---|
| [b453f9] | 160 | 
 | 
|---|
| [0ba410] | 161 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
 | 
|---|
| [e9f8f9] | 162 | {
 | 
|---|
| [83f176] | 163 |   AtomNo[getType()->getAtomicNumber()]++;  // increment number
 | 
|---|
| [e9f8f9] | 164 |   if (out != NULL) {
 | 
|---|
| [8f4df1] | 165 |     const element *elemental = getType();
 | 
|---|
 | 166 |     ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
 | 
|---|
| [83f176] | 167 |     *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t"  << fixed << setprecision(9) << showpoint;
 | 
|---|
| [d74077] | 168 |     *out << at(0) << "\t" << at(1) << "\t" << at(2);
 | 
|---|
| [6625c3] | 169 |     *out << "\t" << getFixedIon();
 | 
|---|
| [bce72c] | 170 |     if (getAtomicVelocity().Norm() > MYEPSILON)
 | 
|---|
 | 171 |       *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
 | 
|---|
| [e9f8f9] | 172 |     if (comment != NULL)
 | 
|---|
 | 173 |       *out << " # " << comment << endl;
 | 
|---|
| [437922] | 174 |     else
 | 
|---|
| [735b1c] | 175 |       *out << " # molecule nr " << getNr() << endl;
 | 
|---|
| [14de469] | 176 |     return true;
 | 
|---|
 | 177 |   } else
 | 
|---|
 | 178 |     return false;
 | 
|---|
 | 179 | };
 | 
|---|
 | 180 | 
 | 
|---|
| [b453f9] | 181 | bool atom::Compare(const atom &ptr) const
 | 
|---|
| [4455f4] | 182 | {
 | 
|---|
| [735b1c] | 183 |   if (getNr() < ptr.getNr())
 | 
|---|
| [4455f4] | 184 |     return true;
 | 
|---|
 | 185 |   else
 | 
|---|
 | 186 |     return false;
 | 
|---|
 | 187 | };
 | 
|---|
 | 188 | 
 | 
|---|
| [b453f9] | 189 | double atom::DistanceSquaredToVector(const Vector &origin) const
 | 
|---|
| [4455f4] | 190 | {
 | 
|---|
| [d74077] | 191 |   return DistanceSquared(origin);
 | 
|---|
| [4455f4] | 192 | };
 | 
|---|
 | 193 | 
 | 
|---|
| [b453f9] | 194 | double atom::DistanceToVector(const Vector &origin) const
 | 
|---|
| [4455f4] | 195 | {
 | 
|---|
| [d74077] | 196 |   return distance(origin);
 | 
|---|
| [4455f4] | 197 | };
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 | void atom::InitComponentNr()
 | 
|---|
 | 200 | {
 | 
|---|
 | 201 |   if (ComponentNr != NULL)
 | 
|---|
| [920c70] | 202 |     delete[](ComponentNr);
 | 
|---|
| [9d83b6] | 203 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| [920c70] | 204 |   ComponentNr = new int[ListOfBonds.size()+1];
 | 
|---|
| [4455f4] | 205 |   for (int i=ListOfBonds.size()+1;i--;)
 | 
|---|
 | 206 |     ComponentNr[i] = -1;
 | 
|---|
| [14b65e] | 207 | };
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 | void atom::resetGraphNr(){
 | 
|---|
 | 210 |   GraphNr=-1;
 | 
|---|
 | 211 | }
 | 
|---|
| [4455f4] | 212 | 
 | 
|---|
| [d74077] | 213 | std::ostream & atom::operator << (std::ostream &ost) const
 | 
|---|
 | 214 | {
 | 
|---|
 | 215 |   ParticleInfo::operator<<(ost);
 | 
|---|
 | 216 |   ost << "," << getPosition();
 | 
|---|
 | 217 |   return ost;
 | 
|---|
 | 218 | }
 | 
|---|
 | 219 | 
 | 
|---|
 | 220 | std::ostream & operator << (std::ostream &ost, const atom &a)
 | 
|---|
 | 221 | {
 | 
|---|
 | 222 |   a.ParticleInfo::operator<<(ost);
 | 
|---|
 | 223 |   ost << "," << a.getPosition();
 | 
|---|
 | 224 |   return ost;
 | 
|---|
 | 225 | }
 | 
|---|
| [4455f4] | 226 | 
 | 
|---|
 | 227 | bool operator < (atom &a, atom &b)
 | 
|---|
 | 228 | {
 | 
|---|
 | 229 |   return a.Compare(b);
 | 
|---|
 | 230 | };
 | 
|---|
 | 231 | 
 | 
|---|
| [46d958] | 232 | World *atom::getWorld(){
 | 
|---|
 | 233 |   return world;
 | 
|---|
 | 234 | }
 | 
|---|
 | 235 | 
 | 
|---|
 | 236 | void atom::setWorld(World* _world){
 | 
|---|
 | 237 |   world = _world;
 | 
|---|
 | 238 | }
 | 
|---|
 | 239 | 
 | 
|---|
| [88d586] | 240 | bool atom::changeId(atomId_t newId){
 | 
|---|
 | 241 |   // first we move ourselves in the world
 | 
|---|
 | 242 |   // the world lets us know if that succeeded
 | 
|---|
| [4f7f0bf] | 243 |   if(world->changeAtomId(id,newId,this)){
 | 
|---|
 | 244 |     OBSERVE;
 | 
|---|
 | 245 |     id = newId;
 | 
|---|
 | 246 |     NOTIFY(IndexChanged);
 | 
|---|
| [88d586] | 247 |     return true;
 | 
|---|
 | 248 |   }
 | 
|---|
 | 249 |   else{
 | 
|---|
 | 250 |     return false;
 | 
|---|
 | 251 |   }
 | 
|---|
 | 252 | }
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 | void atom::setId(atomId_t _id) {
 | 
|---|
| [46d958] | 255 |   id=_id;
 | 
|---|
 | 256 | }
 | 
|---|
 | 257 | 
 | 
|---|
| [ad2b411] | 258 | atomId_t atom::getId() const {
 | 
|---|
| [46d958] | 259 |   return id;
 | 
|---|
 | 260 | }
 | 
|---|
 | 261 | 
 | 
|---|
| [6cfa36] | 262 | void atom::setMolecule(molecule *_mol){
 | 
|---|
 | 263 |   // take this atom from the old molecule
 | 
|---|
 | 264 |   removeFromMolecule();
 | 
|---|
| [3867a7] | 265 |   mol = _mol;
 | 
|---|
 | 266 |   if ((mol) && (!mol->containsAtom(this)))
 | 
|---|
| [dddbfe] | 267 |     mol->insert(this);
 | 
|---|
| [6cfa36] | 268 | }
 | 
|---|
 | 269 | 
 | 
|---|
| [0d9546] | 270 | void atom::unsetMolecule()
 | 
|---|
 | 271 | {
 | 
|---|
 | 272 |   // take this atom from the old molecule
 | 
|---|
 | 273 |   ASSERT(!mol->containsAtom(this),
 | 
|---|
 | 274 |       "atom::unsetMolecule() - old molecule "+toString(mol)+" still contains us!");
 | 
|---|
 | 275 |   mol = NULL;
 | 
|---|
 | 276 | }
 | 
|---|
 | 277 | 
 | 
|---|
| [e41c48] | 278 | molecule* atom::getMolecule() const {
 | 
|---|
| [c084cc] | 279 |   return mol;
 | 
|---|
 | 280 | }
 | 
|---|
 | 281 | 
 | 
|---|
| [6cfa36] | 282 | void atom::removeFromMolecule(){
 | 
|---|
 | 283 |   if(mol){
 | 
|---|
 | 284 |     if(mol->containsAtom(this)){
 | 
|---|
 | 285 |       mol->erase(this);
 | 
|---|
 | 286 |     }
 | 
|---|
 | 287 |     mol=0;
 | 
|---|
 | 288 |   }
 | 
|---|
| [1f8337] | 289 | }
 | 
|---|
 | 290 | 
 | 
|---|
| [560bbe] | 291 | bool atom::changeNr(const int newNr)
 | 
|---|
 | 292 | {
 | 
|---|
 | 293 |   if ((mol) && (mol->changeAtomNr(getNr(),newNr,this))) {
 | 
|---|
 | 294 |     return true;
 | 
|---|
 | 295 |   } else{
 | 
|---|
 | 296 |     return false;
 | 
|---|
 | 297 |   }
 | 
|---|
 | 298 | }
 | 
|---|
 | 299 | 
 | 
|---|
| [e8a21f] | 300 | int atom::getNr() const{
 | 
|---|
| [735b1c] | 301 |   return ParticleInfo::getNr();
 | 
|---|
| [e8a21f] | 302 | }
 | 
|---|
| [6cfa36] | 303 | 
 | 
|---|
| [88d586] | 304 | atom* NewAtom(atomId_t _id){
 | 
|---|
| [11f0fa] | 305 |   atom * res = new atom();
 | 
|---|
 | 306 |   // extent trajectory to current time step
 | 
|---|
 | 307 |   const size_t CurrentTime = WorldTime::getTime();
 | 
|---|
 | 308 |   for (size_t step = res->getTrajectorySize(); step <= CurrentTime; ++step)
 | 
|---|
 | 309 |     res->UpdateSteps();
 | 
|---|
| [88d586] | 310 |   res->setId(_id);
 | 
|---|
 | 311 |   return res;
 | 
|---|
| [46d958] | 312 | }
 | 
|---|
 | 313 | 
 | 
|---|
| [88d586] | 314 | void DeleteAtom(atom* atom){
 | 
|---|
| [46d958] | 315 |   delete atom;
 | 
|---|
| [e5f64de] | 316 | }
 | 
|---|
 | 317 | 
 | 
|---|
 | 318 | bool compareAtomElements(atom* atom1,atom* atom2){
 | 
|---|
| [ed26ae] | 319 |   return atom1->getType()->getAtomicNumber() < atom2->getType()->getAtomicNumber();
 | 
|---|
| [46d958] | 320 | }
 | 
|---|