24#include <triqs/cpp2py_converters/arrays.hpp>
26#include <cpp2py/misc.hpp>
27#include <cpp2py/converters/vector.hpp>
28#include <cpp2py/converters/tuple.hpp>
29#include <cpp2py/converters/std_array.hpp>
30#include <cpp2py/converters/string.hpp>
42 template <
typename M,
typename T>
struct is_view<triqs::gfs::gf_view<M, T>> : std::true_type {};
43 template <
typename M,
typename T>
struct is_view<triqs::gfs::gf_const_view<M, T>> : std::true_type {};
45 template <
typename M,
typename T,
typename L,
int A,
bool C>
struct is_view<triqs::gfs::block_gf_view<M, T, L, A, C>> : std::true_type {};
51 template <
typename M,
typename T>
struct py_converter<triqs::gfs::gf<M, T>> {
52 using c_t = triqs::gfs::gf<M, T>;
53 using conv_t = py_converter<triqs::gfs::gf_view<M, T>>;
56 static std::string tp_name() {
return conv_t::tp_name(); }
59 static PyObject *c2py(triqs::gfs::gf_view<M, T> g) {
return conv_t::c2py(g); }
60 static bool is_convertible(PyObject *ob,
bool raise_exception) {
return conv_t::is_convertible(ob, raise_exception); }
61 static c_t py2c(PyObject *ob) {
return c_t{conv_t::py2c(ob)}; }
68 template <
typename M,
typename T>
struct py_converter<triqs::gfs::gf_const_view<M, T>> {
69 using c_t = triqs::gfs::gf_const_view<M, T>;
70 using conv_t = py_converter<triqs::gfs::gf_view<M, T>>;
73 static std::string tp_name() {
return conv_t::tp_name(); }
76 static PyObject *c2py(c_t g) =
delete;
77 static bool is_convertible(PyObject *ob,
bool raise_exception) {
return conv_t::is_convertible(ob, raise_exception); }
78 static c_t py2c(PyObject *ob) {
return conv_t::py2c(ob); }
86 template <
typename M,
typename T>
struct py_converter<triqs::gfs::gf_view<M, T>> {
87 using c_t = triqs::gfs::gf_view<M, T>;
88 using mesh_t =
typename c_t::mesh_t;
89 using data_t =
typename c_t::data_t;
92 static std::string tp_name() {
93 std::ostringstream out;
94 out <<
"Gf[" << ::c2py::python_typename<M>() <<
", " << T::rank <<
"]";
99 static PyObject *c2py(c_t g) {
101 pyref cls = pyref::get_class(
"triqs.gfs",
"Gf",
true);
102 if (cls.is_null())
return NULL;
103 pyref m = convert_to_python(g.mesh());
104 if (m.is_null())
return NULL;
105 pyref d = convert_to_python(g.data());
106 if (d.is_null())
return NULL;
108 pyref kw = PyDict_New();
109 PyDict_SetItemString(kw,
"mesh", m);
110 PyDict_SetItemString(kw,
"data", d);
112 pyref empty_tuple = PyTuple_New(0);
113 return PyObject_Call(cls, empty_tuple, kw);
118 static void _set_err(PyObject *p,
const char *X, std::string
const &C) {
119 using namespace std::string_literals;
120 std::string err =
"Cpp2py converter: Python to C++ :\n"s +
" ... Conversion of a Gf from Python to C++ "
122 +
" of Gf from Python type : " + p->ob_type->tp_name +
" to the C++ type " + C;
123 PyErr_SetString(PyExc_TypeError, err.c_str());
128 static bool is_convertible(PyObject *ob,
bool raise_exception) {
129 pyref cls = pyref::get_class(
"triqs.gfs",
"Gf",
true);
132 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
133 pyref x = borrowed(ob);
136 pyref m = x.attr(
"_mesh");
137 if (!py_converter<mesh_t>::is_convertible(m,
false)) {
142 pyref d = x.attr(
"_data");
143 if (!py_converter<data_t>::is_convertible(d, raise_exception)) {
153 static c_t py2c(PyObject *ob) {
154 pyref x = borrowed(ob);
155 pyref m = x.attr(
"_mesh");
156 pyref d = x.attr(
"_data");
157 return c_t{convert_from_python<mesh_t>(m), convert_from_python<data_t>(d)};
165 template <
typename M,
typename T,
int A>
struct py_converter<triqs::gfs::block_gf<M, T, nda::C_layout, A>> {
166 using conv_t = py_converter<triqs::gfs::block_gf_view<M, T, nda::C_stride_layout, A>>;
167 using c_t = triqs::gfs::block_gf<M, T, nda::C_layout, A>;
172 static std::string tp_name() {
return conv_t::tp_name(); }
175 static PyObject *c2py(triqs::gfs::block_gf_view<M, T, nda::C_stride_layout, A> g) {
return conv_t::c2py(g); }
176 static bool is_convertible(PyObject *ob,
bool raise_exception) {
return conv_t::is_convertible(ob, raise_exception); }
177 static c_t py2c(PyObject *ob) {
return c_t{conv_t::py2c(ob)}; }
184 template <
typename M,
typename T,
int A>
struct py_converter<triqs::gfs::block_gf_const_view<M, T, nda::C_stride_layout, A>> {
185 using conv_t = py_converter<triqs::gfs::block_gf_view<M, T, nda::C_stride_layout, A>>;
191 static std::string tp_name() {
return conv_t::tp_name(); }
194 static PyObject *c2py(c_t g) =
delete;
195 static bool is_convertible(PyObject *ob,
bool raise_exception) {
return conv_t::is_convertible(ob, raise_exception); }
196 static c_t py2c(PyObject *ob) {
return conv_t::py2c(ob); }
203 template <
typename M,
typename T>
struct py_converter<triqs::gfs::block_gf_view<M, T>> {
204 using gf_type = triqs::gfs::gf<M, T>;
205 using gf_view_type = triqs::gfs::gf_view<M, T>;
206 using c_t = triqs::gfs::block_gf_view<M, T>;
209 static std::string tp_name() {
210 std::ostringstream out;
211 out <<
"BlockGf[" << ::c2py::python_typename<M>() <<
", " << T::rank <<
"]";
218 static PyObject *c2py(c_t g) {
220 std::vector<gf_view_type> vg;
221 vg.reserve(g.data().size());
222 for (
auto const &x : g.data()) vg.push_back(x);
223 pyref v_gf = convert_to_python(vg);
224 pyref v_names = convert_to_python(g.block_names());
225 if (v_gf.is_null() or v_names.is_null())
return NULL;
226 pyref cls = pyref::module(
"triqs.gfs").attr(
"BlockGf");
227 if (cls.is_null())
return NULL;
228 pyref kw = PyDict_New();
229 PyDict_SetItemString(kw,
"name_list", v_names);
230 PyDict_SetItemString(kw,
"block_list", v_gf);
231 pyref empty_tuple = PyTuple_New(0);
232 return PyObject_Call(cls, empty_tuple, kw);
237 static void _set_err(PyObject *p,
const char *X, std::string
const &C) {
238 using namespace std::string_literals;
239 std::string err =
"Cpp2py converter: Python to C++ :\n"s +
" ... Conversion of a BlockGf from Python to C++ "
241 +
" to the C++ type " + C;
242 PyErr_SetString(PyExc_TypeError, err.c_str());
247 static bool is_convertible(PyObject *ob,
bool raise_exception) {
248 pyref cls = pyref::get_class(
"triqs.gfs",
"BlockGf",
true);
251 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
252 pyref x = borrowed(ob);
255 pyref gfs = x.attr(
"_BlockGf__GFlist");
256 if (!py_converter<std::vector<gf_view_type>>::is_convertible(gfs,
false)) {
261 pyref names = x.attr(
"_BlockGf__indices");
262 if (!py_converter<std::vector<std::string>>::is_convertible(names,
false)) {
272 static c_t py2c(PyObject *ob) {
273 pyref x = borrowed(ob);
274 pyref names = x.attr(
"_BlockGf__indices");
275 pyref gfs = x.attr(
"_BlockGf__GFlist");
276 return make_block_gf_view(convert_from_python<std::vector<std::string>>(names), convert_from_python<std::vector<gf_view_type>>(gfs));
284 template <
typename M,
typename T>
struct py_converter<triqs::gfs::block2_gf_view<M, T>> {
285 using gf_type = triqs::gfs::gf<M, T>;
286 using gf_view_type = triqs::gfs::gf_view<M, T>;
290 static std::string tp_name() {
291 std::ostringstream out;
292 out <<
"Block2Gf[" << ::c2py::python_typename<M>() <<
", " << T::rank <<
"]";
299 static PyObject *c2py(c_t g) {
300 std::vector<std::vector<gf_view_type>> vvg;
301 vvg.reserve(g.data().size());
302 for (
auto const &x : g.data()) {
303 std::vector<gf_view_type> vg;
304 vg.reserve(x.size());
305 for (
auto const &y : x) vg.push_back(y);
308 pyref v_gf = convert_to_python(vvg);
309 pyref v_names1 = convert_to_python(g.block_names()[0]);
310 pyref v_names2 = convert_to_python(g.block_names()[1]);
311 if (v_gf.is_null() or v_names1.is_null() or v_names2.is_null())
return NULL;
312 pyref cls = pyref::module(
"triqs.gfs").attr(
"Block2Gf");
313 if (cls.is_null())
return NULL;
314 pyref args = PyTuple_Pack(3, (PyObject *)v_names1, (PyObject *)v_names2, (PyObject *)v_gf);
315 return PyObject_Call(cls, args, NULL);
320 static bool is_convertible(PyObject *ob,
bool raise_exception) {
321 pyref cls = pyref::get_class(
"triqs.gfs",
"Block2Gf",
true);
322 if (cls.is_null())
throw std::runtime_error(
"Cannot find the triqs.gfs.Block2Gf");
325 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
327 pyref x = borrowed(ob);
328 pyref gfs = x.attr(
"_Block2Gf__GFlist");
329 return py_converter<std::vector<std::vector<gf_view_type>>>::is_convertible(gfs, raise_exception);
334 static c_t py2c(PyObject *ob) {
335 pyref x = borrowed(ob);
336 pyref names1 = x.attr(
"_Block2Gf__indices1");
337 pyref names2 = x.attr(
"_Block2Gf__indices2");
338 pyref gfs = x.attr(
"_Block2Gf__GFlist");
339 return make_block2_gf_view(convert_from_python<std::vector<std::string>>(names1), convert_from_python<std::vector<std::string>>(names2),
340 convert_from_python<std::vector<std::vector<gf_view_type>>>(gfs));
Umbrella header for the Green's function library.
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.
Provides various utilities used with Meshes.
Additional converters for gf.
Helper functions built around std::type_info.