source: src/molecule_graph.cpp@ 3738f0

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 3738f0 was 3738f0, checked in by Frederik Heber <heber@…>, 15 years ago

Moved molecule::CreateAdjacencyList over to class BondGraph.

to make this possible we had to:

other changes:

TESTFIXES:

  • the regression test for all Actions mentioned above that don't create adjacency themselves anymore needed to be prepended with --select-all-atoms --create-adjacency.
  • Property mode set to 100644
File size: 59.4 KB
RevLine 
[bcf653]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
[cee0b57]8/*
9 * molecule_graph.cpp
10 *
11 * Created on: Oct 5, 2009
12 * Author: heber
13 */
14
[bf3817]15// include config.h
[aafd77]16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
[ad011c]20#include "CodePatterns/MemDebug.hpp"
[112b09]21
[a564be]22#include <stack>
23
[f66195]24#include "atom.hpp"
25#include "bond.hpp"
[b70721]26#include "bondgraph.hpp"
[34c43a]27#include "Box.hpp"
28#include "CodePatterns/Assert.hpp"
29#include "CodePatterns/Info.hpp"
30#include "CodePatterns/Log.hpp"
31#include "CodePatterns/Verbose.hpp"
[cee0b57]32#include "config.hpp"
[f66195]33#include "element.hpp"
[1d5afa5]34#include "Helpers/defs.hpp"
35#include "Helpers/fast_functions.hpp"
[952f38]36#include "Helpers/helpers.hpp"
[1d5afa5]37#include "LinearAlgebra/RealSpaceMatrix.hpp"
[b8b75d]38#include "linkedcell.hpp"
[cee0b57]39#include "molecule.hpp"
[34c43a]40#include "PointCloudAdaptor.hpp"
[b34306]41#include "World.hpp"
[9d83b6]42#include "WorldTime.hpp"
[1d5afa5]43
44#define MAXBONDS 8
45
[9eefda]46struct BFSAccounting
47{
48 atom **PredecessorList;
49 int *ShortestPathList;
[ce5f05]50 enum bond::Shading *ColorList;
[a564be]51 std::deque<atom *> *BFSStack;
52 std::deque<atom *> *TouchedStack;
[9eefda]53 int AtomCount;
54 int BondOrder;
55 atom *Root;
56 bool BackStepping;
57 int CurrentGraphNr;
58 int ComponentNr;
59};
[cee0b57]60
[9eefda]61/** Accounting data for Depth First Search.
62 */
63struct DFSAccounting
64{
[a564be]65 std::deque<atom *> *AtomStack;
66 std::deque<bond *> *BackEdgeStack;
[9eefda]67 int CurrentGraphNr;
68 int ComponentNumber;
69 atom *Root;
70 bool BackStepping;
71};
72
73/************************************* Functions for class molecule *********************************/
[cee0b57]74
75/** Creates an adjacency list of the molecule.
76 * We obtain an outside file with the indices of atoms which are bondmembers.
77 */
[e138de]78void molecule::CreateAdjacencyListFromDbondFile(ifstream *input)
[cee0b57]79{
[c68c90]80 Info FunctionInfo(__func__);
[cee0b57]81 // 1 We will parse bonds out of the dbond file created by tremolo.
[44a59b]82 int atom1, atom2;
83 atom *Walker, *OtherWalker;
[c68c90]84 char line[MAXSTRINGSIZE];
[44a59b]85
[c68c90]86 if (input->fail()) {
87 DoeLog(0) && (eLog() << Verbose(0) << "Opening of bond file failed \n");
88 performCriticalExit();
[44a59b]89 };
[bd6bfa]90 doCountAtoms();
[44a59b]91
[c68c90]92 // skip header
93 input->getline(line,MAXSTRINGSIZE);
94 DoLog(1) && (Log() << Verbose(1) << "Scanning file ... \n");
[44a59b]95 while (!input->eof()) // Check whether we read everything already
96 {
[c68c90]97 input->getline(line,MAXSTRINGSIZE);
98 stringstream zeile(line);
99 zeile >> atom1;
100 zeile >> atom2;
[44a59b]101
[c68c90]102 DoLog(2) && (Log() << Verbose(2) << "Looking for atoms " << atom1 << " and " << atom2 << "." << endl);
[9eefda]103 if (atom2 < atom1) //Sort indices of atoms in order
[a0064e]104 std::swap(atom1, atom2);
[9eefda]105 Walker = FindAtom(atom1);
[05a97c]106 ASSERT(Walker,"Could not find an atom with the ID given in dbond file");
[9eefda]107 OtherWalker = FindAtom(atom2);
[05a97c]108 ASSERT(OtherWalker,"Could not find an atom with the ID given in dbond file");
[44a59b]109 AddBond(Walker, OtherWalker); //Add the bond between the two atoms with respective indices.
110 }
[9eefda]111}
[cee0b57]112
[e08c46]113/** Checks for presence of bonds within atom list.
114 * TODO: more sophisticated check for bond structure (e.g. connected subgraph, ...)
115 * \return true - bonds present, false - no bonds
116 */
[e4afb4]117bool molecule::hasBondStructure() const
[e08c46]118{
[9d83b6]119 for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
120 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
121 if (!ListOfBonds.empty())
[e08c46]122 return true;
[9d83b6]123 }
[e08c46]124 return false;
125}
126
[b8b75d]127/** Prints a list of all bonds to \a *out.
128 */
[e138de]129void molecule::OutputBondsList() const
[b8b75d]130{
[a67d19]131 DoLog(1) && (Log() << Verbose(1) << endl << "From contents of bond chain list:");
[9d83b6]132 for(molecule::const_iterator AtomRunner = molecule::begin(); AtomRunner != molecule::end(); ++AtomRunner) {
133 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
134 for(BondList::const_iterator BondRunner = ListOfBonds.begin();
135 BondRunner != ListOfBonds.end();
136 ++BondRunner)
[e08c46]137 if ((*BondRunner)->leftatom == *AtomRunner) {
138 DoLog(0) && (Log() << Verbose(0) << *(*BondRunner) << "\t" << endl);
139 }
[9d83b6]140 }
[a67d19]141 DoLog(0) && (Log() << Verbose(0) << endl);
[9eefda]142}
143;
[cee0b57]144
145
146/** Counts all cyclic bonds and returns their number.
147 * \note Hydrogen bonds can never by cyclic, thus no check for that
[9d37ac]148 * \return number of cyclic bonds
[cee0b57]149 */
[e138de]150int molecule::CountCyclicBonds()
[cee0b57]151{
[266237]152 NoCyclicBonds = 0;
[cee0b57]153 int *MinimumRingSize = NULL;
154 MoleculeLeafClass *Subgraphs = NULL;
[a564be]155 std::deque<bond *> *BackEdgeStack = NULL;
[9d83b6]156 for(molecule::iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
157 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
[ce5f05]158 if ((!ListOfBonds.empty()) && ((*ListOfBonds.begin())->Type == bond::Undetermined)) {
[e08c46]159 DoLog(0) && (Log() << Verbose(0) << "No Depth-First-Search analysis performed so far, calling ..." << endl);
160 Subgraphs = DepthFirstSearchAnalysis(BackEdgeStack);
161 while (Subgraphs->next != NULL) {
162 Subgraphs = Subgraphs->next;
163 delete (Subgraphs->previous);
164 }
165 delete (Subgraphs);
166 delete[] (MinimumRingSize);
167 break;
[cee0b57]168 }
[9d83b6]169 }
170 for(molecule::iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
171 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
172 for(BondList::const_iterator BondRunner = ListOfBonds.begin();
173 BondRunner != ListOfBonds.end();
174 ++BondRunner)
[e08c46]175 if ((*BondRunner)->leftatom == *AtomRunner)
176 if ((*BondRunner)->Cyclic)
177 NoCyclicBonds++;
[9d83b6]178 }
[9eefda]179 delete (BackEdgeStack);
[266237]180 return NoCyclicBonds;
[9eefda]181}
182;
[b8b75d]183
[cee0b57]184
[9eefda]185/** Sets atom::GraphNr and atom::LowpointNr to BFSAccounting::CurrentGraphNr.
186 * \param *Walker current node
187 * \param &BFS structure with accounting data for BFS
188 */
[e138de]189void DepthFirstSearchAnalysis_SetWalkersGraphNr(atom *&Walker, struct DFSAccounting &DFS)
[174e0e]190{
[9eefda]191 if (!DFS.BackStepping) { // if we don't just return from (8)
192 Walker->GraphNr = DFS.CurrentGraphNr;
193 Walker->LowpointNr = DFS.CurrentGraphNr;
[68f03d]194 DoLog(1) && (Log() << Verbose(1) << "Setting Walker[" << Walker->getName() << "]'s number to " << Walker->GraphNr << " with Lowpoint " << Walker->LowpointNr << "." << endl);
[a564be]195 DFS.AtomStack->push_front(Walker);
[9eefda]196 DFS.CurrentGraphNr++;
[174e0e]197 }
[9eefda]198}
199;
[174e0e]200
[9eefda]201/** During DFS goes along unvisited bond and touches other atom.
202 * Sets bond::type, if
203 * -# BackEdge: set atom::LowpointNr and push on \a BackEdgeStack
204 * -# TreeEgde: set atom::Ancestor and continue with Walker along this edge
205 * Continue until molecule::FindNextUnused() finds no more unused bonds.
206 * \param *mol molecule with atoms and finding unused bonds
207 * \param *&Binder current edge
208 * \param &DFS DFS accounting data
209 */
[e138de]210void DepthFirstSearchAnalysis_ProbeAlongUnusedBond(const molecule * const mol, atom *&Walker, bond *&Binder, struct DFSAccounting &DFS)
[174e0e]211{
212 atom *OtherAtom = NULL;
213
214 do { // (3) if Walker has no unused egdes, go to (5)
[9eefda]215 DFS.BackStepping = false; // reset backstepping flag for (8)
[174e0e]216 if (Binder == NULL) // if we don't just return from (11), Binder is already set to next unused
217 Binder = mol->FindNextUnused(Walker);
218 if (Binder == NULL)
219 break;
[a67d19]220 DoLog(2) && (Log() << Verbose(2) << "Current Unused Bond is " << *Binder << "." << endl);
[174e0e]221 // (4) Mark Binder used, ...
[ce5f05]222 Binder->MarkUsed(bond::black);
[174e0e]223 OtherAtom = Binder->GetOtherAtom(Walker);
[68f03d]224 DoLog(2) && (Log() << Verbose(2) << "(4) OtherAtom is " << OtherAtom->getName() << "." << endl);
[174e0e]225 if (OtherAtom->GraphNr != -1) {
226 // (4a) ... if "other" atom has been visited (GraphNr != 0), set lowpoint to minimum of both, go to (3)
[ce5f05]227 Binder->Type = bond::BackEdge;
[a564be]228 DFS.BackEdgeStack->push_front(Binder);
[9eefda]229 Walker->LowpointNr = (Walker->LowpointNr < OtherAtom->GraphNr) ? Walker->LowpointNr : OtherAtom->GraphNr;
[68f03d]230 DoLog(3) && (Log() << Verbose(3) << "(4a) Visited: Setting Lowpoint of Walker[" << Walker->getName() << "] to " << Walker->LowpointNr << "." << endl);
[174e0e]231 } else {
232 // (4b) ... otherwise set OtherAtom as Ancestor of Walker and Walker as OtherAtom, go to (2)
[ce5f05]233 Binder->Type = bond::TreeEdge;
[174e0e]234 OtherAtom->Ancestor = Walker;
235 Walker = OtherAtom;
[68f03d]236 DoLog(3) && (Log() << Verbose(3) << "(4b) Not Visited: OtherAtom[" << OtherAtom->getName() << "]'s Ancestor is now " << OtherAtom->Ancestor->getName() << ", Walker is OtherAtom " << OtherAtom->getName() << "." << endl);
[174e0e]237 break;
238 }
239 Binder = NULL;
[9eefda]240 } while (1); // (3)
241}
242;
[174e0e]243
[9eefda]244/** Checks whether we have a new component.
245 * if atom::LowpointNr of \a *&Walker is greater than atom::GraphNr of its atom::Ancestor, we have a new component.
246 * Meaning that if we touch upon a node who suddenly has a smaller atom::LowpointNr than its ancestor, then we
247 * have a found a new branch in the graph tree.
248 * \param *mol molecule with atoms and finding unused bonds
249 * \param *&Walker current node
250 * \param &DFS DFS accounting data
251 */
[e138de]252void DepthFirstSearchAnalysis_CheckForaNewComponent(const molecule * const mol, atom *&Walker, struct DFSAccounting &DFS, MoleculeLeafClass *&LeafWalker)
[174e0e]253{
254 atom *OtherAtom = NULL;
255
256 // (5) if Ancestor of Walker is ...
[68f03d]257 DoLog(1) && (Log() << Verbose(1) << "(5) Number of Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "] is " << Walker->Ancestor->GraphNr << "." << endl);
[174e0e]258
[9eefda]259 if (Walker->Ancestor->GraphNr != DFS.Root->GraphNr) {
[174e0e]260 // (6) (Ancestor of Walker is not Root)
261 if (Walker->LowpointNr < Walker->Ancestor->GraphNr) {
262 // (6a) set Ancestor's Lowpoint number to minimum of of its Ancestor and itself, go to Step(8)
263 Walker->Ancestor->LowpointNr = (Walker->Ancestor->LowpointNr < Walker->LowpointNr) ? Walker->Ancestor->LowpointNr : Walker->LowpointNr;
[68f03d]264 DoLog(2) && (Log() << Verbose(2) << "(6) Setting Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "]'s Lowpoint to " << Walker->Ancestor->LowpointNr << "." << endl);
[174e0e]265 } else {
266 // (7) (Ancestor of Walker is a separating vertex, remove all from stack till Walker (including), these and Ancestor form a component
267 Walker->Ancestor->SeparationVertex = true;
[68f03d]268 DoLog(2) && (Log() << Verbose(2) << "(7) Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "]'s is a separating vertex, creating component." << endl);
[9eefda]269 mol->SetNextComponentNumber(Walker->Ancestor, DFS.ComponentNumber);
[68f03d]270 DoLog(3) && (Log() << Verbose(3) << "(7) Walker[" << Walker->getName() << "]'s Ancestor's Compont is " << DFS.ComponentNumber << "." << endl);
[9eefda]271 mol->SetNextComponentNumber(Walker, DFS.ComponentNumber);
[68f03d]272 DoLog(3) && (Log() << Verbose(3) << "(7) Walker[" << Walker->getName() << "]'s Compont is " << DFS.ComponentNumber << "." << endl);
[174e0e]273 do {
[a564be]274 ASSERT(!DFS.AtomStack->empty(), "DepthFirstSearchAnalysis_CheckForaNewComponent() - DFS.AtomStack is empty!");
275 OtherAtom = DFS.AtomStack->front();
276 DFS.AtomStack->pop_front();
[174e0e]277 LeafWalker->Leaf->AddCopyAtom(OtherAtom);
[9eefda]278 mol->SetNextComponentNumber(OtherAtom, DFS.ComponentNumber);
[68f03d]279 DoLog(3) && (Log() << Verbose(3) << "(7) Other[" << OtherAtom->getName() << "]'s Compont is " << DFS.ComponentNumber << "." << endl);
[174e0e]280 } while (OtherAtom != Walker);
[9eefda]281 DFS.ComponentNumber++;
[174e0e]282 }
283 // (8) Walker becomes its Ancestor, go to (3)
[68f03d]284 DoLog(2) && (Log() << Verbose(2) << "(8) Walker[" << Walker->getName() << "] is now its Ancestor " << Walker->Ancestor->getName() << ", backstepping. " << endl);
[174e0e]285 Walker = Walker->Ancestor;
[9eefda]286 DFS.BackStepping = true;
[174e0e]287 }
[9eefda]288}
289;
[174e0e]290
[9eefda]291/** Cleans the root stack when we have found a component.
292 * If we are not DFSAccounting::BackStepping, then we clear the root stack by putting everything into a
293 * component down till we meet DFSAccounting::Root.
294 * \param *mol molecule with atoms and finding unused bonds
295 * \param *&Walker current node
296 * \param *&Binder current edge
297 * \param &DFS DFS accounting data
298 */
[e138de]299void DepthFirstSearchAnalysis_CleanRootStackDownTillWalker(const molecule * const mol, atom *&Walker, bond *&Binder, struct DFSAccounting &DFS, MoleculeLeafClass *&LeafWalker)
[174e0e]300{
301 atom *OtherAtom = NULL;
302
[9eefda]303 if (!DFS.BackStepping) { // coming from (8) want to go to (3)
[174e0e]304 // (9) remove all from stack till Walker (including), these and Root form a component
[99593f]305 //DFS.AtomStack->Output(out);
[9eefda]306 mol->SetNextComponentNumber(DFS.Root, DFS.ComponentNumber);
[68f03d]307 DoLog(3) && (Log() << Verbose(3) << "(9) Root[" << DFS.Root->getName() << "]'s Component is " << DFS.ComponentNumber << "." << endl);
[9eefda]308 mol->SetNextComponentNumber(Walker, DFS.ComponentNumber);
[68f03d]309 DoLog(3) && (Log() << Verbose(3) << "(9) Walker[" << Walker->getName() << "]'s Component is " << DFS.ComponentNumber << "." << endl);
[174e0e]310 do {
[a564be]311 ASSERT(!DFS.AtomStack->empty(), "DepthFirstSearchAnalysis_CleanRootStackDownTillWalker() - DFS.AtomStack is empty!");
312 OtherAtom = DFS.AtomStack->front();
313 DFS.AtomStack->pop_front();
[174e0e]314 LeafWalker->Leaf->AddCopyAtom(OtherAtom);
[9eefda]315 mol->SetNextComponentNumber(OtherAtom, DFS.ComponentNumber);
[a564be]316 DoLog(3) && (Log() << Verbose(3) << "(7) Other[" << OtherAtom->getName() << "]'s Component is " << DFS.ComponentNumber << "." << endl);
[174e0e]317 } while (OtherAtom != Walker);
[9eefda]318 DFS.ComponentNumber++;
[174e0e]319
320 // (11) Root is separation vertex, set Walker to Root and go to (4)
[9eefda]321 Walker = DFS.Root;
[174e0e]322 Binder = mol->FindNextUnused(Walker);
[68f03d]323 DoLog(1) && (Log() << Verbose(1) << "(10) Walker is Root[" << DFS.Root->getName() << "], next Unused Bond is " << Binder << "." << endl);
[174e0e]324 if (Binder != NULL) { // Root is separation vertex
[a67d19]325 DoLog(1) && (Log() << Verbose(1) << "(11) Root is a separation vertex." << endl);
[174e0e]326 Walker->SeparationVertex = true;
327 }
328 }
[9eefda]329}
330;
331
332/** Initializes DFSAccounting structure.
333 * \param &DFS accounting structure to allocate
[7218f8]334 * \param *mol molecule with AtomCount, BondCount and all atoms
[9eefda]335 */
[e138de]336void DepthFirstSearchAnalysis_Init(struct DFSAccounting &DFS, const molecule * const mol)
[9eefda]337{
[a564be]338 DFS.AtomStack = new std::deque<atom *> (mol->getAtomCount());
[9eefda]339 DFS.CurrentGraphNr = 0;
340 DFS.ComponentNumber = 0;
341 DFS.BackStepping = false;
[7218f8]342 mol->ResetAllBondsToUnused();
[a564be]343 DFS.BackEdgeStack->clear();
[9eefda]344}
345;
[174e0e]346
[9eefda]347/** Free's DFSAccounting structure.
348 * \param &DFS accounting structure to free
349 */
[e138de]350void DepthFirstSearchAnalysis_Finalize(struct DFSAccounting &DFS)
[9eefda]351{
352 delete (DFS.AtomStack);
[7218f8]353 // delete (DFS.BackEdgeStack); // DON'T free, see DepthFirstSearchAnalysis(), is returned as allocated
[9eefda]354}
355;
[174e0e]356
[00ef5c]357void molecule::init_DFS(struct DFSAccounting &DFS) const{
358 DepthFirstSearchAnalysis_Init(DFS, this);
359 for_each(atoms.begin(),atoms.end(),mem_fun(&atom::resetGraphNr));
360 for_each(atoms.begin(),atoms.end(),mem_fun(&atom::InitComponentNr));
361}
362
[cee0b57]363/** Performs a Depth-First search on this molecule.
364 * Marks bonds in molecule as cyclic, bridge, ... and atoms as
365 * articulations points, ...
366 * We use the algorithm from [Even, Graph Algorithms, p.62].
[a564be]367 * \param *&BackEdgeStack NULL pointer to std::deque<bond *> with all the found back edges, allocated and filled on return
[cee0b57]368 * \return list of each disconnected subgraph as an individual molecule class structure
369 */
[a564be]370MoleculeLeafClass * molecule::DepthFirstSearchAnalysis(std::deque<bond *> *&BackEdgeStack) const
[cee0b57]371{
[9eefda]372 struct DFSAccounting DFS;
[458c31]373 BackEdgeStack = new std::deque<bond *> (getBondCount());
[9eefda]374 DFS.BackEdgeStack = BackEdgeStack;
[cee0b57]375 MoleculeLeafClass *SubGraphs = new MoleculeLeafClass(NULL);
376 MoleculeLeafClass *LeafWalker = SubGraphs;
[9eefda]377 int OldGraphNr = 0;
[174e0e]378 atom *Walker = NULL;
[cee0b57]379 bond *Binder = NULL;
380
[a7b761b]381 if (getAtomCount() == 0)
[046783]382 return SubGraphs;
[a67d19]383 DoLog(0) && (Log() << Verbose(0) << "Begin of DepthFirstSearchAnalysis" << endl);
[00ef5c]384 init_DFS(DFS);
[cee0b57]385
[9879f6]386 for (molecule::const_iterator iter = begin(); iter != end();) {
387 DFS.Root = *iter;
[7218f8]388 // (1) mark all edges unused, empty stack, set atom->GraphNr = -1 for all
[a564be]389 DFS.AtomStack->clear();
[cee0b57]390
391 // put into new subgraph molecule and add this to list of subgraphs
392 LeafWalker = new MoleculeLeafClass(LeafWalker);
[5f612ee]393 LeafWalker->Leaf = World::getInstance().createMolecule();
[9eefda]394 LeafWalker->Leaf->AddCopyAtom(DFS.Root);
[cee0b57]395
[9eefda]396 OldGraphNr = DFS.CurrentGraphNr;
397 Walker = DFS.Root;
[cee0b57]398 do { // (10)
399 do { // (2) set number and Lowpoint of Atom to i, increase i, push current atom
[e138de]400 DepthFirstSearchAnalysis_SetWalkersGraphNr(Walker, DFS);
[174e0e]401
[e138de]402 DepthFirstSearchAnalysis_ProbeAlongUnusedBond(this, Walker, Binder, DFS);
[174e0e]403
[cee0b57]404 if (Binder == NULL) {
[a67d19]405 DoLog(2) && (Log() << Verbose(2) << "No more Unused Bonds." << endl);
[cee0b57]406 break;
407 } else
408 Binder = NULL;
[9eefda]409 } while (1); // (2)
[cee0b57]410
411 // 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!
[9eefda]412 if ((Walker == DFS.Root) && (Binder == NULL))
[cee0b57]413 break;
414
[e138de]415 DepthFirstSearchAnalysis_CheckForaNewComponent(this, Walker, DFS, LeafWalker);
[174e0e]416
[e138de]417 DepthFirstSearchAnalysis_CleanRootStackDownTillWalker(this, Walker, Binder, DFS, LeafWalker);
[174e0e]418
[9eefda]419 } while ((DFS.BackStepping) || (Binder != NULL)); // (10) halt only if Root has no unused edges
[cee0b57]420
421 // From OldGraphNr to CurrentGraphNr ranges an disconnected subgraph
[a67d19]422 DoLog(0) && (Log() << Verbose(0) << "Disconnected subgraph ranges from " << OldGraphNr << " to " << DFS.CurrentGraphNr << "." << endl);
[986ed3]423 LeafWalker->Leaf->Output((ofstream *)&(Log() << Verbose(0)));
[a67d19]424 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]425
426 // step on to next root
[9879f6]427 while ((iter != end()) && ((*iter)->GraphNr != -1)) {
428 //Log() << Verbose(1) << "Current next subgraph root candidate is " << (*iter)->Name << "." << endl;
429 if ((*iter)->GraphNr != -1) // if already discovered, step on
430 iter++;
[cee0b57]431 }
432 }
433 // set cyclic bond criterium on "same LP" basis
[266237]434 CyclicBondAnalysis();
435
[e138de]436 OutputGraphInfoPerAtom();
[266237]437
[e138de]438 OutputGraphInfoPerBond();
[266237]439
440 // free all and exit
[e138de]441 DepthFirstSearchAnalysis_Finalize(DFS);
[a67d19]442 DoLog(0) && (Log() << Verbose(0) << "End of DepthFirstSearchAnalysis" << endl);
[266237]443 return SubGraphs;
[9eefda]444}
445;
[266237]446
447/** Scans through all bonds and set bond::Cyclic to true where atom::LowpointNr of both ends is equal: LP criterion.
448 */
[fa649a]449void molecule::CyclicBondAnalysis() const
[266237]450{
451 NoCyclicBonds = 0;
[9d83b6]452 for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
453 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
454 for(BondList::const_iterator BondRunner = ListOfBonds.begin();
455 BondRunner != ListOfBonds.end();
456 ++BondRunner)
[e08c46]457 if ((*BondRunner)->leftatom == *AtomRunner)
458 if ((*BondRunner)->rightatom->LowpointNr == (*BondRunner)->leftatom->LowpointNr) { // cyclic ??
459 (*BondRunner)->Cyclic = true;
460 NoCyclicBonds++;
461 }
[9d83b6]462 }
[9eefda]463}
464;
[cee0b57]465
[266237]466/** Output graph information per atom.
467 */
[e138de]468void molecule::OutputGraphInfoPerAtom() const
[266237]469{
[a67d19]470 DoLog(1) && (Log() << Verbose(1) << "Final graph info for each atom is:" << endl);
[c743f8]471 for_each(atoms.begin(),atoms.end(),mem_fun(&atom::OutputGraphInfo));
[9eefda]472}
473;
[cee0b57]474
[266237]475/** Output graph information per bond.
476 */
[e138de]477void molecule::OutputGraphInfoPerBond() const
[266237]478{
[a67d19]479 DoLog(1) && (Log() << Verbose(1) << "Final graph info for each bond is:" << endl);
[9d83b6]480 for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
481 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
482 for(BondList::const_iterator BondRunner = ListOfBonds.begin();
483 BondRunner != ListOfBonds.end();
484 ++BondRunner)
[e08c46]485 if ((*BondRunner)->leftatom == *AtomRunner) {
[9d83b6]486 const bond *Binder = *BondRunner;
[f9183b]487 if (DoLog(2)) {
488 ostream &out = (Log() << Verbose(2));
[ce5f05]489 out << ((Binder->Type == bond::TreeEdge) ? "TreeEdge " : "BackEdge ") << *Binder << ": <";
[f9183b]490 out << ((Binder->leftatom->SeparationVertex) ? "SP," : "") << "L" << Binder->leftatom->LowpointNr << " G" << Binder->leftatom->GraphNr << " Comp.";
491 Binder->leftatom->OutputComponentNumber(&out);
492 out << " === ";
493 out << ((Binder->rightatom->SeparationVertex) ? "SP," : "") << "L" << Binder->rightatom->LowpointNr << " G" << Binder->rightatom->GraphNr << " Comp.";
494 Binder->rightatom->OutputComponentNumber(&out);
495 out << ">." << endl;
496 }
[e08c46]497 if (Binder->Cyclic) // cyclic ??
498 DoLog(3) && (Log() << Verbose(3) << "Lowpoint at each side are equal: CYCLIC!" << endl);
499 }
[9d83b6]500 }
[9eefda]501}
502;
503
504/** Initialise each vertex as white with no predecessor, empty queue, color Root lightgray.
505 * \param &BFS accounting structure
506 * \param AtomCount number of entries in the array to allocate
507 */
[e138de]508void InitializeBFSAccounting(struct BFSAccounting &BFS, int AtomCount)
[9eefda]509{
510 BFS.AtomCount = AtomCount;
[920c70]511 BFS.PredecessorList = new atom*[AtomCount];
512 BFS.ShortestPathList = new int[AtomCount];
[ce5f05]513 BFS.ColorList = new enum bond::Shading[AtomCount];
[a564be]514 BFS.BFSStack = new std::deque<atom *> (AtomCount);
515 BFS.TouchedStack = new std::deque<atom *> (AtomCount);
[9eefda]516
[920c70]517 for (int i = AtomCount; i--;) {
[9eefda]518 BFS.ShortestPathList[i] = -1;
[920c70]519 BFS.PredecessorList[i] = 0;
[ce5f05]520 BFS.ColorList[i] = bond::white;
[920c70]521 }
[cee0b57]522};
523
[9eefda]524/** Free's accounting structure.
525 * \param &BFS accounting structure
526 */
[e138de]527void FinalizeBFSAccounting(struct BFSAccounting &BFS)
[9eefda]528{
[920c70]529 delete[](BFS.PredecessorList);
530 delete[](BFS.ShortestPathList);
531 delete[](BFS.ColorList);
[9eefda]532 delete (BFS.BFSStack);
[c27778]533 delete (BFS.TouchedStack);
[9eefda]534 BFS.AtomCount = 0;
535};
536
537/** Clean the accounting structure.
538 * \param &BFS accounting structure
[ef9aae]539 */
[e138de]540void CleanBFSAccounting(struct BFSAccounting &BFS)
[ef9aae]541{
[9eefda]542 atom *Walker = NULL;
[a564be]543 while (!BFS.TouchedStack->empty()) {
544 Walker = BFS.TouchedStack->front();
545 BFS.TouchedStack->pop_front();
[735b1c]546 BFS.PredecessorList[Walker->getNr()] = NULL;
547 BFS.ShortestPathList[Walker->getNr()] = -1;
[ce5f05]548 BFS.ColorList[Walker->getNr()] = bond::white;
[ef9aae]549 }
550};
551
[9eefda]552/** Resets shortest path list and BFSStack.
553 * \param *&Walker current node, pushed onto BFSAccounting::BFSStack and BFSAccounting::TouchedStack
554 * \param &BFS accounting structure
555 */
[e138de]556void ResetBFSAccounting(atom *&Walker, struct BFSAccounting &BFS)
[ef9aae]557{
[735b1c]558 BFS.ShortestPathList[Walker->getNr()] = 0;
[a564be]559 BFS.BFSStack->clear(); // start with empty BFS stack
560 BFS.BFSStack->push_front(Walker);
561 BFS.TouchedStack->push_front(Walker);
[ef9aae]562};
563
[9eefda]564/** Performs a BFS from \a *Root, trying to find the same node and hence a cycle.
565 * \param *&BackEdge the edge from root that we don't want to move along
566 * \param &BFS accounting structure
567 */
[e138de]568void CyclicStructureAnalysis_CyclicBFSFromRootToRoot(bond *&BackEdge, struct BFSAccounting &BFS)
[ef9aae]569{
570 atom *Walker = NULL;
571 atom *OtherAtom = NULL;
[9eefda]572 do { // look for Root
[a564be]573 ASSERT(!BFS.BFSStack->empty(), "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - BFS.BFSStack is empty!");
574 Walker = BFS.BFSStack->front();
575 BFS.BFSStack->pop_front();
[a67d19]576 DoLog(2) && (Log() << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *BFS.Root << "." << endl);
[9d83b6]577 const BondList& ListOfBonds = Walker->getListOfBonds();
578 for (BondList::const_iterator Runner = ListOfBonds.begin();
579 Runner != ListOfBonds.end();
580 ++Runner) {
[ef9aae]581 if ((*Runner) != BackEdge) { // only walk along DFS spanning tree (otherwise we always find SP of one being backedge Binder)
582 OtherAtom = (*Runner)->GetOtherAtom(Walker);
[9eefda]583#ifdef ADDHYDROGEN
[83f176]584 if (OtherAtom->getType()->getAtomicNumber() != 1) {
[9eefda]585#endif
[68f03d]586 DoLog(2) && (Log() << Verbose(2) << "Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << "." << endl);
[ce5f05]587 if (BFS.ColorList[OtherAtom->getNr()] == bond::white) {
[a564be]588 BFS.TouchedStack->push_front(OtherAtom);
[ce5f05]589 BFS.ColorList[OtherAtom->getNr()] = bond::lightgray;
[735b1c]590 BFS.PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor
591 BFS.ShortestPathList[OtherAtom->getNr()] = BFS.ShortestPathList[Walker->getNr()] + 1;
592 DoLog(2) && (Log() << Verbose(2) << "Coloring OtherAtom " << OtherAtom->getName() << " lightgray, its predecessor is " << Walker->getName() << " and its Shortest Path is " << BFS.ShortestPathList[OtherAtom->getNr()] << " egde(s) long." << endl);
593 //if (BFS.ShortestPathList[OtherAtom->getNr()] < MinimumRingSize[Walker->GetTrueFather()->nr]) { // Check for maximum distance
[a67d19]594 DoLog(3) && (Log() << Verbose(3) << "Putting OtherAtom into queue." << endl);
[a564be]595 BFS.BFSStack->push_front(OtherAtom);
[9eefda]596 //}
[ef9aae]597 } else {
[a67d19]598 DoLog(3) && (Log() << Verbose(3) << "Not Adding, has already been visited." << endl);
[ef9aae]599 }
[9eefda]600 if (OtherAtom == BFS.Root)
601 break;
602#ifdef ADDHYDROGEN
603 } else {
[a67d19]604 DoLog(2) && (Log() << Verbose(2) << "Skipping hydrogen atom " << *OtherAtom << "." << endl);
[ce5f05]605 BFS.ColorList[OtherAtom->getNr()] = bond::black;
[9eefda]606 }
607#endif
[ef9aae]608 } else {
[a67d19]609 DoLog(2) && (Log() << Verbose(2) << "Bond " << *(*Runner) << " not Visiting, is the back edge." << endl);
[ef9aae]610 }
611 }
[ce5f05]612 BFS.ColorList[Walker->getNr()] = bond::black;
[68f03d]613 DoLog(1) && (Log() << Verbose(1) << "Coloring Walker " << Walker->getName() << " black." << endl);
[9eefda]614 if (OtherAtom == BFS.Root) { // if we have found the root, check whether this cycle wasn't already found beforehand
[ef9aae]615 // step through predecessor list
616 while (OtherAtom != BackEdge->rightatom) {
[9eefda]617 if (!OtherAtom->GetTrueFather()->IsCyclic) // if one bond in the loop is not marked as cyclic, we haven't found this cycle yet
[ef9aae]618 break;
619 else
[735b1c]620 OtherAtom = BFS.PredecessorList[OtherAtom->getNr()];
[ef9aae]621 }
622 if (OtherAtom == BackEdge->rightatom) { // if each atom in found cycle is cyclic, loop's been found before already
[a67d19]623 DoLog(3) && (Log() << Verbose(3) << "This cycle was already found before, skipping and removing seeker from search." << endl);
[ef9aae]624 do {
[a564be]625 ASSERT(!BFS.TouchedStack->empty(), "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - BFS.TouchedStack is empty!");
626 OtherAtom = BFS.TouchedStack->front();
627 BFS.TouchedStack->pop_front();
[735b1c]628 if (BFS.PredecessorList[OtherAtom->getNr()] == Walker) {
[a67d19]629 DoLog(4) && (Log() << Verbose(4) << "Removing " << *OtherAtom << " from lists and stacks." << endl);
[735b1c]630 BFS.PredecessorList[OtherAtom->getNr()] = NULL;
631 BFS.ShortestPathList[OtherAtom->getNr()] = -1;
[ce5f05]632 BFS.ColorList[OtherAtom->getNr()] = bond::white;
[a564be]633 // rats ... deque has no find()
634 std::deque<atom *>::iterator iter = find(
635 BFS.BFSStack->begin(),
636 BFS.BFSStack->end(),
637 OtherAtom);
638 ASSERT(iter != BFS.BFSStack->end(),
639 "CyclicStructureAnalysis_CyclicBFSFromRootToRoot() - can't find "+toString(*OtherAtom)+" on stack!");
640 BFS.BFSStack->erase(iter);
[ef9aae]641 }
[735b1c]642 } while ((!BFS.TouchedStack->empty()) && (BFS.PredecessorList[OtherAtom->getNr()] == NULL));
[a564be]643 BFS.TouchedStack->push_front(OtherAtom); // last was wrongly popped
[ef9aae]644 OtherAtom = BackEdge->rightatom; // set to not Root
645 } else
[9eefda]646 OtherAtom = BFS.Root;
[ef9aae]647 }
[735b1c]648 } while ((!BFS.BFSStack->empty()) && (OtherAtom != BFS.Root) && (OtherAtom != NULL)); // || (ShortestPathList[OtherAtom->getNr()] < MinimumRingSize[Walker->GetTrueFather()->getNr()])));
[ef9aae]649};
650
[9eefda]651/** Climb back the BFSAccounting::PredecessorList and find cycle members.
652 * \param *&OtherAtom
653 * \param *&BackEdge denotes the edge we did not want to travel along when doing CyclicBFSFromRootToRoot()
654 * \param &BFS accounting structure
655 * \param *&MinimumRingSize minimum distance from this node possible without encountering oneself, set on return for each atom
656 * \param &MinRingSize global minimum distance from one node without encountering oneself, set on return
657 */
[e138de]658void CyclicStructureAnalysis_RetrieveCycleMembers(atom *&OtherAtom, bond *&BackEdge, struct BFSAccounting &BFS, int *&MinimumRingSize, int &MinRingSize)
[ef9aae]659{
660 atom *Walker = NULL;
661 int NumCycles = 0;
662 int RingSize = -1;
663
[9eefda]664 if (OtherAtom == BFS.Root) {
[ef9aae]665 // now climb back the predecessor list and thus find the cycle members
666 NumCycles++;
667 RingSize = 1;
[9eefda]668 BFS.Root->GetTrueFather()->IsCyclic = true;
[a67d19]669 DoLog(1) && (Log() << Verbose(1) << "Found ring contains: ");
[9eefda]670 Walker = BFS.Root;
[ef9aae]671 while (Walker != BackEdge->rightatom) {
[68f03d]672 DoLog(0) && (Log() << Verbose(0) << Walker->getName() << " <-> ");
[735b1c]673 Walker = BFS.PredecessorList[Walker->getNr()];
[ef9aae]674 Walker->GetTrueFather()->IsCyclic = true;
675 RingSize++;
676 }
[68f03d]677 DoLog(0) && (Log() << Verbose(0) << Walker->getName() << " with a length of " << RingSize << "." << endl << endl);
[ef9aae]678 // walk through all and set MinimumRingSize
[9eefda]679 Walker = BFS.Root;
[735b1c]680 MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize;
[ef9aae]681 while (Walker != BackEdge->rightatom) {
[735b1c]682 Walker = BFS.PredecessorList[Walker->getNr()];
683 if (RingSize < MinimumRingSize[Walker->GetTrueFather()->getNr()])
684 MinimumRingSize[Walker->GetTrueFather()->getNr()] = RingSize;
[ef9aae]685 }
686 if ((RingSize < MinRingSize) || (MinRingSize == -1))
687 MinRingSize = RingSize;
688 } else {
[735b1c]689 DoLog(1) && (Log() << Verbose(1) << "No ring containing " << *BFS.Root << " with length equal to or smaller than " << MinimumRingSize[BFS.Root->GetTrueFather()->getNr()] << " found." << endl);
[ef9aae]690 }
691};
692
[9eefda]693/** From a given node performs a BFS to touch the next cycle, for whose nodes \a *&MinimumRingSize is set and set it accordingly.
694 * \param *&Root node to look for closest cycle from, i.e. \a *&MinimumRingSize is set for this node
695 * \param *&MinimumRingSize minimum distance from this node possible without encountering oneself, set on return for each atom
696 * \param AtomCount number of nodes in graph
697 */
[e138de]698void CyclicStructureAnalysis_BFSToNextCycle(atom *&Root, atom *&Walker, int *&MinimumRingSize, int AtomCount)
[ef9aae]699{
[9eefda]700 struct BFSAccounting BFS;
[ef9aae]701 atom *OtherAtom = Walker;
702
[e138de]703 InitializeBFSAccounting(BFS, AtomCount);
[ef9aae]704
[e138de]705 ResetBFSAccounting(Walker, BFS);
[9eefda]706 while (OtherAtom != NULL) { // look for Root
[a564be]707 ASSERT(!BFS.BFSStack->empty(), "CyclicStructureAnalysis_BFSToNextCycle() - BFS.BFSStack is empty!");
708 Walker = BFS.BFSStack->front();
709 BFS.BFSStack->pop_front();
[e138de]710 //Log() << Verbose(2) << "Current Walker is " << *Walker << ", we look for SP to Root " << *Root << "." << endl;
[9d83b6]711 const BondList& ListOfBonds = Walker->getListOfBonds();
712 for (BondList::const_iterator Runner = ListOfBonds.begin();
713 Runner != ListOfBonds.end();
714 ++Runner) {
[9eefda]715 // "removed (*Runner) != BackEdge) || " from next if, is u
[9d83b6]716 if ((ListOfBonds.size() == 1)) { // only walk along DFS spanning tree (otherwise we always find SP of 1 being backedge Binder), but terminal hydrogens may be connected via backedge, hence extra check
[ef9aae]717 OtherAtom = (*Runner)->GetOtherAtom(Walker);
[e138de]718 //Log() << Verbose(2) << "Current OtherAtom is: " << OtherAtom->Name << " for bond " << *Binder << "." << endl;
[ce5f05]719 if (BFS.ColorList[OtherAtom->getNr()] == bond::white) {
[a564be]720 BFS.TouchedStack->push_front(OtherAtom);
[ce5f05]721 BFS.ColorList[OtherAtom->getNr()] = bond::lightgray;
[735b1c]722 BFS.PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor
723 BFS.ShortestPathList[OtherAtom->getNr()] = BFS.ShortestPathList[Walker->getNr()] + 1;
724 //Log() << Verbose(2) << "Coloring OtherAtom " << OtherAtom->Name << " lightgray, its predecessor is " << Walker->Name << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long." << endl;
[ef9aae]725 if (OtherAtom->GetTrueFather()->IsCyclic) { // if the other atom is connected to a ring
[735b1c]726 MinimumRingSize[Root->GetTrueFather()->getNr()] = BFS.ShortestPathList[OtherAtom->getNr()] + MinimumRingSize[OtherAtom->GetTrueFather()->getNr()];
[ef9aae]727 OtherAtom = NULL; //break;
728 break;
729 } else
[a564be]730 BFS.BFSStack->push_front(OtherAtom);
[ef9aae]731 } else {
[e138de]732 //Log() << Verbose(3) << "Not Adding, has already been visited." << endl;
[ef9aae]733 }
734 } else {
[e138de]735 //Log() << Verbose(3) << "Not Visiting, is a back edge." << endl;
[ef9aae]736 }
737 }
[ce5f05]738 BFS.ColorList[Walker->getNr()] = bond::black;
[e138de]739 //Log() << Verbose(1) << "Coloring Walker " << Walker->Name << " black." << endl;
[ef9aae]740 }
741 //CleanAccountingLists(TouchedStack, PredecessorList, ShortestPathList, ColorList);
742
[e138de]743 FinalizeBFSAccounting(BFS);
[9eefda]744}
745;
[ef9aae]746
[9eefda]747/** All nodes that are not in cycles get assigned a \a *&MinimumRingSizeby BFS to next cycle.
748 * \param *&MinimumRingSize array with minimum distance without encountering onself for each atom
749 * \param &MinRingSize global minium distance
750 * \param &NumCyles number of cycles in graph
751 * \param *mol molecule with atoms
752 */
[e138de]753void CyclicStructureAnalysis_AssignRingSizetoNonCycleMembers(int *&MinimumRingSize, int &MinRingSize, int &NumCycles, const molecule * const mol)
[ef9aae]754{
[9eefda]755 atom *Root = NULL;
[ef9aae]756 atom *Walker = NULL;
757 if (MinRingSize != -1) { // if rings are present
758 // go over all atoms
[9879f6]759 for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
760 Root = *iter;
[ef9aae]761
[735b1c]762 if (MinimumRingSize[Root->GetTrueFather()->getNr()] == mol->getAtomCount()) { // check whether MinimumRingSize is set, if not BFS to next where it is
[ef9aae]763 Walker = Root;
764
[e138de]765 //Log() << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl;
[ea7176]766 CyclicStructureAnalysis_BFSToNextCycle(Root, Walker, MinimumRingSize, mol->getAtomCount());
[ef9aae]767
768 }
[735b1c]769 DoLog(1) && (Log() << Verbose(1) << "Minimum ring size of " << *Root << " is " << MinimumRingSize[Root->GetTrueFather()->getNr()] << "." << endl);
[ef9aae]770 }
[a67d19]771 DoLog(1) && (Log() << Verbose(1) << "Minimum ring size is " << MinRingSize << ", over " << NumCycles << " cycles total." << endl);
[ef9aae]772 } else
[a67d19]773 DoLog(1) && (Log() << Verbose(1) << "No rings were detected in the molecular structure." << endl);
[9eefda]774}
775;
[ef9aae]776
[cee0b57]777/** Analyses the cycles found and returns minimum of all cycle lengths.
778 * We begin with a list of Back edges found during DepthFirstSearchAnalysis(). We go through this list - one end is the Root,
779 * the other our initial Walker - and do a Breadth First Search for the Root. We mark down each Predecessor and as soon as
780 * we have found the Root via BFS, we may climb back the closed cycle via the Predecessors. Thereby we mark atoms and bonds
781 * as cyclic and print out the cycles.
782 * \param *BackEdgeStack stack with all back edges found during DFS scan. Beware: This stack contains the bonds from the total molecule, not from the subgraph!
783 * \param *&MinimumRingSize contains smallest ring size in molecular structure on return or -1 if no rings were found, if set is maximum search distance
784 * \todo BFS from the not-same-LP to find back to starting point of tributary cycle over more than one bond
785 */
[9d37ac]786void molecule::CyclicStructureAnalysis(
787 std::deque<bond *> * BackEdgeStack,
788 int *&MinimumRingSize
789 ) const
[cee0b57]790{
[9eefda]791 struct BFSAccounting BFS;
[ef9aae]792 atom *Walker = NULL;
793 atom *OtherAtom = NULL;
794 bond *BackEdge = NULL;
795 int NumCycles = 0;
796 int MinRingSize = -1;
[cee0b57]797
[ea7176]798 InitializeBFSAccounting(BFS, getAtomCount());
[cee0b57]799
[e138de]800 //Log() << Verbose(1) << "Back edge list - ";
[99593f]801 //BackEdgeStack->Output(out);
[cee0b57]802
[a67d19]803 DoLog(1) && (Log() << Verbose(1) << "Analysing cycles ... " << endl);
[cee0b57]804 NumCycles = 0;
[a564be]805 while (!BackEdgeStack->empty()) {
806 BackEdge = BackEdgeStack->front();
807 BackEdgeStack->pop_front();
[cee0b57]808 // this is the target
[9eefda]809 BFS.Root = BackEdge->leftatom;
[cee0b57]810 // this is the source point
811 Walker = BackEdge->rightatom;
812
[e138de]813 ResetBFSAccounting(Walker, BFS);
[cee0b57]814
[a67d19]815 DoLog(1) && (Log() << Verbose(1) << "---------------------------------------------------------------------------------------------------------" << endl);
[ef9aae]816 OtherAtom = NULL;
[e138de]817 CyclicStructureAnalysis_CyclicBFSFromRootToRoot(BackEdge, BFS);
[cee0b57]818
[e138de]819 CyclicStructureAnalysis_RetrieveCycleMembers(OtherAtom, BackEdge, BFS, MinimumRingSize, MinRingSize);
[cee0b57]820
[e138de]821 CleanBFSAccounting(BFS);
[ef9aae]822 }
[e138de]823 FinalizeBFSAccounting(BFS);
[ef9aae]824
[e138de]825 CyclicStructureAnalysis_AssignRingSizetoNonCycleMembers(MinimumRingSize, MinRingSize, NumCycles, this);
[fa649a]826};
[cee0b57]827
828/** Sets the next component number.
829 * This is O(N) as the number of bonds per atom is bound.
830 * \param *vertex atom whose next atom::*ComponentNr is to be set
[5309ba]831 * \param Nr number to use
[cee0b57]832 */
[fa649a]833void molecule::SetNextComponentNumber(atom *vertex, int nr) const
[cee0b57]834{
[9eefda]835 size_t i = 0;
[cee0b57]836 if (vertex != NULL) {
[9d83b6]837 const BondList& ListOfBonds = vertex->getListOfBonds();
838 for (; i < ListOfBonds.size(); i++) {
[9eefda]839 if (vertex->ComponentNr[i] == -1) { // check if not yet used
[cee0b57]840 vertex->ComponentNr[i] = nr;
841 break;
[9eefda]842 } else if (vertex->ComponentNr[i] == nr) // if number is already present, don't add another time
843 break; // breaking here will not cause error!
[cee0b57]844 }
[9d83b6]845 if (i == ListOfBonds.size()) {
[58ed4a]846 DoeLog(0) && (eLog()<< Verbose(0) << "Error: All Component entries are already occupied!" << endl);
[e359a8]847 performCriticalExit();
848 }
849 } else {
[58ed4a]850 DoeLog(0) && (eLog()<< Verbose(0) << "Error: Given vertex is NULL!" << endl);
[e359a8]851 performCriticalExit();
852 }
[9eefda]853}
854;
[cee0b57]855
856/** Returns next unused bond for this atom \a *vertex or NULL of none exists.
857 * \param *vertex atom to regard
858 * \return bond class or NULL
859 */
[fa649a]860bond * molecule::FindNextUnused(atom *vertex) const
[cee0b57]861{
[9d83b6]862 const BondList& ListOfBonds = vertex->getListOfBonds();
863 for (BondList::const_iterator Runner = ListOfBonds.begin();
864 Runner != ListOfBonds.end();
865 ++Runner)
[ce5f05]866 if ((*Runner)->IsUsed() == bond::white)
[9eefda]867 return ((*Runner));
[cee0b57]868 return NULL;
[9eefda]869}
870;
[cee0b57]871
872/** Resets bond::Used flag of all bonds in this molecule.
873 * \return true - success, false - -failure
874 */
[fa649a]875void molecule::ResetAllBondsToUnused() const
[cee0b57]876{
[9d83b6]877 for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
878 const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
879 for(BondList::const_iterator BondRunner = ListOfBonds.begin();
880 BondRunner != ListOfBonds.end();
881 ++BondRunner)
[e08c46]882 if ((*BondRunner)->leftatom == *AtomRunner)
883 (*BondRunner)->ResetUsed();
[9d83b6]884 }
[9eefda]885}
886;
[cee0b57]887
888/** Output a list of flags, stating whether the bond was visited or not.
[9d37ac]889 * \param *list list to print
[cee0b57]890 */
[e138de]891void OutputAlreadyVisited(int *list)
[cee0b57]892{
[a67d19]893 DoLog(4) && (Log() << Verbose(4) << "Already Visited Bonds:\t");
[9eefda]894 for (int i = 1; i <= list[0]; i++)
[a67d19]895 DoLog(0) && (Log() << Verbose(0) << list[i] << " ");
896 DoLog(0) && (Log() << Verbose(0) << endl);
[9eefda]897}
898;
[cee0b57]899
900/** Storing the bond structure of a molecule to file.
[5309ba]901 * Simply stores Atom::Nr and then the Atom::Nr of all bond partners per line.
[35b698]902 * \param &filename name of file
903 * \param path path to file, defaults to empty
[cee0b57]904 * \return true - file written successfully, false - writing failed
905 */
[e4afb4]906bool molecule::StoreAdjacencyToFile(std::string filename, std::string path)
[cee0b57]907{
908 ofstream AdjacencyFile;
[35b698]909 string line;
[cee0b57]910 bool status = true;
911
[35b698]912 if (path != "")
913 line = path + "/" + filename;
[8ab0407]914 else
[35b698]915 line = filename;
916 AdjacencyFile.open(line.c_str(), ios::out);
[acf800]917 DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl);
[35b698]918 if (AdjacencyFile.good()) {
[1f1b23]919 AdjacencyFile << "m\tn" << endl;
[00ef5c]920 for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputAdjacency),&AdjacencyFile));
[cee0b57]921 AdjacencyFile.close();
[acf800]922 DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl);
[cee0b57]923 } else {
[35b698]924 DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line << "." << endl);
[cee0b57]925 status = false;
926 }
927
928 return status;
[9eefda]929}
930;
[cee0b57]931
[1f1b23]932/** Storing the bond structure of a molecule to file.
[5309ba]933 * Simply stores Atom::Nr and then the Atom::Nr of all bond partners, one per line.
[35b698]934 * \param &filename name of file
935 * \param path path to file, defaults to empty
[1f1b23]936 * \return true - file written successfully, false - writing failed
937 */
[e4afb4]938bool molecule::StoreBondsToFile(std::string filename, std::string path)
[1f1b23]939{
940 ofstream BondFile;
[35b698]941 string line;
[1f1b23]942 bool status = true;
943
[35b698]944 if (path != "")
945 line = path + "/" + filename;
[8ab0407]946 else
[35b698]947 line = filename;
948 BondFile.open(line.c_str(), ios::out);
[acf800]949 DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl);
[35b698]950 if (BondFile.good()) {
[1f1b23]951 BondFile << "m\tn" << endl;
[00ef5c]952 for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputBonds),&BondFile));
[1f1b23]953 BondFile.close();
[acf800]954 DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl);
[1f1b23]955 } else {
[35b698]956 DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line << "." << endl);
[1f1b23]957 status = false;
958 }
959
960 return status;
961}
962;
963
[35b698]964bool CheckAdjacencyFileAgainstMolecule_Init(std::string &path, ifstream &File, int *&CurrentBonds)
[ba4170]965{
[35b698]966 string filename;
967 filename = path + ADJACENCYFILE;
968 File.open(filename.c_str(), ios::out);
[0de7e8]969 DoLog(1) && (Log() << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... " << endl);
[35b698]970 if (File.fail())
[ba4170]971 return false;
972
973 // allocate storage structure
[1d5afa5]974 CurrentBonds = new int[MAXBONDS]; // contains parsed bonds of current atom
975 for(int i=0;i<MAXBONDS;i++)
[920c70]976 CurrentBonds[i] = 0;
[ba4170]977 return true;
[9eefda]978}
979;
[ba4170]980
[e138de]981void CheckAdjacencyFileAgainstMolecule_Finalize(ifstream &File, int *&CurrentBonds)
[ba4170]982{
983 File.close();
984 File.clear();
[920c70]985 delete[](CurrentBonds);
[9eefda]986}
987;
[ba4170]988
[e138de]989void CheckAdjacencyFileAgainstMolecule_CompareBonds(bool &status, int &NonMatchNumber, atom *&Walker, size_t &CurrentBondsOfAtom, int AtomNr, int *&CurrentBonds, atom **ListOfAtoms)
[ba4170]990{
991 size_t j = 0;
992 int id = -1;
993
[e138de]994 //Log() << Verbose(2) << "Walker is " << *Walker << ", bond partners: ";
[9d83b6]995 const BondList& ListOfBonds = Walker->getListOfBonds();
996 if (CurrentBondsOfAtom == ListOfBonds.size()) {
997 for (BondList::const_iterator Runner = ListOfBonds.begin();
998 Runner != ListOfBonds.end();
999 ++Runner) {
[735b1c]1000 id = (*Runner)->GetOtherAtom(Walker)->getNr();
[ba4170]1001 j = 0;
[9eefda]1002 for (; (j < CurrentBondsOfAtom) && (CurrentBonds[j++] != id);)
[ba4170]1003 ; // check against all parsed bonds
[9eefda]1004 if (CurrentBonds[j - 1] != id) { // no match ? Then mark in ListOfAtoms
[ba4170]1005 ListOfAtoms[AtomNr] = NULL;
1006 NonMatchNumber++;
1007 status = false;
[0de7e8]1008 DoeLog(2) && (eLog() << Verbose(2) << id << " can not be found in list." << endl);
[ba4170]1009 } else {
[0de7e8]1010 //Log() << Verbose(0) << "[" << id << "]\t";
[ba4170]1011 }
1012 }
[e138de]1013 //Log() << Verbose(0) << endl;
[ba4170]1014 } else {
[9d83b6]1015 DoLog(0) && (Log() << Verbose(0) << "Number of bonds for Atom " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << ListOfBonds.size() << "." << endl);
[ba4170]1016 status = false;
1017 }
[9eefda]1018}
1019;
[ba4170]1020
[cee0b57]1021/** Checks contents of adjacency file against bond structure in structure molecule.
1022 * \param *path path to file
[5309ba]1023 * \param **ListOfAtoms allocated (molecule::AtomCount) and filled lookup table for ids (Atom::Nr) to *Atom
[cee0b57]1024 * \return true - structure is equal, false - not equivalence
1025 */
[35b698]1026bool molecule::CheckAdjacencyFileAgainstMolecule(std::string &path, atom **ListOfAtoms)
[cee0b57]1027{
1028 ifstream File;
1029 bool status = true;
[266237]1030 atom *Walker = NULL;
[ba4170]1031 int *CurrentBonds = NULL;
[9eefda]1032 int NonMatchNumber = 0; // will number of atoms with differing bond structure
[ba4170]1033 size_t CurrentBondsOfAtom = -1;
[0de7e8]1034 const int AtomCount = getAtomCount();
[cee0b57]1035
[e138de]1036 if (!CheckAdjacencyFileAgainstMolecule_Init(path, File, CurrentBonds)) {
[a67d19]1037 DoLog(1) && (Log() << Verbose(1) << "Adjacency file not found." << endl);
[ba4170]1038 return true;
1039 }
1040
[920c70]1041 char buffer[MAXSTRINGSIZE];
[1d5afa5]1042 int tmp;
[ba4170]1043 // Parse the file line by line and count the bonds
1044 while (!File.eof()) {
1045 File.getline(buffer, MAXSTRINGSIZE);
1046 stringstream line;
1047 line.str(buffer);
1048 int AtomNr = -1;
1049 line >> AtomNr;
1050 CurrentBondsOfAtom = -1; // we count one too far due to line end
1051 // parse into structure
[0de7e8]1052 if ((AtomNr >= 0) && (AtomNr < AtomCount)) {
[ba4170]1053 Walker = ListOfAtoms[AtomNr];
[1d5afa5]1054 while (line >> ws >> tmp) {
1055 std::cout << "Recognized bond partner " << tmp << std::endl;
1056 CurrentBonds[++CurrentBondsOfAtom] = tmp;
1057 ASSERT(CurrentBondsOfAtom < MAXBONDS,
1058 "molecule::CheckAdjacencyFileAgainstMolecule() - encountered more bonds than allowed: "
1059 +toString(CurrentBondsOfAtom)+" >= "+toString(MAXBONDS)+"!");
1060 }
[ba4170]1061 // compare against present bonds
[e138de]1062 CheckAdjacencyFileAgainstMolecule_CompareBonds(status, NonMatchNumber, Walker, CurrentBondsOfAtom, AtomNr, CurrentBonds, ListOfAtoms);
[0de7e8]1063 } else {
1064 if (AtomNr != -1)
1065 DoeLog(2) && (eLog() << Verbose(2) << AtomNr << " is not valid in the range of ids [" << 0 << "," << AtomCount << ")." << endl);
[ba4170]1066 }
[cee0b57]1067 }
[e138de]1068 CheckAdjacencyFileAgainstMolecule_Finalize(File, CurrentBonds);
[cee0b57]1069
[ba4170]1070 if (status) { // if equal we parse the KeySetFile
[a67d19]1071 DoLog(1) && (Log() << Verbose(1) << "done: Equal." << endl);
[ba4170]1072 } else
[a67d19]1073 DoLog(1) && (Log() << Verbose(1) << "done: Not equal by " << NonMatchNumber << " atoms." << endl);
[cee0b57]1074 return status;
[9eefda]1075}
1076;
[cee0b57]1077
1078/** Picks from a global stack with all back edges the ones in the fragment.
[5309ba]1079 * \param **ListOfLocalAtoms array of father atom::Nr to local atom::Nr (reverse of atom::father)
[cee0b57]1080 * \param *ReferenceStack stack with all the back egdes
1081 * \param *LocalStack stack to be filled
1082 * \return true - everything ok, false - ReferenceStack was empty
1083 */
[a564be]1084bool molecule::PickLocalBackEdges(atom **ListOfLocalAtoms, std::deque<bond *> *&ReferenceStack, std::deque<bond *> *&LocalStack) const
[cee0b57]1085{
1086 bool status = true;
[a564be]1087 if (ReferenceStack->empty()) {
[a67d19]1088 DoLog(1) && (Log() << Verbose(1) << "ReferenceStack is empty!" << endl);
[cee0b57]1089 return false;
1090 }
[a564be]1091 bond *Binder = ReferenceStack->front();
1092 ReferenceStack->pop_front();
[9eefda]1093 bond *FirstBond = Binder; // mark the first bond, so that we don't loop through the stack indefinitely
[cee0b57]1094 atom *Walker = NULL, *OtherAtom = NULL;
[a564be]1095 ReferenceStack->push_front(Binder);
[cee0b57]1096
[9eefda]1097 do { // go through all bonds and push local ones
[735b1c]1098 Walker = ListOfLocalAtoms[Binder->leftatom->getNr()]; // get one atom in the reference molecule
[9d83b6]1099 if (Walker != NULL) { // if this Walker exists in the subgraph ...
1100 const BondList& ListOfBonds = Walker->getListOfBonds();
1101 for (BondList::const_iterator Runner = ListOfBonds.begin();
1102 Runner != ListOfBonds.end();
1103 ++Runner) {
[266237]1104 OtherAtom = (*Runner)->GetOtherAtom(Walker);
[735b1c]1105 if (OtherAtom == ListOfLocalAtoms[(*Runner)->rightatom->getNr()]) { // found the bond
[a564be]1106 LocalStack->push_front((*Runner));
[a67d19]1107 DoLog(3) && (Log() << Verbose(3) << "Found local edge " << *(*Runner) << "." << endl);
[cee0b57]1108 break;
1109 }
1110 }
[9d83b6]1111 }
[a564be]1112 ASSERT(!ReferenceStack->empty(), "molecule::PickLocalBackEdges() - ReferenceStack is empty!");
1113 Binder = ReferenceStack->front(); // loop the stack for next item
1114 ReferenceStack->pop_front();
[a67d19]1115 DoLog(3) && (Log() << Verbose(3) << "Current candidate edge " << Binder << "." << endl);
[a564be]1116 ReferenceStack->push_front(Binder);
[cee0b57]1117 } while (FirstBond != Binder);
1118
1119 return status;
[9eefda]1120}
1121;
[ce7cc5]1122
1123void BreadthFirstSearchAdd_Init(struct BFSAccounting &BFS, atom *&Root, int AtomCount, int BondOrder, atom **AddedAtomList = NULL)
1124{
1125 BFS.AtomCount = AtomCount;
1126 BFS.BondOrder = BondOrder;
[920c70]1127 BFS.PredecessorList = new atom*[AtomCount];
1128 BFS.ShortestPathList = new int[AtomCount];
[ce5f05]1129 BFS.ColorList = new enum bond::Shading[AtomCount];
[a564be]1130 BFS.BFSStack = new std::deque<atom *> (AtomCount);
[ce7cc5]1131
1132 BFS.Root = Root;
[a564be]1133 BFS.BFSStack->clear();
1134 BFS.BFSStack->push_front(Root);
[ce7cc5]1135
1136 // initialise each vertex as white with no predecessor, empty queue, color Root lightgray
[9eefda]1137 for (int i = AtomCount; i--;) {
[920c70]1138 BFS.PredecessorList[i] = NULL;
[ce7cc5]1139 BFS.ShortestPathList[i] = -1;
1140 if ((AddedAtomList != NULL) && (AddedAtomList[i] != NULL)) // mark already present atoms (i.e. Root and maybe others) as visited
[ce5f05]1141 BFS.ColorList[i] = bond::lightgray;
[ce7cc5]1142 else
[ce5f05]1143 BFS.ColorList[i] = bond::white;
[ce7cc5]1144 }
[735b1c]1145 //BFS.ShortestPathList[Root->getNr()] = 0; // done by Calloc
[9eefda]1146}
1147;
[ce7cc5]1148
1149void BreadthFirstSearchAdd_Free(struct BFSAccounting &BFS)
1150{
[920c70]1151 delete[](BFS.PredecessorList);
1152 delete[](BFS.ShortestPathList);
1153 delete[](BFS.ColorList);
[9eefda]1154 delete (BFS.BFSStack);
[ce7cc5]1155 BFS.AtomCount = 0;
[9eefda]1156}
1157;
[ce7cc5]1158
[e138de]1159void BreadthFirstSearchAdd_UnvisitedNode(molecule *Mol, struct BFSAccounting &BFS, atom *&Walker, atom *&OtherAtom, bond *&Binder, bond *&Bond, atom **&AddedAtomList, bond **&AddedBondList, bool IsAngstroem)
[ce7cc5]1160{
1161 if (Binder != Bond) // let other atom white if it's via Root bond. In case it's cyclic it has to be reached again (yet Root is from OtherAtom already black, thus no problem)
[ce5f05]1162 BFS.ColorList[OtherAtom->getNr()] = bond::lightgray;
[735b1c]1163 BFS.PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor
1164 BFS.ShortestPathList[OtherAtom->getNr()] = BFS.ShortestPathList[Walker->getNr()] + 1;
[ce5f05]1165 DoLog(2) && (Log() << Verbose(2) << "Coloring OtherAtom " << OtherAtom->getName() << " " << ((BFS.ColorList[OtherAtom->getNr()] == bond::white) ? "white" : "lightgray") << ", its predecessor is " << Walker->getName() << " and its Shortest Path is " << BFS.ShortestPathList[OtherAtom->getNr()] << " egde(s) long." << endl);
[735b1c]1166 if ((((BFS.ShortestPathList[OtherAtom->getNr()] < BFS.BondOrder) && (Binder != Bond)))) { // Check for maximum distance
[a67d19]1167 DoLog(3) && (Log() << Verbose(3));
[735b1c]1168 if (AddedAtomList[OtherAtom->getNr()] == NULL) { // add if it's not been so far
1169 AddedAtomList[OtherAtom->getNr()] = Mol->AddCopyAtom(OtherAtom);
[68f03d]1170 DoLog(0) && (Log() << Verbose(0) << "Added OtherAtom " << OtherAtom->getName());
[735b1c]1171 AddedBondList[Binder->nr] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
[a67d19]1172 DoLog(0) && (Log() << Verbose(0) << " and bond " << *(AddedBondList[Binder->nr]) << ", ");
[9eefda]1173 } else { // this code should actually never come into play (all white atoms are not yet present in BondMolecule, that's why they are white in the first place)
[68f03d]1174 DoLog(0) && (Log() << Verbose(0) << "Not adding OtherAtom " << OtherAtom->getName());
[ce7cc5]1175 if (AddedBondList[Binder->nr] == NULL) {
[735b1c]1176 AddedBondList[Binder->nr] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
[a67d19]1177 DoLog(0) && (Log() << Verbose(0) << ", added Bond " << *(AddedBondList[Binder->nr]));
[ce7cc5]1178 } else
[a67d19]1179 DoLog(0) && (Log() << Verbose(0) << ", not added Bond ");
[ce7cc5]1180 }
[a67d19]1181 DoLog(0) && (Log() << Verbose(0) << ", putting OtherAtom into queue." << endl);
[a564be]1182 BFS.BFSStack->push_front(OtherAtom);
[ce7cc5]1183 } else { // out of bond order, then replace
[735b1c]1184 if ((AddedAtomList[OtherAtom->getNr()] == NULL) && (Binder->Cyclic))
[ce5f05]1185 BFS.ColorList[OtherAtom->getNr()] = bond::white; // unmark if it has not been queued/added, to make it available via its other bonds (cyclic)
[ce7cc5]1186 if (Binder == Bond)
[a67d19]1187 DoLog(3) && (Log() << Verbose(3) << "Not Queueing, is the Root bond");
[735b1c]1188 else if (BFS.ShortestPathList[OtherAtom->getNr()] >= BFS.BondOrder)
[a67d19]1189 DoLog(3) && (Log() << Verbose(3) << "Not Queueing, is out of Bond Count of " << BFS.BondOrder);
[ce7cc5]1190 if (!Binder->Cyclic)
[a67d19]1191 DoLog(0) && (Log() << Verbose(0) << ", is not part of a cyclic bond, saturating bond with Hydrogen." << endl);
[ce7cc5]1192 if (AddedBondList[Binder->nr] == NULL) {
[735b1c]1193 if ((AddedAtomList[OtherAtom->getNr()] != NULL)) { // .. whether we add or saturate
1194 AddedBondList[Binder->nr] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
[ce7cc5]1195 } else {
[9eefda]1196#ifdef ADDHYDROGEN
[735b1c]1197 if (!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->getNr()], Walker, OtherAtom, IsAngstroem))
[9eefda]1198 exit(1);
1199#endif
[ce7cc5]1200 }
1201 }
1202 }
[9eefda]1203}
1204;
[ce7cc5]1205
[e138de]1206void BreadthFirstSearchAdd_VisitedNode(molecule *Mol, struct BFSAccounting &BFS, atom *&Walker, atom *&OtherAtom, bond *&Binder, bond *&Bond, atom **&AddedAtomList, bond **&AddedBondList, bool IsAngstroem)
[ce7cc5]1207{
[a67d19]1208 DoLog(3) && (Log() << Verbose(3) << "Not Adding, has already been visited." << endl);
[ce7cc5]1209 // This has to be a cyclic bond, check whether it's present ...
1210 if (AddedBondList[Binder->nr] == NULL) {
[735b1c]1211 if ((Binder != Bond) && (Binder->Cyclic) && (((BFS.ShortestPathList[Walker->getNr()] + 1) < BFS.BondOrder))) {
1212 AddedBondList[Binder->nr] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder);
[ce7cc5]1213 } else { // if it's root bond it has to broken (otherwise we would not create the fragments)
[9eefda]1214#ifdef ADDHYDROGEN
[735b1c]1215 if(!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->getNr()], Walker, OtherAtom, IsAngstroem))
[9eefda]1216 exit(1);
1217#endif
[ce7cc5]1218 }
1219 }
[9eefda]1220}
1221;
[cee0b57]1222
1223/** Adds atoms up to \a BondCount distance from \a *Root and notes them down in \a **AddedAtomList.
[a564be]1224 * Gray vertices are always enqueued in an std::deque<atom *> FIFO queue, the rest is usual BFS with adding vertices found was
[cee0b57]1225 * white and putting into queue.
1226 * \param *Mol Molecule class to add atoms to
1227 * \param **AddedAtomList list with added atom pointers, index is atom father's number
1228 * \param **AddedBondList list with added bond pointers, index is bond father's number
1229 * \param *Root root vertex for BFS
1230 * \param *Bond bond not to look beyond
1231 * \param BondOrder maximum distance for vertices to add
1232 * \param IsAngstroem lengths are in angstroem or bohrradii
1233 */
[e138de]1234void molecule::BreadthFirstSearchAdd(molecule *Mol, atom **&AddedAtomList, bond **&AddedBondList, atom *Root, bond *Bond, int BondOrder, bool IsAngstroem)
[cee0b57]1235{
[ce7cc5]1236 struct BFSAccounting BFS;
[cee0b57]1237 atom *Walker = NULL, *OtherAtom = NULL;
[ce7cc5]1238 bond *Binder = NULL;
[cee0b57]1239
1240 // add Root if not done yet
[735b1c]1241 if (AddedAtomList[Root->getNr()] == NULL) // add Root if not yet present
1242 AddedAtomList[Root->getNr()] = Mol->AddCopyAtom(Root);
[cee0b57]1243
[ea7176]1244 BreadthFirstSearchAdd_Init(BFS, Root, BondOrder, getAtomCount(), AddedAtomList);
[cee0b57]1245
1246 // and go on ... Queue always contains all lightgray vertices
[a564be]1247 while (!BFS.BFSStack->empty()) {
[cee0b57]1248 // we have to pop the oldest atom from stack. This keeps the atoms on the stack always of the same ShortestPath distance.
1249 // 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
1250 // append length of 3 (their neighbours). Thus on stack we have always atoms of a certain length n at bottom of stack and
1251 // followed by n+1 till top of stack.
[a564be]1252 Walker = BFS.BFSStack->front(); // pop oldest added
1253 BFS.BFSStack->pop_front();
[9d83b6]1254 const BondList& ListOfBonds = Walker->getListOfBonds();
1255 DoLog(1) && (Log() << Verbose(1) << "Current Walker is: " << Walker->getName() << ", and has " << ListOfBonds.size() << " bonds." << endl);
1256 for (BondList::const_iterator Runner = ListOfBonds.begin();
1257 Runner != ListOfBonds.end();
1258 ++Runner) {
[266237]1259 if ((*Runner) != NULL) { // don't look at bond equal NULL
[ce7cc5]1260 Binder = (*Runner);
[266237]1261 OtherAtom = (*Runner)->GetOtherAtom(Walker);
[68f03d]1262 DoLog(2) && (Log() << Verbose(2) << "Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << "." << endl);
[ce5f05]1263 if (BFS.ColorList[OtherAtom->getNr()] == bond::white) {
[e138de]1264 BreadthFirstSearchAdd_UnvisitedNode(Mol, BFS, Walker, OtherAtom, Binder, Bond, AddedAtomList, AddedBondList, IsAngstroem);
[cee0b57]1265 } else {
[e138de]1266 BreadthFirstSearchAdd_VisitedNode(Mol, BFS, Walker, OtherAtom, Binder, Bond, AddedAtomList, AddedBondList, IsAngstroem);
[cee0b57]1267 }
1268 }
1269 }
[ce5f05]1270 BFS.ColorList[Walker->getNr()] = bond::black;
[68f03d]1271 DoLog(1) && (Log() << Verbose(1) << "Coloring Walker " << Walker->getName() << " black." << endl);
[cee0b57]1272 }
[ce7cc5]1273 BreadthFirstSearchAdd_Free(BFS);
[9eefda]1274}
1275;
[cee0b57]1276
[266237]1277/** Adds a bond as a copy to a given one
1278 * \param *left leftatom of new bond
1279 * \param *right rightatom of new bond
1280 * \param *CopyBond rest of fields in bond are copied from this
1281 * \return pointer to new bond
1282 */
1283bond * molecule::CopyBond(atom *left, atom *right, bond *CopyBond)
1284{
1285 bond *Binder = AddBond(left, right, CopyBond->BondDegree);
1286 Binder->Cyclic = CopyBond->Cyclic;
1287 Binder->Type = CopyBond->Type;
1288 return Binder;
[9eefda]1289}
1290;
[266237]1291
[e138de]1292void BuildInducedSubgraph_Init(atom **&ParentList, int AtomCount)
[cee0b57]1293{
1294 // reset parent list
[920c70]1295 ParentList = new atom*[AtomCount];
1296 for (int i=0;i<AtomCount;i++)
1297 ParentList[i] = NULL;
[a67d19]1298 DoLog(3) && (Log() << Verbose(3) << "Resetting ParentList." << endl);
[9eefda]1299}
1300;
[cee0b57]1301
[e138de]1302void BuildInducedSubgraph_FillParentList(const molecule *mol, const molecule *Father, atom **&ParentList)
[43587e]1303{
[cee0b57]1304 // fill parent list with sons
[a67d19]1305 DoLog(3) && (Log() << Verbose(3) << "Filling Parent List." << endl);
[9879f6]1306 for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
[735b1c]1307 ParentList[(*iter)->father->getNr()] = (*iter);
[cee0b57]1308 // Outputting List for debugging
[735b1c]1309 DoLog(4) && (Log() << Verbose(4) << "Son[" << (*iter)->father->getNr() << "] of " << (*iter)->father << " is " << ParentList[(*iter)->father->getNr()] << "." << endl);
[cee0b57]1310 }
[a7b761b]1311};
[43587e]1312
[e138de]1313void BuildInducedSubgraph_Finalize(atom **&ParentList)
[43587e]1314{
[920c70]1315 delete[](ParentList);
[9eefda]1316}
1317;
[43587e]1318
[e138de]1319bool BuildInducedSubgraph_CreateBondsFromParent(molecule *mol, const molecule *Father, atom **&ParentList)
[43587e]1320{
1321 bool status = true;
1322 atom *OtherAtom = NULL;
[cee0b57]1323 // check each entry of parent list and if ok (one-to-and-onto matching) create bonds
[a67d19]1324 DoLog(3) && (Log() << Verbose(3) << "Creating bonds." << endl);
[9879f6]1325 for (molecule::const_iterator iter = Father->begin(); iter != Father->end(); ++iter) {
[735b1c]1326 if (ParentList[(*iter)->getNr()] != NULL) {
1327 if (ParentList[(*iter)->getNr()]->father != (*iter)) {
[cee0b57]1328 status = false;
1329 } else {
[9d83b6]1330 const BondList& ListOfBonds = (*iter)->getListOfBonds();
1331 for (BondList::const_iterator Runner = ListOfBonds.begin();
1332 Runner != ListOfBonds.end();
1333 ++Runner) {
[9879f6]1334 OtherAtom = (*Runner)->GetOtherAtom((*iter));
[735b1c]1335 if (ParentList[OtherAtom->getNr()] != NULL) { // if otheratom is also a father of an atom on this molecule, create the bond
1336 DoLog(4) && (Log() << Verbose(4) << "Endpoints of Bond " << (*Runner) << " are both present: " << ParentList[(*iter)->getNr()]->getName() << " and " << ParentList[OtherAtom->getNr()]->getName() << "." << endl);
1337 mol->AddBond(ParentList[(*iter)->getNr()], ParentList[OtherAtom->getNr()], (*Runner)->BondDegree);
[cee0b57]1338 }
1339 }
1340 }
1341 }
1342 }
[43587e]1343 return status;
[9eefda]1344}
1345;
[cee0b57]1346
[43587e]1347/** Adds bond structure to this molecule from \a Father molecule.
1348 * This basically causes this molecule to become an induced subgraph of the \a Father, i.e. for every bond in Father
1349 * with end points present in this molecule, bond is created in this molecule.
1350 * Special care was taken to ensure that this is of complexity O(N), where N is the \a Father's molecule::AtomCount.
1351 * \param *Father father molecule
1352 * \return true - is induced subgraph, false - there are atoms with fathers not in \a Father
1353 * \todo not checked, not fully working probably
1354 */
[9d37ac]1355bool molecule::BuildInducedSubgraph(const molecule *Father){
[43587e]1356 bool status = true;
1357 atom **ParentList = NULL;
[a67d19]1358 DoLog(2) && (Log() << Verbose(2) << "Begin of BuildInducedSubgraph." << endl);
[ea7176]1359 BuildInducedSubgraph_Init(ParentList, Father->getAtomCount());
[e138de]1360 BuildInducedSubgraph_FillParentList(this, Father, ParentList);
1361 status = BuildInducedSubgraph_CreateBondsFromParent(this, Father, ParentList);
1362 BuildInducedSubgraph_Finalize(ParentList);
[a67d19]1363 DoLog(2) && (Log() << Verbose(2) << "End of BuildInducedSubgraph." << endl);
[cee0b57]1364 return status;
[9eefda]1365}
1366;
[cee0b57]1367
1368/** For a given keyset \a *Fragment, checks whether it is connected in the current molecule.
1369 * \param *Fragment Keyset of fragment's vertices
1370 * \return true - connected, false - disconnected
1371 * \note this is O(n^2) for it's just a bug checker not meant for permanent use!
1372 */
[e138de]1373bool molecule::CheckForConnectedSubgraph(KeySet *Fragment)
[cee0b57]1374{
1375 atom *Walker = NULL, *Walker2 = NULL;
1376 bool BondStatus = false;
1377 int size;
1378
[a67d19]1379 DoLog(1) && (Log() << Verbose(1) << "Begin of CheckForConnectedSubgraph" << endl);
1380 DoLog(2) && (Log() << Verbose(2) << "Disconnected atom: ");
[cee0b57]1381
1382 // count number of atoms in graph
1383 size = 0;
[9eefda]1384 for (KeySet::iterator runner = Fragment->begin(); runner != Fragment->end(); runner++)
[cee0b57]1385 size++;
1386 if (size > 1)
[9eefda]1387 for (KeySet::iterator runner = Fragment->begin(); runner != Fragment->end(); runner++) {
[cee0b57]1388 Walker = FindAtom(*runner);
1389 BondStatus = false;
[9eefda]1390 for (KeySet::iterator runners = Fragment->begin(); runners != Fragment->end(); runners++) {
[cee0b57]1391 Walker2 = FindAtom(*runners);
[9d83b6]1392 const BondList& ListOfBonds = Walker->getListOfBonds();
1393 for (BondList::const_iterator Runner = ListOfBonds.begin();
1394 Runner != ListOfBonds.end();
1395 ++Runner) {
[266237]1396 if ((*Runner)->GetOtherAtom(Walker) == Walker2) {
[cee0b57]1397 BondStatus = true;
1398 break;
1399 }
1400 if (BondStatus)
1401 break;
1402 }
1403 }
1404 if (!BondStatus) {
[a67d19]1405 DoLog(0) && (Log() << Verbose(0) << (*Walker) << endl);
[cee0b57]1406 return false;
1407 }
1408 }
1409 else {
[a67d19]1410 DoLog(0) && (Log() << Verbose(0) << "none." << endl);
[cee0b57]1411 return true;
1412 }
[a67d19]1413 DoLog(0) && (Log() << Verbose(0) << "none." << endl);
[cee0b57]1414
[a67d19]1415 DoLog(1) && (Log() << Verbose(1) << "End of CheckForConnectedSubgraph" << endl);
[cee0b57]1416
1417 return true;
1418}
Note: See TracBrowser for help on using the repository browser.