TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
block_gf_view.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-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 "./block_gf.hpp"
28
29#include <ostream>
30#include <string>
31#include <type_traits>
32#include <utility>
33#include <vector>
34
35namespace triqs::gfs {
36
52 template <typename Mesh, typename Target, typename Layout, int Arity, bool IsConst>
53 class block_gf_view : is_view_tag, TRIQS_CONCEPT_TAG_NAME(BlockGreenFunction) {
54 using this_t = block_gf_view; // for common code
55
56 public:
58 static constexpr bool is_view = true;
59
61 static constexpr bool is_const = IsConst;
64 static constexpr int arity = Arity;
65
67 using mesh_t = Mesh;
68
70 using target_t = Target;
71
74
76 using mutable_view_type = block_gf_view<Mesh, Target, Layout, Arity>;
77
79 using view_type = block_gf_view<Mesh, Target, Layout, Arity, false>;
80
82 using const_view_type = block_gf_view<Mesh, Target, Layout, Arity, true>;
83
84
85 using real_t = block_gf_view<Mesh, typename Target::real_t, Layout, Arity, IsConst>;
86
88 using g_t = std::conditional_t<IsConst, gf_const_view<Mesh, Target, Layout>, gf_view<Mesh, Target, Layout>>;
91 using data_t = std::conditional_t<Arity == 1, std::vector<g_t>, std::vector<std::vector<g_t>>>;
92
94 using block_names_t = std::conditional_t<Arity == 1, std::vector<std::string>, std::vector<std::vector<std::string>>>;
95
97 std::string name;
98
99 private:
100 block_names_t _block_names;
101 data_t _glist;
102
103 // --------------- Constructors --------------------
104
105 // Tag and constructor used internally to build a view from another block-gf-like object.
106 struct impl_tag {};
107 // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward): x is only read to build views on its data, not forwarded
108 template <typename G> block_gf_view(impl_tag, G &&x) : name(x.name), _block_names(x.block_names()), _glist(factory<data_t>(x.data())) {}
110 public:
115 block_gf_view(block_gf_view const &x) = default;
116
118 block_gf_view(block_gf_view &&) = default;
119
128 block_gf_view(block_names_t b, data_t d) : _block_names(std::move(b)), _glist(std::move(d)) {
129 if constexpr (Arity == 1) {
130 if (_glist.size() != _block_names.size())
131 TRIQS_RUNTIME_ERROR << "block_gf(vector<string>, vector<gf>) : the two vectors do not have the same size !";
132 } else {
133 if (_glist.size() != _block_names[0].size())
134 TRIQS_RUNTIME_ERROR << "block2_gf(vector<vector<string>>, vector<vector<gf>>) : Outer vectors have different sizes !";
135 if (_glist.size() != 0)
136 if (_glist[0].size() != _block_names[1].size())
137 TRIQS_RUNTIME_ERROR << "block2_gf(vector<vector<string>>, vector<vector<gf>>) : Inner vectors have different sizes !";
138 }
140
141 // --------------- Constructors --------------------
142
144 block_gf_view() = default;
145
146 /**
147 * @brief Construct a const view onto a block Green's function.
148 * @tparam L Layout of the block Green's function.
149 * @param g Block Green's function to view.
150 */
151 template <typename L>
153 requires(IsConst)
154 : block_gf_view(impl_tag{}, g) {}
155
158 * @tparam L Layout of the block Green's function.
159 * @param g Block Green's function to view.
160 */
161 template <typename L>
163 requires(!IsConst)
164 : block_gf_view(impl_tag{}, g) {}
165
170 */
171 template <typename L> block_gf_view(block_gf<Mesh, Target, L, Arity> &&g) noexcept : block_gf_view(impl_tag{}, std::move(g)) {}
172
175
178 template <typename L>
179 block_gf_view(block_gf_view<Mesh, Target, L, Arity, !IsConst> const &g)
180 requires(IsConst)
181 : block_gf_view(impl_tag{}, g) {}
182
189
190 block_gf_view &operator=(block_gf_view const &rhs)
191 requires(not IsConst)
193 _assign_impl(rhs);
194 return *this;
195 }
196
197
200 * @details `rhs` can be a scalar (assigned to every block) or anything with `.block_names()` and `[n]` returning a
201 * Green's function. For a non-scalar right hand side the number of blocks must match (throws otherwise).
202 *
203 * @tparam RHS Type of the right hand side.
204 * @param rhs Object to assign from.
205 * @return A reference to this view.
206 */
207 template <typename RHS>
208 block_gf_view &operator=(RHS const &rhs)
209 requires(not IsConst)
210 {
211 if constexpr (not nda::is_scalar_v<RHS>) {
212 if (!(size() == rhs.size())) TRIQS_RUNTIME_ERROR << "Gf Assignment in View : incompatible size" << size() << " vs " << rhs.size();
213 _assign_impl(rhs);
214 } else {
215 if constexpr (Arity == 1) {
216 for (auto &y : _glist) y = rhs;
217 } else {
218 for (auto &x : _glist)
219 for (auto &y : x) y = rhs;
220 }
221 }
222 return *this;
223 }
229
235 template <typename L, typename G>
236 block_gf_view &operator=(lazy_transform_t<L, G> const &rhs)
237 requires(not IsConst)
238 {
239 if constexpr (Arity == 1) {
240 for (int i = 0; i < rhs.value.size(); ++i) (*this)[i] = rhs.lambda(rhs.value[i]);
241 } else {
242
243 for (int i = 0; i < rhs.value.size1(); ++i)
244 for (int j = 0; j < rhs.value.size2(); ++j) (*this)(i, j) = rhs.lambda(rhs.value(i, j));
245 }
246 return *this;
248
249 // --------------- Rebind --------------------
250
255 void rebind(block_gf_view x) noexcept {
256 _block_names = x._block_names;
257 _glist = data_t{x._glist}; // copy of vector<vector<gf_view>>, makes new views on the gf of x
258 name = x.name;
259 }
260
265 void rebind(block_gf_view<Mesh, Target, Layout, Arity, !IsConst> const &X) noexcept
266 requires(IsConst)
268 rebind(block_gf_view{X});
269 }
270
273
276 requires(IsConst)
277 {
278 rebind(block_gf_view{X});
279 }
280
281 /**
282 * @brief Rebind a view onto a (non const) block Green's function.
283 * @param X Block Green's function to rebind onto.
284 */
285 void rebind(block_gf<Mesh, Target, Layout, Arity> &X) noexcept { rebind(block_gf_view{X}); }
286
287 public:
288 //----------------------------- print -----------------------------
289
291 friend std::ostream &operator<<(std::ostream &out, block_gf_view const &) { return out << "block_gf_view"; }
292
293 // Common code for gf, gf_view, gf_const_view
295 };
296
297} // namespace triqs::gfs
298
299/*------------------------------------------------------------------------------------------------------
300 * Delete std::swap for views
301 *-----------------------------------------------------------------------------------------------------*/
302namespace std {
303 // Deleted std::swap for block Green's function views (as for nda array views): use rebind instead.
304 // NOLINTNEXTLINE(bugprone-std-namespace-modification): deleting std::swap for views is intentional, as for nda arrays
305 template <typename Mesh, typename Target, typename Layout, int Arity, bool IsConst>
308} // namespace std
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.
Provides the block Green's function container.
T factory(U &&...x)
Generic factory to construct an object of a given type from an arbitrary parameter pack of arguments.
Definition factory.hpp:95
A non-owning view of a block Green's function.
block_gf_view(block_names_t b, data_t d)
Construct from a list of block names and a list of Green's function views.
void rebind(block_gf_view< Mesh, Target, Layout, Arity, !IsConst > const &X) noexcept
Rebind a const view onto a mutable view of the same kind.
block_gf_view(block_gf_view &&)=default
Move constructor.
block_gf_view()=default
Construct an empty view, not bound to any data.
block_gf_view< M, T, Layout, Arity > mutable_view_type
block_gf_view(block_gf_view const &x)=default
Copy constructor (shallow: the new view refers to the same blocks).
block_gf_view & operator=(lazy_transform_t< L, G > const &rhs)
Assignment operator overload specific for triqs::gfs::lazy_transform_t objects.
void rebind(block_gf< Mesh, Target, Layout, Arity > &X) noexcept
Rebind a view onto a (non const) block Green's function.
block_gf_view(block_gf< Mesh, Target, L, Arity > &g)
Construct a mutable view onto a (non const) block Green's function.
int size() const
Get the total number of blocks.
block_gf_view(block_gf_view< Mesh, Target, L, Arity, !IsConst > const &g)
Construct a const view from a mutable view of the same kind.
std::conditional_t< Arity==1, std::vector< std::string >, std::vector< std::vector< std::string > > > block_names_t
std::conditional_t< IsConst, gf_const_view< M, T, Layout >, gf_view< M, T, Layout > > g_t
block_gf_view(block_gf< Mesh, Target, L, Arity > const &g)
Construct a const view onto a block Green's function.
block_gf_view< M, T, Layout, Arity, true > const_view_type
block_gf_view(block_gf< Mesh, Target, L, Arity > &&g) noexcept
Construct a view onto an rvalue block Green's function.
friend std::ostream & operator<<(std::ostream &out, block_gf_view const &)
Writing a block Green's function to an output stream is not supported.
block_gf_view< M, typename T::real_t, Layout, Arity, IsConst > real_t
std::conditional_t< Arity==1, std::vector< g_t >, std::vector< std::vector< g_t > > > data_t
void rebind(block_gf< Mesh, Target, Layout, Arity > const &X) noexcept
Rebind a const view onto a block Green's function.
block_gf< M, T, typename Layout::contiguous_t, Arity > regular_type
block_gf_view< M, T, Layout, Arity, false > view_type
block_gf_view & operator=(RHS const &rhs)
Assign from any compatible right hand side, writing through the view without resizing it.
block_gf_view & operator=(block_gf_view const &rhs)
------------— Operator = -----------------—
void rebind(block_gf_view x) noexcept
Rebind the view to refer to the blocks of another view of the same kind.
The owning block Green's function container.
Definition block_gf.hpp:259
A mutable, non-owning view of a Green's function.
Definition gf_view.hpp:48
Macros that define a legacy (pre C++20) concept-tag trait pair.
TRIQS exception hierarchy and related macros.
#define TRIQS_RUNTIME_ERROR
Throw a triqs::runtime_error with the current source location.
#define TRIQS_CONCEPT_TAG_NAME(MyBeautifulConcept)
Helper macro that produces the name of the tag for TRIQS_DEFINE_CONCEPT_AND_ASSOCIATED_TRAIT.
Empty tag inherited by every view type in TRIQS. See also is_view.
Definition traits.hpp:68