/** \file periodentafel.cpp * * Function implementations for the class periodentafel. * */ using namespace std; #include #include #include "element.hpp" #include "helpers.hpp" #include "lists.hpp" #include "log.hpp" #include "periodentafel.hpp" #include "verbose.hpp" /************************************* Functions for class periodentafel ***************************/ /** constructor for class periodentafel * Initialises start and end of list and resets periodentafel::checkliste to false. */ periodentafel::periodentafel() : start(new element), end(new element) { start->previous = NULL; start->next = end; end->previous = start; end->next = NULL; }; /** destructor for class periodentafel * Removes every element and afterwards deletes start and end of list. */ periodentafel::~periodentafel() { CleanupPeriodtable(); delete(end); delete(start); }; /** Adds element to period table list * \param *pointer element to be added * \return true - succeeded, false - does not occur */ bool periodentafel::AddElement(element * const pointer) { pointer->sort = &pointer->Z; if (pointer->Z < 1 && pointer->Z >= MAX_ELEMENTS) Log() << Verbose(0) << "Invalid Z number!\n"; return add(pointer, end); }; /** Removes element from list. * \param *pointer element to be removed * \return true - succeeded, false - element not found */ bool periodentafel::RemoveElement(element * const pointer) { return remove(pointer, start, end); }; /** Removes every element from the period table. * \return true - succeeded, false - does not occur */ bool periodentafel::CleanupPeriodtable() { return cleanup(start,end); }; /** Finds an element by its atomic number. * If element is not yet in list, returns NULL. * \param Z atomic number * \return pointer to element or NULL if not found */ element * const periodentafel::FindElement(const int Z) const { element *walker = find(&Z, start,end); return(walker); }; /** Finds an element by its atomic number. * If element is not yet in list, datas are asked and stored in database. * \param shorthand chemical symbol of the element, e.g. H for hydrogene * \return pointer to element */ element * const periodentafel::FindElement(const char * const shorthand) const { element *walker = periodentafel::start; while (walker->next != periodentafel::end) { walker = walker->next; if (strncmp(walker->symbol, shorthand, 3) == 0) return(walker); } return (NULL); }; /** Asks for element number and returns pointer to element */ element * const periodentafel::AskElement() const { element *walker = NULL; int Z; do { Log() << Verbose(0) << "Atomic number Z: "; cin >> Z; walker = this->FindElement(Z); // give type } while (walker == NULL); return walker; }; /** Asks for element and if not found, presents mask to enter info. * \return pointer to either present or newly created element */ element * const periodentafel::EnterElement() { element *walker = NULL; int Z = -1; Log() << Verbose(0) << "Atomic number: " << Z << endl; cin >> Z; walker = FindElement(Z); if (walker == NULL) { Log() << Verbose(0) << "Element not found in database, please enter." << endl; walker = new element; walker->Z = Z; Log() << Verbose(0) << "Mass: " << endl; cin >> walker->mass; Log() << Verbose(0) << "Name [max 64 chars]: " << endl; cin >> walker->name; Log() << Verbose(0) << "Short form [max 3 chars]: " << endl; cin >> walker->symbol; periodentafel::AddElement(walker); } return(walker); }; /** Prints period table to given stream. * \param output stream */ bool periodentafel::Output(ofstream * const output) const { bool result = true; element *walker = start; if (output != NULL) { while (walker->next != end) { walker = walker->next; result = result && walker->Output(output); } return result; } else return false; }; /** Prints period table to given stream. * \param *output output stream * \param *checkliste elements table for this molecule */ bool periodentafel::Checkout(ofstream * const output, const int * const checkliste) const { element *walker = start; bool result = true; int No = 1; if (output != NULL) { *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl; *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl; while (walker->next != end) { walker = walker->next; if ((walker != NULL) && (walker->Z > 0) && (walker->Z < MAX_ELEMENTS) && (checkliste[walker->Z])) { walker->No = No; result = result && walker->Checkout(output, No++, checkliste[walker->Z]); } } return result; } else return false; }; /** Loads element list from file. * \param *path to to standard file names */ bool periodentafel::LoadPeriodentafel(const char *path) { ifstream infile; double tmp; element *ptr; bool status = true; bool otherstatus = true; char filename[255]; // fill elements DB strncpy(filename, path, MAXSTRINGSIZE); strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename)); infile.open(filename); if (infile != NULL) { infile.getline(header1, MAXSTRINGSIZE); infile.getline(header2, MAXSTRINGSIZE); // skip first two header lines Log() << Verbose(0) << "Parsed elements:"; while (!infile.eof()) { element *neues = new element; infile >> neues->name; //infile >> ws; infile >> neues->symbol; //infile >> ws; infile >> neues->period; //infile >> ws; infile >> neues->group; //infile >> ws; infile >> neues->block; //infile >> ws; infile >> neues->Z; //infile >> ws; infile >> neues->mass; //infile >> ws; infile >> neues->CovalentRadius; //infile >> ws; infile >> neues->VanDerWaalsRadius; //infile >> ws; infile >> ws; Log() << Verbose(0) << " " << neues->symbol; //neues->Output((ofstream *)&cout); if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS)) periodentafel::AddElement(neues); else { Log() << Verbose(0) << "Could not parse element: "; neues->Output((ofstream *)&cout); delete(neues); } } Log() << Verbose(0) << endl; infile.close(); infile.clear(); } else status = false; // fill valence DB per element strncpy(filename, path, MAXSTRINGSIZE); strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename)); infile.open(filename); if (infile != NULL) { while (!infile.eof()) { infile >> tmp; infile >> ws; infile >> FindElement((int)tmp)->Valence; infile >> ws; //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl; } infile.close(); infile.clear(); } else otherstatus = false; // fill valence DB per element strncpy(filename, path, MAXSTRINGSIZE); strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename)); infile.open(filename); if (infile != NULL) { while (!infile.eof()) { infile >> tmp; infile >> ws; infile >> FindElement((int)tmp)->NoValenceOrbitals; infile >> ws; //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl; } infile.close(); infile.clear(); } else otherstatus = false; // fill H-BondDistance DB per element strncpy(filename, path, MAXSTRINGSIZE); strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename)); infile.open(filename); if (infile != NULL) { while (!infile.eof()) { infile >> tmp; ptr = FindElement((int)tmp); infile >> ws; infile >> ptr->HBondDistance[0]; infile >> ptr->HBondDistance[1]; infile >> ptr->HBondDistance[2]; infile >> ws; //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl; } infile.close(); infile.clear(); } else otherstatus = false; // fill H-BondAngle DB per element strncpy(filename, path, MAXSTRINGSIZE); strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename)); infile.open(filename); if (infile != NULL) { while (!infile.eof()) { infile >> tmp; ptr = FindElement((int)tmp); infile >> ws; infile >> ptr->HBondAngle[0]; infile >> ptr->HBondAngle[1]; infile >> ptr->HBondAngle[2]; infile >> ws; //Log() << 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; } infile.close(); } else otherstatus = false; if (!otherstatus) eLog() << Verbose(2) << "Something went wrong while parsing the other databases!" << endl; return status; }; /** Stores element list to file. */ bool periodentafel::StorePeriodentafel(const char *path) const { bool result = true; ofstream f; char filename[MAXSTRINGSIZE]; strncpy(filename, path, MAXSTRINGSIZE); strncat(filename, "/", MAXSTRINGSIZE-strlen(filename)); strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename)); f.open(filename); if (f != NULL) { f << header1 << endl; f << header2 << endl; element *walker = periodentafel::start; while (walker->next != periodentafel::end) { walker = walker->next; result = result && walker->Output(&f); } f.close(); } else result = false; return result; };