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