/******************************************************************************* * * TRIQS: a Toolbox for Research in Interacting Quantum Systems * * Copyright (C) 2012 by M. Ferrero, O. Parcollet * * TRIQS is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * TRIQS. If not, see . * ******************************************************************************/ #ifndef TRIQS_GF_BLOCK_H #define TRIQS_GF_BLOCK_H #include "./tools.hpp" #include "./gf.hpp" #include "./local/tail.hpp" #include "./meshes/discrete.hpp" namespace triqs { namespace gfs { struct block_index {}; template struct gf_mesh : discrete_mesh { typedef discrete_mesh B; gf_mesh() = default; gf_mesh(int s) : B(s) {} gf_mesh(discrete_domain const &d) : B(d) {} gf_mesh(std::initializer_list const &s) : B(s) {} }; namespace gfs_implementation { /// --------------------------- hdf5 --------------------------------- template struct h5_name { static std::string invoke() { return "BlockGf"; } }; template struct h5_rw { static void write(h5::group gr, gf_const_view g) { for (size_t i = 0; i < g.mesh().size(); ++i) h5_write(gr, g.mesh().domain().names()[i], g._data[i]); // h5_write(gr,"symmetry",g._symmetry); } template static void read(h5::group gr, gf_impl &g) { // does not work : need to read the block name and remake the mesh... g._mesh = gf_mesh(gr.get_all_subgroup_names()); g._data.resize(g._mesh.size()); // if (g._data.size() != g._mesh.size()) TRIQS_RUNTIME_ERROR << "h5 read block gf : number of block mismatch"; for (size_t i = 0; i < g.mesh().size(); ++i) h5_read(gr, g.mesh().domain().names()[i], g._data[i]); // h5_read(gr,"symmetry",g._symmetry); } }; /// --------------------------- data access --------------------------------- template struct data_proxy : data_proxy_vector::type> {}; // ------------------------------- Factories -------------------------------------------------- template struct factories { typedef gf_mesh mesh_t; typedef gf gf_t; typedef gf_view gf_view_t; struct target_shape_t {}; static typename gf_t::data_t make_data(mesh_t const &m, target_shape_t) { return std::vector(m.size()); } static typename gf_t::singularity_t make_singularity(mesh_t const &m, target_shape_t) { return {}; } }; } // gfs_implementation // ------------------------------- aliases -------------------------------------------------- template using block_gf = gf>; template using block_gf_view = gf_view>; template using block_gf_const_view = gf_const_view>; // ------------------------------- Free Factories for regular type -------------------------------------------------- // from a number and a gf to be copied template block_gf make_block_gf(int n, gf const &g) { auto V = std::vector>{}; for (int i = 0; i < n; ++i) V.push_back(g); return {{n}, std::move(V), nothing{}, nothing{}, nothing{}}; } // from a vector of gf (moving directly) template block_gf make_block_gf(std::vector> V) { return {{int(V.size())}, std::move(V), nothing{}, nothing{}, nothing{}}; } // from a vector of gf : generalized to have a different type of gf in the vector (e.g. views...) template block_gf make_block_gf(std::vector const &V) { auto V2 = std::vector>{}; for (auto const &g : V) V2.push_back(g); return {{int(V.size())}, std::move(V2), nothing{}, nothing{}, nothing{}}; } // from a init list of GF with the correct type template block_gf make_block_gf(std::initializer_list> const &V) { return {{int(V.size())}, V, nothing{}, nothing{}, nothing{}}; } // from vector and a gf to be copied template block_gf make_block_gf(std::vector block_names, gf const &g) { auto V = std::vector>{}; for (int i = 0; i < block_names.size(); ++i) V.push_back(g); return {{block_names}, std::move(V), nothing{}, nothing{}, nothing{}}; } // from vector, vector template block_gf make_block_gf(std::vector block_names, std::vector> V) { if (block_names.size() != V.size()) TRIQS_RUNTIME_ERROR << "make_block_gf(vector, vector) : the two vectors do not have the same size !"; return {{block_names}, std::move(V), nothing{}, nothing{}, nothing{}}; } // from vector, init_list template block_gf make_block_gf(std::vector block_names, std::initializer_list> const &V) { if (block_names.size() != V.size()) TRIQS_RUNTIME_ERROR << "make_block_gf(vector, init_list) : size mismatch !"; return {{block_names}, V, nothing{}, nothing{}, nothing{}}; } // ------------------------------- Free Factories for view type -------------------------------------------------- template gf_view::type::view_type> make_block_gf_view(G0 &&g0, G &&... g) { auto V = std::vector::type::view_type>{std::forward(g0), std::forward(g)...}; return {{int(V.size())}, std::move(V), nothing{}, nothing{}, nothing{}}; // return { gf_mesh {int(V.size())}, std::move(V), nothing{}, nothing{} } ; } template gf_view make_block_gf_view_from_vector(std::vector V) { 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{}}; } // ------------------------------- Extend reinterpret_scalar_valued_gf_as_matrix_valued for block gf ------ template gf_view, Opt2, IsConst> reinterpret_scalar_valued_gf_as_matrix_valued(gf_view, Opt2, IsConst> bg) { std::vector> V; for (auto &g : bg) V.push_back(reinterpret_scalar_valued_gf_as_matrix_valued(g)); return make_block_gf_view_from_vector(std::move(V)); } template gf_const_view, Opt2> reinterpret_scalar_valued_gf_as_matrix_valued(gf, Opt2> const &bg) { return reinterpret_scalar_valued_gf_as_matrix_valued(bg()); } template gf_view, Opt2> reinterpret_scalar_valued_gf_as_matrix_valued(gf, Opt2> &bg) { return reinterpret_scalar_valued_gf_as_matrix_valued(bg()); } // ------------------------------- Free functions -------------------------------------------------- // a simple function to get the number of blocks template size_t n_blocks(gf const &g) { return g.mesh().size(); } template size_t n_blocks(gf_view const &g) { return g.mesh().size(); } // ------------------------------- an iterator over the blocks -------------------------------------------------- template using __get_target = typename std::remove_reference()[0])>::type; // iterator template class block_gf_iterator : public boost::iterator_facade, __get_target, boost::forward_traversal_tag, __get_target &> { friend class boost::iterator_core_access; typedef typename std::remove_reference::type big_gf_t; big_gf_t &big_gf; typedef typename big_gf_t::mesh_t::const_iterator mesh_iterator_t; mesh_iterator_t mesh_it; __get_target &dereference() const { return big_gf[*mesh_it]; } bool equal(block_gf_iterator const &other) const { return ((mesh_it == other.mesh_it)); } public: block_gf_iterator(big_gf_t &bgf, bool at_end = false) : big_gf(bgf), mesh_it(&big_gf.mesh(), at_end) {} void increment() { ++mesh_it; } bool at_end() const { return mesh_it.at_end(); } }; //------------ template block_gf_iterator> begin(gf_impl &bgf) { return {bgf, false}; } //------------ template block_gf_iterator> end(gf_impl &bgf) { return {bgf, true}; } //----- const iterator template class block_gf_const_iterator : public boost::iterator_facade, __get_target, boost::forward_traversal_tag, __get_target const &> { friend class boost::iterator_core_access; typedef typename std::remove_reference::type big_gf_t; big_gf_t const &big_gf; typedef typename big_gf_t::mesh_t::const_iterator mesh_iterator_t; mesh_iterator_t mesh_it; __get_target const &dereference() const { return big_gf[*mesh_it]; } bool equal(block_gf_const_iterator const &other) const { return ((mesh_it == other.mesh_it)); } public: block_gf_const_iterator(big_gf_t const &bgf, bool at_end = false) : big_gf(bgf), mesh_it(&big_gf.mesh(), at_end) {} void increment() { ++mesh_it; } bool at_end() const { return mesh_it.at_end(); } }; template block_gf_const_iterator> begin(gf_impl const &bgf) { return {bgf, false}; } template block_gf_const_iterator> end(gf_impl const &bgf) { return {bgf, true}; } template block_gf_const_iterator> cbegin(gf_impl const &bgf) { return {bgf, false}; } template block_gf_const_iterator> cend(gf_impl const &bgf) { return {bgf, true}; } } } #endif