| [14de469] | 1 | /** \file atom.cpp | 
|---|
| [1907a7] | 2 | * | 
|---|
| [14de469] | 3 | * Function implementations for the class atom. | 
|---|
| [1907a7] | 4 | * | 
|---|
| [14de469] | 5 | */ | 
|---|
|  | 6 |  | 
|---|
| [112b09] | 7 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 8 |  | 
|---|
| [357fba] | 9 | #include "atom.hpp" | 
|---|
| [e41951] | 10 | #include "bond.hpp" | 
|---|
| [4a7776a] | 11 | #include "config.hpp" | 
|---|
| [f66195] | 12 | #include "element.hpp" | 
|---|
| [266237] | 13 | #include "lists.hpp" | 
|---|
| [ccd9f5] | 14 | #include "parser.hpp" | 
|---|
| [57f243] | 15 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [d346b6] | 16 | #include "World.hpp" | 
|---|
| [6cfa36] | 17 | #include "molecule.hpp" | 
|---|
| [c550dd] | 18 | #include "Shapes/Shape.hpp" | 
|---|
| [1907a7] | 19 |  | 
|---|
| [36166d] | 20 | #include <iomanip> | 
|---|
| [0ba410] | 21 | #include <iostream> | 
|---|
| [36166d] | 22 |  | 
|---|
| [14de469] | 23 | /************************************* Functions for class atom *************************************/ | 
|---|
|  | 24 |  | 
|---|
| [70ff32] | 25 |  | 
|---|
| [14de469] | 26 | /** Constructor of class atom. | 
|---|
|  | 27 | */ | 
|---|
| [46d958] | 28 | atom::atom() : | 
|---|
| [97b825] | 29 | father(this), | 
|---|
|  | 30 | sort(&nr), | 
|---|
|  | 31 | mol(0) | 
|---|
| [d74077] | 32 | {}; | 
|---|
| [14de469] | 33 |  | 
|---|
| [2319ed] | 34 | /** Constructor of class atom. | 
|---|
|  | 35 | */ | 
|---|
| [46d958] | 36 | atom::atom(atom *pointer) : | 
|---|
| [97b825] | 37 | ParticleInfo(pointer), | 
|---|
|  | 38 | father(pointer), | 
|---|
|  | 39 | sort(&nr) | 
|---|
| [2319ed] | 40 | { | 
|---|
| [d74077] | 41 | setType(pointer->getType());  // copy element of atom | 
|---|
|  | 42 | setPosition(pointer->getPosition()); // copy coordination | 
|---|
|  | 43 | AtomicVelocity = pointer->AtomicVelocity; // copy velocity | 
|---|
| [2319ed] | 44 | FixedIon = pointer->FixedIon; | 
|---|
| [6cfa36] | 45 | mol = 0; | 
|---|
| [b453f9] | 46 | }; | 
|---|
| [2319ed] | 47 |  | 
|---|
| [46d958] | 48 | atom *atom::clone(){ | 
|---|
| [68f03d] | 49 | atom *res = new atom(this); | 
|---|
| [46d958] | 50 | res->father = this; | 
|---|
| [5f612ee] | 51 | res->sort = &res->nr; | 
|---|
| [d74077] | 52 | res->setType(getType()); | 
|---|
|  | 53 | res->setPosition(this->getPosition()); | 
|---|
|  | 54 | res->AtomicVelocity = this->AtomicVelocity; | 
|---|
| [46d958] | 55 | res->FixedIon = FixedIon; | 
|---|
| [6cfa36] | 56 | res->mol = 0; | 
|---|
| [23b547] | 57 | World::getInstance().registerAtom(res); | 
|---|
| [46d958] | 58 | return res; | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
| [2319ed] | 61 |  | 
|---|
| [14de469] | 62 | /** Destructor of class atom. | 
|---|
|  | 63 | */ | 
|---|
| [1907a7] | 64 | atom::~atom() | 
|---|
| [14de469] | 65 | { | 
|---|
| [6cfa36] | 66 | removeFromMolecule(); | 
|---|
| [a80241] | 67 | for(BondList::iterator iter=ListOfBonds.begin(); iter!=ListOfBonds.end();){ | 
|---|
|  | 68 | // deleting the bond will invalidate the iterator !!! | 
|---|
|  | 69 | bond *bond =*(iter++); | 
|---|
|  | 70 | delete(bond); | 
|---|
|  | 71 | } | 
|---|
| [14de469] | 72 | }; | 
|---|
|  | 73 |  | 
|---|
|  | 74 |  | 
|---|
|  | 75 | /** Climbs up the father list until NULL, last is returned. | 
|---|
|  | 76 | * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen) | 
|---|
|  | 77 | */ | 
|---|
|  | 78 | atom *atom::GetTrueFather() | 
|---|
|  | 79 | { | 
|---|
| [215df0] | 80 | if(father == this){ // top most father is the one that points on itself | 
|---|
|  | 81 | return this; | 
|---|
|  | 82 | } | 
|---|
|  | 83 | else if(!father) { | 
|---|
|  | 84 | return 0; | 
|---|
|  | 85 | } | 
|---|
|  | 86 | else { | 
|---|
|  | 87 | return father->GetTrueFather(); | 
|---|
|  | 88 | } | 
|---|
| [14de469] | 89 | }; | 
|---|
|  | 90 |  | 
|---|
| [e65246] | 91 | /** Sets father to itself or its father in case of copying a molecule. | 
|---|
|  | 92 | */ | 
|---|
|  | 93 | void atom::CorrectFather() | 
|---|
|  | 94 | { | 
|---|
|  | 95 | if (father->father == father)   // same atom in copy's father points to itself | 
|---|
|  | 96 | father = this;  // set father to itself (copy of a whole molecule) | 
|---|
|  | 97 | else | 
|---|
|  | 98 | father = father->father;  // set father to original's father | 
|---|
|  | 99 |  | 
|---|
|  | 100 | }; | 
|---|
|  | 101 |  | 
|---|
|  | 102 | /** Check whether father is equal to given atom. | 
|---|
|  | 103 | * \param *ptr atom to compare father to | 
|---|
|  | 104 | * \param **res return value (only set if atom::father is equal to \a *ptr) | 
|---|
|  | 105 | */ | 
|---|
| [b453f9] | 106 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const | 
|---|
| [e65246] | 107 | { | 
|---|
|  | 108 | if ( ptr == father ) | 
|---|
|  | 109 | *res = this; | 
|---|
|  | 110 | }; | 
|---|
|  | 111 |  | 
|---|
| [00abfc] | 112 | bool atom::isFather(const atom *ptr){ | 
|---|
|  | 113 | return ptr==father; | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
| [e9f8f9] | 116 | /** Checks whether atom is within the given box. | 
|---|
|  | 117 | * \param offset offset to box origin | 
|---|
|  | 118 | * \param *parallelepiped box matrix | 
|---|
|  | 119 | * \return true - is inside, false - is not | 
|---|
|  | 120 | */ | 
|---|
| [c550dd] | 121 | bool atom::IsInShape(const Shape& shape) const | 
|---|
| [e9f8f9] | 122 | { | 
|---|
| [d74077] | 123 | return shape.isInside(getPosition()); | 
|---|
| [e9f8f9] | 124 | }; | 
|---|
|  | 125 |  | 
|---|
| [266237] | 126 | /** Counts the number of bonds weighted by bond::BondDegree. | 
|---|
|  | 127 | * \param bonds times bond::BondDegree | 
|---|
|  | 128 | */ | 
|---|
| [4455f4] | 129 | int BondedParticle::CountBonds() const | 
|---|
| [266237] | 130 | { | 
|---|
|  | 131 | int NoBonds = 0; | 
|---|
|  | 132 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) | 
|---|
|  | 133 | NoBonds += (*Runner)->BondDegree; | 
|---|
|  | 134 | return NoBonds; | 
|---|
|  | 135 | }; | 
|---|
|  | 136 |  | 
|---|
| [b453f9] | 137 | /** Output of a single atom with given numbering. | 
|---|
| [14de469] | 138 | * \param ElementNo cardinal number of the element | 
|---|
|  | 139 | * \param AtomNo cardinal number among these atoms of the same element | 
|---|
|  | 140 | * \param *out stream to output to | 
|---|
| [1907a7] | 141 | * \param *comment commentary after '#' sign | 
|---|
| [e41951] | 142 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| [14de469] | 143 | */ | 
|---|
| [e138de] | 144 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const | 
|---|
| [14de469] | 145 | { | 
|---|
|  | 146 | if (out != NULL) { | 
|---|
|  | 147 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [d74077] | 148 | *out << at(0) << "\t" << at(1) << "\t" << at(2); | 
|---|
| [943d02] | 149 | *out << "\t" << FixedIon; | 
|---|
| [d74077] | 150 | if (AtomicVelocity.Norm() > MYEPSILON) | 
|---|
|  | 151 | *out << "\t" << scientific << setprecision(6) << AtomicVelocity[0] << "\t" << AtomicVelocity[1] << "\t" << AtomicVelocity[2] << "\t"; | 
|---|
| [437922] | 152 | if (comment != NULL) | 
|---|
|  | 153 | *out << " # " << comment << endl; | 
|---|
| [e9f8f9] | 154 | else | 
|---|
|  | 155 | *out << " # molecule nr " << nr << endl; | 
|---|
|  | 156 | return true; | 
|---|
|  | 157 | } else | 
|---|
|  | 158 | return false; | 
|---|
|  | 159 | }; | 
|---|
| [b453f9] | 160 |  | 
|---|
|  | 161 | /** Output of a single atom with numbering from array according to atom::type. | 
|---|
|  | 162 | * \param *ElementNo cardinal number of the element | 
|---|
|  | 163 | * \param *AtomNo cardinal number among these atoms of the same element | 
|---|
|  | 164 | * \param *out stream to output to | 
|---|
|  | 165 | * \param *comment commentary after '#' sign | 
|---|
|  | 166 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
|  | 167 | */ | 
|---|
| [0ba410] | 168 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const | 
|---|
| [e9f8f9] | 169 | { | 
|---|
| [d74077] | 170 | AtomNo[getType()->Z]++;  // increment number | 
|---|
| [e9f8f9] | 171 | if (out != NULL) { | 
|---|
| [8f4df1] | 172 | const element *elemental = getType(); | 
|---|
|  | 173 | cout << "Looking for atom with element " << *elemental << endl; | 
|---|
|  | 174 | ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration"); | 
|---|
|  | 175 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->Z] << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [d74077] | 176 | *out << at(0) << "\t" << at(1) << "\t" << at(2); | 
|---|
| [e9f8f9] | 177 | *out << "\t" << FixedIon; | 
|---|
| [d74077] | 178 | if (AtomicVelocity.Norm() > MYEPSILON) | 
|---|
|  | 179 | *out << "\t" << scientific << setprecision(6) << AtomicVelocity[0] << "\t" << AtomicVelocity[1] << "\t" << AtomicVelocity[2] << "\t"; | 
|---|
| [e9f8f9] | 180 | if (comment != NULL) | 
|---|
|  | 181 | *out << " # " << comment << endl; | 
|---|
| [437922] | 182 | else | 
|---|
|  | 183 | *out << " # molecule nr " << nr << endl; | 
|---|
| [14de469] | 184 | return true; | 
|---|
|  | 185 | } else | 
|---|
|  | 186 | return false; | 
|---|
|  | 187 | }; | 
|---|
|  | 188 |  | 
|---|
|  | 189 | /** Output of a single atom as one lin in xyz file. | 
|---|
|  | 190 | * \param *out stream to output to | 
|---|
| [e41951] | 191 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| [14de469] | 192 | */ | 
|---|
|  | 193 | bool atom::OutputXYZLine(ofstream *out) const | 
|---|
|  | 194 | { | 
|---|
|  | 195 | if (out != NULL) { | 
|---|
| [b5c53d] | 196 | *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl; | 
|---|
| [14de469] | 197 | return true; | 
|---|
|  | 198 | } else | 
|---|
|  | 199 | return false; | 
|---|
|  | 200 | }; | 
|---|
|  | 201 |  | 
|---|
| [fcd7b6] | 202 | /** Output of a single atom as one lin in xyz file. | 
|---|
|  | 203 | * \param *out stream to output to | 
|---|
| [e41951] | 204 | * \param *ElementNo array with ion type number in the config file this atom's element shall have | 
|---|
|  | 205 | * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically | 
|---|
|  | 206 | * \param step Trajectory time step to output | 
|---|
|  | 207 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| [fcd7b6] | 208 | */ | 
|---|
| [e138de] | 209 | bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const | 
|---|
| [fcd7b6] | 210 | { | 
|---|
| [d74077] | 211 | AtomNo[getType()->Z]++; | 
|---|
| [fcd7b6] | 212 | if (out != NULL) { | 
|---|
| [d74077] | 213 | *out << "Ion_Type" << ElementNo[getType()->Z] << "_" << AtomNo[getType()->Z] << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [0a4f7f] | 214 | *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2]; | 
|---|
| [fcd7b6] | 215 | *out << "\t" << FixedIon; | 
|---|
|  | 216 | if (Trajectory.U.at(step).Norm() > MYEPSILON) | 
|---|
| [0a4f7f] | 217 | *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t"; | 
|---|
| [fcd7b6] | 218 | if (Trajectory.F.at(step).Norm() > MYEPSILON) | 
|---|
| [0a4f7f] | 219 | *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t"; | 
|---|
| [fcd7b6] | 220 | *out << "\t# Number in molecule " << nr << endl; | 
|---|
|  | 221 | return true; | 
|---|
|  | 222 | } else | 
|---|
|  | 223 | return false; | 
|---|
|  | 224 | }; | 
|---|
|  | 225 |  | 
|---|
| [681a8a] | 226 | /** Output of a single atom as one lin in xyz file. | 
|---|
|  | 227 | * \param *out stream to output to | 
|---|
| [e41951] | 228 | * \param step Trajectory time step to output | 
|---|
|  | 229 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| [681a8a] | 230 | */ | 
|---|
| [e138de] | 231 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const | 
|---|
| [681a8a] | 232 | { | 
|---|
|  | 233 | if (out != NULL) { | 
|---|
| [b5c53d] | 234 | *out << getType()->getSymbol() << "\t"; | 
|---|
| [0a4f7f] | 235 | *out << Trajectory.R.at(step)[0] << "\t"; | 
|---|
|  | 236 | *out << Trajectory.R.at(step)[1] << "\t"; | 
|---|
|  | 237 | *out << Trajectory.R.at(step)[2] << endl; | 
|---|
| [681a8a] | 238 | return true; | 
|---|
|  | 239 | } else | 
|---|
|  | 240 | return false; | 
|---|
|  | 241 | }; | 
|---|
|  | 242 |  | 
|---|
| [4455f4] | 243 | /** Outputs the MPQC configuration line for this atom. | 
|---|
|  | 244 | * \param *out output stream | 
|---|
|  | 245 | * \param *center center of molecule subtracted from position | 
|---|
|  | 246 | * \param *AtomNo pointer to atom counter that is increased by one | 
|---|
|  | 247 | */ | 
|---|
| [1b2d30] | 248 | void atom::OutputMPQCLine(ostream * const out, const Vector *center, int *AtomNo = NULL) const | 
|---|
| [4455f4] | 249 | { | 
|---|
| [d74077] | 250 | Vector recentered(getPosition()); | 
|---|
|  | 251 | recentered -= *center; | 
|---|
| [b5c53d] | 252 | *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl; | 
|---|
| [4455f4] | 253 | if (AtomNo != NULL) | 
|---|
|  | 254 | *AtomNo++; | 
|---|
|  | 255 | }; | 
|---|
|  | 256 |  | 
|---|
|  | 257 | /** Compares the indices of \a this atom with a given \a ptr. | 
|---|
|  | 258 | * \param ptr atom to compare index against | 
|---|
|  | 259 | * \return true - this one's is smaller, false - not | 
|---|
|  | 260 | */ | 
|---|
| [b453f9] | 261 | bool atom::Compare(const atom &ptr) const | 
|---|
| [4455f4] | 262 | { | 
|---|
|  | 263 | if (nr < ptr.nr) | 
|---|
|  | 264 | return true; | 
|---|
|  | 265 | else | 
|---|
|  | 266 | return false; | 
|---|
|  | 267 | }; | 
|---|
|  | 268 |  | 
|---|
|  | 269 | /** Returns squared distance to a given vector. | 
|---|
|  | 270 | * \param origin vector to calculate distance to | 
|---|
|  | 271 | * \return distance squared | 
|---|
|  | 272 | */ | 
|---|
| [b453f9] | 273 | double atom::DistanceSquaredToVector(const Vector &origin) const | 
|---|
| [4455f4] | 274 | { | 
|---|
| [d74077] | 275 | return DistanceSquared(origin); | 
|---|
| [4455f4] | 276 | }; | 
|---|
|  | 277 |  | 
|---|
|  | 278 | /** Returns distance to a given vector. | 
|---|
|  | 279 | * \param origin vector to calculate distance to | 
|---|
|  | 280 | * \return distance | 
|---|
|  | 281 | */ | 
|---|
| [b453f9] | 282 | double atom::DistanceToVector(const Vector &origin) const | 
|---|
| [4455f4] | 283 | { | 
|---|
| [d74077] | 284 | return distance(origin); | 
|---|
| [4455f4] | 285 | }; | 
|---|
|  | 286 |  | 
|---|
|  | 287 | /** Initialises the component number array. | 
|---|
|  | 288 | * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1) | 
|---|
|  | 289 | */ | 
|---|
|  | 290 | void atom::InitComponentNr() | 
|---|
|  | 291 | { | 
|---|
|  | 292 | if (ComponentNr != NULL) | 
|---|
| [920c70] | 293 | delete[](ComponentNr); | 
|---|
|  | 294 | ComponentNr = new int[ListOfBonds.size()+1]; | 
|---|
| [4455f4] | 295 | for (int i=ListOfBonds.size()+1;i--;) | 
|---|
|  | 296 | ComponentNr[i] = -1; | 
|---|
|  | 297 | }; | 
|---|
|  | 298 |  | 
|---|
| [d74077] | 299 | std::ostream & atom::operator << (std::ostream &ost) const | 
|---|
|  | 300 | { | 
|---|
|  | 301 | ParticleInfo::operator<<(ost); | 
|---|
|  | 302 | ost << "," << getPosition(); | 
|---|
|  | 303 | return ost; | 
|---|
|  | 304 | } | 
|---|
|  | 305 |  | 
|---|
|  | 306 | std::ostream & operator << (std::ostream &ost, const atom &a) | 
|---|
|  | 307 | { | 
|---|
|  | 308 | a.ParticleInfo::operator<<(ost); | 
|---|
|  | 309 | ost << "," << a.getPosition(); | 
|---|
|  | 310 | return ost; | 
|---|
|  | 311 | } | 
|---|
| [4455f4] | 312 |  | 
|---|
|  | 313 | bool operator < (atom &a, atom &b) | 
|---|
|  | 314 | { | 
|---|
|  | 315 | return a.Compare(b); | 
|---|
|  | 316 | }; | 
|---|
|  | 317 |  | 
|---|
| [46d958] | 318 | World *atom::getWorld(){ | 
|---|
|  | 319 | return world; | 
|---|
|  | 320 | } | 
|---|
|  | 321 |  | 
|---|
|  | 322 | void atom::setWorld(World* _world){ | 
|---|
|  | 323 | world = _world; | 
|---|
|  | 324 | } | 
|---|
|  | 325 |  | 
|---|
| [88d586] | 326 | bool atom::changeId(atomId_t newId){ | 
|---|
|  | 327 | // first we move ourselves in the world | 
|---|
|  | 328 | // the world lets us know if that succeeded | 
|---|
|  | 329 | if(world->changeAtomId(id,newId,this)){ | 
|---|
|  | 330 | id = newId; | 
|---|
|  | 331 | return true; | 
|---|
|  | 332 | } | 
|---|
|  | 333 | else{ | 
|---|
|  | 334 | return false; | 
|---|
|  | 335 | } | 
|---|
|  | 336 | } | 
|---|
|  | 337 |  | 
|---|
|  | 338 | void atom::setId(atomId_t _id) { | 
|---|
| [46d958] | 339 | id=_id; | 
|---|
|  | 340 | } | 
|---|
|  | 341 |  | 
|---|
| [ad2b411] | 342 | atomId_t atom::getId() const { | 
|---|
| [46d958] | 343 | return id; | 
|---|
|  | 344 | } | 
|---|
|  | 345 |  | 
|---|
| [6cfa36] | 346 | void atom::setMolecule(molecule *_mol){ | 
|---|
|  | 347 | // take this atom from the old molecule | 
|---|
|  | 348 | removeFromMolecule(); | 
|---|
|  | 349 | mol = _mol; | 
|---|
|  | 350 | if(!mol->containsAtom(this)){ | 
|---|
| [dddbfe] | 351 | mol->insert(this); | 
|---|
| [6cfa36] | 352 | } | 
|---|
|  | 353 | } | 
|---|
|  | 354 |  | 
|---|
| [c084cc] | 355 | molecule* atom::getMolecule(){ | 
|---|
|  | 356 | return mol; | 
|---|
|  | 357 | } | 
|---|
|  | 358 |  | 
|---|
| [6cfa36] | 359 | void atom::removeFromMolecule(){ | 
|---|
|  | 360 | if(mol){ | 
|---|
|  | 361 | if(mol->containsAtom(this)){ | 
|---|
|  | 362 | mol->erase(this); | 
|---|
|  | 363 | } | 
|---|
|  | 364 | mol=0; | 
|---|
|  | 365 | } | 
|---|
|  | 366 | } | 
|---|
|  | 367 |  | 
|---|
|  | 368 |  | 
|---|
| [88d586] | 369 | atom* NewAtom(atomId_t _id){ | 
|---|
|  | 370 | atom * res =new atom(); | 
|---|
|  | 371 | res->setId(_id); | 
|---|
|  | 372 | return res; | 
|---|
| [46d958] | 373 | } | 
|---|
|  | 374 |  | 
|---|
| [88d586] | 375 | void DeleteAtom(atom* atom){ | 
|---|
| [46d958] | 376 | delete atom; | 
|---|
|  | 377 | } | 
|---|