source: src/molecule_graph.cpp@ e7350d4

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 e7350d4 was f71baf, checked in by Frederik Heber <heber@…>, 15 years ago

Made BondGraph an instance of World, removed from config.

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