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