| 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 | atom *atom::GetTrueFather()
|
|---|
| 105 | {
|
|---|
| 106 | const atom *father = const_cast<const atom *>(this)->GetTrueFather();
|
|---|
| 107 | return const_cast<atom *>(father);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | const atom *atom::GetTrueFather() const
|
|---|
| 111 | {
|
|---|
| 112 | if(father == this){ // top most father is the one that points on itself
|
|---|
| 113 | return this;
|
|---|
| 114 | }
|
|---|
| 115 | else if(!father) {
|
|---|
| 116 | return 0;
|
|---|
| 117 | }
|
|---|
| 118 | else {
|
|---|
| 119 | return father->GetTrueFather();
|
|---|
| 120 | }
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | /** Sets father to itself or its father in case of copying a molecule.
|
|---|
| 124 | */
|
|---|
| 125 | void atom::CorrectFather()
|
|---|
| 126 | {
|
|---|
| 127 | if (father->father != father) // same atom in copy's father points to itself
|
|---|
| 128 | // father = this; // set father to itself (copy of a whole molecule)
|
|---|
| 129 | // else
|
|---|
| 130 | father = father->father; // set father to original's father
|
|---|
| 131 |
|
|---|
| 132 | };
|
|---|
| 133 |
|
|---|
| 134 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
|---|
| 135 | {
|
|---|
| 136 | if ( ptr == father )
|
|---|
| 137 | *res = this;
|
|---|
| 138 | };
|
|---|
| 139 |
|
|---|
| 140 | bool atom::isFather(const atom *ptr){
|
|---|
| 141 | return ptr==father;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
|---|
| 145 | {
|
|---|
| 146 | if (out != NULL) {
|
|---|
| 147 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 148 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
|---|
| 149 | *out << "\t" << (int)(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::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
|
|---|
| 162 | {
|
|---|
| 163 | AtomNo[getType()->getAtomicNumber()]++; // increment number
|
|---|
| 164 | if (out != NULL) {
|
|---|
| 165 | const element *elemental = getType();
|
|---|
| 166 | ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
|
|---|
| 167 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
|---|
| 168 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
|---|
| 169 | *out << "\t" << getFixedIon();
|
|---|
| 170 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
|---|
| 171 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
|---|
| 172 | if (comment != NULL)
|
|---|
| 173 | *out << " # " << comment << endl;
|
|---|
| 174 | else
|
|---|
| 175 | *out << " # molecule nr " << getNr() << endl;
|
|---|
| 176 | return true;
|
|---|
| 177 | } else
|
|---|
| 178 | return false;
|
|---|
| 179 | };
|
|---|
| 180 |
|
|---|
| 181 | bool atom::Compare(const atom &ptr) const
|
|---|
| 182 | {
|
|---|
| 183 | if (getNr() < ptr.getNr())
|
|---|
| 184 | return true;
|
|---|
| 185 | else
|
|---|
| 186 | return false;
|
|---|
| 187 | };
|
|---|
| 188 |
|
|---|
| 189 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
|---|
| 190 | {
|
|---|
| 191 | return DistanceSquared(origin);
|
|---|
| 192 | };
|
|---|
| 193 |
|
|---|
| 194 | double atom::DistanceToVector(const Vector &origin) const
|
|---|
| 195 | {
|
|---|
| 196 | return distance(origin);
|
|---|
| 197 | };
|
|---|
| 198 |
|
|---|
| 199 | void atom::InitComponentNr()
|
|---|
| 200 | {
|
|---|
| 201 | if (ComponentNr != NULL)
|
|---|
| 202 | delete[](ComponentNr);
|
|---|
| 203 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| 204 | ComponentNr = new int[ListOfBonds.size()+1];
|
|---|
| 205 | for (int i=ListOfBonds.size()+1;i--;)
|
|---|
| 206 | ComponentNr[i] = -1;
|
|---|
| 207 | };
|
|---|
| 208 |
|
|---|
| 209 | void atom::resetGraphNr(){
|
|---|
| 210 | GraphNr=-1;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | std::ostream & atom::operator << (std::ostream &ost) const
|
|---|
| 214 | {
|
|---|
| 215 | ParticleInfo::operator<<(ost);
|
|---|
| 216 | ost << "," << getPosition();
|
|---|
| 217 | return ost;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | std::ostream & operator << (std::ostream &ost, const atom &a)
|
|---|
| 221 | {
|
|---|
| 222 | a.ParticleInfo::operator<<(ost);
|
|---|
| 223 | ost << "," << a.getPosition();
|
|---|
| 224 | return ost;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | bool operator < (atom &a, atom &b)
|
|---|
| 228 | {
|
|---|
| 229 | return a.Compare(b);
|
|---|
| 230 | };
|
|---|
| 231 |
|
|---|
| 232 | World *atom::getWorld(){
|
|---|
| 233 | return world;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | void atom::setWorld(World* _world){
|
|---|
| 237 | world = _world;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | bool atom::changeId(atomId_t newId){
|
|---|
| 241 | // first we move ourselves in the world
|
|---|
| 242 | // the world lets us know if that succeeded
|
|---|
| 243 | if(world->changeAtomId(id,newId,this)){
|
|---|
| 244 | OBSERVE;
|
|---|
| 245 | id = newId;
|
|---|
| 246 | NOTIFY(IndexChanged);
|
|---|
| 247 | return true;
|
|---|
| 248 | }
|
|---|
| 249 | else{
|
|---|
| 250 | return false;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | void atom::setId(atomId_t _id) {
|
|---|
| 255 | id=_id;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | atomId_t atom::getId() const {
|
|---|
| 259 | return id;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | void atom::setMolecule(molecule *_mol){
|
|---|
| 263 | // take this atom from the old molecule
|
|---|
| 264 | removeFromMolecule();
|
|---|
| 265 | mol = _mol;
|
|---|
| 266 | if ((mol) && (!mol->containsAtom(this)))
|
|---|
| 267 | mol->insert(this);
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | void atom::unsetMolecule()
|
|---|
| 271 | {
|
|---|
| 272 | // take this atom from the old molecule
|
|---|
| 273 | ASSERT(!mol->containsAtom(this),
|
|---|
| 274 | "atom::unsetMolecule() - old molecule "+toString(mol)+" still contains us!");
|
|---|
| 275 | mol = NULL;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | molecule* atom::getMolecule() const {
|
|---|
| 279 | return mol;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | void atom::removeFromMolecule(){
|
|---|
| 283 | if(mol){
|
|---|
| 284 | if(mol->containsAtom(this)){
|
|---|
| 285 | mol->erase(this);
|
|---|
| 286 | }
|
|---|
| 287 | mol=0;
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | bool atom::changeNr(const int newNr)
|
|---|
| 292 | {
|
|---|
| 293 | if ((mol) && (mol->changeAtomNr(getNr(),newNr,this))) {
|
|---|
| 294 | return true;
|
|---|
| 295 | } else{
|
|---|
| 296 | return false;
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | int atom::getNr() const{
|
|---|
| 301 | return ParticleInfo::getNr();
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | atom* NewAtom(atomId_t _id){
|
|---|
| 305 | atom * res = new atom();
|
|---|
| 306 | // extent trajectory to current time step
|
|---|
| 307 | const size_t CurrentTime = WorldTime::getTime();
|
|---|
| 308 | for (size_t step = res->getTrajectorySize(); step <= CurrentTime; ++step)
|
|---|
| 309 | res->UpdateSteps();
|
|---|
| 310 | res->setId(_id);
|
|---|
| 311 | return res;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | void DeleteAtom(atom* atom){
|
|---|
| 315 | delete atom;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | bool compareAtomElements(atom* atom1,atom* atom2){
|
|---|
| 319 | return atom1->getType()->getAtomicNumber() < atom2->getType()->getAtomicNumber();
|
|---|
| 320 | }
|
|---|