20#include <cpp2py/cpp2py.hpp>
30 template <>
struct py_converter<triqs::mesh::all_t> {
32 static constexpr char *tp_name =
"all";
34 static PyObject *c2py(triqs::mesh::all_t m) {
35 pyref all = pyref::get_class(
"builtins",
"all",
true);
36 if (all.is_null())
return NULL;
40 static bool is_convertible(PyObject *ob,
bool raise_exception) {
41 pyref all = pyref::get_class(
"builtins",
"all",
true);
45 static triqs::mesh::all_t py2c(PyObject *ob) {
return {}; }
52 template <>
struct py_converter<triqs::mesh::statistic_enum> {
54 static constexpr char *tp_name =
"Statistic (\"Fermion\" | \"Boson\")";
57 if (x == triqs::mesh::Fermion)
return PyUnicode_FromString(
"Fermion");
58 return PyUnicode_FromString(
"Boson");
61 std::string s = PyUnicode_AsUTF8(ob);
62 if (s ==
"Fermion")
return triqs::mesh::Fermion;
63 return triqs::mesh::Boson;
65 static bool is_convertible(PyObject *ob,
bool raise_exception) {
66 if (!PyUnicode_Check(ob)) {
67 if (raise_exception) PyErr_SetString(PyExc_ValueError,
"Convertion of C++ enum statistic_enum : the object is not a string");
70 std::string s = PyUnicode_AsUTF8(ob);
71 if (s ==
"Fermion")
return true;
72 if (s ==
"Boson")
return true;
73 if (raise_exception) {
74 auto err =
"Convertion of C++ enum statistic_enum : \nThe string \"" + s +
"\" is not in [Fermion,Boson]";
75 PyErr_SetString(PyExc_ValueError, err.c_str());
85 template <>
struct py_converter<triqs::mesh::matsubara_freq> {
86 using c_t = triqs::mesh::matsubara_freq;
88 static constexpr char *tp_name =
"MatsubaraFreq";
90 static PyObject *c2py(c_t
const &x) {
91 pyref cls = pyref::get_class(
"triqs.mesh",
"MatsubaraFreq",
true);
92 if (cls.is_null())
return NULL;
94 pyref kw = PyDict_New();
96 pyref
n = convert_to_python(x.n);
97 if (
n.is_null())
return NULL;
98 pyref beta = convert_to_python(x.beta);
99 if (beta.is_null())
return NULL;
100 pyref statistic = convert_to_python(x.statistic);
101 if (statistic.is_null())
return NULL;
102 PyDict_SetItemString(kw,
"n", n);
103 PyDict_SetItemString(kw,
"beta", beta);
104 PyDict_SetItemString(kw,
"statistic", statistic);
106 pyref empty_tuple = PyTuple_New(0);
107 return PyObject_Call(cls, empty_tuple, kw);
110 static bool is_convertible(PyObject *ob,
bool raise_exception) {
111 pyref cls = pyref::get_class(
"triqs.mesh",
"MatsubaraFreq",
true);
112 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
118 static c_t py2c(PyObject *ob) {
119 pyref x = borrowed(ob);
120 pyref
n = x.attr(
"n");
121 pyref beta = x.attr(
"beta");
122 pyref statistic = x.attr(
"statistic");
123 return c_t{convert_from_python<long>(n), convert_from_python<double>(beta), convert_from_python<triqs::mesh::statistic_enum>(statistic)};
131 template <>
struct py_converter<triqs::lattice::bravais_lattice::point_t> {
132 using c_t = triqs::lattice::bravais_lattice::point_t;
134 static constexpr char *tp_name =
"LatticePoint";
136 static PyObject *c2py(c_t
const &x) {
137 pyref cls = pyref::get_class(
"triqs.lattice",
"LatticePoint",
true);
138 if (cls.is_null())
return NULL;
140 pyref kw = PyDict_New();
142 pyref index = convert_to_python(x.index());
143 if (index.is_null())
return NULL;
144 pyref lattice = convert_to_python(x.lattice());
145 if (lattice.is_null())
return NULL;
146 PyDict_SetItemString(kw,
"index", index);
147 PyDict_SetItemString(kw,
"lattice", lattice);
149 pyref empty_tuple = PyTuple_New(0);
150 return PyObject_Call(cls, empty_tuple, kw);
153 static bool is_convertible(PyObject *ob,
bool raise_exception) {
154 pyref cls = pyref::get_class(
"triqs.lattice",
"LatticePoint",
true);
155 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
161 using lattice_py_type =
struct {
163 triqs::lattice::bravais_lattice *_c;
166 static c_t py2c(PyObject *ob) {
168 pyref x = borrowed(ob);
169 pyref index = x.attr(
"index");
170 pyref lattice = x.attr(
"lattice");
171 auto *lattice_c_ptr =
reinterpret_cast<lattice_py_type *
>(
static_cast<PyObject *
>(lattice))->_c;
172 if (lattice_c_ptr == NULL) {
173 std::cerr <<
"Severe internal error : lattice_ptr is null in py2c\n";
177 return c_t{convert_from_python<std::array<long, 3>>(index), lattice_c_ptr};
185 template <triqs::mesh::Mesh... Ms>
struct py_converter<triqs::mesh::prod<Ms...>> {
186 using c_t = triqs::mesh::prod<Ms...>;
187 using mtuple_conv = py_converter<typename c_t::m_tuple_t>;
190 static std::string tp_name() {
191 std::ostringstream out;
193 out <<
"MeshProduct[";
194 ((out << sep << ::c2py::python_typename<Ms>(), sep =
", "), ...);
200 static PyObject *c2py(c_t m) {
201 pyref cls = pyref::get_class(
"triqs.mesh",
"MeshProduct",
true);
202 if (cls.is_null())
return NULL;
203 pyref m_tuple = mtuple_conv::c2py(m.components());
204 if (m_tuple.is_null())
return NULL;
205 return PyObject_Call(cls, m_tuple, NULL);
208 static bool is_convertible(PyObject *ob,
bool raise_exception) {
209 pyref cls = pyref::get_class(
"triqs.mesh",
"MeshProduct",
true);
212 if (not pyref::check_is_instance(ob, cls, raise_exception))
return false;
213 pyref x = borrowed(ob);
216 pyref ml = x.attr(
"_mlist");
217 return mtuple_conv::is_convertible(ml, raise_exception);
220 static c_t py2c(PyObject *ob) {
221 pyref x = borrowed(ob);
222 pyref ml = x.attr(
"_mlist");
231 template <triqs::mesh::MeshPo
int MP>
struct py_converter<MP> {
234 static constexpr char *tp_name =
"MeshPoint";
236 static PyObject *c2py(c_t
const &p) {
238 pyref cls = pyref::get_class(
"triqs.mesh",
"MeshPoint",
true);
239 if (cls.is_null())
return NULL;
241 pyref index = convert_to_python(p.index());
242 if (index.is_null())
return NULL;
244 pyref data_index = convert_to_python(p.data_index());
245 if (data_index.is_null())
return NULL;
247 pyref mesh_hash = convert_to_python(p.mesh_hash());
248 if (mesh_hash.is_null())
return NULL;
250 if constexpr (
requires { p.value(); }) {
251 pyref val = convert_to_python(p.value());
252 if (val.is_null())
return NULL;
254 if constexpr (
requires { p.weight(); }) {
255 pyref weight = convert_to_python(p.weight());
256 if (weight.is_null())
return NULL;
257 return PyObject_Call(cls, pyref::make_tuple(index, data_index, mesh_hash, val, weight), NULL);
259 return PyObject_Call(cls, pyref::make_tuple(index, data_index, mesh_hash, val), NULL);
262 return PyObject_Call(cls, pyref::make_tuple(index, data_index, mesh_hash), NULL);
statistic_enum
Enum to specify particle statistics.
many_body_operator_generic< T > n(IndexTypes... indices)
Create a number operator .
decltype(auto) apply_construct(T &&t)
Brace-construct an object from the elements of a tuple.
Umbrella header for the TRIQS mesh types.
Additional converters for gf.