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