| [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 |
|
|---|
| [ad011c] | 19 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| [112b09] | 20 |
|
|---|
| [357fba] | 21 | #include "atom.hpp"
|
|---|
| [e41951] | 22 | #include "bond.hpp"
|
|---|
| [e2373df] | 23 | #include "CodePatterns/Log.hpp"
|
|---|
| [4a7776a] | 24 | #include "config.hpp"
|
|---|
| [f66195] | 25 | #include "element.hpp"
|
|---|
| [266237] | 26 | #include "lists.hpp"
|
|---|
| [ccd9f5] | 27 | #include "parser.hpp"
|
|---|
| [57f243] | 28 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| [d346b6] | 29 | #include "World.hpp"
|
|---|
| [6cfa36] | 30 | #include "molecule.hpp"
|
|---|
| [c550dd] | 31 | #include "Shapes/Shape.hpp"
|
|---|
| [a0064e] | 32 |
|
|---|
| [36166d] | 33 | #include <iomanip>
|
|---|
| [0ba410] | 34 | #include <iostream>
|
|---|
| [36166d] | 35 |
|
|---|
| [14de469] | 36 | /************************************* Functions for class atom *************************************/
|
|---|
| 37 |
|
|---|
| [70ff32] | 38 |
|
|---|
| [14de469] | 39 | /** Constructor of class atom.
|
|---|
| 40 | */
|
|---|
| [46d958] | 41 | atom::atom() :
|
|---|
| [97b825] | 42 | father(this),
|
|---|
| 43 | sort(&nr),
|
|---|
| 44 | mol(0)
|
|---|
| [d74077] | 45 | {};
|
|---|
| [14de469] | 46 |
|
|---|
| [2319ed] | 47 | /** Constructor of class atom.
|
|---|
| 48 | */
|
|---|
| [46d958] | 49 | atom::atom(atom *pointer) :
|
|---|
| [97b825] | 50 | ParticleInfo(pointer),
|
|---|
| 51 | father(pointer),
|
|---|
| [9df680] | 52 | sort(&nr),
|
|---|
| 53 | mol(0)
|
|---|
| [2319ed] | 54 | {
|
|---|
| [443547] | 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;
|
|---|
| [6625c3] | 59 | setFixedIon(pointer->getFixedIon());
|
|---|
| [b453f9] | 60 | };
|
|---|
| [2319ed] | 61 |
|
|---|
| [46d958] | 62 | atom *atom::clone(){
|
|---|
| [68f03d] | 63 | atom *res = new atom(this);
|
|---|
| [23b547] | 64 | World::getInstance().registerAtom(res);
|
|---|
| [46d958] | 65 | return res;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| [2319ed] | 68 |
|
|---|
| [14de469] | 69 | /** Destructor of class atom.
|
|---|
| 70 | */
|
|---|
| [1907a7] | 71 | atom::~atom()
|
|---|
| [14de469] | 72 | {
|
|---|
| [6cfa36] | 73 | removeFromMolecule();
|
|---|
| [14de469] | 74 | };
|
|---|
| [e2373df] | 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 |
|
|---|
| [14de469] | 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 | {
|
|---|
| [215df0] | 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 | }
|
|---|
| [14de469] | 98 | };
|
|---|
| 99 |
|
|---|
| [e65246] | 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 | */
|
|---|
| [b453f9] | 115 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
|---|
| [e65246] | 116 | {
|
|---|
| 117 | if ( ptr == father )
|
|---|
| 118 | *res = this;
|
|---|
| 119 | };
|
|---|
| 120 |
|
|---|
| [00abfc] | 121 | bool atom::isFather(const atom *ptr){
|
|---|
| 122 | return ptr==father;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| [e9f8f9] | 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 | */
|
|---|
| [c550dd] | 130 | bool atom::IsInShape(const Shape& shape) const
|
|---|
| [e9f8f9] | 131 | {
|
|---|
| [d74077] | 132 | return shape.isInside(getPosition());
|
|---|
| [e9f8f9] | 133 | };
|
|---|
| 134 |
|
|---|
| [b453f9] | 135 | /** Output of a single atom with given numbering.
|
|---|
| [14de469] | 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
|
|---|
| [1907a7] | 139 | * \param *comment commentary after '#' sign
|
|---|
| [e41951] | 140 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| [14de469] | 141 | */
|
|---|
| [e138de] | 142 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
|---|
| [14de469] | 143 | {
|
|---|
| 144 | if (out != NULL) {
|
|---|
| 145 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| [d74077] | 146 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
|---|
| [6625c3] | 147 | *out << "\t" << (int)(getFixedIon());
|
|---|
| [bce72c] | 148 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
|---|
| 149 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
|---|
| [437922] | 150 | if (comment != NULL)
|
|---|
| 151 | *out << " # " << comment << endl;
|
|---|
| [e9f8f9] | 152 | else
|
|---|
| 153 | *out << " # molecule nr " << nr << endl;
|
|---|
| 154 | return true;
|
|---|
| 155 | } else
|
|---|
| 156 | return false;
|
|---|
| 157 | };
|
|---|
| [b453f9] | 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 | */
|
|---|
| [0ba410] | 166 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
|
|---|
| [e9f8f9] | 167 | {
|
|---|
| [83f176] | 168 | AtomNo[getType()->getAtomicNumber()]++; // increment number
|
|---|
| [e9f8f9] | 169 | if (out != NULL) {
|
|---|
| [8f4df1] | 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");
|
|---|
| [83f176] | 172 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| [d74077] | 173 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
|---|
| [6625c3] | 174 | *out << "\t" << getFixedIon();
|
|---|
| [bce72c] | 175 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
|---|
| 176 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
|---|
| [e9f8f9] | 177 | if (comment != NULL)
|
|---|
| 178 | *out << " # " << comment << endl;
|
|---|
| [437922] | 179 | else
|
|---|
| 180 | *out << " # molecule nr " << nr << endl;
|
|---|
| [14de469] | 181 | return true;
|
|---|
| 182 | } else
|
|---|
| 183 | return false;
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| [6625c3] | 186 | /** Output of a single atom as one line in xyz file.
|
|---|
| [14de469] | 187 | * \param *out stream to output to
|
|---|
| [e41951] | 188 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| [14de469] | 189 | */
|
|---|
| 190 | bool atom::OutputXYZLine(ofstream *out) const
|
|---|
| 191 | {
|
|---|
| 192 | if (out != NULL) {
|
|---|
| [b5c53d] | 193 | *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl;
|
|---|
| [14de469] | 194 | return true;
|
|---|
| 195 | } else
|
|---|
| 196 | return false;
|
|---|
| 197 | };
|
|---|
| 198 |
|
|---|
| [6625c3] | 199 | /** Output of a single atom as one line in xyz file.
|
|---|
| [fcd7b6] | 200 | * \param *out stream to output to
|
|---|
| [e41951] | 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
|
|---|
| [fcd7b6] | 205 | */
|
|---|
| [882a8a] | 206 | bool atom::OutputTrajectory(ofstream * const out, const enumeration<const element*> &elementLookup, int *AtomNo, const int step) const
|
|---|
| [fcd7b6] | 207 | {
|
|---|
| [83f176] | 208 | AtomNo[getType()->getAtomicNumber()]++;
|
|---|
| [882a8a] | 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;
|
|---|
| [056e70] | 213 | *out << getPositionAtStep(step)[0] << "\t" << getPositionAtStep(step)[1] << "\t" << getPositionAtStep(step)[2];
|
|---|
| [6625c3] | 214 | *out << "\t" << (int)(getFixedIon());
|
|---|
| [056e70] | 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";
|
|---|
| [fcd7b6] | 219 | *out << "\t# Number in molecule " << nr << endl;
|
|---|
| 220 | return true;
|
|---|
| 221 | } else
|
|---|
| 222 | return false;
|
|---|
| 223 | };
|
|---|
| 224 |
|
|---|
| [681a8a] | 225 | /** Output of a single atom as one lin in xyz file.
|
|---|
| 226 | * \param *out stream to output to
|
|---|
| [e41951] | 227 | * \param step Trajectory time step to output
|
|---|
| 228 | * \return true - \a *out present, false - \a *out is NULL
|
|---|
| [681a8a] | 229 | */
|
|---|
| [e138de] | 230 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
|
|---|
| [681a8a] | 231 | {
|
|---|
| 232 | if (out != NULL) {
|
|---|
| [b5c53d] | 233 | *out << getType()->getSymbol() << "\t";
|
|---|
| [056e70] | 234 | *out << getPositionAtStep(step)[0] << "\t";
|
|---|
| 235 | *out << getPositionAtStep(step)[1] << "\t";
|
|---|
| 236 | *out << getPositionAtStep(step)[2] << endl;
|
|---|
| [681a8a] | 237 | return true;
|
|---|
| 238 | } else
|
|---|
| 239 | return false;
|
|---|
| 240 | };
|
|---|
| 241 |
|
|---|
| [4455f4] | 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 | */
|
|---|
| [0dc86e2] | 247 | void atom::OutputMPQCLine(ostream * const out, const Vector *center) const
|
|---|
| [4455f4] | 248 | {
|
|---|
| [d74077] | 249 | Vector recentered(getPosition());
|
|---|
| 250 | recentered -= *center;
|
|---|
| [b5c53d] | 251 | *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl;
|
|---|
| [4455f4] | 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 | */
|
|---|
| [b453f9] | 258 | bool atom::Compare(const atom &ptr) const
|
|---|
| [4455f4] | 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 | */
|
|---|
| [b453f9] | 270 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
|---|
| [4455f4] | 271 | {
|
|---|
| [d74077] | 272 | return DistanceSquared(origin);
|
|---|
| [4455f4] | 273 | };
|
|---|
| 274 |
|
|---|
| 275 | /** Returns distance to a given vector.
|
|---|
| 276 | * \param origin vector to calculate distance to
|
|---|
| 277 | * \return distance
|
|---|
| 278 | */
|
|---|
| [b453f9] | 279 | double atom::DistanceToVector(const Vector &origin) const
|
|---|
| [4455f4] | 280 | {
|
|---|
| [d74077] | 281 | return distance(origin);
|
|---|
| [4455f4] | 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)
|
|---|
| [920c70] | 290 | delete[](ComponentNr);
|
|---|
| [9d83b6] | 291 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| [920c70] | 292 | ComponentNr = new int[ListOfBonds.size()+1];
|
|---|
| [4455f4] | 293 | for (int i=ListOfBonds.size()+1;i--;)
|
|---|
| 294 | ComponentNr[i] = -1;
|
|---|
| [14b65e] | 295 | };
|
|---|
| 296 |
|
|---|
| 297 | void atom::resetGraphNr(){
|
|---|
| 298 | GraphNr=-1;
|
|---|
| 299 | }
|
|---|
| [4455f4] | 300 |
|
|---|
| [d74077] | 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 | }
|
|---|
| [4455f4] | 314 |
|
|---|
| 315 | bool operator < (atom &a, atom &b)
|
|---|
| 316 | {
|
|---|
| 317 | return a.Compare(b);
|
|---|
| 318 | };
|
|---|
| 319 |
|
|---|
| [46d958] | 320 | World *atom::getWorld(){
|
|---|
| 321 | return world;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | void atom::setWorld(World* _world){
|
|---|
| 325 | world = _world;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| [88d586] | 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) {
|
|---|
| [46d958] | 341 | id=_id;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| [ad2b411] | 344 | atomId_t atom::getId() const {
|
|---|
| [46d958] | 345 | return id;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| [fa7989] | 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 | */
|
|---|
| [6cfa36] | 352 | void atom::setMolecule(molecule *_mol){
|
|---|
| 353 | // take this atom from the old molecule
|
|---|
| 354 | removeFromMolecule();
|
|---|
| 355 | mol = _mol;
|
|---|
| 356 | if(!mol->containsAtom(this)){
|
|---|
| [dddbfe] | 357 | mol->insert(this);
|
|---|
| [6cfa36] | 358 | }
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| [fa7989] | 361 | /** Returns pointer to the molecule which atom belongs to.
|
|---|
| 362 | * \return containing molecule
|
|---|
| 363 | */
|
|---|
| [e41c48] | 364 | molecule* atom::getMolecule() const {
|
|---|
| [c084cc] | 365 | return mol;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| [fa7989] | 368 | /** Erases the atom in atom::mol's list of atoms and sets it to zero.
|
|---|
| 369 | */
|
|---|
| [6cfa36] | 370 | void atom::removeFromMolecule(){
|
|---|
| 371 | if(mol){
|
|---|
| 372 | if(mol->containsAtom(this)){
|
|---|
| 373 | mol->erase(this);
|
|---|
| 374 | }
|
|---|
| 375 | mol=0;
|
|---|
| 376 | }
|
|---|
| [1f8337] | 377 | }
|
|---|
| 378 |
|
|---|
| [e8a21f] | 379 | int atom::getNr() const{
|
|---|
| 380 | return nr;
|
|---|
| 381 | }
|
|---|
| [6cfa36] | 382 |
|
|---|
| [88d586] | 383 | atom* NewAtom(atomId_t _id){
|
|---|
| 384 | atom * res =new atom();
|
|---|
| 385 | res->setId(_id);
|
|---|
| 386 | return res;
|
|---|
| [46d958] | 387 | }
|
|---|
| 388 |
|
|---|
| [88d586] | 389 | void DeleteAtom(atom* atom){
|
|---|
| [46d958] | 390 | delete atom;
|
|---|
| [e5f64de] | 391 | }
|
|---|
| 392 |
|
|---|
| 393 | bool compareAtomElements(atom* atom1,atom* atom2){
|
|---|
| 394 | return atom1->getType()->getNumber() < atom2->getType()->getNumber();
|
|---|
| [46d958] | 395 | }
|
|---|