| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [14de469] | 8 | /** \file atom.cpp | 
|---|
| [1907a7] | 9 | * | 
|---|
| [14de469] | 10 | * Function implementations for the class atom. | 
|---|
| [1907a7] | 11 | * | 
|---|
| [14de469] | 12 | */ | 
|---|
|  | 13 |  | 
|---|
| [bf3817] | 14 | // include config.h | 
|---|
|  | 15 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 16 | #include <config.h> | 
|---|
|  | 17 | #endif | 
|---|
|  | 18 |  | 
|---|
| [ad011c] | 19 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 20 |  | 
|---|
| [357fba] | 21 | #include "atom.hpp" | 
|---|
| [129204] | 22 | #include "Bond/bond.hpp" | 
|---|
| [e2373df] | 23 | #include "CodePatterns/Log.hpp" | 
|---|
| [4a7776a] | 24 | #include "config.hpp" | 
|---|
| [3bdb6d] | 25 | #include "Element/element.hpp" | 
|---|
| [57f243] | 26 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [d346b6] | 27 | #include "World.hpp" | 
|---|
| [6cfa36] | 28 | #include "molecule.hpp" | 
|---|
| [c550dd] | 29 | #include "Shapes/Shape.hpp" | 
|---|
| [a0064e] | 30 |  | 
|---|
| [36166d] | 31 | #include <iomanip> | 
|---|
| [0ba410] | 32 | #include <iostream> | 
|---|
| [36166d] | 33 |  | 
|---|
| [14de469] | 34 | /************************************* Functions for class atom *************************************/ | 
|---|
|  | 35 |  | 
|---|
| [70ff32] | 36 |  | 
|---|
| [46d958] | 37 | atom::atom() : | 
|---|
| [97b825] | 38 | father(this), | 
|---|
| [5309ba] | 39 | sort(&Nr), | 
|---|
| [97b825] | 40 | mol(0) | 
|---|
| [d74077] | 41 | {}; | 
|---|
| [14de469] | 42 |  | 
|---|
| [46d958] | 43 | atom::atom(atom *pointer) : | 
|---|
| [97b825] | 44 | ParticleInfo(pointer), | 
|---|
|  | 45 | father(pointer), | 
|---|
| [5309ba] | 46 | sort(&Nr), | 
|---|
| [9df680] | 47 | mol(0) | 
|---|
| [2319ed] | 48 | { | 
|---|
| [443547] | 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; | 
|---|
| [6625c3] | 53 | setFixedIon(pointer->getFixedIon()); | 
|---|
| [b453f9] | 54 | }; | 
|---|
| [2319ed] | 55 |  | 
|---|
| [46d958] | 56 | atom *atom::clone(){ | 
|---|
| [68f03d] | 57 | atom *res = new atom(this); | 
|---|
| [23b547] | 58 | World::getInstance().registerAtom(res); | 
|---|
| [46d958] | 59 | return res; | 
|---|
|  | 60 | } | 
|---|
|  | 61 |  | 
|---|
| [2319ed] | 62 |  | 
|---|
| [14de469] | 63 | /** Destructor of class atom. | 
|---|
|  | 64 | */ | 
|---|
| [1907a7] | 65 | atom::~atom() | 
|---|
| [14de469] | 66 | { | 
|---|
| [6cfa36] | 67 | removeFromMolecule(); | 
|---|
| [14de469] | 68 | }; | 
|---|
| [e2373df] | 69 |  | 
|---|
|  | 70 |  | 
|---|
|  | 71 | void atom::UpdateSteps() | 
|---|
|  | 72 | { | 
|---|
|  | 73 | LOG(4,"atom::UpdateSteps() called."); | 
|---|
|  | 74 | // append to position, velocity and force vector | 
|---|
|  | 75 | AtomInfo::AppendTrajectoryStep(); | 
|---|
| [1e6249] | 76 | // append to ListOfBonds vector | 
|---|
|  | 77 | BondedParticleInfo::AppendTrajectoryStep(); | 
|---|
| [e2373df] | 78 | } | 
|---|
|  | 79 |  | 
|---|
| [59fff1] | 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 | 
|---|
| [14de469] | 87 | { | 
|---|
| [215df0] | 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 | } | 
|---|
| [14de469] | 97 | }; | 
|---|
|  | 98 |  | 
|---|
| [e65246] | 99 | /** Sets father to itself or its father in case of copying a molecule. | 
|---|
|  | 100 | */ | 
|---|
|  | 101 | void atom::CorrectFather() | 
|---|
|  | 102 | { | 
|---|
| [2e352f] | 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 | 
|---|
| [e65246] | 106 | father = father->father;  // set father to original's father | 
|---|
|  | 107 |  | 
|---|
|  | 108 | }; | 
|---|
|  | 109 |  | 
|---|
| [b453f9] | 110 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const | 
|---|
| [e65246] | 111 | { | 
|---|
|  | 112 | if ( ptr == father ) | 
|---|
|  | 113 | *res = this; | 
|---|
|  | 114 | }; | 
|---|
|  | 115 |  | 
|---|
| [00abfc] | 116 | bool atom::isFather(const atom *ptr){ | 
|---|
|  | 117 | return ptr==father; | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
| [c550dd] | 120 | bool atom::IsInShape(const Shape& shape) const | 
|---|
| [e9f8f9] | 121 | { | 
|---|
| [d74077] | 122 | return shape.isInside(getPosition()); | 
|---|
| [e9f8f9] | 123 | }; | 
|---|
|  | 124 |  | 
|---|
| [e138de] | 125 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const | 
|---|
| [14de469] | 126 | { | 
|---|
|  | 127 | if (out != NULL) { | 
|---|
|  | 128 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [d74077] | 129 | *out << at(0) << "\t" << at(1) << "\t" << at(2); | 
|---|
| [6625c3] | 130 | *out << "\t" << (int)(getFixedIon()); | 
|---|
| [bce72c] | 131 | if (getAtomicVelocity().Norm() > MYEPSILON) | 
|---|
|  | 132 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t"; | 
|---|
| [437922] | 133 | if (comment != NULL) | 
|---|
|  | 134 | *out << " # " << comment << endl; | 
|---|
| [e9f8f9] | 135 | else | 
|---|
| [735b1c] | 136 | *out << " # molecule nr " << getNr() << endl; | 
|---|
| [e9f8f9] | 137 | return true; | 
|---|
|  | 138 | } else | 
|---|
|  | 139 | return false; | 
|---|
|  | 140 | }; | 
|---|
| [b453f9] | 141 |  | 
|---|
| [0ba410] | 142 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const | 
|---|
| [e9f8f9] | 143 | { | 
|---|
| [83f176] | 144 | AtomNo[getType()->getAtomicNumber()]++;  // increment number | 
|---|
| [e9f8f9] | 145 | if (out != NULL) { | 
|---|
| [8f4df1] | 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"); | 
|---|
| [83f176] | 148 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [d74077] | 149 | *out << at(0) << "\t" << at(1) << "\t" << at(2); | 
|---|
| [6625c3] | 150 | *out << "\t" << getFixedIon(); | 
|---|
| [bce72c] | 151 | if (getAtomicVelocity().Norm() > MYEPSILON) | 
|---|
|  | 152 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t"; | 
|---|
| [e9f8f9] | 153 | if (comment != NULL) | 
|---|
|  | 154 | *out << " # " << comment << endl; | 
|---|
| [437922] | 155 | else | 
|---|
| [735b1c] | 156 | *out << " # molecule nr " << getNr() << endl; | 
|---|
| [14de469] | 157 | return true; | 
|---|
|  | 158 | } else | 
|---|
|  | 159 | return false; | 
|---|
|  | 160 | }; | 
|---|
|  | 161 |  | 
|---|
|  | 162 | bool atom::OutputXYZLine(ofstream *out) const | 
|---|
|  | 163 | { | 
|---|
|  | 164 | if (out != NULL) { | 
|---|
| [b5c53d] | 165 | *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl; | 
|---|
| [14de469] | 166 | return true; | 
|---|
|  | 167 | } else | 
|---|
|  | 168 | return false; | 
|---|
|  | 169 | }; | 
|---|
|  | 170 |  | 
|---|
| [882a8a] | 171 | bool atom::OutputTrajectory(ofstream * const out, const enumeration<const element*> &elementLookup, int *AtomNo, const int step) const | 
|---|
| [fcd7b6] | 172 | { | 
|---|
| [83f176] | 173 | AtomNo[getType()->getAtomicNumber()]++; | 
|---|
| [882a8a] | 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; | 
|---|
| [056e70] | 178 | *out << getPositionAtStep(step)[0] << "\t" << getPositionAtStep(step)[1] << "\t" << getPositionAtStep(step)[2]; | 
|---|
| [6625c3] | 179 | *out << "\t" << (int)(getFixedIon()); | 
|---|
| [056e70] | 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"; | 
|---|
| [735b1c] | 184 | *out << "\t# Number in molecule " << getNr() << endl; | 
|---|
| [fcd7b6] | 185 | return true; | 
|---|
|  | 186 | } else | 
|---|
|  | 187 | return false; | 
|---|
|  | 188 | }; | 
|---|
|  | 189 |  | 
|---|
| [e138de] | 190 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const | 
|---|
| [681a8a] | 191 | { | 
|---|
|  | 192 | if (out != NULL) { | 
|---|
| [b5c53d] | 193 | *out << getType()->getSymbol() << "\t"; | 
|---|
| [056e70] | 194 | *out << getPositionAtStep(step)[0] << "\t"; | 
|---|
|  | 195 | *out << getPositionAtStep(step)[1] << "\t"; | 
|---|
|  | 196 | *out << getPositionAtStep(step)[2] << endl; | 
|---|
| [681a8a] | 197 | return true; | 
|---|
|  | 198 | } else | 
|---|
|  | 199 | return false; | 
|---|
|  | 200 | }; | 
|---|
|  | 201 |  | 
|---|
| [0dc86e2] | 202 | void atom::OutputMPQCLine(ostream * const out, const Vector *center) const | 
|---|
| [4455f4] | 203 | { | 
|---|
| [d74077] | 204 | Vector recentered(getPosition()); | 
|---|
|  | 205 | recentered -= *center; | 
|---|
| [b5c53d] | 206 | *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl; | 
|---|
| [4455f4] | 207 | }; | 
|---|
| [9011c1] | 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 | }; | 
|---|
| [4455f4] | 215 |  | 
|---|
| [b453f9] | 216 | bool atom::Compare(const atom &ptr) const | 
|---|
| [4455f4] | 217 | { | 
|---|
| [735b1c] | 218 | if (getNr() < ptr.getNr()) | 
|---|
| [4455f4] | 219 | return true; | 
|---|
|  | 220 | else | 
|---|
|  | 221 | return false; | 
|---|
|  | 222 | }; | 
|---|
|  | 223 |  | 
|---|
| [b453f9] | 224 | double atom::DistanceSquaredToVector(const Vector &origin) const | 
|---|
| [4455f4] | 225 | { | 
|---|
| [d74077] | 226 | return DistanceSquared(origin); | 
|---|
| [4455f4] | 227 | }; | 
|---|
|  | 228 |  | 
|---|
| [b453f9] | 229 | double atom::DistanceToVector(const Vector &origin) const | 
|---|
| [4455f4] | 230 | { | 
|---|
| [d74077] | 231 | return distance(origin); | 
|---|
| [4455f4] | 232 | }; | 
|---|
|  | 233 |  | 
|---|
|  | 234 | void atom::InitComponentNr() | 
|---|
|  | 235 | { | 
|---|
|  | 236 | if (ComponentNr != NULL) | 
|---|
| [920c70] | 237 | delete[](ComponentNr); | 
|---|
| [9d83b6] | 238 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [920c70] | 239 | ComponentNr = new int[ListOfBonds.size()+1]; | 
|---|
| [4455f4] | 240 | for (int i=ListOfBonds.size()+1;i--;) | 
|---|
|  | 241 | ComponentNr[i] = -1; | 
|---|
| [14b65e] | 242 | }; | 
|---|
|  | 243 |  | 
|---|
|  | 244 | void atom::resetGraphNr(){ | 
|---|
|  | 245 | GraphNr=-1; | 
|---|
|  | 246 | } | 
|---|
| [4455f4] | 247 |  | 
|---|
| [d74077] | 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 | } | 
|---|
| [4455f4] | 261 |  | 
|---|
|  | 262 | bool operator < (atom &a, atom &b) | 
|---|
|  | 263 | { | 
|---|
|  | 264 | return a.Compare(b); | 
|---|
|  | 265 | }; | 
|---|
|  | 266 |  | 
|---|
| [46d958] | 267 | World *atom::getWorld(){ | 
|---|
|  | 268 | return world; | 
|---|
|  | 269 | } | 
|---|
|  | 270 |  | 
|---|
|  | 271 | void atom::setWorld(World* _world){ | 
|---|
|  | 272 | world = _world; | 
|---|
|  | 273 | } | 
|---|
|  | 274 |  | 
|---|
| [88d586] | 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) { | 
|---|
| [46d958] | 288 | id=_id; | 
|---|
|  | 289 | } | 
|---|
|  | 290 |  | 
|---|
| [ad2b411] | 291 | atomId_t atom::getId() const { | 
|---|
| [46d958] | 292 | return id; | 
|---|
|  | 293 | } | 
|---|
|  | 294 |  | 
|---|
| [6cfa36] | 295 | void atom::setMolecule(molecule *_mol){ | 
|---|
|  | 296 | // take this atom from the old molecule | 
|---|
|  | 297 | removeFromMolecule(); | 
|---|
| [3867a7] | 298 | mol = _mol; | 
|---|
|  | 299 | if ((mol) && (!mol->containsAtom(this))) | 
|---|
| [dddbfe] | 300 | mol->insert(this); | 
|---|
| [6cfa36] | 301 | } | 
|---|
|  | 302 |  | 
|---|
| [0d9546] | 303 | void atom::unsetMolecule() | 
|---|
|  | 304 | { | 
|---|
|  | 305 | // take this atom from the old molecule | 
|---|
|  | 306 | ASSERT(!mol->containsAtom(this), | 
|---|
|  | 307 | "atom::unsetMolecule() - old molecule "+toString(mol)+" still contains us!"); | 
|---|
|  | 308 | mol = NULL; | 
|---|
|  | 309 | } | 
|---|
|  | 310 |  | 
|---|
| [e41c48] | 311 | molecule* atom::getMolecule() const { | 
|---|
| [c084cc] | 312 | return mol; | 
|---|
|  | 313 | } | 
|---|
|  | 314 |  | 
|---|
| [6cfa36] | 315 | void atom::removeFromMolecule(){ | 
|---|
|  | 316 | if(mol){ | 
|---|
|  | 317 | if(mol->containsAtom(this)){ | 
|---|
|  | 318 | mol->erase(this); | 
|---|
|  | 319 | } | 
|---|
|  | 320 | mol=0; | 
|---|
|  | 321 | } | 
|---|
| [1f8337] | 322 | } | 
|---|
|  | 323 |  | 
|---|
| [560bbe] | 324 | bool atom::changeNr(const int newNr) | 
|---|
|  | 325 | { | 
|---|
|  | 326 | if ((mol) && (mol->changeAtomNr(getNr(),newNr,this))) { | 
|---|
|  | 327 | return true; | 
|---|
|  | 328 | } else{ | 
|---|
|  | 329 | return false; | 
|---|
|  | 330 | } | 
|---|
|  | 331 | } | 
|---|
|  | 332 |  | 
|---|
| [e8a21f] | 333 | int atom::getNr() const{ | 
|---|
| [735b1c] | 334 | return ParticleInfo::getNr(); | 
|---|
| [e8a21f] | 335 | } | 
|---|
| [6cfa36] | 336 |  | 
|---|
| [88d586] | 337 | atom* NewAtom(atomId_t _id){ | 
|---|
|  | 338 | atom * res =new atom(); | 
|---|
|  | 339 | res->setId(_id); | 
|---|
|  | 340 | return res; | 
|---|
| [46d958] | 341 | } | 
|---|
|  | 342 |  | 
|---|
| [88d586] | 343 | void DeleteAtom(atom* atom){ | 
|---|
| [46d958] | 344 | delete atom; | 
|---|
| [e5f64de] | 345 | } | 
|---|
|  | 346 |  | 
|---|
|  | 347 | bool compareAtomElements(atom* atom1,atom* atom2){ | 
|---|
|  | 348 | return atom1->getType()->getNumber() < atom2->getType()->getNumber(); | 
|---|
| [46d958] | 349 | } | 
|---|