Ignore:
Timestamp:
May 18, 2010, 3:20:52 PM (15 years ago)
Author:
Frederik Heber <heber@…>
Branches:
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
Children:
673c7f, b0e9c9
Parents:
5079a0
Message:

"-e <db path>" not necessary anymore.

Removed necessity of specifying path to databases (this was one check of molecuilder/test/testsuite.at which cannot be fulfilled anymore with boost::program_options)
For this to work a great number of small changes have been necessary:

class periodentafel:

  • all .db files merged into const char * arrays in elements_db.cpp
  • periodentafel rewritten:
  • FindElement(), AskElement() and EnterElement return element * const instead of const element * (i.e. the contents of the pointer is const (the element) not the pointer itself which is very vexatious (i.e. FindElement() yields const element * which can subsequently not be used for RemoveElement(), ...)
  • parsedElems is not needed anymore. Instead we operate on map elements directly
  • new unittest periodentafelTest which is made friend of periodentafel to be able to access private loading functions directly

A number of unit tests had to be changed (all that create elements during setUp() which is now unnecessary)

Some of the analysis_bonds function's signatures were changed in the process:

Finally, the respective tests are removed from molecuilder/tests/testsuite.at.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/periodentafel.cpp

    r5079a0 r4eb4fe  
    88
    99#include <iomanip>
     10#include <iostream>
    1011#include <fstream>
    1112#include <cstring>
    12 #include <cassert>
    13 
     13
     14#include "Helpers/Assert.hpp"
    1415#include "element.hpp"
     16#include "elements_db.hpp"
    1517#include "helpers.hpp"
    1618#include "lists.hpp"
     
    2729 */
    2830periodentafel::periodentafel()
    29 {};
     31{
     32  ASSERT(LoadElementsDatabase(new stringstream(elementsDB,ios_base::in)), "General element initialization failed");
     33  ASSERT(LoadValenceDatabase(new stringstream(valenceDB,ios_base::in)), "Valence entry of element initialization failed");
     34  ASSERT(LoadOrbitalsDatabase(new stringstream(orbitalsDB,ios_base::in)), "Orbitals entry of element initialization failed");
     35  ASSERT(LoadHBondAngleDatabase(new stringstream(HbondangleDB,ios_base::in)), "HBond angle entry of element initialization failed");
     36  ASSERT(LoadHBondLengthsDatabase(new stringstream(HbonddistanceDB,ios_base::in)), "HBond distance entry of element initialization failed");
     37};
    3038
    3139/** destructor for class periodentafel
     
    3947/** Adds element to period table list
    4048 * \param *pointer element to be added
    41  * \return true - succeeded, false - does not occur
     49 * \return iterator to added element
    4250 */
    4351periodentafel::iterator periodentafel::AddElement(element * const pointer)
    4452{
    4553  atomicNumber_t Z = pointer->getNumber();
    46   assert(!elements.count(Z));
     54  ASSERT(!elements.count(Z), "Element is already present.");
    4755  pointer->sort = &pointer->Z;
    4856  if (pointer->getNumber() < 1 && pointer->getNumber() >= MAX_ELEMENTS)
     
    5462/** Removes element from list.
    5563 * \param *pointer element to be removed
    56  * \return true - succeeded, false - element not found
    5764 */
    5865void periodentafel::RemoveElement(element * const pointer)
    5966{
    60   atomicNumber_t Z = pointer->getNumber();
     67  RemoveElement(pointer->getNumber());
     68};
     69
     70/** Removes element from list.
     71 * \param Z element to be removed
     72 */
     73void periodentafel::RemoveElement(atomicNumber_t Z)
     74{
    6175  elements.erase(Z);
    6276};
    6377
    6478/** Removes every element from the period table.
    65  * \return true - succeeded, false - does not occur
    6679 */
    6780void periodentafel::CleanupPeriodtable()
     
    7891 * \return pointer to element or NULL if not found
    7992 */
    80 const element * periodentafel::FindElement(atomicNumber_t Z) const
     93element * const periodentafel::FindElement(atomicNumber_t Z) const
    8194{
    8295  const_iterator res = elements.find(Z);
     
    89102 * \return pointer to element
    90103 */
    91 const element * periodentafel::FindElement(const char * const shorthand) const
     104element * const periodentafel::FindElement(const char * const shorthand) const
    92105{
    93106  element *res = 0;
     
    102115
    103116/** Asks for element number and returns pointer to element
    104  */
    105 const element * periodentafel::AskElement() const
    106 {
    107   const element *walker = NULL;
     117 * \return desired element or NULL
     118 */
     119element * const periodentafel::AskElement() const
     120{
     121  element * walker = NULL;
    108122  int Z;
    109123  do {
     
    118132 * \return pointer to either present or newly created element
    119133 */
    120 const element * periodentafel::EnterElement()
    121 {
    122   const element *res = NULL;
     134element * const periodentafel::EnterElement()
     135{
    123136  atomicNumber_t Z = 0;
    124137  DoLog(0) && (Log() << Verbose(0) << "Atomic number: " << Z << endl);
    125138  cin >> Z;
    126   res = FindElement(Z);
     139  element * const res = FindElement(Z);
    127140  if (!res) {
    128141    // TODO: make this using the constructor
    129     element *tmp;
    130142    DoLog(0) && (Log() << Verbose(0) << "Element not found in database, please enter." << endl);
    131     tmp = new element;
     143    element *tmp = new element;
    132144    tmp->Z = Z;
    133145    DoLog(0) && (Log() << Verbose(0) << "Mass: " << endl);
     
    138150    cin >> tmp->symbol;
    139151    AddElement(tmp);
    140     res = tmp;
     152    return tmp;
    141153  }
    142154  return res;
     
    204216bool periodentafel::LoadPeriodentafel(const char *path)
    205217{
    206   ifstream infile;
    207   element *ptr;
    208   map<atomicNumber_t,element*> parsedElems;
     218  ifstream input;
    209219  bool status = true;
    210220  bool otherstatus = true;
     
    215225  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    216226  strncat(filename, STANDARDELEMENTSDB, MAXSTRINGSIZE-strlen(filename));
    217   infile.open(filename);
    218   if (infile != NULL) {
    219     infile.getline(header1, MAXSTRINGSIZE);
    220     infile.getline(header2, MAXSTRINGSIZE); // skip first two header lines
     227  input.open(filename);
     228  status = status && LoadElementsDatabase(&input);
     229
     230  // fill valence DB per element
     231  strncpy(filename, path, MAXSTRINGSIZE);
     232  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
     233  strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
     234  input.open(filename);
     235  otherstatus = otherstatus && LoadValenceDatabase(&input);
     236
     237  // fill orbitals DB per element
     238  strncpy(filename, path, MAXSTRINGSIZE);
     239  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
     240  strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
     241  input.open(filename);
     242  otherstatus = otherstatus && LoadOrbitalsDatabase(&input);
     243
     244  // fill H-BondAngle DB per element
     245  strncpy(filename, path, MAXSTRINGSIZE);
     246  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
     247  strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
     248  input.open(filename);
     249  otherstatus = otherstatus && LoadHBondAngleDatabase(&input);
     250
     251  // fill H-BondDistance DB per element
     252  strncpy(filename, path, MAXSTRINGSIZE);
     253  strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
     254  strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
     255  input.open(filename);
     256  otherstatus = otherstatus && LoadHBondLengthsDatabase(&input);
     257
     258  if (!otherstatus){
     259    DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
     260  }
     261
     262  return status;
     263};
     264
     265/** load the element info.
     266 * \param *input stream to parse from
     267 * \return true - parsing successful, false - something went wrong
     268 */
     269bool periodentafel::LoadElementsDatabase(istream *input)
     270{
     271  if ((*input) != NULL) {
     272    (*input).getline(header1, MAXSTRINGSIZE);
     273    (*input).getline(header2, MAXSTRINGSIZE); // skip first two header lines
    221274    DoLog(0) && (Log() << Verbose(0) <<  "Parsed elements:");
    222     while (!infile.eof()) {
     275    while (!(*input).eof()) {
    223276      element *neues = new element;
    224       infile >> neues->name;
    225       //infile >> ws;
    226       infile >> neues->symbol;
    227       //infile >> ws;
    228       infile >> neues->period;
    229       //infile >> ws;
    230       infile >> neues->group;
    231       //infile >> ws;
    232       infile >> neues->block;
    233       //infile >> ws;
    234       infile >> neues->Z;
    235       //infile >> ws;
    236       infile >> neues->mass;
    237       //infile >> ws;
    238       infile >> neues->CovalentRadius;
    239       //infile >> ws;
    240       infile >> neues->VanDerWaalsRadius;
    241       //infile >> ws;
    242       infile >> ws;
     277      (*input) >> neues->name;
     278      //(*input) >> ws;
     279      (*input) >> neues->symbol;
     280      //(*input) >> ws;
     281      (*input) >> neues->period;
     282      //(*input) >> ws;
     283      (*input) >> neues->group;
     284      //(*input) >> ws;
     285      (*input) >> neues->block;
     286      //(*input) >> ws;
     287      (*input) >> neues->Z;
     288      //(*input) >> ws;
     289      (*input) >> neues->mass;
     290      //(*input) >> ws;
     291      (*input) >> neues->CovalentRadius;
     292      //(*input) >> ws;
     293      (*input) >> neues->VanDerWaalsRadius;
     294      //(*input) >> ws;
     295      (*input) >> ws;
    243296      DoLog(0) && (Log() << Verbose(0) << " " << neues->symbol);
     297      if (elements.count(neues->Z)) {// if element already present, remove and delete it
     298        element * const Elemental = FindElement(neues->Z);
     299        ASSERT(Elemental != NULL, "element should be present but is not??");
     300        RemoveElement(Elemental);
     301        delete(Elemental);
     302      }
    244303      //neues->Output((ofstream *)&cout);
    245304      if ((neues->Z > 0) && (neues->Z < MAX_ELEMENTS))
    246         parsedElems[neues->getNumber()] = neues;
     305        elements[neues->getNumber()] = neues;
    247306      else {
    248307        DoLog(0) && (Log() << Verbose(0) << "Could not parse element: ");
     
    252311    }
    253312    DoLog(0) && (Log() << Verbose(0) << endl);
    254     infile.close();
    255     infile.clear();
    256   } else
    257     status = false;
    258 
    259   // fill valence DB per element
    260   strncpy(filename, path, MAXSTRINGSIZE);
    261   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    262   strncat(filename, STANDARDVALENCEDB, MAXSTRINGSIZE-strlen(filename));
    263   infile.open(filename);
    264   if (infile != NULL) {
    265     while (!infile.eof()) {
     313    return true;
     314  } else
     315    return false;
     316}
     317
     318/** load the valence info.
     319 * \param *input stream to parse from
     320 * \return true - parsing successful, false - something went wrong
     321 */
     322bool periodentafel::LoadValenceDatabase(istream *input)
     323{
     324  char dummy[MAXSTRINGSIZE];
     325  if ((*input) != NULL) {
     326    (*input).getline(dummy, MAXSTRINGSIZE);
     327    while (!(*input).eof()) {
    266328      atomicNumber_t Z;
    267       infile >> Z;
    268       infile >> ws;
    269       infile >> parsedElems[Z]->Valence;
    270       infile >> ws;
     329      (*input) >> Z;
     330      ASSERT(elements.count(Z), "Element not present");
     331      (*input) >> ws;
     332      (*input) >> elements[Z]->Valence;
     333      (*input) >> ws;
    271334      //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->Valence << " valence electrons." << endl;
    272335    }
    273     infile.close();
    274     infile.clear();
    275   } else
    276     otherstatus = false;
    277 
    278   // fill valence DB per element
    279   strncpy(filename, path, MAXSTRINGSIZE);
    280   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    281   strncat(filename, STANDARDORBITALDB, MAXSTRINGSIZE-strlen(filename));
    282   infile.open(filename);
    283   if (infile != NULL) {
    284     while (!infile.eof()) {
     336    return true;
     337  } else
     338                return false;
     339}
     340
     341/** load the orbitals info.
     342 * \param *input stream to parse from
     343 * \return true - parsing successful, false - something went wrong
     344 */
     345bool periodentafel::LoadOrbitalsDatabase(istream *input)
     346{
     347  char dummy[MAXSTRINGSIZE];
     348  if ((*input) != NULL) {
     349    (*input).getline(dummy, MAXSTRINGSIZE);
     350    while (!(*input).eof()) {
    285351      atomicNumber_t Z;
    286       infile >> Z;
    287       infile >> ws;
    288       infile >> parsedElems[Z]->NoValenceOrbitals;
    289       infile >> ws;
     352      (*input) >> Z;
     353      ASSERT(elements.count(Z), "Element not present");
     354      (*input) >> ws;
     355      (*input) >> elements[Z]->NoValenceOrbitals;
     356      (*input) >> ws;
    290357      //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->NoValenceOrbitals << " number of singly occupied valence orbitals." << endl;
    291358    }
    292     infile.close();
    293     infile.clear();
    294   } else
    295     otherstatus = false;
    296 
    297   // fill H-BondDistance DB per element
    298   strncpy(filename, path, MAXSTRINGSIZE);
    299   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    300   strncat(filename, STANDARDHBONDDISTANCEDB, MAXSTRINGSIZE-strlen(filename));
    301   infile.open(filename);
    302   if (infile != NULL) {
    303     while (!infile.eof()) {
     359    return true;
     360  } else
     361    return false;
     362}
     363
     364/** load the hbond angles info.
     365 * \param *input stream to parse from
     366 * \return true - parsing successful, false - something went wrong
     367 */
     368bool periodentafel::LoadHBondAngleDatabase(istream *input)
     369{
     370  char dummy[MAXSTRINGSIZE];
     371  if ((*input) != NULL) {
     372    (*input).getline(dummy, MAXSTRINGSIZE);
     373    while (!(*input).eof()) {
    304374      atomicNumber_t Z;
    305       infile >> Z;
    306       ptr = parsedElems[Z];
    307       infile >> ws;
    308       infile >> ptr->HBondDistance[0];
    309       infile >> ptr->HBondDistance[1];
    310       infile >> ptr->HBondDistance[2];
    311       infile >> ws;
     375      (*input) >> Z;
     376      ASSERT(elements.count(Z), "Element not present");
     377      (*input) >> ws;
     378      (*input) >> elements[Z]->HBondAngle[0];
     379      (*input) >> elements[Z]->HBondAngle[1];
     380      (*input) >> elements[Z]->HBondAngle[2];
     381      (*input) >> ws;
     382      //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;
     383    }
     384    return true;
     385  } else
     386                return false;
     387}
     388
     389/** load the hbond lengths info.
     390 * \param *input stream to parse from
     391 * \return true - parsing successful, false - something went wrong
     392 */
     393bool periodentafel::LoadHBondLengthsDatabase(istream *input)
     394{
     395  char dummy[MAXSTRINGSIZE];
     396  if ((*input) != NULL) {
     397    (*input).getline(dummy, MAXSTRINGSIZE);
     398    while (!(*input).eof()) {
     399      atomicNumber_t Z;
     400      (*input) >> Z;
     401      ASSERT(elements.count(Z), "Element not present");
     402      (*input) >> ws;
     403      (*input) >> elements[Z]->HBondDistance[0];
     404      (*input) >> elements[Z]->HBondDistance[1];
     405      (*input) >> elements[Z]->HBondDistance[2];
     406      (*input) >> ws;
    312407      //Log() << Verbose(3) << "Element " << (int)tmp << " has " << FindElement((int)tmp)->HBondDistance[0] << " Angstrom typical distance to hydrogen." << endl;
    313408    }
    314     infile.close();
    315     infile.clear();
    316   } else
    317     otherstatus = false;
    318 
    319   // fill H-BondAngle DB per element
    320   strncpy(filename, path, MAXSTRINGSIZE);
    321   strncat(filename, "/", MAXSTRINGSIZE-strlen(filename));
    322   strncat(filename, STANDARDHBONDANGLEDB, MAXSTRINGSIZE-strlen(filename));
    323   infile.open(filename);
    324   if (infile != NULL) {
    325     while (!infile.eof()) {
    326       atomicNumber_t Z;
    327       infile >> Z;
    328       ptr = parsedElems[Z];
    329       infile >> ws;
    330       infile >> ptr->HBondAngle[0];
    331       infile >> ptr->HBondAngle[1];
    332       infile >> ptr->HBondAngle[2];
    333       infile >> ws;
    334       //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;
    335     }
    336     infile.close();
    337   } else
    338     otherstatus = false;
    339 
    340   if (otherstatus){
    341     map<atomicNumber_t,element*>::iterator iter;
    342     for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
    343       AddElement((*iter).second);
    344     }
    345   }
    346   else{
    347     DoeLog(2) && (eLog()<< Verbose(2) << "Something went wrong while parsing the other databases!" << endl);
    348     map<atomicNumber_t,element*>::iterator iter;
    349     for(iter=parsedElems.begin();iter!=parsedElems.end();++iter){
    350       AddElement((*iter).second);
    351     }
    352   }
    353 
    354   return status;
    355 };
     409    return true;
     410  } else
     411                return false;
     412}
    356413
    357414/** Stores element list to file.
     
    374431    }
    375432    f.close();
    376   } else
    377     result = false;
    378   return result;
    379 };
     433    return true;
     434  } else
     435    return result;
     436};
Note: See TracChangeset for help on using the changeset viewer.