| [d3abb1] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | /*
 | 
|---|
 | 9 |  * MoleculeLeafClass.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Oct 20, 2011
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include "MoleculeLeafClass.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | #include "atom.hpp"
 | 
|---|
 | 27 | #include "Element/element.hpp"
 | 
|---|
 | 28 | #include "Fragmentation/Graph.hpp"
 | 
|---|
 | 29 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
 | 30 | #include "molecule.hpp"
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | /** Constructor for MoleculeLeafClass root leaf.
 | 
|---|
 | 33 |  * \param *Up Leaf on upper level
 | 
|---|
 | 34 |  * \param *PreviousLeaf NULL - We are the first leaf on this level, otherwise points to previous in list
 | 
|---|
 | 35 |  */
 | 
|---|
 | 36 | //MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *Up = NULL, MoleculeLeafClass *Previous = NULL)
 | 
|---|
 | 37 | MoleculeLeafClass::MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf = NULL) :
 | 
|---|
 | 38 |   Leaf(NULL),
 | 
|---|
 | 39 |   previous(PreviousLeaf)
 | 
|---|
 | 40 | {
 | 
|---|
 | 41 |   //  if (Up != NULL)
 | 
|---|
 | 42 |   //    if (Up->DownLeaf == NULL) // are we the first down leaf for the upper leaf?
 | 
|---|
 | 43 |   //      Up->DownLeaf = this;
 | 
|---|
 | 44 |   //  UpLeaf = Up;
 | 
|---|
 | 45 |   //  DownLeaf = NULL;
 | 
|---|
 | 46 |   if (previous != NULL) {
 | 
|---|
 | 47 |     MoleculeLeafClass *Walker = previous->next;
 | 
|---|
 | 48 |     previous->next = this;
 | 
|---|
 | 49 |     next = Walker;
 | 
|---|
 | 50 |   } else {
 | 
|---|
 | 51 |     next = NULL;
 | 
|---|
 | 52 |   }
 | 
|---|
 | 53 | };
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 | /** Destructor for MoleculeLeafClass.
 | 
|---|
 | 56 |  */
 | 
|---|
 | 57 | MoleculeLeafClass::~MoleculeLeafClass()
 | 
|---|
 | 58 | {
 | 
|---|
 | 59 |   //  if (DownLeaf != NULL) {// drop leaves further down
 | 
|---|
 | 60 |   //    MoleculeLeafClass *Walker = DownLeaf;
 | 
|---|
 | 61 |   //    MoleculeLeafClass *Next;
 | 
|---|
 | 62 |   //    do {
 | 
|---|
 | 63 |   //      Next = Walker->NextLeaf;
 | 
|---|
 | 64 |   //      delete(Walker);
 | 
|---|
 | 65 |   //      Walker = Next;
 | 
|---|
 | 66 |   //    } while (Walker != NULL);
 | 
|---|
 | 67 |   //    // Last Walker sets DownLeaf automatically to NULL
 | 
|---|
 | 68 |   //  }
 | 
|---|
 | 69 |   // remove the leaf itself
 | 
|---|
 | 70 |   if (Leaf != NULL) {
 | 
|---|
 | 71 |     Leaf->removeAtomsinMolecule();
 | 
|---|
 | 72 |     World::getInstance().destroyMolecule(Leaf);
 | 
|---|
 | 73 |     Leaf = NULL;
 | 
|---|
 | 74 |   }
 | 
|---|
 | 75 |   // remove this Leaf from level list
 | 
|---|
 | 76 |   if (previous != NULL)
 | 
|---|
 | 77 |     previous->next = next;
 | 
|---|
 | 78 |   //  } else { // we are first in list (connects to UpLeaf->DownLeaf)
 | 
|---|
 | 79 |   //    if ((NextLeaf != NULL) && (NextLeaf->UpLeaf == NULL))
 | 
|---|
 | 80 |   //      NextLeaf->UpLeaf = UpLeaf;  // either null as we are top level or the upleaf of the first node
 | 
|---|
 | 81 |   //    if (UpLeaf != NULL)
 | 
|---|
 | 82 |   //      UpLeaf->DownLeaf = NextLeaf;  // either null as we are only leaf or NextLeaf if we are just the first
 | 
|---|
 | 83 |   //  }
 | 
|---|
 | 84 |   //  UpLeaf = NULL;
 | 
|---|
 | 85 |   if (next != NULL) // are we last in list
 | 
|---|
 | 86 |     next->previous = previous;
 | 
|---|
 | 87 |   next = NULL;
 | 
|---|
 | 88 |   previous = NULL;
 | 
|---|
 | 89 | };
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 | /** Adds \a molecule leaf to the tree.
 | 
|---|
 | 92 |  * \param *ptr ptr to molecule to be added
 | 
|---|
 | 93 |  * \param *Previous previous MoleculeLeafClass referencing level and which on the level
 | 
|---|
 | 94 |  * \return true - success, false - something went wrong
 | 
|---|
 | 95 |  */
 | 
|---|
 | 96 | bool MoleculeLeafClass::AddLeaf(molecule *ptr, MoleculeLeafClass *Previous)
 | 
|---|
 | 97 | {
 | 
|---|
 | 98 |   return false;
 | 
|---|
 | 99 | };
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | /** Fills the root stack for sites to be used as root in fragmentation depending on order or adaptivity criteria
 | 
|---|
 | 102 |  * Again, as in \sa FillBondStructureFromReference steps recursively through each Leaf in this chain list of molecule's.
 | 
|---|
 | 103 |  * \param *out output stream for debugging
 | 
|---|
 | 104 |  * \param *&RootStack stack to be filled
 | 
|---|
 | 105 |  * \param *AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site
 | 
|---|
 | 106 |  * \param &FragmentCounter counts through the fragments in this MoleculeLeafClass
 | 
|---|
 | 107 |  * \return true - stack is non-empty, fragmentation necessary, false - stack is empty, no more sites to update
 | 
|---|
 | 108 |  */
 | 
|---|
 | 109 | bool MoleculeLeafClass::FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter)
 | 
|---|
 | 110 | {
 | 
|---|
 | 111 |   atom *Father = NULL;
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |   if (RootStack != NULL) {
 | 
|---|
 | 114 |     // find first root candidates
 | 
|---|
 | 115 |     if (&(RootStack[FragmentCounter]) != NULL) {
 | 
|---|
 | 116 |       RootStack[FragmentCounter].clear();
 | 
|---|
 | 117 |       for(molecule::const_iterator iter = Leaf->begin(); iter != Leaf->end(); ++iter) {
 | 
|---|
 | 118 |         Father = (*iter)->GetTrueFather();
 | 
|---|
 | 119 |         if (AtomMask[Father->getNr()]) // apply mask
 | 
|---|
 | 120 | #ifdef ADDHYDROGEN
 | 
|---|
 | 121 |           if ((*iter)->getType()->getAtomicNumber() != 1) // skip hydrogen
 | 
|---|
 | 122 | #endif
 | 
|---|
 | 123 |           RootStack[FragmentCounter].push_front((*iter)->getNr());
 | 
|---|
 | 124 |       }
 | 
|---|
 | 125 |       if (next != NULL)
 | 
|---|
 | 126 |         next->FillRootStackForSubgraphs(RootStack, AtomMask, ++FragmentCounter);
 | 
|---|
 | 127 |     } else {
 | 
|---|
 | 128 |       DoLog(1) && (Log() << Verbose(1) << "Rootstack[" << FragmentCounter << "] is NULL." << endl);
 | 
|---|
 | 129 |       return false;
 | 
|---|
 | 130 |     }
 | 
|---|
 | 131 |     FragmentCounter--;
 | 
|---|
 | 132 |     return true;
 | 
|---|
 | 133 |   } else {
 | 
|---|
 | 134 |     DoLog(1) && (Log() << Verbose(1) << "Rootstack is NULL." << endl);
 | 
|---|
 | 135 |     return false;
 | 
|---|
 | 136 |   }
 | 
|---|
 | 137 | };
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 | /** The indices per keyset are compared to the respective father's Atom::Nr in each subgraph and thus put into \a **&FragmentList.
 | 
|---|
 | 140 |  * \param *out output stream fro debugging
 | 
|---|
 | 141 |  * \param *reference reference molecule with the bond structure to be copied
 | 
|---|
 | 142 |  * \param *KeySetList list with all keysets
 | 
|---|
 | 143 |  * \param ***ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled
 | 
|---|
 | 144 |  * \param **&FragmentList list to be allocated and returned
 | 
|---|
 | 145 |  * \param &FragmentCounter counts the fragments as we move along the list
 | 
|---|
 | 146 |  * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
 | 
|---|
 | 147 |  * \retuen true - success, false - failure
 | 
|---|
 | 148 |  */
 | 
|---|
 | 149 | bool MoleculeLeafClass::AssignKeySetsToFragment(molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList)
 | 
|---|
 | 150 | {
 | 
|---|
 | 151 |   bool status = true;
 | 
|---|
 | 152 |   int KeySetCounter = 0;
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 |   DoLog(1) && (Log() << Verbose(1) << "Begin of AssignKeySetsToFragment." << endl);
 | 
|---|
 | 155 |   // fill ListOfLocalAtoms if NULL was given
 | 
|---|
 | 156 |   if (!Leaf->FillListOfLocalAtoms(ListOfLocalAtoms[FragmentCounter], reference->getAtomCount())) {
 | 
|---|
 | 157 |     DoLog(1) && (Log() << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl);
 | 
|---|
 | 158 |     return false;
 | 
|---|
 | 159 |   }
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 |   // allocate fragment list
 | 
|---|
 | 162 |   if (FragmentList == NULL) {
 | 
|---|
 | 163 |     KeySetCounter = Count();
 | 
|---|
 | 164 |     FragmentList = new Graph*[KeySetCounter];
 | 
|---|
 | 165 |     for (int i=0;i<KeySetCounter;i++)
 | 
|---|
 | 166 |       FragmentList[i] = NULL;
 | 
|---|
 | 167 |     KeySetCounter = 0;
 | 
|---|
 | 168 |   }
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 |   if ((KeySetList != NULL) && (KeySetList->size() != 0)) { // if there are some scanned keysets at all
 | 
|---|
 | 171 |     // assign scanned keysets
 | 
|---|
 | 172 |     if (FragmentList[FragmentCounter] == NULL)
 | 
|---|
 | 173 |       FragmentList[FragmentCounter] = new Graph;
 | 
|---|
 | 174 |     KeySet *TempSet = new KeySet;
 | 
|---|
 | 175 |     for (Graph::iterator runner = KeySetList->begin(); runner != KeySetList->end(); runner++) { // key sets contain global numbers!
 | 
|---|
 | 176 |       if (ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*((*runner).first.begin()))->getNr()] != NULL) {// as we may assume that that bond structure is unchanged, we only test the first key in each set
 | 
|---|
 | 177 |         // translate keyset to local numbers
 | 
|---|
 | 178 |         for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
 | 
|---|
 | 179 |           TempSet->insert(ListOfLocalAtoms[FragmentCounter][reference->FindAtom(*sprinter)->getNr()]->getNr());
 | 
|---|
 | 180 |         // insert into FragmentList
 | 
|---|
 | 181 |         FragmentList[FragmentCounter]->insert(GraphPair(*TempSet, pair<int, double> (KeySetCounter++, (*runner).second.second)));
 | 
|---|
 | 182 |       }
 | 
|---|
 | 183 |       TempSet->clear();
 | 
|---|
 | 184 |     }
 | 
|---|
 | 185 |     delete (TempSet);
 | 
|---|
 | 186 |     if (KeySetCounter == 0) {// if there are no keysets, delete the list
 | 
|---|
 | 187 |       DoLog(1) && (Log() << Verbose(1) << "KeySetCounter is zero, deleting FragmentList." << endl);
 | 
|---|
 | 188 |       delete (FragmentList[FragmentCounter]);
 | 
|---|
 | 189 |     } else
 | 
|---|
 | 190 |       DoLog(1) && (Log() << Verbose(1) << KeySetCounter << " keysets were assigned to subgraph " << FragmentCounter << "." << endl);
 | 
|---|
 | 191 |     FragmentCounter++;
 | 
|---|
 | 192 |     if (next != NULL)
 | 
|---|
 | 193 |       next->AssignKeySetsToFragment(reference, KeySetList, ListOfLocalAtoms, FragmentList, FragmentCounter, FreeList);
 | 
|---|
 | 194 |     FragmentCounter--;
 | 
|---|
 | 195 |   } else
 | 
|---|
 | 196 |     DoLog(1) && (Log() << Verbose(1) << "KeySetList is NULL or empty." << endl);
 | 
|---|
 | 197 | 
 | 
|---|
 | 198 |   if ((FreeList) && (ListOfLocalAtoms != NULL)) {
 | 
|---|
 | 199 |     // free the index lookup list
 | 
|---|
 | 200 |     delete[](ListOfLocalAtoms[FragmentCounter]);
 | 
|---|
 | 201 |   }
 | 
|---|
 | 202 |   DoLog(1) && (Log() << Verbose(1) << "End of AssignKeySetsToFragment." << endl);
 | 
|---|
 | 203 |   return status;
 | 
|---|
 | 204 | };
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 | /** Translate list into global numbers (i.e. ones that are valid in "this" molecule, not in MolecularWalker->Leaf)
 | 
|---|
 | 207 |  * \param *out output stream for debugging
 | 
|---|
 | 208 |  * \param **FragmentList Graph with local numbers per fragment
 | 
|---|
 | 209 |  * \param &FragmentCounter counts the fragments as we move along the list
 | 
|---|
 | 210 |  * \param &TotalNumberOfKeySets global key set counter
 | 
|---|
 | 211 |  * \param &TotalGraph Graph to be filled with global numbers
 | 
|---|
 | 212 |  */
 | 
|---|
 | 213 | void MoleculeLeafClass::TranslateIndicesToGlobalIDs(Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph)
 | 
|---|
 | 214 | {
 | 
|---|
 | 215 |   DoLog(1) && (Log() << Verbose(1) << "Begin of TranslateIndicesToGlobalIDs." << endl);
 | 
|---|
 | 216 |   KeySet *TempSet = new KeySet;
 | 
|---|
 | 217 |   if (FragmentList[FragmentCounter] != NULL) {
 | 
|---|
 | 218 |     for (Graph::iterator runner = FragmentList[FragmentCounter]->begin(); runner != FragmentList[FragmentCounter]->end(); runner++) {
 | 
|---|
 | 219 |       for (KeySet::iterator sprinter = (*runner).first.begin(); sprinter != (*runner).first.end(); sprinter++)
 | 
|---|
 | 220 |         TempSet->insert((Leaf->FindAtom(*sprinter))->GetTrueFather()->getNr());
 | 
|---|
 | 221 |       TotalGraph.insert(GraphPair(*TempSet, pair<int, double> (TotalNumberOfKeySets++, (*runner).second.second)));
 | 
|---|
 | 222 |       TempSet->clear();
 | 
|---|
 | 223 |     }
 | 
|---|
 | 224 |     delete (TempSet);
 | 
|---|
 | 225 |   } else {
 | 
|---|
 | 226 |     DoLog(1) && (Log() << Verbose(1) << "FragmentList is NULL." << endl);
 | 
|---|
 | 227 |   }
 | 
|---|
 | 228 |   if (next != NULL)
 | 
|---|
 | 229 |     next->TranslateIndicesToGlobalIDs(FragmentList, ++FragmentCounter, TotalNumberOfKeySets, TotalGraph);
 | 
|---|
 | 230 |   FragmentCounter--;
 | 
|---|
 | 231 |   DoLog(1) && (Log() << Verbose(1) << "End of TranslateIndicesToGlobalIDs." << endl);
 | 
|---|
 | 232 | };
 | 
|---|
 | 233 | 
 | 
|---|
 | 234 | /** Simply counts the number of items in the list, from given MoleculeLeafClass.
 | 
|---|
 | 235 |  * \return number of items
 | 
|---|
 | 236 |  */
 | 
|---|
 | 237 | int MoleculeLeafClass::Count() const
 | 
|---|
 | 238 | {
 | 
|---|
 | 239 |   if (next != NULL)
 | 
|---|
 | 240 |     return next->Count() + 1;
 | 
|---|
 | 241 |   else
 | 
|---|
 | 242 |     return 1;
 | 
|---|
 | 243 | };
 | 
|---|
 | 244 | 
 | 
|---|