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