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