2013-07-17 19:24:07 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 by H. Hafermann, O. Parcollet
|
|
|
|
*
|
|
|
|
* TRIQS is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2014-05-16 19:35:48 +02:00
|
|
|
#pragma once
|
|
|
|
#include "./_field.hpp"
|
2014-05-20 17:45:16 +02:00
|
|
|
|
2014-05-16 19:35:48 +02:00
|
|
|
namespace triqs {
|
|
|
|
namespace params {
|
|
|
|
|
2014-05-21 14:33:07 +02:00
|
|
|
/// For add_field method of parameters.
|
2014-05-16 19:35:48 +02:00
|
|
|
template<typename T> struct no_default {};
|
2014-05-21 14:33:07 +02:00
|
|
|
|
2013-07-17 19:24:07 +02:00
|
|
|
/**
|
2014-05-21 14:33:07 +02:00
|
|
|
* DOC to be written.
|
|
|
|
*
|
|
|
|
* Provide a form for program parameters.
|
2014-05-21 17:47:54 +02:00
|
|
|
*
|
2014-05-21 14:33:07 +02:00
|
|
|
* Fields of the form can be added with add_field method (only in C++), with a name, a type, a doc, and optionally a default value.
|
2014-05-21 17:47:54 +02:00
|
|
|
*
|
2014-05-21 14:33:07 +02:00
|
|
|
* They can be retrieved from and put into the form, using the [] operator, in C++ and python.
|
2014-05-21 17:47:54 +02:00
|
|
|
*
|
|
|
|
* The data is stored with a strict type checking at runtime, with some type collapse (all integers into a long,
|
2014-05-21 14:33:07 +02:00
|
|
|
* all string, const char * into a std::string, etc).
|
2014-05-21 17:47:54 +02:00
|
|
|
*
|
2014-05-21 14:33:07 +02:00
|
|
|
* Basic type cast to basic types are provided.
|
|
|
|
*
|
2013-07-17 19:24:07 +02:00
|
|
|
* The class is boost-serializable and implements hdf5 I/O operations.
|
|
|
|
*/
|
|
|
|
class parameters {
|
2014-05-16 19:35:48 +02:00
|
|
|
struct _data_elem {
|
|
|
|
std::string key;
|
|
|
|
_field f;
|
|
|
|
std::string doc;
|
2014-05-21 17:47:54 +02:00
|
|
|
#ifdef TRIQS_SERIALIZATION_WITH_HDF5_IMPOSSIBLE
|
2014-05-16 19:35:48 +02:00
|
|
|
template <class Archive> void serialize(Archive& ar, const unsigned int version) {
|
|
|
|
ar& TRIQS_MAKE_NVP("key", key) & TRIQS_MAKE_NVP("f", f) & TRIQS_MAKE_NVP("doc", doc);
|
2013-07-17 19:24:07 +02:00
|
|
|
}
|
2014-05-21 17:47:54 +02:00
|
|
|
#endif
|
2014-05-16 19:35:48 +02:00
|
|
|
};
|
|
|
|
using _data_t = std::vector<_data_elem>;
|
|
|
|
_data_t _data;
|
|
|
|
void insert(std::string const& key, _field&& f, std::string const& doc);
|
|
|
|
_data_t::iterator find(std::string const& key);
|
|
|
|
_data_t::const_iterator find(std::string const& key) const;
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-05-16 19:35:48 +02:00
|
|
|
friend class boost::serialization::access;
|
2014-05-21 17:47:54 +02:00
|
|
|
#ifdef TRIQS_SERIALIZATION_WITH_HDF5_IMPOSSIBLE
|
|
|
|
template <class Archive> void serialize(Archive& ar, const unsigned int version) {
|
|
|
|
ar& TRIQS_MAKE_NVP("_data", _data);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// do it at once, with one buffer only.
|
|
|
|
template <class Archive> void save(Archive &ar, const unsigned int version) const {
|
|
|
|
std::string s = h5::serialize(*this);
|
|
|
|
ar << TRIQS_MAKE_NVP("seria_str", s);
|
|
|
|
}
|
|
|
|
template <class Archive> void load(Archive &ar, const unsigned int version) {
|
|
|
|
std::string s;
|
|
|
|
ar >> TRIQS_MAKE_NVP("seria_str", s);
|
|
|
|
*this = h5::deserialize<parameters>(s);
|
|
|
|
}
|
|
|
|
BOOST_SERIALIZATION_SPLIT_MEMBER();
|
|
|
|
#endif
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-05-16 19:35:48 +02:00
|
|
|
public:
|
|
|
|
parameters();
|
|
|
|
|
2014-05-21 14:33:07 +02:00
|
|
|
/// Add a field with a default value
|
2014-05-16 19:35:48 +02:00
|
|
|
template <typename T> parameters& add_field(std::string const& key, T&& x, std::string const& doc) {
|
|
|
|
insert(key, _field{std::forward<T>(x),key, false}, doc);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-21 14:33:07 +02:00
|
|
|
// Add a field without a default value
|
2014-05-16 19:35:48 +02:00
|
|
|
template <typename T> parameters& add_field(std::string const& key, no_default<T>, std::string const& doc) {
|
|
|
|
insert(key, _field{T{}, key, true}, doc);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-21 14:33:07 +02:00
|
|
|
/// Add a subgroup (a field which it itself a parameter).
|
2014-05-16 19:35:48 +02:00
|
|
|
parameters& add_group(std::string const& key, std::string const& doc) {
|
|
|
|
insert(key, _field{parameters{}, key, false}, doc);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-21 14:33:07 +02:00
|
|
|
/// Sort with the key. By default the list of fields is ordered as in the order of their addition to the parameter.
|
2014-05-16 19:35:48 +02:00
|
|
|
void sort_by_key();
|
|
|
|
|
2014-05-21 14:33:07 +02:00
|
|
|
/// Does the form have the key ?
|
|
|
|
bool has_key(std::string const& key) const;
|
2014-05-16 19:35:48 +02:00
|
|
|
|
2014-05-21 17:47:54 +02:00
|
|
|
/**
|
2014-05-21 14:33:07 +02:00
|
|
|
* Access the parameter key.
|
|
|
|
* Key must be a valid key or a TRIQS_RUNTIME_ERROR is thrown.
|
|
|
|
*/
|
2014-05-16 19:35:48 +02:00
|
|
|
_field& operator[](const char * key);
|
2014-05-21 14:33:07 +02:00
|
|
|
|
2014-05-21 17:47:54 +02:00
|
|
|
/**
|
2014-05-21 14:33:07 +02:00
|
|
|
* Access the parameter key.
|
|
|
|
* Key must be a valid key or a TRIQS_RUNTIME_ERROR is thrown.
|
|
|
|
*/
|
2014-05-16 19:35:48 +02:00
|
|
|
_field const& operator[](const char * key) const;
|
|
|
|
|
|
|
|
/// generate help in form of a table of strings containing a list of required and optional parameters
|
2014-05-21 14:33:07 +02:00
|
|
|
std::vector<std::vector<std::string>> generate_help(bool with_header = true) const;
|
2014-05-16 19:35:48 +02:00
|
|
|
|
2014-05-21 17:47:54 +02:00
|
|
|
/// help as a string
|
2014-05-21 14:33:07 +02:00
|
|
|
std::string help() const;
|
|
|
|
|
|
|
|
/// hdf5
|
|
|
|
friend std::string get_triqs_hdf5_data_scheme(parameters const &) { return ""; }
|
2014-05-16 19:35:48 +02:00
|
|
|
friend void h5_write(h5::group F, std::string const& subgroup_name, parameters const& p);
|
|
|
|
friend void h5_read(h5::group F, std::string const& subgroup_name, parameters& p);
|
2014-05-21 17:47:54 +02:00
|
|
|
|
2014-05-21 14:33:07 +02:00
|
|
|
/// Ostream
|
2014-05-16 19:35:48 +02:00
|
|
|
friend std::ostream& operator<<(std::ostream& out, parameters const& p);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update with another parameter set.
|
|
|
|
* If a key is present in other and not in this, add parameter to this.
|
2014-05-21 14:33:07 +02:00
|
|
|
* If a key is present in both, overwrite parameter with a strict type checking.
|
2014-05-16 19:35:48 +02:00
|
|
|
*/
|
|
|
|
void update(parameters const&);
|
|
|
|
};
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-05-21 14:33:07 +02:00
|
|
|
/// Another form of update, more pythonic
|
2014-05-16 19:35:48 +02:00
|
|
|
inline parameters operator+(parameters p1, parameters const& p2) {
|
|
|
|
p1.update(p2);
|
|
|
|
return p1;
|
|
|
|
}
|
2014-05-21 14:33:07 +02:00
|
|
|
|
2014-05-16 19:35:48 +02:00
|
|
|
// can only be implemented after complete declaration of parameters
|
|
|
|
template <typename... T> _field& _field::add_field(T&&... x) {
|
|
|
|
auto* pp = dynamic_cast<_data_impl<parameters>*>(p.get());
|
|
|
|
if (!pp) TRIQS_RUNTIME_ERROR << "Can only use add_field on a subgroup, which " << name() << " is not !";
|
|
|
|
pp->x.add_field(std::forward<T>(x)...);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 14:33:07 +02:00
|
|
|
|
|
|
|
#ifdef TRIQS_WITH_PYTHON_SUPPORT
|
|
|
|
// include the generated python converter
|
2014-05-20 17:45:16 +02:00
|
|
|
#include <pytriqs/converters/parameters.hpp>
|
2014-05-21 14:33:07 +02:00
|
|
|
#endif
|
|
|
|
|