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 | * UniqueFragments.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 "UniqueFragments.hpp"
|
---|
23 |
|
---|
24 | #include "CodePatterns/Log.hpp"
|
---|
25 |
|
---|
26 | #include "atom.hpp"
|
---|
27 | #include "Bond/bond.hpp"
|
---|
28 | #include "Element/element.hpp"
|
---|
29 | #include "graph.hpp"
|
---|
30 |
|
---|
31 | /** Constructor of class UniqueFragments.
|
---|
32 | *
|
---|
33 | */
|
---|
34 | UniqueFragments::UniqueFragments()
|
---|
35 | {}
|
---|
36 |
|
---|
37 | /** Destructor of class UniqueFragments.
|
---|
38 | *
|
---|
39 | */
|
---|
40 | UniqueFragments::~UniqueFragments()
|
---|
41 | {}
|
---|
42 |
|
---|
43 | /** Checking whether KeySet is not already present in Graph, if so just adds factor.
|
---|
44 | */
|
---|
45 | void UniqueFragments::InsertFragmentIntoGraph()
|
---|
46 | {
|
---|
47 | GraphTestPair testGraphInsert;
|
---|
48 |
|
---|
49 | testGraphInsert = Leaflet->insert(GraphPair (*FragmentSet,std::pair<int,double>(FragmentCounter,TEFactor))); // store fragment number and current factor
|
---|
50 | if (testGraphInsert.second) {
|
---|
51 | DoLog(2) && (Log() << Verbose(2) << "KeySet " << FragmentCounter << " successfully inserted." << endl);
|
---|
52 | FragmentCounter++;
|
---|
53 | } else {
|
---|
54 | DoLog(2) && (Log() << Verbose(2) << "KeySet " << FragmentCounter << " failed to insert, present fragment is " << ((*(testGraphInsert.first)).second).first << endl);
|
---|
55 | ((*(testGraphInsert.first)).second).second += TEFactor; // increase the "created" counter
|
---|
56 | DoLog(2) && (Log() << Verbose(2) << "New factor is " << ((*(testGraphInsert.first)).second).second << "." << endl);
|
---|
57 | }
|
---|
58 | };
|
---|
59 |
|
---|
60 | /** Allocates memory for UniqueFragments::BondsPerSPList.
|
---|
61 | * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
|
---|
62 | * \sa UniqueFragments::FreeSPList()
|
---|
63 | */
|
---|
64 | void UniqueFragments::InitialiseSPList(int Order)
|
---|
65 | {
|
---|
66 | BondsPerSPList.resize(Order);
|
---|
67 | BondsPerSPCount = new int[Order];
|
---|
68 | for (int i=Order;i--;) {
|
---|
69 | BondsPerSPCount[i] = 0;
|
---|
70 | }
|
---|
71 | };
|
---|
72 |
|
---|
73 | /** Free's memory for for UniqueFragments::BondsPerSPList.
|
---|
74 | * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
|
---|
75 | * \sa UniqueFragments::InitialiseSPList()
|
---|
76 | */
|
---|
77 | void UniqueFragments::FreeSPList(int Order)
|
---|
78 | {
|
---|
79 | delete[](BondsPerSPCount);
|
---|
80 | };
|
---|
81 |
|
---|
82 | /** Sets FragmenSearch to initial value.
|
---|
83 | * Sets UniqueFragments::ShortestPathList entries to zero, UniqueFragments::BondsPerSPCount to zero (except zero level to 1) and
|
---|
84 | * adds initial bond UniqueFragments::Root to UniqueFragments::Root to UniqueFragments::BondsPerSPList
|
---|
85 | * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
|
---|
86 | * \sa UniqueFragments::FreeSPList()
|
---|
87 | */
|
---|
88 | void UniqueFragments::SetSPList(int Order)
|
---|
89 | {
|
---|
90 | // prepare Label and SP arrays of the BFS search
|
---|
91 | ShortestPathList[Root->getNr()] = 0;
|
---|
92 |
|
---|
93 | // prepare root level (SP = 0) and a loop bond denoting Root
|
---|
94 | for (int i=Order;i--;)
|
---|
95 | BondsPerSPCount[i] = 0;
|
---|
96 | BondsPerSPCount[0] = 1;
|
---|
97 | bond *Binder = new bond(Root, Root);
|
---|
98 | BondsPerSPList[0].push_back(Binder);
|
---|
99 | };
|
---|
100 |
|
---|
101 | /** Resets UniqueFragments::ShortestPathList and cleans bonds from UniqueFragments::BondsPerSPList.
|
---|
102 | * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
|
---|
103 | * \sa UniqueFragments::InitialiseSPList()
|
---|
104 | */
|
---|
105 | void UniqueFragments::ResetSPList(int Order)
|
---|
106 | {
|
---|
107 | DoLog(0) && (Log() << Verbose(0) << "Free'ing all found lists. and resetting index lists" << endl);
|
---|
108 | for(int i=Order;i--;) {
|
---|
109 | DoLog(1) && (Log() << Verbose(1) << "Current SP level is " << i << ": ");
|
---|
110 | for (UniqueFragments::BondsPerSP::const_iterator iter = BondsPerSPList[i].begin();
|
---|
111 | iter != BondsPerSPList[i].end();
|
---|
112 | ++iter) {
|
---|
113 | // Log() << Verbose(0) << "Removing atom " << Binder->leftatom->getNr() << " and " << Binder->rightatom->getNr() << "." << endl; // make sure numbers are local
|
---|
114 | ShortestPathList[(*iter)->leftatom->getNr()] = -1;
|
---|
115 | ShortestPathList[(*iter)->rightatom->getNr()] = -1;
|
---|
116 | }
|
---|
117 | // delete added bonds
|
---|
118 | for (UniqueFragments::BondsPerSP::iterator iter = BondsPerSPList[i].begin();
|
---|
119 | iter != BondsPerSPList[i].end();
|
---|
120 | ++iter) {
|
---|
121 | delete(*iter);
|
---|
122 | }
|
---|
123 | BondsPerSPList[i].clear();
|
---|
124 | // also start and end node
|
---|
125 | DoLog(0) && (Log() << Verbose(0) << "cleaned." << endl);
|
---|
126 | }
|
---|
127 | };
|
---|
128 |
|
---|
129 |
|
---|
130 | /** Fills the Bonds per Shortest Path List and set the vertex labels.
|
---|
131 | * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
|
---|
132 | * \param RestrictedKeySet Restricted vertex set to use in context of molecule
|
---|
133 | */
|
---|
134 | void UniqueFragments::FillSPListandLabelVertices(int Order, KeySet &RestrictedKeySet)
|
---|
135 | {
|
---|
136 | // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into
|
---|
137 | // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning
|
---|
138 | // tree already sorted into various SP levels. That's why we just do loops over the depth (CurrentSP) and breadth
|
---|
139 | // (EdgeinSPLevel) of this tree ...
|
---|
140 | // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence
|
---|
141 | // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction.
|
---|
142 | int AtomKeyNr = -1;
|
---|
143 | atom *Walker = NULL;
|
---|
144 | atom *OtherWalker = NULL;
|
---|
145 | atom *Predecessor = NULL;
|
---|
146 | bond *Binder = NULL;
|
---|
147 | int RootKeyNr = Root->GetTrueFather()->getNr();
|
---|
148 | int RemainingWalkers = -1;
|
---|
149 | int SP = -1;
|
---|
150 |
|
---|
151 | DoLog(0) && (Log() << Verbose(0) << "Starting BFS analysis ..." << endl);
|
---|
152 | for (SP = 0; SP < (Order-1); SP++) {
|
---|
153 | DoLog(1) && (Log() << Verbose(1) << "New SP level reached: " << SP << ", creating new SP list with " << BondsPerSPCount[SP] << " item(s)");
|
---|
154 | if (SP > 0) {
|
---|
155 | DoLog(0) && (Log() << Verbose(0) << ", old level closed with " << BondsPerSPCount[SP-1] << " item(s)." << endl);
|
---|
156 | BondsPerSPCount[SP] = 0;
|
---|
157 | } else
|
---|
158 | DoLog(0) && (Log() << Verbose(0) << "." << endl);
|
---|
159 |
|
---|
160 | RemainingWalkers = BondsPerSPCount[SP];
|
---|
161 | for (BondsPerSP::const_iterator CurrentEdge = BondsPerSPList[SP].begin();
|
---|
162 | CurrentEdge != BondsPerSPList[SP].end();
|
---|
163 | ++CurrentEdge) { /// start till end of this SP level's list
|
---|
164 | RemainingWalkers--;
|
---|
165 | Walker = (*CurrentEdge)->rightatom; // rightatom is always the one more distant
|
---|
166 | Predecessor = (*CurrentEdge)->leftatom; // ... and leftatom is predecessor
|
---|
167 | AtomKeyNr = Walker->getNr();
|
---|
168 | 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);
|
---|
169 | // check for new sp level
|
---|
170 | // go through all its bonds
|
---|
171 | DoLog(1) && (Log() << Verbose(1) << "Going through all bonds of Walker." << endl);
|
---|
172 | const BondList& ListOfBonds = Walker->getListOfBonds();
|
---|
173 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
174 | Runner != ListOfBonds.end();
|
---|
175 | ++Runner) {
|
---|
176 | OtherWalker = (*Runner)->GetOtherAtom(Walker);
|
---|
177 | if ((RestrictedKeySet.find(OtherWalker->getNr()) != RestrictedKeySet.end())
|
---|
178 | #ifdef ADDHYDROGEN
|
---|
179 | && (OtherWalker->getType()->getAtomicNumber() != 1)
|
---|
180 | #endif
|
---|
181 | ) { // skip hydrogens and restrict to fragment
|
---|
182 | DoLog(2) && (Log() << Verbose(2) << "Current partner is " << *OtherWalker << " with nr " << OtherWalker->getNr() << " in bond " << *(*Runner) << "." << endl);
|
---|
183 | // set the label if not set (and push on root stack as well)
|
---|
184 | if ((OtherWalker != Predecessor) && (OtherWalker->GetTrueFather()->getNr() > RootKeyNr)) { // only pass through those with label bigger than Root's
|
---|
185 | ShortestPathList[OtherWalker->getNr()] = SP+1;
|
---|
186 | DoLog(3) && (Log() << Verbose(3) << "Set Shortest Path to " << ShortestPathList[OtherWalker->getNr()] << "." << endl);
|
---|
187 | // add the bond in between to the SP list
|
---|
188 | Binder = new bond(Walker, OtherWalker); // create a new bond in such a manner, that bond::rightatom is always the one more distant
|
---|
189 | BondsPerSPList[SP+1].push_back(Binder);
|
---|
190 | BondsPerSPCount[SP+1]++;
|
---|
191 | DoLog(3) && (Log() << Verbose(3) << "Added its bond to SP list, having now " << BondsPerSPCount[SP+1] << " item(s)." << endl);
|
---|
192 | } else {
|
---|
193 | if (OtherWalker != Predecessor)
|
---|
194 | DoLog(3) && (Log() << Verbose(3) << "Not passing on, as index of " << *OtherWalker << " " << OtherWalker->GetTrueFather()->getNr() << " is smaller than that of Root " << RootKeyNr << "." << endl);
|
---|
195 | else
|
---|
196 | DoLog(3) && (Log() << Verbose(3) << "This is my predecessor " << *Predecessor << "." << endl);
|
---|
197 | }
|
---|
198 | } else Log() << Verbose(2) << "Is not in the restricted keyset or skipping hydrogen " << *OtherWalker << "." << endl;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 | };
|
---|
203 |
|
---|
204 | /** prints the Bonds per Shortest Path list in UniqueFragments.
|
---|
205 | * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
|
---|
206 | */
|
---|
207 | void UniqueFragments::OutputSPList(int Order)
|
---|
208 | {
|
---|
209 | DoLog(0) && (Log() << Verbose(0) << "Printing all found lists." << endl);
|
---|
210 | for(int i=1;i<Order;i++) { // skip the root edge in the printing
|
---|
211 | DoLog(1) && (Log() << Verbose(1) << "Current SP level is " << i << "." << endl);
|
---|
212 | for (UniqueFragments::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
|
---|
213 | Binder != BondsPerSPList[i].end();
|
---|
214 | ++Binder) {
|
---|
215 | DoLog(2) && (Log() << Verbose(2) << *Binder << endl);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | };
|
---|
219 |
|
---|
220 | /** Simply counts all bonds in all UniqueFragments::BondsPerSPList lists.
|
---|
221 | * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
|
---|
222 | */
|
---|
223 | int UniqueFragments::CountNumbersInBondsList(int Order)
|
---|
224 | {
|
---|
225 | int SP = -1; // the Root <-> Root edge must be subtracted!
|
---|
226 | for(int i=Order;i--;) { // sum up all found edges
|
---|
227 | for (UniqueFragments::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
|
---|
228 | Binder != BondsPerSPList[i].end();
|
---|
229 | ++Binder) {
|
---|
230 | SP++;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | return SP;
|
---|
234 | };
|
---|
235 |
|
---|
236 | /** Initialization for UniqueFragments.
|
---|
237 | *
|
---|
238 | * \param _Root ref to atom to start from (with graph algorithms, hence root node)
|
---|
239 | * \param AtomCount number of nodes/atoms
|
---|
240 | */
|
---|
241 | void UniqueFragments::Init(atom *_Root, size_t AtomCount)
|
---|
242 | {
|
---|
243 | // initialise the fragments structure
|
---|
244 | FragmentCounter = 0;
|
---|
245 | FragmentSet = new KeySet;
|
---|
246 | Root = _Root;
|
---|
247 | ShortestPathList = new int[AtomCount];
|
---|
248 | for (int i=AtomCount;i--;) {
|
---|
249 | ShortestPathList[i] = -1;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /** Removes some allocated memory.
|
---|
254 | *
|
---|
255 | */
|
---|
256 | void UniqueFragments::Cleanup()
|
---|
257 | {
|
---|
258 | delete[](ShortestPathList);
|
---|
259 | delete(FragmentSet);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /** Getter for UniqueFragments:Root.
|
---|
263 | *
|
---|
264 | * @return const ref to root atom.
|
---|
265 | */
|
---|
266 | const atom *UniqueFragments::getRoot() const
|
---|
267 | {
|
---|
268 | return Root;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** Setter for UniqueFragments:Root.
|
---|
272 | *
|
---|
273 | * @param _root root atom to set
|
---|
274 | */
|
---|
275 | void UniqueFragments::setRoot(atom *_root)
|
---|
276 | {
|
---|
277 | Root=_root;
|
---|
278 | }
|
---|