ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
results_collector.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * ALPS MPS DMRG Project
4  *
5  * Copyright (C) 2013 Institute for Theoretical Physics, ETH Zurich
6  * 2011-2011 by Bela Bauer <bauerb@phys.ethz.ch>
7  *
8  * This software is part of the ALPS Applications, published under the ALPS
9  * Application License; you can use, redistribute it and/or modify it under
10  * the terms of the license, either version 1 or (at your option) any later
11  * version.
12  *
13  * You should have received a copy of the ALPS Application License along with
14  * the ALPS Applications; see the file LICENSE.txt. If not, the license is also
15  * available from http://alps.comp-phys.org/.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  *****************************************************************************/
26 
27 #ifndef UTILS_RESULTS_COLLECTOR_H
28 #define UTILS_RESULTS_COLLECTOR_H
29 
30 #include <map>
31 #include <boost/any.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <utility>
34 
35 #include "dmrg/utils/storage.h"
36 
37 namespace detail {
39  {
40  public:
41  virtual ~collector_impl_base() {}
42  virtual void collect(boost::any const &) = 0;
43  virtual void save(alps::hdf5::archive & ar) const = 0;
44  // TODO: fixed storage type because templated virtual function are not allowed
45  };
46 
47  template<class T>
49  {
50  public:
51  void collect(boost::any const & val)
52  {
53  vals.push_back(boost::any_cast<T>(val));
54  }
55 
56  void save(alps::hdf5::archive & ar) const
57  {
58  std::vector<T> allvalues;
59  if (ar.is_data("mean/value"))
60  ar["mean/value"] >> allvalues;
61  std::copy(vals.begin(), vals.end(), std::back_inserter(allvalues));
62  ar["mean/value"] << allvalues;
63  }
64 
65  private:
66  std::vector<T> vals;
67  };
68 }
69 
71  typedef boost::shared_ptr<detail::collector_impl_base> coll_type;
72 public:
73  collector_proxy(coll_type & collector_)
74  : collector(collector_)
75  { }
76 
77  template<class T>
78  void operator<<(T const& val)
79  {
80  if (!collector)
81  collector.reset(new detail::collector_impl<T>());
82  collector->collect(val);
83  }
84 
85 private:
86  coll_type & collector;
87 };
88 
90 {
91 public:
92 
93  void clear()
94  {
95  collection.clear();
96  }
97 
98  collector_proxy operator[] (std::string name)
99  {
100  return collector_proxy(collection[name]);
101  }
102 
103  template <class Archive>
104  void save(Archive & ar) const
105  {
106  for (std::map<std::string, boost::shared_ptr< ::detail::collector_impl_base> >::const_iterator
107  it = collection.begin(); it != collection.end(); ++it)
108  {
109  ar[it->first] << *it->second;
110  }
111  }
112 
113 private:
114  std::map<std::string, boost::shared_ptr< ::detail::collector_impl_base> > collection;
115 };
116 
117 #endif
virtual void collect(boost::any const &)=0
void collect(boost::any const &val)
collector_proxy operator[](std::string name)
void operator<<(T const &val)
collector_proxy(coll_type &collector_)
void save(Archive &ar) const
virtual void save(alps::hdf5::archive &ar) const =0
void save(alps::hdf5::archive &ar) const