3
0
mirror of https://github.com/triqs/dft_tools synced 2025-01-12 22:18:23 +01:00
dft_tools/triqs/parameters/parameters.cpp
Olivier Parcollet af084f5d59 parameters & wrapper
- new parameter class :
  parameters are viewed as form, built in C++, and filled in C++/python.
  Each field of the form as a precise C++ type (erased using standard techniques).
  First tests ok, to be reread/checked.

  TODO : serialization is commented. Lead to long compilation time & large code
  due to boost::serialization. Use h5 when possible.

- wrapper :
  - separated the converters of the wrapped type in the TRIQS library
  - necessary for parameters (it used outside an .so) and potentially
    other codes, outside an .so module
2014-05-19 15:19:18 +02:00

155 lines
5.6 KiB
C++

#include <triqs/utility/first_include.hpp>
#include <triqs/utility/formatted_output.hpp>
#include <boost/serialization/string.hpp>
#include "./parameters.hpp"
#include <triqs/utility/serialization.hpp>
#include <algorithm>
#include <boost/algorithm/string.hpp>
namespace triqs {
namespace params {
parameters::parameters() {
using triqs::arrays::array;
using dcomplex = std::complex<double>;
if (_field::type_names.size() == 0) {
#define R(...) _field::type_names.insert({typeid(__VA_ARGS__), get_triqs_hdf5_data_scheme(storage_t<__VA_ARGS__>{})});
#define A(N) \
R(array<int, N>); \
R(array<long, N>); \
R(array<double, N>); \
R(array<dcomplex, N>);
R(long);
R(double);
R(int);
R(dcomplex);
A(1);
A(2);
A(3);
A(4);
}
}
parameters::_data_t::iterator parameters::find(std::string const& key) {
return std::find_if(_data.begin(), _data.end(), [&key](_data_elem const& x) { return x.key == key; });
}
parameters::_data_t::const_iterator parameters::find(std::string const& key) const {
return std::find_if(_data.begin(), _data.end(), [&key](_data_elem const& x) { return x.key == key; });
}
bool parameters::has_key(std::string const& k) const { return find(k) != _data.end();}
void parameters::sort_by_key() {
std::sort(_data.begin(), _data.end(), [](_data_elem const& x, _data_elem const& y) { return x.key < y.key; });
}
void parameters::insert(std::string const& key, _field&& f, std::string const& doc) {
if (has_key(key)) TRIQS_RUNTIME_ERROR << "Field " << key << " already defined";
_data.push_back({key, std::move(f),doc});
}
_field& parameters::operator[](const char * key) {
auto it = find(key);
if (it == _data.end()) TRIQS_RUNTIME_ERROR << "Parameters : the key : " << key << " does not exists";
return it->f;
}
_field const& parameters::operator[](const char * key) const {
auto it = find(key);
if (it == _data.end()) TRIQS_RUNTIME_ERROR << "Parameters : the key : " << key << " does not exists";
return it->f;
}
void h5_write(h5::group F, std::string const& subgroup_name, parameters const& p) {
auto gr = F.create_group(subgroup_name);
for (auto& pvp : p._data) h5_write(gr, pvp.key, pvp.f);
}
void h5_read(h5::group F, std::string const& subgroup_name, parameters& p) {
auto gr = F.open_group(subgroup_name);
for (auto& pvp : p._data) h5_read(gr, pvp.key, pvp.f);
}
//-----------------------------------------------------------------------
void parameters::update(parameters const& other) {
for (auto const& kfd : other._data) {
auto it = find(kfd.key);
if (it !=_data.end()) {
if (it->f.index != kfd.f.index) TRIQS_RUNTIME_ERROR << "Index mismatch in merging parameters";
if (kfd.doc.size()>0) it->doc = kfd.doc;
} else
_data.push_back(kfd);
}
}
//-----------------------------------------------------------------------
std::vector<std::vector<std::string>> parameters::generate_help() const {
std::vector<std::vector<std::string>> str;
str.push_back({"parameter:", "type:", "value:", "description:"});
for (auto const& s : _data)
if (!s.f.is_modified()) str.push_back({s.key, s.f.type_name(), "-", s.doc});
for (auto const& s : _data) {
if (is_parameter(s.f)) continue; // no subgroup
if (s.f.is_modified()) {
std::ostringstream val;
val << s.f;
auto sv = val.str();
boost::algorithm::trim(sv);
int size_max = 30;
if (sv.size()>size_max) {
auto s2 = std::string(size_max+5,'.');
std::copy(sv.begin(), sv.begin()+size_max/2, s2.begin());
std::copy(sv.end()-size_max/2, sv.end(), s2.end()-size_max/2);
sv = s2;
}
std::replace( sv.begin(), sv.end(), '\n', ','); // replace all 'x' to 'y'
str.push_back({s.key, s.f.type_name(), sv, s.doc});
}
}
// all sub groups after
for (auto const& s : _data) {
if (!is_parameter(s.f)) continue;
auto p = dynamic_cast<_field::_data_impl<parameters>*>(s.f.p.get());
auto str2 = p->x.generate_help();
for (auto & x : str2) x[0] = " " + x[0];
str2.insert(str2.begin(), {"Sub-group : ", s.key, " ", ""});
str.insert(str.end(), str2.begin(), str2.end());
}
return str;
}
std::ostream& operator<<(std::ostream& out, parameters const& p) {
out << utility::print_formatted(p.generate_help());
return out;
}
//-----------------------------------------------------------------------
_field& _field::operator[](const char* key) {
auto* pp = dynamic_cast<_data_impl<parameters>*>(p.get());
if (!pp) TRIQS_RUNTIME_ERROR << "Can only use [] on a subgroup, which " << name() << " is not !";
return pp->x.operator[](key);
}
_field const& _field::operator[](const char* key) const {
auto* pp = dynamic_cast<_data_impl<parameters>*>(p.get());
if (!pp) TRIQS_RUNTIME_ERROR << "Can only use [] on a subgroup, which " << name() << " is not !";
return pp->x.operator[](key);
}
bool is_parameter(_field const& f) { return dynamic_cast<_field::_data_impl<parameters>*>(f.p.get()); }
_field& _field::add_group(std::string const& key, std::string const& doc) {
auto* pp = dynamic_cast<_data_impl<parameters>*>(p.get());
if (!pp) TRIQS_RUNTIME_ERROR << "Can only use [] on a subgroup, which " << name() << " is not !";
pp->x.add_group(key, doc);
return *this;
}
}
}