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