ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseParameters.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 #if !defined(BASEPARAMETERS_H) && !defined(DMRGPARAMETERS_H)
28 #define BASEPARAMETERS_H
29 
30 #include <string>
31 #include <fstream>
32 #include <iostream>
33 
34 #include <boost/tokenizer.hpp>
35 #include <boost/lexical_cast.hpp>
36 #include <boost/regex.hpp>
37 #include <boost/algorithm/string.hpp>
38 
39 #include <alps/parameter.h>
40 #include "utils/io.hpp"
41 
43 
44 
45 namespace parameters {
46  class value {
47  public:
48  value () : val_(""), empty_(true) { }
49 
50  template <class T>
51  value (const T & val)
52  : val_(boost::lexical_cast<std::string>(val))
53  , empty_(false)
54  { }
55 
56  std::string get() const {return val_;}
57  bool empty() const {return empty_;}
58  private:
59  std::string val_;
60  bool empty_;
61 
62  };
63 }
64 
65 class BaseParameters : public alps::Parameters
66 {
67 public:
68 
70  : alps::Parameters()
71  { }
72 
73  BaseParameters (std::istream& param_file)
74  : alps::Parameters()
75  {
76  try {
77  alps::Parameters temp(param_file);
78  *static_cast<alps::Parameters*>(this) = temp;
79  } catch (std::exception & e) {
80  maquis::cerr << "Exception thrown when parsing parameters:" << std::endl;
81  maquis::cerr << e.what() << std::endl;
82  exit(1);
83  }
84  }
85 
86  BaseParameters(alps::Parameters const& p)
87  : alps::Parameters(p)
88  { }
89 
90  bool is_set (std::string const & key) const
91  {
92  return defined(key);
93  }
94 
95  parameters::proxy operator[](std::string const& key)
96  {
97  if (!defined(key)) {
98  std::map<std::string, std::string>::const_iterator match = defaults.find(key);
99  if (match != defaults.end())
100  alps::Parameters::operator[](key) = match->second;
101  else
102  boost::throw_exception(std::runtime_error("parameter " + key + " not defined"));
103  }
104  return parameters::proxy(alps::Parameters::operator[](key));
105  }
106 
107  template<class T> T get(std::string const & key)
108  {
109  parameters::proxy const& p = (*this)[key];
110  return p.as<T>();
111  }
112 
113  template<class T> void set(std::string const & key, T const & value)
114  {
115  alps::Parameters::operator[](key) = boost::lexical_cast<std::string>(value);
116  }
117 
118  BaseParameters iteration_params(std::string const & var, std::size_t val)
119  {
120  BaseParameters p;
121 
122  boost::regex expression("^(.*)\\[" + var + "\\]$");
123  boost::smatch what;
124  for (alps::Parameters::const_iterator it=this->begin();it != this->end();++it) {
125  std::string key = it->key();
126  if (boost::regex_match(key, what, expression)) {
127  std::vector<value_type> v = (*this)[key];
128  if (val < v.size())
129  p.set(what.str(1), v[val]);
130  else
131  p.set(what.str(1), *(v.rbegin()));
132  }
133  }
134  p.set(var, val);
135  return p;
136  }
137 
139  {
140  for (alps::Parameters::const_iterator it=p.begin(); it!=p.end(); ++it)
141  alps::Parameters::operator[](it->key()) = it->value();
142  defaults.insert(p.defaults.begin(), p.defaults.end());
143 
144  return *this;
145  }
146 
147 protected:
148  void add_option(std::string const & name,
149  std::string const & desc,
150  parameters::value const & val = parameters::value())
151  {
152  if (!val.empty())
153  defaults[name] = val.get();
154  descriptions[name] = desc;
155  }
156 
157  std::map<std::string, std::string> defaults;
158  std::map<std::string, std::string> descriptions;
159 
160 };
161 
162 #endif
parameters::proxy operator[](std::string const &key)
bool is_set(std::string const &key) const
void set(std::string const &key, T const &value)
BaseParameters & operator<<(BaseParameters const &p)
std::map< std::string, std::string > descriptions
BaseParameters iteration_params(std::string const &var, std::size_t val)
std::map< std::string, std::string > defaults
BaseParameters(std::istream &param_file)
void add_option(std::string const &name, std::string const &desc, parameters::value const &val=parameters::value())
bool empty() const
BaseParameters(alps::Parameters const &p)
value(const T &val)