| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /** \file atom.cpp
 | 
|---|
| 24 |  *
 | 
|---|
| 25 |  * Function implementations for the class atom.
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | // include config.h
 | 
|---|
| 30 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 31 | #include <config.h>
 | 
|---|
| 32 | #endif
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "atom.hpp"
 | 
|---|
| 37 | #include "Bond/bond.hpp"
 | 
|---|
| 38 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 39 | #include "config.hpp"
 | 
|---|
| 40 | #include "Element/element.hpp"
 | 
|---|
| 41 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 42 | #include "World.hpp"
 | 
|---|
| 43 | #include "molecule.hpp"
 | 
|---|
| 44 | #include "Shapes/Shape.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include <iomanip>
 | 
|---|
| 47 | #include <iostream>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | /************************************* Functions for class atom *************************************/
 | 
|---|
| 50 | 
 | 
|---|
| 51 | 
 | 
|---|
| 52 | atom::atom() :
 | 
|---|
| 53 |   father(this),
 | 
|---|
| 54 |   sort(&Nr),
 | 
|---|
| 55 |   mol(0)
 | 
|---|
| 56 | {};
 | 
|---|
| 57 | 
 | 
|---|
| 58 | atom::atom(atom *pointer) :
 | 
|---|
| 59 |     ParticleInfo(*pointer),
 | 
|---|
| 60 |     AtomInfo(*pointer),
 | 
|---|
| 61 |     father(pointer),
 | 
|---|
| 62 |     sort(&Nr),
 | 
|---|
| 63 |     mol(0)
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   AtomicPosition = pointer->AtomicPosition; // copy trajectory of coordination
 | 
|---|
| 66 |   AtomicVelocity = pointer->AtomicVelocity; // copy trajectory of velocity
 | 
|---|
| 67 |   AtomicForce = pointer->AtomicForce;
 | 
|---|
| 68 | };
 | 
|---|
| 69 | 
 | 
|---|
| 70 | atom *atom::clone(){
 | 
|---|
| 71 |   atom *res = new atom(this);
 | 
|---|
| 72 |   World::getInstance().registerAtom(res);
 | 
|---|
| 73 |   return res;
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | 
 | 
|---|
| 77 | /** Destructor of class atom.
 | 
|---|
| 78 |  */
 | 
|---|
| 79 | atom::~atom()
 | 
|---|
| 80 | {
 | 
|---|
| 81 |   removeFromMolecule();
 | 
|---|
| 82 | };
 | 
|---|
| 83 | 
 | 
|---|
| 84 | 
 | 
|---|
| 85 | void atom::UpdateSteps()
 | 
|---|
| 86 | {
 | 
|---|
| 87 |   LOG(4,"atom::UpdateSteps() called.");
 | 
|---|
| 88 |   // append to position, velocity and force vector
 | 
|---|
| 89 |   AtomInfo::AppendTrajectoryStep();
 | 
|---|
| 90 |   // append to ListOfBonds vector
 | 
|---|
| 91 |   BondedParticleInfo::AppendTrajectoryStep();
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | atom *atom::GetTrueFather()
 | 
|---|
| 95 | {
 | 
|---|
| 96 |   const atom *father = const_cast<const atom *>(this)->GetTrueFather();
 | 
|---|
| 97 |   return const_cast<atom *>(father);
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | const atom *atom::GetTrueFather() const
 | 
|---|
| 101 | {
 | 
|---|
| 102 |   if(father == this){ // top most father is the one that points on itself
 | 
|---|
| 103 |     return this;
 | 
|---|
| 104 |   }
 | 
|---|
| 105 |   else if(!father) {
 | 
|---|
| 106 |     return 0;
 | 
|---|
| 107 |   }
 | 
|---|
| 108 |   else {
 | 
|---|
| 109 |     return father->GetTrueFather();
 | 
|---|
| 110 |   }
 | 
|---|
| 111 | };
 | 
|---|
| 112 | 
 | 
|---|
| 113 | /** Sets father to itself or its father in case of copying a molecule.
 | 
|---|
| 114 |  */
 | 
|---|
| 115 | void atom::CorrectFather()
 | 
|---|
| 116 | {
 | 
|---|
| 117 |   if (father->father != father)   // same atom in copy's father points to itself
 | 
|---|
| 118 | //    father = this;  // set father to itself (copy of a whole molecule)
 | 
|---|
| 119 | //  else
 | 
|---|
| 120 |    father = father->father;  // set father to original's father
 | 
|---|
| 121 | 
 | 
|---|
| 122 | };
 | 
|---|
| 123 | 
 | 
|---|
| 124 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   if ( ptr == father )
 | 
|---|
| 127 |     *res = this;
 | 
|---|
| 128 | };
 | 
|---|
| 129 | 
 | 
|---|
| 130 | bool atom::isFather(const atom *ptr){
 | 
|---|
| 131 |   return ptr==father;
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   if (out != NULL) {
 | 
|---|
| 137 |     *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"  << fixed << setprecision(9) << showpoint;
 | 
|---|
| 138 |     *out << at(0) << "\t" << at(1) << "\t" << at(2);
 | 
|---|
| 139 |     *out << "\t" << (int)(getFixedIon());
 | 
|---|
| 140 |     if (getAtomicVelocity().Norm() > MYEPSILON)
 | 
|---|
| 141 |       *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
 | 
|---|
| 142 |     if (comment != NULL)
 | 
|---|
| 143 |       *out << " # " << comment << endl;
 | 
|---|
| 144 |     else
 | 
|---|
| 145 |       *out << " # molecule nr " << getNr() << endl;
 | 
|---|
| 146 |     return true;
 | 
|---|
| 147 |   } else
 | 
|---|
| 148 |     return false;
 | 
|---|
| 149 | };
 | 
|---|
| 150 | 
 | 
|---|
| 151 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
 | 
|---|
| 152 | {
 | 
|---|
| 153 |   AtomNo[getType()->getAtomicNumber()]++;  // increment number
 | 
|---|
| 154 |   if (out != NULL) {
 | 
|---|
| 155 |     const element *elemental = getType();
 | 
|---|
| 156 |     ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
 | 
|---|
| 157 |     *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t"  << fixed << setprecision(9) << showpoint;
 | 
|---|
| 158 |     *out << at(0) << "\t" << at(1) << "\t" << at(2);
 | 
|---|
| 159 |     *out << "\t" << getFixedIon();
 | 
|---|
| 160 |     if (getAtomicVelocity().Norm() > MYEPSILON)
 | 
|---|
| 161 |       *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
 | 
|---|
| 162 |     if (comment != NULL)
 | 
|---|
| 163 |       *out << " # " << comment << endl;
 | 
|---|
| 164 |     else
 | 
|---|
| 165 |       *out << " # molecule nr " << getNr() << endl;
 | 
|---|
| 166 |     return true;
 | 
|---|
| 167 |   } else
 | 
|---|
| 168 |     return false;
 | 
|---|
| 169 | };
 | 
|---|
| 170 | 
 | 
|---|
| 171 | bool atom::Compare(const atom &ptr) const
 | 
|---|
| 172 | {
 | 
|---|
| 173 |   if (getNr() < ptr.getNr())
 | 
|---|
| 174 |     return true;
 | 
|---|
| 175 |   else
 | 
|---|
| 176 |     return false;
 | 
|---|
| 177 | };
 | 
|---|
| 178 | 
 | 
|---|
| 179 | double atom::DistanceSquaredToVector(const Vector &origin) const
 | 
|---|
| 180 | {
 | 
|---|
| 181 |   return DistanceSquared(origin);
 | 
|---|
| 182 | };
 | 
|---|
| 183 | 
 | 
|---|
| 184 | double atom::DistanceToVector(const Vector &origin) const
 | 
|---|
| 185 | {
 | 
|---|
| 186 |   return distance(origin);
 | 
|---|
| 187 | };
 | 
|---|
| 188 | 
 | 
|---|
| 189 | void atom::InitComponentNr()
 | 
|---|
| 190 | {
 | 
|---|
| 191 |   if (ComponentNr != NULL)
 | 
|---|
| 192 |     delete[](ComponentNr);
 | 
|---|
| 193 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| 194 |   ComponentNr = new int[ListOfBonds.size()+1];
 | 
|---|
| 195 |   for (int i=ListOfBonds.size()+1;i--;)
 | 
|---|
| 196 |     ComponentNr[i] = -1;
 | 
|---|
| 197 | };
 | 
|---|
| 198 | 
 | 
|---|
| 199 | void atom::resetGraphNr(){
 | 
|---|
| 200 |   GraphNr=-1;
 | 
|---|
| 201 | }
 | 
|---|
| 202 | 
 | 
|---|
| 203 | std::ostream & atom::operator << (std::ostream &ost) const
 | 
|---|
| 204 | {
 | 
|---|
| 205 |   ParticleInfo::operator<<(ost);
 | 
|---|
| 206 |   ost << "," << getPosition();
 | 
|---|
| 207 |   return ost;
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|
| 210 | std::ostream & operator << (std::ostream &ost, const atom &a)
 | 
|---|
| 211 | {
 | 
|---|
| 212 |   a.ParticleInfo::operator<<(ost);
 | 
|---|
| 213 |   ost << "," << a.getPosition();
 | 
|---|
| 214 |   return ost;
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | bool operator < (atom &a, atom &b)
 | 
|---|
| 218 | {
 | 
|---|
| 219 |   return a.Compare(b);
 | 
|---|
| 220 | };
 | 
|---|
| 221 | 
 | 
|---|
| 222 | World *atom::getWorld(){
 | 
|---|
| 223 |   return world;
 | 
|---|
| 224 | }
 | 
|---|
| 225 | 
 | 
|---|
| 226 | void atom::setWorld(World* _world){
 | 
|---|
| 227 |   world = _world;
 | 
|---|
| 228 | }
 | 
|---|
| 229 | 
 | 
|---|
| 230 | bool atom::changeId(atomId_t newId){
 | 
|---|
| 231 |   // first we move ourselves in the world
 | 
|---|
| 232 |   // the world lets us know if that succeeded
 | 
|---|
| 233 |   if(world->changeAtomId(id,newId,this)){
 | 
|---|
| 234 |     OBSERVE;
 | 
|---|
| 235 |     id = newId;
 | 
|---|
| 236 |     NOTIFY(IndexChanged);
 | 
|---|
| 237 |     return true;
 | 
|---|
| 238 |   }
 | 
|---|
| 239 |   else{
 | 
|---|
| 240 |     return false;
 | 
|---|
| 241 |   }
 | 
|---|
| 242 | }
 | 
|---|
| 243 | 
 | 
|---|
| 244 | void atom::setId(atomId_t _id) {
 | 
|---|
| 245 |   id=_id;
 | 
|---|
| 246 | }
 | 
|---|
| 247 | 
 | 
|---|
| 248 | atomId_t atom::getId() const {
 | 
|---|
| 249 |   return id;
 | 
|---|
| 250 | }
 | 
|---|
| 251 | 
 | 
|---|
| 252 | void atom::setMolecule(molecule *_mol){
 | 
|---|
| 253 |   // take this atom from the old molecule
 | 
|---|
| 254 |   removeFromMolecule();
 | 
|---|
| 255 |   mol = _mol;
 | 
|---|
| 256 |   if ((mol) && (!mol->containsAtom(this)))
 | 
|---|
| 257 |     mol->insert(this);
 | 
|---|
| 258 | }
 | 
|---|
| 259 | 
 | 
|---|
| 260 | void atom::unsetMolecule()
 | 
|---|
| 261 | {
 | 
|---|
| 262 |   // take this atom from the old molecule
 | 
|---|
| 263 |   ASSERT(!mol->containsAtom(this),
 | 
|---|
| 264 |       "atom::unsetMolecule() - old molecule "+toString(mol)+" still contains us!");
 | 
|---|
| 265 |   mol = NULL;
 | 
|---|
| 266 | }
 | 
|---|
| 267 | 
 | 
|---|
| 268 | molecule* atom::getMolecule() const {
 | 
|---|
| 269 |   return mol;
 | 
|---|
| 270 | }
 | 
|---|
| 271 | 
 | 
|---|
| 272 | void atom::removeFromMolecule(){
 | 
|---|
| 273 |   if(mol){
 | 
|---|
| 274 |     if(mol->containsAtom(this)){
 | 
|---|
| 275 |       mol->erase(this);
 | 
|---|
| 276 |     }
 | 
|---|
| 277 |     mol=0;
 | 
|---|
| 278 |   }
 | 
|---|
| 279 | }
 | 
|---|
| 280 | 
 | 
|---|
| 281 | bool atom::changeNr(const int newNr)
 | 
|---|
| 282 | {
 | 
|---|
| 283 |   if ((mol) && (mol->changeAtomNr(getNr(),newNr,this))) {
 | 
|---|
| 284 |     return true;
 | 
|---|
| 285 |   } else{
 | 
|---|
| 286 |     return false;
 | 
|---|
| 287 |   }
 | 
|---|
| 288 | }
 | 
|---|
| 289 | 
 | 
|---|
| 290 | int atom::getNr() const{
 | 
|---|
| 291 |   return ParticleInfo::getNr();
 | 
|---|
| 292 | }
 | 
|---|
| 293 | 
 | 
|---|
| 294 | atom* NewAtom(atomId_t _id){
 | 
|---|
| 295 |   atom * res =new atom();
 | 
|---|
| 296 |   res->setId(_id);
 | 
|---|
| 297 |   return res;
 | 
|---|
| 298 | }
 | 
|---|
| 299 | 
 | 
|---|
| 300 | void DeleteAtom(atom* atom){
 | 
|---|
| 301 |   delete atom;
 | 
|---|
| 302 | }
 | 
|---|
| 303 | 
 | 
|---|
| 304 | bool compareAtomElements(atom* atom1,atom* atom2){
 | 
|---|
| 305 |   return atom1->getType()->getAtomicNumber() < atom2->getType()->getAtomicNumber();
 | 
|---|
| 306 | }
 | 
|---|