| [14de469] | 1 | /** \file atom.cpp
|
|---|
| [1907a7] | 2 | *
|
|---|
| [14de469] | 3 | * Function implementations for the class atom.
|
|---|
| [1907a7] | 4 | *
|
|---|
| [14de469] | 5 | */
|
|---|
| 6 |
|
|---|
| [112b09] | 7 | #include "Helpers/MemDebug.hpp"
|
|---|
| 8 |
|
|---|
| [357fba] | 9 | #include "atom.hpp"
|
|---|
| [e41951] | 10 | #include "bond.hpp"
|
|---|
| [4a7776a] | 11 | #include "config.hpp"
|
|---|
| [f66195] | 12 | #include "element.hpp"
|
|---|
| [266237] | 13 | #include "lists.hpp"
|
|---|
| [29812d] | 14 | #include "memoryallocator.hpp"
|
|---|
| [ccd9f5] | 15 | #include "parser.hpp"
|
|---|
| [f66195] | 16 | #include "vector.hpp"
|
|---|
| [d346b6] | 17 | #include "World.hpp"
|
|---|
| [1907a7] | 18 |
|
|---|
| [14de469] | 19 | /************************************* Functions for class atom *************************************/
|
|---|
| 20 |
|
|---|
| [70ff32] | 21 |
|
|---|
| [14de469] | 22 | /** Constructor of class atom.
|
|---|
| 23 | */
|
|---|
| [46d958] | 24 | atom::atom() :
|
|---|
| 25 | previous(NULL), next(NULL), father(this), sort(&nr)
|
|---|
| [14de469] | 26 | {
|
|---|
| [70ff32] | 27 | node = &x; // TesselPoint::x can only be referenced from here
|
|---|
| [14de469] | 28 | };
|
|---|
| 29 |
|
|---|
| [2319ed] | 30 | /** Constructor of class atom.
|
|---|
| 31 | */
|
|---|
| [46d958] | 32 | atom::atom(atom *pointer) :
|
|---|
| 33 | ParticleInfo(pointer),
|
|---|
| 34 | previous(NULL), next(NULL), father(pointer), sort(&nr)
|
|---|
| [2319ed] | 35 | {
|
|---|
| 36 | type = pointer->type; // copy element of atom
|
|---|
| [273382] | 37 | x = pointer->x; // copy coordination
|
|---|
| 38 | v = pointer->v; // copy velocity
|
|---|
| [2319ed] | 39 | FixedIon = pointer->FixedIon;
|
|---|
| [89c8b2] | 40 | node = &x;
|
|---|
| [b453f9] | 41 | };
|
|---|
| [2319ed] | 42 |
|
|---|
| [46d958] | 43 | atom *atom::clone(){
|
|---|
| [68f03d] | 44 | atom *res = new atom(this);
|
|---|
| [46d958] | 45 | res->previous=0;
|
|---|
| 46 | res->next=0;
|
|---|
| 47 | res->father = this;
|
|---|
| [5f612ee] | 48 | res->sort = &res->nr;
|
|---|
| [46d958] | 49 | res->type = type;
|
|---|
| [273382] | 50 | res->x = this->x;
|
|---|
| 51 | res->v = this->v;
|
|---|
| [46d958] | 52 | res->FixedIon = FixedIon;
|
|---|
| 53 | res->node = &x;
|
|---|
| [23b547] | 54 | World::getInstance().registerAtom(res);
|
|---|
| [46d958] | 55 | return res;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| [2319ed] | 58 |
|
|---|
| [14de469] | 59 | /** Destructor of class atom.
|
|---|
| 60 | */
|
|---|
| [1907a7] | 61 | atom::~atom()
|
|---|
| [14de469] | 62 | {
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | /** Climbs up the father list until NULL, last is returned.
|
|---|
| 67 | * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen)
|
|---|
| 68 | */
|
|---|
| 69 | atom *atom::GetTrueFather()
|
|---|
| 70 | {
|
|---|
| [215df0] | 71 | if(father == this){ // top most father is the one that points on itself
|
|---|
| 72 | return this;
|
|---|
| 73 | }
|
|---|
| 74 | else if(!father) {
|
|---|
| 75 | return 0;
|
|---|
| 76 | }
|
|---|
| 77 | else {
|
|---|
| 78 | return father->GetTrueFather();
|
|---|
| 79 | }
|
|---|
| [14de469] | 80 | };
|
|---|
| 81 |
|
|---|
| [e65246] | 82 | /** Sets father to itself or its father in case of copying a molecule.
|
|---|
| 83 | */
|
|---|
| 84 | void atom::CorrectFather()
|
|---|
| 85 | {
|
|---|
| 86 | if (father->father == father) // same atom in copy's father points to itself
|
|---|
| 87 | father = this; // set father to itself (copy of a whole molecule)
|
|---|
| 88 | else
|
|---|
| 89 | father = father->father; // set father to original's father
|
|---|
| 90 |
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | /** Check whether father is equal to given atom.
|
|---|
| 94 | * \param *ptr atom to compare father to
|
|---|
| 95 | * \param **res return value (only set if atom::father is equal to \a *ptr)
|
|---|
| 96 | */
|
|---|
| [b453f9] | 97 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
|---|
| [e65246] | 98 | {
|
|---|
| 99 | if ( ptr == father )
|
|---|
| 100 | *res = this;
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| [e9f8f9] | 103 | /** Checks whether atom is within the given box.
|
|---|
| 104 | * \param offset offset to box origin
|
|---|
| 105 | * \param *parallelepiped box matrix
|
|---|
| 106 | * \return true - is inside, false - is not
|
|---|
| 107 | */
|
|---|
| [b453f9] | 108 | bool atom::IsInParallelepiped(const Vector offset, const double *parallelepiped) const
|
|---|
| [e9f8f9] | 109 | {
|
|---|
| 110 | return (node->IsInParallelepiped(offset, parallelepiped));
|
|---|
| 111 | };
|
|---|
| 112 |
|
|---|
| [266237] | 113 | /** Counts the number of bonds weighted by bond::BondDegree.
|
|---|
| 114 | * \param bonds times bond::BondDegree
|
|---|
| 115 | */
|
|---|
| [4455f4] | 116 | int BondedParticle::CountBonds() const
|
|---|
| [266237] | 117 | {
|
|---|
| 118 | int NoBonds = 0;
|
|---|
| 119 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
|---|
| 120 | NoBonds += (*Runner)->BondDegree;
|
|---|
| 121 | return NoBonds;
|
|---|
| 122 | };
|
|---|
| 123 |
|
|---|
| [b453f9] | 124 | /** Output of a single atom with given numbering.
|
|---|
| [14de469] | 125 | * \param ElementNo cardinal number of the element
|
|---|
| 126 | * \param AtomNo cardinal number among these atoms of the same element
|
|---|
| 127 | * \param *out stream to output to
|
|---|
| [1907a7] | 128 | * \param *comment commentary after '#' sign
|
|---|
| [e41951] | 129 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| [14de469] | 130 | */
|
|---|
| [e138de] | 131 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
|---|
| [14de469] | 132 | {
|
|---|
| 133 | if (out != NULL) {
|
|---|
| 134 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| [0a4f7f] | 135 | *out << x[0] << "\t" << x[1] << "\t" << x[2];
|
|---|
| [943d02] | 136 | *out << "\t" << FixedIon;
|
|---|
| 137 | if (v.Norm() > MYEPSILON)
|
|---|
| [0a4f7f] | 138 | *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t";
|
|---|
| [437922] | 139 | if (comment != NULL)
|
|---|
| 140 | *out << " # " << comment << endl;
|
|---|
| [e9f8f9] | 141 | else
|
|---|
| 142 | *out << " # molecule nr " << nr << endl;
|
|---|
| 143 | return true;
|
|---|
| 144 | } else
|
|---|
| 145 | return false;
|
|---|
| 146 | };
|
|---|
| [b453f9] | 147 |
|
|---|
| 148 | /** Output of a single atom with numbering from array according to atom::type.
|
|---|
| 149 | * \param *ElementNo cardinal number of the element
|
|---|
| 150 | * \param *AtomNo cardinal number among these atoms of the same element
|
|---|
| 151 | * \param *out stream to output to
|
|---|
| 152 | * \param *comment commentary after '#' sign
|
|---|
| 153 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| 154 | */
|
|---|
| [e138de] | 155 | bool atom::OutputArrayIndexed(ofstream * const out, const int *ElementNo, int *AtomNo, const char *comment) const
|
|---|
| [e9f8f9] | 156 | {
|
|---|
| 157 | AtomNo[type->Z]++; // increment number
|
|---|
| 158 | if (out != NULL) {
|
|---|
| 159 | *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| [0a4f7f] | 160 | *out << x[0] << "\t" << x[1] << "\t" << x[2];
|
|---|
| [e9f8f9] | 161 | *out << "\t" << FixedIon;
|
|---|
| 162 | if (v.Norm() > MYEPSILON)
|
|---|
| [0a4f7f] | 163 | *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t";
|
|---|
| [e9f8f9] | 164 | if (comment != NULL)
|
|---|
| 165 | *out << " # " << comment << endl;
|
|---|
| [437922] | 166 | else
|
|---|
| 167 | *out << " # molecule nr " << nr << endl;
|
|---|
| [14de469] | 168 | return true;
|
|---|
| 169 | } else
|
|---|
| 170 | return false;
|
|---|
| 171 | };
|
|---|
| 172 |
|
|---|
| 173 | /** Output of a single atom as one lin in xyz file.
|
|---|
| 174 | * \param *out stream to output to
|
|---|
| [e41951] | 175 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| [14de469] | 176 | */
|
|---|
| 177 | bool atom::OutputXYZLine(ofstream *out) const
|
|---|
| 178 | {
|
|---|
| 179 | if (out != NULL) {
|
|---|
| [0a4f7f] | 180 | *out << type->symbol << "\t" << x[0] << "\t" << x[1] << "\t" << x[2] << "\t" << endl;
|
|---|
| [14de469] | 181 | return true;
|
|---|
| 182 | } else
|
|---|
| 183 | return false;
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| [fcd7b6] | 186 | /** Output of a single atom as one lin in xyz file.
|
|---|
| 187 | * \param *out stream to output to
|
|---|
| [e41951] | 188 | * \param *ElementNo array with ion type number in the config file this atom's element shall have
|
|---|
| 189 | * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
|
|---|
| 190 | * \param step Trajectory time step to output
|
|---|
| 191 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| [fcd7b6] | 192 | */
|
|---|
| [e138de] | 193 | bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const
|
|---|
| [fcd7b6] | 194 | {
|
|---|
| 195 | AtomNo[type->Z]++;
|
|---|
| 196 | if (out != NULL) {
|
|---|
| 197 | *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| [0a4f7f] | 198 | *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2];
|
|---|
| [fcd7b6] | 199 | *out << "\t" << FixedIon;
|
|---|
| 200 | if (Trajectory.U.at(step).Norm() > MYEPSILON)
|
|---|
| [0a4f7f] | 201 | *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t";
|
|---|
| [fcd7b6] | 202 | if (Trajectory.F.at(step).Norm() > MYEPSILON)
|
|---|
| [0a4f7f] | 203 | *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t";
|
|---|
| [fcd7b6] | 204 | *out << "\t# Number in molecule " << nr << endl;
|
|---|
| 205 | return true;
|
|---|
| 206 | } else
|
|---|
| 207 | return false;
|
|---|
| 208 | };
|
|---|
| 209 |
|
|---|
| [681a8a] | 210 | /** Output of a single atom as one lin in xyz file.
|
|---|
| 211 | * \param *out stream to output to
|
|---|
| [e41951] | 212 | * \param step Trajectory time step to output
|
|---|
| 213 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| [681a8a] | 214 | */
|
|---|
| [e138de] | 215 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
|
|---|
| [681a8a] | 216 | {
|
|---|
| 217 | if (out != NULL) {
|
|---|
| 218 | *out << type->symbol << "\t";
|
|---|
| [0a4f7f] | 219 | *out << Trajectory.R.at(step)[0] << "\t";
|
|---|
| 220 | *out << Trajectory.R.at(step)[1] << "\t";
|
|---|
| 221 | *out << Trajectory.R.at(step)[2] << endl;
|
|---|
| [681a8a] | 222 | return true;
|
|---|
| 223 | } else
|
|---|
| 224 | return false;
|
|---|
| 225 | };
|
|---|
| 226 |
|
|---|
| [4455f4] | 227 | /** Outputs the MPQC configuration line for this atom.
|
|---|
| 228 | * \param *out output stream
|
|---|
| 229 | * \param *center center of molecule subtracted from position
|
|---|
| 230 | * \param *AtomNo pointer to atom counter that is increased by one
|
|---|
| 231 | */
|
|---|
| [e138de] | 232 | void atom::OutputMPQCLine(ofstream * const out, const Vector *center, int *AtomNo = NULL) const
|
|---|
| [4455f4] | 233 | {
|
|---|
| [0a4f7f] | 234 | *out << "\t\t" << type->symbol << " [ " << x[0]-center->at(0) << "\t" << x[1]-center->at(1) << "\t" << x[2]-center->at(2) << " ]" << endl;
|
|---|
| [4455f4] | 235 | if (AtomNo != NULL)
|
|---|
| 236 | *AtomNo++;
|
|---|
| 237 | };
|
|---|
| 238 |
|
|---|
| 239 | /** Compares the indices of \a this atom with a given \a ptr.
|
|---|
| 240 | * \param ptr atom to compare index against
|
|---|
| 241 | * \return true - this one's is smaller, false - not
|
|---|
| 242 | */
|
|---|
| [b453f9] | 243 | bool atom::Compare(const atom &ptr) const
|
|---|
| [4455f4] | 244 | {
|
|---|
| 245 | if (nr < ptr.nr)
|
|---|
| 246 | return true;
|
|---|
| 247 | else
|
|---|
| 248 | return false;
|
|---|
| 249 | };
|
|---|
| 250 |
|
|---|
| 251 | /** Returns squared distance to a given vector.
|
|---|
| 252 | * \param origin vector to calculate distance to
|
|---|
| 253 | * \return distance squared
|
|---|
| 254 | */
|
|---|
| [b453f9] | 255 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
|---|
| [4455f4] | 256 | {
|
|---|
| [273382] | 257 | return origin.DistanceSquared(x);
|
|---|
| [4455f4] | 258 | };
|
|---|
| 259 |
|
|---|
| 260 | /** Returns distance to a given vector.
|
|---|
| 261 | * \param origin vector to calculate distance to
|
|---|
| 262 | * \return distance
|
|---|
| 263 | */
|
|---|
| [b453f9] | 264 | double atom::DistanceToVector(const Vector &origin) const
|
|---|
| [4455f4] | 265 | {
|
|---|
| [1513a74] | 266 | return origin.distance(x);
|
|---|
| [4455f4] | 267 | };
|
|---|
| 268 |
|
|---|
| 269 | /** Initialises the component number array.
|
|---|
| 270 | * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1)
|
|---|
| 271 | */
|
|---|
| 272 | void atom::InitComponentNr()
|
|---|
| 273 | {
|
|---|
| 274 | if (ComponentNr != NULL)
|
|---|
| [920c70] | 275 | delete[](ComponentNr);
|
|---|
| 276 | ComponentNr = new int[ListOfBonds.size()+1];
|
|---|
| [4455f4] | 277 | for (int i=ListOfBonds.size()+1;i--;)
|
|---|
| 278 | ComponentNr[i] = -1;
|
|---|
| 279 | };
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 | bool operator < (atom &a, atom &b)
|
|---|
| 283 | {
|
|---|
| 284 | return a.Compare(b);
|
|---|
| 285 | };
|
|---|
| 286 |
|
|---|
| [46d958] | 287 | World *atom::getWorld(){
|
|---|
| 288 | return world;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | void atom::setWorld(World* _world){
|
|---|
| 292 | world = _world;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| [88d586] | 295 | bool atom::changeId(atomId_t newId){
|
|---|
| 296 | // first we move ourselves in the world
|
|---|
| 297 | // the world lets us know if that succeeded
|
|---|
| 298 | if(world->changeAtomId(id,newId,this)){
|
|---|
| 299 | id = newId;
|
|---|
| 300 | return true;
|
|---|
| 301 | }
|
|---|
| 302 | else{
|
|---|
| 303 | return false;
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | void atom::setId(atomId_t _id) {
|
|---|
| [46d958] | 308 | id=_id;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| [57adc7] | 311 | atomId_t atom::getId() {
|
|---|
| [46d958] | 312 | return id;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| [88d586] | 315 | atom* NewAtom(atomId_t _id){
|
|---|
| 316 | atom * res =new atom();
|
|---|
| 317 | res->setId(_id);
|
|---|
| 318 | return res;
|
|---|
| [46d958] | 319 | }
|
|---|
| 320 |
|
|---|
| [88d586] | 321 | void DeleteAtom(atom* atom){
|
|---|
| [46d958] | 322 | delete atom;
|
|---|
| 323 | }
|
|---|