Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/molecule_graph.cpp

    r68f03d r112b09  
    55 *      Author: heber
    66 */
     7
     8#include "Helpers/MemDebug.hpp"
    79
    810#include "atom.hpp"
     
    1214#include "element.hpp"
    1315#include "helpers.hpp"
     16#include "info.hpp"
    1417#include "linkedcell.hpp"
    1518#include "lists.hpp"
     
    1922#include "World.hpp"
    2023#include "Helpers/fast_functions.hpp"
     24#include "Helpers/Assert.hpp"
     25
    2126
    2227struct BFSAccounting
     
    5459void molecule::CreateAdjacencyListFromDbondFile(ifstream *input)
    5560{
    56 
     61  Info FunctionInfo(__func__);
    5762  // 1 We will parse bonds out of the dbond file created by tremolo.
    5863  int atom1, atom2;
    5964  atom *Walker, *OtherWalker;
    60 
    61   if (!input) {
    62     DoLog(1) && (Log() << Verbose(1) << "Opening silica failed \n");
     65  char line[MAXSTRINGSIZE];
     66
     67  if (input->fail()) {
     68    DoeLog(0) && (eLog() << Verbose(0) << "Opening of bond file failed \n");
     69    performCriticalExit();
    6370  };
    64 
    65   *input >> ws >> atom1;
    66   *input >> ws >> atom2;
    67   DoLog(1) && (Log() << Verbose(1) << "Scanning file\n");
     71  doCountAtoms();
     72
     73  // skip header
     74  input->getline(line,MAXSTRINGSIZE);
     75  DoLog(1) && (Log() << Verbose(1) << "Scanning file ... \n");
    6876  while (!input->eof()) // Check whether we read everything already
    6977  {
    70     *input >> ws >> atom1;
    71     *input >> ws >> atom2;
    72 
     78    input->getline(line,MAXSTRINGSIZE);
     79    stringstream zeile(line);
     80    zeile >> atom1;
     81    zeile >> atom2;
     82
     83    DoLog(2) && (Log() << Verbose(2) << "Looking for atoms " << atom1 << " and " << atom2 << "." << endl);
    7384    if (atom2 < atom1) //Sort indices of atoms in order
    7485      flip(atom1, atom2);
    7586    Walker = FindAtom(atom1);
     87    ASSERT(Walker,"Could not find an atom with the ID given in dbond file");
    7688    OtherWalker = FindAtom(atom2);
     89    ASSERT(OtherWalker,"Could not find an atom with the ID given in dbond file");
    7790    AddBond(Walker, OtherWalker); //Add the bond between the two atoms with respective indices.
    7891  }
     
    103116  atom *Walker = NULL;
    104117  atom *OtherWalker = NULL;
    105   atom **AtomMap = NULL;
    106118  int n[NDIM];
    107119  double MinDistance, MaxDistance;
     
    118130  DoLog(0) && (Log() << Verbose(0) << "Begin of CreateAdjacencyList." << endl);
    119131  // remove every bond from the list
    120   bond *Binder = NULL;
    121   while (last->previous != first) {
    122     Binder = last->previous;
    123     Binder->leftatom->UnregisterBond(Binder);
    124     Binder->rightatom->UnregisterBond(Binder);
    125     removewithoutcheck(Binder);
    126   }
     132  for(molecule::iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner)
     133    for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
     134      if ((*BondRunner)->leftatom == *AtomRunner)
     135        delete((*BondRunner));
    127136  BondCount = 0;
    128137
    129138  // count atoms in molecule = dimension of matrix (also give each unique name and continuous numbering)
    130   CountAtoms();
    131   DoLog(1) && (Log() << Verbose(1) << "AtomCount " << AtomCount << " and bonddistance is " << bonddistance << "." << endl);
    132 
    133   if ((AtomCount > 1) && (bonddistance > 1.)) {
     139  DoLog(1) && (Log() << Verbose(1) << "AtomCount " << getAtomCount() << " and bonddistance is " << bonddistance << "." << endl);
     140
     141  if ((getAtomCount() > 1) && (bonddistance > 1.)) {
    134142    DoLog(2) && (Log() << Verbose(2) << "Creating Linked Cell structure ... " << endl);
    135143    LC = new LinkedCell(this, bonddistance);
     
    137145    // create a list to map Tesselpoint::nr to atom *
    138146    DoLog(2) && (Log() << Verbose(2) << "Creating TesselPoint to atom map ... " << endl);
    139     AtomMap = Calloc<atom *> (AtomCount, "molecule::CreateAdjacencyList - **AtomCount");
    140     Walker = start;
    141     while (Walker->next != end) {
    142       Walker = Walker->next;
    143       AtomMap[Walker->nr] = Walker;
     147
     148    // set numbers for atoms that can later be used
     149    int i=0;
     150    for(internal_iterator iter = atoms.begin();iter!= atoms.end(); ++iter){
     151      (*iter)->nr = i++;
    144152    }
    145153
     
    153161          if (List != NULL) {
    154162            for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
    155               Walker = AtomMap[(*Runner)->nr];
    156 //              Log() << Verbose(0) << "Current Atom is " << *Walker << "." << endl;
     163              Walker = dynamic_cast<atom*>(*Runner);
     164              ASSERT(Walker,"Tesselpoint that was not an atom retrieved from LinkedNode");
     165              //Log() << Verbose(0) << "Current Atom is " << *Walker << "." << endl;
    157166              // 3c. check for possible bond between each atom in this and every one in the 27 cells
    158167              for (n[0] = -1; n[0] <= 1; n[0]++)
     
    164173                      for (LinkedCell::LinkedNodes::const_iterator OtherRunner = OtherList->begin(); OtherRunner != OtherList->end(); OtherRunner++) {
    165174                        if ((*OtherRunner)->nr > Walker->nr) {
    166                           OtherWalker = AtomMap[(*OtherRunner)->nr];
    167 //                          Log() << Verbose(0) << "Current other Atom is " << *OtherWalker << "." << endl;
    168                           const double distance = OtherWalker->x.PeriodicDistanceSquared(Walker->x, cell_size);
    169 //                          Log() << Verbose(1) << "Checking distance " << distance << " against typical bond length of " << bonddistance*bonddistance << "." << endl;
     175                          OtherWalker = dynamic_cast<atom*>(*OtherRunner);
     176                          ASSERT(OtherWalker,"TesselPoint that was not an atom retrieved from LinkedNode");
     177                          //Log() << Verbose(1) << "Checking distance " << OtherWalker->x.PeriodicDistanceSquared(&(Walker->x), cell_size) << " against typical bond length of " << bonddistance*bonddistance << "." << endl;
    170178                          (BG->*minmaxdistance)(Walker, OtherWalker, MinDistance, MaxDistance, IsAngstroem);
     179                          const double distance = OtherWalker->x.PeriodicDistanceSquared(Walker->x,cell_size);
    171180                          const bool status = (distance <= MaxDistance * MaxDistance) && (distance >= MinDistance * MinDistance);
    172181//                          Log() << Verbose(1) << "MinDistance is " << MinDistance << " and MaxDistance is " << MaxDistance << "." << endl;
     
    188197          }
    189198        }
    190     Free(&AtomMap);
    191199    delete (LC);
    192200    DoLog(1) && (Log() << Verbose(1) << "I detected " << BondCount << " bonds in the molecule with distance " << BondDistance << "." << endl);
     
    199207    ActOnAllAtoms( &atom::OutputBondOfAtom );
    200208  } else
    201     DoLog(1) && (Log() << Verbose(1) << "AtomCount is " << AtomCount << ", thus no bonds, no connections!." << endl);
     209    DoLog(1) && (Log() << Verbose(1) << "AtomCount is " << getAtomCount() << ", thus no bonds, no connections!." << endl);
    202210  DoLog(0) && (Log() << Verbose(0) << "End of CreateAdjacencyList." << endl);
    203211  if (free_BG)
     
    206214;
    207215
     216/** Checks for presence of bonds within atom list.
     217 * TODO: more sophisticated check for bond structure (e.g. connected subgraph, ...)
     218 * \return true - bonds present, false - no bonds
     219 */
     220bool molecule::hasBondStructure()
     221{
     222  for(molecule::iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner)
     223    if (!(*AtomRunner)->ListOfBonds.empty())
     224      return true;
     225  return false;
     226}
     227
     228/** Counts the number of present bonds.
     229 * \return number of bonds
     230 */
     231unsigned int molecule::CountBonds() const
     232{
     233  unsigned int counter = 0;
     234  for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner)
     235    for(BondList::const_iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); BondRunner != (*AtomRunner)->ListOfBonds.end(); ++BondRunner)
     236      if ((*BondRunner)->leftatom == *AtomRunner)
     237        counter++;
     238  return counter;
     239}
     240
    208241/** Prints a list of all bonds to \a *out.
    209242 * \param output stream
     
    212245{
    213246  DoLog(1) && (Log() << Verbose(1) << endl << "From contents of bond chain list:");
    214   bond *Binder = first;
    215   while (Binder->next != last) {
    216     Binder = Binder->next;
    217     DoLog(0) && (Log() << Verbose(0) << *Binder << "\t" << endl);
    218   }
     247  for(molecule::const_iterator AtomRunner = molecule::begin(); AtomRunner != molecule::end(); ++AtomRunner)
     248    for(BondList::const_iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); BondRunner != (*AtomRunner)->ListOfBonds.end(); ++BondRunner)
     249      if ((*BondRunner)->leftatom == *AtomRunner) {
     250        DoLog(0) && (Log() << Verbose(0) << *(*BondRunner) << "\t" << endl);
     251      }
    219252  DoLog(0) && (Log() << Verbose(0) << endl);
    220253}
     
    241274    DoLog(0) && (Log() << Verbose(0) << " done." << endl);
    242275  } else {
    243     DoLog(1) && (Log() << Verbose(1) << "BondCount is " << BondCount << ", no bonds between any of the " << AtomCount << " atoms." << endl);
     276    DoLog(1) && (Log() << Verbose(1) << "BondCount is " << BondCount << ", no bonds between any of the " << getAtomCount() << " atoms." << endl);
    244277  }
    245278  DoLog(0) && (Log() << Verbose(0) << No << " bonds could not be corrected." << endl);
     
    260293  MoleculeLeafClass *Subgraphs = NULL;
    261294  class StackClass<bond *> *BackEdgeStack = NULL;
    262   bond *Binder = first;
    263   if ((Binder->next != last) && (Binder->next->Type == Undetermined)) {
    264     DoLog(0) && (Log() << Verbose(0) << "No Depth-First-Search analysis performed so far, calling ..." << endl);
    265     Subgraphs = DepthFirstSearchAnalysis(BackEdgeStack);
    266     while (Subgraphs->next != NULL) {
    267       Subgraphs = Subgraphs->next;
    268       delete (Subgraphs->previous);
    269     }
    270     delete (Subgraphs);
    271     delete[] (MinimumRingSize);
    272   }
    273   while (Binder->next != last) {
    274     Binder = Binder->next;
    275     if (Binder->Cyclic)
    276       NoCyclicBonds++;
    277   }
     295  for(molecule::iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner)
     296    if ((!(*AtomRunner)->ListOfBonds.empty()) && ((*(*AtomRunner)->ListOfBonds.begin())->Type == Undetermined)) {
     297      DoLog(0) && (Log() << Verbose(0) << "No Depth-First-Search analysis performed so far, calling ..." << endl);
     298      Subgraphs = DepthFirstSearchAnalysis(BackEdgeStack);
     299      while (Subgraphs->next != NULL) {
     300        Subgraphs = Subgraphs->next;
     301        delete (Subgraphs->previous);
     302      }
     303      delete (Subgraphs);
     304      delete[] (MinimumRingSize);
     305      break;
     306    }
     307  for(molecule::iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner)
     308    for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); BondRunner != (*AtomRunner)->ListOfBonds.end(); ++BondRunner)
     309      if ((*BondRunner)->leftatom == *AtomRunner)
     310        if ((*BondRunner)->Cyclic)
     311          NoCyclicBonds++;
    278312  delete (BackEdgeStack);
    279313  return NoCyclicBonds;
     
    461495void DepthFirstSearchAnalysis_Init(struct DFSAccounting &DFS, const molecule * const mol)
    462496{
    463   DFS.AtomStack = new StackClass<atom *> (mol->AtomCount);
     497  DFS.AtomStack = new StackClass<atom *> (mol->getAtomCount());
    464498  DFS.CurrentGraphNr = 0;
    465499  DFS.ComponentNumber = 0;
     
    502536  bond *Binder = NULL;
    503537
    504   if (AtomCount == 0)
     538  if (getAtomCount() == 0)
    505539    return SubGraphs;
    506540  DoLog(0) && (Log() << Verbose(0) << "Begin of DepthFirstSearchAnalysis" << endl);
    507541  DepthFirstSearchAnalysis_Init(DFS, this);
    508542
    509   DFS.Root = start->next;
    510   while (DFS.Root != end) { // if there any atoms at all
     543  for (molecule::const_iterator iter = begin(); iter != end();) {
     544    DFS.Root = *iter;
    511545    // (1) mark all edges unused, empty stack, set atom->GraphNr = -1 for all
    512546    DFS.AtomStack->ClearStack();
     
    548582
    549583    // step on to next root
    550     while ((DFS.Root != end) && (DFS.Root->GraphNr != -1)) {
    551       //Log() << Verbose(1) << "Current next subgraph root candidate is " << Root->Name << "." << endl;
    552       if (DFS.Root->GraphNr != -1) // if already discovered, step on
    553         DFS.Root = DFS.Root->next;
     584    while ((iter != end()) && ((*iter)->GraphNr != -1)) {
     585      //Log() << Verbose(1) << "Current next subgraph root candidate is " << (*iter)->Name << "." << endl;
     586      if ((*iter)->GraphNr != -1) // if already discovered, step on
     587        iter++;
    554588    }
    555589  }
     
    573607{
    574608  NoCyclicBonds = 0;
    575   bond *Binder = first;
    576   while (Binder->next != last) {
    577     Binder = Binder->next;
    578     if (Binder->rightatom->LowpointNr == Binder->leftatom->LowpointNr) { // cyclic ??
    579       Binder->Cyclic = true;
    580       NoCyclicBonds++;
    581     }
    582   }
     609  for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner)
     610    for(BondList::const_iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); BondRunner != (*AtomRunner)->ListOfBonds.end(); ++BondRunner)
     611      if ((*BondRunner)->leftatom == *AtomRunner)
     612        if ((*BondRunner)->rightatom->LowpointNr == (*BondRunner)->leftatom->LowpointNr) { // cyclic ??
     613          (*BondRunner)->Cyclic = true;
     614          NoCyclicBonds++;
     615        }
    583616}
    584617;
     
    599632void molecule::OutputGraphInfoPerBond() const
    600633{
     634  bond *Binder = NULL;
    601635  DoLog(1) && (Log() << Verbose(1) << "Final graph info for each bond is:" << endl);
    602   bond *Binder = first;
    603   while (Binder->next != last) {
    604     Binder = Binder->next;
    605     DoLog(2) && (Log() << Verbose(2) << ((Binder->Type == TreeEdge) ? "TreeEdge " : "BackEdge ") << *Binder << ": <");
    606     DoLog(0) && (Log() << Verbose(0) << ((Binder->leftatom->SeparationVertex) ? "SP," : "") << "L" << Binder->leftatom->LowpointNr << " G" << Binder->leftatom->GraphNr << " Comp.");
    607     Binder->leftatom->OutputComponentNumber();
    608     DoLog(0) && (Log() << Verbose(0) << " ===  ");
    609     DoLog(0) && (Log() << Verbose(0) << ((Binder->rightatom->SeparationVertex) ? "SP," : "") << "L" << Binder->rightatom->LowpointNr << " G" << Binder->rightatom->GraphNr << " Comp.");
    610     Binder->rightatom->OutputComponentNumber();
    611     DoLog(0) && (Log() << Verbose(0) << ">." << endl);
    612     if (Binder->Cyclic) // cyclic ??
    613       DoLog(3) && (Log() << Verbose(3) << "Lowpoint at each side are equal: CYCLIC!" << endl);
    614   }
     636  for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner)
     637    for(BondList::const_iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); BondRunner != (*AtomRunner)->ListOfBonds.end(); ++BondRunner)
     638      if ((*BondRunner)->leftatom == *AtomRunner) {
     639        Binder = *BondRunner;
     640        DoLog(2) && (Log() << Verbose(2) << ((Binder->Type == TreeEdge) ? "TreeEdge " : "BackEdge ") << *Binder << ": <");
     641        DoLog(0) && (Log() << Verbose(0) << ((Binder->leftatom->SeparationVertex) ? "SP," : "") << "L" << Binder->leftatom->LowpointNr << " G" << Binder->leftatom->GraphNr << " Comp.");
     642        Binder->leftatom->OutputComponentNumber();
     643        DoLog(0) && (Log() << Verbose(0) << " ===  ");
     644        DoLog(0) && (Log() << Verbose(0) << ((Binder->rightatom->SeparationVertex) ? "SP," : "") << "L" << Binder->rightatom->LowpointNr << " G" << Binder->rightatom->GraphNr << " Comp.");
     645        Binder->rightatom->OutputComponentNumber();
     646        DoLog(0) && (Log() << Verbose(0) << ">." << endl);
     647        if (Binder->Cyclic) // cyclic ??
     648          DoLog(3) && (Log() << Verbose(3) << "Lowpoint at each side are equal: CYCLIC!" << endl);
     649      }
    615650}
    616651;
     
    624659{
    625660  BFS.AtomCount = AtomCount;
    626   BFS.PredecessorList = Calloc<atom*> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: **PredecessorList");
    627   BFS.ShortestPathList = Malloc<int> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: *ShortestPathList");
    628   BFS.ColorList = Calloc<enum Shading> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: *ColorList");
     661  BFS.PredecessorList = new atom*[AtomCount];
     662  BFS.ShortestPathList = new int[AtomCount];
     663  BFS.ColorList = new enum Shading[AtomCount];
    629664  BFS.BFSStack = new StackClass<atom *> (AtomCount);
    630665
    631   for (int i = AtomCount; i--;)
     666  for (int i = AtomCount; i--;) {
    632667    BFS.ShortestPathList[i] = -1;
     668    BFS.PredecessorList[i] = 0;
     669  }
    633670};
    634671
     
    639676void FinalizeBFSAccounting(struct BFSAccounting &BFS)
    640677{
    641   Free(&BFS.PredecessorList);
    642   Free(&BFS.ShortestPathList);
    643   Free(&BFS.ColorList);
     678  delete[](BFS.PredecessorList);
     679  delete[](BFS.ShortestPathList);
     680  delete[](BFS.ColorList);
    644681  delete (BFS.BFSStack);
    645682  BFS.AtomCount = 0;
     
    854891  if (MinRingSize != -1) { // if rings are present
    855892    // go over all atoms
    856     Root = mol->start;
    857     while (Root->next != mol->end) {
    858       Root = Root->next;
    859 
    860       if (MinimumRingSize[Root->GetTrueFather()->nr] == mol->AtomCount) { // check whether MinimumRingSize is set, if not BFS to next where it is
     893    for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
     894      Root = *iter;
     895
     896      if (MinimumRingSize[Root->GetTrueFather()->nr] == mol->getAtomCount()) { // check whether MinimumRingSize is set, if not BFS to next where it is
    861897        Walker = Root;
    862898
    863899        //Log() << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl;
    864         CyclicStructureAnalysis_BFSToNextCycle(Root, Walker, MinimumRingSize, mol->AtomCount);
     900        CyclicStructureAnalysis_BFSToNextCycle(Root, Walker, MinimumRingSize, mol->getAtomCount());
    865901
    866902      }
     
    892928  int MinRingSize = -1;
    893929
    894   InitializeBFSAccounting(BFS, AtomCount);
     930  InitializeBFSAccounting(BFS, getAtomCount());
    895931
    896932  //Log() << Verbose(1) << "Back edge list - ";
     
    9661002void molecule::ResetAllBondsToUnused() const
    9671003{
    968   bond *Binder = first;
    969   while (Binder->next != last) {
    970     Binder = Binder->next;
    971     Binder->ResetUsed();
    972   }
     1004  for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner)
     1005    for(BondList::const_iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); BondRunner != (*AtomRunner)->ListOfBonds.end(); ++BondRunner)
     1006      if ((*BondRunner)->leftatom == *AtomRunner)
     1007        (*BondRunner)->ResetUsed();
    9731008}
    9741009;
     
    10041039    line << filename;
    10051040  AdjacencyFile.open(line.str().c_str(), ios::out);
    1006   DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... ");
     1041  DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl);
    10071042  if (AdjacencyFile != NULL) {
    10081043    AdjacencyFile << "m\tn" << endl;
    10091044    ActOnAllAtoms(&atom::OutputAdjacency, &AdjacencyFile);
    10101045    AdjacencyFile.close();
    1011     DoLog(1) && (Log() << Verbose(1) << "done." << endl);
     1046    DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl);
    10121047  } else {
    1013     DoLog(1) && (Log() << Verbose(1) << "failed to open file " << line.str() << "." << endl);
     1048    DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line.str() << "." << endl);
    10141049    status = false;
    10151050  }
     
    10361071    line << filename;
    10371072  BondFile.open(line.str().c_str(), ios::out);
    1038   DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... ");
     1073  DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl);
    10391074  if (BondFile != NULL) {
    10401075    BondFile << "m\tn" << endl;
    10411076    ActOnAllAtoms(&atom::OutputBonds, &BondFile);
    10421077    BondFile.close();
    1043     DoLog(1) && (Log() << Verbose(1) << "done." << endl);
     1078    DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl);
    10441079  } else {
    1045     DoLog(1) && (Log() << Verbose(1) << "failed to open file " << line.str() << "." << endl);
     1080    DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line.str() << "." << endl);
    10461081    status = false;
    10471082  }
     
    10561091  filename << path << "/" << FRAGMENTPREFIX << ADJACENCYFILE;
    10571092  File.open(filename.str().c_str(), ios::out);
    1058   DoLog(1) && (Log() << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... ");
     1093  DoLog(1) && (Log() << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... " << endl);
    10591094  if (File == NULL)
    10601095    return false;
    10611096
    10621097  // allocate storage structure
    1063   CurrentBonds = Calloc<int> (8, "molecule::CheckAdjacencyFileAgainstMolecule - CurrentBonds"); // contains parsed bonds of current atom
     1098  CurrentBonds = new int[8]; // contains parsed bonds of current atom
     1099  for(int i=0;i<8;i++)
     1100    CurrentBonds[i] = 0;
    10641101  return true;
    10651102}
     
    10701107  File.close();
    10711108  File.clear();
    1072   Free(&CurrentBonds);
     1109  delete[](CurrentBonds);
    10731110}
    10741111;
     
    10901127        NonMatchNumber++;
    10911128        status = false;
     1129        DoeLog(2) && (eLog() << Verbose(2) << id << " can not be found in list." << endl);
     1130      } else {
    10921131        //Log() << Verbose(0) << "[" << id << "]\t";
    1093       } else {
    1094         //Log() << Verbose(0) << id << "\t";
    10951132      }
    10961133    }
     
    11141151  bool status = true;
    11151152  atom *Walker = NULL;
    1116   char *buffer = NULL;
    11171153  int *CurrentBonds = NULL;
    11181154  int NonMatchNumber = 0; // will number of atoms with differing bond structure
    11191155  size_t CurrentBondsOfAtom = -1;
     1156  const int AtomCount = getAtomCount();
    11201157
    11211158  if (!CheckAdjacencyFileAgainstMolecule_Init(path, File, CurrentBonds)) {
     
    11241161  }
    11251162
    1126   buffer = Malloc<char> (MAXSTRINGSIZE, "molecule::CheckAdjacencyFileAgainstMolecule: *buffer");
     1163  char buffer[MAXSTRINGSIZE];
    11271164  // Parse the file line by line and count the bonds
    11281165  while (!File.eof()) {
     
    11401177      // compare against present bonds
    11411178      CheckAdjacencyFileAgainstMolecule_CompareBonds(status, NonMatchNumber, Walker, CurrentBondsOfAtom, AtomNr, CurrentBonds, ListOfAtoms);
    1142     }
    1143   }
    1144   Free(&buffer);
     1179    } else {
     1180      if (AtomNr != -1)
     1181        DoeLog(2) && (eLog() << Verbose(2) << AtomNr << " is not valid in the range of ids [" << 0 << "," << AtomCount << ")." << endl);
     1182    }
     1183  }
    11451184  CheckAdjacencyFileAgainstMolecule_Finalize(File, CurrentBonds);
    11461185
     
    11961235  BFS.AtomCount = AtomCount;
    11971236  BFS.BondOrder = BondOrder;
    1198   BFS.PredecessorList = Calloc<atom*> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: **PredecessorList");
    1199   BFS.ShortestPathList = Calloc<int> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: *ShortestPathList");
    1200   BFS.ColorList = Malloc<enum Shading> (AtomCount, "molecule::BreadthFirstSearchAdd_Init: *ColorList");
     1237  BFS.PredecessorList = new atom*[AtomCount];
     1238  BFS.ShortestPathList = new int[AtomCount];
     1239  BFS.ColorList = new enum Shading[AtomCount];
    12011240  BFS.BFSStack = new StackClass<atom *> (AtomCount);
    12021241
     
    12071246  // initialise each vertex as white with no predecessor, empty queue, color Root lightgray
    12081247  for (int i = AtomCount; i--;) {
     1248    BFS.PredecessorList[i] = NULL;
    12091249    BFS.ShortestPathList[i] = -1;
    12101250    if ((AddedAtomList != NULL) && (AddedAtomList[i] != NULL)) // mark already present atoms (i.e. Root and maybe others) as visited
     
    12131253      BFS.ColorList[i] = white;
    12141254  }
    1215   //BFS.ShortestPathList[Root->nr] = 0; //is set due to Calloc()
     1255  //BFS.ShortestPathList[Root->nr] = 0; // done by Calloc
    12161256}
    12171257;
     
    12191259void BreadthFirstSearchAdd_Free(struct BFSAccounting &BFS)
    12201260{
    1221   Free(&BFS.PredecessorList);
    1222   Free(&BFS.ShortestPathList);
    1223   Free(&BFS.ColorList);
     1261  delete[](BFS.PredecessorList);
     1262  delete[](BFS.ShortestPathList);
     1263  delete[](BFS.ColorList);
    12241264  delete (BFS.BFSStack);
    12251265  BFS.AtomCount = 0;
     
    13131353    AddedAtomList[Root->nr] = Mol->AddCopyAtom(Root);
    13141354
    1315   BreadthFirstSearchAdd_Init(BFS, Root, BondOrder, AtomCount, AddedAtomList);
     1355  BreadthFirstSearchAdd_Init(BFS, Root, BondOrder, getAtomCount(), AddedAtomList);
    13161356
    13171357  // and go on ... Queue always contains all lightgray vertices
     
    13601400{
    13611401  // reset parent list
    1362   ParentList = Calloc<atom*> (AtomCount, "molecule::BuildInducedSubgraph_Init: **ParentList");
     1402  ParentList = new atom*[AtomCount];
     1403  for (int i=0;i<AtomCount;i++)
     1404    ParentList[i] = NULL;
    13631405  DoLog(3) && (Log() << Verbose(3) << "Resetting ParentList." << endl);
    13641406}
     
    13691411  // fill parent list with sons
    13701412  DoLog(3) && (Log() << Verbose(3) << "Filling Parent List." << endl);
    1371   atom *Walker = mol->start;
    1372   while (Walker->next != mol->end) {
    1373     Walker = Walker->next;
    1374     ParentList[Walker->father->nr] = Walker;
     1413  for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
     1414    ParentList[(*iter)->father->nr] = (*iter);
    13751415    // Outputting List for debugging
    1376     DoLog(4) && (Log() << Verbose(4) << "Son[" << Walker->father->nr << "] of " << Walker->father << " is " << ParentList[Walker->father->nr] << "." << endl);
    1377   }
    1378 
    1379 }
    1380 ;
     1416    DoLog(4) && (Log() << Verbose(4) << "Son[" << (*iter)->father->nr << "] of " << (*iter)->father << " is " << ParentList[(*iter)->father->nr] << "." << endl);
     1417  }
     1418};
    13811419
    13821420void BuildInducedSubgraph_Finalize(atom **&ParentList)
    13831421{
    1384   Free(&ParentList);
     1422  delete[](ParentList);
    13851423}
    13861424;
     
    13891427{
    13901428  bool status = true;
    1391   atom *Walker = NULL;
    13921429  atom *OtherAtom = NULL;
    13931430  // check each entry of parent list and if ok (one-to-and-onto matching) create bonds
    13941431  DoLog(3) && (Log() << Verbose(3) << "Creating bonds." << endl);
    1395   Walker = Father->start;
    1396   while (Walker->next != Father->end) {
    1397     Walker = Walker->next;
    1398     if (ParentList[Walker->nr] != NULL) {
    1399       if (ParentList[Walker->nr]->father != Walker) {
     1432  for (molecule::const_iterator iter = Father->begin(); iter != Father->end(); ++iter) {
     1433    if (ParentList[(*iter)->nr] != NULL) {
     1434      if (ParentList[(*iter)->nr]->father != (*iter)) {
    14001435        status = false;
    14011436      } else {
    1402         for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
    1403           OtherAtom = (*Runner)->GetOtherAtom(Walker);
     1437        for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
     1438          OtherAtom = (*Runner)->GetOtherAtom((*iter));
    14041439          if (ParentList[OtherAtom->nr] != NULL) { // if otheratom is also a father of an atom on this molecule, create the bond
    1405             DoLog(4) && (Log() << Verbose(4) << "Endpoints of Bond " << (*Runner) << " are both present: " << ParentList[Walker->nr]->getName() << " and " << ParentList[OtherAtom->nr]->getName() << "." << endl);
    1406             mol->AddBond(ParentList[Walker->nr], ParentList[OtherAtom->nr], (*Runner)->BondDegree);
     1440            DoLog(4) && (Log() << Verbose(4) << "Endpoints of Bond " << (*Runner) << " are both present: " << ParentList[(*iter)->nr]->getName() << " and " << ParentList[OtherAtom->nr]->getName() << "." << endl);
     1441            mol->AddBond(ParentList[(*iter)->nr], ParentList[OtherAtom->nr], (*Runner)->BondDegree);
    14071442          }
    14081443        }
     
    14271462  bool status = true;
    14281463  atom **ParentList = NULL;
    1429 
    14301464  DoLog(2) && (Log() << Verbose(2) << "Begin of BuildInducedSubgraph." << endl);
    1431   BuildInducedSubgraph_Init(ParentList, Father->AtomCount);
     1465  BuildInducedSubgraph_Init(ParentList, Father->getAtomCount());
    14321466  BuildInducedSubgraph_FillParentList(this, Father, ParentList);
    14331467  status = BuildInducedSubgraph_CreateBondsFromParent(this, Father, ParentList);
Note: See TracChangeset for help on using the changeset viewer.