| 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 "element.hpp"
|
|---|
| 10 | #include "memoryallocator.hpp"
|
|---|
| 11 | #include "parser.hpp"
|
|---|
| 12 | #include "vector.hpp"
|
|---|
| 13 |
|
|---|
| 14 | /************************************* Functions for class atom *************************************/
|
|---|
| 15 |
|
|---|
| 16 | /** Constructor of class atom.
|
|---|
| 17 | */
|
|---|
| 18 | atom::atom()
|
|---|
| 19 | {
|
|---|
| 20 | father = this; // generally, father is itself
|
|---|
| 21 | previous = NULL;
|
|---|
| 22 | next = NULL;
|
|---|
| 23 | Ancestor = NULL;
|
|---|
| 24 | type = NULL;
|
|---|
| 25 | sort = NULL;
|
|---|
| 26 | FixedIon = 0;
|
|---|
| 27 | GraphNr = -1;
|
|---|
| 28 | ComponentNr = NULL;
|
|---|
| 29 | IsCyclic = false;
|
|---|
| 30 | SeparationVertex = false;
|
|---|
| 31 | LowpointNr = -1;
|
|---|
| 32 | AdaptiveOrder = 0;
|
|---|
| 33 | MaxOrder = false;
|
|---|
| 34 | // set LCNode::Vector to our Vector
|
|---|
| 35 | node = &x;
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | /** Constructor of class atom.
|
|---|
| 39 | */
|
|---|
| 40 | atom::atom(atom *pointer)
|
|---|
| 41 | {
|
|---|
| 42 | Name = NULL;
|
|---|
| 43 | previous = NULL;
|
|---|
| 44 | next = NULL;
|
|---|
| 45 | father = pointer; // generally, father is itself
|
|---|
| 46 | Ancestor = NULL;
|
|---|
| 47 | GraphNr = -1;
|
|---|
| 48 | ComponentNr = NULL;
|
|---|
| 49 | IsCyclic = false;
|
|---|
| 50 | SeparationVertex = false;
|
|---|
| 51 | LowpointNr = -1;
|
|---|
| 52 | AdaptiveOrder = 0;
|
|---|
| 53 | MaxOrder = false;
|
|---|
| 54 | type = pointer->type; // copy element of atom
|
|---|
| 55 | x.CopyVector(&pointer->x); // copy coordination
|
|---|
| 56 | v.CopyVector(&pointer->v); // copy velocity
|
|---|
| 57 | FixedIon = pointer->FixedIon;
|
|---|
| 58 | nr = -1;
|
|---|
| 59 | sort = &nr;
|
|---|
| 60 | node = &x;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | /** Destructor of class atom.
|
|---|
| 65 | */
|
|---|
| 66 | atom::~atom()
|
|---|
| 67 | {
|
|---|
| 68 | Free<int>(&ComponentNr, "atom::~atom: *ComponentNr");
|
|---|
| 69 | Free<char>(&Name, "atom::~atom: *Name");
|
|---|
| 70 | Trajectory.R.clear();
|
|---|
| 71 | Trajectory.U.clear();
|
|---|
| 72 | Trajectory.F.clear();
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | /** Climbs up the father list until NULL, last is returned.
|
|---|
| 77 | * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen)
|
|---|
| 78 | */
|
|---|
| 79 | atom *atom::GetTrueFather()
|
|---|
| 80 | {
|
|---|
| 81 | atom *walker = this;
|
|---|
| 82 | do {
|
|---|
| 83 | if (walker == walker->father) // top most father is the one that points on itself
|
|---|
| 84 | break;
|
|---|
| 85 | walker = walker->father;
|
|---|
| 86 | } while (walker != NULL);
|
|---|
| 87 | return walker;
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | /** Sets father to itself or its father in case of copying a molecule.
|
|---|
| 91 | */
|
|---|
| 92 | void atom::CorrectFather()
|
|---|
| 93 | {
|
|---|
| 94 | if (father->father == father) // same atom in copy's father points to itself
|
|---|
| 95 | father = this; // set father to itself (copy of a whole molecule)
|
|---|
| 96 | else
|
|---|
| 97 | father = father->father; // set father to original's father
|
|---|
| 98 |
|
|---|
| 99 | };
|
|---|
| 100 |
|
|---|
| 101 | /** Check whether father is equal to given atom.
|
|---|
| 102 | * \param *ptr atom to compare father to
|
|---|
| 103 | * \param **res return value (only set if atom::father is equal to \a *ptr)
|
|---|
| 104 | */
|
|---|
| 105 | void atom::EqualsFather ( atom *ptr, atom **res )
|
|---|
| 106 | {
|
|---|
| 107 | if ( ptr == father )
|
|---|
| 108 | *res = this;
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | /** Checks whether atom is within the given box.
|
|---|
| 112 | * \param offset offset to box origin
|
|---|
| 113 | * \param *parallelepiped box matrix
|
|---|
| 114 | * \return true - is inside, false - is not
|
|---|
| 115 | */
|
|---|
| 116 | bool atom::IsInParallelepiped(Vector offset, double *parallelepiped)
|
|---|
| 117 | {
|
|---|
| 118 | return (node->IsInParallelepiped(offset, parallelepiped));
|
|---|
| 119 | };
|
|---|
| 120 |
|
|---|
| 121 | /** Output of a single atom.
|
|---|
| 122 | * \param ElementNo cardinal number of the element
|
|---|
| 123 | * \param AtomNo cardinal number among these atoms of the same element
|
|---|
| 124 | * \param *out stream to output to
|
|---|
| 125 | * \param *comment commentary after '#' sign
|
|---|
| 126 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| 127 | */
|
|---|
| 128 | bool atom::Output(ofstream *out, int ElementNo, int AtomNo, const char *comment) const
|
|---|
| 129 | {
|
|---|
| 130 | if (out != NULL) {
|
|---|
| 131 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 132 | *out << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2];
|
|---|
| 133 | *out << "\t" << FixedIon;
|
|---|
| 134 | if (v.Norm() > MYEPSILON)
|
|---|
| 135 | *out << "\t" << scientific << setprecision(6) << v.x[0] << "\t" << v.x[1] << "\t" << v.x[2] << "\t";
|
|---|
| 136 | if (comment != NULL)
|
|---|
| 137 | *out << " # " << comment << endl;
|
|---|
| 138 | else
|
|---|
| 139 | *out << " # molecule nr " << nr << endl;
|
|---|
| 140 | return true;
|
|---|
| 141 | } else
|
|---|
| 142 | return false;
|
|---|
| 143 | };
|
|---|
| 144 | bool atom::Output(ofstream *out, int *ElementNo, int *AtomNo, const char *comment)
|
|---|
| 145 | {
|
|---|
| 146 | AtomNo[type->Z]++; // increment number
|
|---|
| 147 | if (out != NULL) {
|
|---|
| 148 | *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 149 | *out << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2];
|
|---|
| 150 | *out << "\t" << FixedIon;
|
|---|
| 151 | if (v.Norm() > MYEPSILON)
|
|---|
| 152 | *out << "\t" << scientific << setprecision(6) << v.x[0] << "\t" << v.x[1] << "\t" << v.x[2] << "\t";
|
|---|
| 153 | if (comment != NULL)
|
|---|
| 154 | *out << " # " << comment << endl;
|
|---|
| 155 | else
|
|---|
| 156 | *out << " # molecule nr " << nr << endl;
|
|---|
| 157 | return true;
|
|---|
| 158 | } else
|
|---|
| 159 | return false;
|
|---|
| 160 | };
|
|---|
| 161 |
|
|---|
| 162 | /** Output of a single atom as one lin in xyz file.
|
|---|
| 163 | * \param *out stream to output to
|
|---|
| 164 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| 165 | */
|
|---|
| 166 | bool atom::OutputXYZLine(ofstream *out) const
|
|---|
| 167 | {
|
|---|
| 168 | if (out != NULL) {
|
|---|
| 169 | *out << type->symbol << "\t" << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2] << "\t" << endl;
|
|---|
| 170 | return true;
|
|---|
| 171 | } else
|
|---|
| 172 | return false;
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | /** Output of a single atom as one lin in xyz file.
|
|---|
| 176 | * \param *out stream to output to
|
|---|
| 177 | * \param *ElementNo array with ion type number in the config file this atom's element shall have
|
|---|
| 178 | * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
|
|---|
| 179 | * \param step Trajectory time step to output
|
|---|
| 180 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| 181 | */
|
|---|
| 182 | bool atom::OutputTrajectory(ofstream *out, int *ElementNo, int *AtomNo, int step) const
|
|---|
| 183 | {
|
|---|
| 184 | AtomNo[type->Z]++;
|
|---|
| 185 | if (out != NULL) {
|
|---|
| 186 | *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 187 | *out << Trajectory.R.at(step).x[0] << "\t" << Trajectory.R.at(step).x[1] << "\t" << Trajectory.R.at(step).x[2];
|
|---|
| 188 | *out << "\t" << FixedIon;
|
|---|
| 189 | if (Trajectory.U.at(step).Norm() > MYEPSILON)
|
|---|
| 190 | *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";
|
|---|
| 191 | if (Trajectory.F.at(step).Norm() > MYEPSILON)
|
|---|
| 192 | *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";
|
|---|
| 193 | *out << "\t# Number in molecule " << nr << endl;
|
|---|
| 194 | return true;
|
|---|
| 195 | } else
|
|---|
| 196 | return false;
|
|---|
| 197 | };
|
|---|
| 198 |
|
|---|
| 199 | /** Output of a single atom as one lin in xyz file.
|
|---|
| 200 | * \param *out stream to output to
|
|---|
| 201 | * \param step Trajectory time step to output
|
|---|
| 202 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| 203 | */
|
|---|
| 204 | bool atom::OutputTrajectoryXYZ(ofstream *out, int step) const
|
|---|
| 205 | {
|
|---|
| 206 | if (out != NULL) {
|
|---|
| 207 | *out << type->symbol << "\t";
|
|---|
| 208 | *out << Trajectory.R.at(step).x[0] << "\t";
|
|---|
| 209 | *out << Trajectory.R.at(step).x[1] << "\t";
|
|---|
| 210 | *out << Trajectory.R.at(step).x[2] << endl;
|
|---|
| 211 | return true;
|
|---|
| 212 | } else
|
|---|
| 213 | return false;
|
|---|
| 214 | };
|
|---|
| 215 |
|
|---|
| 216 | /** Prints all bonds of this atom from given global lists.
|
|---|
| 217 | * \param *out stream to output to
|
|---|
| 218 | * \param *NumberOfBondsPerAtom array with number of bonds per atomic index
|
|---|
| 219 | * \param ***ListOfBondsPerAtom array per atomic index of array with pointer to bond
|
|---|
| 220 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| 221 | */
|
|---|
| 222 | bool atom::OutputBondOfAtom(ofstream *out, int *NumberOfBondsPerAtom, bond ***ListOfBondsPerAtom) const
|
|---|
| 223 | {
|
|---|
| 224 | if (out != NULL) {
|
|---|
| 225 | #ifdef ADDHYDROGEN
|
|---|
| 226 | if (type->Z != 1) { // regard only non-hydrogen
|
|---|
| 227 | #endif
|
|---|
| 228 | *out << Verbose(4) << "Atom " << Name << "/" << nr << " with " << NumberOfBondsPerAtom[nr] << " bonds: ";
|
|---|
| 229 | int TotalDegree = 0;
|
|---|
| 230 | for (int j=0;j<NumberOfBondsPerAtom[nr];j++) {
|
|---|
| 231 | *out << *ListOfBondsPerAtom[nr][j] << "\t";
|
|---|
| 232 | TotalDegree += ListOfBondsPerAtom[nr][j]->BondDegree;
|
|---|
| 233 | }
|
|---|
| 234 | *out << " -- TotalDegree: " << TotalDegree << endl;
|
|---|
| 235 | #ifdef ADDHYDROGEN
|
|---|
| 236 | }
|
|---|
| 237 | #endif
|
|---|
| 238 | return true;
|
|---|
| 239 | } else
|
|---|
| 240 | return false;
|
|---|
| 241 | };
|
|---|
| 242 |
|
|---|
| 243 | ostream & operator << (ostream &ost, const atom &a)
|
|---|
| 244 | {
|
|---|
| 245 | ost << "[" << a.Name << "|" << &a << "]";
|
|---|
| 246 | return ost;
|
|---|
| 247 | };
|
|---|
| 248 |
|
|---|
| 249 | ostream & atom::operator << (ostream &ost)
|
|---|
| 250 | {
|
|---|
| 251 | ost << "[" << Name << "|" << this << "]";
|
|---|
| 252 | return ost;
|
|---|
| 253 | };
|
|---|
| 254 |
|
|---|
| 255 | /** Compares the indices of \a this atom with a given \a ptr.
|
|---|
| 256 | * \param ptr atom to compare index against
|
|---|
| 257 | * \return true - this one's is smaller, false - not
|
|---|
| 258 | */
|
|---|
| 259 | bool atom::Compare(const atom &ptr)
|
|---|
| 260 | {
|
|---|
| 261 | if (nr < ptr.nr)
|
|---|
| 262 | return true;
|
|---|
| 263 | else
|
|---|
| 264 | return false;
|
|---|
| 265 | };
|
|---|
| 266 |
|
|---|
| 267 | /** Returns squared distance to a given vector.
|
|---|
| 268 | * \param origin vector to calculate distance to
|
|---|
| 269 | * \return distance squared
|
|---|
| 270 | */
|
|---|
| 271 | double atom::DistanceSquaredToVector(Vector &origin)
|
|---|
| 272 | {
|
|---|
| 273 | return origin.DistanceSquared(&x);
|
|---|
| 274 | };
|
|---|
| 275 |
|
|---|
| 276 | /** Adds kinetic energy of this atom to given temperature value.
|
|---|
| 277 | * \param *temperature add on this value
|
|---|
| 278 | * \param step given step of trajectory to add
|
|---|
| 279 | */
|
|---|
| 280 | void atom::AddKineticToTemperature(double *temperature, int step) const
|
|---|
| 281 | {
|
|---|
| 282 | for (int i=NDIM;i--;)
|
|---|
| 283 | *temperature += type->mass * Trajectory.U.at(step).x[i]* Trajectory.U.at(step).x[i];
|
|---|
| 284 | };
|
|---|
| 285 |
|
|---|
| 286 | /** Returns distance to a given vector.
|
|---|
| 287 | * \param origin vector to calculate distance to
|
|---|
| 288 | * \return distance
|
|---|
| 289 | */
|
|---|
| 290 | double atom::DistanceToVector(Vector &origin)
|
|---|
| 291 | {
|
|---|
| 292 | return origin.Distance(&x);
|
|---|
| 293 | };
|
|---|
| 294 |
|
|---|
| 295 | bool operator < (atom &a, atom &b)
|
|---|
| 296 | {
|
|---|
| 297 | return a.Compare(b);
|
|---|
| 298 | };
|
|---|
| 299 |
|
|---|
| 300 | /** Evaluates some constraint potential if atom moves from \a startstep at once to \endstep in trajectory.
|
|---|
| 301 | * \param startstep trajectory begins at
|
|---|
| 302 | * \param endstep trajectory ends at
|
|---|
| 303 | * \param **PermutationMap if atom switches places with some other atom, there is no translation but a permutaton noted here (not in the trajectories of each).
|
|---|
| 304 | * \param *Force Force matrix to store result in
|
|---|
| 305 | */
|
|---|
| 306 | void atom::EvaluateConstrainedForce(int startstep, int endstep, atom **PermutationMap, ForceMatrix *Force)
|
|---|
| 307 | {
|
|---|
| 308 | double constant = 10.;
|
|---|
| 309 | atom *Sprinter = PermutationMap[nr];
|
|---|
| 310 | // set forces
|
|---|
| 311 | for (int i=NDIM;i++;)
|
|---|
| 312 | Force->Matrix[0][nr][5+i] += 2.*constant*sqrt(Trajectory.R.at(startstep).Distance(&Sprinter->Trajectory.R.at(endstep)));
|
|---|
| 313 | };
|
|---|