[14de469] | 1 | /** \file atom.cpp
|
---|
[1907a7] | 2 | *
|
---|
[14de469] | 3 | * Function implementations for the class atom.
|
---|
[1907a7] | 4 | *
|
---|
[14de469] | 5 | */
|
---|
| 6 |
|
---|
[357fba] | 7 | #include "atom.hpp"
|
---|
[e41951] | 8 | #include "bond.hpp"
|
---|
[4a7776a] | 9 | #include "config.hpp"
|
---|
[f66195] | 10 | #include "element.hpp"
|
---|
[266237] | 11 | #include "lists.hpp"
|
---|
[29812d] | 12 | #include "memoryallocator.hpp"
|
---|
[ccd9f5] | 13 | #include "parser.hpp"
|
---|
[f66195] | 14 | #include "vector.hpp"
|
---|
[1907a7] | 15 |
|
---|
[14de469] | 16 | /************************************* Functions for class atom *************************************/
|
---|
| 17 |
|
---|
[70ff32] | 18 |
|
---|
[14de469] | 19 | /** Constructor of class atom.
|
---|
| 20 | */
|
---|
[70ff32] | 21 | atom::atom() : previous(NULL), next(NULL), father(this), sort(&nr)
|
---|
[14de469] | 22 | {
|
---|
[70ff32] | 23 | node = &x; // TesselPoint::x can only be referenced from here
|
---|
[14de469] | 24 | };
|
---|
| 25 |
|
---|
[2319ed] | 26 | /** Constructor of class atom.
|
---|
| 27 | */
|
---|
[70ff32] | 28 | atom::atom(atom *pointer) : previous(NULL), next(NULL), father(pointer), sort(&nr)
|
---|
[2319ed] | 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;
|
---|
[89c8b2] | 34 | node = &x;
|
---|
[b453f9] | 35 | };
|
---|
[2319ed] | 36 |
|
---|
| 37 |
|
---|
[14de469] | 38 | /** Destructor of class atom.
|
---|
| 39 | */
|
---|
[1907a7] | 40 | atom::~atom()
|
---|
[14de469] | 41 | {
|
---|
[266237] | 42 | unlink(this);
|
---|
[14de469] | 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 |
|
---|
[e65246] | 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 | */
|
---|
[b453f9] | 75 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
---|
[e65246] | 76 | {
|
---|
| 77 | if ( ptr == father )
|
---|
| 78 | *res = this;
|
---|
| 79 | };
|
---|
| 80 |
|
---|
[e9f8f9] | 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 | */
|
---|
[b453f9] | 86 | bool atom::IsInParallelepiped(const Vector offset, const double *parallelepiped) const
|
---|
[e9f8f9] | 87 | {
|
---|
| 88 | return (node->IsInParallelepiped(offset, parallelepiped));
|
---|
| 89 | };
|
---|
| 90 |
|
---|
[266237] | 91 | /** Counts the number of bonds weighted by bond::BondDegree.
|
---|
| 92 | * \param bonds times bond::BondDegree
|
---|
| 93 | */
|
---|
[4455f4] | 94 | int BondedParticle::CountBonds() const
|
---|
[266237] | 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 |
|
---|
[b453f9] | 102 | /** Output of a single atom with given numbering.
|
---|
[14de469] | 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
|
---|
[1907a7] | 106 | * \param *comment commentary after '#' sign
|
---|
[e41951] | 107 | * \return true - \a *out present, false - \a *out is NULL
|
---|
[14de469] | 108 | */
|
---|
[e138de] | 109 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
---|
[14de469] | 110 | {
|
---|
| 111 | if (out != NULL) {
|
---|
| 112 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
---|
[943d02] | 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";
|
---|
[437922] | 117 | if (comment != NULL)
|
---|
| 118 | *out << " # " << comment << endl;
|
---|
[e9f8f9] | 119 | else
|
---|
| 120 | *out << " # molecule nr " << nr << endl;
|
---|
| 121 | return true;
|
---|
| 122 | } else
|
---|
| 123 | return false;
|
---|
| 124 | };
|
---|
[b453f9] | 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 | */
|
---|
[e138de] | 133 | bool atom::OutputArrayIndexed(ofstream * const out, const int *ElementNo, int *AtomNo, const char *comment) const
|
---|
[e9f8f9] | 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;
|
---|
[437922] | 144 | else
|
---|
| 145 | *out << " # molecule nr " << nr << endl;
|
---|
[14de469] | 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
|
---|
[e41951] | 153 | * \return true - \a *out present, false - \a *out is NULL
|
---|
[14de469] | 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 |
|
---|
[fcd7b6] | 164 | /** Output of a single atom as one lin in xyz file.
|
---|
| 165 | * \param *out stream to output to
|
---|
[e41951] | 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
|
---|
[fcd7b6] | 170 | */
|
---|
[e138de] | 171 | bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const
|
---|
[fcd7b6] | 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 |
|
---|
[681a8a] | 188 | /** Output of a single atom as one lin in xyz file.
|
---|
| 189 | * \param *out stream to output to
|
---|
[e41951] | 190 | * \param step Trajectory time step to output
|
---|
| 191 | * \return true - \a *out present, false - \a *out is NULL
|
---|
[681a8a] | 192 | */
|
---|
[e138de] | 193 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
|
---|
[681a8a] | 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 |
|
---|
[4455f4] | 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 | */
|
---|
[e138de] | 210 | void atom::OutputMPQCLine(ofstream * const out, const Vector *center, int *AtomNo = NULL) const
|
---|
[4455f4] | 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 | */
|
---|
[b453f9] | 221 | bool atom::Compare(const atom &ptr) const
|
---|
[4455f4] | 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 | */
|
---|
[b453f9] | 233 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
---|
[4455f4] | 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 | */
|
---|
[b453f9] | 242 | double atom::DistanceToVector(const Vector &origin) const
|
---|
[4455f4] | 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 |
|
---|