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