[bcf653] | 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 |
|
---|
[14de469] | 8 | /** \file atom.cpp
|
---|
[1907a7] | 9 | *
|
---|
[14de469] | 10 | * Function implementations for the class atom.
|
---|
[1907a7] | 11 | *
|
---|
[14de469] | 12 | */
|
---|
| 13 |
|
---|
[bf3817] | 14 | // include config.h
|
---|
| 15 | #ifdef HAVE_CONFIG_H
|
---|
| 16 | #include <config.h>
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
[112b09] | 19 | #include "Helpers/MemDebug.hpp"
|
---|
| 20 |
|
---|
[357fba] | 21 | #include "atom.hpp"
|
---|
[e41951] | 22 | #include "bond.hpp"
|
---|
[4a7776a] | 23 | #include "config.hpp"
|
---|
[f66195] | 24 | #include "element.hpp"
|
---|
[266237] | 25 | #include "lists.hpp"
|
---|
[ccd9f5] | 26 | #include "parser.hpp"
|
---|
[57f243] | 27 | #include "LinearAlgebra/Vector.hpp"
|
---|
[d346b6] | 28 | #include "World.hpp"
|
---|
[6cfa36] | 29 | #include "molecule.hpp"
|
---|
[c550dd] | 30 | #include "Shapes/Shape.hpp"
|
---|
[1907a7] | 31 |
|
---|
[36166d] | 32 | #include <iomanip>
|
---|
[0ba410] | 33 | #include <iostream>
|
---|
[36166d] | 34 |
|
---|
[14de469] | 35 | /************************************* Functions for class atom *************************************/
|
---|
| 36 |
|
---|
[70ff32] | 37 |
|
---|
[14de469] | 38 | /** Constructor of class atom.
|
---|
| 39 | */
|
---|
[46d958] | 40 | atom::atom() :
|
---|
[97b825] | 41 | father(this),
|
---|
| 42 | sort(&nr),
|
---|
| 43 | mol(0)
|
---|
[d74077] | 44 | {};
|
---|
[14de469] | 45 |
|
---|
[2319ed] | 46 | /** Constructor of class atom.
|
---|
| 47 | */
|
---|
[46d958] | 48 | atom::atom(atom *pointer) :
|
---|
[97b825] | 49 | ParticleInfo(pointer),
|
---|
| 50 | father(pointer),
|
---|
| 51 | sort(&nr)
|
---|
[2319ed] | 52 | {
|
---|
[d74077] | 53 | setType(pointer->getType()); // copy element of atom
|
---|
| 54 | setPosition(pointer->getPosition()); // copy coordination
|
---|
| 55 | AtomicVelocity = pointer->AtomicVelocity; // copy velocity
|
---|
[2319ed] | 56 | FixedIon = pointer->FixedIon;
|
---|
[6cfa36] | 57 | mol = 0;
|
---|
[b453f9] | 58 | };
|
---|
[2319ed] | 59 |
|
---|
[46d958] | 60 | atom *atom::clone(){
|
---|
[68f03d] | 61 | atom *res = new atom(this);
|
---|
[46d958] | 62 | res->father = this;
|
---|
[5f612ee] | 63 | res->sort = &res->nr;
|
---|
[d74077] | 64 | res->setType(getType());
|
---|
| 65 | res->setPosition(this->getPosition());
|
---|
| 66 | res->AtomicVelocity = this->AtomicVelocity;
|
---|
[46d958] | 67 | res->FixedIon = FixedIon;
|
---|
[6cfa36] | 68 | res->mol = 0;
|
---|
[23b547] | 69 | World::getInstance().registerAtom(res);
|
---|
[46d958] | 70 | return res;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[2319ed] | 73 |
|
---|
[14de469] | 74 | /** Destructor of class atom.
|
---|
| 75 | */
|
---|
[1907a7] | 76 | atom::~atom()
|
---|
[14de469] | 77 | {
|
---|
[6cfa36] | 78 | removeFromMolecule();
|
---|
[a80241] | 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 | }
|
---|
[14de469] | 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 | {
|
---|
[215df0] | 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 | }
|
---|
[14de469] | 101 | };
|
---|
| 102 |
|
---|
[e65246] | 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 | */
|
---|
[b453f9] | 118 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
---|
[e65246] | 119 | {
|
---|
| 120 | if ( ptr == father )
|
---|
| 121 | *res = this;
|
---|
| 122 | };
|
---|
| 123 |
|
---|
[00abfc] | 124 | bool atom::isFather(const atom *ptr){
|
---|
| 125 | return ptr==father;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[e9f8f9] | 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 | */
|
---|
[c550dd] | 133 | bool atom::IsInShape(const Shape& shape) const
|
---|
[e9f8f9] | 134 | {
|
---|
[d74077] | 135 | return shape.isInside(getPosition());
|
---|
[e9f8f9] | 136 | };
|
---|
| 137 |
|
---|
[266237] | 138 | /** Counts the number of bonds weighted by bond::BondDegree.
|
---|
| 139 | * \param bonds times bond::BondDegree
|
---|
| 140 | */
|
---|
[4455f4] | 141 | int BondedParticle::CountBonds() const
|
---|
[266237] | 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 |
|
---|
[b453f9] | 149 | /** Output of a single atom with given numbering.
|
---|
[14de469] | 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
|
---|
[1907a7] | 153 | * \param *comment commentary after '#' sign
|
---|
[e41951] | 154 | * \return true - \a *out present, false - \a *out is NULL
|
---|
[14de469] | 155 | */
|
---|
[e138de] | 156 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
---|
[14de469] | 157 | {
|
---|
| 158 | if (out != NULL) {
|
---|
| 159 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
---|
[d74077] | 160 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
---|
[943d02] | 161 | *out << "\t" << FixedIon;
|
---|
[d74077] | 162 | if (AtomicVelocity.Norm() > MYEPSILON)
|
---|
| 163 | *out << "\t" << scientific << setprecision(6) << AtomicVelocity[0] << "\t" << AtomicVelocity[1] << "\t" << AtomicVelocity[2] << "\t";
|
---|
[437922] | 164 | if (comment != NULL)
|
---|
| 165 | *out << " # " << comment << endl;
|
---|
[e9f8f9] | 166 | else
|
---|
| 167 | *out << " # molecule nr " << nr << endl;
|
---|
| 168 | return true;
|
---|
| 169 | } else
|
---|
| 170 | return false;
|
---|
| 171 | };
|
---|
[b453f9] | 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 | */
|
---|
[0ba410] | 180 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
|
---|
[e9f8f9] | 181 | {
|
---|
[83f176] | 182 | AtomNo[getType()->getAtomicNumber()]++; // increment number
|
---|
[e9f8f9] | 183 | if (out != NULL) {
|
---|
[8f4df1] | 184 | const element *elemental = getType();
|
---|
| 185 | ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
|
---|
[83f176] | 186 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
---|
[d74077] | 187 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
---|
[e9f8f9] | 188 | *out << "\t" << FixedIon;
|
---|
[d74077] | 189 | if (AtomicVelocity.Norm() > MYEPSILON)
|
---|
| 190 | *out << "\t" << scientific << setprecision(6) << AtomicVelocity[0] << "\t" << AtomicVelocity[1] << "\t" << AtomicVelocity[2] << "\t";
|
---|
[e9f8f9] | 191 | if (comment != NULL)
|
---|
| 192 | *out << " # " << comment << endl;
|
---|
[437922] | 193 | else
|
---|
| 194 | *out << " # molecule nr " << nr << endl;
|
---|
[14de469] | 195 | return true;
|
---|
| 196 | } else
|
---|
| 197 | return false;
|
---|
| 198 | };
|
---|
| 199 |
|
---|
| 200 | /** Output of a single atom as one lin in xyz file.
|
---|
| 201 | * \param *out stream to output to
|
---|
[e41951] | 202 | * \return true - \a *out present, false - \a *out is NULL
|
---|
[14de469] | 203 | */
|
---|
| 204 | bool atom::OutputXYZLine(ofstream *out) const
|
---|
| 205 | {
|
---|
| 206 | if (out != NULL) {
|
---|
[b5c53d] | 207 | *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl;
|
---|
[14de469] | 208 | return true;
|
---|
| 209 | } else
|
---|
| 210 | return false;
|
---|
| 211 | };
|
---|
| 212 |
|
---|
[fcd7b6] | 213 | /** Output of a single atom as one lin in xyz file.
|
---|
| 214 | * \param *out stream to output to
|
---|
[e41951] | 215 | * \param *ElementNo array with ion type number in the config file this atom's element shall have
|
---|
| 216 | * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
|
---|
| 217 | * \param step Trajectory time step to output
|
---|
| 218 | * \return true - \a *out present, false - \a *out is NULL
|
---|
[fcd7b6] | 219 | */
|
---|
[882a8a] | 220 | bool atom::OutputTrajectory(ofstream * const out, const enumeration<const element*> &elementLookup, int *AtomNo, const int step) const
|
---|
[fcd7b6] | 221 | {
|
---|
[83f176] | 222 | AtomNo[getType()->getAtomicNumber()]++;
|
---|
[882a8a] | 223 | if (out != NULL) {
|
---|
| 224 | const element *elemental = getType();
|
---|
| 225 | ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
|
---|
| 226 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[getType()->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
---|
[0a4f7f] | 227 | *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2];
|
---|
[fcd7b6] | 228 | *out << "\t" << FixedIon;
|
---|
| 229 | if (Trajectory.U.at(step).Norm() > MYEPSILON)
|
---|
[0a4f7f] | 230 | *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t";
|
---|
[fcd7b6] | 231 | if (Trajectory.F.at(step).Norm() > MYEPSILON)
|
---|
[0a4f7f] | 232 | *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t";
|
---|
[fcd7b6] | 233 | *out << "\t# Number in molecule " << nr << endl;
|
---|
| 234 | return true;
|
---|
| 235 | } else
|
---|
| 236 | return false;
|
---|
| 237 | };
|
---|
| 238 |
|
---|
[681a8a] | 239 | /** Output of a single atom as one lin in xyz file.
|
---|
| 240 | * \param *out stream to output to
|
---|
[e41951] | 241 | * \param step Trajectory time step to output
|
---|
| 242 | * \return true - \a *out present, false - \a *out is NULL
|
---|
[681a8a] | 243 | */
|
---|
[e138de] | 244 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
|
---|
[681a8a] | 245 | {
|
---|
| 246 | if (out != NULL) {
|
---|
[b5c53d] | 247 | *out << getType()->getSymbol() << "\t";
|
---|
[0a4f7f] | 248 | *out << Trajectory.R.at(step)[0] << "\t";
|
---|
| 249 | *out << Trajectory.R.at(step)[1] << "\t";
|
---|
| 250 | *out << Trajectory.R.at(step)[2] << endl;
|
---|
[681a8a] | 251 | return true;
|
---|
| 252 | } else
|
---|
| 253 | return false;
|
---|
| 254 | };
|
---|
| 255 |
|
---|
[4455f4] | 256 | /** Outputs the MPQC configuration line for this atom.
|
---|
| 257 | * \param *out output stream
|
---|
| 258 | * \param *center center of molecule subtracted from position
|
---|
| 259 | * \param *AtomNo pointer to atom counter that is increased by one
|
---|
| 260 | */
|
---|
[0dc86e2] | 261 | void atom::OutputMPQCLine(ostream * const out, const Vector *center) const
|
---|
[4455f4] | 262 | {
|
---|
[d74077] | 263 | Vector recentered(getPosition());
|
---|
| 264 | recentered -= *center;
|
---|
[b5c53d] | 265 | *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl;
|
---|
[4455f4] | 266 | };
|
---|
| 267 |
|
---|
| 268 | /** Compares the indices of \a this atom with a given \a ptr.
|
---|
| 269 | * \param ptr atom to compare index against
|
---|
| 270 | * \return true - this one's is smaller, false - not
|
---|
| 271 | */
|
---|
[b453f9] | 272 | bool atom::Compare(const atom &ptr) const
|
---|
[4455f4] | 273 | {
|
---|
| 274 | if (nr < ptr.nr)
|
---|
| 275 | return true;
|
---|
| 276 | else
|
---|
| 277 | return false;
|
---|
| 278 | };
|
---|
| 279 |
|
---|
| 280 | /** Returns squared distance to a given vector.
|
---|
| 281 | * \param origin vector to calculate distance to
|
---|
| 282 | * \return distance squared
|
---|
| 283 | */
|
---|
[b453f9] | 284 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
---|
[4455f4] | 285 | {
|
---|
[d74077] | 286 | return DistanceSquared(origin);
|
---|
[4455f4] | 287 | };
|
---|
| 288 |
|
---|
| 289 | /** Returns distance to a given vector.
|
---|
| 290 | * \param origin vector to calculate distance to
|
---|
| 291 | * \return distance
|
---|
| 292 | */
|
---|
[b453f9] | 293 | double atom::DistanceToVector(const Vector &origin) const
|
---|
[4455f4] | 294 | {
|
---|
[d74077] | 295 | return distance(origin);
|
---|
[4455f4] | 296 | };
|
---|
| 297 |
|
---|
| 298 | /** Initialises the component number array.
|
---|
| 299 | * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1)
|
---|
| 300 | */
|
---|
| 301 | void atom::InitComponentNr()
|
---|
| 302 | {
|
---|
| 303 | if (ComponentNr != NULL)
|
---|
[920c70] | 304 | delete[](ComponentNr);
|
---|
| 305 | ComponentNr = new int[ListOfBonds.size()+1];
|
---|
[4455f4] | 306 | for (int i=ListOfBonds.size()+1;i--;)
|
---|
| 307 | ComponentNr[i] = -1;
|
---|
[14b65e] | 308 | };
|
---|
| 309 |
|
---|
| 310 | void atom::resetGraphNr(){
|
---|
| 311 | GraphNr=-1;
|
---|
| 312 | }
|
---|
[4455f4] | 313 |
|
---|
[d74077] | 314 | std::ostream & atom::operator << (std::ostream &ost) const
|
---|
| 315 | {
|
---|
| 316 | ParticleInfo::operator<<(ost);
|
---|
| 317 | ost << "," << getPosition();
|
---|
| 318 | return ost;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | std::ostream & operator << (std::ostream &ost, const atom &a)
|
---|
| 322 | {
|
---|
| 323 | a.ParticleInfo::operator<<(ost);
|
---|
| 324 | ost << "," << a.getPosition();
|
---|
| 325 | return ost;
|
---|
| 326 | }
|
---|
[4455f4] | 327 |
|
---|
| 328 | bool operator < (atom &a, atom &b)
|
---|
| 329 | {
|
---|
| 330 | return a.Compare(b);
|
---|
| 331 | };
|
---|
| 332 |
|
---|
[46d958] | 333 | World *atom::getWorld(){
|
---|
| 334 | return world;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | void atom::setWorld(World* _world){
|
---|
| 338 | world = _world;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[88d586] | 341 | bool atom::changeId(atomId_t newId){
|
---|
| 342 | // first we move ourselves in the world
|
---|
| 343 | // the world lets us know if that succeeded
|
---|
| 344 | if(world->changeAtomId(id,newId,this)){
|
---|
| 345 | id = newId;
|
---|
| 346 | return true;
|
---|
| 347 | }
|
---|
| 348 | else{
|
---|
| 349 | return false;
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | void atom::setId(atomId_t _id) {
|
---|
[46d958] | 354 | id=_id;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[ad2b411] | 357 | atomId_t atom::getId() const {
|
---|
[46d958] | 358 | return id;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[fa7989] | 361 | /** Makes the atom be contained in the new molecule \a *_mol.
|
---|
| 362 | * Uses atom::removeFromMolecule() to delist from old molecule.
|
---|
| 363 | * \param *_mol pointer to new molecule
|
---|
| 364 | */
|
---|
[6cfa36] | 365 | void atom::setMolecule(molecule *_mol){
|
---|
| 366 | // take this atom from the old molecule
|
---|
| 367 | removeFromMolecule();
|
---|
| 368 | mol = _mol;
|
---|
| 369 | if(!mol->containsAtom(this)){
|
---|
[dddbfe] | 370 | mol->insert(this);
|
---|
[6cfa36] | 371 | }
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[fa7989] | 374 | /** Returns pointer to the molecule which atom belongs to.
|
---|
| 375 | * \return containing molecule
|
---|
| 376 | */
|
---|
[e41c48] | 377 | molecule* atom::getMolecule() const {
|
---|
[c084cc] | 378 | return mol;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[fa7989] | 381 | /** Erases the atom in atom::mol's list of atoms and sets it to zero.
|
---|
| 382 | */
|
---|
[6cfa36] | 383 | void atom::removeFromMolecule(){
|
---|
| 384 | if(mol){
|
---|
| 385 | if(mol->containsAtom(this)){
|
---|
| 386 | mol->erase(this);
|
---|
| 387 | }
|
---|
| 388 | mol=0;
|
---|
| 389 | }
|
---|
[1f8337] | 390 | }
|
---|
| 391 |
|
---|
| 392 | double atom::getMass() const{
|
---|
| 393 | return getType()->getMass();
|
---|
[6cfa36] | 394 | }
|
---|
[e8a21f] | 395 |
|
---|
| 396 | int atom::getNr() const{
|
---|
| 397 | return nr;
|
---|
| 398 | }
|
---|
[6cfa36] | 399 |
|
---|
[88d586] | 400 | atom* NewAtom(atomId_t _id){
|
---|
| 401 | atom * res =new atom();
|
---|
| 402 | res->setId(_id);
|
---|
| 403 | return res;
|
---|
[46d958] | 404 | }
|
---|
| 405 |
|
---|
[88d586] | 406 | void DeleteAtom(atom* atom){
|
---|
[46d958] | 407 | delete atom;
|
---|
[e5f64de] | 408 | }
|
---|
| 409 |
|
---|
| 410 | bool compareAtomElements(atom* atom1,atom* atom2){
|
---|
| 411 | return atom1->getType()->getNumber() < atom2->getType()->getNumber();
|
---|
[46d958] | 412 | }
|
---|