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