source: src/periodentafel.cpp@ d427bd

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 d427bd was 14d4d4, checked in by Frederik Heber <heber@…>, 17 years ago

BUGFIX: If other databases could not be loaded, no error was produced, resulting in strange behaviour of the fragmentation routine.

Now an error message is produced, though we still continue. The problem was the switch in handling const char * and a huge mess in LoadPeriodenTafel() with strncat and strncpy.

  • Property mode set to 100644
File size: 9.6 KB
Line 
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 */
14periodentafel::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 */
27periodentafel::~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 */
38bool 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 */
50bool 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 */
58bool 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 */
68element * 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 */
92element * 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 */
105element * 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 */
121bool 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 */
139bool 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 ((walker != NULL) && (walker->Z > 0) && (walker->Z < MAX_ELEMENTS) && (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 * \param *path to to standard file names
163 */
164bool periodentafel::LoadPeriodentafel(char *path)
165{
166 ifstream infile;
167 double tmp;
168 element *ptr;
169 bool status = true;
170 bool otherstatus = true;
171 char *filename = new char[MAXSTRINGSIZE];
172
173 // fill elements DB
174 strncpy(filename, path, MAXSTRINGSIZE);
175 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
176 strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
177 infile.open(filename);
178 if (infile != NULL) {
179 infile.getline(header1, MAXSTRINGSIZE);
180 infile.getline(header2, MAXSTRINGSIZE); // skip first two header lines
181 cout << "Parsed elements:";
182 while (!infile.eof()) {
183 element *neues = new element;
184 infile >> neues->name;
185 //infile >> ws;
186 infile >> neues->symbol;
187 //infile >> ws;
188 infile >> neues->period;
189 //infile >> ws;
190 infile >> neues->group;
191 //infile >> ws;
192 infile >> neues->block;
193 //infile >> ws;
194 infile >> neues->Z;
195 //infile >> ws;
196 infile >> neues->mass;
197 //infile >> ws;
198 infile >> neues->CovalentRadius;
199 //infile >> ws;
200 infile >> neues->VanDerWaalsRadius;
201 //infile >> ws;
202 infile >> ws;
203 cout << " " << neues->symbol;
204 //neues->Output((ofstream *)&cout);
205 if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
206 periodentafel::AddElement(neues);
207 else {
208 cout << "Could not parse element: ";
209 neues->Output((ofstream *)&cout);
210 }
211 }
212 cout << endl;
213 infile.close();
214 infile.clear();
215 } else
216 status = false;
217
218 // fill valence DB per element
219 strncpy(filename, path, MAXSTRINGSIZE);
220 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
221 strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
222 infile.open(filename);
223 if (infile != NULL) {
224 while (!infile.eof()) {
225 infile >> tmp;
226 infile >> ws;
227 infile >> FindElement((int)tmp)->Valence;
228 infile >> ws;
229 //cout << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
230 }
231 infile.close();
232 infile.clear();
233 } else
234 otherstatus = false;
235
236 // fill valence DB per element
237 strncpy(filename, path, MAXSTRINGSIZE);
238 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
239 strncat(filename, STANDARDORBITALDB, 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)->NoValenceOrbitals;
246 infile >> ws;
247 //cout << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
248 }
249 infile.close();
250 infile.clear();
251 } else
252 otherstatus = false;
253
254 // fill H-BondDistance DB per element
255 strncpy(filename, path, MAXSTRINGSIZE);
256 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
257 strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
258 infile.open(filename);
259 if (infile != NULL) {
260 while (!infile.eof()) {
261 infile >> tmp;
262 ptr = FindElement((int)tmp);
263 infile >> ws;
264 infile >> ptr->HBondDistance[0];
265 infile >> ptr->HBondDistance[1];
266 infile >> ptr->HBondDistance[2];
267 infile >> ws;
268 //cout << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
269 }
270 infile.close();
271 infile.clear();
272 } else
273 otherstatus = false;
274
275 // fill H-BondAngle DB per element
276 strncpy(filename, path, MAXSTRINGSIZE);
277 strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
278 strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
279 infile.open(filename);
280 if (infile != NULL) {
281 while (!infile.eof()) {
282 infile >> tmp;
283 ptr = FindElement((int)tmp);
284 infile >> ws;
285 infile >> ptr->HBondAngle[0];
286 infile >> ptr->HBondAngle[1];
287 infile >> ptr->HBondAngle[2];
288 infile >> ws;
289 //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;
290 }
291 infile.close();
292 } else
293 otherstatus = false;
294
295 if (!otherstatus)
296 cerr << "ERROR: Something went wrong while parsing the databases!" << endl;
297
298 return status;
299};
300
301/** Stores element list to file.
302 */
303bool periodentafel::StorePeriodentafel(char *filename) const
304{
305 bool result = true;
306 ofstream f;
307 char file[MAXSTRINGSIZE];
308
309 if (filename == STANDARDELEMENTSDB)
310 f.open(file);
311 else
312 f.open(filename);
313 if (f != NULL) {
314 f << header1 << endl;
315 f << header2 << endl;
316 element *walker = periodentafel::start;
317 while (walker->next != periodentafel::end) {
318 walker = walker->next;
319 result = result && walker->Output(&f);
320 }
321 f.close();
322 } else
323 result = false;
324 return result;
325};
Note: See TracBrowser for help on using the repository browser.