TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
gf.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2023 Simons Foundation
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You may obtain a copy of the License at
14// https://www.gnu.org/licenses/gpl-3.0.txt
15//
16// Authors: Michel Ferrero, Olivier Parcollet, Nils Wentzell
17
22
23#pragma once
24
25#include "./defs.hpp"
26#include "./targets.hpp"
28#include "../../utility/exceptions.hpp" // IWYU pragma: keep (used by ./_gf_view_common.hpp)
29#include "../../utility/macros.hpp" // IWYU pragma: keep (used by ./_gf_view_common.hpp)
30
31#include <itertools/itertools.hpp>
32#include <mpi/mpi.hpp>
33#include <nda/nda.hpp>
34
35#include <array>
36#include <concepts>
37#include <numeric> // IWYU pragma: keep (used by ./_gf_view_common.hpp)
38#include <ostream> // IWYU pragma: keep (used by ./_gf_view_common.hpp)
39#include <string> // IWYU pragma: keep (used by ./_gf_view_common.hpp)
40#include <tuple> // IWYU pragma: keep (used by ./_gf_view_common.hpp)
41#include <type_traits>
42#include <utility>
43
44namespace triqs::gfs {
45
46 // Elevate certain types into the triqs::gfs namespace.
47 using nda::C_layout;
48 using nda::C_stride_layout;
50 // Forward declarations.
51 template <Mesh M, typename Target = matrix_valued, typename Layout = C_layout> class gf;
52 template <Mesh M, typename Target = matrix_valued, typename Layout = C_stride_layout> class gf_view;
53 template <Mesh M, typename Target = matrix_valued, typename Layout = C_stride_layout> class gf_const_view;
54
59
60 // Trait and concept tag marking a type as a Green's function.
62
64
72 template <template <typename...> class TMPLT, typename T> struct is_instantiation_of : std::false_type {};
73
74 // Specialization of is_instantiation_of for a matching instantiation.
75 template <template <typename...> class TMPLT, typename... U> struct is_instantiation_of<TMPLT, TMPLT<U...>> : std::true_type {};
76
83 template <template <typename...> class gf, typename T>
85
86 //template <typename G> using mesh_t_of = std::decay_t<decltype(std::declval<G>().mesh())>;
87
93 *
94 * When a second template argument `M` is supplied, it additionally requires the mesh type of `G` to be `M`.
95 *
96 * @deprecated In favour of the triqs::gfs::MemoryGf concept.
97 *
98 * @tparam G Type to test.
99 * @tparam M Optional mesh type to require.
100 */
101 template <typename G, typename M = void> inline constexpr bool is_gf_v = false;
102
103 // Specialization of is_gf_v for cvref-qualified types, which decays G first.
104 template <typename G, typename M>
105 requires(!std::is_same_v<G, std::remove_cvref_t<G>>)
106 inline constexpr bool is_gf_v<G, M> = is_gf_v<std::remove_cvref_t<G>, M>;
107
108 // Specialization of is_gf_v without a mesh constraint.
109 template <typename G>
110 inline constexpr bool is_gf_v<G, void> =
112
113 // Specialization of is_gf_v requiring the mesh type of G.
114 template <typename G> inline constexpr bool is_gf_v<G, typename std::remove_cvref_t<G>::mesh_t> = is_gf_v<G, void>;
115
117
132 template <typename G, typename M = typename std::remove_cvref_t<G>::mesh_t>
133 concept MemoryGf = mesh::Mesh<M> and requires(G g) {
134 { g.data() } -> nda::MemoryArray;
135 requires std::same_as<std::decay_t<decltype(g.mesh())>, M>;
136 };
137
138 // Forward declarations with default arguments. The corresponding friend declarations inside `gf` and
139 // the definitions in `./mpi.hpp` may then re-declare these templates without re-introducing defaults.
140 template <MemoryGf G> void mpi_broadcast(G &&g, mpi::communicator c = {}, int root = 0);
141 template <MemoryGf G1, MemoryGf G2>
142 void mpi_reduce_into(G1 const &g_in, G2 &&g_out, mpi::communicator c = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM);
143
148
150 template <typename G> using get_target_t = typename std::decay_t<G>::target_t;
151
160 template <int N = 0, MemoryGf G> auto const &get_mesh(G const &g) {
162 return std::get<N>(g.mesh());
163 else
164 return g.mesh();
165 }
166
168 template <MemoryGf G> using target_value_t = decltype(std::declval<G>()[std::declval<typename G::mesh_point_t>()]);
169
170 /** @} */
171
172 // Tags used internally to select implementation-detail constructors.
173 struct impl_tag {};
174 struct impl_tag2 {};
175
178 * @brief The owning Green's function container.
179 *
180 * @details triqs::gfs::gf is the central, value-semantic container of the Green's function module. It owns a mesh
181 * and a data array, and represents a function defined on the mesh whose value at each mesh point is fixed by the
182 * `Target` type (see triqs::gfs::scalar_valued, triqs::gfs::matrix_valued, triqs::gfs::tensor_valued).
183 *
184 * The data array has rank `arity + Target::rank`, where `arity` is the number of mesh dimensions. The function can
185 * be accessed at a mesh point with `operator[]` and evaluated at an arbitrary point with `operator()` (using the
186 * interpolation scheme of the mesh).
187 *
188 * Non-owning views are provided by triqs::gfs::gf_view and triqs::gfs::gf_const_view.
189 *
190 * @tparam M Mesh type, modeling triqs::mesh::Mesh; fixes the domain of definition.
191 * @tparam Target Target type; fixes the value stored at each mesh point.
192 * @tparam Layout Memory layout policy of the data array.
193 */
194 template <Mesh M, typename Target, typename Layout> class gf : TRIQS_CONCEPT_TAG_NAME(GreenFunction) {
195
196 static_assert(not std::is_same_v<M, triqs::lattice::brillouin_zone>,
197 "Since TRIQS 2.3, brillouin_zone is replaced by mesh::brzone as a mesh name. Cf Doc, changelog");
199 using this_t = gf<M, Target, Layout>; // used in common code
200
201 public:
203 static constexpr bool is_view = false;
204
206 static constexpr bool is_const = false;
207
213
216
219
227 using target_t = Target;
228
230 using mesh_t = M;
231
233 using mesh_point_t = typename mesh_t::mesh_point_t;
234
236 using mesh_index_t = typename mesh_t::index_t;
239 using scalar_t = typename Target::scalar_t;
240
242 static constexpr int arity = n_variables<M>;
243
245 static constexpr int target_rank = Target::rank;
246
248 static constexpr int data_rank = arity + Target::rank;
249
251 using data_t = nda::basic_array<scalar_t, data_rank, Layout, 'A', nda::heap<>>;
252
254 using target_shape_t = std::array<long, Target::rank>;
255
260
262 using target_t = Target;
263
265 target_shape_t const &shape() const { return _shape; }
266 };
267
268 // ------------- Accessors -----------------------------
269
274 mesh_t const &mesh() const { return _mesh; }
275
280 data_t &data() & { return _data; }
281
286 data_t const &data() const & { return _data; }
287
292 data_t data() && { return std::move(_data); }
293
302 auto const &data_shape() const { return _data.shape(); }
303
308 target_and_shape_t target() const { return target_and_shape_t{stdutil::front_mpop<arity>(_data.shape())}; } // drop arity dims
309
314 std::array<long, Target::rank> target_shape() const { return target().shape(); } // drop arity dims
315
320 auto target_indices() const { return itertools::product_range(target().shape()); }
321
322 private:
323 mesh_t _mesh;
324 data_t _data;
325
326 // -------------------------------- impl. details common to all classes -----------------------------------------------
327
328 public:
330 gf() = default; // {}
331
336 gf(gf const &x) = default;
337
339 gf(gf &&) = default;
340
341 private:
342 void swap_impl(gf &b) noexcept {
343 using std::swap;
344 swap(this->_mesh, b._mesh);
345 swap(this->_data, b._data);
346 }
347
348 private:
349 // Compute the shape of the data array from the mesh and the target shape.
350 static auto make_data_shape(mesh_t const &m, target_shape_t const &shape) {
351 if constexpr (mesh::is_product<mesh_t>)
352 return stdutil::join(m.size_of_components(), shape);
353 else
354 return stdutil::front_append(shape, m.size());
355 }
356
357 public:
366 gf(mesh_t m, data_t dat) : _mesh(std::move(m)), _data(std::move(dat)) {}
367
376 gf(mesh_t m, target_shape_t shape = {}) : _mesh(std::move(m)), _data(make_data_shape(_mesh, shape)) {}
377
382 explicit gf(gf_view<M, Target> const &g) : _mesh(g.mesh()), _data(g.data()) {}
383
388 explicit gf(gf_const_view<M, Target> const &g) : _mesh(g.mesh()), _data(g.data()) {}
389
399 template <typename G>
400 explicit gf(G const &g)
401 requires(GreenFunction<G>::value and std::is_same_v<mesh_t, typename G::mesh_t>)
402 : gf() {
403 *this = g;
404 } // explicit is very important here.
405 // TODO: We would like to refine this, G should have the same mesh, target, at least ...
406
407 // --------------- Operator = --------------------
408
414 gf &operator=(gf const &rhs) = default;
415
421 gf &operator=(gf &&rhs) noexcept {
422 this->swap_impl(rhs);
423 return *this;
424 }
425
437 template <typename RHS>
438 gf &operator=(RHS &&rhs) // NOLINT
439 requires(GreenFunction<RHS>::value and not std::is_same_v<std::decay_t<RHS>, gf>)
440 {
441 _mesh = rhs.mesh();
442 _data.resize(rhs.data_shape());
443 for (auto w : _mesh) (*this)[w] = rhs[w];
444 return *this;
445 }
446
447 // other = late, cf MPI
448
449 public:
450 // ------------- apply_on_data -----------------------------
451
462 template <typename Fdata> auto apply_on_data(Fdata &&fd) {
463 auto d2 = std::forward<Fdata>(fd)(_data);
464 using t2 = target_from_array<decltype(d2), arity>;
465 using gv_t = gf_view<M, t2>;
466 return gv_t{mesh(), d2};
467 }
468
479 template <typename Fdata> auto apply_on_data(Fdata &&fd) const {
480 auto d2 = std::forward<Fdata>(fd)(_data);
481 using t2 = target_from_array<decltype(d2), arity>;
482 using gv_t = gf_const_view<M, t2>;
483 return gv_t{mesh(), d2};
484 }
485
486 // Friend declarations (hidden from doxygen; documented as free functions in gf/mpi.hpp).
488 template <MemoryGf G> friend void mpi_broadcast(G &&, mpi::communicator, int root);
489 template <MemoryGf G1, MemoryGf G2> friend void mpi_reduce_into(G1 const &, G2 &&, mpi::communicator, int, bool, MPI_Op);
491
492 // Common code for gf, gf_view, gf_const_view
493#include "./_gf_view_common.hpp"
494 };
495
496 /*------------------------------------------------------------------------
497 * Deduction guides
498 *-----------------------------------------------------------------------*/
499
501 template <Mesh M, nda::MemoryArray DataArray> gf(M, DataArray) -> gf<M, target_from_array<DataArray, n_variables<M>>>;
502
504 template <Mesh M, std::integral I, size_t R> gf(M, std::array<I, R>) -> gf<M, typename _target_from_type_rank<dcomplex, R>::type>;
505
507 template <Mesh M> gf(M) -> gf<M, scalar_valued>;
508
509 // Forward declare gf_expr
510 template <typename Tag, typename L, typename R> struct gf_expr;
511
513 template <typename Tag, typename L, typename R>
515
517 template <typename Tag, typename L, typename R>
519
520} // namespace triqs::gfs
Member code shared by triqs::gfs::gf, triqs::gfs::gf_view and triqs::gfs::gf_const_view.
A read-only, non-owning view of a Green's function.
A mutable, non-owning view of a Green's function.
Definition gf_view.hpp:48
The owning Green's function container.
Definition gf.hpp:194
gf< M, typename T::complex_t, Layout > complex_t
Definition gf.hpp:224
typename mesh_t::mesh_point_t mesh_point_t
Definition gf.hpp:233
static constexpr int data_rank
Definition gf.hpp:248
gf(gf_const_view< M, Target > const &g)
Construct from a const view, making a deep copy of the data.
Definition gf.hpp:388
gf_view< M, T, typename Layout::with_lowest_guarantee_t > view_type
Definition gf.hpp:215
static constexpr int target_rank
Definition gf.hpp:245
gf(gf_view< M, Target > const &g)
Construct from a view, making a deep copy of the data.
Definition gf.hpp:382
nda::basic_array< scalar_t, data_rank, Layout, 'A', nda::heap<> > data_t
Definition gf.hpp:251
gf_view< M, T, typename Layout::with_lowest_guarantee_t > mutable_view_type
Definition gf.hpp:209
data_t & data() &
Get the data array.
Definition gf.hpp:280
data_t data() &&
Get the data array (rvalue overload).
Definition gf.hpp:292
target_and_shape_t target() const
Get a handle to the target and its shape.
Definition gf.hpp:308
static constexpr bool is_const
Definition gf.hpp:206
typename mesh_t::index_t mesh_index_t
Definition gf.hpp:236
gf & operator=(gf const &rhs)=default
Copy assignment.
std::array< long, T::rank > target_shape_t
Definition gf.hpp:254
auto const & data_shape() const
Get the shape of the data array.
Definition gf.hpp:302
gf< M, T, Layout > regular_type
Definition gf.hpp:218
typename T::scalar_t scalar_t
Definition gf.hpp:239
gf_const_view< M, T, typename Layout::with_lowest_guarantee_t > const_view_type
Definition gf.hpp:212
gf(gf const &x)=default
Copy constructor.
gf(mesh_t m, data_t dat)
Construct from a mesh and a data array.
Definition gf.hpp:366
auto apply_on_data(Fdata &&fd) const
Build a const view of a Green's function whose data is the result of applying a function to the data ...
Definition gf.hpp:479
gf(G const &g)
Construct from any object modeling the GreenFunction concept with the same mesh type.
Definition gf.hpp:400
auto target_indices() const
Get a generator over the multi-indices of the target space.
Definition gf.hpp:320
auto apply_on_data(Fdata &&fd)
Build a view of a Green's function whose data is the result of applying a function to the data array.
Definition gf.hpp:462
mesh_t const & mesh() const
Get the mesh of the Green's function.
Definition gf.hpp:274
gf< M, typename T::real_t, Layout > real_t
Definition gf.hpp:221
gf(gf &&)=default
Move constructor.
gf & operator=(RHS &&rhs)
Assign from any object modeling the GreenFunction concept.
Definition gf.hpp:438
static constexpr bool is_view
Definition gf.hpp:203
static constexpr int arity
Definition gf.hpp:242
gf & operator=(gf &&rhs) noexcept
Move assignment.
Definition gf.hpp:421
std::array< long, Target::rank > target_shape() const
Get the shape of the target.
Definition gf.hpp:314
data_t const & data() const &
Get the data array (const overload).
Definition gf.hpp:286
gf(mesh_t m, target_shape_t shape={})
Construct from a mesh and a target shape.
Definition gf.hpp:376
Macros that define a legacy (pre C++20) concept-tag trait pair.
Concept checking that a type behaves like an in-memory Green's function.
Definition gf.hpp:133
Concept for a mesh.
Definition concepts.hpp:85
Provides common type aliases, forward declarations and internal helpers for the Green's function cont...
static constexpr int n_variables
Constexpr variable that holds the number of meshes in a triqs::mesh::Mesh type ( for non-product mes...
Definition utils.hpp:154
TRIQS exception hierarchy and related macros.
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.
Definition mpi.hpp:79
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...
Definition mpi.hpp:124
typename _target_from_type_rank< typename std::decay_t< typename std::decay_t< A >::value_type >, std::decay_t< A >::rank - nvar >::type target_from_array
Deduce the target type of a Green's function from its data array type and arity.
Definition targets.hpp:259
typename std::decay_t< G >::target_t get_target_t
The target type of a block Green's function type G.
Definition block_gf.hpp:169
decltype(std::declval< G >()[std::declval< typename G::mesh_point_t >()]) target_value_t
The type of value obtained when accessing a Green's function of type G with a mesh point.
Definition gf.hpp:168
constexpr bool is_gf_v
Trait to check whether a type models the Green's function concept.
Definition gf.hpp:101
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.
Definition block_gf.hpp:158
constexpr bool is_instantiation_of_v
Variable template for triqs::gfs::is_instantiation_of, decaying the tested type first.
Definition gf.hpp:84
static constexpr bool is_product
Constexpr bool that is true if the given triqs::mesh::Mesh type is a product of meshes,...
Definition utils.hpp:151
#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.
Common macros used in TRIQS.
Lightweight handle bundling the target type and its shape.
Definition gf.hpp:257
Target target_t
The target type.
Definition gf.hpp:262
target_shape_t _shape
The shape of the target.
Definition gf.hpp:259
target_shape_t const & shape() const
Get the shape of the target.
Definition gf.hpp:265
Lazy expression node representing a binary operation between two Green's function operands.
Definition gf_expr.hpp:172
typename gfs_expr_tools::_or_< typename L_t::target_t, typename R_t::target_t >::type target_t
Target type of the expression, deduced from the two operands.
Definition gf_expr.hpp:183
Trait to detect whether a type is an instantiation of a given class template.
Definition gf.hpp:72
Provides the target types that fix the value stored at each mesh point of a Green's function.