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