TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
legendre.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2016 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2016 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, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./mesh_iterator.hpp"
28#include "./utils.hpp"
30#include "../utility/macros.hpp"
31
32#include <fmt/format.h>
33#include <h5/h5.hpp>
34#include <itertools/itertools.hpp>
35#include <nda/nda.hpp>
36
37#include <cmath>
38#include <cstdint>
39#include <iostream>
40#include <string>
41
42namespace triqs::mesh {
43
48
76 class C2PY_RENAME(MeshLegendre) legendre {
77 public:
79 using index_t = long;
80
82 using data_index_t = long;
83
88 class C2PY_IGNORE mesh_point_t {
89 public:
92
94 mesh_point_t() = default;
95
104 mesh_point_t(long n, long d, uint64_t mhash) : index_(n), data_index_(d), mesh_hash_(mhash) {}
105
107 [[nodiscard]] long index() const { return index_; }
108
110 [[nodiscard]] long data_index() const { return data_index_; }
111
113 [[nodiscard]] uint64_t mesh_hash() const noexcept { return mesh_hash_; }
114
115 private:
116 long index_ = 0;
117 long data_index_ = 0;
118 uint64_t mesh_hash_ = 0;
119 };
120
122 legendre() = default;
123
132 C2PY_DEPRECATED_PARAMETER_NAME(S : statistic, n_max : max_n)
133 legendre(double beta, statistic_enum statistic, long max_n) : beta_(beta), stat_(statistic), N_(max_n), mesh_hash_(hash(beta, statistic, max_n)) {
134 EXPECTS(beta_ > 0);
135 EXPECTS(N_ >= 0);
136 }
137
139 bool operator==(legendre const &) const = default;
140
147 [[nodiscard]] bool is_index_valid(index_t n) const noexcept { return 0 <= n and n < N_; }
148
155 [[nodiscard]] data_index_t to_data_index(index_t n) const noexcept {
156 EXPECTS(is_index_valid(n));
157 return n;
158 }
159
166 [[nodiscard]] index_t to_index(long d) const noexcept {
167 EXPECTS(is_index_valid(d));
168 return d;
169 }
170
177 [[nodiscard]] mesh_point_t operator[](long d) const { return {to_index(d), d, mesh_hash_}; }
178
185 [[nodiscard]] mesh_point_t operator()(long n) const { return {n, to_data_index(n), mesh_hash_}; }
186
188 [[nodiscard]] C2PY_PROPERTY_GET(beta) double beta() const noexcept { return beta_; }
189
191 [[nodiscard]] C2PY_PROPERTY_GET(statistic) auto statistic() const noexcept { return stat_; }
192
194 [[nodiscard]] C2PY_PROPERTY_GET(mesh_hash) uint64_t mesh_hash() const { return mesh_hash_; }
195
197 [[nodiscard]] long size() const { return N_; }
198
200 [[nodiscard]] auto begin() const { return mesh_iterator<legendre>{.mesh_ptr = this, .data_index = 0}; }
201
203 [[nodiscard]] auto cbegin() const { return begin(); }
204
206 [[nodiscard]] auto end() const { return mesh_iterator<legendre>{.mesh_ptr = this, .data_index = size()}; }
207
209 [[nodiscard]] auto cend() const { return end(); }
210
218 friend std::ostream &operator<<(std::ostream &sout, legendre const &m) {
219 auto stat_cstr = (m.stat_ == Boson ? "Boson" : "Fermion");
220 return sout << fmt::format("Legendre mesh with beta = {}, statistics = {}, N = {}", m.beta_, stat_cstr, m.size());
221 }
222
227 void serialize(auto &ar) const { ar & beta_ & stat_ & N_ & mesh_hash_; }
228
233 void deserialize(auto &ar) { ar & beta_ & stat_ & N_ & mesh_hash_; }
234
236 [[nodiscard]] static std::string hdf5_format() { return "MeshLegendre"; }
237
245 friend void h5_write(h5::group g, std::string const &name, legendre const &m) {
246 h5::group gr = g.create_group(name);
247 h5::write_hdf5_format(gr, m); // NOLINT (downcasting to base class)
248 h5::write(gr, "beta", m.beta_);
249 h5::write(gr, "statistic", (m.stat_ == Fermion ? "F" : "B"));
250 h5::write(gr, "max_n", m.N_);
251 }
252
260 friend void h5_read(h5::group g, std::string const &name, legendre &m) {
261 h5::group gr = g.open_group(name);
262 h5::assert_hdf5_format(gr, m, true); // NOLINT (downcasting to base class)
263
264 // for backward compatibility
265 if (gr.has_key("domain")) { gr = gr.open_group("domain"); }
266
267 auto beta = h5::read<double>(gr, "beta");
268 auto statistic = (h5::read<std::string>(gr, "statistic") == "F" ? Fermion : Boson);
269
270 // for backward compatibility
271 long max_n{};
272 if (not h5::try_read(gr, "max_n", max_n)) max_n = static_cast<long>(h5::read<size_t>(gr, "n_max"));
273
274 m = legendre(beta, statistic, max_n);
275 }
276
277 private:
278 double beta_ = 1.0;
279 statistic_enum stat_ = Fermion;
280 long N_ = 0;
281 uint64_t mesh_hash_ = 0;
282 };
283
304 inline auto evaluate(legendre const &m, auto const &f, double tau) {
305 EXPECTS(m.size() > 0 and tau >= 0 and tau <= m.beta());
307 gen.reset(2 * tau / m.beta() - 1);
308 return detail::sum_to_regular(nda::range(m.size()), [&](auto n) { return f(n) * std::sqrt(2 * n + 1) * gen.next() / m.beta(); });
309 }
310
312
313} // namespace triqs::mesh
iterator end()
Get an iterator past the last block.
iterator begin()
Get an iterator to the first block.
int size() const
Get the total number of blocks.
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
legendre()=default
Default constructor constructs an empty mesh.
Mesh point of a triqs::mesh::legendre mesh.
Definition legendre.hpp:88
long data_index() const
Get the data index of the mesh point.
Definition legendre.hpp:110
legendre mesh_t
Parent mesh type.
Definition legendre.hpp:91
uint64_t mesh_hash() const noexcept
Get the hash value of the parent mesh.
Definition legendre.hpp:113
mesh_point_t(long n, long d, uint64_t mhash)
Construct a mesh point with a given index , data index and hash value of the parent mesh.
Definition legendre.hpp:104
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
long index() const
Get the index of the mesh point.
Definition legendre.hpp:107
Legendre mesh type.
Definition legendre.hpp:76
double beta() const noexcept
Get the inverse temperature .
Definition legendre.hpp:188
mesh_point_t operator[](long d) const
Subscript operator to access a mesh point by its data index .
Definition legendre.hpp:177
auto begin() const
Get an iterator to the beginning of the mesh.
Definition legendre.hpp:200
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
Definition legendre.hpp:233
friend void h5_write(h5::group g, std::string const &name, legendre const &m)
Write a triqs::mesh::legendre mesh to HDF5.
Definition legendre.hpp:245
long size() const
Get the size of the mesh, i.e. the number of mesh points or polynomials in the series expansion.
Definition legendre.hpp:197
uint64_t mesh_hash() const
Get the hash value of the mesh.
Definition legendre.hpp:194
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
Definition legendre.hpp:227
index_t to_index(long d) const noexcept
Map a data index to the corresponding index .
Definition legendre.hpp:166
auto cbegin() const
Get a const iterator to the beginning of the mesh.
Definition legendre.hpp:203
bool is_index_valid(index_t n) const noexcept
Check if an index is valid.
Definition legendre.hpp:147
auto statistic() const noexcept
Get the particle statistics.
Definition legendre.hpp:191
mesh_point_t operator()(long n) const
Function call operator to access a mesh point by its index .
Definition legendre.hpp:185
long data_index_t
Data index type.
Definition legendre.hpp:82
long index_t
Index type.
Definition legendre.hpp:79
data_index_t to_data_index(index_t n) const noexcept
Map an index to its corresponding data index .
Definition legendre.hpp:155
static std::string hdf5_format()
Get the HDF5 format tag.
Definition legendre.hpp:236
auto cend() const
Get a const iterator to the end of the mesh.
Definition legendre.hpp:209
legendre()=default
Default constructor constructs an empty mesh.
bool operator==(legendre const &) const =default
Equal-to comparison operator compares , and the particle statistics.
auto end() const
Get an iterator to the end of the mesh.
Definition legendre.hpp:206
friend std::ostream & operator<<(std::ostream &sout, legendre const &m)
Write a triqs::mesh::legendre mesh to a std::ostream.
Definition legendre.hpp:218
friend void h5_read(h5::group g, std::string const &name, legendre &m)
Read a triqs::mesh::legendre mesh from HDF5.
Definition legendre.hpp:260
Recursive generation of Legendre polynomials .
Definition legendre.hpp:87
void reset(double x)
Reset the generator to 0th order and with a new value.
Definition legendre.hpp:117
statistic_enum
Enum to specify particle statistics.
Definition utils.hpp:163
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.
A generic random access iterator for 1D meshes.
Provides Legendre polynomials and related functions.