mirror of
https://github.com/triqs/dft_tools
synced 2024-12-25 13:53:40 +01:00
add converter for scalar_valued gf
- converted as 1x1 matrix view. - add a small test
This commit is contained in:
parent
16057c7a4e
commit
6934692327
@ -24,3 +24,23 @@ void pass_bgf(block_gf_view<imfreq> g) {
|
||||
h5_write(file, "g", g);
|
||||
}
|
||||
}
|
||||
|
||||
// scalar gf
|
||||
|
||||
gf_view<imfreq,scalar_valued> make_sgf(double a) {
|
||||
double beta = 1;
|
||||
auto G1 = gf<imfreq, scalar_valued>({beta, Fermion});
|
||||
{
|
||||
H5::H5File file("ess_test_g3a.h5", H5F_ACC_TRUNC);
|
||||
h5_write(file, "g", G1);
|
||||
}
|
||||
return G1;
|
||||
}
|
||||
|
||||
void pass_sgf(gf_view<imfreq,scalar_valued> g) {
|
||||
|
||||
{
|
||||
H5::H5File file("ess_test_g3b.h5", H5F_ACC_TRUNC);
|
||||
h5_write(file, "g", g);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,10 @@ 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")
|
||||
|
||||
module.add_function (name = "make_sgf", signature = "gf_view<imfreq,scalar_valued> (double a)", doc = "DOC of print_a")
|
||||
module.add_function (name = "pass_sgf", signature = "void (gf_view<imfreq,scalar_valued> 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])
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
#pragma once
|
||||
#include <Python.h>
|
||||
#include "structmember.h"
|
||||
@ -27,7 +26,6 @@ namespace triqs { namespace py_tools {
|
||||
//--------------------- pyref -----------------------------
|
||||
|
||||
// a little class to hold an owned ref, make sure it is decref at destruction
|
||||
// with some useful factories.
|
||||
class pyref {
|
||||
PyObject * ob = NULL;
|
||||
public:
|
||||
@ -46,9 +44,6 @@ class pyref {
|
||||
pyref(pyref && p){ ob = p.ob; p.ob=NULL;}
|
||||
pyref& operator =(pyref const&) = delete;
|
||||
pyref& operator =(pyref &&p) {ob = p.ob; p.ob=NULL; return *this;}
|
||||
// factories. No public constructor.
|
||||
// The point is that PyObject is borrowed or new, depending on the function that produced it
|
||||
// Need to check in the API doc for each case.
|
||||
static pyref module(std::string const &module_name) { return PyImport_ImportModule(module_name.c_str()); }
|
||||
static pyref string(std::string const &s) { return PyString_FromString(s.c_str());}
|
||||
};
|
||||
@ -58,18 +53,19 @@ class pyref {
|
||||
//--------------------- py_converters -----------------------------
|
||||
|
||||
// default version for a wrapped type. To be specialized later.
|
||||
// py2c behaviour is undefined is is_convertible return false
|
||||
template<typename T> struct py_converter;
|
||||
//{
|
||||
// static PyObject * c2py(T const & x);
|
||||
// static T & py2c(PyObject * ob);
|
||||
// static bool is_convertible(PyObject * ob, bool raise_exception);
|
||||
//
|
||||
//}
|
||||
|
||||
// We only use these functions in the code, not converter
|
||||
// TODO : Does c2py return NULL in failure ? Or is it undefined...
|
||||
template <typename T> static PyObject *convert_to_python(T &&x) {
|
||||
return py_converter<typename std::decay<T>::type>::c2py(std::forward<T>(x));
|
||||
}
|
||||
// can convert_from_python raise a triqs exception ? NO
|
||||
template<typename T> static auto convert_from_python(PyObject * ob) -> decltype(py_converter<T>::py2c(ob)) { return py_converter<T>::py2c(ob);}
|
||||
template <typename T> static bool convertible_from_python(PyObject *ob, bool raise_exception) {
|
||||
return py_converter<T>::is_convertible(ob, raise_exception);
|
||||
@ -371,19 +367,6 @@ template <> struct py_converter<triqs::arrays::range> {
|
||||
}
|
||||
};
|
||||
|
||||
// --- gf<U...> ----
|
||||
/*template <typename ...U > struct py_converter<triqs::gfs::gf<U...>> {
|
||||
using conv = py_converter<triqs::gfs::gf_view<U...>>;
|
||||
static PyObject *c2py(triqs::gfs::gf<U...> &g) { return conv::c2py(g); }
|
||||
static PyObject *c2py(triqs::gfs::gf<U...> &&g) { return conv::c2py(g); }
|
||||
static bool is_convertible(PyObject *ob, bool raise_exception) {
|
||||
return conv::is_convertible(ob,raise_exception);
|
||||
}
|
||||
static triqs::gfs::gf<U...> py2c(PyObject *ob) { return conv::py2c(ob); }
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
// --- nothing <--> None ----
|
||||
#ifdef TRIQS_GF_INCLUDED
|
||||
|
||||
@ -409,9 +392,7 @@ template <typename... T> struct py_converter<triqs::gfs::gf_view<triqs::gfs::blo
|
||||
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) {
|
||||
static PyObject *c2py(c_type g) {
|
||||
// rm the view_proxy
|
||||
std::vector<gf_view_type> vg;
|
||||
vg.reserve(g.data().size());
|
||||
@ -448,6 +429,29 @@ template <typename... T> struct py_converter<triqs::gfs::gf_view<triqs::gfs::blo
|
||||
}
|
||||
};
|
||||
|
||||
// Converter for scalar_valued gf : reinterpreted as 1x1 matrix
|
||||
template <typename Variable, typename Opt> struct py_converter<triqs::gfs::gf_view<Variable, triqs::gfs::scalar_valued, Opt>>{
|
||||
|
||||
using conv = py_converter<triqs::gfs::gf_view<Variable, triqs::gfs::matrix_valued, Opt>>;
|
||||
using c_t = triqs::gfs::gf_view<Variable, triqs::gfs::scalar_valued, Opt>;
|
||||
|
||||
static PyObject *c2py(c_t g) {
|
||||
return conv::c2py(reinterpret_scalar_valued_gf_as_matrix_valued(g));
|
||||
}
|
||||
|
||||
static bool is_convertible(PyObject *ob, bool raise_exception) {
|
||||
if (!conv::is_convertible(ob,raise_exception)) return false;
|
||||
auto g = conv::py2c(ob); // matrix view
|
||||
if (get_target_shape(g) == make_shape(1,1)) return true;
|
||||
if (raise_exception) PyErr_SetString(PyExc_RuntimeError,"The green function is not of dimension 1x1 : can not be reinterpreted as a scalar_valued Green function");
|
||||
return false;
|
||||
}
|
||||
|
||||
static c_t py2c(PyObject *ob) {
|
||||
return slice_target_to_scalar(conv::py2c(ob),0,0);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// ---- function ----
|
||||
|
Loading…
Reference in New Issue
Block a user