source: src/Fragmentation/Summation/OrthogonalSummator.hpp@ 346b0c

FitPartialCharges_GlobalError PartialCharges_OrthogonalSummation
Last change on this file since 346b0c was dbd841, checked in by Frederik Heber <heber@…>, 8 years ago

FIX: Fixed some missing includes in ..(Orthogonal)Summator.

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * OrthogonalSummator.hpp
3 *
4 * Created on: 28.07.2012
5 * Author: heber
6 */
7
8#ifndef ORTHOGONALSUMMATOR_HPP_
9#define ORTHOGONALSUMMATOR_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <boost/fusion/sequence.hpp>
17
18#include <iterator>
19
20#include "CodePatterns/Assert.hpp"
21
22#include "Fragmentation/Summation/OrthogonalSummation.hpp"
23#include "Fragmentation/Summation/SetValue.hpp"
24#include "Fragmentation/Summation/ZeroInstance.hpp"
25
26#ifdef HAVE_JOBMARKET
27#include "JobMarket/JobId.hpp"
28#else
29#include "Jobs/JobMarket/types.hpp"
30#endif
31
32/** OrthogonalSummator is a general class for making us of OrthogonalSummation.
33 *
34 * The idea is that we want to sum up not only one value but a whole bunch of.
35 * However, in general the types of the values will all differ. We would like
36 * to call the summation in a as simple a fashion as possible and at best in
37 * a kind of for_each on each type/value.
38 * For this purpose we require a list of all the types, represented by the
39 * MPQCDataMap_t map that uses the types enumerated in namespace MPQCDataFused,
40 * see the simple example for the associative sequence in boost::fusion.
41 * The MPQCDataMap_t then gives the essential mapping from the type to a specific
42 * key. Via this key, which is a type and hence can be used for template
43 * specification, we may gather all information for launching the
44 * OrthogonalSummation. We only need to convert MPQCData into the above
45 * MPQCDataMap_t instance and may then access the member variables via the above
46 * key and also obtain the type at \b run-time through the key.
47 *
48 * Note that we then need a conversion from the type stored in the MPQCData,
49 * e.g. a vector<double>, to the type in MPQCDataMap_t, e.g. Histogram.
50 *
51 * Boost::fusion is very cool! If we have a namespace with just structs as keys
52 * and the fusion::map from these keys to the true types, then we may do ...
53 * \code
54 * MPQCDataMap_t MPQCDataMap;
55 * using namespace MPQCDataFused;
56 * double MPQCData_energy_total = boost::fusion::at_key<MPQCDataFused::energy_total>(MPQCDataMap);
57 * \endcode
58 * i.e. at_key knows the correct type!
59 *
60 * Note that you may skip values in the summation by setting their index in
61 * _MatrixNrLookup to something outside the range of [0,_data.size()], e.g.
62 * -1.
63 *
64 */
65template <typename MapType, typename MapKey>
66struct OrthogonalSummator {
67 /** We retrieve the type of the MPQCData member variable from the
68 * boost::fusion::map and stored it in this typedef. Note that for
69 * internal types (e.g. MapValue::const_iterator we need to place
70 * \b typename before).
71 */
72 typedef typename boost::fusion::result_of::value_at_key<MapType, MapKey>::type MapValue;
73
74 /** Constructor for class OrthogonalSummator.
75 *
76 * \param _subsetmap map with hierarchy of IndexSet's
77 * \param _data MPQCData converted to MPQCDataMap_t type, associated to job id
78 * \param _container container of IndexSet's such that each set has correct order
79 * to job id and hence to _data.
80 * \param _MatrixNrLookup lookup from job id to ordering in above vectors
81 */
82 OrthogonalSummator(
83 SubsetMap::ptr &_subsetmap,
84 const std::map<JobId_t, MapType> &_data,
85 const IndexSetContainer::Container_t &_container,
86 const std::map< JobId_t, size_t > &_MatrixNrLookup) :
87 indices(getSubsets(_data.size(),_container)),
88 values(createValues(_data, _container, _MatrixNrLookup)),
89 OS(indices, values, _subsetmap)
90 {
91 ASSERT( _data.size() == _MatrixNrLookup.size(),
92 "OrthogonalSummator() - ids and MatrixNrLookup don't have same size.");
93 }
94
95 /** Summation operator.
96 *
97 * Initialises instantiated OrthogonalSummation of the respective type via
98 * \a OrthogonalSummator::data, uses OrthogonalSummation::operator() to sum and returns
99 * the result.
100 *
101 * \param level up to which level to sum up
102 * \return result of OrthogonalSummation for given type from MPQCDataMap_t.
103 */
104 MapValue operator()(const size_t level)
105 {
106 // evaluate
107 const MapValue result = OS(level);
108 return result;
109 }
110
111 /** Setter for the zero instance to use as the base in orthogonal summation.
112 *
113 * \sa OrthogonalSummation::setZeroInstance() for explanations.
114 *
115 * \param _zeroinstance
116 */
117 void setZeroInstance(const MapValue &_zeroinstance)
118 {
119 OS.setZeroInstance(_zeroinstance);
120 }
121
122 /** Getter for the contribution of the SetValue<> to a specific \a index.
123 *
124 * @param index SetValue to this index
125 * @return contribution
126 */
127 MapValue getContributionForIndexSet(const IndexSet::ptr &ptr) const
128 {
129 typedef SetValueMap<MapValue> setvalues_t;
130 const setvalues_t &values = OS.getSetValues();
131 return values.getConstValue(ptr)->getContribution();
132 }
133
134 /** Getter for the value of the SetValue<> to a specific \a index.
135 *
136 * @param index SetValue to this index
137 * @return value
138 */
139 MapValue getValueForIndexSet(const IndexSet::ptr &ptr) const
140 {
141 typedef SetValueMap<MapValue> setvalues_t;
142 const setvalues_t &values = OS.getSetValues();
143 return values.getConstValue(ptr)->getValue();
144 }
145
146private:
147 /** Tiny helper to create the indices from a given IndexSetContainer.
148 *
149 * Basically, we just have to make sure we miss the last one but only
150 * if the \a container has more than one set.
151 *
152 * @param count how many indices of container to use
153 * @param container container with IndexSet
154 * @return all subsets contained in \a container
155 */
156 typename OrthogonalSummation<MapValue>::InputSets_t getSubsets(
157 const size_t count,
158 const IndexSetContainer::Container_t &container)
159 {
160 IndexSetContainer::Container_t::const_iterator iter = container.begin();
161 std::advance(iter, count); // step till after desired element
162 typename OrthogonalSummation<MapValue>::InputSets_t indices(container.begin(), iter);
163 return indices;
164 }
165
166 /** Tiny helper to create the values for the summation in the correct order.
167 *
168 * @param data
169 * @param container
170 * @param MatrixNrLookup
171 * @return
172 */
173 typename OrthogonalSummation<MapValue>::InputValues_t createValues(
174 const std::map<JobId_t, MapType> &data,
175 const IndexSetContainer::Container_t &container,
176 const std::map< JobId_t, size_t > &MatrixNrLookup)
177 {
178 // if the power set of , we don't need to get rid of the "union index set"
179 typename OrthogonalSummation<MapValue>::InputValues_t values(data.size(), ZeroInstance<MapValue>());
180 for (typename std::map<JobId_t, MapType>::const_iterator dataiter = data.begin();
181 dataiter != data.end(); ++dataiter) {
182 const MapType &Data = dataiter->second;
183 const JobId_t &jobid = dataiter->first;
184 const MapValue &value = boost::fusion::at_key<MapKey>(Data);
185 const std::map< JobId_t, size_t >::const_iterator nriter = MatrixNrLookup.find(jobid);
186 ASSERT( nriter != MatrixNrLookup.end(),
187 "OrthogonalSummation<>::createValues() - MatrixNrLookup does not contain id "
188 +toString(jobid)+".");
189 if ((nriter->second >= 0 ) && (nriter->second < data.size()))
190 values[ nriter->second ] = value;
191 }
192 return values;
193 }
194private:
195 //!> created indices for OS such that we may hand over refs
196 typename OrthogonalSummation<MapValue>::InputSets_t indices;
197 //!> created values for OS such that we may hand over refs
198 typename OrthogonalSummation<MapValue>::InputValues_t values;
199 //!> Summation instance to use in operator()(level)
200 OrthogonalSummation<MapValue> OS;
201};
202
203
204#endif /* ORTHOGONALSUMMATOR_HPP_ */
Note: See TracBrowser for help on using the repository browser.