| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 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/bond.hpp"
|
|---|
| 23 | #include "CodePatterns/Log.hpp"
|
|---|
| 24 | #include "config.hpp"
|
|---|
| 25 | #include "Element/element.hpp"
|
|---|
| 26 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 27 | #include "World.hpp"
|
|---|
| 28 | #include "molecule.hpp"
|
|---|
| 29 | #include "Shapes/Shape.hpp"
|
|---|
| 30 |
|
|---|
| 31 | #include <iomanip>
|
|---|
| 32 | #include <iostream>
|
|---|
| 33 |
|
|---|
| 34 | /************************************* Functions for class atom *************************************/
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | atom::atom() :
|
|---|
| 38 | father(this),
|
|---|
| 39 | sort(&Nr),
|
|---|
| 40 | mol(0)
|
|---|
| 41 | {};
|
|---|
| 42 |
|
|---|
| 43 | atom::atom(atom *pointer) :
|
|---|
| 44 | ParticleInfo(*pointer),
|
|---|
| 45 | AtomInfo(*pointer),
|
|---|
| 46 | father(pointer),
|
|---|
| 47 | sort(&Nr),
|
|---|
| 48 | mol(0)
|
|---|
| 49 | {
|
|---|
| 50 | AtomicPosition = pointer->AtomicPosition; // copy trajectory of coordination
|
|---|
| 51 | AtomicVelocity = pointer->AtomicVelocity; // copy trajectory of velocity
|
|---|
| 52 | AtomicForce = pointer->AtomicForce;
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | atom *atom::clone(){
|
|---|
| 56 | atom *res = new atom(this);
|
|---|
| 57 | World::getInstance().registerAtom(res);
|
|---|
| 58 | return res;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | /** Destructor of class atom.
|
|---|
| 63 | */
|
|---|
| 64 | atom::~atom()
|
|---|
| 65 | {
|
|---|
| 66 | removeFromMolecule();
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | void atom::UpdateSteps()
|
|---|
| 71 | {
|
|---|
| 72 | LOG(4,"atom::UpdateSteps() called.");
|
|---|
| 73 | // append to position, velocity and force vector
|
|---|
| 74 | AtomInfo::AppendTrajectoryStep();
|
|---|
| 75 | // append to ListOfBonds vector
|
|---|
| 76 | BondedParticleInfo::AppendTrajectoryStep();
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | atom *atom::GetTrueFather()
|
|---|
| 80 | {
|
|---|
| 81 | const atom *father = const_cast<const atom *>(this)->GetTrueFather();
|
|---|
| 82 | return const_cast<atom *>(father);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | const atom *atom::GetTrueFather() const
|
|---|
| 86 | {
|
|---|
| 87 | if(father == this){ // top most father is the one that points on itself
|
|---|
| 88 | return this;
|
|---|
| 89 | }
|
|---|
| 90 | else if(!father) {
|
|---|
| 91 | return 0;
|
|---|
| 92 | }
|
|---|
| 93 | else {
|
|---|
| 94 | return father->GetTrueFather();
|
|---|
| 95 | }
|
|---|
| 96 | };
|
|---|
| 97 |
|
|---|
| 98 | /** Sets father to itself or its father in case of copying a molecule.
|
|---|
| 99 | */
|
|---|
| 100 | void atom::CorrectFather()
|
|---|
| 101 | {
|
|---|
| 102 | if (father->father != father) // same atom in copy's father points to itself
|
|---|
| 103 | // father = this; // set father to itself (copy of a whole molecule)
|
|---|
| 104 | // else
|
|---|
| 105 | father = father->father; // set father to original's father
|
|---|
| 106 |
|
|---|
| 107 | };
|
|---|
| 108 |
|
|---|
| 109 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
|---|
| 110 | {
|
|---|
| 111 | if ( ptr == father )
|
|---|
| 112 | *res = this;
|
|---|
| 113 | };
|
|---|
| 114 |
|
|---|
| 115 | bool atom::isFather(const atom *ptr){
|
|---|
| 116 | return ptr==father;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | bool atom::IsInShape(const Shape& shape) const
|
|---|
| 120 | {
|
|---|
| 121 | return shape.isInside(getPosition());
|
|---|
| 122 | };
|
|---|
| 123 |
|
|---|
| 124 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
|---|
| 125 | {
|
|---|
| 126 | if (out != NULL) {
|
|---|
| 127 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 128 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
|---|
| 129 | *out << "\t" << (int)(getFixedIon());
|
|---|
| 130 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
|---|
| 131 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
|---|
| 132 | if (comment != NULL)
|
|---|
| 133 | *out << " # " << comment << endl;
|
|---|
| 134 | else
|
|---|
| 135 | *out << " # molecule nr " << getNr() << endl;
|
|---|
| 136 | return true;
|
|---|
| 137 | } else
|
|---|
| 138 | return false;
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
|
|---|
| 142 | {
|
|---|
| 143 | AtomNo[getType()->getAtomicNumber()]++; // increment number
|
|---|
| 144 | if (out != NULL) {
|
|---|
| 145 | const element *elemental = getType();
|
|---|
| 146 | ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
|
|---|
| 147 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 148 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
|---|
| 149 | *out << "\t" << 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 " << getNr() << endl;
|
|---|
| 156 | return true;
|
|---|
| 157 | } else
|
|---|
| 158 | return false;
|
|---|
| 159 | };
|
|---|
| 160 |
|
|---|
| 161 | bool atom::OutputXYZLine(ofstream *out) const
|
|---|
| 162 | {
|
|---|
| 163 | if (out != NULL) {
|
|---|
| 164 | *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl;
|
|---|
| 165 | return true;
|
|---|
| 166 | } else
|
|---|
| 167 | return false;
|
|---|
| 168 | };
|
|---|
| 169 |
|
|---|
| 170 | bool atom::OutputTrajectory(ofstream * const out, const enumeration<const element*> &elementLookup, int *AtomNo, const int step) const
|
|---|
| 171 | {
|
|---|
| 172 | AtomNo[getType()->getAtomicNumber()]++;
|
|---|
| 173 | if (out != NULL) {
|
|---|
| 174 | const element *elemental = getType();
|
|---|
| 175 | ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
|
|---|
| 176 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[getType()->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 177 | *out << getPositionAtStep(step)[0] << "\t" << getPositionAtStep(step)[1] << "\t" << getPositionAtStep(step)[2];
|
|---|
| 178 | *out << "\t" << (int)(getFixedIon());
|
|---|
| 179 | if (getAtomicVelocityAtStep(step).Norm() > MYEPSILON)
|
|---|
| 180 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocityAtStep(step)[0] << "\t" << getAtomicVelocityAtStep(step)[1] << "\t" << getAtomicVelocityAtStep(step)[2] << "\t";
|
|---|
| 181 | if (getAtomicForceAtStep(step).Norm() > MYEPSILON)
|
|---|
| 182 | *out << "\t" << scientific << setprecision(6) << getAtomicForceAtStep(step)[0] << "\t" << getAtomicForceAtStep(step)[1] << "\t" << getAtomicForceAtStep(step)[2] << "\t";
|
|---|
| 183 | *out << "\t# Number in molecule " << getNr() << endl;
|
|---|
| 184 | return true;
|
|---|
| 185 | } else
|
|---|
| 186 | return false;
|
|---|
| 187 | };
|
|---|
| 188 |
|
|---|
| 189 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
|
|---|
| 190 | {
|
|---|
| 191 | if (out != NULL) {
|
|---|
| 192 | *out << getType()->getSymbol() << "\t";
|
|---|
| 193 | *out << getPositionAtStep(step)[0] << "\t";
|
|---|
| 194 | *out << getPositionAtStep(step)[1] << "\t";
|
|---|
| 195 | *out << getPositionAtStep(step)[2] << endl;
|
|---|
| 196 | return true;
|
|---|
| 197 | } else
|
|---|
| 198 | return false;
|
|---|
| 199 | };
|
|---|
| 200 |
|
|---|
| 201 | void atom::OutputMPQCLine(ostream * const out, const Vector *center) const
|
|---|
| 202 | {
|
|---|
| 203 | Vector recentered(getPosition());
|
|---|
| 204 | recentered -= *center;
|
|---|
| 205 | *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl;
|
|---|
| 206 | };
|
|---|
| 207 |
|
|---|
| 208 | void atom::OutputPsi3Line(ostream * const out, const Vector *center) const
|
|---|
| 209 | {
|
|---|
| 210 | Vector recentered(getPosition());
|
|---|
| 211 | recentered -= *center;
|
|---|
| 212 | *out << "\t( " << getType()->getSymbol() << "\t" << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " )" << endl;
|
|---|
| 213 | };
|
|---|
| 214 |
|
|---|
| 215 | bool atom::Compare(const atom &ptr) const
|
|---|
| 216 | {
|
|---|
| 217 | if (getNr() < ptr.getNr())
|
|---|
| 218 | return true;
|
|---|
| 219 | else
|
|---|
| 220 | return false;
|
|---|
| 221 | };
|
|---|
| 222 |
|
|---|
| 223 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
|---|
| 224 | {
|
|---|
| 225 | return DistanceSquared(origin);
|
|---|
| 226 | };
|
|---|
| 227 |
|
|---|
| 228 | double atom::DistanceToVector(const Vector &origin) const
|
|---|
| 229 | {
|
|---|
| 230 | return distance(origin);
|
|---|
| 231 | };
|
|---|
| 232 |
|
|---|
| 233 | void atom::InitComponentNr()
|
|---|
| 234 | {
|
|---|
| 235 | if (ComponentNr != NULL)
|
|---|
| 236 | delete[](ComponentNr);
|
|---|
| 237 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| 238 | ComponentNr = new int[ListOfBonds.size()+1];
|
|---|
| 239 | for (int i=ListOfBonds.size()+1;i--;)
|
|---|
| 240 | ComponentNr[i] = -1;
|
|---|
| 241 | };
|
|---|
| 242 |
|
|---|
| 243 | void atom::resetGraphNr(){
|
|---|
| 244 | GraphNr=-1;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | std::ostream & atom::operator << (std::ostream &ost) const
|
|---|
| 248 | {
|
|---|
| 249 | ParticleInfo::operator<<(ost);
|
|---|
| 250 | ost << "," << getPosition();
|
|---|
| 251 | return ost;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | std::ostream & operator << (std::ostream &ost, const atom &a)
|
|---|
| 255 | {
|
|---|
| 256 | a.ParticleInfo::operator<<(ost);
|
|---|
| 257 | ost << "," << a.getPosition();
|
|---|
| 258 | return ost;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | bool operator < (atom &a, atom &b)
|
|---|
| 262 | {
|
|---|
| 263 | return a.Compare(b);
|
|---|
| 264 | };
|
|---|
| 265 |
|
|---|
| 266 | World *atom::getWorld(){
|
|---|
| 267 | return world;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | void atom::setWorld(World* _world){
|
|---|
| 271 | world = _world;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | bool atom::changeId(atomId_t newId){
|
|---|
| 275 | // first we move ourselves in the world
|
|---|
| 276 | // the world lets us know if that succeeded
|
|---|
| 277 | if(world->changeAtomId(id,newId,this)){
|
|---|
| 278 | id = newId;
|
|---|
| 279 | return true;
|
|---|
| 280 | }
|
|---|
| 281 | else{
|
|---|
| 282 | return false;
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | void atom::setId(atomId_t _id) {
|
|---|
| 287 | id=_id;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | atomId_t atom::getId() const {
|
|---|
| 291 | return id;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | void atom::setMolecule(molecule *_mol){
|
|---|
| 295 | // take this atom from the old molecule
|
|---|
| 296 | removeFromMolecule();
|
|---|
| 297 | mol = _mol;
|
|---|
| 298 | if ((mol) && (!mol->containsAtom(this)))
|
|---|
| 299 | mol->insert(this);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | void atom::unsetMolecule()
|
|---|
| 303 | {
|
|---|
| 304 | // take this atom from the old molecule
|
|---|
| 305 | ASSERT(!mol->containsAtom(this),
|
|---|
| 306 | "atom::unsetMolecule() - old molecule "+toString(mol)+" still contains us!");
|
|---|
| 307 | mol = NULL;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | molecule* atom::getMolecule() const {
|
|---|
| 311 | return mol;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | void atom::removeFromMolecule(){
|
|---|
| 315 | if(mol){
|
|---|
| 316 | if(mol->containsAtom(this)){
|
|---|
| 317 | mol->erase(this);
|
|---|
| 318 | }
|
|---|
| 319 | mol=0;
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | bool atom::changeNr(const int newNr)
|
|---|
| 324 | {
|
|---|
| 325 | if ((mol) && (mol->changeAtomNr(getNr(),newNr,this))) {
|
|---|
| 326 | return true;
|
|---|
| 327 | } else{
|
|---|
| 328 | return false;
|
|---|
| 329 | }
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | int atom::getNr() const{
|
|---|
| 333 | return ParticleInfo::getNr();
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | atom* NewAtom(atomId_t _id){
|
|---|
| 337 | atom * res =new atom();
|
|---|
| 338 | res->setId(_id);
|
|---|
| 339 | return res;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | void DeleteAtom(atom* atom){
|
|---|
| 343 | delete atom;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | bool compareAtomElements(atom* atom1,atom* atom2){
|
|---|
| 347 | return atom1->getType()->getNumber() < atom2->getType()->getNumber();
|
|---|
| 348 | }
|
|---|