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"
|
---|
29 | #include "Fragmentation/KeySet.hpp"
|
---|
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 | * \param saturation this tells whether to treat hydrogen special or not.
|
---|
104 | */
|
---|
105 | void BondsPerShortestPath::FillSPListandLabelVertices(int _RootKeyNr, KeySet &RestrictedKeySet, const enum HydrogenSaturation saturation)
|
---|
106 | {
|
---|
107 | // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into
|
---|
108 | // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning
|
---|
109 | // tree already sorted into various SP levels. That's why we just do loops over the depth (CurrentSP) and breadth
|
---|
110 | // (EdgeinSPLevel) of this tree ...
|
---|
111 | // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence
|
---|
112 | // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction.
|
---|
113 | int AtomKeyNr = -1;
|
---|
114 | atom *Walker = NULL;
|
---|
115 | atom *OtherWalker = NULL;
|
---|
116 | atom *Predecessor = NULL;
|
---|
117 | bond *Binder = NULL;
|
---|
118 | int RootKeyNr = _RootKeyNr;
|
---|
119 | int RemainingWalkers = -1;
|
---|
120 | int SP = -1;
|
---|
121 |
|
---|
122 | DoLog(0) && (Log() << Verbose(0) << "Starting BFS analysis ..." << endl);
|
---|
123 | for (SP = 0; SP < (Order-1); SP++) {
|
---|
124 | DoLog(1) && (Log() << Verbose(1) << "New SP level reached: " << SP << ", creating new SP list with " << BondsPerSPCount[SP] << " item(s)");
|
---|
125 | if (SP > 0) {
|
---|
126 | DoLog(0) && (Log() << Verbose(0) << ", old level closed with " << BondsPerSPCount[SP-1] << " item(s)." << endl);
|
---|
127 | BondsPerSPCount[SP] = 0;
|
---|
128 | } else
|
---|
129 | DoLog(0) && (Log() << Verbose(0) << "." << endl);
|
---|
130 |
|
---|
131 | RemainingWalkers = BondsPerSPCount[SP];
|
---|
132 | for (BondsPerSP::const_iterator CurrentEdge = BondsPerSPList[SP].begin();
|
---|
133 | CurrentEdge != BondsPerSPList[SP].end();
|
---|
134 | ++CurrentEdge) { /// start till end of this SP level's list
|
---|
135 | RemainingWalkers--;
|
---|
136 | Walker = (*CurrentEdge)->rightatom; // rightatom is always the one more distant
|
---|
137 | Predecessor = (*CurrentEdge)->leftatom; // ... and leftatom is predecessor
|
---|
138 | AtomKeyNr = Walker->getNr();
|
---|
139 | 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);
|
---|
140 | // check for new sp level
|
---|
141 | // go through all its bonds
|
---|
142 | DoLog(1) && (Log() << Verbose(1) << "Going through all bonds of Walker." << endl);
|
---|
143 | const BondList& ListOfBonds = Walker->getListOfBonds();
|
---|
144 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
145 | Runner != ListOfBonds.end();
|
---|
146 | ++Runner) {
|
---|
147 | OtherWalker = (*Runner)->GetOtherAtom(Walker);
|
---|
148 | if ((RestrictedKeySet.find(OtherWalker->getNr()) != RestrictedKeySet.end())
|
---|
149 | // skip hydrogens if desired and restrict to fragment
|
---|
150 | && ((saturation == DontSaturate) || (OtherWalker->getType()->getAtomicNumber() != 1))) {
|
---|
151 | DoLog(2) && (Log() << Verbose(2) << "Current partner is " << *OtherWalker << " with nr " << OtherWalker->getNr() << " in bond " << *(*Runner) << "." << endl);
|
---|
152 | // set the label if not set (and push on root stack as well)
|
---|
153 | if ((OtherWalker != Predecessor) && (OtherWalker->GetTrueFather()->getNr() > RootKeyNr)) { // only pass through those with label bigger than Root's
|
---|
154 | // add the bond in between to the SP list
|
---|
155 | Binder = new bond(Walker, OtherWalker); // create a new bond in such a manner, that bond::rightatom is always the one more distant
|
---|
156 | BondsPerSPList[SP+1].push_back(Binder);
|
---|
157 | BondsPerSPCount[SP+1]++;
|
---|
158 | DoLog(3) && (Log() << Verbose(3) << "Added its bond to SP list, having now " << BondsPerSPCount[SP+1] << " item(s)." << endl);
|
---|
159 | } else {
|
---|
160 | if (OtherWalker != Predecessor)
|
---|
161 | DoLog(3) && (Log() << Verbose(3) << "Not passing on, as index of " << *OtherWalker << " " << OtherWalker->GetTrueFather()->getNr() << " is smaller than that of Root " << RootKeyNr << "." << endl);
|
---|
162 | else
|
---|
163 | DoLog(3) && (Log() << Verbose(3) << "This is my predecessor " << *Predecessor << "." << endl);
|
---|
164 | }
|
---|
165 | } else Log() << Verbose(2) << "Is not in the restricted keyset or skipping hydrogen " << *OtherWalker << "." << endl;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | };
|
---|
170 |
|
---|
171 | /** prints the Bonds per Shortest Path list in BondsPerShortestPath.
|
---|
172 | */
|
---|
173 | void BondsPerShortestPath::OutputSPList()
|
---|
174 | {
|
---|
175 | DoLog(0) && (Log() << Verbose(0) << "Printing all found lists." << endl);
|
---|
176 | for(int i=1;i<Order;i++) { // skip the root edge in the printing
|
---|
177 | DoLog(1) && (Log() << Verbose(1) << "Current SP level is " << i << "." << endl);
|
---|
178 | for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
|
---|
179 | Binder != BondsPerSPList[i].end();
|
---|
180 | ++Binder) {
|
---|
181 | DoLog(2) && (Log() << Verbose(2) << *Binder << endl);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | };
|
---|
185 |
|
---|
186 | /** Simply counts all bonds in all BondsPerShortestPath::BondsPerSPList lists.
|
---|
187 | */
|
---|
188 | int BondsPerShortestPath::CountNumbersInBondsList()
|
---|
189 | {
|
---|
190 | int SP = -1; // the Root <-> Root edge must be subtracted!
|
---|
191 | for(int i=Order;i--;) { // sum up all found edges
|
---|
192 | for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
|
---|
193 | Binder != BondsPerSPList[i].end();
|
---|
194 | ++Binder) {
|
---|
195 | SP++;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | return SP;
|
---|
199 | };
|
---|
200 |
|
---|
201 | /** Getter for BondsPerShortestPath::Order.
|
---|
202 | *
|
---|
203 | * @return returns BondsPerShortestPath::Order
|
---|
204 | */
|
---|
205 | int BondsPerShortestPath::getOrder() const
|
---|
206 | {
|
---|
207 | return Order;
|
---|
208 | }
|
---|