| [bcf653] | 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 |  | 
|---|
| [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 |  | 
|---|
| [14de469] | 80 | atom *atom::GetTrueFather() | 
|---|
|  | 81 | { | 
|---|
| [215df0] | 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 | } | 
|---|
| [14de469] | 91 | }; | 
|---|
|  | 92 |  | 
|---|
| [e65246] | 93 | /** Sets father to itself or its father in case of copying a molecule. | 
|---|
|  | 94 | */ | 
|---|
|  | 95 | void atom::CorrectFather() | 
|---|
|  | 96 | { | 
|---|
| [2e352f] | 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 | 
|---|
| [e65246] | 100 | father = father->father;  // set father to original's father | 
|---|
|  | 101 |  | 
|---|
|  | 102 | }; | 
|---|
|  | 103 |  | 
|---|
| [b453f9] | 104 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const | 
|---|
| [e65246] | 105 | { | 
|---|
|  | 106 | if ( ptr == father ) | 
|---|
|  | 107 | *res = this; | 
|---|
|  | 108 | }; | 
|---|
|  | 109 |  | 
|---|
| [00abfc] | 110 | bool atom::isFather(const atom *ptr){ | 
|---|
|  | 111 | return ptr==father; | 
|---|
|  | 112 | } | 
|---|
|  | 113 |  | 
|---|
| [c550dd] | 114 | bool atom::IsInShape(const Shape& shape) const | 
|---|
| [e9f8f9] | 115 | { | 
|---|
| [d74077] | 116 | return shape.isInside(getPosition()); | 
|---|
| [e9f8f9] | 117 | }; | 
|---|
|  | 118 |  | 
|---|
| [e138de] | 119 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const | 
|---|
| [14de469] | 120 | { | 
|---|
|  | 121 | if (out != NULL) { | 
|---|
|  | 122 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [d74077] | 123 | *out << at(0) << "\t" << at(1) << "\t" << at(2); | 
|---|
| [6625c3] | 124 | *out << "\t" << (int)(getFixedIon()); | 
|---|
| [bce72c] | 125 | if (getAtomicVelocity().Norm() > MYEPSILON) | 
|---|
|  | 126 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t"; | 
|---|
| [437922] | 127 | if (comment != NULL) | 
|---|
|  | 128 | *out << " # " << comment << endl; | 
|---|
| [e9f8f9] | 129 | else | 
|---|
| [735b1c] | 130 | *out << " # molecule nr " << getNr() << endl; | 
|---|
| [e9f8f9] | 131 | return true; | 
|---|
|  | 132 | } else | 
|---|
|  | 133 | return false; | 
|---|
|  | 134 | }; | 
|---|
| [b453f9] | 135 |  | 
|---|
| [0ba410] | 136 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const | 
|---|
| [e9f8f9] | 137 | { | 
|---|
| [83f176] | 138 | AtomNo[getType()->getAtomicNumber()]++;  // increment number | 
|---|
| [e9f8f9] | 139 | if (out != NULL) { | 
|---|
| [8f4df1] | 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"); | 
|---|
| [83f176] | 142 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [d74077] | 143 | *out << at(0) << "\t" << at(1) << "\t" << at(2); | 
|---|
| [6625c3] | 144 | *out << "\t" << getFixedIon(); | 
|---|
| [bce72c] | 145 | if (getAtomicVelocity().Norm() > MYEPSILON) | 
|---|
|  | 146 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t"; | 
|---|
| [e9f8f9] | 147 | if (comment != NULL) | 
|---|
|  | 148 | *out << " # " << comment << endl; | 
|---|
| [437922] | 149 | else | 
|---|
| [735b1c] | 150 | *out << " # molecule nr " << getNr() << endl; | 
|---|
| [14de469] | 151 | return true; | 
|---|
|  | 152 | } else | 
|---|
|  | 153 | return false; | 
|---|
|  | 154 | }; | 
|---|
|  | 155 |  | 
|---|
|  | 156 | bool atom::OutputXYZLine(ofstream *out) const | 
|---|
|  | 157 | { | 
|---|
|  | 158 | if (out != NULL) { | 
|---|
| [b5c53d] | 159 | *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl; | 
|---|
| [14de469] | 160 | return true; | 
|---|
|  | 161 | } else | 
|---|
|  | 162 | return false; | 
|---|
|  | 163 | }; | 
|---|
|  | 164 |  | 
|---|
| [882a8a] | 165 | bool atom::OutputTrajectory(ofstream * const out, const enumeration<const element*> &elementLookup, int *AtomNo, const int step) const | 
|---|
| [fcd7b6] | 166 | { | 
|---|
| [83f176] | 167 | AtomNo[getType()->getAtomicNumber()]++; | 
|---|
| [882a8a] | 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; | 
|---|
| [056e70] | 172 | *out << getPositionAtStep(step)[0] << "\t" << getPositionAtStep(step)[1] << "\t" << getPositionAtStep(step)[2]; | 
|---|
| [6625c3] | 173 | *out << "\t" << (int)(getFixedIon()); | 
|---|
| [056e70] | 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"; | 
|---|
| [735b1c] | 178 | *out << "\t# Number in molecule " << getNr() << endl; | 
|---|
| [fcd7b6] | 179 | return true; | 
|---|
|  | 180 | } else | 
|---|
|  | 181 | return false; | 
|---|
|  | 182 | }; | 
|---|
|  | 183 |  | 
|---|
| [e138de] | 184 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const | 
|---|
| [681a8a] | 185 | { | 
|---|
|  | 186 | if (out != NULL) { | 
|---|
| [b5c53d] | 187 | *out << getType()->getSymbol() << "\t"; | 
|---|
| [056e70] | 188 | *out << getPositionAtStep(step)[0] << "\t"; | 
|---|
|  | 189 | *out << getPositionAtStep(step)[1] << "\t"; | 
|---|
|  | 190 | *out << getPositionAtStep(step)[2] << endl; | 
|---|
| [681a8a] | 191 | return true; | 
|---|
|  | 192 | } else | 
|---|
|  | 193 | return false; | 
|---|
|  | 194 | }; | 
|---|
|  | 195 |  | 
|---|
| [0dc86e2] | 196 | void atom::OutputMPQCLine(ostream * const out, const Vector *center) const | 
|---|
| [4455f4] | 197 | { | 
|---|
| [d74077] | 198 | Vector recentered(getPosition()); | 
|---|
|  | 199 | recentered -= *center; | 
|---|
| [b5c53d] | 200 | *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl; | 
|---|
| [4455f4] | 201 | }; | 
|---|
| [9011c1] | 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 | }; | 
|---|
| [4455f4] | 209 |  | 
|---|
| [b453f9] | 210 | bool atom::Compare(const atom &ptr) const | 
|---|
| [4455f4] | 211 | { | 
|---|
| [735b1c] | 212 | if (getNr() < ptr.getNr()) | 
|---|
| [4455f4] | 213 | return true; | 
|---|
|  | 214 | else | 
|---|
|  | 215 | return false; | 
|---|
|  | 216 | }; | 
|---|
|  | 217 |  | 
|---|
| [b453f9] | 218 | double atom::DistanceSquaredToVector(const Vector &origin) const | 
|---|
| [4455f4] | 219 | { | 
|---|
| [d74077] | 220 | return DistanceSquared(origin); | 
|---|
| [4455f4] | 221 | }; | 
|---|
|  | 222 |  | 
|---|
| [b453f9] | 223 | double atom::DistanceToVector(const Vector &origin) const | 
|---|
| [4455f4] | 224 | { | 
|---|
| [d74077] | 225 | return distance(origin); | 
|---|
| [4455f4] | 226 | }; | 
|---|
|  | 227 |  | 
|---|
|  | 228 | void atom::InitComponentNr() | 
|---|
|  | 229 | { | 
|---|
|  | 230 | if (ComponentNr != NULL) | 
|---|
| [920c70] | 231 | delete[](ComponentNr); | 
|---|
| [9d83b6] | 232 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [920c70] | 233 | ComponentNr = new int[ListOfBonds.size()+1]; | 
|---|
| [4455f4] | 234 | for (int i=ListOfBonds.size()+1;i--;) | 
|---|
|  | 235 | ComponentNr[i] = -1; | 
|---|
| [14b65e] | 236 | }; | 
|---|
|  | 237 |  | 
|---|
|  | 238 | void atom::resetGraphNr(){ | 
|---|
|  | 239 | GraphNr=-1; | 
|---|
|  | 240 | } | 
|---|
| [4455f4] | 241 |  | 
|---|
| [d74077] | 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 | } | 
|---|
| [4455f4] | 255 |  | 
|---|
|  | 256 | bool operator < (atom &a, atom &b) | 
|---|
|  | 257 | { | 
|---|
|  | 258 | return a.Compare(b); | 
|---|
|  | 259 | }; | 
|---|
|  | 260 |  | 
|---|
| [46d958] | 261 | World *atom::getWorld(){ | 
|---|
|  | 262 | return world; | 
|---|
|  | 263 | } | 
|---|
|  | 264 |  | 
|---|
|  | 265 | void atom::setWorld(World* _world){ | 
|---|
|  | 266 | world = _world; | 
|---|
|  | 267 | } | 
|---|
|  | 268 |  | 
|---|
| [88d586] | 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) { | 
|---|
| [46d958] | 282 | id=_id; | 
|---|
|  | 283 | } | 
|---|
|  | 284 |  | 
|---|
| [ad2b411] | 285 | atomId_t atom::getId() const { | 
|---|
| [46d958] | 286 | return id; | 
|---|
|  | 287 | } | 
|---|
|  | 288 |  | 
|---|
| [6cfa36] | 289 | void atom::setMolecule(molecule *_mol){ | 
|---|
|  | 290 | // take this atom from the old molecule | 
|---|
|  | 291 | removeFromMolecule(); | 
|---|
|  | 292 | mol = _mol; | 
|---|
|  | 293 | if(!mol->containsAtom(this)){ | 
|---|
| [dddbfe] | 294 | mol->insert(this); | 
|---|
| [6cfa36] | 295 | } | 
|---|
|  | 296 | } | 
|---|
|  | 297 |  | 
|---|
| [0d9546] | 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 |  | 
|---|
| [e41c48] | 306 | molecule* atom::getMolecule() const { | 
|---|
| [c084cc] | 307 | return mol; | 
|---|
|  | 308 | } | 
|---|
|  | 309 |  | 
|---|
| [6cfa36] | 310 | void atom::removeFromMolecule(){ | 
|---|
|  | 311 | if(mol){ | 
|---|
|  | 312 | if(mol->containsAtom(this)){ | 
|---|
|  | 313 | mol->erase(this); | 
|---|
|  | 314 | } | 
|---|
|  | 315 | mol=0; | 
|---|
|  | 316 | } | 
|---|
| [1f8337] | 317 | } | 
|---|
|  | 318 |  | 
|---|
| [e8a21f] | 319 | int atom::getNr() const{ | 
|---|
| [735b1c] | 320 | return ParticleInfo::getNr(); | 
|---|
| [e8a21f] | 321 | } | 
|---|
| [6cfa36] | 322 |  | 
|---|
| [88d586] | 323 | atom* NewAtom(atomId_t _id){ | 
|---|
|  | 324 | atom * res =new atom(); | 
|---|
|  | 325 | res->setId(_id); | 
|---|
|  | 326 | return res; | 
|---|
| [46d958] | 327 | } | 
|---|
|  | 328 |  | 
|---|
| [88d586] | 329 | void DeleteAtom(atom* atom){ | 
|---|
| [46d958] | 330 | delete atom; | 
|---|
| [e5f64de] | 331 | } | 
|---|
|  | 332 |  | 
|---|
|  | 333 | bool compareAtomElements(atom* atom1,atom* atom2){ | 
|---|
|  | 334 | return atom1->getType()->getNumber() < atom2->getType()->getNumber(); | 
|---|
| [46d958] | 335 | } | 
|---|