source: molecuilder/src/atom.cpp@ 94d0ad

Last change on this file since 94d0ad was 94d0ad, checked in by Frederik Heber <heber@…>, 16 years ago

Begun with ticket #38 (make const what is const).

  • basically all changes to member function that now state that they do not change member attributes.
  • in molecule_template.hpp all member functions are declared const, as we only need start and end from molecule and these are never changed (lots of overloaded templates removed thereby).
  • Vector::Distance...() and ...DistanceSquared() are const now too
  • Property mode set to 100644
File size: 8.6 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"
[c830e8e]15
[a0bcf1]16/************************************* Functions for class atom *************************************/
17
18/** Constructor of class atom.
19 */
[c830e8e]20atom::atom()
[a0bcf1]21{
[834ff3]22 father = this; // generally, father is itself
[a0bcf1]23 previous = NULL;
24 next = NULL;
[6b937bd]25 sort = &nr;
26
[834ff3]27 // set LCNode::Vector to our Vector
28 node = &x;
[a0bcf1]29};
30
[4e4940]31/** Constructor of class atom.
32 */
33atom::atom(atom *pointer)
34{
35 previous = NULL;
36 next = NULL;
[bc3953]37 father = pointer; // generally, father is itself
[6b937bd]38
[4e4940]39 type = pointer->type; // copy element of atom
40 x.CopyVector(&pointer->x); // copy coordination
41 v.CopyVector(&pointer->v); // copy velocity
42 FixedIon = pointer->FixedIon;
43 sort = &nr;
[bc3953]44 node = &x;
[94d0ad]45};
[4e4940]46
47
[a0bcf1]48/** Destructor of class atom.
49 */
[c830e8e]50atom::~atom()
[a0bcf1]51{
[872b51]52 unlink(this);
[a0bcf1]53};
54
55
56/** Climbs up the father list until NULL, last is returned.
57 * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen)
58 */
59atom *atom::GetTrueFather()
60{
61 atom *walker = this;
62 do {
63 if (walker == walker->father) // top most father is the one that points on itself
64 break;
65 walker = walker->father;
66 } while (walker != NULL);
67 return walker;
68};
69
[dcbdf2]70/** Sets father to itself or its father in case of copying a molecule.
71 */
72void atom::CorrectFather()
73{
74 if (father->father == father) // same atom in copy's father points to itself
75 father = this; // set father to itself (copy of a whole molecule)
76 else
77 father = father->father; // set father to original's father
78
79};
80
81/** Check whether father is equal to given atom.
82 * \param *ptr atom to compare father to
83 * \param **res return value (only set if atom::father is equal to \a *ptr)
84 */
[94d0ad]85void atom::EqualsFather ( const atom *ptr, const atom **res ) const
[dcbdf2]86{
87 if ( ptr == father )
88 *res = this;
89};
90
[8ffe32]91/** Checks whether atom is within the given box.
92 * \param offset offset to box origin
93 * \param *parallelepiped box matrix
94 * \return true - is inside, false - is not
95 */
[94d0ad]96bool atom::IsInParallelepiped(const Vector offset, const double *parallelepiped) const
[8ffe32]97{
98 return (node->IsInParallelepiped(offset, parallelepiped));
99};
100
[872b51]101/** Counts the number of bonds weighted by bond::BondDegree.
102 * \param bonds times bond::BondDegree
103 */
[6b937bd]104int BondedParticle::CountBonds() const
[872b51]105{
106 int NoBonds = 0;
107 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
108 NoBonds += (*Runner)->BondDegree;
109 return NoBonds;
110};
111
[94d0ad]112/** Output of a single atom with given numbering.
[a0bcf1]113 * \param ElementNo cardinal number of the element
114 * \param AtomNo cardinal number among these atoms of the same element
115 * \param *out stream to output to
[c830e8e]116 * \param *comment commentary after '#' sign
[70b7aa]117 * \return true - \a *out present, false - \a *out is NULL
[a0bcf1]118 */
[94d0ad]119bool atom::OutputIndexed(ofstream *out, const int ElementNo, const int AtomNo, const char *comment) const
[a0bcf1]120{
121 if (out != NULL) {
122 *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
[090299]123 *out << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2];
124 *out << "\t" << FixedIon;
125 if (v.Norm() > MYEPSILON)
126 *out << "\t" << scientific << setprecision(6) << v.x[0] << "\t" << v.x[1] << "\t" << v.x[2] << "\t";
[1b2aa1]127 if (comment != NULL)
128 *out << " # " << comment << endl;
[8ffe32]129 else
130 *out << " # molecule nr " << nr << endl;
131 return true;
132 } else
133 return false;
134};
[94d0ad]135
136/** Output of a single atom with numbering from array according to atom::type.
137 * \param *ElementNo cardinal number of the element
138 * \param *AtomNo cardinal number among these atoms of the same element
139 * \param *out stream to output to
140 * \param *comment commentary after '#' sign
141 * \return true - \a *out present, false - \a *out is NULL
142 */
143bool atom::OutputArrayIndexed(ofstream *out, const int *ElementNo, int *AtomNo, const char *comment) const
[8ffe32]144{
145 AtomNo[type->Z]++; // increment number
146 if (out != NULL) {
147 *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
148 *out << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2];
149 *out << "\t" << FixedIon;
150 if (v.Norm() > MYEPSILON)
151 *out << "\t" << scientific << setprecision(6) << v.x[0] << "\t" << v.x[1] << "\t" << v.x[2] << "\t";
152 if (comment != NULL)
153 *out << " # " << comment << endl;
[1b2aa1]154 else
155 *out << " # molecule nr " << nr << endl;
[a0bcf1]156 return true;
157 } else
158 return false;
159};
160
161/** Output of a single atom as one lin in xyz file.
162 * \param *out stream to output to
[70b7aa]163 * \return true - \a *out present, false - \a *out is NULL
[a0bcf1]164 */
165bool atom::OutputXYZLine(ofstream *out) const
166{
167 if (out != NULL) {
168 *out << type->symbol << "\t" << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2] << "\t" << endl;
169 return true;
170 } else
171 return false;
172};
173
[567b7f]174/** Output of a single atom as one lin in xyz file.
175 * \param *out stream to output to
[70b7aa]176 * \param *ElementNo array with ion type number in the config file this atom's element shall have
177 * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
178 * \param step Trajectory time step to output
179 * \return true - \a *out present, false - \a *out is NULL
[567b7f]180 */
[94d0ad]181bool atom::OutputTrajectory(ofstream *out, const int *ElementNo, int *AtomNo, const int step) const
[567b7f]182{
183 AtomNo[type->Z]++;
184 if (out != NULL) {
185 *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
186 *out << Trajectory.R.at(step).x[0] << "\t" << Trajectory.R.at(step).x[1] << "\t" << Trajectory.R.at(step).x[2];
187 *out << "\t" << FixedIon;
188 if (Trajectory.U.at(step).Norm() > MYEPSILON)
189 *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step).x[0] << "\t" << Trajectory.U.at(step).x[1] << "\t" << Trajectory.U.at(step).x[2] << "\t";
190 if (Trajectory.F.at(step).Norm() > MYEPSILON)
191 *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step).x[0] << "\t" << Trajectory.F.at(step).x[1] << "\t" << Trajectory.F.at(step).x[2] << "\t";
192 *out << "\t# Number in molecule " << nr << endl;
193 return true;
194 } else
195 return false;
196};
197
[0cd3b2]198/** Output of a single atom as one lin in xyz file.
199 * \param *out stream to output to
[70b7aa]200 * \param step Trajectory time step to output
201 * \return true - \a *out present, false - \a *out is NULL
[0cd3b2]202 */
[94d0ad]203bool atom::OutputTrajectoryXYZ(ofstream *out, const int step) const
[0cd3b2]204{
205 if (out != NULL) {
206 *out << type->symbol << "\t";
207 *out << Trajectory.R.at(step).x[0] << "\t";
208 *out << Trajectory.R.at(step).x[1] << "\t";
209 *out << Trajectory.R.at(step).x[2] << endl;
210 return true;
211 } else
212 return false;
213};
214
[6b937bd]215/** Outputs the MPQC configuration line for this atom.
216 * \param *out output stream
217 * \param *center center of molecule subtracted from position
218 * \param *AtomNo pointer to atom counter that is increased by one
219 */
[94d0ad]220void atom::OutputMPQCLine(ofstream *out, const Vector *center, int *AtomNo = NULL) const
[6b937bd]221{
222 *out << "\t\t" << type->symbol << " [ " << x.x[0]-center->x[0] << "\t" << x.x[1]-center->x[1] << "\t" << x.x[2]-center->x[2] << " ]" << endl;
223 if (AtomNo != NULL)
224 *AtomNo++;
225};
226
227/** Compares the indices of \a this atom with a given \a ptr.
228 * \param ptr atom to compare index against
229 * \return true - this one's is smaller, false - not
230 */
[94d0ad]231bool atom::Compare(const atom &ptr) const
[6b937bd]232{
233 if (nr < ptr.nr)
234 return true;
235 else
236 return false;
237};
238
239/** Returns squared distance to a given vector.
240 * \param origin vector to calculate distance to
241 * \return distance squared
242 */
[94d0ad]243double atom::DistanceSquaredToVector(const Vector &origin) const
[6b937bd]244{
245 return origin.DistanceSquared(&x);
246};
247
248/** Returns distance to a given vector.
249 * \param origin vector to calculate distance to
250 * \return distance
251 */
[94d0ad]252double atom::DistanceToVector(const Vector &origin) const
[6b937bd]253{
254 return origin.Distance(&x);
255};
256
257/** Initialises the component number array.
258 * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1)
259 */
260void atom::InitComponentNr()
261{
262 if (ComponentNr != NULL)
263 Free(&ComponentNr);
264 ComponentNr = Malloc<int>(ListOfBonds.size()+1, "atom::InitComponentNumbers: *ComponentNr");
265 for (int i=ListOfBonds.size()+1;i--;)
266 ComponentNr[i] = -1;
267};
268
269
270bool operator < (atom &a, atom &b)
271{
272 return a.Compare(b);
273};
274
Note: See TracBrowser for help on using the repository browser.