| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
|---|
| 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | *
|
|---|
| 8 | * This file is part of MoleCuilder.
|
|---|
| 9 | *
|
|---|
| 10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
|---|
| 11 | * it under the terms of the GNU General Public License as published by
|
|---|
| 12 | * the Free Software Foundation, either version 2 of the License, or
|
|---|
| 13 | * (at your option) any later version.
|
|---|
| 14 | *
|
|---|
| 15 | * MoleCuilder is distributed in the hope that it will be useful,
|
|---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 18 | * GNU General Public License for more details.
|
|---|
| 19 | *
|
|---|
| 20 | * You should have received a copy of the GNU General Public License
|
|---|
| 21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | /** \file atom.cpp
|
|---|
| 25 | *
|
|---|
| 26 | * Function implementations for the class atom.
|
|---|
| 27 | *
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | // include config.h
|
|---|
| 31 | #ifdef HAVE_CONFIG_H
|
|---|
| 32 | #include <config.h>
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 36 |
|
|---|
| 37 | #include "atom.hpp"
|
|---|
| 38 | #include "AtomObserver.hpp"
|
|---|
| 39 | #include "Bond/bond.hpp"
|
|---|
| 40 | #include "CodePatterns/Log.hpp"
|
|---|
| 41 | #include "config.hpp"
|
|---|
| 42 | #include "Element/element.hpp"
|
|---|
| 43 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 44 | #include "World.hpp"
|
|---|
| 45 | #include "WorldTime.hpp"
|
|---|
| 46 | #include "molecule.hpp"
|
|---|
| 47 | #include "Shapes/Shape.hpp"
|
|---|
| 48 |
|
|---|
| 49 | #include <iomanip>
|
|---|
| 50 | #include <iostream>
|
|---|
| 51 |
|
|---|
| 52 | /************************************* Functions for class atom *************************************/
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | atom::atom() :
|
|---|
| 56 | father(this),
|
|---|
| 57 | sort(&Nr),
|
|---|
| 58 | mol(0)
|
|---|
| 59 | {
|
|---|
| 60 | // sign on to global atom change tracker
|
|---|
| 61 | AtomObserver::getInstance().AtomInserted(this);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | atom::atom(atom *pointer) :
|
|---|
| 65 | ParticleInfo(*pointer),
|
|---|
| 66 | AtomInfo(*pointer),
|
|---|
| 67 | father(pointer),
|
|---|
| 68 | sort(&Nr),
|
|---|
| 69 | mol(0)
|
|---|
| 70 | {
|
|---|
| 71 | AtomicPosition = pointer->AtomicPosition; // copy trajectory of coordination
|
|---|
| 72 | AtomicVelocity = pointer->AtomicVelocity; // copy trajectory of velocity
|
|---|
| 73 | AtomicForce = pointer->AtomicForce;
|
|---|
| 74 | // sign on to global atom change tracker
|
|---|
| 75 | AtomObserver::getInstance().AtomInserted(this);
|
|---|
| 76 | };
|
|---|
| 77 |
|
|---|
| 78 | atom *atom::clone(){
|
|---|
| 79 | atom *res = new atom(this);
|
|---|
| 80 | World::getInstance().registerAtom(res);
|
|---|
| 81 | return res;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | /** Destructor of class atom.
|
|---|
| 86 | */
|
|---|
| 87 | atom::~atom()
|
|---|
| 88 | {
|
|---|
| 89 | removeFromMolecule();
|
|---|
| 90 | // sign off from global atom change tracker
|
|---|
| 91 | AtomObserver::getInstance().AtomRemoved(this);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | void atom::UpdateSteps()
|
|---|
| 96 | {
|
|---|
| 97 | LOG(4,"atom::UpdateSteps() called.");
|
|---|
| 98 | // append to position, velocity and force vector
|
|---|
| 99 | AtomInfo::AppendTrajectoryStep();
|
|---|
| 100 | // append to ListOfBonds vector
|
|---|
| 101 | BondedParticleInfo::AppendTrajectoryStep();
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void atom::removeSteps()
|
|---|
| 105 | {
|
|---|
| 106 | LOG(4,"atom::removeSteps() called.");
|
|---|
| 107 | // append to position, velocity and force vector
|
|---|
| 108 | AtomInfo::removeTrajectoryStep();
|
|---|
| 109 | // append to ListOfBonds vector
|
|---|
| 110 | BondedParticleInfo::removeTrajectoryStep();
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | atom *atom::GetTrueFather()
|
|---|
| 114 | {
|
|---|
| 115 | const atom *father = const_cast<const atom *>(this)->GetTrueFather();
|
|---|
| 116 | return const_cast<atom *>(father);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | const atom *atom::GetTrueFather() const
|
|---|
| 120 | {
|
|---|
| 121 | if(father == this){ // top most father is the one that points on itself
|
|---|
| 122 | return this;
|
|---|
| 123 | }
|
|---|
| 124 | else if(!father) {
|
|---|
| 125 | return 0;
|
|---|
| 126 | }
|
|---|
| 127 | else {
|
|---|
| 128 | return father->GetTrueFather();
|
|---|
| 129 | }
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | /** Sets father to itself or its father in case of copying a molecule.
|
|---|
| 133 | */
|
|---|
| 134 | void atom::CorrectFather()
|
|---|
| 135 | {
|
|---|
| 136 | if (father->father != father) // same atom in copy's father points to itself
|
|---|
| 137 | // father = this; // set father to itself (copy of a whole molecule)
|
|---|
| 138 | // else
|
|---|
| 139 | father = father->father; // set father to original's father
|
|---|
| 140 |
|
|---|
| 141 | };
|
|---|
| 142 |
|
|---|
| 143 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
|---|
| 144 | {
|
|---|
| 145 | if ( ptr == father )
|
|---|
| 146 | *res = this;
|
|---|
| 147 | };
|
|---|
| 148 |
|
|---|
| 149 | bool atom::isFather(const atom *ptr){
|
|---|
| 150 | return ptr==father;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
|---|
| 154 | {
|
|---|
| 155 | if (out != NULL) {
|
|---|
| 156 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 157 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
|---|
| 158 | *out << "\t" << (int)(getFixedIon());
|
|---|
| 159 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
|---|
| 160 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
|---|
| 161 | if (comment != NULL)
|
|---|
| 162 | *out << " # " << comment << endl;
|
|---|
| 163 | else
|
|---|
| 164 | *out << " # molecule nr " << getNr() << endl;
|
|---|
| 165 | return true;
|
|---|
| 166 | } else
|
|---|
| 167 | return false;
|
|---|
| 168 | };
|
|---|
| 169 |
|
|---|
| 170 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
|
|---|
| 171 | {
|
|---|
| 172 | AtomNo[getType()->getAtomicNumber()]++; // increment number
|
|---|
| 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[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 177 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
|---|
| 178 | *out << "\t" << getFixedIon();
|
|---|
| 179 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
|---|
| 180 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
|---|
| 181 | if (comment != NULL)
|
|---|
| 182 | *out << " # " << comment << endl;
|
|---|
| 183 | else
|
|---|
| 184 | *out << " # molecule nr " << getNr() << endl;
|
|---|
| 185 | return true;
|
|---|
| 186 | } else
|
|---|
| 187 | return false;
|
|---|
| 188 | };
|
|---|
| 189 |
|
|---|
| 190 | bool atom::Compare(const atom &ptr) const
|
|---|
| 191 | {
|
|---|
| 192 | if (getNr() < ptr.getNr())
|
|---|
| 193 | return true;
|
|---|
| 194 | else
|
|---|
| 195 | return false;
|
|---|
| 196 | };
|
|---|
| 197 |
|
|---|
| 198 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
|---|
| 199 | {
|
|---|
| 200 | return DistanceSquared(origin);
|
|---|
| 201 | };
|
|---|
| 202 |
|
|---|
| 203 | double atom::DistanceToVector(const Vector &origin) const
|
|---|
| 204 | {
|
|---|
| 205 | return distance(origin);
|
|---|
| 206 | };
|
|---|
| 207 |
|
|---|
| 208 | void atom::InitComponentNr()
|
|---|
| 209 | {
|
|---|
| 210 | if (ComponentNr != NULL)
|
|---|
| 211 | delete[](ComponentNr);
|
|---|
| 212 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| 213 | ComponentNr = new int[ListOfBonds.size()+1];
|
|---|
| 214 | for (int i=ListOfBonds.size()+1;i--;)
|
|---|
| 215 | ComponentNr[i] = -1;
|
|---|
| 216 | };
|
|---|
| 217 |
|
|---|
| 218 | void atom::resetGraphNr(){
|
|---|
| 219 | GraphNr=-1;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | std::ostream & atom::operator << (std::ostream &ost) const
|
|---|
| 223 | {
|
|---|
| 224 | ParticleInfo::operator<<(ost);
|
|---|
| 225 | ost << "," << getPosition();
|
|---|
| 226 | return ost;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | std::ostream & operator << (std::ostream &ost, const atom &a)
|
|---|
| 230 | {
|
|---|
| 231 | a.ParticleInfo::operator<<(ost);
|
|---|
| 232 | ost << "," << a.getPosition();
|
|---|
| 233 | return ost;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | bool operator < (atom &a, atom &b)
|
|---|
| 237 | {
|
|---|
| 238 | return a.Compare(b);
|
|---|
| 239 | };
|
|---|
| 240 |
|
|---|
| 241 | World *atom::getWorld(){
|
|---|
| 242 | return world;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | void atom::setWorld(World* _world){
|
|---|
| 246 | world = _world;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | bool atom::changeId(atomId_t newId){
|
|---|
| 250 | // first we move ourselves in the world
|
|---|
| 251 | // the world lets us know if that succeeded
|
|---|
| 252 | if(world->changeAtomId(id,newId,this)){
|
|---|
| 253 | OBSERVE;
|
|---|
| 254 | id = newId;
|
|---|
| 255 | NOTIFY(IndexChanged);
|
|---|
| 256 | return true;
|
|---|
| 257 | }
|
|---|
| 258 | else{
|
|---|
| 259 | return false;
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | void atom::setId(atomId_t _id) {
|
|---|
| 264 | id=_id;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | atomId_t atom::getId() const {
|
|---|
| 268 | return id;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | void atom::setMolecule(molecule *_mol){
|
|---|
| 272 | // take this atom from the old molecule
|
|---|
| 273 | removeFromMolecule();
|
|---|
| 274 | mol = _mol;
|
|---|
| 275 | if ((mol) && (!mol->containsAtom(this)))
|
|---|
| 276 | mol->insert(this);
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | void atom::unsetMolecule()
|
|---|
| 280 | {
|
|---|
| 281 | // take this atom from the old molecule
|
|---|
| 282 | ASSERT(!mol->containsAtom(this),
|
|---|
| 283 | "atom::unsetMolecule() - old molecule "+toString(mol)+" still contains us!");
|
|---|
| 284 | mol = NULL;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | molecule* atom::getMolecule() const {
|
|---|
| 288 | return mol;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | void atom::removeFromMolecule(){
|
|---|
| 292 | if(mol){
|
|---|
| 293 | if(mol->containsAtom(this)){
|
|---|
| 294 | mol->erase(this);
|
|---|
| 295 | }
|
|---|
| 296 | mol=0;
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | bool atom::changeNr(const int newNr)
|
|---|
| 301 | {
|
|---|
| 302 | if ((mol) && (mol->changeAtomNr(getNr(),newNr,this))) {
|
|---|
| 303 | return true;
|
|---|
| 304 | } else{
|
|---|
| 305 | return false;
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | int atom::getNr() const{
|
|---|
| 310 | return ParticleInfo::getNr();
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | atom* NewAtom(atomId_t _id){
|
|---|
| 314 | atom * res = new atom();
|
|---|
| 315 | // extent trajectory to current time step
|
|---|
| 316 | const size_t CurrentTime = WorldTime::getTime();
|
|---|
| 317 | for (size_t step = res->getTrajectorySize(); step <= CurrentTime; ++step)
|
|---|
| 318 | res->UpdateSteps();
|
|---|
| 319 | res->setId(_id);
|
|---|
| 320 | return res;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | void DeleteAtom(atom* atom){
|
|---|
| 324 | delete atom;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | bool compareAtomElements(atom* atom1,atom* atom2){
|
|---|
| 328 | return atom1->getType()->getAtomicNumber() < atom2->getType()->getAtomicNumber();
|
|---|
| 329 | }
|
|---|