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