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