| 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 | 
 | 
|---|
| 22 | /************************************* Functions for class atom *************************************/
 | 
|---|
| 23 | 
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /** Constructor of class atom.
 | 
|---|
| 26 |  */
 | 
|---|
| 27 | atom::atom() :
 | 
|---|
| 28 |   father(this), sort(&nr), mol(0)
 | 
|---|
| 29 | {
 | 
|---|
| 30 |   node = &x;  // TesselPoint::x can only be referenced from here
 | 
|---|
| 31 | };
 | 
|---|
| 32 | 
 | 
|---|
| 33 | /** Constructor of class atom.
 | 
|---|
| 34 |  */
 | 
|---|
| 35 | atom::atom(atom *pointer) :
 | 
|---|
| 36 |     ParticleInfo(pointer),father(pointer), sort(&nr)
 | 
|---|
| 37 | {
 | 
|---|
| 38 |   type = pointer->type;  // copy element of atom
 | 
|---|
| 39 |   x = pointer->x; // copy coordination
 | 
|---|
| 40 |   v = pointer->v; // copy velocity
 | 
|---|
| 41 |   FixedIon = pointer->FixedIon;
 | 
|---|
| 42 |   node = &x;
 | 
|---|
| 43 |   mol = 0;
 | 
|---|
| 44 | };
 | 
|---|
| 45 | 
 | 
|---|
| 46 | atom *atom::clone(){
 | 
|---|
| 47 |   atom *res = new atom(this);
 | 
|---|
| 48 |   res->father = this;
 | 
|---|
| 49 |   res->sort = &res->nr;
 | 
|---|
| 50 |   res->type = type;
 | 
|---|
| 51 |   res->x = this->x;
 | 
|---|
| 52 |   res->v = this->v;
 | 
|---|
| 53 |   res->FixedIon = FixedIon;
 | 
|---|
| 54 |   res->node = &x;
 | 
|---|
| 55 |   res->mol = 0;
 | 
|---|
| 56 |   World::getInstance().registerAtom(res);
 | 
|---|
| 57 |   return res;
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | 
 | 
|---|
| 61 | /** Destructor of class atom.
 | 
|---|
| 62 |  */
 | 
|---|
| 63 | atom::~atom()
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   removeFromMolecule();
 | 
|---|
| 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 |   }
 | 
|---|
| 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 | {
 | 
|---|
| 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 |   }
 | 
|---|
| 88 | };
 | 
|---|
| 89 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 105 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
 | 
|---|
| 106 | {
 | 
|---|
| 107 |   if ( ptr == father )
 | 
|---|
| 108 |     *res = this;
 | 
|---|
| 109 | };
 | 
|---|
| 110 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 116 | bool atom::IsInShape(const Shape& shape) const
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   return shape.isInside(*node);
 | 
|---|
| 119 | };
 | 
|---|
| 120 | 
 | 
|---|
| 121 | /** Counts the number of bonds weighted by bond::BondDegree.
 | 
|---|
| 122 |  * \param bonds times bond::BondDegree
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | int BondedParticle::CountBonds() const
 | 
|---|
| 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 | 
 | 
|---|
| 132 | /** Output of a single atom with given numbering.
 | 
|---|
| 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
 | 
|---|
| 136 |  * \param *comment commentary after '#' sign
 | 
|---|
| 137 |   * \return true - \a *out present, false - \a *out is NULL
 | 
|---|
| 138 |  */
 | 
|---|
| 139 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
 | 
|---|
| 140 | {
 | 
|---|
| 141 |   if (out != NULL) {
 | 
|---|
| 142 |     *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"  << fixed << setprecision(9) << showpoint;
 | 
|---|
| 143 |     *out << x[0] << "\t" << x[1] << "\t" << x[2];
 | 
|---|
| 144 |     *out << "\t" << FixedIon;
 | 
|---|
| 145 |     if (v.Norm() > MYEPSILON)
 | 
|---|
| 146 |       *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t";
 | 
|---|
| 147 |     if (comment != NULL)
 | 
|---|
| 148 |       *out << " # " << comment << endl;
 | 
|---|
| 149 |     else
 | 
|---|
| 150 |       *out << " # molecule nr " << nr << endl;
 | 
|---|
| 151 |     return true;
 | 
|---|
| 152 |   } else
 | 
|---|
| 153 |     return false;
 | 
|---|
| 154 | };
 | 
|---|
| 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 |  */
 | 
|---|
| 163 | bool atom::OutputArrayIndexed(ostream * const out, const int *ElementNo, int *AtomNo, const char *comment) const
 | 
|---|
| 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;
 | 
|---|
| 168 |     *out << x[0] << "\t" << x[1] << "\t" << x[2];
 | 
|---|
| 169 |     *out << "\t" << FixedIon;
 | 
|---|
| 170 |     if (v.Norm() > MYEPSILON)
 | 
|---|
| 171 |       *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t";
 | 
|---|
| 172 |     if (comment != NULL)
 | 
|---|
| 173 |       *out << " # " << comment << endl;
 | 
|---|
| 174 |     else
 | 
|---|
| 175 |       *out << " # molecule nr " << nr << endl;
 | 
|---|
| 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
 | 
|---|
| 183 |   * \return true - \a *out present, false - \a *out is NULL
 | 
|---|
| 184 |  */
 | 
|---|
| 185 | bool atom::OutputXYZLine(ofstream *out) const
 | 
|---|
| 186 | {
 | 
|---|
| 187 |   if (out != NULL) {
 | 
|---|
| 188 |     *out << type->symbol << "\t" << x[0] << "\t" << x[1] << "\t" << x[2] << "\t" << endl;
 | 
|---|
| 189 |     return true;
 | 
|---|
| 190 |   } else
 | 
|---|
| 191 |     return false;
 | 
|---|
| 192 | };
 | 
|---|
| 193 | 
 | 
|---|
| 194 | /** Output of a single atom as one lin in xyz file.
 | 
|---|
| 195 |  * \param *out stream to output to
 | 
|---|
| 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
 | 
|---|
| 200 |  */
 | 
|---|
| 201 | bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const
 | 
|---|
| 202 | {
 | 
|---|
| 203 |   AtomNo[type->Z]++;
 | 
|---|
| 204 |   if (out != NULL) {
 | 
|---|
| 205 |     *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t"  << fixed << setprecision(9) << showpoint;
 | 
|---|
| 206 |     *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2];
 | 
|---|
| 207 |     *out << "\t" << FixedIon;
 | 
|---|
| 208 |     if (Trajectory.U.at(step).Norm() > MYEPSILON)
 | 
|---|
| 209 |       *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t";
 | 
|---|
| 210 |     if (Trajectory.F.at(step).Norm() > MYEPSILON)
 | 
|---|
| 211 |       *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t";
 | 
|---|
| 212 |     *out << "\t# Number in molecule " << nr << endl;
 | 
|---|
| 213 |     return true;
 | 
|---|
| 214 |   } else
 | 
|---|
| 215 |     return false;
 | 
|---|
| 216 | };
 | 
|---|
| 217 | 
 | 
|---|
| 218 | /** Output of a single atom as one lin in xyz file.
 | 
|---|
| 219 |  * \param *out stream to output to
 | 
|---|
| 220 |  * \param step Trajectory time step to output
 | 
|---|
| 221 |  * \return true - \a *out present, false - \a *out is NULL
 | 
|---|
| 222 |  */
 | 
|---|
| 223 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
 | 
|---|
| 224 | {
 | 
|---|
| 225 |   if (out != NULL) {
 | 
|---|
| 226 |     *out << type->symbol << "\t";
 | 
|---|
| 227 |     *out << Trajectory.R.at(step)[0] << "\t";
 | 
|---|
| 228 |     *out << Trajectory.R.at(step)[1] << "\t";
 | 
|---|
| 229 |     *out << Trajectory.R.at(step)[2] << endl;
 | 
|---|
| 230 |     return true;
 | 
|---|
| 231 |   } else
 | 
|---|
| 232 |     return false;
 | 
|---|
| 233 | };
 | 
|---|
| 234 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 240 | void atom::OutputMPQCLine(ostream * const out, const Vector *center, int *AtomNo = NULL) const
 | 
|---|
| 241 | {
 | 
|---|
| 242 |   *out << "\t\t" << type->symbol << " [ " << x[0]-center->at(0) << "\t" << x[1]-center->at(1) << "\t" << x[2]-center->at(2) << " ]" << endl;
 | 
|---|
| 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 |  */
 | 
|---|
| 251 | bool atom::Compare(const atom &ptr) const
 | 
|---|
| 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 |  */
 | 
|---|
| 263 | double atom::DistanceSquaredToVector(const Vector &origin) const
 | 
|---|
| 264 | {
 | 
|---|
| 265 |   return origin.DistanceSquared(x);
 | 
|---|
| 266 | };
 | 
|---|
| 267 | 
 | 
|---|
| 268 | /** Returns distance to a given vector.
 | 
|---|
| 269 |  * \param origin vector to calculate distance to
 | 
|---|
| 270 |  * \return distance
 | 
|---|
| 271 |  */
 | 
|---|
| 272 | double atom::DistanceToVector(const Vector &origin) const
 | 
|---|
| 273 | {
 | 
|---|
| 274 |   return origin.distance(x);
 | 
|---|
| 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)
 | 
|---|
| 283 |     delete[](ComponentNr);
 | 
|---|
| 284 |   ComponentNr = new int[ListOfBonds.size()+1];
 | 
|---|
| 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 | 
 | 
|---|
| 295 | World *atom::getWorld(){
 | 
|---|
| 296 |   return world;
 | 
|---|
| 297 | }
 | 
|---|
| 298 | 
 | 
|---|
| 299 | void atom::setWorld(World* _world){
 | 
|---|
| 300 |   world = _world;
 | 
|---|
| 301 | }
 | 
|---|
| 302 | 
 | 
|---|
| 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) {
 | 
|---|
| 316 |   id=_id;
 | 
|---|
| 317 | }
 | 
|---|
| 318 | 
 | 
|---|
| 319 | atomId_t atom::getId() const {
 | 
|---|
| 320 |   return id;
 | 
|---|
| 321 | }
 | 
|---|
| 322 | 
 | 
|---|
| 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 | 
 | 
|---|
| 342 | atom* NewAtom(atomId_t _id){
 | 
|---|
| 343 |   atom * res =new atom();
 | 
|---|
| 344 |   res->setId(_id);
 | 
|---|
| 345 |   return res;
 | 
|---|
| 346 | }
 | 
|---|
| 347 | 
 | 
|---|
| 348 | void DeleteAtom(atom* atom){
 | 
|---|
| 349 |   delete atom;
 | 
|---|
| 350 | }
 | 
|---|