Skip to content

Commit

Permalink
Update python wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
j-otsuki committed May 7, 2018
1 parent 0254d63 commit 429ce9c
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 8 deletions.
8 changes: 4 additions & 4 deletions c++/g2_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {}
};

/*
Expand Down
3 changes: 2 additions & 1 deletion c++/pomerol_ed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ namespace pomerol2triqs {
/// Retarded Green's function on real energy axis
block_gf<refreq> G_w(gf_struct_t const &gf_struct, double beta, std::pair<double, double> 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);

Expand Down
108 changes: 108 additions & 0 deletions python/pomerol2triqs_converters.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,112 @@ template <> struct py_converter<g2_iw_inu_inup_params_t> {
}
};

}}


// --- C++ Python converter for g2_three_freqs_params_t
#include <triqs/python_tools/converters/vector.hpp>
#include <triqs/python_tools/converters/string.hpp>
#include <algorithm>

namespace triqs { namespace py_tools {

template <> struct py_converter<g2_three_freqs_params_t> {
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 <typename T, typename U> 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<T>(PyDict_GetItemString(dic, name));
else
r = init_default;
}

template <typename T> static void _get_optional(PyObject *dic, const char *name, T &r) {
if (PyDict_Contains(dic, pyref::string(name)))
r = convert_from_python<T>(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<gf_struct_t>(PyDict_GetItemString(dic, "gf_struct"));
res.beta = convert_from_python<double>(PyDict_GetItemString(dic, "beta"));
_get_optional(dic, "channel" , res.channel ,PH);
res.three_freqs = convert_from_python<three_freqs_t>(PyDict_GetItemString(dic, "three_freqs"));
res.index1 = convert_from_python<indices_t>(PyDict_GetItemString(dic, "index1"));
res.index2 = convert_from_python<indices_t>(PyDict_GetItemString(dic, "index2"));
res.index3 = convert_from_python<indices_t>(PyDict_GetItemString(dic, "index3"));
res.index4 = convert_from_python<indices_t>(PyDict_GetItemString(dic, "index4"));
return res;
}

template <typename T>
static void _check(PyObject *dic, std::stringstream &fs, int &err, const char *name, const char *tname) {
if (!convertible_from_python<T>(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 <typename T>
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<T>(dic,fs,err,name,tname);
}

template <typename T>
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<T>(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<std::string> ks, all_keys = {"gf_struct","beta","channel","three_freqs","index1","index2","index3","index4"};
pyref keys = PyDict_Keys(dic);
if (!convertible_from_python<std::vector<std::string>>(keys, true)) {
fs << "\nThe dict keys are not strings";
goto _error;
}
ks = convert_from_python<std::vector<std::string>>(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<gf_struct_t >(dic, fs, err, "gf_struct" , "gf_struct_t");
_check_mandatory<double >(dic, fs, err, "beta" , "double");
_check_optional <pomerol2triqs::channel_t>(dic, fs, err, "channel" , "pomerol2triqs::channel_t");
_check_mandatory<three_freqs_t >(dic, fs, err, "three_freqs", "three_freqs_t");
_check_mandatory<indices_t >(dic, fs, err, "index1" , "indices_t");
_check_mandatory<indices_t >(dic, fs, err, "index2" , "indices_t");
_check_mandatory<indices_t >(dic, fs, err, "index3" , "indices_t");
_check_mandatory<indices_t >(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" <<fs.str();
if (raise_exception) PyErr_SetString(PyExc_TypeError, fs2.str().c_str());
return false;
}
};

}}
5 changes: 4 additions & 1 deletion python/pomerol2triqs_desc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
doc = r"""Retarded Green's function on real energy axis""")

c.add_method("""triqs::arrays::array<std::complex<double>, 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<std::complex<double> > 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)

Expand Down
26 changes: 24 additions & 2 deletions python/pomerol2triqs_parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
+----------------+--------------------------+---------+----------------------------------------------------------------------+
Expand Down

0 comments on commit 429ce9c

Please sign in to comment.