32#include <c2py/converters/stl/vector.hpp>
33#include <c2py/py_converter.hpp>
34#include <c2py/pyref.hpp>
48 inline void set_err_impl(PyObject *p, std::string_view kind,
const char *X, std::string
const &self_type, std::string
const &C) {
49 std::ostringstream err;
50 err <<
"C2py converter: Python to C++ :\n"
51 <<
" ... Conversion of a " << kind <<
" from Python to C++ " << self_type <<
"\n"
52 <<
" ... Cannot convert the " << X <<
" of " << kind <<
" from Python type : " << p->ob_type->tp_name <<
" to the C++ type " << C;
53 PyErr_SetString(PyExc_TypeError, err.str().c_str());
62 template <
typename M,
typename T>
struct py_converter<triqs::gfs::gf<M, T>> {
63 using c_t = triqs::gfs::gf<M, T>;
64 using conv_t = py_converter<triqs::gfs::gf_view<M, T>>;
66 static std::string tp_name() {
return conv_t::tp_name(); }
68 static PyObject *c2py(triqs::gfs::gf_view<M, T> g) {
return conv_t::c2py(g); }
69 static bool is_convertible(PyObject *ob,
bool raise_exception) {
return conv_t::is_convertible(ob, raise_exception); }
70 static c_t py2c(PyObject *ob) {
return c_t{conv_t::py2c(ob)}; }
77 template <
typename M,
typename T>
struct py_converter<triqs::gfs::gf_const_view<M, T>> {
78 using c_t = triqs::gfs::gf_const_view<M, T>;
79 using conv_t = py_converter<triqs::gfs::gf_view<M, T>>;
81 static std::string tp_name() {
return conv_t::tp_name(); }
83 static PyObject *c2py(c_t g) =
delete;
84 static bool is_convertible(PyObject *ob,
bool raise_exception) {
return conv_t::is_convertible(ob, raise_exception); }
85 static c_t py2c(PyObject *ob) {
return conv_t::py2c(ob); }
93 template <
typename M,
typename T>
struct py_converter<triqs::gfs::gf_view<M, T>> {
94 using c_t = triqs::gfs::gf_view<M, T>;
95 using mesh_t =
typename c_t::mesh_t;
96 using data_t =
typename c_t::data_t;
98 static std::string tp_name() {
99 std::ostringstream out;
100 out <<
"Gf[" << ::c2py::python_typename<M>() <<
", " << T::rank <<
"]";
104 static PyObject *c2py(c_t g) {
106 pyref cls = pyref::get_class(
"triqs.gfs",
"Gf",
true);
107 if (cls.is_null())
return NULL;
108 pyref m = cxx2py(g.mesh());
109 if (m.is_null())
return NULL;
110 pyref d = cxx2py(g.data());
111 if (d.is_null())
return NULL;
113 pyref kw = PyDict_New();
114 PyDict_SetItemString(kw,
"mesh", m);
115 PyDict_SetItemString(kw,
"data", d);
117 pyref empty_tuple = PyTuple_New(0);
118 return PyObject_Call(cls, empty_tuple, kw);
123 static void _set_err(PyObject *p,
const char *X, std::string
const &C) {
129 static bool is_convertible(PyObject *ob,
bool raise_exception) {
130 pyref cls = pyref::get_class(
"triqs.gfs",
"Gf",
true);
133 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
134 pyref x = pyref::borrowed(ob);
137 pyref m = x.attr(
"_mesh");
138 if (!py_converter<mesh_t>::is_convertible(m,
false)) {
143 pyref d = x.attr(
"_data");
144 if (!py_converter<data_t>::is_convertible(d, raise_exception)) {
154 static c_t py2c(PyObject *ob) {
155 pyref x = pyref::borrowed(ob);
156 pyref m = x.attr(
"_mesh");
157 pyref d = x.attr(
"_data");
158 return c_t{py2cxx<mesh_t>(m), py2cxx<data_t>(d)};
166 template <
typename M,
typename T,
int A>
struct py_converter<triqs::gfs::block_gf<M, T, nda::C_layout, A>> {
167 using conv_t = py_converter<triqs::gfs::block_gf_view<M, T, nda::C_stride_layout, A>>;
168 using c_t = triqs::gfs::block_gf<M, T, nda::C_layout, A>;
172 static std::string tp_name() {
return conv_t::tp_name(); }
174 static PyObject *c2py(triqs::gfs::block_gf_view<M, T, nda::C_stride_layout, A> g) {
return conv_t::c2py(g); }
175 static bool is_convertible(PyObject *ob,
bool raise_exception) {
return conv_t::is_convertible(ob, raise_exception); }
176 static c_t py2c(PyObject *ob) {
return c_t{conv_t::py2c(ob)}; }
183 template <
typename M,
typename T,
int A>
struct py_converter<triqs::gfs::block_gf_const_view<M, T, nda::C_stride_layout, A>> {
184 using conv_t = py_converter<triqs::gfs::block_gf_view<M, T, nda::C_stride_layout, A>>;
189 static std::string tp_name() {
return conv_t::tp_name(); }
191 static PyObject *c2py(c_t g) =
delete;
192 static bool is_convertible(PyObject *ob,
bool raise_exception) {
return conv_t::is_convertible(ob, raise_exception); }
193 static c_t py2c(PyObject *ob) {
return conv_t::py2c(ob); }
200 template <
typename M,
typename T>
struct py_converter<triqs::gfs::block_gf_view<M, T>> {
201 using gf_type = triqs::gfs::gf<M, T>;
202 using gf_view_type = triqs::gfs::gf_view<M, T>;
203 using c_t = triqs::gfs::block_gf_view<M, T>;
205 static std::string tp_name() {
206 std::ostringstream out;
207 out <<
"BlockGf[" << ::c2py::python_typename<M>() <<
", " << T::rank <<
"]";
213 static PyObject *c2py(c_t g) {
215 std::vector<gf_view_type> vg;
216 vg.reserve(g.data().size());
217 for (
auto const &x : g.data()) vg.push_back(x);
218 pyref v_gf = cxx2py(vg);
219 pyref v_names = cxx2py(g.block_names());
220 if (v_gf.is_null() or v_names.is_null())
return NULL;
221 pyref cls = pyref::module(
"triqs.gfs").attr(
"BlockGf");
222 if (cls.is_null())
return NULL;
223 pyref kw = PyDict_New();
224 PyDict_SetItemString(kw,
"name_list", v_names);
225 PyDict_SetItemString(kw,
"block_list", v_gf);
226 pyref empty_tuple = PyTuple_New(0);
227 return PyObject_Call(cls, empty_tuple, kw);
232 static void _set_err(PyObject *p,
const char *X, std::string
const &C) {
238 static bool is_convertible(PyObject *ob,
bool raise_exception) {
239 pyref cls = pyref::get_class(
"triqs.gfs",
"BlockGf",
true);
242 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
243 pyref x = pyref::borrowed(ob);
246 pyref gfs = x.attr(
"_BlockGf__GFlist");
247 if (!py_converter<std::vector<gf_view_type>>::is_convertible(gfs,
false)) {
252 pyref names = x.attr(
"_BlockGf__indices");
253 if (!py_converter<std::vector<std::string>>::is_convertible(names,
false)) {
263 static c_t py2c(PyObject *ob) {
264 pyref x = pyref::borrowed(ob);
265 pyref names = x.attr(
"_BlockGf__indices");
266 pyref gfs = x.attr(
"_BlockGf__GFlist");
267 return make_block_gf_view(py2cxx<std::vector<std::string>>(names), py2cxx<std::vector<gf_view_type>>(gfs));
275 template <
typename M,
typename T>
struct py_converter<triqs::gfs::block2_gf_view<M, T>> {
276 using gf_type = triqs::gfs::gf<M, T>;
277 using gf_view_type = triqs::gfs::gf_view<M, T>;
280 static std::string tp_name() {
281 std::ostringstream out;
282 out <<
"Block2Gf[" << ::c2py::python_typename<M>() <<
", " << T::rank <<
"]";
288 static PyObject *c2py(c_t g) {
289 std::vector<std::vector<gf_view_type>> vvg;
290 vvg.reserve(g.data().size());
291 for (
auto const &x : g.data()) {
292 std::vector<gf_view_type> vg;
293 vg.reserve(x.size());
294 for (
auto const &y : x) vg.push_back(y);
297 pyref v_gf = cxx2py(vvg);
298 pyref v_names1 = cxx2py(g.block_names()[0]);
299 pyref v_names2 = cxx2py(g.block_names()[1]);
300 if (v_gf.is_null() or v_names1.is_null() or v_names2.is_null())
return NULL;
301 pyref cls = pyref::module(
"triqs.gfs").attr(
"Block2Gf");
302 if (cls.is_null())
return NULL;
303 pyref args = PyTuple_Pack(3, (PyObject *)v_names1, (PyObject *)v_names2, (PyObject *)v_gf);
304 return PyObject_Call(cls, args, NULL);
309 static bool is_convertible(PyObject *ob,
bool raise_exception) {
310 pyref cls = pyref::get_class(
"triqs.gfs",
"Block2Gf",
true);
311 if (cls.is_null())
throw std::runtime_error(
"Cannot find the triqs.gfs.Block2Gf");
314 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
316 pyref x = pyref::borrowed(ob);
317 pyref gfs = x.attr(
"_Block2Gf__GFlist");
318 return py_converter<std::vector<std::vector<gf_view_type>>>::is_convertible(gfs, raise_exception);
323 static c_t py2c(PyObject *ob) {
324 pyref x = pyref::borrowed(ob);
325 pyref names1 = x.attr(
"_Block2Gf__indices1");
326 pyref names2 = x.attr(
"_Block2Gf__indices2");
327 pyref gfs = x.attr(
"_Block2Gf__GFlist");
328 return make_block2_gf_view(py2cxx<std::vector<std::string>>(names1), py2cxx<std::vector<std::string>>(names2),
329 py2cxx<std::vector<std::vector<gf_view_type>>>(gfs));
Provides the block Green's function container.
Provides a non-owning view of a block Green's function.
Provides the free factory functions for block Green's functions.
Provides the triqs::gfs::gf_const_view container, a read-only non-owning view of a Green's function.
Provides a mutable non-owning view of a Green's function.
Provides the Green's function class.
block_gf_view< Mesh, Target, Layout, Arity, true > block_gf_const_view
Const view alias for a block Green's function.
block_gf_view< Mesh, Target, Layout, 2, false > block2_gf_view
Mutable view of a two-index block Green's function.
block2_gf_view_of< Gf > make_block2_gf_view(std::vector< std::vector< Gf > > &v)
Make a triqs::gfs::block2_gf_view from a matrix of views (block names default to "0",...
block_gf_view_of< G0 > make_block_gf_view(G0 &&g0, G &&...g)
Make a triqs::gfs::block_gf_view from a list of Green's function views (block names default to "0",...
std::string typeid_name()
Human-readable name of a given static type.
Additional converters for gf.
Provides the target types that fix the value stored at each mesh point of a Green's function.
Helper functions built around std::type_info.