TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
linear.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2015 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2015 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2019-2023 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Philipp Dumitrescu, Michel Ferrero, Henri Menke, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "../utils.hpp"
28#include "../mesh_iterator.hpp"
30
31#include <h5/h5.hpp>
32#include <itertools/itertools.hpp>
33#include <nda/nda.hpp>
34
35#include <algorithm>
36#include <concepts>
37#include <cstdint>
38#include <limits>
39#include <string>
40#include <type_traits>
41#include <utility>
42
43namespace triqs::mesh::detail {
44
72 template <typename M, typename T>
73 requires std::totally_ordered<T>
74 class linear {
75 public:
77 using value_t = T;
78
80 using index_t = long;
81
83 using data_index_t = long;
84
95 public:
97 using mesh_t = M;
98
100 mesh_point_t() = default;
101
111 mesh_point_t(long n, long d, uint64_t mhash, double m) : index_(n), data_index_(d), mesh_hash_(mhash), value_(m) {}
112
114 [[nodiscard]] long index() const { return index_; }
115
117 [[nodiscard]] long data_index() const { return data_index_; }
118
120 [[nodiscard]] value_t value() const { return value_; }
121
123 [[nodiscard]] uint64_t mesh_hash() const noexcept { return mesh_hash_; }
124
126 operator value_t() const { return value_; }
127
128#define IMPL_OP(OP) \
129 \
130 template <typename U> friend auto operator OP(mesh_point_t const &mp, U &&y) { return mp.value() OP std::forward<U>(y); } \
131 \
132 template <typename U> \
133 requires(not std::is_same_v<std::decay_t<U>, mesh_point_t>) \
134 friend auto operator OP(U &&x, mesh_point_t const &mp) { \
135 return std::forward<U>(x) OP mp.value(); \
136 }
137 IMPL_OP(+)
138 IMPL_OP(-)
139 IMPL_OP(*)
140 IMPL_OP(/)
141#undef IMPL_OP
142
143 private:
144 long index_ = 0;
145 long data_index_ = 0;
146 uint64_t mesh_hash_ = 0;
147 value_t value_ = {};
148 };
149
153 linear() = default;
154
162 linear(value_t a, value_t b, long N) : N_(N), a_(a), b_(b) {
163 EXPECTS(N_ >= 0);
164 if (N_ == 1) {
165 EXPECTS(a_ == b_);
166 delta_ = 0;
167 delta_inv_ = std::numeric_limits<double>::infinity();
168 } else if (N_ > 1) {
169 EXPECTS(a_ < b_);
170 delta_ = (b_ - a_) / (N_ - 1);
171 delta_inv_ = 1 / delta_;
172 }
173 }
174
176 C2PY_IGNORE bool operator==(linear const &rhs) const = default;
177
184 [[nodiscard]] bool is_index_valid(index_t n) const noexcept { return 0 <= n and n < N_; }
185
186 private:
187 // Check if a value is valid.
188 [[nodiscard]] bool is_value_valid(value_t m) const noexcept { return N_ > 0 and a_ <= m and m <= b_; }
189
190 public:
197 [[nodiscard]] data_index_t to_data_index(index_t n) const noexcept {
198 EXPECTS(is_index_valid(n));
199 return n;
200 }
201
208 [[nodiscard]] C2PY_IGNORE index_t to_data_index(closest_mesh_point_t<value_t> const &cmp) const noexcept { return to_data_index(to_index(cmp)); }
209
216 [[nodiscard]] index_t to_index(data_index_t d) const noexcept {
217 EXPECTS(is_index_valid(d));
218 return d;
219 }
220
227 [[nodiscard]] C2PY_IGNORE index_t to_index(closest_mesh_point_t<value_t> const &cmp) const noexcept {
228 EXPECTS(is_value_valid(cmp.value));
229 return (N_ == 1 ? 0 : static_cast<index_t>((cmp.value - a_) * delta_inv_ + 0.5));
230 }
231
239 [[nodiscard]] mesh_point_t operator[](long d) const noexcept { return (*this)(d); }
240
249 [[nodiscard]] C2PY_IGNORE mesh_point_t operator[](closest_mesh_point_t<value_t> const &cmp) const noexcept { return (*this)[to_data_index(cmp)]; }
250
258 [[nodiscard]] mesh_point_t operator()(index_t n) const noexcept { return {n, n, mesh_hash_, to_value(n)}; }
259
266 [[nodiscard]] value_t to_value(index_t n) const noexcept {
267 EXPECTS(is_index_valid(n));
268 if (N_ == 1) return a_;
269 auto const w = static_cast<double>(n) / (N_ - 1);
270 return a_ * (1 - w) + b_ * w;
271 }
272
274 [[nodiscard]] C2PY_PROPERTY_GET(mesh_hash) uint64_t mesh_hash() const noexcept { return mesh_hash_; }
275
277 [[nodiscard]] long size() const noexcept { return N_; }
278
280 [[nodiscard]] C2PY_PROPERTY_GET(delta) value_t delta() const noexcept { return delta_; }
281
283 [[nodiscard]] value_t delta_inv() const noexcept { return delta_inv_; }
284
286 [[nodiscard]] long first_index() const { return 0; }
287
289 [[nodiscard]] long last_index() const { return N_ - 1; }
290
292 [[nodiscard]] auto begin() const { return mesh_iterator<linear<M, T>>{.mesh_ptr = this, .data_index = 0}; }
293
295 [[nodiscard]] auto cbegin() const { return begin(); }
296
298 [[nodiscard]] auto end() const { return mesh_iterator<linear<M, T>>{.mesh_ptr = this, .data_index = size()}; }
299
301 [[nodiscard]] auto cend() const { return end(); }
302
307 void serialize(auto &ar) const { ar & N_ & a_ & b_ & delta_ & delta_inv_ & mesh_hash_; }
308
313 void deserialize(auto &ar) { ar & N_ & a_ & b_ & delta_ & delta_inv_ & mesh_hash_; }
314
315 protected:
323 void h5_write_impl(h5::group g, std::string const &name, const char *format) const {
324 h5::group gr = g.create_group(name);
325 h5::write_hdf5_format_as_string(gr, format); // NOLINT (downcasting to base class)
326 h5::write(gr, "min", a_);
327 h5::write(gr, "max", b_);
328 h5::write(gr, "size", N_);
329 }
330
338 void h5_read_impl(h5::group g, std::string const &name, const char *exp_format) {
339 h5::group gr = g.open_group(name);
340 h5::assert_hdf5_format_as_string(gr, exp_format, true); // NOLINT (downcasting to base class)
341 auto a = h5::read<value_t>(gr, "min");
342 auto b = h5::read<value_t>(gr, "max");
343 auto N = h5::read<long>(gr, "size");
344 *this = linear(a, b, N);
345 }
346
347 public:
364 auto evaluate(auto const &f, double x) const {
365 EXPECTS(is_value_valid(x) and N_ > 1);
366 auto const n_dbl = (x - a_) * delta_inv_;
367 auto const n = std::clamp<index_t>(static_cast<index_t>(n_dbl), 0, N_ - 2);
368 auto const w = std::min(n_dbl - n, 1.0);
369 return (1 - w) * f(n) + w * f(n + 1);
370 }
371
372 protected:
373 long N_{0};
374 value_t a_{0}, b_{0};
375 value_t delta_{0}, delta_inv_{0};
376 uint64_t mesh_hash_{hash(N_, a_, b_)};
377 };
378
379} // namespace triqs::mesh::detail
long data_index() const
Get the data index of the mesh point.
Definition linear.hpp:117
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
mesh_point_t(long n, long d, uint64_t mhash, double m)
Construct a mesh point with a given index , data index , hash value of the parent mesh and value .
Definition linear.hpp:111
long index() const
Get the index of the mesh point.
Definition linear.hpp:114
value_t value() const
Get the value of the mesh point.
Definition linear.hpp:120
uint64_t mesh_hash() const noexcept
Get the hash value of the parent mesh.
Definition linear.hpp:123
long last_index() const
Get the last index of the mesh, i.e. .
Definition linear.hpp:289
long data_index_t
Data index type.
Definition linear.hpp:83
mesh_point_t operator[](long d) const noexcept
Subscript operator to access a mesh point by its data index .
Definition linear.hpp:239
value_t delta_inv() const noexcept
Get the inverse of the step size of the mesh, i.e. .
Definition linear.hpp:283
data_index_t to_data_index(index_t n) const noexcept
Map an index to its corresponding data index .
Definition linear.hpp:197
long index_t
Index type.
Definition linear.hpp:80
linear(value_t a, value_t b, long N)
Construct a linear mesh on the interval of a given size .
Definition linear.hpp:162
mesh_point_t operator[](closest_mesh_point_t< value_t > const &cmp) const noexcept
Subscript operator to access a mesh point by a value contained in a triqs::mesh::closest_mesh_point_...
Definition linear.hpp:249
index_t to_index(data_index_t d) const noexcept
Map a data index to the corresponding index .
Definition linear.hpp:216
auto evaluate(auto const &f, double x) const
Linear interpolation of a function defined on a triqs::mesh::detail::linear mesh at a value .
Definition linear.hpp:364
long size() const noexcept
Get the size of the mesh, i.e. the number of mesh points.
Definition linear.hpp:277
bool operator==(linear const &rhs) const =default
Equal-to comparison operator compares and the interval .
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
Definition linear.hpp:307
auto cend() const
Get a const iterator to the end of the mesh.
Definition linear.hpp:301
void h5_write_impl(h5::group g, std::string const &name, const char *format) const
Write the mesh to HDF5.
Definition linear.hpp:323
long first_index() const
Get the first index of the mesh, i.e. .
Definition linear.hpp:286
mesh_point_t operator()(index_t n) const noexcept
Function call operator to access a mesh point by its index .
Definition linear.hpp:258
auto begin() const
Get an iterator to the beginning of the mesh.
Definition linear.hpp:292
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
Definition linear.hpp:313
auto end() const
Get an iterator to the end of the mesh.
Definition linear.hpp:298
void h5_read_impl(h5::group g, std::string const &name, const char *exp_format)
Read the mesh from HDF5.
Definition linear.hpp:338
linear()=default
Default construct an empty linear mesh of size .
index_t to_index(closest_mesh_point_t< value_t > const &cmp) const noexcept
Map a value to the closest mesh point and return its index .
Definition linear.hpp:227
bool is_index_valid(index_t n) const noexcept
Check if an index is valid.
Definition linear.hpp:184
value_t to_value(index_t n) const noexcept
Map an index to its corresponding value .
Definition linear.hpp:266
auto cbegin() const
Get a const iterator to the beginning of the mesh.
Definition linear.hpp:295
index_t to_data_index(closest_mesh_point_t< value_t > const &cmp) const noexcept
Map a value to the closest mesh point and return its data index .
Definition linear.hpp:208
uint64_t hash(Ts &&...ts)
Generic hash function for multiple arguments.
Definition utils.hpp:70
Common macros used in TRIQS.
Provides various utilities used with Meshes.
Provides a generic random access iterator for 1D meshes.
Lazy struct used in various function overloads as a placeholder for the closest mesh point to a given...
Definition utils.hpp:186
A generic random access iterator for 1D meshes.