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