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