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