1 | /** \file atom.cpp
|
---|
2 | *
|
---|
3 | * Function implementations for the class atom.
|
---|
4 | *
|
---|
5 | */
|
---|
6 |
|
---|
7 | // include config.h
|
---|
8 | #ifdef HAVE_CONFIG_H
|
---|
9 | #include <config.h>
|
---|
10 | #endif
|
---|
11 |
|
---|
12 | #include "Helpers/MemDebug.hpp"
|
---|
13 |
|
---|
14 | #include "atom.hpp"
|
---|
15 | #include "bond.hpp"
|
---|
16 | #include "config.hpp"
|
---|
17 | #include "element.hpp"
|
---|
18 | #include "lists.hpp"
|
---|
19 | #include "parser.hpp"
|
---|
20 | #include "LinearAlgebra/Vector.hpp"
|
---|
21 | #include "World.hpp"
|
---|
22 | #include "molecule.hpp"
|
---|
23 | #include "Shapes/Shape.hpp"
|
---|
24 |
|
---|
25 | #include <iomanip>
|
---|
26 | #include <iostream>
|
---|
27 |
|
---|
28 | /************************************* Functions for class atom *************************************/
|
---|
29 |
|
---|
30 |
|
---|
31 | /** Constructor of class atom.
|
---|
32 | */
|
---|
33 | atom::atom() :
|
---|
34 | father(this),
|
---|
35 | sort(&nr),
|
---|
36 | mol(0)
|
---|
37 | {};
|
---|
38 |
|
---|
39 | /** Constructor of class atom.
|
---|
40 | */
|
---|
41 | atom::atom(atom *pointer) :
|
---|
42 | ParticleInfo(pointer),
|
---|
43 | father(pointer),
|
---|
44 | sort(&nr)
|
---|
45 | {
|
---|
46 | setType(pointer->getType()); // copy element of atom
|
---|
47 | setPosition(pointer->getPosition()); // copy coordination
|
---|
48 | AtomicVelocity = pointer->AtomicVelocity; // copy velocity
|
---|
49 | FixedIon = pointer->FixedIon;
|
---|
50 | mol = 0;
|
---|
51 | };
|
---|
52 |
|
---|
53 | atom *atom::clone(){
|
---|
54 | atom *res = new atom(this);
|
---|
55 | res->father = this;
|
---|
56 | res->sort = &res->nr;
|
---|
57 | res->setType(getType());
|
---|
58 | res->setPosition(this->getPosition());
|
---|
59 | res->AtomicVelocity = this->AtomicVelocity;
|
---|
60 | res->FixedIon = FixedIon;
|
---|
61 | res->mol = 0;
|
---|
62 | World::getInstance().registerAtom(res);
|
---|
63 | return res;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /** Destructor of class atom.
|
---|
68 | */
|
---|
69 | atom::~atom()
|
---|
70 | {
|
---|
71 | removeFromMolecule();
|
---|
72 | for(BondList::iterator iter=ListOfBonds.begin(); iter!=ListOfBonds.end();){
|
---|
73 | // deleting the bond will invalidate the iterator !!!
|
---|
74 | bond *bond =*(iter++);
|
---|
75 | delete(bond);
|
---|
76 | }
|
---|
77 | };
|
---|
78 |
|
---|
79 |
|
---|
80 | /** Climbs up the father list until NULL, last is returned.
|
---|
81 | * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen)
|
---|
82 | */
|
---|
83 | atom *atom::GetTrueFather()
|
---|
84 | {
|
---|
85 | if(father == this){ // top most father is the one that points on itself
|
---|
86 | return this;
|
---|
87 | }
|
---|
88 | else if(!father) {
|
---|
89 | return 0;
|
---|
90 | }
|
---|
91 | else {
|
---|
92 | return father->GetTrueFather();
|
---|
93 | }
|
---|
94 | };
|
---|
95 |
|
---|
96 | /** Sets father to itself or its father in case of copying a molecule.
|
---|
97 | */
|
---|
98 | void atom::CorrectFather()
|
---|
99 | {
|
---|
100 | if (father->father == father) // same atom in copy's father points to itself
|
---|
101 | father = this; // set father to itself (copy of a whole molecule)
|
---|
102 | else
|
---|
103 | father = father->father; // set father to original's father
|
---|
104 |
|
---|
105 | };
|
---|
106 |
|
---|
107 | /** Check whether father is equal to given atom.
|
---|
108 | * \param *ptr atom to compare father to
|
---|
109 | * \param **res return value (only set if atom::father is equal to \a *ptr)
|
---|
110 | */
|
---|
111 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const
|
---|
112 | {
|
---|
113 | if ( ptr == father )
|
---|
114 | *res = this;
|
---|
115 | };
|
---|
116 |
|
---|
117 | bool atom::isFather(const atom *ptr){
|
---|
118 | return ptr==father;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Checks whether atom is within the given box.
|
---|
122 | * \param offset offset to box origin
|
---|
123 | * \param *parallelepiped box matrix
|
---|
124 | * \return true - is inside, false - is not
|
---|
125 | */
|
---|
126 | bool atom::IsInShape(const Shape& shape) const
|
---|
127 | {
|
---|
128 | return shape.isInside(getPosition());
|
---|
129 | };
|
---|
130 |
|
---|
131 | /** Counts the number of bonds weighted by bond::BondDegree.
|
---|
132 | * \param bonds times bond::BondDegree
|
---|
133 | */
|
---|
134 | int BondedParticle::CountBonds() const
|
---|
135 | {
|
---|
136 | int NoBonds = 0;
|
---|
137 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
138 | NoBonds += (*Runner)->BondDegree;
|
---|
139 | return NoBonds;
|
---|
140 | };
|
---|
141 |
|
---|
142 | /** Output of a single atom with given numbering.
|
---|
143 | * \param ElementNo cardinal number of the element
|
---|
144 | * \param AtomNo cardinal number among these atoms of the same element
|
---|
145 | * \param *out stream to output to
|
---|
146 | * \param *comment commentary after '#' sign
|
---|
147 | * \return true - \a *out present, false - \a *out is NULL
|
---|
148 | */
|
---|
149 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
|
---|
150 | {
|
---|
151 | if (out != NULL) {
|
---|
152 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
|
---|
153 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
---|
154 | *out << "\t" << FixedIon;
|
---|
155 | if (AtomicVelocity.Norm() > MYEPSILON)
|
---|
156 | *out << "\t" << scientific << setprecision(6) << AtomicVelocity[0] << "\t" << AtomicVelocity[1] << "\t" << AtomicVelocity[2] << "\t";
|
---|
157 | if (comment != NULL)
|
---|
158 | *out << " # " << comment << endl;
|
---|
159 | else
|
---|
160 | *out << " # molecule nr " << nr << endl;
|
---|
161 | return true;
|
---|
162 | } else
|
---|
163 | return false;
|
---|
164 | };
|
---|
165 |
|
---|
166 | /** Output of a single atom with numbering from array according to atom::type.
|
---|
167 | * \param *ElementNo cardinal number of the element
|
---|
168 | * \param *AtomNo cardinal number among these atoms of the same element
|
---|
169 | * \param *out stream to output to
|
---|
170 | * \param *comment commentary after '#' sign
|
---|
171 | * \return true - \a *out present, false - \a *out is NULL
|
---|
172 | */
|
---|
173 | bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
|
---|
174 | {
|
---|
175 | AtomNo[getType()->Z]++; // increment number
|
---|
176 | if (out != NULL) {
|
---|
177 | const element *elemental = getType();
|
---|
178 | cout << "Looking for atom with element " << *elemental << endl;
|
---|
179 | ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
|
---|
180 | *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->Z] << "\t" << fixed << setprecision(9) << showpoint;
|
---|
181 | *out << at(0) << "\t" << at(1) << "\t" << at(2);
|
---|
182 | *out << "\t" << FixedIon;
|
---|
183 | if (AtomicVelocity.Norm() > MYEPSILON)
|
---|
184 | *out << "\t" << scientific << setprecision(6) << AtomicVelocity[0] << "\t" << AtomicVelocity[1] << "\t" << AtomicVelocity[2] << "\t";
|
---|
185 | if (comment != NULL)
|
---|
186 | *out << " # " << comment << endl;
|
---|
187 | else
|
---|
188 | *out << " # molecule nr " << nr << endl;
|
---|
189 | return true;
|
---|
190 | } else
|
---|
191 | return false;
|
---|
192 | };
|
---|
193 |
|
---|
194 | /** Output of a single atom as one lin in xyz file.
|
---|
195 | * \param *out stream to output to
|
---|
196 | * \return true - \a *out present, false - \a *out is NULL
|
---|
197 | */
|
---|
198 | bool atom::OutputXYZLine(ofstream *out) const
|
---|
199 | {
|
---|
200 | if (out != NULL) {
|
---|
201 | *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl;
|
---|
202 | return true;
|
---|
203 | } else
|
---|
204 | return false;
|
---|
205 | };
|
---|
206 |
|
---|
207 | /** Output of a single atom as one lin in xyz file.
|
---|
208 | * \param *out stream to output to
|
---|
209 | * \param *ElementNo array with ion type number in the config file this atom's element shall have
|
---|
210 | * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
|
---|
211 | * \param step Trajectory time step to output
|
---|
212 | * \return true - \a *out present, false - \a *out is NULL
|
---|
213 | */
|
---|
214 | bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const
|
---|
215 | {
|
---|
216 | AtomNo[getType()->Z]++;
|
---|
217 | if (out != NULL) {
|
---|
218 | *out << "Ion_Type" << ElementNo[getType()->Z] << "_" << AtomNo[getType()->Z] << "\t" << fixed << setprecision(9) << showpoint;
|
---|
219 | *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2];
|
---|
220 | *out << "\t" << FixedIon;
|
---|
221 | if (Trajectory.U.at(step).Norm() > MYEPSILON)
|
---|
222 | *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t";
|
---|
223 | if (Trajectory.F.at(step).Norm() > MYEPSILON)
|
---|
224 | *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t";
|
---|
225 | *out << "\t# Number in molecule " << nr << endl;
|
---|
226 | return true;
|
---|
227 | } else
|
---|
228 | return false;
|
---|
229 | };
|
---|
230 |
|
---|
231 | /** Output of a single atom as one lin in xyz file.
|
---|
232 | * \param *out stream to output to
|
---|
233 | * \param step Trajectory time step to output
|
---|
234 | * \return true - \a *out present, false - \a *out is NULL
|
---|
235 | */
|
---|
236 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
|
---|
237 | {
|
---|
238 | if (out != NULL) {
|
---|
239 | *out << getType()->getSymbol() << "\t";
|
---|
240 | *out << Trajectory.R.at(step)[0] << "\t";
|
---|
241 | *out << Trajectory.R.at(step)[1] << "\t";
|
---|
242 | *out << Trajectory.R.at(step)[2] << endl;
|
---|
243 | return true;
|
---|
244 | } else
|
---|
245 | return false;
|
---|
246 | };
|
---|
247 |
|
---|
248 | /** Outputs the MPQC configuration line for this atom.
|
---|
249 | * \param *out output stream
|
---|
250 | * \param *center center of molecule subtracted from position
|
---|
251 | * \param *AtomNo pointer to atom counter that is increased by one
|
---|
252 | */
|
---|
253 | void atom::OutputMPQCLine(ostream * const out, const Vector *center, int *AtomNo = NULL) const
|
---|
254 | {
|
---|
255 | Vector recentered(getPosition());
|
---|
256 | recentered -= *center;
|
---|
257 | *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl;
|
---|
258 | if (AtomNo != NULL)
|
---|
259 | *AtomNo++;
|
---|
260 | };
|
---|
261 |
|
---|
262 | /** Compares the indices of \a this atom with a given \a ptr.
|
---|
263 | * \param ptr atom to compare index against
|
---|
264 | * \return true - this one's is smaller, false - not
|
---|
265 | */
|
---|
266 | bool atom::Compare(const atom &ptr) const
|
---|
267 | {
|
---|
268 | if (nr < ptr.nr)
|
---|
269 | return true;
|
---|
270 | else
|
---|
271 | return false;
|
---|
272 | };
|
---|
273 |
|
---|
274 | /** Returns squared distance to a given vector.
|
---|
275 | * \param origin vector to calculate distance to
|
---|
276 | * \return distance squared
|
---|
277 | */
|
---|
278 | double atom::DistanceSquaredToVector(const Vector &origin) const
|
---|
279 | {
|
---|
280 | return DistanceSquared(origin);
|
---|
281 | };
|
---|
282 |
|
---|
283 | /** Returns distance to a given vector.
|
---|
284 | * \param origin vector to calculate distance to
|
---|
285 | * \return distance
|
---|
286 | */
|
---|
287 | double atom::DistanceToVector(const Vector &origin) const
|
---|
288 | {
|
---|
289 | return distance(origin);
|
---|
290 | };
|
---|
291 |
|
---|
292 | /** Initialises the component number array.
|
---|
293 | * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1)
|
---|
294 | */
|
---|
295 | void atom::InitComponentNr()
|
---|
296 | {
|
---|
297 | if (ComponentNr != NULL)
|
---|
298 | delete[](ComponentNr);
|
---|
299 | ComponentNr = new int[ListOfBonds.size()+1];
|
---|
300 | for (int i=ListOfBonds.size()+1;i--;)
|
---|
301 | ComponentNr[i] = -1;
|
---|
302 | };
|
---|
303 |
|
---|
304 | std::ostream & atom::operator << (std::ostream &ost) const
|
---|
305 | {
|
---|
306 | ParticleInfo::operator<<(ost);
|
---|
307 | ost << "," << getPosition();
|
---|
308 | return ost;
|
---|
309 | }
|
---|
310 |
|
---|
311 | std::ostream & operator << (std::ostream &ost, const atom &a)
|
---|
312 | {
|
---|
313 | a.ParticleInfo::operator<<(ost);
|
---|
314 | ost << "," << a.getPosition();
|
---|
315 | return ost;
|
---|
316 | }
|
---|
317 |
|
---|
318 | bool operator < (atom &a, atom &b)
|
---|
319 | {
|
---|
320 | return a.Compare(b);
|
---|
321 | };
|
---|
322 |
|
---|
323 | World *atom::getWorld(){
|
---|
324 | return world;
|
---|
325 | }
|
---|
326 |
|
---|
327 | void atom::setWorld(World* _world){
|
---|
328 | world = _world;
|
---|
329 | }
|
---|
330 |
|
---|
331 | bool atom::changeId(atomId_t newId){
|
---|
332 | // first we move ourselves in the world
|
---|
333 | // the world lets us know if that succeeded
|
---|
334 | if(world->changeAtomId(id,newId,this)){
|
---|
335 | id = newId;
|
---|
336 | return true;
|
---|
337 | }
|
---|
338 | else{
|
---|
339 | return false;
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | void atom::setId(atomId_t _id) {
|
---|
344 | id=_id;
|
---|
345 | }
|
---|
346 |
|
---|
347 | atomId_t atom::getId() const {
|
---|
348 | return id;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /** Makes the atom be contained in the new molecule \a *_mol.
|
---|
352 | * Uses atom::removeFromMolecule() to delist from old molecule.
|
---|
353 | * \param *_mol pointer to new molecule
|
---|
354 | */
|
---|
355 | void atom::setMolecule(molecule *_mol){
|
---|
356 | // take this atom from the old molecule
|
---|
357 | removeFromMolecule();
|
---|
358 | mol = _mol;
|
---|
359 | if(!mol->containsAtom(this)){
|
---|
360 | mol->insert(this);
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | /** Returns pointer to the molecule which atom belongs to.
|
---|
365 | * \return containing molecule
|
---|
366 | */
|
---|
367 | molecule* atom::getMolecule() const {
|
---|
368 | return mol;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /** Erases the atom in atom::mol's list of atoms and sets it to zero.
|
---|
372 | */
|
---|
373 | void atom::removeFromMolecule(){
|
---|
374 | if(mol){
|
---|
375 | if(mol->containsAtom(this)){
|
---|
376 | mol->erase(this);
|
---|
377 | }
|
---|
378 | mol=0;
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | atom* NewAtom(atomId_t _id){
|
---|
384 | atom * res =new atom();
|
---|
385 | res->setId(_id);
|
---|
386 | return res;
|
---|
387 | }
|
---|
388 |
|
---|
389 | void DeleteAtom(atom* atom){
|
---|
390 | delete atom;
|
---|
391 | }
|
---|