diff --git a/pytriqs/wrap_generator/py_converter_wrapper.mako.hpp b/pytriqs/wrap_generator/py_converter_wrapper.mako.hpp index b55ea440..0b63d8ea 100644 --- a/pytriqs/wrap_generator/py_converter_wrapper.mako.hpp +++ b/pytriqs/wrap_generator/py_converter_wrapper.mako.hpp @@ -71,8 +71,6 @@ template<> struct py_converter<${c.c_type_absolute}> { %for en in module.enums : -namespace triqs { namespace py_tools { - template <> struct py_converter<${en.c_name_absolute}> { static PyObject * c2py(${en.c_name_absolute} x) { %for n,val in enumerate(en.values[:-1]) : @@ -104,7 +102,6 @@ template <> struct py_converter<${en.c_name_absolute}> { } }; -}} %endfor }} diff --git a/pytriqs/wrap_test/CMakeLists.txt b/pytriqs/wrap_test/CMakeLists.txt index 908a2f6d..9444e69f 100644 --- a/pytriqs/wrap_test/CMakeLists.txt +++ b/pytriqs/wrap_test/CMakeLists.txt @@ -10,6 +10,7 @@ install (FILES ${PYTHON_SOURCES} DESTINATION ${TRIQS_PYTHON_LIB_DEST}/wrap_test) # Build C extension module triqs_python_extension(my_module wrap_test) triqs_python_extension(my_moduleB wrap_test) +triqs_python_extension(test_g wrap_test) # ??triqs_set_rpath_for_target(my_module) include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/pytriqs/wrap_test/g.hpp b/pytriqs/wrap_test/g.hpp new file mode 100644 index 00000000..429ea49e --- /dev/null +++ b/pytriqs/wrap_test/g.hpp @@ -0,0 +1,26 @@ +#include +using namespace triqs::gfs; +using namespace triqs; + +block_gf_view make_bgf(double a) { + + double beta = 1; + auto G1 = gf({beta, Fermion}, {2, 2}); + + auto B1 = make_block_gf(3, G1); + + { + H5::H5File file("ess_test_g1.h5", H5F_ACC_TRUNC); + h5_write(file, "g", B1); + } + + return B1; +} + +void pass_bgf(block_gf_view g) { + + { + H5::H5File file("ess_test_g2.h5", H5F_ACC_TRUNC); + h5_write(file, "g", g); + } +} diff --git a/pytriqs/wrap_test/test_g_desc.py b/pytriqs/wrap_test/test_g_desc.py new file mode 100644 index 00000000..a192c3fd --- /dev/null +++ b/pytriqs/wrap_test/test_g_desc.py @@ -0,0 +1,14 @@ +from wrap_generator import * + +# The module +module = module_(full_name = "pytriqs.wrap_test.test_g", doc = " Doc of my_module ") +module.add_include("") +module.add_include("") + +module.add_function (name = "make_bgf", signature = "block_gf_view (double a)", doc = "DOC of print_a") +module.add_function (name = "pass_bgf", signature = "void (block_gf_view g) ", doc = "DOC of print_a") + +if __name__ == '__main__' : + module.generate_code(mako_template = sys.argv[1], wrap_file = sys.argv[2]) + module.generate_py_converter_header(mako_template = sys.argv[3], wrap_file = sys.argv[4]) + diff --git a/triqs/gfs/block.hpp b/triqs/gfs/block.hpp index cd4ab913..cd36b724 100644 --- a/triqs/gfs/block.hpp +++ b/triqs/gfs/block.hpp @@ -157,9 +157,10 @@ namespace gfs { return {{int(V.size())}, std::move(V), nothing{}, nothing{}, nothing{}}; } - // for cython proxy only. do not document. - template gf_view make_block_gf_view_from_vector_of_cython_proxy(std::vector V) { - return {{int(V.size())}, std::move(V), nothing{}, nothing{}, nothing{}}; + template + gf_view make_block_gf_view_from_vector(std::vector block_names, + std::vector V) { + return {{std::move(block_names)}, std::move(V), nothing{}, nothing{}, nothing{}}; } // ------------------------------- Extend reinterpret_scalar_valued_gf_as_matrix_valued for block gf ------ diff --git a/triqs/gfs/gf.hpp b/triqs/gfs/gf.hpp index 3173c8aa..b703c29f 100644 --- a/triqs/gfs/gf.hpp +++ b/triqs/gfs/gf.hpp @@ -528,9 +528,9 @@ namespace gfs { gf_view(gf_impl &&g) noexcept : B(std::move(g), bool {}) {} // from a gf && template - gf_view(typename B::mesh_t const &m, D const &dat, typename B::singularity_view_t const &t, typename B::symmetry_t const &s, + gf_view(typename B::mesh_t const &m, D &&dat, typename B::singularity_view_t const &t, typename B::symmetry_t const &s, typename B::indices_t const &ind = typename B::indices_t{}, std::string name = "") - : B(m, factory(dat), t, s, ind, name, typename B::evaluator_t{}) {} + : B(m, factory(std::forward(dat)), t, s, ind, name, typename B::evaluator_t{}) {} friend void swap(gf_view &a, gf_view &b) noexcept { a.swap_impl(b); } diff --git a/triqs/python_tools/wrapper_tools.hpp b/triqs/python_tools/wrapper_tools.hpp index 768b0d53..abba8030 100644 --- a/triqs/python_tools/wrapper_tools.hpp +++ b/triqs/python_tools/wrapper_tools.hpp @@ -25,7 +25,7 @@ namespace triqs { namespace py_tools { //--------------------- pyref ----------------------------- -// a little class to hold an owned ref, make sure it is decref at desctruction +// a little class to hold an owned ref, make sure it is decref at destruction // with some useful factories. class pyref { PyObject * ob = NULL; @@ -272,7 +272,12 @@ template <> struct py_converter { template struct py_converter> { static PyObject * c2py(std::vector const &v) { PyObject * list = PyList_New(0); - for (auto const & x : v) if (PyList_Append(list, py_converter::c2py(x)) == -1) { Py_DECREF(list); return NULL;} // error + for (auto & x : v) if (PyList_Append(list, py_converter::c2py(x)) == -1) { Py_DECREF(list); return NULL;} // error + return list; + } + static PyObject * c2py(std::vector &v) { + PyObject * list = PyList_New(0); + for (auto & x : v) if (PyList_Append(list, py_converter::c2py(x)) == -1) { Py_DECREF(list); return NULL;} // error return list; } static bool is_convertible(PyObject *ob, bool raise_exception) { @@ -374,6 +379,7 @@ template <> struct py_converter { }; */ + // --- nothing <--> None ---- #ifdef TRIQS_GF_INCLUDED @@ -392,6 +398,52 @@ template<> struct py_converter : py_converter_from_reduct template struct py_converter> : py_converter_from_reductor>{}; template<> struct py_converter : py_converter_from_reductor{}; +// Converter for Block gf +template struct py_converter>> { + + using gf_type = triqs::gfs::gf; + using gf_view_type = triqs::gfs::gf_view; + using c_type = triqs::gfs::gf_view; + + static PyObject *c2py(c_type &&g) { return c2py(g);} + + static PyObject *c2py(c_type &g) { + // rm the view_proxy + std::vector vg; + vg.reserve(g.data().size()); + for (auto const & x : g.data()) vg.push_back(x); + pyref v_gf = convert_to_python(vg); + pyref v_names = convert_to_python(g.mesh().domain().names()); + pyref cls = pyref::module("pytriqs.gf.local").attr("BlockGf"); + if (cls.is_null()) TRIQS_RUNTIME_ERROR <<"Can not find the pytriqs.gf.local.BlockGf"; + pyref kw = PyDict_New(); + PyDict_SetItemString(kw, "name_list", v_names); + PyDict_SetItemString(kw, "block_list", v_gf); + pyref empty_tuple = PyTuple_New(0); + return PyObject_Call(cls, empty_tuple, kw); + } + + static bool is_convertible(PyObject *ob, bool raise_exception) { + pyref cls = pyref::module("pytriqs.gf.local").attr("BlockGf"); + if (cls.is_null()) TRIQS_RUNTIME_ERROR <<"Can not find the pytriqs.gf.local.BlockGf"; + int i = PyObject_IsInstance(ob, cls); + if (i == -1) { // an error has occurred + i = 0; + if (!raise_exception) PyErr_Clear(); + } + if ((i == 0) && (raise_exception)) PyErr_SetString(PyExc_TypeError, "The object is not a BlockGf"); + return i; + } + + static c_type py2c(PyObject *ob) { + pyref x = borrowed(ob); + pyref names = x.attr("_BlockGf__indices"); + pyref gfs = x.attr("_BlockGf__GFlist"); + return make_block_gf_view_from_vector(convert_from_python>(names), + convert_from_python>(gfs)); + } +}; + #endif // ---- function ----