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