source: src/periodentafel.cpp@ c0f6c6

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since c0f6c6 was fb73b8, checked in by Frederik Heber <heber@…>, 16 years ago

class config, ConfigFileBuffer, periodentafel and Vector are refactored with respect to ticket #38, #4 and #39.

  • <type> * const ptr ... means the pointer itself is const (not its contents that he points at).
  • const <type> * ptr ... means the pointer's content is const.
  • "ofstream *out" ... can be changed to "ofstream * const out" (pointer is constant, not output).

Signed-off-by: Frederik Heber <heber@…>

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