source: molecuilder/src/atom.cpp@ 1f591b

Last change on this file since 1f591b was 1f591b, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Prepared interface of Vector Class for transition to VectorComposites

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[a0bcf1]1/** \file atom.cpp
[c830e8e]2 *
[a0bcf1]3 * Function implementations for the class atom.
[c830e8e]4 *
[a0bcf1]5 */
6
[834ff3]7#include "atom.hpp"
[70b7aa]8#include "bond.hpp"
[7c2f6b]9#include "config.hpp"
[17b3a5c]10#include "element.hpp"
[872b51]11#include "lists.hpp"
[390a2b]12#include "memoryallocator.hpp"
[104cf4]13#include "parser.hpp"
[17b3a5c]14#include "vector.hpp"
[d2d8f5]15#include "World.hpp"
[c830e8e]16
[a0bcf1]17/************************************* Functions for class atom *************************************/
18
[8b6ee4]19
[a0bcf1]20/** Constructor of class atom.
21 */
[7bfc19]22atom::atom() :
23 previous(NULL), next(NULL), father(this), sort(&nr)
[a0bcf1]24{
[8b6ee4]25 node = &x; // TesselPoint::x can only be referenced from here
[a0bcf1]26};
27
[4e4940]28/** Constructor of class atom.
29 */
[7bfc19]30atom::atom(atom *pointer) :
31 ParticleInfo(pointer),
32 previous(NULL), next(NULL), father(pointer), sort(&nr)
[4e4940]33{
34 type = pointer->type; // copy element of atom
[1f591b]35 x = pointer->x; // copy coordination
36 v = pointer->v; // copy velocity
[4e4940]37 FixedIon = pointer->FixedIon;
[bc3953]38 node = &x;
[94d0ad]39};
[4e4940]40
[7bfc19]41atom *atom::clone(){
42 atom *res = new atom();
43 res->previous=0;
44 res->next=0;
45 res->father = this;
46 res->sort = &nr;
47 res->type = type;
[1f591b]48 res->x = this->x;
49 res->v = this->v;
[7bfc19]50 res->FixedIon = FixedIon;
51 res->node = &x;
[4c60ef]52 World::getInstance().registerAtom(res);
[7bfc19]53 return res;
54}
55
[4e4940]56
[a0bcf1]57/** Destructor of class atom.
58 */
[c830e8e]59atom::~atom()
[a0bcf1]60{
[872b51]61 unlink(this);
[a0bcf1]62};
63
64
65/** Climbs up the father list until NULL, last is returned.
66 * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen)
67 */
68atom *atom::GetTrueFather()
69{
70 atom *walker = this;
71 do {
72 if (walker == walker->father) // top most father is the one that points on itself
73 break;
74 walker = walker->father;
75 } while (walker != NULL);
76 return walker;
77};
78
[dcbdf2]79/** Sets father to itself or its father in case of copying a molecule.
80 */
81void atom::CorrectFather()
82{
83 if (father->father == father) // same atom in copy's father points to itself
84 father = this; // set father to itself (copy of a whole molecule)
85 else
86 father = father->father; // set father to original's father
87
88};
89
90/** Check whether father is equal to given atom.
91 * \param *ptr atom to compare father to
92 * \param **res return value (only set if atom::father is equal to \a *ptr)
93 */
[94d0ad]94void atom::EqualsFather ( const atom *ptr, const atom **res ) const
[dcbdf2]95{
96 if ( ptr == father )
97 *res = this;
98};
99
[8ffe32]100/** Checks whether atom is within the given box.
101 * \param offset offset to box origin
102 * \param *parallelepiped box matrix
103 * \return true - is inside, false - is not
104 */
[94d0ad]105bool atom::IsInParallelepiped(const Vector offset, const double *parallelepiped) const
[8ffe32]106{
107 return (node->IsInParallelepiped(offset, parallelepiped));
108};
109
[872b51]110/** Counts the number of bonds weighted by bond::BondDegree.
111 * \param bonds times bond::BondDegree
112 */
[6b937bd]113int BondedParticle::CountBonds() const
[872b51]114{
115 int NoBonds = 0;
116 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
117 NoBonds += (*Runner)->BondDegree;
118 return NoBonds;
119};
120
[94d0ad]121/** Output of a single atom with given numbering.
[a0bcf1]122 * \param ElementNo cardinal number of the element
123 * \param AtomNo cardinal number among these atoms of the same element
124 * \param *out stream to output to
[c830e8e]125 * \param *comment commentary after '#' sign
[70b7aa]126 * \return true - \a *out present, false - \a *out is NULL
[a0bcf1]127 */
[543ce4]128bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
[a0bcf1]129{
130 if (out != NULL) {
131 *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
[71910a]132 *out << x[0] << "\t" << x[1] << "\t" << x[2];
[090299]133 *out << "\t" << FixedIon;
134 if (v.Norm() > MYEPSILON)
[71910a]135 *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t";
[1b2aa1]136 if (comment != NULL)
137 *out << " # " << comment << endl;
[8ffe32]138 else
139 *out << " # molecule nr " << nr << endl;
140 return true;
141 } else
142 return false;
143};
[94d0ad]144
145/** Output of a single atom with numbering from array according to atom::type.
146 * \param *ElementNo cardinal number of the element
147 * \param *AtomNo cardinal number among these atoms of the same element
148 * \param *out stream to output to
149 * \param *comment commentary after '#' sign
150 * \return true - \a *out present, false - \a *out is NULL
151 */
[543ce4]152bool atom::OutputArrayIndexed(ofstream * const out, const int *ElementNo, int *AtomNo, const char *comment) const
[8ffe32]153{
154 AtomNo[type->Z]++; // increment number
155 if (out != NULL) {
156 *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
[71910a]157 *out << x[0] << "\t" << x[1] << "\t" << x[2];
[8ffe32]158 *out << "\t" << FixedIon;
159 if (v.Norm() > MYEPSILON)
[71910a]160 *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t";
[8ffe32]161 if (comment != NULL)
162 *out << " # " << comment << endl;
[1b2aa1]163 else
164 *out << " # molecule nr " << nr << endl;
[a0bcf1]165 return true;
166 } else
167 return false;
168};
169
170/** Output of a single atom as one lin in xyz file.
171 * \param *out stream to output to
[70b7aa]172 * \return true - \a *out present, false - \a *out is NULL
[a0bcf1]173 */
174bool atom::OutputXYZLine(ofstream *out) const
175{
176 if (out != NULL) {
[71910a]177 *out << type->symbol << "\t" << x[0] << "\t" << x[1] << "\t" << x[2] << "\t" << endl;
[a0bcf1]178 return true;
179 } else
180 return false;
181};
182
[567b7f]183/** Output of a single atom as one lin in xyz file.
184 * \param *out stream to output to
[70b7aa]185 * \param *ElementNo array with ion type number in the config file this atom's element shall have
186 * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
187 * \param step Trajectory time step to output
188 * \return true - \a *out present, false - \a *out is NULL
[567b7f]189 */
[543ce4]190bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const
[567b7f]191{
192 AtomNo[type->Z]++;
193 if (out != NULL) {
194 *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
[71910a]195 *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2];
[567b7f]196 *out << "\t" << FixedIon;
197 if (Trajectory.U.at(step).Norm() > MYEPSILON)
[71910a]198 *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t";
[567b7f]199 if (Trajectory.F.at(step).Norm() > MYEPSILON)
[71910a]200 *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t";
[567b7f]201 *out << "\t# Number in molecule " << nr << endl;
202 return true;
203 } else
204 return false;
205};
206
[0cd3b2]207/** Output of a single atom as one lin in xyz file.
208 * \param *out stream to output to
[70b7aa]209 * \param step Trajectory time step to output
210 * \return true - \a *out present, false - \a *out is NULL
[0cd3b2]211 */
[543ce4]212bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
[0cd3b2]213{
214 if (out != NULL) {
215 *out << type->symbol << "\t";
[71910a]216 *out << Trajectory.R.at(step)[0] << "\t";
217 *out << Trajectory.R.at(step)[1] << "\t";
218 *out << Trajectory.R.at(step)[2] << endl;
[0cd3b2]219 return true;
220 } else
221 return false;
222};
223
[6b937bd]224/** Outputs the MPQC configuration line for this atom.
225 * \param *out output stream
226 * \param *center center of molecule subtracted from position
227 * \param *AtomNo pointer to atom counter that is increased by one
228 */
[543ce4]229void atom::OutputMPQCLine(ofstream * const out, const Vector *center, int *AtomNo = NULL) const
[6b937bd]230{
[71910a]231 *out << "\t\t" << type->symbol << " [ " << x[0]-center->at(0) << "\t" << x[1]-center->at(1) << "\t" << x[2]-center->at(2) << " ]" << endl;
[6b937bd]232 if (AtomNo != NULL)
233 *AtomNo++;
234};
235
236/** Compares the indices of \a this atom with a given \a ptr.
237 * \param ptr atom to compare index against
238 * \return true - this one's is smaller, false - not
239 */
[94d0ad]240bool atom::Compare(const atom &ptr) const
[6b937bd]241{
242 if (nr < ptr.nr)
243 return true;
244 else
245 return false;
246};
247
248/** Returns squared distance to a given vector.
249 * \param origin vector to calculate distance to
250 * \return distance squared
251 */
[94d0ad]252double atom::DistanceSquaredToVector(const Vector &origin) const
[6b937bd]253{
[1f591b]254 return origin.DistanceSquared(x);
[6b937bd]255};
256
257/** Returns distance to a given vector.
258 * \param origin vector to calculate distance to
259 * \return distance
260 */
[94d0ad]261double atom::DistanceToVector(const Vector &origin) const
[6b937bd]262{
[1f591b]263 return origin.Distance(x);
[6b937bd]264};
265
266/** Initialises the component number array.
267 * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1)
268 */
269void atom::InitComponentNr()
270{
271 if (ComponentNr != NULL)
272 Free(&ComponentNr);
273 ComponentNr = Malloc<int>(ListOfBonds.size()+1, "atom::InitComponentNumbers: *ComponentNr");
274 for (int i=ListOfBonds.size()+1;i--;)
275 ComponentNr[i] = -1;
276};
277
278
279bool operator < (atom &a, atom &b)
280{
281 return a.Compare(b);
282};
283
[7bfc19]284World *atom::getWorld(){
285 return world;
286}
287
288void atom::setWorld(World* _world){
289 world = _world;
290}
291
[3746aeb]292bool atom::changeId(atomId_t newId){
293 // first we move ourselves in the world
294 // the world lets us know if that succeeded
295 if(world->changeAtomId(id,newId,this)){
296 id = newId;
297 return true;
298 }
299 else{
300 return false;
301 }
302}
303
304void atom::setId(atomId_t _id) {
[7bfc19]305 id=_id;
306}
307
[f058ef]308atomId_t atom::getId() {
[7bfc19]309 return id;
310}
311
[3746aeb]312atom* NewAtom(atomId_t _id){
313 atom * res =new atom();
314 res->setId(_id);
315 return res;
[7bfc19]316}
317
[3746aeb]318void DeleteAtom(atom* atom){
[7bfc19]319 delete atom;
320}
Note: See TracBrowser for help on using the repository browser.