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