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