| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 6 |  * 
 | 
|---|
| 7 |  *
 | 
|---|
| 8 |  *   This file is part of MoleCuilder.
 | 
|---|
| 9 |  *
 | 
|---|
| 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 13 |  *    (at your option) any later version.
 | 
|---|
| 14 |  *
 | 
|---|
| 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 18 |  *    GNU General Public License for more details.
 | 
|---|
| 19 |  *
 | 
|---|
| 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 22 |  */
 | 
|---|
| 23 | 
 | 
|---|
| 24 | /*
 | 
|---|
| 25 |  * DepthFirstSearchAnalysis.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: Feb 16, 2011
 | 
|---|
| 28 |  *      Author: heber
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "DepthFirstSearchAnalysis.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include <algorithm>
 | 
|---|
| 41 | #include <functional>
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "Atom/atom.hpp"
 | 
|---|
| 44 | #include "Bond/bond.hpp"
 | 
|---|
| 45 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 46 | #include "CodePatterns/Info.hpp"
 | 
|---|
| 47 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 48 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 49 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 50 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
| 51 | #include "Graph/ListOfLocalAtoms.hpp"
 | 
|---|
| 52 | #include "molecule.hpp"
 | 
|---|
| 53 | #include "MoleculeLeafClass.hpp"
 | 
|---|
| 54 | #include "MoleculeListClass.hpp"
 | 
|---|
| 55 | #include "World.hpp"
 | 
|---|
| 56 | 
 | 
|---|
| 57 | DepthFirstSearchAnalysis::DepthFirstSearchAnalysis() :
 | 
|---|
| 58 |   CurrentGraphNr(0),
 | 
|---|
| 59 |   ComponentNumber(0),
 | 
|---|
| 60 |   BackStepping(false)
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   ResetAllBondsToUnused();
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | DepthFirstSearchAnalysis::~DepthFirstSearchAnalysis()
 | 
|---|
| 66 | {}
 | 
|---|
| 67 | 
 | 
|---|
| 68 | void DepthFirstSearchAnalysis::Init()
 | 
|---|
| 69 | {
 | 
|---|
| 70 |   CurrentGraphNr = 0;
 | 
|---|
| 71 |   ComponentNumber = 0;
 | 
|---|
| 72 |   BackStepping = false;
 | 
|---|
| 73 |   std::for_each(World::getInstance().getAtomIter(),World::getInstance().atomEnd(),
 | 
|---|
| 74 |       std::mem_fun(&atom::resetGraphNr));
 | 
|---|
| 75 |   std::for_each(World::getInstance().getAtomIter(),World::getInstance().atomEnd(),
 | 
|---|
| 76 |       std::mem_fun(&atom::InitComponentNr));
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | 
 | 
|---|
| 80 | bond::ptr DepthFirstSearchAnalysis::FindNextUnused(atom *vertex) const
 | 
|---|
| 81 | {
 | 
|---|
| 82 |   const BondList& ListOfBonds = vertex->getListOfBonds();
 | 
|---|
| 83 |   for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
| 84 |       Runner != ListOfBonds.end();
 | 
|---|
| 85 |       ++Runner)
 | 
|---|
| 86 |     if ((*Runner)->IsUsed() == GraphEdge::white)
 | 
|---|
| 87 |       return ((*Runner));
 | 
|---|
| 88 |   return bond::ptr();
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | 
 | 
|---|
| 92 | void DepthFirstSearchAnalysis::ResetAllBondsToUnused() const
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   World::AtomComposite allatoms = World::getInstance().getAllAtoms();
 | 
|---|
| 95 |   for(World::AtomComposite::const_iterator AtomRunner = allatoms.begin();
 | 
|---|
| 96 |       AtomRunner != allatoms.end();
 | 
|---|
| 97 |       ++AtomRunner) {
 | 
|---|
| 98 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
| 99 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
| 100 |         BondRunner != ListOfBonds.end();
 | 
|---|
| 101 |         ++BondRunner)
 | 
|---|
| 102 |       if ((*BondRunner)->leftatom == *AtomRunner)
 | 
|---|
| 103 |         (*BondRunner)->ResetUsed();
 | 
|---|
| 104 |   }
 | 
|---|
| 105 | }
 | 
|---|
| 106 | 
 | 
|---|
| 107 | void DepthFirstSearchAnalysis::SetNextComponentNumber(atom *vertex, int nr) const
 | 
|---|
| 108 | {
 | 
|---|
| 109 |   size_t i = 0;
 | 
|---|
| 110 |   ASSERT(vertex != NULL,
 | 
|---|
| 111 |       "DepthFirstSearchAnalysis::SetNextComponentNumber() - Given vertex is NULL!");
 | 
|---|
| 112 |   const BondList& ListOfBonds = vertex->getListOfBonds();
 | 
|---|
| 113 |   for (; i < ListOfBonds.size(); i++) {
 | 
|---|
| 114 |     if (vertex->ComponentNr[i] == -1) { // check if not yet used
 | 
|---|
| 115 |       vertex->ComponentNr[i] = nr;
 | 
|---|
| 116 |       break;
 | 
|---|
| 117 |     } else if (vertex->ComponentNr[i] == nr) // if number is already present, don't add another time
 | 
|---|
| 118 |       break; // breaking here will not cause error!
 | 
|---|
| 119 |   }
 | 
|---|
| 120 |   ASSERT(i < ListOfBonds.size(),
 | 
|---|
| 121 |       "DepthFirstSearchAnalysis::SetNextComponentNumber() - All Component entries are already occupied!");
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | 
 | 
|---|
| 125 | bool DepthFirstSearchAnalysis::PickLocalBackEdges(const ListOfLocalAtoms_t &ListOfLocalAtoms, std::deque<bond::ptr > *&LocalStack) const
 | 
|---|
| 126 | {
 | 
|---|
| 127 |   bool status = true;
 | 
|---|
| 128 |   if (BackEdgeStack.empty()) {
 | 
|---|
| 129 |     ELOG(1, "Reference BackEdgeStack is empty!");
 | 
|---|
| 130 |     return false;
 | 
|---|
| 131 |   }
 | 
|---|
| 132 |   bond::ptr Binder = BackEdgeStack.front();
 | 
|---|
| 133 |   bond::ptr FirstBond = Binder; // mark the first bond, so that we don't loop through the stack indefinitely
 | 
|---|
| 134 |   atom *Walker = NULL, *OtherAtom = NULL;
 | 
|---|
| 135 | 
 | 
|---|
| 136 |   do { // go through all bonds and push local ones
 | 
|---|
| 137 |     const ListOfLocalAtoms_t::const_iterator leftiter = ListOfLocalAtoms.find(Binder->leftatom->getNr());
 | 
|---|
| 138 |     ASSERT( leftiter != ListOfLocalAtoms.end(),
 | 
|---|
| 139 |         "DepthFirstSearchAnalysis::PickLocalBackEdges() - could not find atom id "
 | 
|---|
| 140 |         +toString(Binder->leftatom->getNr())+" in ListOfLocalAtoms.");
 | 
|---|
| 141 |     Walker = leftiter->second; // get one atom in the reference molecule
 | 
|---|
| 142 |     if (Walker != NULL) { // if this Walker exists in the subgraph ...
 | 
|---|
| 143 |       const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
| 144 |       for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
| 145 |           Runner != ListOfBonds.end();
 | 
|---|
| 146 |           ++Runner) {
 | 
|---|
| 147 |         OtherAtom = (*Runner)->GetOtherAtom(Walker);
 | 
|---|
| 148 |         const ListOfLocalAtoms_t::const_iterator rightiter = ListOfLocalAtoms.find((*Runner)->rightatom->getNr());
 | 
|---|
| 149 |         if (OtherAtom == rightiter->second) { // found the bond
 | 
|---|
| 150 |           LocalStack->push_front((*Runner));
 | 
|---|
| 151 |           LOG(3, "INFO: Found local edge " << *(*Runner) << ".");
 | 
|---|
| 152 |           break;
 | 
|---|
| 153 |         }
 | 
|---|
| 154 |       }
 | 
|---|
| 155 |     }
 | 
|---|
| 156 |     ASSERT(!BackEdgeStack.empty(), "DepthFirstSearchAnalysis::PickLocalBackEdges() - ReferenceStack is empty!");
 | 
|---|
| 157 |     Binder = BackEdgeStack.front(); // loop the stack for next item
 | 
|---|
| 158 |     LOG(3, "Current candidate edge " << Binder << ".");
 | 
|---|
| 159 |   } while (FirstBond != Binder);
 | 
|---|
| 160 | 
 | 
|---|
| 161 |   return status;
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | 
 | 
|---|
| 165 | 
 | 
|---|
| 166 | void DepthFirstSearchAnalysis::OutputGraphInfoPerAtom() const
 | 
|---|
| 167 | {
 | 
|---|
| 168 |   LOG(1, "Final graph info for each atom is:");
 | 
|---|
| 169 |   World::AtomComposite allatoms = World::getInstance().getAllAtoms();
 | 
|---|
| 170 |   for_each(allatoms.begin(),allatoms.end(),mem_fun(&atom::OutputGraphInfo));
 | 
|---|
| 171 | }
 | 
|---|
| 172 | 
 | 
|---|
| 173 | 
 | 
|---|
| 174 | void DepthFirstSearchAnalysis::OutputGraphInfoPerBond() const
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   LOG(1, "Final graph info for each bond is:");
 | 
|---|
| 177 |   World::AtomComposite allatoms = World::getInstance().getAllAtoms();
 | 
|---|
| 178 |   for(World::AtomComposite::const_iterator AtomRunner = allatoms.begin();
 | 
|---|
| 179 |       AtomRunner != allatoms.end();
 | 
|---|
| 180 |       ++AtomRunner) {
 | 
|---|
| 181 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
| 182 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
| 183 |         BondRunner != ListOfBonds.end();
 | 
|---|
| 184 |         ++BondRunner)
 | 
|---|
| 185 |       if ((*BondRunner)->leftatom == *AtomRunner) {
 | 
|---|
| 186 |         const bond::ptr Binder = *BondRunner;
 | 
|---|
| 187 |         if (DoLog(2)) {
 | 
|---|
| 188 |           std::stringstream output;
 | 
|---|
| 189 |           output << ((Binder->Type == GraphEdge::TreeEdge) ? "TreeEdge " : "BackEdge ") << *Binder << ": <";
 | 
|---|
| 190 |           output << ((Binder->leftatom->SeparationVertex) ? "SP," : "") << "L" << Binder->leftatom->LowpointNr << " G" << Binder->leftatom->GraphNr << " Comp.";
 | 
|---|
| 191 |           Binder->leftatom->OutputComponentNumber(&output);
 | 
|---|
| 192 |           output << " ===  ";
 | 
|---|
| 193 |           output << ((Binder->rightatom->SeparationVertex) ? "SP," : "") << "L" << Binder->rightatom->LowpointNr << " G" << Binder->rightatom->GraphNr << " Comp.";
 | 
|---|
| 194 |           Binder->rightatom->OutputComponentNumber(&output);
 | 
|---|
| 195 |           output << ">.";
 | 
|---|
| 196 |           LOG(2, output.str());
 | 
|---|
| 197 |         }
 | 
|---|
| 198 |         if (Binder->Cyclic) // cyclic ??
 | 
|---|
| 199 |           LOG(3, "Lowpoint at each side are equal: CYCLIC!");
 | 
|---|
| 200 |       }
 | 
|---|
| 201 |   }
 | 
|---|
| 202 | }
 | 
|---|
| 203 | 
 | 
|---|
| 204 | 
 | 
|---|
| 205 | unsigned int DepthFirstSearchAnalysis::CyclicBondAnalysis() const
 | 
|---|
| 206 | {
 | 
|---|
| 207 |   unsigned int NoCyclicBonds = 0;
 | 
|---|
| 208 |   World::AtomComposite allatoms = World::getInstance().getAllAtoms();
 | 
|---|
| 209 |   for(World::AtomComposite::const_iterator AtomRunner = allatoms.begin();
 | 
|---|
| 210 |       AtomRunner != allatoms.end();
 | 
|---|
| 211 |       ++AtomRunner) {
 | 
|---|
| 212 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
| 213 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
| 214 |         BondRunner != ListOfBonds.end();
 | 
|---|
| 215 |         ++BondRunner)
 | 
|---|
| 216 |       if ((*BondRunner)->leftatom == *AtomRunner)
 | 
|---|
| 217 |         if ((*BondRunner)->rightatom->LowpointNr == (*BondRunner)->leftatom->LowpointNr) { // cyclic ??
 | 
|---|
| 218 |           (*BondRunner)->Cyclic = true;
 | 
|---|
| 219 |           NoCyclicBonds++;
 | 
|---|
| 220 |         }
 | 
|---|
| 221 |   }
 | 
|---|
| 222 |   return NoCyclicBonds;
 | 
|---|
| 223 | }
 | 
|---|
| 224 | 
 | 
|---|
| 225 | 
 | 
|---|
| 226 | void DepthFirstSearchAnalysis::SetWalkersGraphNr(atom *&Walker)
 | 
|---|
| 227 | {
 | 
|---|
| 228 |   if (!BackStepping) { // if we don't just return from (8)
 | 
|---|
| 229 |     Walker->GraphNr = CurrentGraphNr;
 | 
|---|
| 230 |     Walker->LowpointNr = CurrentGraphNr;
 | 
|---|
| 231 |     LOG(1, "Setting Walker[" << Walker->getName() << "]'s number to " << Walker->GraphNr << " with Lowpoint " << Walker->LowpointNr << ".");
 | 
|---|
| 232 |     AtomStack.push_front(Walker);
 | 
|---|
| 233 |     CurrentGraphNr++;
 | 
|---|
| 234 |   }
 | 
|---|
| 235 | }
 | 
|---|
| 236 | 
 | 
|---|
| 237 | 
 | 
|---|
| 238 | void DepthFirstSearchAnalysis::ProbeAlongUnusedBond(atom *&Walker, bond::ptr &Binder)
 | 
|---|
| 239 | {
 | 
|---|
| 240 |   atom *OtherAtom = NULL;
 | 
|---|
| 241 | 
 | 
|---|
| 242 |   do { // (3) if Walker has no unused egdes, go to (5)
 | 
|---|
| 243 |     BackStepping = false; // reset backstepping flag for (8)
 | 
|---|
| 244 |     if (Binder == NULL) // if we don't just return from (11), Binder is already set to next unused
 | 
|---|
| 245 |       Binder = FindNextUnused(Walker);
 | 
|---|
| 246 |     if (Binder == NULL)
 | 
|---|
| 247 |       break;
 | 
|---|
| 248 |     LOG(2, "Current Unused Bond is " << *Binder << ".");
 | 
|---|
| 249 |     // (4) Mark Binder used, ...
 | 
|---|
| 250 |     Binder->MarkUsed(GraphEdge::black);
 | 
|---|
| 251 |     OtherAtom = Binder->GetOtherAtom(Walker);
 | 
|---|
| 252 |     LOG(2, "(4) OtherAtom is " << OtherAtom->getName() << ".");
 | 
|---|
| 253 |     if (OtherAtom->GraphNr != -1) {
 | 
|---|
| 254 |       // (4a) ... if "other" atom has been visited (GraphNr != 0), set lowpoint to minimum of both, go to (3)
 | 
|---|
| 255 |       Binder->Type = GraphEdge::BackEdge;
 | 
|---|
| 256 |       BackEdgeStack.push_front(Binder);
 | 
|---|
| 257 |       Walker->LowpointNr = (Walker->LowpointNr < OtherAtom->GraphNr) ? Walker->LowpointNr : OtherAtom->GraphNr;
 | 
|---|
| 258 |       LOG(3, "(4a) Visited: Setting Lowpoint of Walker[" << Walker->getName() << "] to " << Walker->LowpointNr << ".");
 | 
|---|
| 259 |     } else {
 | 
|---|
| 260 |       // (4b) ... otherwise set OtherAtom as Ancestor of Walker and Walker as OtherAtom, go to (2)
 | 
|---|
| 261 |       Binder->Type = GraphEdge::TreeEdge;
 | 
|---|
| 262 |       OtherAtom->Ancestor = Walker;
 | 
|---|
| 263 |       Walker = OtherAtom;
 | 
|---|
| 264 |       LOG(3, "(4b) Not Visited: OtherAtom[" << OtherAtom->getName() << "]'s Ancestor is now " << OtherAtom->Ancestor->getName() << ", Walker is OtherAtom " << OtherAtom->getName() << ".");
 | 
|---|
| 265 |       break;
 | 
|---|
| 266 |     }
 | 
|---|
| 267 |     Binder.reset();
 | 
|---|
| 268 |   } while (1); // (3)
 | 
|---|
| 269 | }
 | 
|---|
| 270 | 
 | 
|---|
| 271 | 
 | 
|---|
| 272 | void DepthFirstSearchAnalysis::CheckForaNewComponent(atom *&Walker, ConnectedSubgraph &Subgraph)
 | 
|---|
| 273 | {
 | 
|---|
| 274 |   atom *OtherAtom = NULL;
 | 
|---|
| 275 | 
 | 
|---|
| 276 |   // (5) if Ancestor of Walker is ...
 | 
|---|
| 277 |   LOG(1, "(5) Number of Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "] is " << Walker->Ancestor->GraphNr << ".");
 | 
|---|
| 278 | 
 | 
|---|
| 279 |   if (Walker->Ancestor->GraphNr != Root->GraphNr) {
 | 
|---|
| 280 |     // (6)  (Ancestor of Walker is not Root)
 | 
|---|
| 281 |     if (Walker->LowpointNr < Walker->Ancestor->GraphNr) {
 | 
|---|
| 282 |       // (6a) set Ancestor's Lowpoint number to minimum of of its Ancestor and itself, go to Step(8)
 | 
|---|
| 283 |       Walker->Ancestor->LowpointNr = (Walker->Ancestor->LowpointNr < Walker->LowpointNr) ? Walker->Ancestor->LowpointNr : Walker->LowpointNr;
 | 
|---|
| 284 |       LOG(2, "(6) Setting Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "]'s Lowpoint to " << Walker->Ancestor->LowpointNr << ".");
 | 
|---|
| 285 |     } else {
 | 
|---|
| 286 |       // (7) (Ancestor of Walker is a separating vertex, remove all from stack till Walker (including), these and Ancestor form a component
 | 
|---|
| 287 |       Walker->Ancestor->SeparationVertex = true;
 | 
|---|
| 288 |       LOG(2, "(7) Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "]'s is a separating vertex, creating component.");
 | 
|---|
| 289 |       SetNextComponentNumber(Walker->Ancestor, ComponentNumber);
 | 
|---|
| 290 |       LOG(3, "(7) Walker[" << Walker->getName() << "]'s Ancestor's Compont is " << ComponentNumber << ".");
 | 
|---|
| 291 |       SetNextComponentNumber(Walker, ComponentNumber);
 | 
|---|
| 292 |       LOG(3, "(7) Walker[" << Walker->getName() << "]'s Compont is " << ComponentNumber << ".");
 | 
|---|
| 293 |       do {
 | 
|---|
| 294 |         ASSERT(!AtomStack.empty(), "DepthFirstSearchAnalysis_CheckForaNewComponent() - AtomStack is empty!");
 | 
|---|
| 295 |         OtherAtom = AtomStack.front();
 | 
|---|
| 296 |         AtomStack.pop_front();
 | 
|---|
| 297 |         Subgraph.push_back(OtherAtom);
 | 
|---|
| 298 |         SetNextComponentNumber(OtherAtom, ComponentNumber);
 | 
|---|
| 299 |         LOG(3, "(7) Other[" << OtherAtom->getName() << "]'s Compont is " << ComponentNumber << ".");
 | 
|---|
| 300 |       } while (OtherAtom != Walker);
 | 
|---|
| 301 |       ComponentNumber++;
 | 
|---|
| 302 |     }
 | 
|---|
| 303 |     // (8) Walker becomes its Ancestor, go to (3)
 | 
|---|
| 304 |     LOG(2, "(8) Walker[" << Walker->getName() << "] is now its Ancestor " << Walker->Ancestor->getName() << ", backstepping. ");
 | 
|---|
| 305 |     Walker = Walker->Ancestor;
 | 
|---|
| 306 |     BackStepping = true;
 | 
|---|
| 307 |   }
 | 
|---|
| 308 | }
 | 
|---|
| 309 | 
 | 
|---|
| 310 | 
 | 
|---|
| 311 | void DepthFirstSearchAnalysis::CleanRootStackDownTillWalker(atom *&Walker, bond::ptr &Binder, ConnectedSubgraph &Subgraph)
 | 
|---|
| 312 | {
 | 
|---|
| 313 |   atom *OtherAtom = NULL;
 | 
|---|
| 314 | 
 | 
|---|
| 315 |   if (!BackStepping) { // coming from (8) want to go to (3)
 | 
|---|
| 316 |     // (9) remove all from stack till Walker (including), these and Root form a component
 | 
|---|
| 317 |     //AtomStack.Output(out);
 | 
|---|
| 318 |     SetNextComponentNumber(Root, ComponentNumber);
 | 
|---|
| 319 |     LOG(3, "(9) Root[" << Root->getName() << "]'s Component is " << ComponentNumber << ".");
 | 
|---|
| 320 |     SetNextComponentNumber(Walker, ComponentNumber);
 | 
|---|
| 321 |     LOG(3, "(9) Walker[" << Walker->getName() << "]'s Component is " << ComponentNumber << ".");
 | 
|---|
| 322 |     do {
 | 
|---|
| 323 |       ASSERT(!AtomStack.empty(), "DepthFirstSearchAnalysis::CleanRootStackDownTillWalker() - AtomStack is empty!");
 | 
|---|
| 324 |       OtherAtom = AtomStack.front();
 | 
|---|
| 325 |       AtomStack.pop_front();
 | 
|---|
| 326 |       Subgraph.push_back(OtherAtom);
 | 
|---|
| 327 |       SetNextComponentNumber(OtherAtom, ComponentNumber);
 | 
|---|
| 328 |       LOG(3, "(7) Other[" << OtherAtom->getName() << "]'s Component is " << ComponentNumber << ".");
 | 
|---|
| 329 |     } while (OtherAtom != Walker);
 | 
|---|
| 330 |     ComponentNumber++;
 | 
|---|
| 331 | 
 | 
|---|
| 332 |     // (11) Root is separation vertex,  set Walker to Root and go to (4)
 | 
|---|
| 333 |     Walker = Root;
 | 
|---|
| 334 |     Binder = FindNextUnused(Walker);
 | 
|---|
| 335 |     if (Binder != NULL) { // Root is separation vertex
 | 
|---|
| 336 |       LOG(1, "(10) Walker is Root[" << Root->getName() << "], next Unused Bond is " << *Binder << ".");
 | 
|---|
| 337 |       LOG(1, "(11) Root is a separation vertex.");
 | 
|---|
| 338 |       Walker->SeparationVertex = true;
 | 
|---|
| 339 |     } else {
 | 
|---|
| 340 |       LOG(1, "(10) Walker is Root[" << Root->getName() << "], no next Unused Bond.");
 | 
|---|
| 341 |     }
 | 
|---|
| 342 |   }
 | 
|---|
| 343 | }
 | 
|---|
| 344 | 
 | 
|---|
| 345 | 
 | 
|---|
| 346 | const std::deque<bond::ptr >& DepthFirstSearchAnalysis::getBackEdgeStack() const
 | 
|---|
| 347 | {
 | 
|---|
| 348 |   return BackEdgeStack;
 | 
|---|
| 349 | }
 | 
|---|
| 350 | 
 | 
|---|
| 351 | 
 | 
|---|
| 352 | void DepthFirstSearchAnalysis::operator()()
 | 
|---|
| 353 | {
 | 
|---|
| 354 |   Info FunctionInfo("DepthFirstSearchAnalysis");
 | 
|---|
| 355 |   ListOfConnectedSubgraphs.clear();
 | 
|---|
| 356 |   int OldGraphNr = 0;
 | 
|---|
| 357 |   atom *Walker = NULL;
 | 
|---|
| 358 |   bond::ptr Binder;
 | 
|---|
| 359 | 
 | 
|---|
| 360 |   if (World::getInstance().numAtoms() == 0)
 | 
|---|
| 361 |     return;
 | 
|---|
| 362 | 
 | 
|---|
| 363 |   Init();
 | 
|---|
| 364 | 
 | 
|---|
| 365 |   LOG(0, "STATUS: Start walking the bond graph.");
 | 
|---|
| 366 |   for(World::AtomIterator iter = World::getInstance().getAtomIter();
 | 
|---|
| 367 |       iter != World::getInstance().atomEnd();) { // don't advance, is done at the end
 | 
|---|
| 368 |     Root = *iter;
 | 
|---|
| 369 |     // (1) mark all edges unused, empty stack, set atom->GraphNr = -1 for all
 | 
|---|
| 370 |     AtomStack.clear();
 | 
|---|
| 371 | 
 | 
|---|
| 372 |     // put into new subgraph molecule and add this to list of subgraphs
 | 
|---|
| 373 |     ConnectedSubgraph CurrentSubgraph;
 | 
|---|
| 374 |     CurrentSubgraph.push_back(Root);
 | 
|---|
| 375 | 
 | 
|---|
| 376 |     OldGraphNr = CurrentGraphNr;
 | 
|---|
| 377 |     Walker = Root;
 | 
|---|
| 378 |     do { // (10)
 | 
|---|
| 379 |       do { // (2) set number and Lowpoint of Atom to i, increase i, push current atom
 | 
|---|
| 380 |         SetWalkersGraphNr(Walker);
 | 
|---|
| 381 | 
 | 
|---|
| 382 |         ProbeAlongUnusedBond(Walker, Binder);
 | 
|---|
| 383 | 
 | 
|---|
| 384 |         if (Binder == NULL) {
 | 
|---|
| 385 |           LOG(2, "No more Unused Bonds.");
 | 
|---|
| 386 |           break;
 | 
|---|
| 387 |         } else
 | 
|---|
| 388 |           Binder.reset();
 | 
|---|
| 389 |       } while (1); // (2)
 | 
|---|
| 390 | 
 | 
|---|
| 391 |       // if we came from backstepping, yet there were no more unused bonds, we end up here with no Ancestor, because Walker is Root! Then we are finished!
 | 
|---|
| 392 |       if ((Walker == Root) && (Binder == NULL))
 | 
|---|
| 393 |         break;
 | 
|---|
| 394 | 
 | 
|---|
| 395 |       CheckForaNewComponent( Walker,  CurrentSubgraph);
 | 
|---|
| 396 | 
 | 
|---|
| 397 |       CleanRootStackDownTillWalker(Walker, Binder,  CurrentSubgraph);
 | 
|---|
| 398 | 
 | 
|---|
| 399 |     } while ((BackStepping) || (Binder != NULL)); // (10) halt only if Root has no unused edges
 | 
|---|
| 400 | 
 | 
|---|
| 401 |     ListOfConnectedSubgraphs.push_back(CurrentSubgraph);
 | 
|---|
| 402 |     // From OldGraphNr to CurrentGraphNr ranges an disconnected subgraph
 | 
|---|
| 403 |     std::stringstream output;
 | 
|---|
| 404 |     output << CurrentSubgraph;
 | 
|---|
| 405 |     LOG(1, "INFO: Disconnected subgraph ranges from " << OldGraphNr << " to "
 | 
|---|
| 406 |         << CurrentGraphNr-1 << ": " << output.str());
 | 
|---|
| 407 | 
 | 
|---|
| 408 |     // step on to next root
 | 
|---|
| 409 |     while (iter != World::getInstance().atomEnd()) {
 | 
|---|
| 410 |       if ((*iter)->GraphNr != -1) { // if already discovered, step on
 | 
|---|
| 411 |         iter++;
 | 
|---|
| 412 |       } else {
 | 
|---|
| 413 |         LOG(1,"Current next subgraph root candidate is " << (*iter)->getName()
 | 
|---|
| 414 |             << " with GraphNr " << (*iter)->GraphNr << ".");
 | 
|---|
| 415 |         break;
 | 
|---|
| 416 |       }
 | 
|---|
| 417 |     }
 | 
|---|
| 418 |   }
 | 
|---|
| 419 |   LOG(0, "STATUS: Done walking the bond graph.");
 | 
|---|
| 420 | 
 | 
|---|
| 421 |   // set cyclic bond criterium on "same LP" basis
 | 
|---|
| 422 |   CyclicBondAnalysis();
 | 
|---|
| 423 | 
 | 
|---|
| 424 |   OutputGraphInfoPerAtom();
 | 
|---|
| 425 | 
 | 
|---|
| 426 |   OutputGraphInfoPerBond();
 | 
|---|
| 427 | }
 | 
|---|
| 428 | 
 | 
|---|
| 429 | void DepthFirstSearchAnalysis::UpdateMoleculeStructure() const
 | 
|---|
| 430 | {
 | 
|---|
| 431 |   // remove all of World's molecules
 | 
|---|
| 432 |   for (World::MoleculeIterator iter = World::getInstance().getMoleculeIter();
 | 
|---|
| 433 |       World::getInstance().getMoleculeIter() != World::getInstance().moleculeEnd();
 | 
|---|
| 434 |       iter = World::getInstance().getMoleculeIter()) {
 | 
|---|
| 435 |     World::getInstance().getMolecules()->erase(*iter);
 | 
|---|
| 436 |     World::getInstance().destroyMolecule(*iter);
 | 
|---|
| 437 |   }
 | 
|---|
| 438 |   // instantiate new molecules
 | 
|---|
| 439 |   molecule *newmol = NULL;
 | 
|---|
| 440 |   for (ConnectedSubgraphList::const_iterator iter = ListOfConnectedSubgraphs.begin();
 | 
|---|
| 441 |       iter != ListOfConnectedSubgraphs.end();
 | 
|---|
| 442 |       ++iter) {
 | 
|---|
| 443 |     newmol = (*iter).getMolecule();
 | 
|---|
| 444 |     if (DoLog(2)) {
 | 
|---|
| 445 |       LOG(2, "STATUS: Creating new molecule:");
 | 
|---|
| 446 |       std::stringstream output;
 | 
|---|
| 447 |       newmol->Output(&output);
 | 
|---|
| 448 |       std::stringstream outstream(output.str());
 | 
|---|
| 449 |       std::string line;
 | 
|---|
| 450 |       while (getline(outstream, line)) {
 | 
|---|
| 451 |         LOG(2, "\t"+line);
 | 
|---|
| 452 |       }
 | 
|---|
| 453 |     }
 | 
|---|
| 454 |   }
 | 
|---|
| 455 | }
 | 
|---|
| 456 | 
 | 
|---|
| 457 | MoleculeLeafClass *DepthFirstSearchAnalysis::getMoleculeStructure() const
 | 
|---|
| 458 | {
 | 
|---|
| 459 |   MoleculeLeafClass *Subgraphs = new MoleculeLeafClass(NULL);
 | 
|---|
| 460 |   MoleculeLeafClass *MolecularWalker = Subgraphs;
 | 
|---|
| 461 |   for (World::MoleculeIterator iter = World::getInstance().getMoleculeIter();
 | 
|---|
| 462 |       iter != World::getInstance().moleculeEnd();
 | 
|---|
| 463 |       ++iter) {
 | 
|---|
| 464 |     // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| 465 |     MolecularWalker = new MoleculeLeafClass(MolecularWalker);
 | 
|---|
| 466 |     MolecularWalker->Leaf = (*iter);
 | 
|---|
| 467 |   }
 | 
|---|
| 468 |   return Subgraphs;
 | 
|---|
| 469 | }
 | 
|---|
| 470 | 
 | 
|---|