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