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