TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
gf_const_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"
27
28#include <itertools/itertools.hpp>
29
30#include <array>
31#include <utility>
32
33namespace triqs::gfs {
34
47 template <Mesh M, typename Target, typename Layout> class gf_const_view : is_view_tag, TRIQS_CONCEPT_TAG_NAME(GreenFunction) {
48
49 using this_t = gf_const_view<M, Target, Layout>; // used in common code
50
51 public:
53 static constexpr bool is_view = true;
54
56 static constexpr bool is_const = true;
57
60
63
64
68 using regular_type = gf<M, Target>; // FIXME : find the Layout
69
72
75
77 using target_t = Target;
78
80 using mesh_t = M;
81
83 using mesh_point_t = typename mesh_t::mesh_point_t;
84
86 using mesh_index_t = typename mesh_t::index_t;
87
89 using scalar_t = typename Target::scalar_t;
90
92 static constexpr int arity = n_variables<M>;
95 static constexpr int target_rank = Target::rank;
96
98 static constexpr int data_rank = arity + Target::rank;
99
101 using data_t = nda::basic_array_view<const scalar_t, data_rank, Layout>;
102
104 using target_shape_t = std::array<long, Target::rank>;
105
110
112 using target_t = Target;
113
115 target_shape_t const &shape() const { return _shape; }
116 };
117
118 // ------------- Accessors -----------------------------
119
124 mesh_t const &mesh() const { return _mesh; }
125
130 data_t &data() & { return _data; }
131
136 data_t const &data() const & { return _data; }
137
142 data_t data() && { return std::move(_data); }
143
152 auto const &data_shape() const { return _data.shape(); }
153
158 target_and_shape_t target() const { return target_and_shape_t{stdutil::front_mpop<arity>(_data.shape())}; } // drop arity dims
159
164 std::array<long, Target::rank> target_shape() const { return target().shape(); } // drop arity dims
165
170 auto target_indices() const { return itertools::product_range(target().shape()); }
171
172 private:
173 mesh_t _mesh;
174 data_t _data;
175
176 // -------------------------------- impl. details common to all classes -----------------------------------------------
177
178 public:
183 gf_const_view(gf_const_view const &x) = default;
184
187
188 private:
189 void swap_impl(gf_const_view &b) noexcept {
190 using std::swap;
191 swap(this->_mesh, b._mesh);
192 swap(this->_data, b._data);
193 }
194
195 public:
196 // --------------- Constructors --------------------
197
198 /// Construct an empty view, not bound to any data.
199 gf_const_view() = default;
200
203
205 gf_const_view(gf_view<M, Target> const &g) : _mesh(g.mesh()), _data(g.data()) {}
206
211 gf_const_view(gf<M, Target> const &g) : _mesh(g.mesh()), _data(g.data()) {}
212
217 gf_const_view(gf<M, Target> &g) : _mesh(g.mesh()), _data(g.data()) {} // from a gf &
218
222 */
223 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved): a view binds to the data of `g`, not moving it
224 gf_const_view(gf<M, Target> &&g) noexcept : _mesh(std::move(g.mesh())), _data(std::move(g.data())) {} // from a gf &&
225
227
232 gf_const_view(mesh_t m, data_t dat) : _mesh(std::move(m)), _data(dat) {}
233
234 // --------------- swap --------------------
235
237 * @brief Swap two const views.
238 * @param a First view.
239 * @param b Second view.
240 */
241 friend void swap(gf_const_view &a, gf_const_view &b) noexcept { a.swap_impl(b); }
242
243 // --------------- Rebind --------------------
244
245
249 void rebind(gf_const_view<M, Target> const &g) noexcept {
250 this->_mesh = g._mesh;
251 this->_data.rebind(g._data);
252 }
258 void rebind(gf_view<M, Target> const &X) noexcept { rebind(gf_const_view{X}); }
259
260 // --------------- No = since it is const ... --------------------
261
263 gf_const_view &operator=(gf_const_view const &) = delete; // a const view can not be assigned to
264
265 public:
266 // ------------- apply_on_data -----------------------------
267
275 template <typename Fdata> auto apply_on_data(Fdata &&fd) {
276 auto d2 = std::forward<Fdata>(fd)(_data);
277 using t2 = target_from_array<decltype(d2), arity>;
278 using gv_t = gf_const_view<M, t2>;
279 return gv_t{mesh(), d2};
280 }
281
289 template <typename Fdata> auto apply_on_data(Fdata &&fd) const {
290 auto d2 = std::forward<Fdata>(fd)(_data);
291 using t2 = target_from_array<decltype(d2), arity>;
292 using gv_t = gf_const_view<M, t2>;
293 return gv_t{mesh(), d2};
294 }
295
296 // Common code for gf, gf_view, gf_const_view
297#include "./_gf_view_common.hpp"
298 };
299
300} // namespace triqs::gfs
Member code shared by triqs::gfs::gf, triqs::gfs::gf_view and triqs::gfs::gf_const_view.
gf_const_view(gf_view< M, Target > const &g)
Construct a const view from a (mutable) view.
gf_const_view(gf< M, Target > &&g) noexcept
Construct a const view onto an rvalue Green's function.
gf_view< M, T, Layout > mutable_view_type
gf_const_view(gf_const_view const &x)=default
Copy constructor (shallow: the new view refers to the same data).
typename mesh_t::mesh_point_t mesh_point_t
auto const & data_shape() const
Get the shape of the data array.
gf_const_view(gf_const_view &&)=default
Move constructor.
friend void swap(gf_const_view &a, gf_const_view &b) noexcept
Swap two const views.
gf_const_view(gf< M, Target > &g)
Construct a const view onto a (non const) Green's function.
gf_const_view(mesh_t m, data_t dat)
Build a const view on top of a mesh and a data array.
data_t data() &&
Get the data array view (rvalue overload).
gf_const_view< M, T, Layout > const_view_type
auto apply_on_data(Fdata &&fd)
Build a const view whose data is the result of applying a function to the data array.
mesh_t const & mesh() const
Get the mesh of the Green's function.
typename mesh_t::index_t mesh_index_t
gf_const_view()=default
Construct an empty view, not bound to any data.
gf_const_view< M, typename T::real_t, Layout > real_t
nda::basic_array_view< const scalar_t, data_rank, Layout > data_t
gf_const_view & operator=(gf_const_view const &)=delete
Deleted: a const view cannot be assigned to.
void rebind(gf_const_view< M, Target > const &g) noexcept
Rebind the const view to refer to the mesh and data of another const view.
void rebind(gf_view< M, Target > const &X) noexcept
Rebind the const view onto a (mutable) view.
data_t & data() &
Get the data array view.
auto apply_on_data(Fdata &&fd) const
Build a const view whose data is the result of applying a function to the data array (const overload)...
data_t const & data() const &
Get the data array view (const overload).
std::array< long, Target::rank > target_shape() const
Get the shape of the target.
std::array< long, T::rank > target_shape_t
gf_const_view(gf< M, Target > const &g)
Construct a const view onto a const Green's function.
auto target_indices() const
Get a generator over the multi-indices of the target space.
gf_const_view< M, T, Layout > view_type
gf_const_view< M, typename T::complex_t, Layout > complex_t
target_and_shape_t target() const
Get a handle to the target and its shape.
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
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
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_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.
target_shape_t const & shape() const
Get the shape of the target.
target_shape_t _shape
The shape of the target.
Empty tag inherited by every view type in TRIQS. See also is_view.
Definition traits.hpp:68