Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/molecule_fragmentation.cpp

    rd4c9ae r35b698  
    55 *      Author: heber
    66 */
     7
     8#include "Helpers/MemDebug.hpp"
    79
    810#include <cstring>
     
    3941  int FragmentCount;
    4042  // get maximum bond degree
    41   atom *Walker = start;
    42   while (Walker->next != end) {
    43     Walker = Walker->next;
    44     c = (Walker->ListOfBonds.size() > c) ? Walker->ListOfBonds.size() : c;
     43  for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
     44    c = ((*iter)->ListOfBonds.size() > c) ? (*iter)->ListOfBonds.size() : c;
    4545  }
    4646  FragmentCount = NoNonHydrogen*(1 << (c*order));
     
    8282 * -# Scans TEFactors file and sets the TEFactor of each key set in the temporary graph accordingly
    8383 * Finally, the temporary graph is inserted into the given \a FragmentList for return.
    84  * \param *out output stream for debugging
    85  * \param *path path to file
     84 * \param &path path to file
    8685 * \param *FragmentList empty, filled on return
    8786 * \return true - parsing successfully, false - failure on parsing (FragmentList will be NULL)
    8887 */
    89 bool ParseKeySetFile(char *path, Graph *&FragmentList)
     88bool ParseKeySetFile(std::string &path, Graph *&FragmentList)
    9089{
    9190  bool status = true;
     
    9493  GraphTestPair testGraphInsert;
    9594  int NumberOfFragments = 0;
    96   char *filename = Malloc<char>(MAXSTRINGSIZE, "molecule::ParseKeySetFile - filename");
     95  string filename;
    9796
    9897  if (FragmentList == NULL) { // check list pointer
     
    102101  // 1st pass: open file and read
    103102  DoLog(1) && (Log() << Verbose(1) << "Parsing the KeySet file ... " << endl);
    104   sprintf(filename, "%s/%s%s", path, FRAGMENTPREFIX, KEYSETFILE);
    105   InputFile.open(filename);
    106   if (InputFile != NULL) {
     103  filename = path + KEYSETFILE;
     104  InputFile.open(filename.c_str());
     105  if (InputFile.good()) {
    107106    // each line represents a new fragment
    108     char *buffer = Malloc<char>(MAXSTRINGSIZE, "molecule::ParseKeySetFile - *buffer");
     107    char buffer[MAXSTRINGSIZE];
    109108    // 1. parse keysets and insert into temp. graph
    110109    while (!InputFile.eof()) {
     
    122121    InputFile.close();
    123122    InputFile.clear();
    124     Free(&buffer);
    125     DoLog(1) && (Log() << Verbose(1) << "done." << endl);
     123    DoLog(1) && (Log() << Verbose(1) << "\t ... done." << endl);
    126124  } else {
    127     DoLog(1) && (Log() << Verbose(1) << "File " << filename << " not found." << endl);
     125    DoLog(1) && (Log() << Verbose(1) << "\t ... File " << filename << " not found." << endl);
    128126    status = false;
    129127  }
    130128
    131   Free(&filename);
    132129  return status;
    133130};
     
    148145  int NumberOfFragments = 0;
    149146  double TEFactor;
    150   char *filename = Malloc<char>(MAXSTRINGSIZE, "molecule::ParseTEFactorsFile - filename");
     147  char filename[MAXSTRINGSIZE];
    151148
    152149  if (FragmentList == NULL) { // check list pointer
     
    179176  }
    180177
    181   // free memory
    182   Free(&filename);
    183 
    184178  return status;
    185179};
    186180
    187181/** Stores key sets to file.
    188  * \param *out output stream for debugging
    189182 * \param KeySetList Graph with Keysets
    190  * \param *path path to file
     183 * \param &path path to file
    191184 * \return true - file written successfully, false - writing failed
    192185 */
    193 bool StoreKeySetFile(Graph &KeySetList, char *path)
    194 {
    195   ofstream output;
     186bool StoreKeySetFile(Graph &KeySetList, std::string &path)
     187{
    196188  bool status =  true;
    197   string line;
     189  string line = path + KEYSETFILE;
     190  ofstream output(line.c_str());
    198191
    199192  // open KeySet file
    200   line = path;
    201   line.append("/");
    202   line += FRAGMENTPREFIX;
    203   line += KEYSETFILE;
    204   output.open(line.c_str(), ios::out);
    205193  DoLog(1) && (Log() << Verbose(1) << "Saving key sets of the total graph ... ");
    206   if(output != NULL) {
     194  if(output.good()) {
    207195    for(Graph::iterator runner = KeySetList.begin(); runner != KeySetList.end(); runner++) {
    208196      for (KeySet::iterator sprinter = (*runner).first.begin();sprinter != (*runner).first.end(); sprinter++) {
     
    307295
    308296/** Scans the adaptive order file and insert (index, value) into map.
    309  * \param *out output stream for debugging
    310  * \param *path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
     297 * \param &path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
    311298 * \param &IndexedKeySetList list to find key set for a given index \a No
    312299 * \return adaptive criteria list from file
    313300 */
    314 map<int, pair<double,int> > * ScanAdaptiveFileIntoMap(char *path, map<int,KeySet> &IndexKeySetList)
     301map<int, pair<double,int> > * ScanAdaptiveFileIntoMap(std::string &path, map<int,KeySet> &IndexKeySetList)
    315302{
    316303  map<int, pair<double,int> > *AdaptiveCriteriaList = new map<int, pair<double,int> >;
    317304  int No = 0, FragOrder = 0;
    318305  double Value = 0.;
    319   char *buffer = Malloc<char>(MAXSTRINGSIZE, "molecule::CheckOrderAtSite: *buffer");
    320   sprintf(buffer, "%s/%s%s.dat", path, FRAGMENTPREFIX, ENERGYPERFRAGMENT);
    321   ifstream InputFile(buffer, ios::in);
     306  char buffer[MAXSTRINGSIZE];
     307  string filename = path + ENERGYPERFRAGMENT;
     308  ifstream InputFile(filename.c_str());
     309
     310  if (InputFile.fail()) {
     311    DoeLog(1) && (eLog() << Verbose(1) << "Cannot find file " << filename << "." << endl);
     312    return AdaptiveCriteriaList;
     313  }
    322314
    323315  if (CountLinesinFile(InputFile) > 0) {
     
    345337    InputFile.clear();
    346338  }
    347   Free(&buffer);
    348339
    349340  return AdaptiveCriteriaList;
     
    359350map<double, pair<int,int> >  * ReMapAdaptiveCriteriaListToValue(map<int, pair<double,int> > *AdaptiveCriteriaList, molecule *mol)
    360351{
    361   atom *Walker = mol->start;
     352  atom *Walker = NULL;
    362353  map<double, pair<int,int> > *FinalRootCandidates = new map<double, pair<int,int> > ;
    363354  DoLog(1) && (Log() << Verbose(1) << "Root candidate list is: " << endl);
     
    391382bool MarkUpdateCandidates(bool *AtomMask, map<double, pair<int,int> > &FinalRootCandidates, int Order, molecule *mol)
    392383{
    393   atom *Walker = mol->start;
     384  atom *Walker = NULL;
    394385  int No = -1;
    395386  bool status = false;
     
    425416
    426417/** Checks whether the OrderAtSite is still below \a Order at some site.
    427  * \param *out output stream for debugging
    428418 * \param *AtomMask defines true/false per global Atom::nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
    429419 * \param *GlobalKeySetList list of keysets with global ids (valid in "this" molecule) needed for adaptive increase
    430420 * \param Order desired Order if positive, desired exponent in threshold criteria if negative (0 is single-step)
    431421 * \param *MinimumRingSize array of max. possible order to avoid loops
    432  * \param *path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
     422 * \param path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
    433423 * \return true - needs further fragmentation, false - does not need fragmentation
    434424 */
    435 bool molecule::CheckOrderAtSite(bool *AtomMask, Graph *GlobalKeySetList, int Order, int *MinimumRingSize, char *path)
    436 {
    437   atom *Walker = start;
     425bool molecule::CheckOrderAtSite(bool *AtomMask, Graph *GlobalKeySetList, int Order, int *MinimumRingSize, std::string path)
     426{
    438427  bool status = false;
    439428
    440429  // initialize mask list
    441   for(int i=AtomCount;i--;)
     430  for(int i=getAtomCount();i--;)
    442431    AtomMask[i] = false;
    443432
    444433  if (Order < 0) { // adaptive increase of BondOrder per site
    445     if (AtomMask[AtomCount] == true)  // break after one step
     434    if (AtomMask[getAtomCount()] == true)  // break after one step
    446435      return false;
    447436
     
    457446    if (AdaptiveCriteriaList->empty()) {
    458447      DoeLog(2) && (eLog()<< Verbose(2) << "Unable to parse file, incrementing all." << endl);
    459       while (Walker->next != end) {
    460         Walker = Walker->next;
     448      for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
    461449    #ifdef ADDHYDROGEN
    462         if (Walker->type->Z != 1) // skip hydrogen
     450        if ((*iter)->type->Z != 1) // skip hydrogen
    463451    #endif
    464452        {
    465           AtomMask[Walker->nr] = true;  // include all (non-hydrogen) atoms
     453          AtomMask[(*iter)->nr] = true;  // include all (non-hydrogen) atoms
    466454          status = true;
    467455        }
     
    474462    MarkUpdateCandidates(AtomMask, *FinalRootCandidates, Order, this);
    475463
    476     Free(&IndexKeySetList);
    477     Free(&AdaptiveCriteriaList);
    478     Free(&FinalRootCandidates);
     464    delete[](IndexKeySetList);
     465    delete[](AdaptiveCriteriaList);
     466    delete[](FinalRootCandidates);
    479467  } else { // global increase of Bond Order
    480     while (Walker->next != end) {
    481       Walker = Walker->next;
     468    for(molecule::const_iterator iter = begin(); iter != end(); ++iter) {
    482469  #ifdef ADDHYDROGEN
    483       if (Walker->type->Z != 1) // skip hydrogen
     470      if ((*iter)->type->Z != 1) // skip hydrogen
    484471  #endif
    485472      {
    486         AtomMask[Walker->nr] = true;  // include all (non-hydrogen) atoms
    487         if ((Order != 0) && (Walker->AdaptiveOrder < Order)) // && (Walker->AdaptiveOrder < MinimumRingSize[Walker->nr]))
     473        AtomMask[(*iter)->nr] = true;  // include all (non-hydrogen) atoms
     474        if ((Order != 0) && ((*iter)->AdaptiveOrder < Order)) // && ((*iter)->AdaptiveOrder < MinimumRingSize[(*iter)->nr]))
    488475          status = true;
    489476      }
    490477    }
    491     if ((Order == 0) && (AtomMask[AtomCount] == false))  // single stepping, just check
     478    if ((!Order) && (!AtomMask[getAtomCount()]))  // single stepping, just check
    492479      status = true;
    493480
     
    500487  }
    501488
    502   PrintAtomMask(AtomMask, AtomCount); // for debugging
     489  PrintAtomMask(AtomMask, getAtomCount()); // for debugging
    503490
    504491  return status;
     
    516503    return false;
    517504  }
    518   SortIndex = Malloc<int>(AtomCount, "molecule::CreateMappingLabelsToConfigSequence: *SortIndex");
    519   for(int i=AtomCount;i--;)
     505  SortIndex = new int[getAtomCount()];
     506  for(int i=getAtomCount();i--;)
    520507    SortIndex[i] = -1;
    521508
     
    524511
    525512  return true;
     513};
     514
     515
     516
     517/** Creates a lookup table for true father's Atom::Nr -> atom ptr.
     518 * \param *start begin of list (STL iterator, i.e. first item)
     519 * \paran *end end of list (STL iterator, i.e. one past last item)
     520 * \param **Lookuptable pointer to return allocated lookup table (should be NULL on start)
     521 * \param count optional predetermined size for table (otherwise we set the count to highest true father id)
     522 * \return true - success, false - failure
     523 */
     524bool molecule::CreateFatherLookupTable(atom **&LookupTable, int count)
     525{
     526  bool status = true;
     527  int AtomNo;
     528
     529  if (LookupTable != NULL) {
     530    Log() << Verbose(0) << "Pointer for Lookup table is not NULL! Aborting ..." <<endl;
     531    return false;
     532  }
     533
     534  // count them
     535  if (count == 0) {
     536    for (molecule::iterator iter = begin(); iter != end(); ++iter) { // create a lookup table (Atom::nr -> atom) used as a marker table lateron
     537      count = (count < (*iter)->GetTrueFather()->nr) ? (*iter)->GetTrueFather()->nr : count;
     538    }
     539  }
     540  if (count <= 0) {
     541    Log() << Verbose(0) << "Count of lookup list is 0 or less." << endl;
     542    return false;
     543  }
     544
     545  // allocate and fill
     546  LookupTable = new atom *[count];
     547  if (LookupTable == NULL) {
     548    eLog() << Verbose(0) << "LookupTable memory allocation failed!" << endl;
     549    performCriticalExit();
     550    status = false;
     551  } else {
     552    for (int i=0;i<count;i++)
     553      LookupTable[i] = NULL;
     554    for (molecule::iterator iter = begin(); iter != end(); ++iter) {
     555      AtomNo = (*iter)->GetTrueFather()->nr;
     556      if ((AtomNo >= 0) && (AtomNo < count)) {
     557        //*out << "Setting LookupTable[" << AtomNo << "] to " << *(*iter) << endl;
     558        LookupTable[AtomNo] = (*iter);
     559      } else {
     560        Log() << Verbose(0) << "Walker " << *(*iter) << " exceeded range of nuclear ids [0, " << count << ")." << endl;
     561        status = false;
     562        break;
     563      }
     564    }
     565  }
     566
     567  return status;
    526568};
    527569
     
    539581 * of vertex indices: Global always means the index in "this" molecule, whereas local refers to the molecule or
    540582 * subgraph in the MoleculeListClass.
    541  * \param *out output stream for debugging
    542583 * \param Order up to how many neighbouring bonds a fragment contains in BondOrderScheme::BottumUp scheme
    543  * \param *configuration configuration for writing config files for each fragment
     584 * \param &prefix path and prefix of the bond order configs to be written
    544585 * \return 1 - continue, 2 - stop (no fragmentation occured)
    545586 */
    546 int molecule::FragmentMolecule(int Order, config *configuration)
     587int molecule::FragmentMolecule(int Order, std::string &prefix)
    547588{
    548589  MoleculeListClass *BondFragments = NULL;
    549   int *SortIndex = NULL;
    550   int *MinimumRingSize = new int[AtomCount];
     590  int *MinimumRingSize = new int[getAtomCount()];
    551591  int FragmentCounter;
    552592  MoleculeLeafClass *MolecularWalker = NULL;
     
    576616
    577617  // create lookup table for Atom::nr
    578   FragmentationToDo = FragmentationToDo && CreateFatherLookupTable(start, end, ListOfAtoms, AtomCount);
     618  FragmentationToDo = FragmentationToDo && CreateFatherLookupTable(ListOfAtoms, getAtomCount());
    579619
    580620  // === compare it with adjacency file ===
    581   FragmentationToDo = FragmentationToDo && CheckAdjacencyFileAgainstMolecule(configuration->configpath, ListOfAtoms);
    582   Free(&ListOfAtoms);
     621  FragmentationToDo = FragmentationToDo && CheckAdjacencyFileAgainstMolecule(prefix, ListOfAtoms);
     622  delete[](ListOfAtoms);
    583623
    584624  // ===== 2. perform a DFS analysis to gather info on cyclic structure and a list of disconnected subgraphs =====
     
    586626
    587627  // analysis of the cycles (print rings, get minimum cycle length) for each subgraph
    588   for(int i=AtomCount;i--;)
    589     MinimumRingSize[i] = AtomCount;
     628  for(int i=getAtomCount();i--;)
     629    MinimumRingSize[i] = getAtomCount();
    590630  MolecularWalker = Subgraphs;
    591631  FragmentCounter = 0;
     
    598638//    // check the list of local atoms for debugging
    599639//    Log() << Verbose(0) << "ListOfLocalAtoms for this subgraph is:" << endl;
    600 //    for (int i=0;i<AtomCount;i++)
     640//    for (int i=0;i<getAtomCount();i++)
    601641//      if (ListOfLocalAtoms[FragmentCounter][i] == NULL)
    602642//        Log() << Verbose(0) << "\tNULL";
     
    613653
    614654  // ===== 3. if structure still valid, parse key set file and others =====
    615   FragmentationToDo = FragmentationToDo && ParseKeySetFile(configuration->configpath, ParsedFragmentList);
     655  FragmentationToDo = FragmentationToDo && ParseKeySetFile(prefix, ParsedFragmentList);
    616656
    617657  // ===== 4. check globally whether there's something to do actually (first adaptivity check)
    618   FragmentationToDo = FragmentationToDo && ParseOrderAtSiteFromFile(configuration->configpath);
     658  FragmentationToDo = FragmentationToDo && ParseOrderAtSiteFromFile(prefix);
    619659
    620660  // =================================== Begin of FRAGMENTATION ===============================
     
    624664  // ===== 6b. prepare and go into the adaptive (Order<0), single-step (Order==0) or incremental (Order>0) cycle
    625665  KeyStack *RootStack = new KeyStack[Subgraphs->next->Count()];
    626   AtomMask = new bool[AtomCount+1];
    627   AtomMask[AtomCount] = false;
     666  AtomMask = new bool[getAtomCount()+1];
     667  AtomMask[getAtomCount()] = false;
    628668  FragmentationToDo = false;  // if CheckOrderAtSite just ones recommends fragmentation, we will save fragments afterwards
    629   while ((CheckOrder = CheckOrderAtSite(AtomMask, ParsedFragmentList, Order, MinimumRingSize, configuration->configpath))) {
     669  while ((CheckOrder = CheckOrderAtSite(AtomMask, ParsedFragmentList, Order, MinimumRingSize, prefix))) {
    630670    FragmentationToDo = FragmentationToDo || CheckOrder;
    631     AtomMask[AtomCount] = true;   // last plus one entry is used as marker that we have been through this loop once already in CheckOrderAtSite()
     671    AtomMask[getAtomCount()] = true;   // last plus one entry is used as marker that we have been through this loop once already in CheckOrderAtSite()
    632672    // ===== 6b. fill RootStack for each subgraph (second adaptivity check) =====
    633673    Subgraphs->next->FillRootStackForSubgraphs(RootStack, AtomMask, (FragmentCounter = 0));
     
    640680      DoLog(1) && (Log() << Verbose(1) << "Fragmenting subgraph " << MolecularWalker << "." << endl);
    641681      //MolecularWalker->Leaf->OutputListOfBonds(out);  // output atom::ListOfBonds for debugging
    642       if (MolecularWalker->Leaf->first->next != MolecularWalker->Leaf->last) {
     682      if (MolecularWalker->Leaf->hasBondStructure()) {
    643683        // call BOSSANOVA method
    644684        DoLog(0) && (Log() << Verbose(0) << endl << " ========== BOND ENERGY of subgraph " << FragmentCounter << " ========================= " << endl);
     
    672712    delete(Subgraphs);
    673713  }
    674   Free(&FragmentList);
     714  delete[](FragmentList);
    675715
    676716  // ===== 8b. gather keyset lists (graphs) from all subgraphs and transform into MoleculeListClass =====
     
    682722    KeySet test = (*runner).first;
    683723    DoLog(0) && (Log() << Verbose(0) << "Fragment No." << (*runner).second.first << " with TEFactor " << (*runner).second.second << "." << endl);
    684     BondFragments->insert(StoreFragmentFromKeySet(test, configuration));
     724    BondFragments->insert(StoreFragmentFromKeySet(test, World::getInstance().getConfig()));
    685725    k++;
    686726  }
     
    690730  if (BondFragments->ListOfMolecules.size() != 0) {
    691731    // create the SortIndex from BFS labels to order in the config file
     732    int *SortIndex = NULL;
    692733    CreateMappingLabelsToConfigSequence(SortIndex);
    693734
    694735    DoLog(1) && (Log() << Verbose(1) << "Writing " << BondFragments->ListOfMolecules.size() << " possible bond fragmentation configs" << endl);
    695     if (BondFragments->OutputConfigForListOfFragments(configuration, SortIndex))
     736    if (BondFragments->OutputConfigForListOfFragments(prefix, SortIndex))
    696737      DoLog(1) && (Log() << Verbose(1) << "All configs written." << endl);
    697738    else
     
    699740
    700741    // store force index reference file
    701     BondFragments->StoreForcesFile(configuration->configpath, SortIndex);
     742    BondFragments->StoreForcesFile(prefix, SortIndex);
    702743
    703744    // store keysets file
    704     StoreKeySetFile(TotalGraph, configuration->configpath);
    705 
    706     // store Adjacency file
    707     char *filename = Malloc<char> (MAXSTRINGSIZE, "molecule::FragmentMolecule - *filename");
    708     strcpy(filename, FRAGMENTPREFIX);
    709     strcat(filename, ADJACENCYFILE);
    710     StoreAdjacencyToFile(configuration->configpath, filename);
    711     Free(&filename);
     745    StoreKeySetFile(TotalGraph, prefix);
     746
     747    {
     748      // store Adjacency file
     749      std::string filename = prefix + ADJACENCYFILE;
     750      StoreAdjacencyToFile(filename);
     751    }
    712752
    713753    // store Hydrogen saturation correction file
    714     BondFragments->AddHydrogenCorrection(configuration->configpath);
     754    BondFragments->AddHydrogenCorrection(prefix);
    715755
    716756    // store adaptive orders into file
    717     StoreOrderAtSiteFile(configuration->configpath);
     757    StoreOrderAtSiteFile(prefix);
    718758
    719759    // restore orbital and Stop values
    720     CalculateOrbitals(*configuration);
     760    //CalculateOrbitals(*configuration);
    721761
    722762    // free memory for bond part
    723763    DoLog(1) && (Log() << Verbose(1) << "Freeing bond memory" << endl);
    724     Free(&FragmentList); // remove bond molecule from memory
    725     Free(&SortIndex);
     764    delete[](SortIndex);
    726765  } else {
    727766    DoLog(1) && (Log() << Verbose(1) << "FragmentList is zero on return, splitting failed." << endl);
     
    736775/** Stores pairs (Atom::nr, Atom::AdaptiveOrder) into file.
    737776 * Atoms not present in the file get "-1".
    738  * \param *out output stream for debugging
    739  * \param *path path to file ORDERATSITEFILE
     777 * \param &path path to file ORDERATSITEFILE
    740778 * \return true - file writable, false - not writable
    741779 */
    742 bool molecule::StoreOrderAtSiteFile(char *path)
    743 {
    744   stringstream line;
     780bool molecule::StoreOrderAtSiteFile(std::string &path)
     781{
     782  string line;
    745783  ofstream file;
    746784
    747   line << path << "/" << FRAGMENTPREFIX << ORDERATSITEFILE;
    748   file.open(line.str().c_str());
     785  line = path + ORDERATSITEFILE;
     786  file.open(line.c_str());
    749787  DoLog(1) && (Log() << Verbose(1) << "Writing OrderAtSite " << ORDERATSITEFILE << " ... " << endl);
    750   if (file != NULL) {
     788  if (file.good()) {
    751789    ActOnAllAtoms( &atom::OutputOrder, &file );
    752790    file.close();
     
    754792    return true;
    755793  } else {
    756     DoLog(1) && (Log() << Verbose(1) << "failed to open file " << line.str() << "." << endl);
     794    DoLog(1) && (Log() << Verbose(1) << "failed to open file " << line << "." << endl);
    757795    return false;
    758796  }
     
    761799/** Parses pairs(Atom::nr, Atom::AdaptiveOrder) from file and stores in molecule's Atom's.
    762800 * Atoms not present in the file get "0".
    763  * \param *out output stream for debugging
    764  * \param *path path to file ORDERATSITEFILEe
     801 * \param &path path to file ORDERATSITEFILEe
    765802 * \return true - file found and scanned, false - file not found
    766803 * \sa ParseKeySetFile() and CheckAdjacencyFileAgainstMolecule() as this is meant to be used in conjunction with the two
    767804 */
    768 bool molecule::ParseOrderAtSiteFromFile(char *path)
    769 {
    770   unsigned char *OrderArray = Calloc<unsigned char>(AtomCount, "molecule::ParseOrderAtSiteFromFile - *OrderArray");
    771   bool *MaxArray = Calloc<bool>(AtomCount, "molecule::ParseOrderAtSiteFromFile - *MaxArray");
     805bool molecule::ParseOrderAtSiteFromFile(std::string &path)
     806{
     807  unsigned char *OrderArray = new unsigned char[getAtomCount()];
     808  bool *MaxArray = new bool[getAtomCount()];
    772809  bool status;
    773810  int AtomNr, value;
    774   stringstream line;
     811  string line;
    775812  ifstream file;
    776813
     814  for(int i=0;i<getAtomCount();i++) {
     815    OrderArray[i] = 0;
     816    MaxArray[i] = false;
     817  }
     818
    777819  DoLog(1) && (Log() << Verbose(1) << "Begin of ParseOrderAtSiteFromFile" << endl);
    778   line << path << "/" << FRAGMENTPREFIX << ORDERATSITEFILE;
    779   file.open(line.str().c_str());
    780   if (file != NULL) {
     820  line = path + ORDERATSITEFILE;
     821  file.open(line.c_str());
     822  if (file.good()) {
    781823    while (!file.eof()) { // parse from file
    782824      AtomNr = -1;
     
    796838    SetAtomValueToIndexedArray( MaxArray, &atom::nr, &atom::MaxOrder );
    797839
    798     DoLog(1) && (Log() << Verbose(1) << "done." << endl);
     840    DoLog(1) && (Log() << Verbose(1) << "\t ... done." << endl);
    799841    status = true;
    800842  } else {
    801     DoLog(1) && (Log() << Verbose(1) << "failed to open file " << line.str() << "." << endl);
     843    DoLog(1) && (Log() << Verbose(1) << "\t ... failed to open file " << line << "." << endl);
    802844    status = false;
    803845  }
    804   Free(&OrderArray);
    805   Free(&MaxArray);
     846  delete[](OrderArray);
     847  delete[](MaxArray);
    806848
    807849  DoLog(1) && (Log() << Verbose(1) << "End of ParseOrderAtSiteFromFile" << endl);
     
    872914  atom *OtherFather = NULL;
    873915  atom *FatherOfRunner = NULL;
    874   Leaf->CountAtoms();
    875 
    876   atom *Runner = Leaf->start;
    877   while (Runner->next != Leaf->end) {
    878     Runner = Runner->next;
     916
     917#ifdef ADDHYDROGEN
     918  molecule::const_iterator runner;
     919#endif
     920  // we increment the iter just before skipping the hydrogen
     921  for (molecule::const_iterator iter = Leaf->begin(); iter != Leaf->end();) {
    879922    LonelyFlag = true;
    880     FatherOfRunner = Runner->father;
     923    FatherOfRunner = (*iter)->father;
     924    ASSERT(FatherOfRunner,"Atom without father found");
    881925    if (SonList[FatherOfRunner->nr] != NULL)  {  // check if this, our father, is present in list
    882926      // create all bonds
     
    889933//            Log() << Verbose(3) << "Adding Bond: ";
    890934//            Log() << Verbose(0) <<
    891             Leaf->AddBond(Runner, SonList[OtherFather->nr], (*BondRunner)->BondDegree);
     935            Leaf->AddBond((*iter), SonList[OtherFather->nr], (*BondRunner)->BondDegree);
    892936//            Log() << Verbose(0) << "." << endl;
    893             //NumBonds[Runner->nr]++;
     937            //NumBonds[(*iter)->nr]++;
    894938          } else {
    895939//            Log() << Verbose(3) << "Not adding bond, labels in wrong order." << endl;
     
    899943//          Log() << Verbose(0) << ", who has no son in this fragment molecule." << endl;
    900944#ifdef ADDHYDROGEN
    901           //Log() << Verbose(3) << "Adding Hydrogen to " << Runner->Name << " and a bond in between." << endl;
    902           if(!Leaf->AddHydrogenReplacementAtom((*BondRunner), Runner, FatherOfRunner, OtherFather, IsAngstroem))
     945          //Log() << Verbose(3) << "Adding Hydrogen to " << (*iter)->Name << " and a bond in between." << endl;
     946          if(!Leaf->AddHydrogenReplacementAtom((*BondRunner), (*iter), FatherOfRunner, OtherFather, IsAngstroem))
    903947            exit(1);
    904948#endif
    905           //NumBonds[Runner->nr] += Binder->BondDegree;
     949          //NumBonds[(*iter)->nr] += Binder->BondDegree;
    906950        }
    907951      }
    908952    } else {
    909       DoeLog(1) && (eLog()<< Verbose(1) << "Son " << Runner->getName() << " has father " << FatherOfRunner->getName() << " but its entry in SonList is " << SonList[FatherOfRunner->nr] << "!" << endl);
    910     }
    911     if ((LonelyFlag) && (Leaf->AtomCount > 1)) {
    912       DoLog(0) && (Log() << Verbose(0) << *Runner << "has got bonds only to hydrogens!" << endl);
    913     }
     953    DoeLog(1) && (eLog()<< Verbose(1) << "Son " << (*iter)->getName() << " has father " << FatherOfRunner->getName() << " but its entry in SonList is " << SonList[FatherOfRunner->nr] << "!" << endl);
     954    }
     955    if ((LonelyFlag) && (Leaf->getAtomCount() > 1)) {
     956      DoLog(0) && (Log() << Verbose(0) << **iter << "has got bonds only to hydrogens!" << endl);
     957    }
     958    ++iter;
    914959#ifdef ADDHYDROGEN
    915     while ((Runner->next != Leaf->end) && (Runner->next->type->Z == 1)) // skip added hydrogen
    916       Runner = Runner->next;
     960    while ((iter != Leaf->end()) && ((*iter)->type->Z == 1)){ // skip added hydrogen
     961      iter++;
     962    }
    917963#endif
    918964  }
     
    929975molecule * molecule::StoreFragmentFromKeySet(KeySet &Leaflet, bool IsAngstroem)
    930976{
    931   atom **SonList = Calloc<atom*>(AtomCount, "molecule::StoreFragmentFromStack: **SonList");
     977  atom **SonList = new atom*[getAtomCount()];
    932978  molecule *Leaf = World::getInstance().createMolecule();
     979
     980  for(int i=0;i<getAtomCount();i++)
     981    SonList[i] = NULL;
    933982
    934983//  Log() << Verbose(1) << "Begin of StoreFragmentFromKeyset." << endl;
     
    939988
    940989  //Leaflet->Leaf->ScanForPeriodicCorrection(out);
    941   Free(&SonList);
     990  delete[](SonList);
    942991//  Log() << Verbose(1) << "End of StoreFragmentFromKeyset." << endl;
    943992  return Leaf;
     
    10841133  int bits, TouchedIndex, SubSetDimension, SP, Added;
    10851134  int SpaceLeft;
    1086   int *TouchedList = Malloc<int>(SubOrder + 1, "molecule::SPFragmentGenerator: *TouchedList");
    1087   bond **BondsList = NULL;
     1135  int *TouchedList = new int[SubOrder + 1];
    10881136  KeySetTestPair TestKeySetInsert;
    10891137
     
    11241172
    11251173          // then allocate and fill the list
    1126           BondsList = Malloc<bond*>(SubSetDimension, "molecule::SPFragmentGenerator: **BondsList");
     1174          bond *BondsList[SubSetDimension];
    11271175          SubSetDimension = FillBondsList(BondsList, FragmentSearch->BondsPerSPList[2*SP], FragmentSearch->BondsPerSPList[2*SP+1], TouchedList, TouchedIndex);
    11281176
     
    11301178          Log() << Verbose(2+verbosity) << "Calling subset generator " << SP << " away from root " << *FragmentSearch->Root << " with sub set dimension " << SubSetDimension << "." << endl;
    11311179          SPFragmentGenerator(FragmentSearch, SP, BondsList, SubSetDimension, SubOrder-bits);
    1132 
    1133           Free(&BondsList);
    11341180        }
    11351181      } else {
     
    11531199    }
    11541200  }
    1155   Free(&TouchedList);
     1201  delete[](TouchedList);
    11561202  Log() << Verbose(1+verbosity) << "End of SPFragmentGenerator, " << RootDistance << " away from Root " << *FragmentSearch->Root << " and SubOrder is " << SubOrder << "." << endl;
    11571203};
     
    11651211void InitialiseSPList(int Order, struct UniqueFragments &FragmentSearch)
    11661212{
    1167   FragmentSearch.BondsPerSPList = Malloc<bond*>(Order * 2, "molecule::PowerSetGenerator: ***BondsPerSPList");
    1168   FragmentSearch.BondsPerSPCount = Malloc<int>(Order, "molecule::PowerSetGenerator: *BondsPerSPCount");
     1213  FragmentSearch.BondsPerSPList = new bond* [Order * 2];
     1214  FragmentSearch.BondsPerSPCount = new int[Order];
    11691215  for (int i=Order;i--;) {
    11701216    FragmentSearch.BondsPerSPList[2*i] = new bond();    // start node
     
    11841230void FreeSPList(int Order, struct UniqueFragments &FragmentSearch)
    11851231{
    1186   Free(&FragmentSearch.BondsPerSPCount);
     1232  delete[](FragmentSearch.BondsPerSPCount);
    11871233  for (int i=Order;i--;) {
    11881234    delete(FragmentSearch.BondsPerSPList[2*i]);
    11891235    delete(FragmentSearch.BondsPerSPList[2*i+1]);
    11901236  }
    1191   Free(&FragmentSearch.BondsPerSPList);
     1237  delete[](FragmentSearch.BondsPerSPList);
    11921238};
    11931239
     
    13701416int molecule::PowerSetGenerator(int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet)
    13711417{
    1372   bond **BondsList = NULL;
    13731418  int Counter = FragmentSearch.FragmentCounter; // mark current value of counter
    13741419
     
    13941439
    13951440    // prepare the subset and call the generator
    1396     BondsList = Calloc<bond*>(FragmentSearch.BondsPerSPCount[0], "molecule::PowerSetGenerator: **BondsList");
     1441    bond* BondsList[FragmentSearch.BondsPerSPCount[0]];
     1442    for(int i=0;i<FragmentSearch.BondsPerSPCount[0];i++)
     1443      BondsList[i] = NULL;
    13971444    BondsList[0] = FragmentSearch.BondsPerSPList[0]->next;  // on SP level 0 there's only the root bond
    13981445
    13991446    SPFragmentGenerator(&FragmentSearch, 0, BondsList, FragmentSearch.BondsPerSPCount[0], Order);
    1400 
    1401     Free(&BondsList);
    14021447  } else {
    14031448    DoLog(0) && (Log() << Verbose(0) << "Not enough total number of edges to build " << Order << "-body fragments." << endl);
     
    15071552      }
    15081553    }
    1509     Free(&FragmentLowerOrdersList[RootNr]);
     1554    delete[](FragmentLowerOrdersList[RootNr]);
    15101555    RootNr++;
    15111556  }
    1512   Free(&FragmentLowerOrdersList);
     1557  delete[](FragmentLowerOrdersList);
    15131558};
    15141559
     
    15491594  // FragmentLowerOrdersList is a 2D-array of pointer to MoleculeListClass objects, one dimension represents the ANOVA expansion of a single order (i.e. 5)
    15501595  // with all needed lower orders that are subtracted, the other dimension is the BondOrder (i.e. from 1 to 5)
    1551   NumMoleculesOfOrder = Calloc<int>(UpgradeCount, "molecule::FragmentBOSSANOVA: *NumMoleculesOfOrder");
    1552   FragmentLowerOrdersList = Calloc<Graph**>(UpgradeCount, "molecule::FragmentBOSSANOVA: ***FragmentLowerOrdersList");
     1596  NumMoleculesOfOrder = new int[UpgradeCount];
     1597  FragmentLowerOrdersList = new Graph**[UpgradeCount];
     1598
     1599  for(int i=0;i<UpgradeCount;i++) {
     1600    NumMoleculesOfOrder[i] = 0;
     1601    FragmentLowerOrdersList[i] = NULL;
     1602  }
    15531603
    15541604  // initialise the fragments structure
     
    15561606  FragmentSearch.FragmentSet = new KeySet;
    15571607  FragmentSearch.Root = FindAtom(RootKeyNr);
    1558   FragmentSearch.ShortestPathList = Malloc<int>(AtomCount, "molecule::PowerSetGenerator: *ShortestPathList");
    1559   for (int i=AtomCount;i--;) {
     1608  FragmentSearch.ShortestPathList = new int[getAtomCount()];
     1609  for (int i=getAtomCount();i--;) {
    15601610    FragmentSearch.ShortestPathList[i] = -1;
    15611611  }
    15621612
    15631613  // Construct the complete KeySet which we need for topmost level only (but for all Roots)
    1564   atom *Walker = start;
    15651614  KeySet CompleteMolecule;
    1566   while (Walker->next != end) {
    1567     Walker = Walker->next;
    1568     CompleteMolecule.insert(Walker->GetTrueFather()->nr);
     1615  for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
     1616    CompleteMolecule.insert((*iter)->GetTrueFather()->nr);
    15691617  }
    15701618
     
    15771625    RootKeyNr = RootStack.front();
    15781626    RootStack.pop_front();
    1579     Walker = FindAtom(RootKeyNr);
     1627    atom *Walker = FindAtom(RootKeyNr);
    15801628    // check cyclic lengths
    15811629    //if ((MinimumRingSize[Walker->GetTrueFather()->nr] != -1) && (Walker->GetTrueFather()->AdaptiveOrder+1 > MinimumRingSize[Walker->GetTrueFather()->nr])) {
     
    15921640      // allocate memory for all lower level orders in this 1D-array of ptrs
    15931641      NumLevels = 1 << (Order-1); // (int)pow(2,Order);
    1594       FragmentLowerOrdersList[RootNr] = Calloc<Graph*>(NumLevels, "molecule::FragmentBOSSANOVA: **FragmentLowerOrdersList[]");
     1642      FragmentLowerOrdersList[RootNr] = new Graph*[NumLevels];
     1643      for (int i=0;i<NumLevels;i++)
     1644        FragmentLowerOrdersList[RootNr][i] = NULL;
    15951645
    15961646      // create top order where nothing is reduced
     
    16281678
    16291679  // cleanup FragmentSearch structure
    1630   Free(&FragmentSearch.ShortestPathList);
     1680  delete[](FragmentSearch.ShortestPathList);
    16311681  delete(FragmentSearch.FragmentSet);
    16321682
     
    16411691  CombineAllOrderListIntoOne(FragmentList, FragmentLowerOrdersList, RootStack, this);
    16421692  FreeAllOrdersList(FragmentLowerOrdersList, RootStack, this);
    1643   Free(&NumMoleculesOfOrder);
     1693  delete[](NumMoleculesOfOrder);
    16441694
    16451695  DoLog(0) && (Log() << Verbose(0) << "End of FragmentBOSSANOVA." << endl);
     
    16641714  Vector Translationvector;
    16651715  //class StackClass<atom *> *CompStack = NULL;
    1666   class StackClass<atom *> *AtomStack = new StackClass<atom *>(AtomCount);
     1716  class StackClass<atom *> *AtomStack = new StackClass<atom *>(getAtomCount());
    16671717  bool flag = true;
    16681718
    16691719  DoLog(2) && (Log() << Verbose(2) << "Begin of ScanForPeriodicCorrection." << endl);
    16701720
    1671   ColorList = Calloc<enum Shading>(AtomCount, "molecule::ScanForPeriodicCorrection: *ColorList");
     1721  ColorList = new enum Shading[getAtomCount()];
     1722  for (int i=0;i<getAtomCount();i++)
     1723    ColorList[i] = (enum Shading)0;
    16721724  while (flag) {
    16731725    // remove bonds that are beyond bonddistance
    16741726    Translationvector.Zero();
    16751727    // scan all bonds
    1676     Binder = first;
    16771728    flag = false;
    1678     while ((!flag) && (Binder->next != last)) {
    1679       Binder = Binder->next;
    1680       for (int i=NDIM;i--;) {
    1681         tmp = fabs(Binder->leftatom->x[i] - Binder->rightatom->x[i]);
    1682         //Log() << Verbose(3) << "Checking " << i << "th distance of " << *Binder->leftatom << " to " << *Binder->rightatom << ": " << tmp << "." << endl;
    1683         if (tmp > BondDistance) {
    1684           OtherBinder = Binder->next; // note down binding partner for later re-insertion
    1685           unlink(Binder);   // unlink bond
    1686           DoLog(2) && (Log() << Verbose(2) << "Correcting at bond " << *Binder << "." << endl);
    1687           flag = true;
    1688           break;
     1729    for(molecule::iterator AtomRunner = begin(); (!flag) && (AtomRunner != end()); ++AtomRunner)
     1730      for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); (!flag) && (BondRunner != (*AtomRunner)->ListOfBonds.end()); ++BondRunner) {
     1731        Binder = (*BondRunner);
     1732        for (int i=NDIM;i--;) {
     1733          tmp = fabs(Binder->leftatom->x[i] - Binder->rightatom->x[i]);
     1734          //Log() << Verbose(3) << "Checking " << i << "th distance of " << *Binder->leftatom << " to " << *Binder->rightatom << ": " << tmp << "." << endl;
     1735          if (tmp > BondDistance) {
     1736            OtherBinder = Binder->next; // note down binding partner for later re-insertion
     1737            unlink(Binder);   // unlink bond
     1738            DoLog(2) && (Log() << Verbose(2) << "Correcting at bond " << *Binder << "." << endl);
     1739            flag = true;
     1740            break;
     1741          }
    16891742        }
    16901743      }
    1691     }
    16921744    if (flag) {
    16931745      // create translation vector from their periodically modified distance
     
    17011753      Log() << Verbose(0) << Translationvector <<  endl;
    17021754      // apply to all atoms of first component via BFS
    1703       for (int i=AtomCount;i--;)
     1755      for (int i=getAtomCount();i--;)
    17041756        ColorList[i] = white;
    17051757      AtomStack->Push(Binder->leftatom);
     
    17271779  // free allocated space from ReturnFullMatrixforSymmetric()
    17281780  delete(AtomStack);
    1729   Free(&ColorList);
    1730   Free(&matrix);
     1781  delete[](ColorList);
     1782  delete[](matrix);
    17311783  DoLog(2) && (Log() << Verbose(2) << "End of ScanForPeriodicCorrection." << endl);
    17321784};
Note: See TracChangeset for help on using the changeset viewer.