TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
gf_view.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 "./gf.hpp"
28
29#include <itertools/itertools.hpp>
30
31#include <array>
32#include <utility>
33
34namespace triqs::gfs {
35
48 template <Mesh M, typename Target, typename Layout> class gf_view : is_view_tag, TRIQS_CONCEPT_TAG_NAME(GreenFunction) {
50 using this_t = gf_view<M, Target, Layout>; // used in common code
51
52 public:
54 static constexpr bool is_view = true;
55
57 static constexpr bool is_const = false;
58
61
64
67
73
76
78 using target_t = Target;
81 using mesh_t = M;
82
84 using mesh_point_t = typename mesh_t::mesh_point_t;
85
87 using mesh_index_t = typename mesh_t::index_t;
88
90 using scalar_t = typename Target::scalar_t;
91
93 static constexpr int arity = n_variables<M>;
94
96 static constexpr int target_rank = Target::rank;
97
99 static constexpr int data_rank = arity + Target::rank;
100
101 /// Type of the data array view.
102 using data_t = nda::basic_array_view<scalar_t, data_rank, Layout>;
103
105 using target_shape_t = std::array<long, Target::rank>;
106
111
113 using target_t = Target;
114
116 target_shape_t const &shape() const { return _shape; }
117 };
118
119 // ------------- Accessors -----------------------------
120
125 mesh_t const &mesh() const { return _mesh; }
126
131 data_t &data() & { return _data; }
132
137 data_t const &data() const & { return _data; }
138
143 data_t data() && { return std::move(_data); }
144
153 auto const &data_shape() const { return _data.shape(); }
154
159 target_and_shape_t target() const { return target_and_shape_t{stdutil::front_mpop<arity>(_data.shape())}; } // drop arity dims
160
165 std::array<long, Target::rank> target_shape() const { return target().shape(); } // drop arity dims
166
170 */
171 auto target_indices() const { return itertools::product_range(target().shape()); }
172
173 private:
174 mesh_t _mesh;
175 data_t _data;
176
177 // -------------------------------- impl. details common to all classes -----------------------------------------------
179 public:
183
184 gf_view(gf_view const &x) = default;
185
186 /// Move constructor.
187 gf_view(gf_view &&) = default;
188
189 private:
190 void swap_impl(gf_view &b) noexcept {
191 using std::swap;
192 swap(this->_mesh, b._mesh);
193 swap(this->_data, b._data);
194 }
195
196 public:
197 // --------------- Constructors --------------------
200 gf_view() = default;
201
204
206 template <typename L> gf_view(gf<M, Target, L> const &g) = delete;
207
213 template <typename L> gf_view(gf<M, Target, L> &g) : _mesh(g.mesh()), _data(g.data()) {}
214
220 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved): a view binds to the data of `g`, not moving it
221 template <typename L> gf_view(gf<M, Target, L> &&g) noexcept : _mesh(std::move(g.mesh())), _data(g.data()) {}
227
229 gf_view(mesh_t m, data_t dat) : _mesh(std::move(m)), _data(dat) {}
230
231 // --------------- swap --------------------
232
237 */
238 friend void swap(gf_view &a, gf_view &b) noexcept { a.swap_impl(b); }
239
240 // --------------- Rebind --------------------
245 void rebind(gf_view<M, Target> const &g) noexcept {
246 this->_mesh = g._mesh;
247 this->_data.rebind(g._data);
248 }
249
250 // --------------- operator = --------------------
259 gf_view &operator=(gf_view const &rhs) {
260 triqs_gf_view_assign_delegation(*this, rhs);
261 return *this;
262 }
263
274 template <typename RHS> gf_view &operator=(RHS const &rhs) {
275 triqs_gf_view_assign_delegation(*this, rhs);
276 return *this;
277 }
278
279 public:
280 // ------------- apply_on_data -----------------------------
281
289 template <typename Fdata> auto apply_on_data(Fdata &&fd) {
290 auto d2 = std::forward<Fdata>(fd)(_data);
291 using t2 = target_from_array<decltype(d2), arity>;
292 using gv_t = gf_view<M, t2>;
293 return gv_t{mesh(), d2};
294 }
295
303 template <typename Fdata> auto apply_on_data(Fdata &&fd) const {
304 auto d2 = std::forward<Fdata>(fd)(_data);
305 using t2 = target_from_array<decltype(d2), arity>;
306 using gv_t = gf_const_view<M, t2>;
307 return gv_t{mesh(), d2};
308 }
309
310 // Common code for gf, gf_view, gf_const_view
311#include "./_gf_view_common.hpp"
312 };
313
314 /*------------------------------------------------------------------------------------------------------
315 * View assignment
316 *-----------------------------------------------------------------------------------------------------*/
317
318 // Implementation of the assignment to a gf_view: scalar rhs is assigned to every mesh point, otherwise the meshes
319 // must match and the data is copied point by point (throws on a mesh mismatch).
320 template <typename M, typename T, typename L, typename RHS> void triqs_gf_view_assign_delegation(gf_view<M, T, L> g, RHS const &rhs) {
321 if constexpr (nda::is_scalar_v<RHS>) {
322 for (auto w : g.mesh()) g[w] = rhs;
323 } else {
324 if (!(g.mesh() == rhs.mesh())) TRIQS_RUNTIME_ERROR << "Gf Assignment in View : incompatible mesh \n" << g.mesh() << "\n vs \n" << rhs.mesh();
325 for (auto w : g.mesh()) g[w] = rhs[w];
326 }
327 }
328} // namespace triqs::gfs
329/*------------------------------------------------------------------------------------------------------
330 * Delete std::swap for views, as for arrays
331 *-----------------------------------------------------------------------------------------------------*/
332namespace std {
333 // Deleted std::swap for Green's function views (as for nda array views): use the hidden friend swap instead.
334 // NOLINTNEXTLINE(bugprone-std-namespace-modification): deleting std::swap for views is intentional, as for nda arrays
335 template <typename M, typename Target> void swap(triqs::gfs::gf_view<M, Target> &a, triqs::gfs::gf_view<M, Target> &b) = delete;
336} // namespace std
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.
gf< M, T, typename Layout::contiguous_t > regular_type
Definition gf_view.hpp:69
data_t data() &&
Get the data array view (rvalue overload).
Definition gf_view.hpp:143
static constexpr bool is_view
Definition gf_view.hpp:54
gf_view(gf< M, Target, L > const &g)=delete
Deleted: a mutable view cannot be built from a const Green's function.
static constexpr int data_rank
Definition gf_view.hpp:99
gf_view(gf< M, Target, L > &g)
Construct a view onto a (non const) Green's function.
Definition gf_view.hpp:213
auto target_indices() const
Get a generator over the multi-indices of the target space.
Definition gf_view.hpp:171
static constexpr int arity
Definition gf_view.hpp:93
std::array< long, Target::rank > target_shape() const
Get the shape of the target.
Definition gf_view.hpp:165
gf_view & operator=(gf_view const &rhs)
Copy assignment: copy the data through the view without resizing it.
Definition gf_view.hpp:259
gf_view & operator=(RHS const &rhs)
Assign from any compatible right hand side, writing through the view without resizing it.
Definition gf_view.hpp:274
gf_view< M, typename T::complex_t, Layout > complex_t
Definition gf_view.hpp:75
static constexpr int target_rank
Definition gf_view.hpp:96
gf_view< M, T, Layout > view_type
Definition gf_view.hpp:66
data_t & data() &
Get the data array view.
Definition gf_view.hpp:131
friend void swap(gf_view &a, gf_view &b) noexcept
Swap two views.
Definition gf_view.hpp:238
static constexpr bool is_const
Definition gf_view.hpp:57
mesh_t const & mesh() const
Get the mesh of the Green's function.
Definition gf_view.hpp:125
auto apply_on_data(Fdata &&fd) const
Build a const view whose data is the result of applying a function to the data array.
Definition gf_view.hpp:303
gf_view(gf< M, Target, L > &&g) noexcept
Construct a view onto an rvalue Green's function.
Definition gf_view.hpp:221
auto apply_on_data(Fdata &&fd)
Build a view whose data is the result of applying a function to the data array.
Definition gf_view.hpp:289
typename mesh_t::mesh_point_t mesh_point_t
Definition gf_view.hpp:84
typename T::scalar_t scalar_t
Definition gf_view.hpp:90
gf_view< M, typename T::real_t, Layout > real_t
Definition gf_view.hpp:72
gf_view(mesh_t m, data_t dat)
Build a view on top of a mesh and a data array.
Definition gf_view.hpp:229
data_t const & data() const &
Get the data array view (const overload).
Definition gf_view.hpp:137
gf_const_view< M, T, Layout > const_view_type
Definition gf_view.hpp:63
nda::basic_array_view< scalar_t, data_rank, Layout > data_t
Definition gf_view.hpp:102
typename mesh_t::index_t mesh_index_t
Definition gf_view.hpp:87
gf_view()=default
Construct an empty view, not bound to any data.
auto const & data_shape() const
Get the shape of the data array.
Definition gf_view.hpp:153
gf_view(gf_const_view< M, Target > const &g)=delete
Deleted: a mutable view cannot be built from a const view.
target_and_shape_t target() const
Get a handle to the target and its shape.
Definition gf_view.hpp:159
gf_view(gf_view const &x)=default
Copy constructor (shallow: the new view refers to the same data).
void rebind(gf_view< M, Target > const &g) noexcept
Rebind the view to refer to the mesh and data of another view.
Definition gf_view.hpp:245
std::array< long, T::rank > target_shape_t
Definition gf_view.hpp:105
gf_view(gf_view &&)=default
Move constructor.
gf_view< M, T, Layout > mutable_view_type
Definition gf_view.hpp:60
The owning Green's function container.
Definition gf.hpp:194
Macros that define a legacy (pre C++20) concept-tag trait pair.
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.
Provides the Green's function class.
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
#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.
Lightweight handle bundling the target type and its shape.
Definition gf_view.hpp:108
target_shape_t const & shape() const
Get the shape of the target.
Definition gf_view.hpp:116
target_shape_t _shape
The shape of the target.
Definition gf_view.hpp:110
Empty tag inherited by every view type in TRIQS. See also is_view.
Definition traits.hpp:68