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