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