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
Line 
1/** \file atom.cpp
2 *
3 * Function implementations for the class atom.
4 *
5 */
6
7#include "atom.hpp"
8#include "bond.hpp"
9#include "memoryallocator.hpp"
10
11/************************************* Functions for class atom *************************************/
12
13/** Constructor of class atom.
14 */
15atom::atom()
16{
17 father = this; // generally, father is itself
18 previous = NULL;
19 next = NULL;
20 Ancestor = NULL;
21 type = NULL;
22 sort = NULL;
23 FixedIon = 0;
24 GraphNr = -1;
25 ComponentNr = NULL;
26 IsCyclic = false;
27 SeparationVertex = false;
28 LowpointNr = -1;
29 AdaptiveOrder = 0;
30 MaxOrder = false;
31 // set LCNode::Vector to our Vector
32 node = &x;
33};
34
35/** Constructor of class atom.
36 */
37atom::atom(atom *pointer)
38{
39 Name = NULL;
40 previous = NULL;
41 next = NULL;
42 father = pointer; // generally, father is itself
43 Ancestor = NULL;
44 GraphNr = -1;
45 ComponentNr = NULL;
46 IsCyclic = false;
47 SeparationVertex = false;
48 LowpointNr = -1;
49 AdaptiveOrder = 0;
50 MaxOrder = false;
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;
57 node = &x;
58}
59
60
61/** Destructor of class atom.
62 */
63atom::~atom()
64{
65 Free<int>(&ComponentNr, "atom::~atom: *ComponentNr");
66 Free<char>(&Name, "atom::~atom: *Name");
67 Trajectory.R.clear();
68 Trajectory.U.clear();
69 Trajectory.F.clear();
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
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
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
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
122 * \param *comment commentary after '#' sign
123 * \return true - \a *out present, false - \a *out is NULL
124 */
125bool atom::Output(ofstream *out, int ElementNo, int AtomNo, const char *comment) const
126{
127 if (out != NULL) {
128 *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
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";
133 if (comment != NULL)
134 *out << " # " << comment << endl;
135 else
136 *out << " # molecule nr " << nr << endl;
137 return true;
138 } else
139 return false;
140};
141bool atom::Output(ofstream *out, int *ElementNo, int *AtomNo, const char *comment)
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;
152 else
153 *out << " # molecule nr " << nr << endl;
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
161 * \return true - \a *out present, false - \a *out is NULL
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
172/** Output of a single atom as one lin in xyz file.
173 * \param *out stream to output to
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
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
196/** Output of a single atom as one lin in xyz file.
197 * \param *out stream to output to
198 * \param step Trajectory time step to output
199 * \return true - \a *out present, false - \a *out is NULL
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
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
234ostream & operator << (ostream &ost, const atom &a)
235{
236 ost << "[" << a.Name << "|" << &a << "]";
237 return ost;
238};
239
240ostream & atom::operator << (ostream &ost)
241{
242 ost << "[" << Name << "|" << this << "]";
243 return ost;
244};
245
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
249 */
250bool atom::Compare(const atom &ptr)
251{
252 if (nr < ptr.nr)
253 return true;
254 else
255 return false;
256};
257
258bool operator < (atom &a, atom &b)
259{
260 return a.Compare(b);
261};
262
Note: See TracBrowser for help on using the repository browser.