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