diff --git a/c++/g2_parameters.hpp b/c++/g2_parameters.hpp index 82216ea..d911df2 100644 --- a/c++/g2_parameters.hpp +++ b/c++/g2_parameters.hpp @@ -29,8 +29,8 @@ namespace pomerol2triqs { /// indices of operators in TRIQS convention: (block_name, inner_index) indices_t index1, index2, index3, index4; - g2_iw_inu_inup_params_t() {} - g2_iw_inu_inup_params_t(gf_struct_t const &gf_struct, double beta) : gf_struct(gf_struct), beta(beta) {} + // g2_iw_inu_inup_params_t() {} + // g2_iw_inu_inup_params_t(gf_struct_t const &gf_struct, double beta) : gf_struct(gf_struct), beta(beta) {} }; struct g2_three_freqs_params_t { @@ -50,8 +50,8 @@ namespace pomerol2triqs { /// indices of operators in TRIQS convention: (block_name, inner_index) indices_t index1, index2, index3, index4; - g2_three_freqs_params_t() {} - g2_three_freqs_params_t(gf_struct_t const &gf_struct, double beta) : gf_struct(gf_struct), beta(beta) {} + // g2_three_freqs_params_t() {} + // g2_three_freqs_params_t(gf_struct_t const &gf_struct, double beta) : gf_struct(gf_struct), beta(beta) {} }; /* diff --git a/c++/pomerol_ed.hpp b/c++/pomerol_ed.hpp index cd58066..4213915 100644 --- a/c++/pomerol_ed.hpp +++ b/c++/pomerol_ed.hpp @@ -119,10 +119,11 @@ namespace pomerol2triqs { /// Retarded Green's function on real energy axis block_gf G_w(gf_struct_t const &gf_struct, double beta, std::pair const &energy_window, int n_w, double im_shift = 0); - /// Two-particle Green's function, Matsubara frequencies + /// Two-particle Green's function. Specify frequency cutoff, n_b and n_f. TRIQS_WRAP_ARG_AS_DICT g2_t G2_iw(g2_iw_inu_inup_params_t const &p); + /// Two-particle Green's function. Specify three frequencies (wb, wf1, wf2). TRIQS_WRAP_ARG_AS_DICT g2_three_freqs_t G2_iw_three_freqs(g2_three_freqs_params_t const &p); diff --git a/python/pomerol2triqs_converters.hxx b/python/pomerol2triqs_converters.hxx index c8eae7d..b20165f 100644 --- a/python/pomerol2triqs_converters.hxx +++ b/python/pomerol2triqs_converters.hxx @@ -111,4 +111,112 @@ template <> struct py_converter { } }; +}} + + +// --- C++ Python converter for g2_three_freqs_params_t +#include +#include +#include + +namespace triqs { namespace py_tools { + +template <> struct py_converter { + static PyObject *c2py(g2_three_freqs_params_t const & x) { + PyObject * d = PyDict_New(); + PyDict_SetItemString( d, "gf_struct" , convert_to_python(x.gf_struct)); + PyDict_SetItemString( d, "beta" , convert_to_python(x.beta)); + PyDict_SetItemString( d, "channel" , convert_to_python(x.channel)); + PyDict_SetItemString( d, "three_freqs", convert_to_python(x.three_freqs)); + PyDict_SetItemString( d, "index1" , convert_to_python(x.index1)); + PyDict_SetItemString( d, "index2" , convert_to_python(x.index2)); + PyDict_SetItemString( d, "index3" , convert_to_python(x.index3)); + PyDict_SetItemString( d, "index4" , convert_to_python(x.index4)); + return d; + } + + template static void _get_optional(PyObject *dic, const char *name, T &r, U const &init_default) { + if (PyDict_Contains(dic, pyref::string(name))) + r = convert_from_python(PyDict_GetItemString(dic, name)); + else + r = init_default; + } + + template static void _get_optional(PyObject *dic, const char *name, T &r) { + if (PyDict_Contains(dic, pyref::string(name))) + r = convert_from_python(PyDict_GetItemString(dic, name)); + else + r = T{}; + } + + static g2_three_freqs_params_t py2c(PyObject *dic) { + g2_three_freqs_params_t res; + res.gf_struct = convert_from_python(PyDict_GetItemString(dic, "gf_struct")); + res.beta = convert_from_python(PyDict_GetItemString(dic, "beta")); + _get_optional(dic, "channel" , res.channel ,PH); + res.three_freqs = convert_from_python(PyDict_GetItemString(dic, "three_freqs")); + res.index1 = convert_from_python(PyDict_GetItemString(dic, "index1")); + res.index2 = convert_from_python(PyDict_GetItemString(dic, "index2")); + res.index3 = convert_from_python(PyDict_GetItemString(dic, "index3")); + res.index4 = convert_from_python(PyDict_GetItemString(dic, "index4")); + return res; + } + + template + static void _check(PyObject *dic, std::stringstream &fs, int &err, const char *name, const char *tname) { + if (!convertible_from_python(PyDict_GetItemString(dic, name), false)) + fs << "\n" << ++err << " The parameter " << name << " does not have the right type : expecting " << tname + << " in C++, but got '" << PyDict_GetItemString(dic, name)->ob_type->tp_name << "' in Python."; + } + + template + static void _check_mandatory(PyObject *dic, std::stringstream &fs, int &err, const char *name, const char *tname) { + if (!PyDict_Contains(dic, pyref::string(name))) + fs << "\n" << ++err << " Mandatory parameter " << name << " is missing."; + else _check(dic,fs,err,name,tname); + } + + template + static void _check_optional(PyObject *dic, std::stringstream &fs, int &err, const char *name, const char *tname) { + if (PyDict_Contains(dic, pyref::string(name))) _check(dic, fs, err, name, tname); + } + + static bool is_convertible(PyObject *dic, bool raise_exception) { + if (dic == nullptr or !PyDict_Check(dic)) { + if (raise_exception) { PyErr_SetString(PyExc_TypeError, "The function must be called with named arguments");} + return false; + } + std::stringstream fs, fs2; int err=0; + +#ifndef TRIQS_ALLOW_UNUSED_PARAMETERS + std::vector ks, all_keys = {"gf_struct","beta","channel","three_freqs","index1","index2","index3","index4"}; + pyref keys = PyDict_Keys(dic); + if (!convertible_from_python>(keys, true)) { + fs << "\nThe dict keys are not strings"; + goto _error; + } + ks = convert_from_python>(keys); + for (auto & k : ks) + if (std::find(all_keys.begin(), all_keys.end(), k) == all_keys.end()) + fs << "\n"<< ++err << " The parameter '" << k << "' is not recognized."; +#endif + + _check_mandatory(dic, fs, err, "gf_struct" , "gf_struct_t"); + _check_mandatory(dic, fs, err, "beta" , "double"); + _check_optional (dic, fs, err, "channel" , "pomerol2triqs::channel_t"); + _check_mandatory(dic, fs, err, "three_freqs", "three_freqs_t"); + _check_mandatory(dic, fs, err, "index1" , "indices_t"); + _check_mandatory(dic, fs, err, "index2" , "indices_t"); + _check_mandatory(dic, fs, err, "index3" , "indices_t"); + _check_mandatory(dic, fs, err, "index4" , "indices_t"); + if (err) goto _error; + return true; + + _error: + fs2 << "\n---- There " << (err > 1 ? "are " : "is ") << err<< " error"<<(err >1 ?"s" : "")<< " in Python -> C++ transcription for the class g2_three_freqs_params_t\n" <, 3> G2_iw (**pomerol2triqs::g2_iw_inu_inup_params_t)""", - doc = r"""Two-particle Green's function, Matsubara frequencies""") + doc = r"""Two-particle Green's function. Specify frequency cutoff, n_b and n_f.""") + +c.add_method("""std::vector > G2_iw_three_freqs (**pomerol2triqs::g2_three_freqs_params_t)""", + doc = r"""Two-particle Green's function. Specify three frequencies (wb, wf1, wf2).""") module.add_class(c) diff --git a/python/pomerol2triqs_parameters.rst b/python/pomerol2triqs_parameters.rst index be8cd2e..6dddaaf 100644 --- a/python/pomerol2triqs_parameters.rst +++ b/python/pomerol2triqs_parameters.rst @@ -7,9 +7,31 @@ +----------------+--------------------------+---------+----------------------------------------------------------------------+ | channel | pomerol2triqs::channel_t | PH | Channel in which Matsubara frequency representation is defined. | +----------------+--------------------------+---------+----------------------------------------------------------------------+ -| n_b | int | -- | Number of bosonic Matsubara frequencies. | +| n_b | int | -- | Number of bosonic and fermionic Matsubara frequencies. | +----------------+--------------------------+---------+----------------------------------------------------------------------+ -| n_f | int | -- | Number of fermionic Matsubara frequencies. | +| n_f | int | -- | Number of bosonic and fermionic Matsubara frequencies. | ++----------------+--------------------------+---------+----------------------------------------------------------------------+ +| index1 | indices_t | -- | indices of operators in TRIQS convention: (block_name, inner_index) | ++----------------+--------------------------+---------+----------------------------------------------------------------------+ +| index2 | indices_t | -- | indices of operators in TRIQS convention: (block_name, inner_index) | ++----------------+--------------------------+---------+----------------------------------------------------------------------+ +| index3 | indices_t | -- | indices of operators in TRIQS convention: (block_name, inner_index) | ++----------------+--------------------------+---------+----------------------------------------------------------------------+ +| index4 | indices_t | -- | indices of operators in TRIQS convention: (block_name, inner_index) | ++----------------+--------------------------+---------+----------------------------------------------------------------------+ + + + ++----------------+--------------------------+---------+----------------------------------------------------------------------+ +| Parameter Name | Type | Default | Documentation | ++================+==========================+=========+======================================================================+ +| gf_struct | gf_struct_t | -- | Block structure of GF | ++----------------+--------------------------+---------+----------------------------------------------------------------------+ +| beta | double | -- | Inverse temperature | ++----------------+--------------------------+---------+----------------------------------------------------------------------+ +| channel | pomerol2triqs::channel_t | PH | Channel in which Matsubara frequency representation is defined. | ++----------------+--------------------------+---------+----------------------------------------------------------------------+ +| three_freqs | three_freqs_t | -- | three frequencies (wb, wf1, wf2). | +----------------+--------------------------+---------+----------------------------------------------------------------------+ | index1 | indices_t | -- | indices of operators in TRIQS convention: (block_name, inner_index) | +----------------+--------------------------+---------+----------------------------------------------------------------------+