[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 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"
|
---|
[129204] | 22 | #include "Bond/bond.hpp"
|
---|
[e2373df] | 23 | #include "CodePatterns/Log.hpp"
|
---|
[4a7776a] | 24 | #include "config.hpp"
|
---|
[3bdb6d] | 25 | #include "Element/element.hpp"
|
---|
[57f243] | 26 | #include "LinearAlgebra/Vector.hpp"
|
---|
[d346b6] | 27 | #include "World.hpp"
|
---|
[6cfa36] | 28 | #include "molecule.hpp"
|
---|
[c550dd] | 29 | #include "Shapes/Shape.hpp"
|
---|
[a0064e] | 30 |
|
---|
[36166d] | 31 | #include <iomanip>
|
---|
[0ba410] | 32 | #include <iostream>
|
---|
[36166d] | 33 |
|
---|
[14de469] | 34 | /************************************* Functions for class atom *************************************/
|
---|
| 35 |
|
---|
[70ff32] | 36 |
|
---|
[46d958] | 37 | atom::atom() :
|
---|
[97b825] | 38 | father(this),
|
---|
[5309ba] | 39 | sort(&Nr),
|
---|
[97b825] | 40 | mol(0)
|
---|
[d74077] | 41 | {};
|
---|
[14de469] | 42 |
|
---|
[ea0c8b] | 43 | atom::atom(atom *pointer) :
|
---|
| 44 | ParticleInfo(*pointer),
|
---|
[c742fe] | 45 | AtomInfo(*pointer),
|
---|
[97b825] | 46 | father(pointer),
|
---|
[5309ba] | 47 | sort(&Nr),
|
---|
[9df680] | 48 | mol(0)
|
---|
[2319ed] | 49 | {
|
---|
[443547] | 50 | AtomicPosition = pointer->AtomicPosition; // copy trajectory of coordination
|
---|
| 51 | AtomicVelocity = pointer->AtomicVelocity; // copy trajectory of velocity
|
---|
| 52 | AtomicForce = pointer->AtomicForce;
|
---|
[b453f9] | 53 | };
|
---|
[2319ed] | 54 |
|
---|
[46d958] | 55 | atom *atom::clone(){
|
---|
[68f03d] | 56 | atom *res = new atom(this);
|
---|
[23b547] | 57 | World::getInstance().registerAtom(res);
|
---|
[46d958] | 58 | return res;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[2319ed] | 61 |
|
---|
[14de469] | 62 | /** Destructor of class atom.
|
---|
| 63 | */
|
---|
[1907a7] | 64 | atom::~atom()
|
---|
[14de469] | 65 | {
|
---|
[6cfa36] | 66 | removeFromMolecule();
|
---|
[14de469] | 67 | };
|
---|
[e2373df] | 68 |
|
---|
| 69 |
|
---|
| 70 | void atom::UpdateSteps()
|
---|
| 71 | {
|
---|
| 72 | LOG(4,"atom::UpdateSteps() called.");
|
---|
| 73 | // append to position, velocity and force vector
|
---|
| 74 | AtomInfo::AppendTrajectoryStep();
|
---|
[1e6249] | 75 | // append to ListOfBonds vector
|
---|
| 76 | BondedParticleInfo::AppendTrajectoryStep();
|
---|
[e2373df] | 77 | }
|
---|
| 78 |
|
---|
[59fff1] | 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
|
---|
[14de469] | 86 | {
|
---|
[215df0] | 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 | }
|
---|
[14de469] | 96 | };
|
---|
| 97 |
|
---|
[e65246] | 98 | /** Sets father to itself or its father in case of copying a molecule.
|
---|
| 99 | */
|
---|
| 100 | void atom::CorrectFather()
|
---|
| 101 | {
|
---|
[2e352f] | 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
|
---|
[e65246] | 105 | father = father->father; // set father to original's father
|
---|
| 106 |
|
---|
| 107 | };
|
---|
| 108 |
|
---|
[b453f9] | 109 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
---|
[e65246] | 110 | {
|
---|
| 111 | if ( ptr == father )
|
---|
| 112 | *res = this;
|
---|
| 113 | };
|
---|
| 114 |
|
---|
[00abfc] | 115 | bool atom::isFather(const atom *ptr){
|
---|
| 116 | return ptr==father;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[c550dd] | 119 | bool atom::IsInShape(const Shape& shape) const
|
---|
[e9f8f9] | 120 | {
|
---|
[d74077] | 121 | return shape.isInside(getPosition());
|
---|
[e9f8f9] | 122 | };
|
---|
| 123 |
|
---|
[e138de] | 124 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
---|
[14de469] | 125 | {
|
---|
| 126 | if (out != NULL) {
|
---|
| 127 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
---|
[d74077] | 128 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
---|
[6625c3] | 129 | *out << "\t" << (int)(getFixedIon());
|
---|
[bce72c] | 130 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
---|
| 131 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
---|
[437922] | 132 | if (comment != NULL)
|
---|
| 133 | *out << " # " << comment << endl;
|
---|
[e9f8f9] | 134 | else
|
---|
[735b1c] | 135 | *out << " # molecule nr " << getNr() << endl;
|
---|
[e9f8f9] | 136 | return true;
|
---|
| 137 | } else
|
---|
| 138 | return false;
|
---|
| 139 | };
|
---|
[b453f9] | 140 |
|
---|
[0ba410] | 141 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
|
---|
[e9f8f9] | 142 | {
|
---|
[83f176] | 143 | AtomNo[getType()->getAtomicNumber()]++; // increment number
|
---|
[e9f8f9] | 144 | if (out != NULL) {
|
---|
[8f4df1] | 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");
|
---|
[83f176] | 147 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
---|
[d74077] | 148 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
---|
[6625c3] | 149 | *out << "\t" << getFixedIon();
|
---|
[bce72c] | 150 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
---|
| 151 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
---|
[e9f8f9] | 152 | if (comment != NULL)
|
---|
| 153 | *out << " # " << comment << endl;
|
---|
[437922] | 154 | else
|
---|
[735b1c] | 155 | *out << " # molecule nr " << getNr() << endl;
|
---|
[14de469] | 156 | return true;
|
---|
| 157 | } else
|
---|
| 158 | return false;
|
---|
| 159 | };
|
---|
| 160 |
|
---|
| 161 | bool atom::OutputXYZLine(ofstream *out) const
|
---|
| 162 | {
|
---|
| 163 | if (out != NULL) {
|
---|
[b5c53d] | 164 | *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl;
|
---|
[14de469] | 165 | return true;
|
---|
| 166 | } else
|
---|
| 167 | return false;
|
---|
| 168 | };
|
---|
| 169 |
|
---|
[882a8a] | 170 | bool atom::OutputTrajectory(ofstream * const out, const enumeration<const element*> &elementLookup, int *AtomNo, const int step) const
|
---|
[fcd7b6] | 171 | {
|
---|
[83f176] | 172 | AtomNo[getType()->getAtomicNumber()]++;
|
---|
[882a8a] | 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;
|
---|
[056e70] | 177 | *out << getPositionAtStep(step)[0] << "\t" << getPositionAtStep(step)[1] << "\t" << getPositionAtStep(step)[2];
|
---|
[6625c3] | 178 | *out << "\t" << (int)(getFixedIon());
|
---|
[056e70] | 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";
|
---|
[735b1c] | 183 | *out << "\t# Number in molecule " << getNr() << endl;
|
---|
[fcd7b6] | 184 | return true;
|
---|
| 185 | } else
|
---|
| 186 | return false;
|
---|
| 187 | };
|
---|
| 188 |
|
---|
[e138de] | 189 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
|
---|
[681a8a] | 190 | {
|
---|
| 191 | if (out != NULL) {
|
---|
[b5c53d] | 192 | *out << getType()->getSymbol() << "\t";
|
---|
[056e70] | 193 | *out << getPositionAtStep(step)[0] << "\t";
|
---|
| 194 | *out << getPositionAtStep(step)[1] << "\t";
|
---|
| 195 | *out << getPositionAtStep(step)[2] << endl;
|
---|
[681a8a] | 196 | return true;
|
---|
| 197 | } else
|
---|
| 198 | return false;
|
---|
| 199 | };
|
---|
| 200 |
|
---|
[0dc86e2] | 201 | void atom::OutputMPQCLine(ostream * const out, const Vector *center) const
|
---|
[4455f4] | 202 | {
|
---|
[d74077] | 203 | Vector recentered(getPosition());
|
---|
| 204 | recentered -= *center;
|
---|
[b5c53d] | 205 | *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl;
|
---|
[4455f4] | 206 | };
|
---|
[9011c1] | 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 | };
|
---|
[4455f4] | 214 |
|
---|
[b453f9] | 215 | bool atom::Compare(const atom &ptr) const
|
---|
[4455f4] | 216 | {
|
---|
[735b1c] | 217 | if (getNr() < ptr.getNr())
|
---|
[4455f4] | 218 | return true;
|
---|
| 219 | else
|
---|
| 220 | return false;
|
---|
| 221 | };
|
---|
| 222 |
|
---|
[b453f9] | 223 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
---|
[4455f4] | 224 | {
|
---|
[d74077] | 225 | return DistanceSquared(origin);
|
---|
[4455f4] | 226 | };
|
---|
| 227 |
|
---|
[b453f9] | 228 | double atom::DistanceToVector(const Vector &origin) const
|
---|
[4455f4] | 229 | {
|
---|
[d74077] | 230 | return distance(origin);
|
---|
[4455f4] | 231 | };
|
---|
| 232 |
|
---|
| 233 | void atom::InitComponentNr()
|
---|
| 234 | {
|
---|
| 235 | if (ComponentNr != NULL)
|
---|
[920c70] | 236 | delete[](ComponentNr);
|
---|
[9d83b6] | 237 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[920c70] | 238 | ComponentNr = new int[ListOfBonds.size()+1];
|
---|
[4455f4] | 239 | for (int i=ListOfBonds.size()+1;i--;)
|
---|
| 240 | ComponentNr[i] = -1;
|
---|
[14b65e] | 241 | };
|
---|
| 242 |
|
---|
| 243 | void atom::resetGraphNr(){
|
---|
| 244 | GraphNr=-1;
|
---|
| 245 | }
|
---|
[4455f4] | 246 |
|
---|
[d74077] | 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 | }
|
---|
[4455f4] | 260 |
|
---|
| 261 | bool operator < (atom &a, atom &b)
|
---|
| 262 | {
|
---|
| 263 | return a.Compare(b);
|
---|
| 264 | };
|
---|
| 265 |
|
---|
[46d958] | 266 | World *atom::getWorld(){
|
---|
| 267 | return world;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | void atom::setWorld(World* _world){
|
---|
| 271 | world = _world;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[88d586] | 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) {
|
---|
[46d958] | 287 | id=_id;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[ad2b411] | 290 | atomId_t atom::getId() const {
|
---|
[46d958] | 291 | return id;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[6cfa36] | 294 | void atom::setMolecule(molecule *_mol){
|
---|
| 295 | // take this atom from the old molecule
|
---|
| 296 | removeFromMolecule();
|
---|
[3867a7] | 297 | mol = _mol;
|
---|
| 298 | if ((mol) && (!mol->containsAtom(this)))
|
---|
[dddbfe] | 299 | mol->insert(this);
|
---|
[6cfa36] | 300 | }
|
---|
| 301 |
|
---|
[0d9546] | 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 |
|
---|
[e41c48] | 310 | molecule* atom::getMolecule() const {
|
---|
[c084cc] | 311 | return mol;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[6cfa36] | 314 | void atom::removeFromMolecule(){
|
---|
| 315 | if(mol){
|
---|
| 316 | if(mol->containsAtom(this)){
|
---|
| 317 | mol->erase(this);
|
---|
| 318 | }
|
---|
| 319 | mol=0;
|
---|
| 320 | }
|
---|
[1f8337] | 321 | }
|
---|
| 322 |
|
---|
[560bbe] | 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 |
|
---|
[e8a21f] | 332 | int atom::getNr() const{
|
---|
[735b1c] | 333 | return ParticleInfo::getNr();
|
---|
[e8a21f] | 334 | }
|
---|
[6cfa36] | 335 |
|
---|
[88d586] | 336 | atom* NewAtom(atomId_t _id){
|
---|
| 337 | atom * res =new atom();
|
---|
| 338 | res->setId(_id);
|
---|
| 339 | return res;
|
---|
[46d958] | 340 | }
|
---|
| 341 |
|
---|
[88d586] | 342 | void DeleteAtom(atom* atom){
|
---|
[46d958] | 343 | delete atom;
|
---|
[e5f64de] | 344 | }
|
---|
| 345 |
|
---|
| 346 | bool compareAtomElements(atom* atom1,atom* atom2){
|
---|
| 347 | return atom1->getType()->getNumber() < atom2->getType()->getNumber();
|
---|
[46d958] | 348 | }
|
---|