| 1 | /** \file atom.cpp
 | 
|---|
| 2 |  *
 | 
|---|
| 3 |  * Function implementations for the class atom.
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  */
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 8 | 
 | 
|---|
| 9 | #include "atom.hpp"
 | 
|---|
| 10 | #include "bond.hpp"
 | 
|---|
| 11 | #include "config.hpp"
 | 
|---|
| 12 | #include "element.hpp"
 | 
|---|
| 13 | #include "lists.hpp"
 | 
|---|
| 14 | #include "parser.hpp"
 | 
|---|
| 15 | #include "vector.hpp"
 | 
|---|
| 16 | #include "World.hpp"
 | 
|---|
| 17 | #include "molecule.hpp"
 | 
|---|
| 18 | #include "Shapes/Shape.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include <iomanip>
 | 
|---|
| 21 | #include <iostream>
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /************************************* Functions for class atom *************************************/
 | 
|---|
| 24 | 
 | 
|---|
| 25 | 
 | 
|---|
| 26 | /** Constructor of class atom.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | atom::atom() :
 | 
|---|
| 29 |   father(this), sort(&nr), mol(0)
 | 
|---|
| 30 | {
 | 
|---|
| 31 |   node = &x;  // TesselPoint::x can only be referenced from here
 | 
|---|
| 32 | };
 | 
|---|
| 33 | 
 | 
|---|
| 34 | /** Constructor of class atom.
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | atom::atom(atom *pointer) :
 | 
|---|
| 37 |     ParticleInfo(pointer),father(pointer), sort(&nr)
 | 
|---|
| 38 | {
 | 
|---|
| 39 |   type = pointer->type;  // copy element of atom
 | 
|---|
| 40 |   x = pointer->x; // copy coordination
 | 
|---|
| 41 |   v = pointer->v; // copy velocity
 | 
|---|
| 42 |   FixedIon = pointer->FixedIon;
 | 
|---|
| 43 |   node = &x;
 | 
|---|
| 44 |   mol = 0;
 | 
|---|
| 45 | };
 | 
|---|
| 46 | 
 | 
|---|
| 47 | atom *atom::clone(){
 | 
|---|
| 48 |   atom *res = new atom(this);
 | 
|---|
| 49 |   res->father = this;
 | 
|---|
| 50 |   res->sort = &res->nr;
 | 
|---|
| 51 |   res->type = type;
 | 
|---|
| 52 |   res->x = this->x;
 | 
|---|
| 53 |   res->v = this->v;
 | 
|---|
| 54 |   res->FixedIon = FixedIon;
 | 
|---|
| 55 |   res->node = &x;
 | 
|---|
| 56 |   res->mol = 0;
 | 
|---|
| 57 |   World::getInstance().registerAtom(res);
 | 
|---|
| 58 |   return res;
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /** Destructor of class atom.
 | 
|---|
| 63 |  */
 | 
|---|
| 64 | atom::~atom()
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   removeFromMolecule();
 | 
|---|
| 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 |   }
 | 
|---|
| 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 | {
 | 
|---|
| 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 |   }
 | 
|---|
| 89 | };
 | 
|---|
| 90 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 106 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
 | 
|---|
| 107 | {
 | 
|---|
| 108 |   if ( ptr == father )
 | 
|---|
| 109 |     *res = this;
 | 
|---|
| 110 | };
 | 
|---|
| 111 | 
 | 
|---|
| 112 | bool atom::isFather(const atom *ptr){
 | 
|---|
| 113 |   return ptr==father;
 | 
|---|
| 114 | }
 | 
|---|
| 115 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 121 | bool atom::IsInShape(const Shape& shape) const
 | 
|---|
| 122 | {
 | 
|---|
| 123 |   return shape.isInside(*node);
 | 
|---|
| 124 | };
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /** Counts the number of bonds weighted by bond::BondDegree.
 | 
|---|
| 127 |  * \param bonds times bond::BondDegree
 | 
|---|
| 128 |  */
 | 
|---|
| 129 | int BondedParticle::CountBonds() const
 | 
|---|
| 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 | 
 | 
|---|
| 137 | /** Output of a single atom with given numbering.
 | 
|---|
| 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
 | 
|---|
| 141 |  * \param *comment commentary after '#' sign
 | 
|---|
| 142 |   * \return true - \a *out present, false - \a *out is NULL
 | 
|---|
| 143 |  */
 | 
|---|
| 144 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
 | 
|---|
| 145 | {
 | 
|---|
| 146 |   if (out != NULL) {
 | 
|---|
| 147 |     *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"  << fixed << setprecision(9) << showpoint;
 | 
|---|
| 148 |     *out << x[0] << "\t" << x[1] << "\t" << x[2];
 | 
|---|
| 149 |     *out << "\t" << FixedIon;
 | 
|---|
| 150 |     if (v.Norm() > MYEPSILON)
 | 
|---|
| 151 |       *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t";
 | 
|---|
| 152 |     if (comment != NULL)
 | 
|---|
| 153 |       *out << " # " << comment << endl;
 | 
|---|
| 154 |     else
 | 
|---|
| 155 |       *out << " # molecule nr " << nr << endl;
 | 
|---|
| 156 |     return true;
 | 
|---|
| 157 |   } else
 | 
|---|
| 158 |     return false;
 | 
|---|
| 159 | };
 | 
|---|
| 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 |  */
 | 
|---|
| 168 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
 | 
|---|
| 169 | {
 | 
|---|
| 170 |   AtomNo[type->Z]++;  // increment number
 | 
|---|
| 171 |   if (out != NULL) {
 | 
|---|
| 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;
 | 
|---|
| 175 |     *out << x[0] << "\t" << x[1] << "\t" << x[2];
 | 
|---|
| 176 |     *out << "\t" << FixedIon;
 | 
|---|
| 177 |     if (v.Norm() > MYEPSILON)
 | 
|---|
| 178 |       *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t";
 | 
|---|
| 179 |     if (comment != NULL)
 | 
|---|
| 180 |       *out << " # " << comment << endl;
 | 
|---|
| 181 |     else
 | 
|---|
| 182 |       *out << " # molecule nr " << nr << endl;
 | 
|---|
| 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
 | 
|---|
| 190 |   * \return true - \a *out present, false - \a *out is NULL
 | 
|---|
| 191 |  */
 | 
|---|
| 192 | bool atom::OutputXYZLine(ofstream *out) const
 | 
|---|
| 193 | {
 | 
|---|
| 194 |   if (out != NULL) {
 | 
|---|
| 195 |     *out << type->getSymbol() << "\t" << x[0] << "\t" << x[1] << "\t" << x[2] << "\t" << endl;
 | 
|---|
| 196 |     return true;
 | 
|---|
| 197 |   } else
 | 
|---|
| 198 |     return false;
 | 
|---|
| 199 | };
 | 
|---|
| 200 | 
 | 
|---|
| 201 | /** Output of a single atom as one lin in xyz file.
 | 
|---|
| 202 |  * \param *out stream to output to
 | 
|---|
| 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
 | 
|---|
| 207 |  */
 | 
|---|
| 208 | bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const
 | 
|---|
| 209 | {
 | 
|---|
| 210 |   AtomNo[type->Z]++;
 | 
|---|
| 211 |   if (out != NULL) {
 | 
|---|
| 212 |     *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t"  << fixed << setprecision(9) << showpoint;
 | 
|---|
| 213 |     *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2];
 | 
|---|
| 214 |     *out << "\t" << FixedIon;
 | 
|---|
| 215 |     if (Trajectory.U.at(step).Norm() > MYEPSILON)
 | 
|---|
| 216 |       *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t";
 | 
|---|
| 217 |     if (Trajectory.F.at(step).Norm() > MYEPSILON)
 | 
|---|
| 218 |       *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t";
 | 
|---|
| 219 |     *out << "\t# Number in molecule " << nr << endl;
 | 
|---|
| 220 |     return true;
 | 
|---|
| 221 |   } else
 | 
|---|
| 222 |     return false;
 | 
|---|
| 223 | };
 | 
|---|
| 224 | 
 | 
|---|
| 225 | /** Output of a single atom as one lin in xyz file.
 | 
|---|
| 226 |  * \param *out stream to output to
 | 
|---|
| 227 |  * \param step Trajectory time step to output
 | 
|---|
| 228 |  * \return true - \a *out present, false - \a *out is NULL
 | 
|---|
| 229 |  */
 | 
|---|
| 230 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
 | 
|---|
| 231 | {
 | 
|---|
| 232 |   if (out != NULL) {
 | 
|---|
| 233 |     *out << type->getSymbol() << "\t";
 | 
|---|
| 234 |     *out << Trajectory.R.at(step)[0] << "\t";
 | 
|---|
| 235 |     *out << Trajectory.R.at(step)[1] << "\t";
 | 
|---|
| 236 |     *out << Trajectory.R.at(step)[2] << endl;
 | 
|---|
| 237 |     return true;
 | 
|---|
| 238 |   } else
 | 
|---|
| 239 |     return false;
 | 
|---|
| 240 | };
 | 
|---|
| 241 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 247 | void atom::OutputMPQCLine(ostream * const out, const Vector *center, int *AtomNo = NULL) const
 | 
|---|
| 248 | {
 | 
|---|
| 249 |   *out << "\t\t" << type->getSymbol() << " [ " << x[0]-center->at(0) << "\t" << x[1]-center->at(1) << "\t" << x[2]-center->at(2) << " ]" << endl;
 | 
|---|
| 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 |  */
 | 
|---|
| 258 | bool atom::Compare(const atom &ptr) const
 | 
|---|
| 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 |  */
 | 
|---|
| 270 | double atom::DistanceSquaredToVector(const Vector &origin) const
 | 
|---|
| 271 | {
 | 
|---|
| 272 |   return origin.DistanceSquared(x);
 | 
|---|
| 273 | };
 | 
|---|
| 274 | 
 | 
|---|
| 275 | /** Returns distance to a given vector.
 | 
|---|
| 276 |  * \param origin vector to calculate distance to
 | 
|---|
| 277 |  * \return distance
 | 
|---|
| 278 |  */
 | 
|---|
| 279 | double atom::DistanceToVector(const Vector &origin) const
 | 
|---|
| 280 | {
 | 
|---|
| 281 |   return origin.distance(x);
 | 
|---|
| 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)
 | 
|---|
| 290 |     delete[](ComponentNr);
 | 
|---|
| 291 |   ComponentNr = new int[ListOfBonds.size()+1];
 | 
|---|
| 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 | 
 | 
|---|
| 302 | World *atom::getWorld(){
 | 
|---|
| 303 |   return world;
 | 
|---|
| 304 | }
 | 
|---|
| 305 | 
 | 
|---|
| 306 | void atom::setWorld(World* _world){
 | 
|---|
| 307 |   world = _world;
 | 
|---|
| 308 | }
 | 
|---|
| 309 | 
 | 
|---|
| 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) {
 | 
|---|
| 323 |   id=_id;
 | 
|---|
| 324 | }
 | 
|---|
| 325 | 
 | 
|---|
| 326 | atomId_t atom::getId() const {
 | 
|---|
| 327 |   return id;
 | 
|---|
| 328 | }
 | 
|---|
| 329 | 
 | 
|---|
| 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 | 
 | 
|---|
| 339 | molecule* atom::getMolecule(){
 | 
|---|
| 340 |   return mol;
 | 
|---|
| 341 | }
 | 
|---|
| 342 | 
 | 
|---|
| 343 | void atom::removeFromMolecule(){
 | 
|---|
| 344 |   if(mol){
 | 
|---|
| 345 |     if(mol->containsAtom(this)){
 | 
|---|
| 346 |       mol->erase(this);
 | 
|---|
| 347 |     }
 | 
|---|
| 348 |     mol=0;
 | 
|---|
| 349 |   }
 | 
|---|
| 350 | }
 | 
|---|
| 351 | 
 | 
|---|
| 352 | 
 | 
|---|
| 353 | atom* NewAtom(atomId_t _id){
 | 
|---|
| 354 |   atom * res =new atom();
 | 
|---|
| 355 |   res->setId(_id);
 | 
|---|
| 356 |   return res;
 | 
|---|
| 357 | }
 | 
|---|
| 358 | 
 | 
|---|
| 359 | void DeleteAtom(atom* atom){
 | 
|---|
| 360 |   delete atom;
 | 
|---|
| 361 | }
 | 
|---|