[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[bcf653] | 21 | */
|
---|
| 22 |
|
---|
[14de469] | 23 | /** \file atom.cpp
|
---|
[1907a7] | 24 | *
|
---|
[14de469] | 25 | * Function implementations for the class atom.
|
---|
[1907a7] | 26 | *
|
---|
[14de469] | 27 | */
|
---|
| 28 |
|
---|
[bf3817] | 29 | // include config.h
|
---|
| 30 | #ifdef HAVE_CONFIG_H
|
---|
| 31 | #include <config.h>
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
[ad011c] | 34 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 35 |
|
---|
[357fba] | 36 | #include "atom.hpp"
|
---|
[708277] | 37 | #include "AtomObserver.hpp"
|
---|
[129204] | 38 | #include "Bond/bond.hpp"
|
---|
[e2373df] | 39 | #include "CodePatterns/Log.hpp"
|
---|
[4a7776a] | 40 | #include "config.hpp"
|
---|
[3bdb6d] | 41 | #include "Element/element.hpp"
|
---|
[57f243] | 42 | #include "LinearAlgebra/Vector.hpp"
|
---|
[d346b6] | 43 | #include "World.hpp"
|
---|
[6cfa36] | 44 | #include "molecule.hpp"
|
---|
[c550dd] | 45 | #include "Shapes/Shape.hpp"
|
---|
[a0064e] | 46 |
|
---|
[36166d] | 47 | #include <iomanip>
|
---|
[0ba410] | 48 | #include <iostream>
|
---|
[36166d] | 49 |
|
---|
[14de469] | 50 | /************************************* Functions for class atom *************************************/
|
---|
| 51 |
|
---|
[70ff32] | 52 |
|
---|
[46d958] | 53 | atom::atom() :
|
---|
[97b825] | 54 | father(this),
|
---|
[5309ba] | 55 | sort(&Nr),
|
---|
[97b825] | 56 | mol(0)
|
---|
[708277] | 57 | {
|
---|
| 58 | // sign on to global atom change tracker
|
---|
| 59 | AtomObserver::getInstance().AtomInserted(this);
|
---|
| 60 | }
|
---|
[14de469] | 61 |
|
---|
[ea0c8b] | 62 | atom::atom(atom *pointer) :
|
---|
| 63 | ParticleInfo(*pointer),
|
---|
[c742fe] | 64 | AtomInfo(*pointer),
|
---|
[97b825] | 65 | father(pointer),
|
---|
[5309ba] | 66 | sort(&Nr),
|
---|
[9df680] | 67 | mol(0)
|
---|
[2319ed] | 68 | {
|
---|
[443547] | 69 | AtomicPosition = pointer->AtomicPosition; // copy trajectory of coordination
|
---|
| 70 | AtomicVelocity = pointer->AtomicVelocity; // copy trajectory of velocity
|
---|
| 71 | AtomicForce = pointer->AtomicForce;
|
---|
[708277] | 72 | // sign on to global atom change tracker
|
---|
| 73 | AtomObserver::getInstance().AtomInserted(this);
|
---|
[b453f9] | 74 | };
|
---|
[2319ed] | 75 |
|
---|
[46d958] | 76 | atom *atom::clone(){
|
---|
[68f03d] | 77 | atom *res = new atom(this);
|
---|
[23b547] | 78 | World::getInstance().registerAtom(res);
|
---|
[46d958] | 79 | return res;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[2319ed] | 82 |
|
---|
[14de469] | 83 | /** Destructor of class atom.
|
---|
| 84 | */
|
---|
[1907a7] | 85 | atom::~atom()
|
---|
[14de469] | 86 | {
|
---|
[6cfa36] | 87 | removeFromMolecule();
|
---|
[708277] | 88 | // sign off from global atom change tracker
|
---|
| 89 | AtomObserver::getInstance().AtomRemoved(this);
|
---|
| 90 | }
|
---|
[e2373df] | 91 |
|
---|
| 92 |
|
---|
| 93 | void atom::UpdateSteps()
|
---|
| 94 | {
|
---|
| 95 | LOG(4,"atom::UpdateSteps() called.");
|
---|
| 96 | // append to position, velocity and force vector
|
---|
| 97 | AtomInfo::AppendTrajectoryStep();
|
---|
[1e6249] | 98 | // append to ListOfBonds vector
|
---|
| 99 | BondedParticleInfo::AppendTrajectoryStep();
|
---|
[e2373df] | 100 | }
|
---|
| 101 |
|
---|
[59fff1] | 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
|
---|
[14de469] | 109 | {
|
---|
[215df0] | 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 | }
|
---|
[14de469] | 119 | };
|
---|
| 120 |
|
---|
[e65246] | 121 | /** Sets father to itself or its father in case of copying a molecule.
|
---|
| 122 | */
|
---|
| 123 | void atom::CorrectFather()
|
---|
| 124 | {
|
---|
[2e352f] | 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
|
---|
[e65246] | 128 | father = father->father; // set father to original's father
|
---|
| 129 |
|
---|
| 130 | };
|
---|
| 131 |
|
---|
[b453f9] | 132 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
---|
[e65246] | 133 | {
|
---|
| 134 | if ( ptr == father )
|
---|
| 135 | *res = this;
|
---|
| 136 | };
|
---|
| 137 |
|
---|
[00abfc] | 138 | bool atom::isFather(const atom *ptr){
|
---|
| 139 | return ptr==father;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[e138de] | 142 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
---|
[14de469] | 143 | {
|
---|
| 144 | if (out != NULL) {
|
---|
| 145 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
---|
[d74077] | 146 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
---|
[6625c3] | 147 | *out << "\t" << (int)(getFixedIon());
|
---|
[bce72c] | 148 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
---|
| 149 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
---|
[437922] | 150 | if (comment != NULL)
|
---|
| 151 | *out << " # " << comment << endl;
|
---|
[e9f8f9] | 152 | else
|
---|
[735b1c] | 153 | *out << " # molecule nr " << getNr() << endl;
|
---|
[e9f8f9] | 154 | return true;
|
---|
| 155 | } else
|
---|
| 156 | return false;
|
---|
| 157 | };
|
---|
[b453f9] | 158 |
|
---|
[0ba410] | 159 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
|
---|
[e9f8f9] | 160 | {
|
---|
[83f176] | 161 | AtomNo[getType()->getAtomicNumber()]++; // increment number
|
---|
[e9f8f9] | 162 | if (out != NULL) {
|
---|
[8f4df1] | 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");
|
---|
[83f176] | 165 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
|
---|
[d74077] | 166 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
---|
[6625c3] | 167 | *out << "\t" << getFixedIon();
|
---|
[bce72c] | 168 | if (getAtomicVelocity().Norm() > MYEPSILON)
|
---|
| 169 | *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
|
---|
[e9f8f9] | 170 | if (comment != NULL)
|
---|
| 171 | *out << " # " << comment << endl;
|
---|
[437922] | 172 | else
|
---|
[735b1c] | 173 | *out << " # molecule nr " << getNr() << endl;
|
---|
[14de469] | 174 | return true;
|
---|
| 175 | } else
|
---|
| 176 | return false;
|
---|
| 177 | };
|
---|
| 178 |
|
---|
[b453f9] | 179 | bool atom::Compare(const atom &ptr) const
|
---|
[4455f4] | 180 | {
|
---|
[735b1c] | 181 | if (getNr() < ptr.getNr())
|
---|
[4455f4] | 182 | return true;
|
---|
| 183 | else
|
---|
| 184 | return false;
|
---|
| 185 | };
|
---|
| 186 |
|
---|
[b453f9] | 187 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
---|
[4455f4] | 188 | {
|
---|
[d74077] | 189 | return DistanceSquared(origin);
|
---|
[4455f4] | 190 | };
|
---|
| 191 |
|
---|
[b453f9] | 192 | double atom::DistanceToVector(const Vector &origin) const
|
---|
[4455f4] | 193 | {
|
---|
[d74077] | 194 | return distance(origin);
|
---|
[4455f4] | 195 | };
|
---|
| 196 |
|
---|
| 197 | void atom::InitComponentNr()
|
---|
| 198 | {
|
---|
| 199 | if (ComponentNr != NULL)
|
---|
[920c70] | 200 | delete[](ComponentNr);
|
---|
[9d83b6] | 201 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[920c70] | 202 | ComponentNr = new int[ListOfBonds.size()+1];
|
---|
[4455f4] | 203 | for (int i=ListOfBonds.size()+1;i--;)
|
---|
| 204 | ComponentNr[i] = -1;
|
---|
[14b65e] | 205 | };
|
---|
| 206 |
|
---|
| 207 | void atom::resetGraphNr(){
|
---|
| 208 | GraphNr=-1;
|
---|
| 209 | }
|
---|
[4455f4] | 210 |
|
---|
[d74077] | 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 | }
|
---|
[4455f4] | 224 |
|
---|
| 225 | bool operator < (atom &a, atom &b)
|
---|
| 226 | {
|
---|
| 227 | return a.Compare(b);
|
---|
| 228 | };
|
---|
| 229 |
|
---|
[46d958] | 230 | World *atom::getWorld(){
|
---|
| 231 | return world;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | void atom::setWorld(World* _world){
|
---|
| 235 | world = _world;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[88d586] | 238 | bool atom::changeId(atomId_t newId){
|
---|
| 239 | // first we move ourselves in the world
|
---|
| 240 | // the world lets us know if that succeeded
|
---|
[4f7f0bf] | 241 | if(world->changeAtomId(id,newId,this)){
|
---|
| 242 | OBSERVE;
|
---|
| 243 | id = newId;
|
---|
| 244 | NOTIFY(IndexChanged);
|
---|
[88d586] | 245 | return true;
|
---|
| 246 | }
|
---|
| 247 | else{
|
---|
| 248 | return false;
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | void atom::setId(atomId_t _id) {
|
---|
[46d958] | 253 | id=_id;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[ad2b411] | 256 | atomId_t atom::getId() const {
|
---|
[46d958] | 257 | return id;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[6cfa36] | 260 | void atom::setMolecule(molecule *_mol){
|
---|
| 261 | // take this atom from the old molecule
|
---|
| 262 | removeFromMolecule();
|
---|
[3867a7] | 263 | mol = _mol;
|
---|
| 264 | if ((mol) && (!mol->containsAtom(this)))
|
---|
[dddbfe] | 265 | mol->insert(this);
|
---|
[6cfa36] | 266 | }
|
---|
| 267 |
|
---|
[0d9546] | 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 |
|
---|
[e41c48] | 276 | molecule* atom::getMolecule() const {
|
---|
[c084cc] | 277 | return mol;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[6cfa36] | 280 | void atom::removeFromMolecule(){
|
---|
| 281 | if(mol){
|
---|
| 282 | if(mol->containsAtom(this)){
|
---|
| 283 | mol->erase(this);
|
---|
| 284 | }
|
---|
| 285 | mol=0;
|
---|
| 286 | }
|
---|
[1f8337] | 287 | }
|
---|
| 288 |
|
---|
[560bbe] | 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 |
|
---|
[e8a21f] | 298 | int atom::getNr() const{
|
---|
[735b1c] | 299 | return ParticleInfo::getNr();
|
---|
[e8a21f] | 300 | }
|
---|
[6cfa36] | 301 |
|
---|
[88d586] | 302 | atom* NewAtom(atomId_t _id){
|
---|
| 303 | atom * res =new atom();
|
---|
| 304 | res->setId(_id);
|
---|
| 305 | return res;
|
---|
[46d958] | 306 | }
|
---|
| 307 |
|
---|
[88d586] | 308 | void DeleteAtom(atom* atom){
|
---|
[46d958] | 309 | delete atom;
|
---|
[e5f64de] | 310 | }
|
---|
| 311 |
|
---|
| 312 | bool compareAtomElements(atom* atom1,atom* atom2){
|
---|
[ed26ae] | 313 | return atom1->getType()->getAtomicNumber() < atom2->getType()->getAtomicNumber();
|
---|
[46d958] | 314 | }
|
---|