33#include <itertools/itertools.hpp>
46 template <
typename Mesh,
typename Target = matrix_valued,
typename Layout = nda::C_layout,
int Arity = 1>
class block_gf;
47 template <
typename Mesh,
typename Target = matrix_valued,
typename Layout = nda::C_str
ide_layout,
int Arity = 1,
bool IsConst = false>
59 template <
typename Mesh,
typename Target = matrix_valued,
typename Layout = nda::C_str
ide_layout,
int Arity = 1>
80 template <
typename Mesh,
typename Target = matrix_valued,
typename Layout = nda::C_str
ide_layout>
91 template <
typename Mesh,
typename Target = matrix_valued,
typename Layout = nda::C_str
ide_layout>
100 template <
typename Mesh> block_gf(Mesh
const &,
gf_struct_t const &) -> block_gf<Mesh, matrix_valued>;
103 template <
typename Mesh,
typename Target,
typename Layout,
int Arity,
bool IsConst>
119 template <
typename G,
int n = 0>
inline constexpr bool is_block_gf_v =
false;
122 template <
typename G,
int n>
123 requires(!std::is_same_v<G, std::remove_cvref_t<G>>)
127 template <
typename Mesh,
typename Target,
typename Layout,
int Arity>
131 template <
typename Mesh,
typename Target,
typename Layout,
int Arity,
bool IsConst>
138 template <
typename,
typename = std::
void_t<>>
inline constexpr int arity_of = -1;
141 template <
typename T>
inline constexpr int arity_of<T, std::void_t<
decltype(T::arity)>> = T::arity;
156 template <
int N = 0,
typename BG>
166 template <
typename G>
using get_mesh_t =
typename std::decay_t<G>::mesh_t;
169 template <
typename G>
using get_target_t =
typename std::decay_t<G>::target_t;
204 template <
typename G>
209 template <BlockGf BG>
void mpi_broadcast(BG &&bg, mpi::communicator c = {},
int root = 0);
210 template <BlockGf BG1, BlockGf BG2>
211 void mpi_reduce_into(BG1
const &bg_in, BG2 &&bg_out, mpi::communicator c = {},
int root = 0,
bool all =
false, MPI_Op op = MPI_SUM);
216 template <
typename Lambda,
typename T>
struct lazy_transform_t {
222 template <
typename Lambda,
typename T> lazy_transform_t<Lambda, T> make_lazy_transform(Lambda &&l, T &&x) {
223 return {std::forward<Lambda>(l), std::forward<T>(x)};
230 inline auto _make_block_names1(
int n) {
231 std::vector<std::string> r(n);
236 inline std::vector<std::vector<std::string>> _make_block_names2(
int n,
int p) {
return {_make_block_names1(n), _make_block_names1(p)}; }
259 template <
typename Mesh,
typename Target,
typename Layout,
int Arity>
class block_gf :
TRIQS_CONCEPT_TAG_NAME(BlockGreenFunction) {
260 using this_t = block_gf;
290 using real_t = block_gf<Mesh, typename Target::real_t, Layout, Arity>;
296 using data_t = std::conditional_t<Arity == 1, std::vector<g_t>, std::vector<std::vector<g_t>>>;
299 using block_names_t = std::conditional_t<Arity == 1, std::vector<std::string>, std::vector<std::vector<std::string>>>;
334 if constexpr (Arity == 1) {
335 if (_glist.size() != _block_names.size())
336 TRIQS_RUNTIME_ERROR <<
"block_gf(vector<string>, vector<gf>) : the two vectors do not have the same size !";
338 if (_glist.size() != _block_names[0].size())
339 TRIQS_RUNTIME_ERROR <<
"block2_gf(vector<vector<string>>, vector<vector<gf>>) : Outer vectors have different sizes !";
340 if (_glist.size() != 0)
341 if (_glist[0].
size() != _block_names[1].
size())
342 TRIQS_RUNTIME_ERROR <<
"block2_gf(vector<vector<string>>, vector<vector<gf>>) : Inner vectors have different sizes !";
368 template <
typename G>
372 static_assert(G::arity == Arity,
"Impossible");
382 : _block_names(details::_make_block_names1(V.
size())), _glist(std::move(V)) {}
400 : block_gf(
data_t(n, g)) {}
410 : _block_names(std::move(b)), _glist(_block_names.
size(), g) {}
418 : _block_names(std::move(b)), _glist(_block_names.
size()) {}
433 for (
auto const &[bl_name, bl_size] :
gf_struct) {
434 _block_names.push_back(bl_name);
435 if constexpr (Target::rank == 0)
436 _glist.emplace_back(m);
438 _glist.emplace_back(m, make_shape(bl_size, bl_size));
449 template <
typename Int>
450 block_gf(Mesh
const &m, std::vector<Int>
const &bl_sizes)
451 requires(Arity == 1 && std::is_integral_v<Int>)
454 for (
auto const &[bl, bl_size] : itertools::enumerate(bl_sizes)) {
456 if constexpr (Target::rank == 0)
457 _glist.emplace_back(m);
459 _glist.emplace_back(m, make_shape(bl_size, bl_size));
472 : _block_names(details::_make_block_names2(n, p)), _glist(n, std::vector<
g_t>(p, g)) {}
480 : _block_names(details::_make_block_names2(V.
size(), V[0].
size())), _glist(std::move(V)) {}
511 template <
typename RHS>
512 requires(BlockGreenFunction_v<RHS>)
513 block_gf &
operator=(RHS &&rhs) {
514 if constexpr (Arity == 1) {
515 _glist.resize(rhs.size());
516 _block_names.resize(rhs.size());
519 _block_names.resize(2);
520 _glist.resize(rhs.size1());
521 for (
auto &g_bl : _glist) g_bl.resize(rhs.size2());
522 _block_names[0].resize(rhs.size1());
523 _block_names[1].resize(rhs.size2());
533 friend std::ostream &
operator<<(std::ostream &out, block_gf
const &) {
return out <<
"block_gf"; }
537 template <BlockGf BG>
friend void mpi_broadcast(BG &&, mpi::communicator c,
int root);
538 template <BlockGf BG1, BlockGf BG2>
friend void mpi_reduce_into(BG1
const &, BG2 &&, mpi::communicator,
int,
bool, MPI_Op);
Member code shared by triqs::gfs::block_gf and triqs::gfs::block_gf_view.
data_t & data()
Direct access to the blocks.
block_names_t const & block_names() const
Get the block names.
T factory(U &&...x)
Generic factory to construct an object of a given type from an arbitrary parameter pack of arguments.
A non-owning view of a block Green's function.
The owning block Green's function container.
block_gf()=default
Construct an empty block Green's function (with no blocks).
int size() const
Get the total number of blocks.
block_gf(int n)
Construct a one-index block_gf of n default-constructed blocks.
block_gf_view< M, T, typename nda::C_layout::with_lowest_guarantee_t, Arity > mutable_view_type
block_gf(int n, g_t const &g)
Construct a one-index block_gf of n copies of a Green's function.
block_gf(Mesh const &m, std::vector< Int > const &bl_sizes)
Construct a one-index block_gf from a mesh and a vector of block sizes (block names default to "0",...
static constexpr int arity
std::conditional_t< Arity==1, std::vector< std::string >, std::vector< std::vector< std::string > > > block_names_t
block_gf< M, typename T::real_t, nda::C_layout, Arity > real_t
block_gf(data_t V)
Construct a one-index block_gf from a vector of Green's functions (block names default to "0",...
block_gf_view< M, T, typename nda::C_layout::with_lowest_guarantee_t, Arity, false > view_type
block_gf(block_names_t b)
Construct a one-index block_gf with the given block names and default-constructed blocks.
block_gf(block_gf &&)=default
Move constructor.
block_gf< M, T, nda::C_layout, Arity > regular_type
block_gf(G const &x)
Construct from any object modeling the BlockGreenFunction concept with a compatible target.
static constexpr bool is_view
block_gf_view< M, T, typename nda::C_layout::with_lowest_guarantee_t, Arity, true > const_view_type
block_gf(block_names_t b, g_t const &g)
Construct a one-index block_gf from block names and one Green's function copied into every block.
block_gf & operator=(block_gf const &rhs)=default
Copy assignment.
block_gf & operator=(block_gf &&rhs)=default
Move assignment.
gf< M, T, nda::C_layout > g_t
block_gf(block_names_t b, data_t d)
Construct from a list of block names and a list of Green's functions.
static constexpr bool is_const
block_gf(block_gf_view< Mesh, Target, L, Arity, Cnst > const &g)
Construct from a view of the same kind, making a deep copy of the data.
block_gf(int n, int p, g_t const &g)
Construct a two-index block2_gf of n x p copies of a Green's function.
std::conditional_t< Arity==1, std::vector< g_t >, std::vector< std::vector< g_t > > > data_t
block_gf(data_t V)
Construct a two-index block2_gf from a matrix of Green's functions.
block_gf(Mesh const &m, gf_struct_t const &gf_struct)
Construct a one-index block_gf from a mesh and a triqs::gfs::gf_struct_t.
gf_struct_t gf_struct() const
friend std::ostream & operator<<(std::ostream &out, block_gf const &)
Writing a block Green's function to an output stream is not supported.
block_gf(block_gf const &x)=default
Copy constructor.
The owning Green's function container.
Concept checking that a type is a block Green's function.
TRIQS exception hierarchy and related macros.
Generic factory for constructing objects of a given type.
Provides the type describing the block structure of a block Green's function.
Provides the Green's function class.
block_gf_view< Mesh, Target, Layout, Arity, true > block_gf_const_view
Const view alias for a block Green's function.
block_gf_view< Mesh, Target, Layout, 2, false > block2_gf_view
Mutable view of a two-index block Green's function.
block_gf_view< Mesh, Target, Layout, 2, true > block2_gf_const_view
Const view of a two-index block Green's function.
std::vector< std::pair< std::string, long > > gf_struct_t
Type describing the structure of a block Green's function.
block_gf< Mesh, Target, Layout, 2 > block2_gf
Owning two-index block Green's function (a matrix of blocks).
void mpi_broadcast(BG &&bg, mpi::communicator c={}, int root=0)
Implementation of an MPI broadcast for triqs::gfs::block_gf and triqs::gfs::block_gf_view types.
void mpi_reduce_into(BG1 const &bg_in, BG2 &&bg_out, mpi::communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for triqs::gfs::block_gf and triqs::gfs::block_gf_view types that red...
typename std::decay_t< G >::target_t get_target_t
The target type of a block Green's function type G.
typename std::decay_t< G >::mesh_t get_mesh_t
The mesh type of a block Green's function type G.
block_gf< get_mesh_t< G >, get_target_t< G > > block_gf_of
The triqs::gfs::block_gf type matching the mesh and target of G.
block2_gf_const_view< get_mesh_t< G >, get_target_t< G > > block2_gf_const_view_of
The triqs::gfs::block2_gf_const_view type matching the mesh and target of G.
block2_gf_view< get_mesh_t< G >, get_target_t< G > > block2_gf_view_of
The triqs::gfs::block2_gf_view type matching the mesh and target of G.
constexpr bool is_block_gf_v
Trait to check whether a type is a block Green's function.
constexpr int arity_of
Block arity of a type: T::arity if present, -1 otherwise.
block_gf_const_view< get_mesh_t< G >, get_target_t< G > > block_gf_const_view_of
The triqs::gfs::block_gf_const_view type matching the mesh and target of G.
block2_gf< get_mesh_t< G >, get_target_t< G > > block2_gf_of
The triqs::gfs::block2_gf type matching the mesh and target of G.
auto const & get_mesh(BG const &bg)
Get the mesh of a block Green's function, or its N-th component for a product mesh.
block_gf_view< get_mesh_t< G >, get_target_t< G > > block_gf_view_of
The triqs::gfs::block_gf_view type matching the mesh and target of G.
#define TRIQS_RUNTIME_ERROR
Throw a triqs::runtime_error with the current source location.
#define TRIQS_DEFINE_CONCEPT_AND_ASSOCIATED_TRAIT(MyBeautifulConcept)
Define a tag-based pseudo-concept.
#define TRIQS_CONCEPT_TAG_NAME(MyBeautifulConcept)
Helper macro that produces the name of the tag for TRIQS_DEFINE_CONCEPT_AND_ASSOCIATED_TRAIT.
T factory(U &&...x)
Generic factory to construct an object of a given type from an arbitrary parameter pack of arguments.
string to_string(string const &str)
Identity overload for std::string.
Common macros used in TRIQS.
Provides the target types that fix the value stored at each mesh point of a Green's function.