TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
imfreq.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-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: Thomas Ayral, Philipp Dumitrescu, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./matsubara_freq.hpp"
28#include "./mesh_iterator.hpp"
29#include "./tail_fitter.hpp"
30#include "./utils.hpp"
31#include "../utility/macros.hpp"
32
33#include <fmt/format.h>
34#include <h5/h5.hpp>
35#include <nda/nda.hpp>
36
37#include <complex>
38#include <cstdint>
39#include <iostream>
40#include <numbers>
41#include <string>
42#include <tuple>
43
44namespace triqs::mesh {
45
50 struct energy_t {
52 double value = 0;
53
55 explicit operator double() const { return value; }
56 };
57
62
102 class C2PY_RENAME(MeshImFreq) imfreq : public tail_fitter_handle {
103 public:
106
108 using index_t = long;
109
111 using data_index_t = long;
112
120 class C2PY_IGNORE mesh_point_t : public matsubara_freq {
121 public:
123 using mesh_t = imfreq;
124
126 mesh_point_t() = default;
127
140 mesh_point_t(double b, statistic_enum stat, index_t n_idx, long d, uint64_t mhash)
141 : matsubara_freq(n_idx, b, stat), data_index_(d), mesh_hash_(mhash) {}
142
144 [[nodiscard]] long index() const { return n; }
145
147 [[nodiscard]] long data_index() const { return data_index_; }
148
150 [[nodiscard]] matsubara_freq const &value() const { return *this; }
151
153 [[nodiscard]] uint64_t mesh_hash() const noexcept { return mesh_hash_; }
154
155 private:
156 long data_index_ = 0;
157 uint64_t mesh_hash_ = 0;
158 };
159
167 enum class option { all_frequencies, positive_frequencies_only };
168
170 imfreq() = default;
171
180 C2PY_DEPRECATED_PARAMETER_NAME(S : statistic, n_max : n_iw)
181 imfreq(double beta, statistic_enum statistic, long n_iw = 1025, option opt = option::all_frequencies)
182 : beta_(beta), stat_(statistic), N_iw_(n_iw), opt_(opt), mesh_hash_(hash(beta, statistic, n_iw, opt)) {
183 EXPECTS(beta_ > 0);
184 EXPECTS(N_iw_ >= 0);
185 if (opt == option::positive_frequencies_only) first_index_ = 0;
186 }
187
206 C2PY_IGNORE imfreq(double beta, statistic_enum statistic, energy_t w_max, option opt = option::all_frequencies)
207 : imfreq(beta, statistic, static_cast<long>((w_max.value * beta / std::numbers::pi - (statistic == Fermion ? 1 : 0)) / 2) + 1, opt) {}
208
213 bool operator==(imfreq const &m) const { return (std::tie(beta_, stat_, N_iw_, opt_) == std::tie(m.beta_, m.stat_, m.N_iw_, m.opt_)); }
214
221 [[nodiscard]] bool is_index_valid(index_t n) const { return first_index() <= n and n <= last_index(); }
222
230 [[nodiscard]] data_index_t to_data_index(index_t n) const noexcept {
231 EXPECTS(is_index_valid(n));
232 return n - first_index();
233 }
234
241 [[nodiscard]] data_index_t to_data_index(matsubara_freq const &iw) const noexcept {
242 EXPECTS(beta_ == iw.beta and stat_ == iw.statistic);
243 return to_data_index(iw.n);
244 }
245
253 [[nodiscard]] C2PY_IGNORE data_index_t to_data_index(closest_mesh_point_t<value_t> const &cmp) const {
254 EXPECTS(beta_ == cmp.value.beta and stat_ == cmp.value.statistic);
255 return to_data_index(to_index(cmp));
256 }
257
264 [[nodiscard]] index_t to_index(data_index_t d) const {
265 EXPECTS(0 <= d and d < size());
266 return d + first_index();
267 }
268
276 [[nodiscard]] C2PY_IGNORE index_t to_index(closest_mesh_point_t<value_t> const &cmp) const {
277 EXPECTS(is_index_valid(cmp.value.n));
278 return cmp.value.n;
279 }
280
288 [[nodiscard]] mesh_point_t operator[](long d) const { return {beta_, stat_, to_index(d), d, mesh_hash_}; }
289
298 [[nodiscard]] C2PY_IGNORE mesh_point_t operator[](closest_mesh_point_t<value_t> const &cmp) const { return (*this)[to_data_index(cmp)]; }
299
308 [[nodiscard]] mesh_point_t operator()(long n) const { return {beta_, stat_, n, to_data_index(n), mesh_hash_}; }
309
317 [[nodiscard]] matsubara_freq to_value(index_t n) const {
318 EXPECTS(is_index_valid(n));
319 return {n, beta_, stat_};
320 }
321
326 imfreq get_positive_freq() const { return {beta_, stat_, N_iw_, option::positive_frequencies_only}; }
327
329 std::complex<double> w_max() const { return to_value(last_index_); }
330
332 [[nodiscard]] C2PY_PROPERTY_GET(beta) double beta() const noexcept { return beta_; }
333
335 [[nodiscard]] C2PY_PROPERTY_GET(statistic) statistic_enum statistic() const noexcept { return stat_; }
336
338 [[nodiscard]] C2PY_PROPERTY_GET(n_iw) long n_iw() const noexcept { return N_iw_; }
339
341 [[nodiscard]] C2PY_PROPERTY_GET(mesh_hash) uint64_t mesh_hash() const noexcept { return mesh_hash_; }
342
344 [[nodiscard]] long size() const noexcept { return last_index_ - first_index_ + 1; }
345
347 [[nodiscard]] long first_index() const { return first_index_; }
348
350 [[nodiscard]] long last_index() const { return last_index_; }
351
353 [[nodiscard]] bool positive_only() const { return opt_ == option::positive_frequencies_only; }
354
356 [[nodiscard]] auto begin() const { return mesh_iterator<imfreq>{.mesh_ptr = this, .data_index = 0}; }
357
359 [[nodiscard]] auto cbegin() const { return begin(); }
360
362 [[nodiscard]] auto end() const { return mesh_iterator<imfreq>{.mesh_ptr = this, .data_index = size()}; }
363
365 [[nodiscard]] auto cend() const { return end(); }
366
374 friend std::ostream &operator<<(std::ostream &sout, imfreq const &m) {
375 auto stat_cstr = (m.stat_ == Boson ? "Boson" : "Fermion");
376 return sout << fmt::format("Imaginary frequency mesh with beta = {}, statistics = {}, N_iw = {}, positive_only = {}", m.beta_, stat_cstr,
377 m.N_iw_, m.positive_only());
378 }
379
384 void serialize(auto &ar) const { ar & beta_ & stat_ & N_iw_ & opt_ & last_index_ & first_index_ & mesh_hash_; }
385
390 void deserialize(auto &ar) { ar & beta_ & stat_ & N_iw_ & opt_ & last_index_ & first_index_ & mesh_hash_; }
391
393 [[nodiscard]] static std::string hdf5_format() { return "MeshImFreq"; }
394
402 friend void h5_write(h5::group g, std::string name, imfreq const &m) {
403 h5::group gr = g.create_group(name);
404 h5::write_hdf5_format(gr, m); // NOLINT (downcasting to base class)
405 h5::write(gr, "beta", m.beta_);
406 h5::write(gr, "statistic", (m.stat_ == Fermion ? "F" : "B"));
407 h5::write(gr, "size", m.size());
408 h5::write(gr, "positive_freq_only", (m.positive_only() ? 1 : 0));
409 }
410
418 friend void h5_read(h5::group g, std::string name, imfreq &m) {
419 h5::group gr = g.open_group(name);
420 h5::assert_hdf5_format(gr, m, true); // NOLINT (downcasting to base class)
421
422 // enum option: positive_frequencies_only = 1, all_frequencies = 0
423 int pos_freq = 0;
424 if (gr.has_key("positive_freq_only")) h5::read(gr, "positive_freq_only", pos_freq);
425 if (gr.has_key("start_at_0")) h5::read(gr, "start_at_0", pos_freq); // backward compatibility
426 auto opt = (pos_freq == 1 ? option::positive_frequencies_only : option::all_frequencies);
427
428 // size N and the number of positive frequencies N_iw
429 long N = h5::read<long>(gr, "size");
430 long N_iw = (pos_freq ? N : (N + 1) / 2);
431
432 // Matsubara frequency domain for backward compatibility
433 if (gr.has_key("domain")) { gr = gr.open_group("domain"); }
434
435 // beta and statistics
436 auto beta = h5::read<double>(gr, "beta");
437 auto stat = (h5::read<std::string>(gr, "statistic") == "F" ? Fermion : Boson);
438
439 m = imfreq{beta, stat, N_iw, opt};
440 }
441
443 C2PY_IGNORE bool eval_to_zero(index_t n) const { return !is_index_valid(n); }
444
446 C2PY_IGNORE bool eval_to_zero(matsubara_freq iw) const { return eval_to_zero(iw.n); }
447
449 C2PY_IGNORE bool eval_to_zero(mesh_point_t mp) const { return eval_to_zero(mp.value()); }
450
451 private:
452 double beta_ = 1.0;
453 statistic_enum stat_ = Fermion;
454 long N_iw_ = 0;
455 option opt_ = option::all_frequencies;
456 long last_index_ = N_iw_ - 1;
457 long first_index_ = -(last_index_ + ((stat_ == Fermion) ? 1 : 0));
458 uint64_t mesh_hash_ = 0;
459 };
460
470 auto evaluate([[maybe_unused]] imfreq const &m, auto const &f, matsubara_freq const &iw) {
471 EXPECTS(m.beta() == iw.beta and m.statistic() == iw.statistic);
472 return f(iw.n);
473 }
474
476
477} // 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.
imfreq()=default
Default constructor constructs an empty mesh.
Mesh point of a triqs::mesh::imfreq mesh.
Definition imfreq.hpp:120
mesh_point_t(double b, statistic_enum stat, index_t n_idx, long d, uint64_t mhash)
Construct a mesh point with the given parameters.
Definition imfreq.hpp:140
uint64_t mesh_hash() const noexcept
Get the hash value of the parent mesh.
Definition imfreq.hpp:153
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
long index() const
Get the Matsubara index of the mesh point.
Definition imfreq.hpp:144
long data_index() const
Get the data index of the mesh point.
Definition imfreq.hpp:147
matsubara_freq const & value() const
Get the corresponding Matsubara frequency .
Definition imfreq.hpp:150
imfreq mesh_t
Parent mesh type.
Definition imfreq.hpp:123
Imaginary frequency mesh type.
Definition imfreq.hpp:102
option
Enum to specify which frequencies should be included in the mesh.
Definition imfreq.hpp:167
bool operator==(imfreq const &m) const
Equal-to comparison operator compares , the particle statistics, and whether all or only positive fr...
Definition imfreq.hpp:213
long n_iw() const noexcept
Get the number of positive Matsubara frequencies .
Definition imfreq.hpp:338
statistic_enum statistic() const noexcept
Get the particle statistics.
Definition imfreq.hpp:335
long first_index() const
Get the first Matsubara index, i.e. .
Definition imfreq.hpp:347
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
Definition imfreq.hpp:390
data_index_t to_data_index(closest_mesh_point_t< value_t > const &cmp) const
Map a Matsubara frequency contained in a triqs::mesh::closest_mesh_point_t to its data index .
Definition imfreq.hpp:253
mesh_point_t operator[](long d) const
Subscript operator to access a mesh point by its data index .
Definition imfreq.hpp:288
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
Definition imfreq.hpp:384
bool eval_to_zero(index_t n) const
Return true if the given Matsubara index is not valid (see is_index_valid()).
Definition imfreq.hpp:443
long size() const noexcept
Get the size of the mesh, i.e. the total number of mesh points.
Definition imfreq.hpp:344
std::complex< double > w_max() const
Get the complex value of the largest positive Matsubara frequency in the mesh.
Definition imfreq.hpp:329
uint64_t mesh_hash() const noexcept
Get the hash value of the mesh.
Definition imfreq.hpp:341
bool eval_to_zero(matsubara_freq iw) const
Return true if the Matsubara index of the given Matsubara frequency is not valid.
Definition imfreq.hpp:446
imfreq(double beta, statistic_enum statistic, energy_t w_max, option opt=option::all_frequencies)
Construct an imaginary frequency mesh with a threshold for the largest positive Matsubara frequency o...
Definition imfreq.hpp:206
friend void h5_read(h5::group g, std::string name, imfreq &m)
Read a triqs::mesh::imfreq mesh from HDF5.
Definition imfreq.hpp:418
double beta() const noexcept
Get the inverse temperature .
Definition imfreq.hpp:332
long last_index() const
Get the last Matsubara index, i.e. .
Definition imfreq.hpp:350
mesh_point_t operator()(long n) const
Function call operator to access a mesh point by its Matsubara index .
Definition imfreq.hpp:308
data_index_t to_data_index(index_t n) const noexcept
Map a Matsubara index to its corresponding data index .
Definition imfreq.hpp:230
index_t to_index(closest_mesh_point_t< value_t > const &cmp) const
Map a Matsubara frequency contained in a triqs::mesh::closest_mesh_point_t to its Matsubara index .
Definition imfreq.hpp:276
bool is_index_valid(index_t n) const
Check if a Matsubara index is valid.
Definition imfreq.hpp:221
auto cend() const
Get a const iterator to the end of the mesh.
Definition imfreq.hpp:365
index_t to_index(data_index_t d) const
Map a data index to the corresponding Matsubara index .
Definition imfreq.hpp:264
data_index_t to_data_index(matsubara_freq const &iw) const noexcept
Map a Matsubara frequency to its data index .
Definition imfreq.hpp:241
friend void h5_write(h5::group g, std::string name, imfreq const &m)
Write a triqs::mesh::imfreq mesh to HDF5.
Definition imfreq.hpp:402
bool positive_only() const
Is the mesh restricted to positive Matsubara frequencies?
Definition imfreq.hpp:353
matsubara_freq to_value(index_t n) const
Map a Matsubara index to its corresponding Matsubara frequency .
Definition imfreq.hpp:317
long data_index_t
Data index type.
Definition imfreq.hpp:111
bool eval_to_zero(mesh_point_t mp) const
Return true if the Matsubara index of the given mesh point is not valid (see is_index_valid()).
Definition imfreq.hpp:449
matsubara_freq value_t
Value type.
Definition imfreq.hpp:105
auto begin() const
Get an iterator to the beginning of the mesh.
Definition imfreq.hpp:356
auto end() const
Get an iterator to the end of the mesh.
Definition imfreq.hpp:362
friend std::ostream & operator<<(std::ostream &sout, imfreq const &m)
Write a triqs::mesh::imfreq mesh to a std::ostream.
Definition imfreq.hpp:374
long index_t
Index type.
Definition imfreq.hpp:108
imfreq get_positive_freq() const
Get a new mesh with the same , particle statistics and but only positive frequencies.
Definition imfreq.hpp:326
auto cbegin() const
Get a const iterator to the beginning of the mesh.
Definition imfreq.hpp:359
static std::string hdf5_format()
Get the HDF5 format tag.
Definition imfreq.hpp:393
imfreq()=default
Default constructor constructs an empty mesh.
mesh_point_t operator[](closest_mesh_point_t< value_t > const &cmp) const
Subscript operator to access a mesh point by a Matsubara frequency contained in a triqs::mesh::close...
Definition imfreq.hpp:298
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 a struct to represent Matsubara frequencies.
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
T value
Use the mesh point closest to this value.
Definition utils.hpp:188
Represents an energy value to distinguish constructors in imaginary-frequency mesh.
Definition imfreq.hpp:50
double value
Energy value.
Definition imfreq.hpp:52
Represents a Matsubara frequency .
double beta
Inverse temperature .
long n
Matsubara index .
statistic_enum statistic
Particle statistics.
matsubara_freq()=default
Default constructor initializes the Matsubara frequency with zero index, zero inverse temperature and...
A generic random access iterator for 1D meshes.
Provides tail fitting for functions defined on frequency meshes.