/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * UniqueFragments.cpp * * Created on: Oct 18, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include "UniqueFragments.hpp" #include "CodePatterns/Log.hpp" #include "atom.hpp" #include "Bond/bond.hpp" #include "Element/element.hpp" #include "graph.hpp" /** Constructor of class UniqueFragments. * */ UniqueFragments::UniqueFragments() {} /** Destructor of class UniqueFragments. * */ UniqueFragments::~UniqueFragments() {} /** Checking whether KeySet is not already present in Graph, if so just adds factor. */ void UniqueFragments::InsertFragmentIntoGraph() { GraphTestPair testGraphInsert; testGraphInsert = Leaflet->insert(GraphPair (*FragmentSet,std::pair(FragmentCounter,TEFactor))); // store fragment number and current factor if (testGraphInsert.second) { DoLog(2) && (Log() << Verbose(2) << "KeySet " << FragmentCounter << " successfully inserted." << endl); FragmentCounter++; } else { DoLog(2) && (Log() << Verbose(2) << "KeySet " << FragmentCounter << " failed to insert, present fragment is " << ((*(testGraphInsert.first)).second).first << endl); ((*(testGraphInsert.first)).second).second += TEFactor; // increase the "created" counter DoLog(2) && (Log() << Verbose(2) << "New factor is " << ((*(testGraphInsert.first)).second).second << "." << endl); } }; /** Allocates memory for UniqueFragments::BondsPerSPList. * \param Order bond order (limits BFS exploration and "number of digits" in power set generation * \sa UniqueFragments::FreeSPList() */ void UniqueFragments::InitialiseSPList(int Order) { BondsPerSPList.resize(Order); BondsPerSPCount = new int[Order]; for (int i=Order;i--;) { BondsPerSPCount[i] = 0; } }; /** Free's memory for for UniqueFragments::BondsPerSPList. * \param Order bond order (limits BFS exploration and "number of digits" in power set generation * \sa UniqueFragments::InitialiseSPList() */ void UniqueFragments::FreeSPList(int Order) { delete[](BondsPerSPCount); }; /** Sets FragmenSearch to initial value. * Sets UniqueFragments::ShortestPathList entries to zero, UniqueFragments::BondsPerSPCount to zero (except zero level to 1) and * adds initial bond UniqueFragments::Root to UniqueFragments::Root to UniqueFragments::BondsPerSPList * \param Order bond order (limits BFS exploration and "number of digits" in power set generation * \sa UniqueFragments::FreeSPList() */ void UniqueFragments::SetSPList(int Order) { // prepare Label and SP arrays of the BFS search ShortestPathList[Root->getNr()] = 0; // prepare root level (SP = 0) and a loop bond denoting Root for (int i=Order;i--;) BondsPerSPCount[i] = 0; BondsPerSPCount[0] = 1; bond *Binder = new bond(Root, Root); BondsPerSPList[0].push_back(Binder); }; /** Resets UniqueFragments::ShortestPathList and cleans bonds from UniqueFragments::BondsPerSPList. * \param Order bond order (limits BFS exploration and "number of digits" in power set generation * \sa UniqueFragments::InitialiseSPList() */ void UniqueFragments::ResetSPList(int Order) { DoLog(0) && (Log() << Verbose(0) << "Free'ing all found lists. and resetting index lists" << endl); for(int i=Order;i--;) { DoLog(1) && (Log() << Verbose(1) << "Current SP level is " << i << ": "); for (UniqueFragments::BondsPerSP::const_iterator iter = BondsPerSPList[i].begin(); iter != BondsPerSPList[i].end(); ++iter) { // Log() << Verbose(0) << "Removing atom " << Binder->leftatom->getNr() << " and " << Binder->rightatom->getNr() << "." << endl; // make sure numbers are local ShortestPathList[(*iter)->leftatom->getNr()] = -1; ShortestPathList[(*iter)->rightatom->getNr()] = -1; } // delete added bonds for (UniqueFragments::BondsPerSP::iterator iter = BondsPerSPList[i].begin(); iter != BondsPerSPList[i].end(); ++iter) { delete(*iter); } BondsPerSPList[i].clear(); // also start and end node DoLog(0) && (Log() << Verbose(0) << "cleaned." << endl); } }; /** Fills the Bonds per Shortest Path List and set the vertex labels. * \param Order bond order (limits BFS exploration and "number of digits" in power set generation * \param RestrictedKeySet Restricted vertex set to use in context of molecule */ void UniqueFragments::FillSPListandLabelVertices(int Order, KeySet &RestrictedKeySet) { // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning // tree already sorted into various SP levels. That's why we just do loops over the depth (CurrentSP) and breadth // (EdgeinSPLevel) of this tree ... // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction. int AtomKeyNr = -1; atom *Walker = NULL; atom *OtherWalker = NULL; atom *Predecessor = NULL; bond *Binder = NULL; int RootKeyNr = Root->GetTrueFather()->getNr(); int RemainingWalkers = -1; int SP = -1; DoLog(0) && (Log() << Verbose(0) << "Starting BFS analysis ..." << endl); for (SP = 0; SP < (Order-1); SP++) { DoLog(1) && (Log() << Verbose(1) << "New SP level reached: " << SP << ", creating new SP list with " << BondsPerSPCount[SP] << " item(s)"); if (SP > 0) { DoLog(0) && (Log() << Verbose(0) << ", old level closed with " << BondsPerSPCount[SP-1] << " item(s)." << endl); BondsPerSPCount[SP] = 0; } else DoLog(0) && (Log() << Verbose(0) << "." << endl); RemainingWalkers = BondsPerSPCount[SP]; for (BondsPerSP::const_iterator CurrentEdge = BondsPerSPList[SP].begin(); CurrentEdge != BondsPerSPList[SP].end(); ++CurrentEdge) { /// start till end of this SP level's list RemainingWalkers--; Walker = (*CurrentEdge)->rightatom; // rightatom is always the one more distant Predecessor = (*CurrentEdge)->leftatom; // ... and leftatom is predecessor AtomKeyNr = Walker->getNr(); DoLog(0) && (Log() << Verbose(0) << "Current Walker is: " << *Walker << " with nr " << Walker->getNr() << " and SP of " << SP << ", with " << RemainingWalkers << " remaining walkers on this level." << endl); // check for new sp level // go through all its bonds DoLog(1) && (Log() << Verbose(1) << "Going through all bonds of Walker." << endl); const BondList& ListOfBonds = Walker->getListOfBonds(); for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) { OtherWalker = (*Runner)->GetOtherAtom(Walker); if ((RestrictedKeySet.find(OtherWalker->getNr()) != RestrictedKeySet.end()) #ifdef ADDHYDROGEN && (OtherWalker->getType()->getAtomicNumber() != 1) #endif ) { // skip hydrogens and restrict to fragment DoLog(2) && (Log() << Verbose(2) << "Current partner is " << *OtherWalker << " with nr " << OtherWalker->getNr() << " in bond " << *(*Runner) << "." << endl); // set the label if not set (and push on root stack as well) if ((OtherWalker != Predecessor) && (OtherWalker->GetTrueFather()->getNr() > RootKeyNr)) { // only pass through those with label bigger than Root's ShortestPathList[OtherWalker->getNr()] = SP+1; DoLog(3) && (Log() << Verbose(3) << "Set Shortest Path to " << ShortestPathList[OtherWalker->getNr()] << "." << endl); // add the bond in between to the SP list Binder = new bond(Walker, OtherWalker); // create a new bond in such a manner, that bond::rightatom is always the one more distant BondsPerSPList[SP+1].push_back(Binder); BondsPerSPCount[SP+1]++; DoLog(3) && (Log() << Verbose(3) << "Added its bond to SP list, having now " << BondsPerSPCount[SP+1] << " item(s)." << endl); } else { if (OtherWalker != Predecessor) DoLog(3) && (Log() << Verbose(3) << "Not passing on, as index of " << *OtherWalker << " " << OtherWalker->GetTrueFather()->getNr() << " is smaller than that of Root " << RootKeyNr << "." << endl); else DoLog(3) && (Log() << Verbose(3) << "This is my predecessor " << *Predecessor << "." << endl); } } else Log() << Verbose(2) << "Is not in the restricted keyset or skipping hydrogen " << *OtherWalker << "." << endl; } } } }; /** prints the Bonds per Shortest Path list in UniqueFragments. * \param Order bond order (limits BFS exploration and "number of digits" in power set generation */ void UniqueFragments::OutputSPList(int Order) { DoLog(0) && (Log() << Verbose(0) << "Printing all found lists." << endl); for(int i=1;i Root edge must be subtracted! for(int i=Order;i--;) { // sum up all found edges for (UniqueFragments::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin(); Binder != BondsPerSPList[i].end(); ++Binder) { SP++; } } return SP; }; /** Initialization for UniqueFragments. * * \param _Root ref to atom to start from (with graph algorithms, hence root node) * \param AtomCount number of nodes/atoms */ void UniqueFragments::Init(atom *_Root, size_t AtomCount) { // initialise the fragments structure FragmentCounter = 0; FragmentSet = new KeySet; Root = _Root; ShortestPathList = new int[AtomCount]; for (int i=AtomCount;i--;) { ShortestPathList[i] = -1; } } /** Removes some allocated memory. * */ void UniqueFragments::Cleanup() { delete[](ShortestPathList); delete(FragmentSet); }