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