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