source: molecuilder/src/atom.cpp@ 70b7aa

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

used forward declaration to untangle atom and bond declarations, molecule::CreateListOfBondsPerAtom() uses ActOnAllAtoms

  • a major problem is two classes depending on one another and we've often made constraints in order to accommodate for this. But there is an easier solution: Have forward declarations of the other class in each header file, only include the true other header file in each implementation (i.e. cpp file).
  • This was done with atom and bond class to allow for a new function atom::OutputBondOfAtom()
  • molecule::CreateListOfBondsPerAtom() uses ActOnAllAtoms with this new function.
  • Property mode set to 100755
File size: 8.0 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"
[390a2b]9#include "memoryallocator.hpp"
[c830e8e]10
[a0bcf1]11/************************************* Functions for class atom *************************************/
12
13/** Constructor of class atom.
14 */
[c830e8e]15atom::atom()
[a0bcf1]16{
[834ff3]17 father = this; // generally, father is itself
[a0bcf1]18 previous = NULL;
19 next = NULL;
20 Ancestor = NULL;
21 type = NULL;
22 sort = NULL;
[090299]23 FixedIon = 0;
[a0bcf1]24 GraphNr = -1;
25 ComponentNr = NULL;
[126934]26 IsCyclic = false;
[a0bcf1]27 SeparationVertex = false;
28 LowpointNr = -1;
[c75363]29 AdaptiveOrder = 0;
[644ba1]30 MaxOrder = false;
[834ff3]31 // set LCNode::Vector to our Vector
32 node = &x;
[a0bcf1]33};
34
[4e4940]35/** Constructor of class atom.
36 */
37atom::atom(atom *pointer)
38{
39 Name = NULL;
40 previous = NULL;
41 next = NULL;
[bc3953]42 father = pointer; // generally, father is itself
[4e4940]43 Ancestor = NULL;
[834ff3]44 GraphNr = -1;
45 ComponentNr = NULL;
46 IsCyclic = false;
47 SeparationVertex = false;
48 LowpointNr = -1;
49 AdaptiveOrder = 0;
50 MaxOrder = false;
[4e4940]51 type = pointer->type; // copy element of atom
52 x.CopyVector(&pointer->x); // copy coordination
53 v.CopyVector(&pointer->v); // copy velocity
54 FixedIon = pointer->FixedIon;
55 nr = -1;
56 sort = &nr;
[bc3953]57 node = &x;
[4e4940]58}
59
60
[a0bcf1]61/** Destructor of class atom.
62 */
[c830e8e]63atom::~atom()
[a0bcf1]64{
[8afe31]65 Free<int>(&ComponentNr, "atom::~atom: *ComponentNr");
[58808e]66 Free<char>(&Name, "atom::~atom: *Name");
[567b7f]67 Trajectory.R.clear();
68 Trajectory.U.clear();
69 Trajectory.F.clear();
[a0bcf1]70};
71
72
73/** Climbs up the father list until NULL, last is returned.
74 * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen)
75 */
76atom *atom::GetTrueFather()
77{
78 atom *walker = this;
79 do {
80 if (walker == walker->father) // top most father is the one that points on itself
81 break;
82 walker = walker->father;
83 } while (walker != NULL);
84 return walker;
85};
86
[dcbdf2]87/** Sets father to itself or its father in case of copying a molecule.
88 */
89void atom::CorrectFather()
90{
91 if (father->father == father) // same atom in copy's father points to itself
92 father = this; // set father to itself (copy of a whole molecule)
93 else
94 father = father->father; // set father to original's father
95
96};
97
98/** Check whether father is equal to given atom.
99 * \param *ptr atom to compare father to
100 * \param **res return value (only set if atom::father is equal to \a *ptr)
101 */
102void atom::EqualsFather ( atom *ptr, atom **res )
103{
104 if ( ptr == father )
105 *res = this;
106};
107
[8ffe32]108/** Checks whether atom is within the given box.
109 * \param offset offset to box origin
110 * \param *parallelepiped box matrix
111 * \return true - is inside, false - is not
112 */
113bool atom::IsInParallelepiped(Vector offset, double *parallelepiped)
114{
115 return (node->IsInParallelepiped(offset, parallelepiped));
116};
117
[a0bcf1]118/** Output of a single atom.
119 * \param ElementNo cardinal number of the element
120 * \param AtomNo cardinal number among these atoms of the same element
121 * \param *out stream to output to
[c830e8e]122 * \param *comment commentary after '#' sign
[70b7aa]123 * \return true - \a *out present, false - \a *out is NULL
[a0bcf1]124 */
[567b7f]125bool atom::Output(ofstream *out, int ElementNo, int AtomNo, const char *comment) const
[a0bcf1]126{
127 if (out != NULL) {
128 *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
[090299]129 *out << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2];
130 *out << "\t" << FixedIon;
131 if (v.Norm() > MYEPSILON)
132 *out << "\t" << scientific << setprecision(6) << v.x[0] << "\t" << v.x[1] << "\t" << v.x[2] << "\t";
[1b2aa1]133 if (comment != NULL)
134 *out << " # " << comment << endl;
[8ffe32]135 else
136 *out << " # molecule nr " << nr << endl;
137 return true;
138 } else
139 return false;
140};
[567b7f]141bool atom::Output(ofstream *out, int *ElementNo, int *AtomNo, const char *comment)
[8ffe32]142{
143 AtomNo[type->Z]++; // increment number
144 if (out != NULL) {
145 *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
146 *out << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2];
147 *out << "\t" << FixedIon;
148 if (v.Norm() > MYEPSILON)
149 *out << "\t" << scientific << setprecision(6) << v.x[0] << "\t" << v.x[1] << "\t" << v.x[2] << "\t";
150 if (comment != NULL)
151 *out << " # " << comment << endl;
[1b2aa1]152 else
153 *out << " # molecule nr " << nr << endl;
[a0bcf1]154 return true;
155 } else
156 return false;
157};
158
159/** Output of a single atom as one lin in xyz file.
160 * \param *out stream to output to
[70b7aa]161 * \return true - \a *out present, false - \a *out is NULL
[a0bcf1]162 */
163bool atom::OutputXYZLine(ofstream *out) const
164{
165 if (out != NULL) {
166 *out << type->symbol << "\t" << x.x[0] << "\t" << x.x[1] << "\t" << x.x[2] << "\t" << endl;
167 return true;
168 } else
169 return false;
170};
171
[567b7f]172/** Output of a single atom as one lin in xyz file.
173 * \param *out stream to output to
[70b7aa]174 * \param *ElementNo array with ion type number in the config file this atom's element shall have
175 * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
176 * \param step Trajectory time step to output
177 * \return true - \a *out present, false - \a *out is NULL
[567b7f]178 */
179bool atom::OutputTrajectory(ofstream *out, int *ElementNo, int *AtomNo, int step) const
180{
181 AtomNo[type->Z]++;
182 if (out != NULL) {
183 *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t" << fixed << setprecision(9) << showpoint;
184 *out << Trajectory.R.at(step).x[0] << "\t" << Trajectory.R.at(step).x[1] << "\t" << Trajectory.R.at(step).x[2];
185 *out << "\t" << FixedIon;
186 if (Trajectory.U.at(step).Norm() > MYEPSILON)
187 *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";
188 if (Trajectory.F.at(step).Norm() > MYEPSILON)
189 *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";
190 *out << "\t# Number in molecule " << nr << endl;
191 return true;
192 } else
193 return false;
194};
195
[0cd3b2]196/** Output of a single atom as one lin in xyz file.
197 * \param *out stream to output to
[70b7aa]198 * \param step Trajectory time step to output
199 * \return true - \a *out present, false - \a *out is NULL
[0cd3b2]200 */
201bool atom::OutputTrajectoryXYZ(ofstream *out, int step) const
202{
203 if (out != NULL) {
204 *out << type->symbol << "\t";
205 *out << Trajectory.R.at(step).x[0] << "\t";
206 *out << Trajectory.R.at(step).x[1] << "\t";
207 *out << Trajectory.R.at(step).x[2] << endl;
208 return true;
209 } else
210 return false;
211};
212
[70b7aa]213/** Prints all bonds of this atom from given global lists.
214 * \param *out stream to output to
215 * \param *NumberOfBondsPerAtom array with number of bonds per atomic index
216 * \param ***ListOfBondsPerAtom array per atomic index of array with pointer to bond
217 * \return true - \a *out present, false - \a *out is NULL
218 */
219bool atom::OutputBondOfAtom(ofstream *out, int *NumberOfBondsPerAtom, bond ***ListOfBondsPerAtom) const
220{
221 if (out != NULL) {
222 *out << Verbose(4) << "Atom " << Name << "/" << nr << " with " << NumberOfBondsPerAtom[nr] << " bonds: ";
223 int TotalDegree = 0;
224 for (int j=0;j<NumberOfBondsPerAtom[nr];j++) {
225 *out << *ListOfBondsPerAtom[nr][j] << "\t";
226 TotalDegree += ListOfBondsPerAtom[nr][j]->BondDegree;
227 }
228 *out << " -- TotalDegree: " << TotalDegree << endl;
229 return true;
230 } else
231 return false;
232};
233
[b65771]234ostream & operator << (ostream &ost, const atom &a)
[a0bcf1]235{
236 ost << "[" << a.Name << "|" << &a << "]";
237 return ost;
238};
239
[e0521b]240ostream & atom::operator << (ostream &ost)
241{
242 ost << "[" << Name << "|" << this << "]";
243 return ost;
244};
245
[a0bcf1]246/** Compares the indices of \a this atom with a given \a ptr.
247 * \param ptr atom to compare index against
248 * \return true - this one's is smaller, false - not
[c830e8e]249 */
[b65771]250bool atom::Compare(const atom &ptr)
[a0bcf1]251{
252 if (nr < ptr.nr)
253 return true;
254 else
255 return false;
256};
257
[c830e8e]258bool operator < (atom &a, atom &b)
[a0bcf1]259{
260 return a.Compare(b);
261};
262
Note: See TracBrowser for help on using the repository browser.