1 | /** \file periodentafel.cpp
|
---|
2 | *
|
---|
3 | * Function implementations for the class periodentafel.
|
---|
4 | *
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "molecules.hpp"
|
---|
8 |
|
---|
9 | /************************************* Functions for class periodentafel ***************************/
|
---|
10 |
|
---|
11 | /** constructor for class periodentafel
|
---|
12 | * Initialises start and end of list and resets periodentafel::checkliste to false.
|
---|
13 | */
|
---|
14 | periodentafel::periodentafel()
|
---|
15 | {
|
---|
16 | start = new element;
|
---|
17 | end = new element;
|
---|
18 | start->previous = NULL;
|
---|
19 | start->next = end;
|
---|
20 | end->previous = start;
|
---|
21 | end->next = NULL;
|
---|
22 | };
|
---|
23 |
|
---|
24 | /** destructor for class periodentafel
|
---|
25 | * Removes every element and afterwards deletes start and end of list.
|
---|
26 | */
|
---|
27 | periodentafel::~periodentafel()
|
---|
28 | {
|
---|
29 | CleanupPeriodtable();
|
---|
30 | delete(end);
|
---|
31 | delete(start);
|
---|
32 | };
|
---|
33 |
|
---|
34 | /** Adds element to period table list
|
---|
35 | * \param *pointer element to be added
|
---|
36 | * \return true - succeeded, false - does not occur
|
---|
37 | */
|
---|
38 | bool periodentafel::AddElement(element *pointer)
|
---|
39 | {
|
---|
40 | pointer->sort = &pointer->Z;
|
---|
41 | if (pointer->Z < 1 && pointer->Z >= MAX_ELEMENTS)
|
---|
42 | cout << Verbose(0) << "Invalid Z number!\n";
|
---|
43 | return add(pointer, end);
|
---|
44 | };
|
---|
45 |
|
---|
46 | /** Removes element from list.
|
---|
47 | * \param *pointer element to be removed
|
---|
48 | * \return true - succeeded, false - element not found
|
---|
49 | */
|
---|
50 | bool periodentafel::RemoveElement(element *pointer)
|
---|
51 | {
|
---|
52 | return remove(pointer, start, end);
|
---|
53 | };
|
---|
54 |
|
---|
55 | /** Removes every element from the period table.
|
---|
56 | * \return true - succeeded, false - does not occur
|
---|
57 | */
|
---|
58 | bool periodentafel::CleanupPeriodtable()
|
---|
59 | {
|
---|
60 | return cleanup(start,end);
|
---|
61 | };
|
---|
62 |
|
---|
63 | /** Finds an element by its atomic number.
|
---|
64 | * If element is not yet in list, datas are asked and stored in database.
|
---|
65 | * \param Z atomic number
|
---|
66 | * \return pointer to element
|
---|
67 | */
|
---|
68 | element * periodentafel::FindElement(int Z)
|
---|
69 | {
|
---|
70 | element *walker = find(&Z, start,end);
|
---|
71 | if (walker == NULL) { // not found: enter and put into db
|
---|
72 | cout << Verbose(0) << "Element not found in database, please enter." << endl;
|
---|
73 | walker = new element;
|
---|
74 | cout << Verbose(0) << "Mass: " << endl;
|
---|
75 | cin >> walker->mass;
|
---|
76 | walker->Z = Z;
|
---|
77 | cout << Verbose(0) << "Atomic number: " << walker->Z << endl;
|
---|
78 | cout << Verbose(0) << "Name [max 64 chars]: " << endl;
|
---|
79 | cin >> walker->name;
|
---|
80 | cout << Verbose(0) << "Short form [max 3 chars]: " << endl;
|
---|
81 | cin >> walker->symbol;
|
---|
82 | periodentafel::AddElement(walker);
|
---|
83 | }
|
---|
84 | return(walker);
|
---|
85 | };
|
---|
86 |
|
---|
87 | /** Finds an element by its atomic number.
|
---|
88 | * If element is not yet in list, datas are asked and stored in database.
|
---|
89 | * \param shorthand chemical symbol of the element, e.g. H for hydrogene
|
---|
90 | * \return pointer to element
|
---|
91 | */
|
---|
92 | element * periodentafel::FindElement(char *shorthand) const
|
---|
93 | {
|
---|
94 | element *walker = periodentafel::start;
|
---|
95 | while (walker->next != periodentafel::end) {
|
---|
96 | walker = walker->next;
|
---|
97 | if (strncmp(walker->symbol, shorthand, 3) == 0)
|
---|
98 | return(walker);
|
---|
99 | }
|
---|
100 | return (NULL);
|
---|
101 | };
|
---|
102 |
|
---|
103 | /** Asks for element number and returns pointer to element
|
---|
104 | */
|
---|
105 | element * periodentafel::AskElement()
|
---|
106 | {
|
---|
107 | element *walker = NULL;
|
---|
108 | int Z;
|
---|
109 | do {
|
---|
110 | cout << Verbose(0) << "Atomic number Z: ";
|
---|
111 | cin >> Z;
|
---|
112 | walker = this->FindElement(Z); // give type
|
---|
113 | } while (walker == NULL);
|
---|
114 | return walker;
|
---|
115 | };
|
---|
116 |
|
---|
117 |
|
---|
118 | /** Prints period table to given stream.
|
---|
119 | * \param output stream
|
---|
120 | */
|
---|
121 | bool periodentafel::Output(ofstream *output) const
|
---|
122 | {
|
---|
123 | bool result = true;
|
---|
124 | element *walker = start;
|
---|
125 | if (output != NULL) {
|
---|
126 | while (walker->next != end) {
|
---|
127 | walker = walker->next;
|
---|
128 | result = result && walker->Output(output);
|
---|
129 | }
|
---|
130 | return result;
|
---|
131 | } else
|
---|
132 | return false;
|
---|
133 | };
|
---|
134 |
|
---|
135 | /** Prints period table to given stream.
|
---|
136 | * \param *output output stream
|
---|
137 | * \param *checkliste elements table for this molecule
|
---|
138 | */
|
---|
139 | bool periodentafel::Checkout(ofstream *output, const int *checkliste) const
|
---|
140 | {
|
---|
141 | element *walker = start;
|
---|
142 | bool result = true;
|
---|
143 | int No = 1;
|
---|
144 |
|
---|
145 | if (output != NULL) {
|
---|
146 | *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
|
---|
147 | *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
|
---|
148 | while (walker->next != end) {
|
---|
149 | walker = walker->next;
|
---|
150 | if (checkliste[walker->Z]) {
|
---|
151 | walker->No = No;
|
---|
152 | result = result && walker->Checkout(output, No++, checkliste[walker->Z]);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | return result;
|
---|
156 | } else
|
---|
157 | return false;
|
---|
158 | };
|
---|
159 |
|
---|
160 |
|
---|
161 | /** Loads element list from file.
|
---|
162 | */
|
---|
163 | bool periodentafel::LoadPeriodentafel(char *filename)
|
---|
164 | {
|
---|
165 | ifstream infile;
|
---|
166 | double tmp;
|
---|
167 | element *ptr;
|
---|
168 | bool status = true;
|
---|
169 | bool otherstatus = true;
|
---|
170 |
|
---|
171 | // fill elements DB
|
---|
172 | if (filename == NULL)
|
---|
173 | infile.open(STANDARDELEMENTSDB);
|
---|
174 | else
|
---|
175 | infile.open(filename);
|
---|
176 | if (infile != NULL) {
|
---|
177 | infile.getline(header1, MAXSTRINGSIZE);
|
---|
178 | infile.getline(header2, MAXSTRINGSIZE); // skip first two header lines
|
---|
179 | cout << "Parsed elements:";
|
---|
180 | while (!infile.eof()) {
|
---|
181 | element *neues = new element;
|
---|
182 | infile >> neues->name;
|
---|
183 | //infile >> ws;
|
---|
184 | infile >> neues->symbol;
|
---|
185 | //infile >> ws;
|
---|
186 | infile >> neues->period;
|
---|
187 | //infile >> ws;
|
---|
188 | infile >> neues->group;
|
---|
189 | //infile >> ws;
|
---|
190 | infile >> neues->block;
|
---|
191 | //infile >> ws;
|
---|
192 | infile >> neues->Z;
|
---|
193 | //infile >> ws;
|
---|
194 | infile >> neues->mass;
|
---|
195 | //infile >> ws;
|
---|
196 | infile >> neues->CovalentRadius;
|
---|
197 | //infile >> ws;
|
---|
198 | infile >> neues->VanDerWaalsRadius;
|
---|
199 | //infile >> ws;
|
---|
200 | periodentafel::AddElement(neues);
|
---|
201 | infile >> ws;
|
---|
202 | cout << " " << neues->symbol;
|
---|
203 | //neues->Output((ofstream *)&cout);
|
---|
204 | }
|
---|
205 | cout << endl;
|
---|
206 | infile.close();
|
---|
207 | infile.clear();
|
---|
208 | } else
|
---|
209 | status = false;
|
---|
210 |
|
---|
211 | // fill valence DB per element
|
---|
212 | infile.open(STANDARDVALENCEDB);
|
---|
213 | if (infile != NULL) {
|
---|
214 | while (!infile.eof()) {
|
---|
215 | infile >> tmp;
|
---|
216 | infile >> ws;
|
---|
217 | infile >> FindElement((int)tmp)->Valence;
|
---|
218 | infile >> ws;
|
---|
219 | //cout << Verbose(3) << "Element " << (int)tmp << " has " << find_element((int)tmp)->Valence << " valence electrons." << endl;
|
---|
220 | }
|
---|
221 | infile.close();
|
---|
222 | infile.clear();
|
---|
223 | } else
|
---|
224 | otherstatus = false;
|
---|
225 |
|
---|
226 | // fill valence DB per element
|
---|
227 | infile.open(STANDARDORBITALDB);
|
---|
228 | if (infile != NULL) {
|
---|
229 | while (!infile.eof()) {
|
---|
230 | infile >> tmp;
|
---|
231 | infile >> ws;
|
---|
232 | infile >> FindElement((int)tmp)->NoValenceOrbitals;
|
---|
233 | infile >> ws;
|
---|
234 | //cout << Verbose(3) << "Element " << (int)tmp << " has " << find_element((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
|
---|
235 | }
|
---|
236 | infile.close();
|
---|
237 | infile.clear();
|
---|
238 | } else
|
---|
239 | otherstatus = false;
|
---|
240 |
|
---|
241 | // fill H-BondDistance DB per element
|
---|
242 | infile.open(STANDARDHBONDDISTANCEDB);
|
---|
243 | if (infile != NULL) {
|
---|
244 | while (!infile.eof()) {
|
---|
245 | infile >> tmp;
|
---|
246 | ptr = FindElement((int)tmp);
|
---|
247 | infile >> ws;
|
---|
248 | infile >> ptr->HBondDistance[0];
|
---|
249 | infile >> ptr->HBondDistance[1];
|
---|
250 | infile >> ptr->HBondDistance[2];
|
---|
251 | infile >> ws;
|
---|
252 | //cout << Verbose(3) << "Element " << (int)tmp << " has " << find_element((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
|
---|
253 | }
|
---|
254 | infile.close();
|
---|
255 | infile.clear();
|
---|
256 | } else
|
---|
257 | otherstatus = false;
|
---|
258 |
|
---|
259 | // fill H-BondAngle DB per element
|
---|
260 | infile.open(STANDARDHBONDANGLEDB);
|
---|
261 | if (infile != NULL) {
|
---|
262 | while (!infile.eof()) {
|
---|
263 | infile >> tmp;
|
---|
264 | ptr = FindElement((int)tmp);
|
---|
265 | infile >> ws;
|
---|
266 | infile >> ptr->HBondAngle[0];
|
---|
267 | infile >> ptr->HBondAngle[1];
|
---|
268 | infile >> ptr->HBondAngle[2];
|
---|
269 | infile >> ws;
|
---|
270 | //cout << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondAngle[0] << ", " << FindElement((int)tmp)->HBondAngle[1] << ", " << FindElement((int)tmp)->HBondAngle[2] << " degrees bond angle for one, two, three connected hydrogens." << endl;
|
---|
271 | }
|
---|
272 | infile.close();
|
---|
273 | } else
|
---|
274 | otherstatus = false;
|
---|
275 |
|
---|
276 | return status;
|
---|
277 | };
|
---|
278 |
|
---|
279 | /** Stores element list to file.
|
---|
280 | */
|
---|
281 | bool periodentafel::StorePeriodentafel(char *filename) const
|
---|
282 | {
|
---|
283 | bool result = true;
|
---|
284 | ofstream f;
|
---|
285 |
|
---|
286 | if (filename == NULL)
|
---|
287 | f.open("elements.db");
|
---|
288 | else
|
---|
289 | f.open(filename);
|
---|
290 | if (f != NULL) {
|
---|
291 | f << header1 << endl;
|
---|
292 | f << header2 << endl;
|
---|
293 | element *walker = periodentafel::start;
|
---|
294 | while (walker->next != periodentafel::end) {
|
---|
295 | walker = walker->next;
|
---|
296 | result = result && walker->Output(&f);
|
---|
297 | }
|
---|
298 | f.close();
|
---|
299 | } else
|
---|
300 | result = false;
|
---|
301 | return result;
|
---|
302 | };
|
---|