[212c179] | 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 | * BondsPerShortestPath.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Oct 18, 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 "BondsPerShortestPath.hpp"
|
---|
| 23 |
|
---|
| 24 | #include "CodePatterns/Log.hpp"
|
---|
| 25 |
|
---|
| 26 | #include "atom.hpp"
|
---|
| 27 | #include "Bond/bond.hpp"
|
---|
| 28 | #include "Element/element.hpp"
|
---|
[f0674a] | 29 | #include "Fragmentation/KeySet.hpp"
|
---|
[212c179] | 30 |
|
---|
| 31 | BondsPerShortestPath::BondsPerShortestPath(int _Order) :
|
---|
| 32 | Order(_Order)
|
---|
| 33 | {
|
---|
| 34 | InitialiseSPList();
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | BondsPerShortestPath::~BondsPerShortestPath()
|
---|
| 38 | {
|
---|
| 39 | // free Order-dependent entries of UniqueFragments structure for next loop cycle
|
---|
| 40 | FreeSPList();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /** Allocates memory for BondsPerShortestPath::BondsPerSPList.
|
---|
| 44 | * \sa BondsPerShortestPath::FreeSPList()
|
---|
| 45 | */
|
---|
| 46 | void BondsPerShortestPath::InitialiseSPList()
|
---|
| 47 | {
|
---|
| 48 | BondsPerSPList.resize(Order);
|
---|
| 49 | BondsPerSPCount = new int[Order];
|
---|
| 50 | for (int i=Order;i--;) {
|
---|
| 51 | BondsPerSPCount[i] = 0;
|
---|
| 52 | }
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | /** Free's memory for for BondsPerShortestPath::BondsPerSPList.
|
---|
| 56 | * \sa BondsPerShortestPath::InitialiseSPList()
|
---|
| 57 | */
|
---|
| 58 | void BondsPerShortestPath::FreeSPList()
|
---|
| 59 | {
|
---|
| 60 | delete[](BondsPerSPCount);
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | /** Sets FragmenSearch to initial value.
|
---|
| 64 | * Sets BondsPerShortestPath::ShortestPathList entries to zero, BondsPerShortestPath::BondsPerSPCount to zero (except zero level to 1) and
|
---|
| 65 | * adds initial bond BondsPerShortestPath::Root to BondsPerShortestPath::Root to BondsPerShortestPath::BondsPerSPList
|
---|
| 66 | * \param *_Root root node, self loop becomes first bond
|
---|
| 67 | * \sa BondsPerShortestPath::FreeSPList()
|
---|
| 68 | */
|
---|
| 69 | void BondsPerShortestPath::SetSPList(atom *_Root)
|
---|
| 70 | {
|
---|
| 71 | // prepare root level (SP = 0) and a loop bond denoting Root
|
---|
| 72 | for (int i=Order;i--;)
|
---|
| 73 | BondsPerSPCount[i] = 0;
|
---|
| 74 | BondsPerSPCount[0] = 1;
|
---|
| 75 | bond *Binder = new bond(_Root, _Root);
|
---|
| 76 | BondsPerSPList[0].push_back(Binder);
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | /** Resets BondsPerShortestPath::ShortestPathList and cleans bonds from BondsPerShortestPath::BondsPerSPList.
|
---|
| 80 | * \sa BondsPerShortestPath::InitialiseSPList()
|
---|
| 81 | */
|
---|
| 82 | void BondsPerShortestPath::ResetSPList()
|
---|
| 83 | {
|
---|
| 84 | DoLog(0) && (Log() << Verbose(0) << "Free'ing all found lists. and resetting index lists" << endl);
|
---|
| 85 | for(int i=Order;i--;) {
|
---|
| 86 | DoLog(1) && (Log() << Verbose(1) << "Current SP level is " << i << ": ");
|
---|
| 87 | // delete added bonds
|
---|
| 88 | for (BondsPerSP::iterator iter = BondsPerSPList[i].begin();
|
---|
| 89 | iter != BondsPerSPList[i].end();
|
---|
| 90 | ++iter) {
|
---|
| 91 | delete(*iter);
|
---|
| 92 | }
|
---|
| 93 | BondsPerSPList[i].clear();
|
---|
| 94 | // also start and end node
|
---|
| 95 | DoLog(0) && (Log() << Verbose(0) << "cleaned." << endl);
|
---|
| 96 | }
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | /** Fills the Bonds per Shortest Path List and set the vertex labels.
|
---|
| 101 | * \param _RootKeyNr index of root node
|
---|
| 102 | * \param RestrictedKeySet Restricted vertex set to use in context of molecule
|
---|
| 103 | */
|
---|
| 104 | void BondsPerShortestPath::FillSPListandLabelVertices(int _RootKeyNr, KeySet &RestrictedKeySet)
|
---|
| 105 | {
|
---|
| 106 | // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into
|
---|
| 107 | // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning
|
---|
| 108 | // tree already sorted into various SP levels. That's why we just do loops over the depth (CurrentSP) and breadth
|
---|
| 109 | // (EdgeinSPLevel) of this tree ...
|
---|
| 110 | // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence
|
---|
| 111 | // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction.
|
---|
| 112 | int AtomKeyNr = -1;
|
---|
| 113 | atom *Walker = NULL;
|
---|
| 114 | atom *OtherWalker = NULL;
|
---|
| 115 | atom *Predecessor = NULL;
|
---|
| 116 | bond *Binder = NULL;
|
---|
| 117 | int RootKeyNr = _RootKeyNr;
|
---|
| 118 | int RemainingWalkers = -1;
|
---|
| 119 | int SP = -1;
|
---|
| 120 |
|
---|
| 121 | DoLog(0) && (Log() << Verbose(0) << "Starting BFS analysis ..." << endl);
|
---|
| 122 | for (SP = 0; SP < (Order-1); SP++) {
|
---|
| 123 | DoLog(1) && (Log() << Verbose(1) << "New SP level reached: " << SP << ", creating new SP list with " << BondsPerSPCount[SP] << " item(s)");
|
---|
| 124 | if (SP > 0) {
|
---|
| 125 | DoLog(0) && (Log() << Verbose(0) << ", old level closed with " << BondsPerSPCount[SP-1] << " item(s)." << endl);
|
---|
| 126 | BondsPerSPCount[SP] = 0;
|
---|
| 127 | } else
|
---|
| 128 | DoLog(0) && (Log() << Verbose(0) << "." << endl);
|
---|
| 129 |
|
---|
| 130 | RemainingWalkers = BondsPerSPCount[SP];
|
---|
| 131 | for (BondsPerSP::const_iterator CurrentEdge = BondsPerSPList[SP].begin();
|
---|
| 132 | CurrentEdge != BondsPerSPList[SP].end();
|
---|
| 133 | ++CurrentEdge) { /// start till end of this SP level's list
|
---|
| 134 | RemainingWalkers--;
|
---|
| 135 | Walker = (*CurrentEdge)->rightatom; // rightatom is always the one more distant
|
---|
| 136 | Predecessor = (*CurrentEdge)->leftatom; // ... and leftatom is predecessor
|
---|
| 137 | AtomKeyNr = Walker->getNr();
|
---|
| 138 | 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);
|
---|
| 139 | // check for new sp level
|
---|
| 140 | // go through all its bonds
|
---|
| 141 | DoLog(1) && (Log() << Verbose(1) << "Going through all bonds of Walker." << endl);
|
---|
| 142 | const BondList& ListOfBonds = Walker->getListOfBonds();
|
---|
| 143 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 144 | Runner != ListOfBonds.end();
|
---|
| 145 | ++Runner) {
|
---|
| 146 | OtherWalker = (*Runner)->GetOtherAtom(Walker);
|
---|
| 147 | if ((RestrictedKeySet.find(OtherWalker->getNr()) != RestrictedKeySet.end())
|
---|
| 148 | #ifdef ADDHYDROGEN
|
---|
| 149 | && (OtherWalker->getType()->getAtomicNumber() != 1)
|
---|
| 150 | #endif
|
---|
| 151 | ) { // skip hydrogens and restrict to fragment
|
---|
| 152 | DoLog(2) && (Log() << Verbose(2) << "Current partner is " << *OtherWalker << " with nr " << OtherWalker->getNr() << " in bond " << *(*Runner) << "." << endl);
|
---|
| 153 | // set the label if not set (and push on root stack as well)
|
---|
| 154 | if ((OtherWalker != Predecessor) && (OtherWalker->GetTrueFather()->getNr() > RootKeyNr)) { // only pass through those with label bigger than Root's
|
---|
| 155 | // add the bond in between to the SP list
|
---|
| 156 | Binder = new bond(Walker, OtherWalker); // create a new bond in such a manner, that bond::rightatom is always the one more distant
|
---|
| 157 | BondsPerSPList[SP+1].push_back(Binder);
|
---|
| 158 | BondsPerSPCount[SP+1]++;
|
---|
| 159 | DoLog(3) && (Log() << Verbose(3) << "Added its bond to SP list, having now " << BondsPerSPCount[SP+1] << " item(s)." << endl);
|
---|
| 160 | } else {
|
---|
| 161 | if (OtherWalker != Predecessor)
|
---|
| 162 | DoLog(3) && (Log() << Verbose(3) << "Not passing on, as index of " << *OtherWalker << " " << OtherWalker->GetTrueFather()->getNr() << " is smaller than that of Root " << RootKeyNr << "." << endl);
|
---|
| 163 | else
|
---|
| 164 | DoLog(3) && (Log() << Verbose(3) << "This is my predecessor " << *Predecessor << "." << endl);
|
---|
| 165 | }
|
---|
| 166 | } else Log() << Verbose(2) << "Is not in the restricted keyset or skipping hydrogen " << *OtherWalker << "." << endl;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | };
|
---|
| 171 |
|
---|
| 172 | /** prints the Bonds per Shortest Path list in BondsPerShortestPath.
|
---|
| 173 | */
|
---|
| 174 | void BondsPerShortestPath::OutputSPList()
|
---|
| 175 | {
|
---|
| 176 | DoLog(0) && (Log() << Verbose(0) << "Printing all found lists." << endl);
|
---|
| 177 | for(int i=1;i<Order;i++) { // skip the root edge in the printing
|
---|
| 178 | DoLog(1) && (Log() << Verbose(1) << "Current SP level is " << i << "." << endl);
|
---|
| 179 | for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
|
---|
| 180 | Binder != BondsPerSPList[i].end();
|
---|
| 181 | ++Binder) {
|
---|
| 182 | DoLog(2) && (Log() << Verbose(2) << *Binder << endl);
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | /** Simply counts all bonds in all BondsPerShortestPath::BondsPerSPList lists.
|
---|
| 188 | */
|
---|
| 189 | int BondsPerShortestPath::CountNumbersInBondsList()
|
---|
| 190 | {
|
---|
| 191 | int SP = -1; // the Root <-> Root edge must be subtracted!
|
---|
| 192 | for(int i=Order;i--;) { // sum up all found edges
|
---|
| 193 | for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
|
---|
| 194 | Binder != BondsPerSPList[i].end();
|
---|
| 195 | ++Binder) {
|
---|
| 196 | SP++;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | return SP;
|
---|
| 200 | };
|
---|
| 201 |
|
---|
| 202 | /** Getter for BondsPerShortestPath::Order.
|
---|
| 203 | *
|
---|
| 204 | * @return returns BondsPerShortestPath::Order
|
---|
| 205 | */
|
---|
| 206 | int BondsPerShortestPath::getOrder() const
|
---|
| 207 | {
|
---|
| 208 | return Order;
|
---|
| 209 | }
|
---|