3
0
mirror of https://github.com/triqs/dft_tools synced 2024-10-31 19:23:45 +01:00

gf_block converter

This commit is contained in:
Olivier Parcollet 2014-05-21 23:01:27 +02:00
parent 4789dccda6
commit 5c8270c437
7 changed files with 101 additions and 10 deletions

View File

@ -71,8 +71,6 @@ template<> struct py_converter<${c.c_type_absolute}> {
%for en in module.enums : %for en in module.enums :
namespace triqs { namespace py_tools {
template <> struct py_converter<${en.c_name_absolute}> { template <> struct py_converter<${en.c_name_absolute}> {
static PyObject * c2py(${en.c_name_absolute} x) { static PyObject * c2py(${en.c_name_absolute} x) {
%for n,val in enumerate(en.values[:-1]) : %for n,val in enumerate(en.values[:-1]) :
@ -104,7 +102,6 @@ template <> struct py_converter<${en.c_name_absolute}> {
} }
}; };
}}
%endfor %endfor
}} }}

View File

@ -10,6 +10,7 @@ install (FILES ${PYTHON_SOURCES} DESTINATION ${TRIQS_PYTHON_LIB_DEST}/wrap_test)
# Build C extension module # Build C extension module
triqs_python_extension(my_module wrap_test) triqs_python_extension(my_module wrap_test)
triqs_python_extension(my_moduleB wrap_test) triqs_python_extension(my_moduleB wrap_test)
triqs_python_extension(test_g wrap_test)
# ??triqs_set_rpath_for_target(my_module) # ??triqs_set_rpath_for_target(my_module)
include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ) include_directories( ${CMAKE_CURRENT_SOURCE_DIR} )

26
pytriqs/wrap_test/g.hpp Normal file
View File

@ -0,0 +1,26 @@
#include <triqs/gfs.hpp>
using namespace triqs::gfs;
using namespace triqs;
block_gf_view<imfreq> make_bgf(double a) {
double beta = 1;
auto G1 = gf<imfreq>({beta, Fermion}, {2, 2});
auto B1 = make_block_gf<imfreq>(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<imfreq> g) {
{
H5::H5File file("ess_test_g2.h5", H5F_ACC_TRUNC);
h5_write(file, "g", g);
}
}

View File

@ -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("<triqs/../pytriqs/wrap_test/g.hpp>")
module.add_include("<pytriqs/converters/gf.hpp>")
module.add_function (name = "make_bgf", signature = "block_gf_view<imfreq> (double a)", doc = "DOC of print_a")
module.add_function (name = "pass_bgf", signature = "void (block_gf_view<imfreq> 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])

View File

@ -157,9 +157,10 @@ namespace gfs {
return {{int(V.size())}, std::move(V), nothing{}, nothing{}, nothing{}}; return {{int(V.size())}, std::move(V), nothing{}, nothing{}, nothing{}};
} }
// for cython proxy only. do not document. template <typename GF>
template <typename GF, typename GF2> gf_view<block_index, GF> make_block_gf_view_from_vector_of_cython_proxy(std::vector<GF2> V) { gf_view<block_index, typename GF::regular_type> make_block_gf_view_from_vector(std::vector<std::string> block_names,
return {{int(V.size())}, std::move(V), nothing{}, nothing{}, nothing{}}; std::vector<GF> V) {
return {{std::move(block_names)}, std::move(V), nothing{}, nothing{}, nothing{}};
} }
// ------------------------------- Extend reinterpret_scalar_valued_gf_as_matrix_valued for block gf ------ // ------------------------------- Extend reinterpret_scalar_valued_gf_as_matrix_valued for block gf ------

View File

@ -528,9 +528,9 @@ namespace gfs {
gf_view(gf_impl<Variable, Target, Opt, false, false> &&g) noexcept : B(std::move(g), bool {}) {} // from a gf && gf_view(gf_impl<Variable, Target, Opt, false, false> &&g) noexcept : B(std::move(g), bool {}) {} // from a gf &&
template <typename D> template <typename D>
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 = "") typename B::indices_t const &ind = typename B::indices_t{}, std::string name = "")
: B(m, factory<typename B::data_t>(dat), t, s, ind, name, typename B::evaluator_t{}) {} : B(m, factory<typename B::data_t>(std::forward<D>(dat)), t, s, ind, name, typename B::evaluator_t{}) {}
friend void swap(gf_view &a, gf_view &b) noexcept { a.swap_impl(b); } friend void swap(gf_view &a, gf_view &b) noexcept { a.swap_impl(b); }

View File

@ -25,7 +25,7 @@ namespace triqs { namespace py_tools {
//--------------------- pyref ----------------------------- //--------------------- 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. // with some useful factories.
class pyref { class pyref {
PyObject * ob = NULL; PyObject * ob = NULL;
@ -272,7 +272,12 @@ template <> struct py_converter<triqs::h5::group> {
template <typename T> struct py_converter<std::vector<T>> { template <typename T> struct py_converter<std::vector<T>> {
static PyObject * c2py(std::vector<T> const &v) { static PyObject * c2py(std::vector<T> const &v) {
PyObject * list = PyList_New(0); PyObject * list = PyList_New(0);
for (auto const & x : v) if (PyList_Append(list, py_converter<T>::c2py(x)) == -1) { Py_DECREF(list); return NULL;} // error for (auto & x : v) if (PyList_Append(list, py_converter<T>::c2py(x)) == -1) { Py_DECREF(list); return NULL;} // error
return list;
}
static PyObject * c2py(std::vector<T> &v) {
PyObject * list = PyList_New(0);
for (auto & x : v) if (PyList_Append(list, py_converter<T>::c2py(x)) == -1) { Py_DECREF(list); return NULL;} // error
return list; return list;
} }
static bool is_convertible(PyObject *ob, bool raise_exception) { static bool is_convertible(PyObject *ob, bool raise_exception) {
@ -374,6 +379,7 @@ template <> struct py_converter<triqs::arrays::range> {
}; };
*/ */
// --- nothing <--> None ---- // --- nothing <--> None ----
#ifdef TRIQS_GF_INCLUDED #ifdef TRIQS_GF_INCLUDED
@ -392,6 +398,52 @@ template<> struct py_converter<triqs::gfs::indices_2> : py_converter_from_reduct
template<bool B> struct py_converter<triqs::gfs::matsubara_domain<B>> : py_converter_from_reductor<triqs::gfs::matsubara_domain<B>>{}; template<bool B> struct py_converter<triqs::gfs::matsubara_domain<B>> : py_converter_from_reductor<triqs::gfs::matsubara_domain<B>>{};
template<> struct py_converter<triqs::gfs::R_domain> : py_converter_from_reductor<triqs::gfs::R_domain>{}; template<> struct py_converter<triqs::gfs::R_domain> : py_converter_from_reductor<triqs::gfs::R_domain>{};
// Converter for Block gf
template <typename... T> struct py_converter<triqs::gfs::gf_view<triqs::gfs::block_index, triqs::gfs::gf<T...>>> {
using gf_type = triqs::gfs::gf<T...>;
using gf_view_type = triqs::gfs::gf_view<T...>;
using c_type = triqs::gfs::gf_view<triqs::gfs::block_index, gf_type>;
static PyObject *c2py(c_type &&g) { return c2py(g);}
static PyObject *c2py(c_type &g) {
// rm the view_proxy
std::vector<gf_view_type> 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<std::vector<std::string>>(names),
convert_from_python<std::vector<gf_view_type>>(gfs));
}
};
#endif #endif
// ---- function ---- // ---- function ----