source: src/Fragmentation/BondsPerShortestPath.cpp@ 990a62

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 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 990a62 was e85169, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Prevented errors message occured due to clearing BondsPerSPList.

  • BondsPerSPList contains newly allocated bonds which a not registered with the atoms. Hence, we trigger a warning when removing the bonds which tries to unregister them first.
  • this is because we abuse our atom-bond system a sort of low man's way to a graph structure. We would be better off by obtaining the key lists representing the fragments from a true (dissected) graph. Then, we would have no need for this bond con- and destruction.
  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * BondsPerShortestPath.cpp
25 *
26 * Created on: Oct 18, 2011
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
37#include "BondsPerShortestPath.hpp"
38
39#include <sstream>
40
41#include "CodePatterns/Log.hpp"
42
43#include "Atom/atom.hpp"
44#include "Bond/bond.hpp"
45#include "Element/element.hpp"
46#include "Fragmentation/KeySet.hpp"
47
48BondsPerShortestPath::BondsPerShortestPath(int _Order) :
49 Order(_Order)
50{
51 InitialiseSPList();
52}
53
54BondsPerShortestPath::~BondsPerShortestPath()
55{
56 // free Order-dependent entries of UniqueFragments structure for next loop cycle
57 FreeSPList();
58}
59
60/** Allocates memory for BondsPerShortestPath::BondsPerSPList.
61 * \sa BondsPerShortestPath::FreeSPList()
62 */
63void BondsPerShortestPath::InitialiseSPList()
64{
65 BondsPerSPList.resize(Order);
66 BondsPerSPCount = new int[Order];
67 for (int i=Order;i--;) {
68 BondsPerSPCount[i] = 0;
69 }
70};
71
72/** Free's memory for for BondsPerShortestPath::BondsPerSPList.
73 * \sa BondsPerShortestPath::InitialiseSPList()
74 */
75void BondsPerShortestPath::FreeSPList()
76{
77 delete[](BondsPerSPCount);
78};
79
80/** Sets FragmenSearch to initial value.
81 * Sets BondsPerShortestPath::ShortestPathList entries to zero, BondsPerShortestPath::BondsPerSPCount to zero (except zero level to 1) and
82 * adds initial bond BondsPerShortestPath::Root to BondsPerShortestPath::Root to BondsPerShortestPath::BondsPerSPList
83 * \param *_Root root node, self loop becomes first bond
84 * \sa BondsPerShortestPath::FreeSPList()
85 */
86void BondsPerShortestPath::SetSPList(atom *_Root)
87{
88 // prepare root level (SP = 0) and a loop bond denoting Root
89 for (int i=Order;i--;)
90 BondsPerSPCount[i] = 0;
91 BondsPerSPCount[0] = 1;
92 bond *Binder = new bond(_Root, _Root);
93 BondsPerSPList[0].push_back(Binder);
94};
95
96/** Resets BondsPerShortestPath::ShortestPathList and cleans bonds from BondsPerShortestPath::BondsPerSPList.
97 * \sa BondsPerShortestPath::InitialiseSPList()
98 */
99void BondsPerShortestPath::ResetSPList()
100{
101 LOG(0, "Free'ing all found lists and resetting index lists");
102 std::stringstream output;
103 for(int i=Order;i--;) {
104 output << "Current SP level is " << i << ": ";
105 // delete added bonds
106 for (BondsPerSP::iterator iter = BondsPerSPList[i].begin();
107 iter != BondsPerSPList[i].end();
108 ++iter) {
109 // TODO: Hack because we have not registered bond's in BondsPerSPList with atoms
110 (*iter)->leftatom = NULL;
111 (*iter)->rightatom = NULL;
112 // now delete it, there we unregister but this checks for NULL
113 delete(*iter);
114 }
115 BondsPerSPList[i].clear();
116 // also start and end node
117 output << "cleaned.";
118 }
119 LOG(1, output.str());
120};
121
122
123/** Fills the Bonds per Shortest Path List and set the vertex labels.
124 * \param _RootKeyNr index of root node
125 * \param RestrictedKeySet Restricted vertex set to use in context of molecule
126 * \param saturation this tells whether to treat hydrogen special or not.
127 */
128void BondsPerShortestPath::FillSPListandLabelVertices(int _RootKeyNr, KeySet &RestrictedKeySet, const enum HydrogenSaturation saturation)
129{
130 // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into
131 // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning
132 // tree already sorted into various SP levels. That's why we just do loops over the depth (CurrentSP) and breadth
133 // (EdgeinSPLevel) of this tree ...
134 // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence
135 // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction.
136 int AtomKeyNr = -1;
137 atom *Walker = NULL;
138 atom *OtherWalker = NULL;
139 atom *Predecessor = NULL;
140 bond *Binder = NULL;
141 int RootKeyNr = _RootKeyNr;
142 int RemainingWalkers = -1;
143 int SP = -1;
144
145 LOG(0, "Starting BFS analysis ...");
146 for (SP = 0; SP < (Order-1); SP++) {
147 {
148 std::stringstream output;
149 output << "New SP level reached: " << SP << ", creating new SP list with " << BondsPerSPCount[SP] << " item(s)";
150 if (SP > 0) {
151 output << ", old level closed with " << BondsPerSPCount[SP-1] << " item(s).";
152 BondsPerSPCount[SP] = 0;
153 } else
154 output << ".";
155 LOG(1, output.str());
156 }
157
158 RemainingWalkers = BondsPerSPCount[SP];
159 for (BondsPerSP::const_iterator CurrentEdge = BondsPerSPList[SP].begin();
160 CurrentEdge != BondsPerSPList[SP].end();
161 ++CurrentEdge) { /// start till end of this SP level's list
162 RemainingWalkers--;
163 Walker = (*CurrentEdge)->rightatom; // rightatom is always the one more distant
164 Predecessor = (*CurrentEdge)->leftatom; // ... and leftatom is predecessor
165 AtomKeyNr = Walker->getNr();
166 LOG(0, "Current Walker is: " << *Walker << " with nr " << Walker->getNr() << " and SP of " << SP << ", with " << RemainingWalkers << " remaining walkers on this level.");
167 // check for new sp level
168 // go through all its bonds
169 LOG(1, "Going through all bonds of Walker.");
170 const BondList& ListOfBonds = Walker->getListOfBonds();
171 for (BondList::const_iterator Runner = ListOfBonds.begin();
172 Runner != ListOfBonds.end();
173 ++Runner) {
174 OtherWalker = (*Runner)->GetOtherAtom(Walker);
175 if ((RestrictedKeySet.find(OtherWalker->getNr()) != RestrictedKeySet.end())
176 // skip hydrogens if desired and restrict to fragment
177 && ((saturation == DontSaturate) || (OtherWalker->getType()->getAtomicNumber() != 1))) {
178 LOG(2, "Current partner is " << *OtherWalker << " with nr " << OtherWalker->getNr() << " in bond " << *(*Runner) << ".");
179 // set the label if not set (and push on root stack as well)
180 if ((OtherWalker != Predecessor) && (OtherWalker->GetTrueFather()->getNr() > RootKeyNr)) { // only pass through those with label bigger than Root's
181 // add the bond in between to the SP list
182 Binder = new bond(Walker, OtherWalker); // create a new bond in such a manner, that bond::rightatom is always the one more distant
183 BondsPerSPList[SP+1].push_back(Binder);
184 BondsPerSPCount[SP+1]++;
185 LOG(3, "Added its bond to SP list, having now " << BondsPerSPCount[SP+1] << " item(s).");
186 } else {
187 if (OtherWalker != Predecessor)
188 LOG(3, "Not passing on, as index of " << *OtherWalker << " " << OtherWalker->GetTrueFather()->getNr() << " is smaller than that of Root " << RootKeyNr << ".");
189 else
190 LOG(3, "This is my predecessor " << *Predecessor << ".");
191 }
192 } else LOG(2, "Is not in the restricted keyset or skipping hydrogen " << *OtherWalker << ".");
193 }
194 }
195 }
196};
197
198/** prints the Bonds per Shortest Path list in BondsPerShortestPath.
199 */
200void BondsPerShortestPath::OutputSPList()
201{
202 LOG(0, "Printing all found lists.");
203 for(int i=1;i<Order;i++) { // skip the root edge in the printing
204 LOG(1, "Current SP level is " << i << ".");
205 for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
206 Binder != BondsPerSPList[i].end();
207 ++Binder) {
208 LOG(2, *Binder);
209 }
210 }
211};
212
213/** Simply counts all bonds in all BondsPerShortestPath::BondsPerSPList lists.
214 */
215int BondsPerShortestPath::CountNumbersInBondsList()
216{
217 int SP = -1; // the Root <-> Root edge must be subtracted!
218 for(int i=Order;i--;) { // sum up all found edges
219 for (BondsPerShortestPath::BondsPerSP::const_iterator Binder = BondsPerSPList[i].begin();
220 Binder != BondsPerSPList[i].end();
221 ++Binder) {
222 SP++;
223 }
224 }
225 return SP;
226};
227
228/** Getter for BondsPerShortestPath::Order.
229 *
230 * @return returns BondsPerShortestPath::Order
231 */
232int BondsPerShortestPath::getOrder() const
233{
234 return Order;
235}
Note: See TracBrowser for help on using the repository browser.