| [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() : | 
|---|
| [6cfa36] | 29 | father(this), sort(&nr), mol(0) | 
|---|
| [14de469] | 30 | { | 
|---|
| [70ff32] | 31 | node = &x;  // TesselPoint::x can only be referenced from here | 
|---|
| [14de469] | 32 | }; | 
|---|
|  | 33 |  | 
|---|
| [2319ed] | 34 | /** Constructor of class atom. | 
|---|
|  | 35 | */ | 
|---|
| [46d958] | 36 | atom::atom(atom *pointer) : | 
|---|
| [6cfa36] | 37 | ParticleInfo(pointer),father(pointer), sort(&nr) | 
|---|
| [2319ed] | 38 | { | 
|---|
|  | 39 | type = pointer->type;  // copy element of atom | 
|---|
| [273382] | 40 | x = pointer->x; // copy coordination | 
|---|
|  | 41 | v = pointer->v; // copy velocity | 
|---|
| [2319ed] | 42 | FixedIon = pointer->FixedIon; | 
|---|
| [89c8b2] | 43 | node = &x; | 
|---|
| [6cfa36] | 44 | mol = 0; | 
|---|
| [b453f9] | 45 | }; | 
|---|
| [2319ed] | 46 |  | 
|---|
| [46d958] | 47 | atom *atom::clone(){ | 
|---|
| [68f03d] | 48 | atom *res = new atom(this); | 
|---|
| [46d958] | 49 | res->father = this; | 
|---|
| [5f612ee] | 50 | res->sort = &res->nr; | 
|---|
| [46d958] | 51 | res->type = type; | 
|---|
| [273382] | 52 | res->x = this->x; | 
|---|
|  | 53 | res->v = this->v; | 
|---|
| [46d958] | 54 | res->FixedIon = FixedIon; | 
|---|
|  | 55 | res->node = &x; | 
|---|
| [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 | { | 
|---|
| [c550dd] | 123 | return shape.isInside(*node); | 
|---|
| [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; | 
|---|
| [0a4f7f] | 148 | *out << x[0] << "\t" << x[1] << "\t" << x[2]; | 
|---|
| [943d02] | 149 | *out << "\t" << FixedIon; | 
|---|
|  | 150 | if (v.Norm() > MYEPSILON) | 
|---|
| [0a4f7f] | 151 | *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[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 | { | 
|---|
|  | 170 | AtomNo[type->Z]++;  // increment number | 
|---|
|  | 171 | if (out != NULL) { | 
|---|
| [0ba410] | 172 | cout << "Looking for atom with element " << *type << endl; | 
|---|
|  | 173 | ASSERT(elementLookup.there.find(type)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration"); | 
|---|
|  | 174 | *out << "Ion_Type" << elementLookup.there.find(type)->second << "_" << AtomNo[type->Z] << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [0a4f7f] | 175 | *out << x[0] << "\t" << x[1] << "\t" << x[2]; | 
|---|
| [e9f8f9] | 176 | *out << "\t" << FixedIon; | 
|---|
|  | 177 | if (v.Norm() > MYEPSILON) | 
|---|
| [0a4f7f] | 178 | *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t"; | 
|---|
| [e9f8f9] | 179 | if (comment != NULL) | 
|---|
|  | 180 | *out << " # " << comment << endl; | 
|---|
| [437922] | 181 | else | 
|---|
|  | 182 | *out << " # molecule nr " << nr << endl; | 
|---|
| [14de469] | 183 | return true; | 
|---|
|  | 184 | } else | 
|---|
|  | 185 | return false; | 
|---|
|  | 186 | }; | 
|---|
|  | 187 |  | 
|---|
|  | 188 | /** Output of a single atom as one lin in xyz file. | 
|---|
|  | 189 | * \param *out stream to output to | 
|---|
| [e41951] | 190 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| [14de469] | 191 | */ | 
|---|
|  | 192 | bool atom::OutputXYZLine(ofstream *out) const | 
|---|
|  | 193 | { | 
|---|
|  | 194 | if (out != NULL) { | 
|---|
| [0a4f7f] | 195 | *out << type->symbol << "\t" << x[0] << "\t" << x[1] << "\t" << x[2] << "\t" << endl; | 
|---|
| [14de469] | 196 | return true; | 
|---|
|  | 197 | } else | 
|---|
|  | 198 | return false; | 
|---|
|  | 199 | }; | 
|---|
|  | 200 |  | 
|---|
| [fcd7b6] | 201 | /** Output of a single atom as one lin in xyz file. | 
|---|
|  | 202 | * \param *out stream to output to | 
|---|
| [e41951] | 203 | * \param *ElementNo array with ion type number in the config file this atom's element shall have | 
|---|
|  | 204 | * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically | 
|---|
|  | 205 | * \param step Trajectory time step to output | 
|---|
|  | 206 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| [fcd7b6] | 207 | */ | 
|---|
| [e138de] | 208 | bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const | 
|---|
| [fcd7b6] | 209 | { | 
|---|
|  | 210 | AtomNo[type->Z]++; | 
|---|
|  | 211 | if (out != NULL) { | 
|---|
|  | 212 | *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| [0a4f7f] | 213 | *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2]; | 
|---|
| [fcd7b6] | 214 | *out << "\t" << FixedIon; | 
|---|
|  | 215 | if (Trajectory.U.at(step).Norm() > MYEPSILON) | 
|---|
| [0a4f7f] | 216 | *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t"; | 
|---|
| [fcd7b6] | 217 | if (Trajectory.F.at(step).Norm() > MYEPSILON) | 
|---|
| [0a4f7f] | 218 | *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t"; | 
|---|
| [fcd7b6] | 219 | *out << "\t# Number in molecule " << nr << endl; | 
|---|
|  | 220 | return true; | 
|---|
|  | 221 | } else | 
|---|
|  | 222 | return false; | 
|---|
|  | 223 | }; | 
|---|
|  | 224 |  | 
|---|
| [681a8a] | 225 | /** Output of a single atom as one lin in xyz file. | 
|---|
|  | 226 | * \param *out stream to output to | 
|---|
| [e41951] | 227 | * \param step Trajectory time step to output | 
|---|
|  | 228 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| [681a8a] | 229 | */ | 
|---|
| [e138de] | 230 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const | 
|---|
| [681a8a] | 231 | { | 
|---|
|  | 232 | if (out != NULL) { | 
|---|
|  | 233 | *out << type->symbol << "\t"; | 
|---|
| [0a4f7f] | 234 | *out << Trajectory.R.at(step)[0] << "\t"; | 
|---|
|  | 235 | *out << Trajectory.R.at(step)[1] << "\t"; | 
|---|
|  | 236 | *out << Trajectory.R.at(step)[2] << endl; | 
|---|
| [681a8a] | 237 | return true; | 
|---|
|  | 238 | } else | 
|---|
|  | 239 | return false; | 
|---|
|  | 240 | }; | 
|---|
|  | 241 |  | 
|---|
| [4455f4] | 242 | /** Outputs the MPQC configuration line for this atom. | 
|---|
|  | 243 | * \param *out output stream | 
|---|
|  | 244 | * \param *center center of molecule subtracted from position | 
|---|
|  | 245 | * \param *AtomNo pointer to atom counter that is increased by one | 
|---|
|  | 246 | */ | 
|---|
| [1b2d30] | 247 | void atom::OutputMPQCLine(ostream * const out, const Vector *center, int *AtomNo = NULL) const | 
|---|
| [4455f4] | 248 | { | 
|---|
| [0a4f7f] | 249 | *out << "\t\t" << type->symbol << " [ " << x[0]-center->at(0) << "\t" << x[1]-center->at(1) << "\t" << x[2]-center->at(2) << " ]" << endl; | 
|---|
| [4455f4] | 250 | if (AtomNo != NULL) | 
|---|
|  | 251 | *AtomNo++; | 
|---|
|  | 252 | }; | 
|---|
|  | 253 |  | 
|---|
|  | 254 | /** Compares the indices of \a this atom with a given \a ptr. | 
|---|
|  | 255 | * \param ptr atom to compare index against | 
|---|
|  | 256 | * \return true - this one's is smaller, false - not | 
|---|
|  | 257 | */ | 
|---|
| [b453f9] | 258 | bool atom::Compare(const atom &ptr) const | 
|---|
| [4455f4] | 259 | { | 
|---|
|  | 260 | if (nr < ptr.nr) | 
|---|
|  | 261 | return true; | 
|---|
|  | 262 | else | 
|---|
|  | 263 | return false; | 
|---|
|  | 264 | }; | 
|---|
|  | 265 |  | 
|---|
|  | 266 | /** Returns squared distance to a given vector. | 
|---|
|  | 267 | * \param origin vector to calculate distance to | 
|---|
|  | 268 | * \return distance squared | 
|---|
|  | 269 | */ | 
|---|
| [b453f9] | 270 | double atom::DistanceSquaredToVector(const Vector &origin) const | 
|---|
| [4455f4] | 271 | { | 
|---|
| [273382] | 272 | return origin.DistanceSquared(x); | 
|---|
| [4455f4] | 273 | }; | 
|---|
|  | 274 |  | 
|---|
|  | 275 | /** Returns distance to a given vector. | 
|---|
|  | 276 | * \param origin vector to calculate distance to | 
|---|
|  | 277 | * \return distance | 
|---|
|  | 278 | */ | 
|---|
| [b453f9] | 279 | double atom::DistanceToVector(const Vector &origin) const | 
|---|
| [4455f4] | 280 | { | 
|---|
| [1513a74] | 281 | return origin.distance(x); | 
|---|
| [4455f4] | 282 | }; | 
|---|
|  | 283 |  | 
|---|
|  | 284 | /** Initialises the component number array. | 
|---|
|  | 285 | * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1) | 
|---|
|  | 286 | */ | 
|---|
|  | 287 | void atom::InitComponentNr() | 
|---|
|  | 288 | { | 
|---|
|  | 289 | if (ComponentNr != NULL) | 
|---|
| [920c70] | 290 | delete[](ComponentNr); | 
|---|
|  | 291 | ComponentNr = new int[ListOfBonds.size()+1]; | 
|---|
| [4455f4] | 292 | for (int i=ListOfBonds.size()+1;i--;) | 
|---|
|  | 293 | ComponentNr[i] = -1; | 
|---|
|  | 294 | }; | 
|---|
|  | 295 |  | 
|---|
|  | 296 |  | 
|---|
|  | 297 | bool operator < (atom &a, atom &b) | 
|---|
|  | 298 | { | 
|---|
|  | 299 | return a.Compare(b); | 
|---|
|  | 300 | }; | 
|---|
|  | 301 |  | 
|---|
| [46d958] | 302 | World *atom::getWorld(){ | 
|---|
|  | 303 | return world; | 
|---|
|  | 304 | } | 
|---|
|  | 305 |  | 
|---|
|  | 306 | void atom::setWorld(World* _world){ | 
|---|
|  | 307 | world = _world; | 
|---|
|  | 308 | } | 
|---|
|  | 309 |  | 
|---|
| [88d586] | 310 | bool atom::changeId(atomId_t newId){ | 
|---|
|  | 311 | // first we move ourselves in the world | 
|---|
|  | 312 | // the world lets us know if that succeeded | 
|---|
|  | 313 | if(world->changeAtomId(id,newId,this)){ | 
|---|
|  | 314 | id = newId; | 
|---|
|  | 315 | return true; | 
|---|
|  | 316 | } | 
|---|
|  | 317 | else{ | 
|---|
|  | 318 | return false; | 
|---|
|  | 319 | } | 
|---|
|  | 320 | } | 
|---|
|  | 321 |  | 
|---|
|  | 322 | void atom::setId(atomId_t _id) { | 
|---|
| [46d958] | 323 | id=_id; | 
|---|
|  | 324 | } | 
|---|
|  | 325 |  | 
|---|
| [ad2b411] | 326 | atomId_t atom::getId() const { | 
|---|
| [46d958] | 327 | return id; | 
|---|
|  | 328 | } | 
|---|
|  | 329 |  | 
|---|
| [6cfa36] | 330 | void atom::setMolecule(molecule *_mol){ | 
|---|
|  | 331 | // take this atom from the old molecule | 
|---|
|  | 332 | removeFromMolecule(); | 
|---|
|  | 333 | mol = _mol; | 
|---|
|  | 334 | if(!mol->containsAtom(this)){ | 
|---|
|  | 335 | mol->AddAtom(this); | 
|---|
|  | 336 | } | 
|---|
|  | 337 | } | 
|---|
|  | 338 |  | 
|---|
| [c084cc] | 339 | molecule* atom::getMolecule(){ | 
|---|
|  | 340 | return mol; | 
|---|
|  | 341 | } | 
|---|
|  | 342 |  | 
|---|
| [6cfa36] | 343 | void atom::removeFromMolecule(){ | 
|---|
|  | 344 | if(mol){ | 
|---|
|  | 345 | if(mol->containsAtom(this)){ | 
|---|
|  | 346 | mol->erase(this); | 
|---|
|  | 347 | } | 
|---|
|  | 348 | mol=0; | 
|---|
|  | 349 | } | 
|---|
|  | 350 | } | 
|---|
|  | 351 |  | 
|---|
|  | 352 |  | 
|---|
| [88d586] | 353 | atom* NewAtom(atomId_t _id){ | 
|---|
|  | 354 | atom * res =new atom(); | 
|---|
|  | 355 | res->setId(_id); | 
|---|
|  | 356 | return res; | 
|---|
| [46d958] | 357 | } | 
|---|
|  | 358 |  | 
|---|
| [88d586] | 359 | void DeleteAtom(atom* atom){ | 
|---|
| [46d958] | 360 | delete atom; | 
|---|
|  | 361 | } | 
|---|