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