source: src/Fragmentation/Summation/SubsetMap.cpp@ 2df580

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

Added SubsetMap which is a container to a lookup map from IndexSet to all contained sets.

  • also added unit test.
  • added shared_ptr typedef ptr.
  • Property mode set to 100644
File size: 7.7 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 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 * SubsetMap.cpp
25 *
26 * Created on: Jul 3, 2012
27 * Author: heber
28 */
29
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "CodePatterns/MemDebug.hpp"
37
38#include "SubsetMap.hpp"
39
40#include <boost/bind.hpp>
41#include <boost/foreach.hpp>
42#include <algorithm>
43#include <bitset>
44#include <set>
45
46#include "CodePatterns/Log.hpp"
47
48SubsetMap::SubsetMap(IndexSetContainer &_container) :
49 tempset(new IndexSet())
50{
51 const IndexSetContainer::Container_t &container = _container.getContainer();
52
53// we don't need this level-wise sorting as this is automatically achieved by specific sorting
54// /// place in level-wise multimap
55// typedef size_t level_t;
56// // note: We use an IndexSetContainer as it is not accessed while filling it
57// // also, as insert uses push_back, the IndexSetContainer is actually created
58// // as sorted. Hence, the call to sort() just needs linear time (and does
59// // nothing)
60// typedef std::map<level_t, IndexSetContainer > IndexSetPerLevel_t;
61// IndexSetPerLevel_t IndexSetPerLevel;
62// for(IndexSetContainer::Container_t::const_iterator iter = container.begin();
63// iter != container.end(); ++iter) {
64// // key present in map?
65// const size_t level = (*iter)->size();
66// LOG(1, "INFO: Current set is " << **iter << " with size " << level << ".");
67// IndexSetPerLevel_t::iterator leveliter = IndexSetPerLevel.find(level);
68// // we have to explicitly copy the shared_ptr for insertion due to const container
69// IndexSet_ptr ptr(*iter);
70// if (leveliter == IndexSetPerLevel.end()) {
71// LOG(2, "DEBUG: Level not present in Lookup, creating new container");
72// IndexSetPerLevel.insert( std::make_pair( level, IndexSetContainer(ptr)) );
73// } else {
74// LOG(2, "DEBUG: Level present in Lookup, appending to container");
75// leveliter->second.insert(ptr);
76// }
77// }
78
79 /// go through all IndexSets
80 std::for_each( container.begin(), container.end(),
81 boost::bind(&SubsetMap::gatherSubsets, this, _1));
82}
83
84void SubsetMap::gatherSubsets(const IndexSet_ptr &ptr) {
85 // get the sets size and the size of the power sets
86 const size_t SetSize = ptr->size();
87 LOG(1, "INFO: Current set to gather all subsets for is " << *ptr << " with size " << SetSize << ".");
88 const size_t NoPowerSubsets = getNoPowerSets(SetSize);
89 LOG(1, "INFO: There are " << NoPowerSubsets << " sets to check for containment.");
90 std::vector<IndexSet> returnsets;
91 // sets is the number of possible subsets to check: NotContained means still
92 // needs check or is not contained; Contained means is contained, or has
93 // already been treated via a super(sub)set.
94 typedef std::vector<enum ContainedState> StateOfSubsets;
95 StateOfSubsets sets( NoPowerSubsets, NotContained);
96 // we have to traverse backwards, i.e. from greatest subsets to smallest to
97 // make the best possible use of the subset knowledge we already have for these
98 // subsets of the subset.
99 // WARNING: last set is the set itself which must not be in IndexSetContainer due to
100 // cyclic structure which shared_ptr cannot resolve.
101 StateOfSubsets::reverse_iterator iter = sets.rbegin()+1;
102 for (;iter != sets.rend(); iter = std::find(iter+1, sets.rend(), NotContained)) {
103 // get IndexSet (get index via distance)
104 // NOTE: reverse iterator naturally give reverse distance, hence flip both arguments
105 // also, reverse iterator is off by one, see ASSERT
106 const size_t index = std::distance(iter, sets.rend())-1;
107 ASSERT( sets.at(index) == *iter,
108 "SubsetMap::gatherSubsets() - distance from begin to iterator is not correct.");
109 *tempset = getSubset(*ptr, index );
110 LOG(2, "INFO: Current subset is " << *tempset << " at " << index << ".");
111 // check whether IndexSet is contained and known
112 if (ptr->contains(*tempset)) {
113 // mark as contained
114 *iter = Contained;
115 LOG(3, "DEBUG: Marking subset as Contained.");
116 if (Lookup.count(tempset)) {
117 LOG(2, "DEBUG: Subset is present in Lookup, adding.");
118 returnsets.push_back(*tempset);
119 // if so, also add its contained sets and mark them out
120 const IndexSetContainer::Container_t &container = getSubsets(tempset)->getContainer();
121 if (container.size()) {
122 LOG(2, "DEBUG: Subset is also present in Lookup, adding all its subsets");
123 for(IndexSetContainer::Container_t::const_iterator containeriter = container.begin();
124 containeriter != container.end();
125 ++containeriter) {
126 const size_t subindex = getPowerSetIndex(ptr, **containeriter);
127 LOG(4, "INFO: Current subset of subset is " << **containeriter << " at " << subindex << ".");
128 if (sets[subindex] != Contained) {
129 LOG(5, "DEBUG: Setting power subset to Contained.");
130 sets[subindex] = Contained;
131 returnsets.push_back(**containeriter);
132 }
133 }
134 }
135 }
136 }
137 }
138 LOG(1, "INFO: Adding " << returnsets.size() << " sets to Lookup for set " << *ptr << ".");
139 Lookup.insert( std::make_pair(ptr, IndexSetContainer::ptr(new IndexSetContainer(returnsets))) );
140}
141
142const size_t SubsetMap::getPowerSetIndex(const IndexSet_ptr &ptr, const IndexSet &subset) {
143 ASSERT( (ptr->size() < MAX_SETSIZE) && (subset.size() < MAX_SETSIZE),
144 "SubsetMap::getPowerSetIndex() - power sets of subsets must not be bigger than "
145 +toString((int)MAX_SETSIZE)+".");
146 ASSERT( ptr->contains(subset),
147 "SubsetMap::getPowerSetIndex() - "+(toString(*ptr))+" does not contain "
148 +toString(subset)+".");
149 std::bitset<MAX_SETSIZE> bits(0);
150 // go through each and search for indices present in both
151 IndexSet::iterator indexiter = ptr->begin();
152 IndexSet::iterator subindexiter = subset.begin();
153 for (; subindexiter != subset.end();) {
154 if (*indexiter == *subindexiter) {
155 // if matching, set bit and advance both iterators
156 bits.set( (size_t)std::distance(ptr->begin(), indexiter) );
157 ++indexiter;
158 ++subindexiter;
159 // if not, advance the iterator lacking behind
160 } else if ( *indexiter < *subindexiter) {
161 ++indexiter;
162 } else {
163 ++subindexiter;
164 }
165 }
166 return bits.to_ulong();
167}
168
169IndexSet SubsetMap::getSubset(const IndexSet &_set, size_t PowerSetNo) {
170 ASSERT( _set.size() < MAX_SETSIZE,
171 "SubsetMap::getSubset() - power sets of subsets must not be bigger than "
172 +toString((int)MAX_SETSIZE)+".");
173 ASSERT((PowerSetNo >= 0) && (PowerSetNo < getNoPowerSets(_set.size())),
174 "SubsetMap::getSubset() - Power set index "+toString(PowerSetNo)+" is out of range.");
175 std::bitset<MAX_SETSIZE> bits(PowerSetNo);
176 // place index into container with random access
177 std::vector<IndexSet::value_type> indices(_set.begin(), _set.end());
178 IndexSet indexset;
179 // go through each bit and insert if 1, not if 0
180 for (size_t index=0; index < indices.size(); ++index)
181 if (bits[index])
182 indexset.insert(indices[index]);
183 return indexset;
184}
Note: See TracBrowser for help on using the repository browser.