[53d6b2] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[53d6b2] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * BreadthFirstSearchAdd.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Feb 16, 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 "BreadthFirstSearchAdd.hpp"
|
---|
| 23 |
|
---|
[47d041] | 24 | #include <sstream>
|
---|
| 25 |
|
---|
[6f0841] | 26 | #include "Atom/atom.hpp"
|
---|
[129204] | 27 | #include "Bond/bond.hpp"
|
---|
[53d6b2] | 28 | #include "CodePatterns/Assert.hpp"
|
---|
| 29 | #include "CodePatterns/Info.hpp"
|
---|
| 30 | #include "CodePatterns/Log.hpp"
|
---|
| 31 | #include "CodePatterns/Verbose.hpp"
|
---|
| 32 | #include "molecule.hpp"
|
---|
| 33 | #include "World.hpp"
|
---|
| 34 |
|
---|
| 35 |
|
---|
[07a47e] | 36 | BreadthFirstSearchAdd::BreadthFirstSearchAdd(atom *&_Root, int _BondOrder, bool _IsAngstroem, const enum HydrogenSaturation _saturation) :
|
---|
[53d6b2] | 37 | BondOrder(_BondOrder),
|
---|
| 38 | Root(_Root),
|
---|
[07a47e] | 39 | saturation(_saturation),
|
---|
[53d6b2] | 40 | IsAngstroem(_IsAngstroem)
|
---|
| 41 | {
|
---|
| 42 | BFSStack.push_front(Root);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | BreadthFirstSearchAdd::~BreadthFirstSearchAdd()
|
---|
| 47 | {}
|
---|
| 48 |
|
---|
| 49 | void BreadthFirstSearchAdd::Init(atom *&_Root, int _BondOrder)
|
---|
| 50 | {
|
---|
| 51 | BondOrder = _BondOrder;
|
---|
| 52 | Root = _Root;
|
---|
| 53 | BFSStack.clear();
|
---|
| 54 | BFSStack.push_front(Root);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | void BreadthFirstSearchAdd::InitNode(atomId_t atom_id)
|
---|
| 58 | {
|
---|
| 59 | PredecessorList[atom_id] = NULL;
|
---|
| 60 | ShortestPathList[atom_id] = -1;
|
---|
| 61 | if (AddedAtomList.find(atom_id) != AddedAtomList.end()) // mark already present atoms (i.e. Root and maybe others) as visited
|
---|
[129204] | 62 | ColorList[atom_id] = GraphEdge::lightgray;
|
---|
[53d6b2] | 63 | else
|
---|
[129204] | 64 | ColorList[atom_id] = GraphEdge::white;
|
---|
[53d6b2] | 65 | }
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | void BreadthFirstSearchAdd::UnvisitedNode(molecule *Mol, atom *&Walker, atom *&OtherAtom, bond *&Binder, bond *&Bond)
|
---|
| 69 | {
|
---|
[129204] | 70 | if (Binder != Bond) // let other atom GraphEdge::white if it's via Root bond. In case it's cyclic it has to be reached again (yet Root is from OtherAtom already GraphEdge::black, thus no problem)
|
---|
| 71 | ColorList[OtherAtom->getNr()] = GraphEdge::lightgray;
|
---|
[53d6b2] | 72 | PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor
|
---|
| 73 | ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1;
|
---|
[47d041] | 74 | LOG(2, "Coloring OtherAtom " << OtherAtom->getName() << " " << GraphEdge::getColorName(ColorList[OtherAtom->getNr()]) << ", its predecessor is " << Walker->getName() << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long.");
|
---|
[53d6b2] | 75 | if ((((ShortestPathList[OtherAtom->getNr()] < BondOrder) && (Binder != Bond)))) { // Check for maximum distance
|
---|
[47d041] | 76 | std::stringstream output;
|
---|
[53d6b2] | 77 | if (AddedAtomList[OtherAtom->getNr()] == NULL) { // add if it's not been so far
|
---|
| 78 | AddedAtomList[OtherAtom->getNr()] = Mol->AddCopyAtom(OtherAtom);
|
---|
[47d041] | 79 | output << "Added OtherAtom " << OtherAtom->getName();
|
---|
[efe516] | 80 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
|
---|
[47d041] | 81 | output << " and bond " << *(AddedBondList[Binder]) << ", ";
|
---|
[129204] | 82 | } else { // this code should actually never come into play (all GraphEdge::white atoms are not yet present in BondMolecule, that's why they are GraphEdge::white in the first place)
|
---|
[47d041] | 83 | output << "Not adding OtherAtom " << OtherAtom->getName();
|
---|
[efe516] | 84 | if (AddedBondList[Binder] == NULL) {
|
---|
| 85 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
|
---|
[47d041] | 86 | output << ", added Bond " << *(AddedBondList[Binder]);
|
---|
[53d6b2] | 87 | } else
|
---|
[47d041] | 88 | output << ", not added Bond ";
|
---|
[53d6b2] | 89 | }
|
---|
[47d041] | 90 | output << ", putting OtherAtom into queue.";
|
---|
| 91 | LOG(0, output.str());
|
---|
[53d6b2] | 92 | BFSStack.push_front(OtherAtom);
|
---|
| 93 | } else { // out of bond order, then replace
|
---|
| 94 | if ((AddedAtomList[OtherAtom->getNr()] == NULL) && (Binder->Cyclic))
|
---|
[129204] | 95 | ColorList[OtherAtom->getNr()] = GraphEdge::white; // unmark if it has not been queued/added, to make it available via its other bonds (cyclic)
|
---|
[47d041] | 96 | {
|
---|
| 97 | std::stringstream output;
|
---|
| 98 | if (Binder == Bond)
|
---|
| 99 | output << "Not Queueing, is the Root bond";
|
---|
| 100 | else if (ShortestPathList[OtherAtom->getNr()] >= BondOrder)
|
---|
| 101 | output << "Not Queueing, is out of Bond Count of " << BondOrder;
|
---|
| 102 | if (!Binder->Cyclic)
|
---|
| 103 | output << ", is not part of a cyclic bond, saturating bond with Hydrogen.";
|
---|
| 104 | LOG(3, output.str());
|
---|
| 105 | }
|
---|
[efe516] | 106 | if (AddedBondList[Binder] == NULL) {
|
---|
[53d6b2] | 107 | if ((AddedAtomList[OtherAtom->getNr()] != NULL)) { // .. whether we add or saturate
|
---|
[efe516] | 108 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
|
---|
[53d6b2] | 109 | } else {
|
---|
[07a47e] | 110 | if (saturation == DoSaturate)
|
---|
| 111 | if (!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->getNr()], Walker, OtherAtom, IsAngstroem))
|
---|
| 112 | exit(1);
|
---|
[53d6b2] | 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | void BreadthFirstSearchAdd::VisitedNode(molecule *Mol, atom *&Walker, atom *&OtherAtom, bond *&Binder, bond *&Bond)
|
---|
| 120 | {
|
---|
[47d041] | 121 | LOG(3, "Not Adding, has already been visited.");
|
---|
[53d6b2] | 122 | // This has to be a cyclic bond, check whether it's present ...
|
---|
[efe516] | 123 | if (AddedBondList[Binder] == NULL) {
|
---|
[53d6b2] | 124 | if ((Binder != Bond) && (Binder->Cyclic) && (((ShortestPathList[Walker->getNr()] + 1) < BondOrder))) {
|
---|
[efe516] | 125 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
|
---|
[53d6b2] | 126 | } else { // if it's root bond it has to broken (otherwise we would not create the fragments)
|
---|
[07a47e] | 127 | if (saturation == DoSaturate)
|
---|
| 128 | if(!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->getNr()], Walker, OtherAtom, IsAngstroem))
|
---|
| 129 | exit(1);
|
---|
[53d6b2] | 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | void BreadthFirstSearchAdd::operator()(molecule *Mol, atom *_Root, bond *Bond, int _BondOrder)
|
---|
| 136 | {
|
---|
| 137 | Info FunctionInfo("BreadthFirstSearchAdd");
|
---|
| 138 | atom *Walker = NULL, *OtherAtom = NULL;
|
---|
| 139 | bond *Binder = NULL;
|
---|
| 140 |
|
---|
| 141 | // add Root if not done yet
|
---|
| 142 | if (AddedAtomList[_Root->getNr()] == NULL) // add Root if not yet present
|
---|
| 143 | AddedAtomList[_Root->getNr()] = Mol->AddCopyAtom(_Root);
|
---|
| 144 |
|
---|
| 145 | Init(_Root, _BondOrder);
|
---|
| 146 |
|
---|
[129204] | 147 | // and go on ... Queue always contains all GraphEdge::lightgray vertices
|
---|
[53d6b2] | 148 | while (!BFSStack.empty()) {
|
---|
| 149 | // we have to pop the oldest atom from stack. This keeps the atoms on the stack always of the same ShortestPath distance.
|
---|
| 150 | // e.g. if current atom is 2, push to end of stack are of length 3, but first all of length 2 would be popped. They again
|
---|
| 151 | // append length of 3 (their neighbours). Thus on stack we have always atoms of a certain length n at bottom of stack and
|
---|
| 152 | // followed by n+1 till top of stack.
|
---|
| 153 | Walker = BFSStack.front(); // pop oldest added
|
---|
| 154 | BFSStack.pop_front();
|
---|
| 155 | const BondList& ListOfBonds = Walker->getListOfBonds();
|
---|
[47d041] | 156 | LOG(1, "Current Walker is: " << Walker->getName() << ", and has " << ListOfBonds.size() << " bonds.");
|
---|
[53d6b2] | 157 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 158 | Runner != ListOfBonds.end();
|
---|
| 159 | ++Runner) {
|
---|
| 160 | if ((*Runner) != NULL) { // don't look at bond equal NULL
|
---|
| 161 | Binder = (*Runner);
|
---|
| 162 | OtherAtom = (*Runner)->GetOtherAtom(Walker);
|
---|
[47d041] | 163 | LOG(2, "Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << ".");
|
---|
[129204] | 164 | if (ColorList[OtherAtom->getNr()] == GraphEdge::white) {
|
---|
[53d6b2] | 165 | UnvisitedNode(Mol, Walker, OtherAtom, Binder, Bond);
|
---|
| 166 | } else {
|
---|
| 167 | VisitedNode(Mol, Walker, OtherAtom, Binder, Bond);
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
[129204] | 171 | ColorList[Walker->getNr()] = GraphEdge::black;
|
---|
[47d041] | 172 | LOG(1, "Coloring Walker " << Walker->getName() << " GraphEdge::black.");
|
---|
[53d6b2] | 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|