TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
dlr_imfreq.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Simons Foundation
2// Copyright (c) 2023 Hugo Strand
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You may obtain a copy of the License at
15// https://www.gnu.org/licenses/gpl-3.0.txt
16//
17// Authors: Alexander Hampel, Olivier Parcollet, Hugo Strand, Nils Wentzell
18
23
24#pragma once
25
26#include "./matsubara_freq.hpp"
27#include "./mesh_iterator.hpp"
28#include "./utils.hpp"
29#include "./dlr.hpp"
30#include "../utility/macros.hpp"
31
32#include <cppdlr/cppdlr.hpp>
33#include <h5/h5.hpp>
34#include <fmt/format.h>
35#include <itertools/itertools.hpp>
36#include <nda/nda.hpp>
37
38#include <cstdint>
39#include <iostream>
40#include <memory>
41#include <string>
42#include <string_view>
43#include <utility>
44
45namespace triqs::mesh {
46
51
80 class C2PY_RENAME(MeshDLRImFreq) dlr_imfreq {
81 public:
84
86 using index_t = long;
87
89 using data_index_t = long;
90
98 class C2PY_IGNORE mesh_point_t : public matsubara_freq {
99 public:
101 using mesh_t = dlr_imfreq;
102
104 mesh_point_t() = default;
105
118 mesh_point_t(double b, statistic_enum stat, long n_l, long d, uint64_t mhash)
119 : matsubara_freq(n_l, b, stat), data_index_(d), mesh_hash_(mhash) {}
120
122 [[nodiscard]] long index() const { return n; }
123
125 [[nodiscard]] long data_index() const { return data_index_; }
126
128 [[nodiscard]] matsubara_freq const &value() const { return *this; }
129
131 [[nodiscard]] uint64_t mesh_hash() const noexcept { return mesh_hash_; }
132
133 private:
134 long data_index_ = 0;
135 uint64_t mesh_hash_ = 0;
136 };
137
138 private:
139 // Construct an imaginary frequency DLR mesh with a given set of DLR frequencies.
140 dlr_imfreq(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize, nda::vector<double> const &dlr_freq)
141 : dlr_imfreq(beta, statistic, w_max, eps, symmetrize,
142 detail::dlr_ops{.freq = dlr_freq,
143 .imt = {w_max * beta, dlr_freq, symmetrize},
144 .imf = {w_max * beta, dlr_freq, static_cast<cppdlr::statistic_t>(statistic), symmetrize}}) {}
145
146 // Construct an imaginary frequency DLR mesh with given DLR operations.
147 dlr_imfreq(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize, detail::dlr_ops dlr)
148 : beta_(beta),
149 stat_(statistic),
150 w_max_(w_max),
151 eps_(eps),
152 symmetrize_(symmetrize),
153 mesh_hash_(hash(beta, statistic, w_max, eps, symmetrize, hash_bytes(dlr.imf.get_ifnodes()), std::string_view{"dlr_imfreq"})),
154 dlr_{std::make_shared<detail::dlr_ops>(std::move(dlr))} {}
155
156 public:
158 dlr_imfreq() = default;
159
174 dlr_imfreq(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize = true)
175 : dlr_imfreq(beta, statistic, w_max, eps, symmetrize, cppdlr::build_dlr_rf(w_max * beta, eps, symmetrize)) {}
176
183 template <nda::AnyOf<dlr_imtime, dlr> M>
184 explicit dlr_imfreq(M const &m)
185 : beta_(m.beta_),
186 stat_(m.stat_),
187 w_max_(m.w_max_),
188 eps_(m.eps_),
189 symmetrize_(m.symmetrize_),
190 mesh_hash_(hash(beta_, stat_, w_max_, eps_, symmetrize_, hash_bytes(m.dlr_->imf.get_ifnodes()), std::string_view{"dlr_imfreq"})),
191 dlr_(m.dlr_) {}
192
194 bool operator==(dlr_imfreq const &m) const { return mesh_hash_ == m.mesh_hash_; }
195
202 [[nodiscard]] bool is_index_valid(long l) const noexcept { return 0 <= l and l < size(); }
203
210 [[nodiscard]] long to_data_index(long l) const noexcept {
211 EXPECTS(is_index_valid(l));
212 return l;
213 }
214
221 [[nodiscard]] long to_index(long d) const noexcept {
222 EXPECTS(is_index_valid(d));
223 return d;
224 }
225
233 [[nodiscard]] mesh_point_t operator[](long d) const { return (*this)(d); }
234
242 [[nodiscard]] mesh_point_t operator()(long l) const {
243 EXPECTS(is_index_valid(l));
244 return {beta_, stat_, dlr_->imf.get_ifnodes()[l], l, mesh_hash_};
245 }
246
254 [[nodiscard]] matsubara_freq to_value(long l) const noexcept {
255 EXPECTS(is_index_valid(l));
256 return {dlr_->imf.get_ifnodes()[l], beta_, stat_};
257 }
258
260 [[nodiscard]] C2PY_PROPERTY_GET(beta) double beta() const noexcept { return beta_; }
261
263 [[nodiscard]] C2PY_PROPERTY_GET(statistic) statistic_enum statistic() const noexcept { return stat_; }
264
266 [[nodiscard]] C2PY_PROPERTY_GET(w_max) double w_max() const noexcept { return w_max_; }
267
269 [[nodiscard]] C2PY_PROPERTY_GET(eps) double eps() const noexcept { return eps_; }
270
272 [[nodiscard]] C2PY_PROPERTY_GET(symmetrize) bool symmetrize() const noexcept { return symmetrize_; }
273
275 [[nodiscard]] C2PY_PROPERTY_GET(dlr_freq) auto const &dlr_freq() const { return dlr_->freq; }
276
278 [[nodiscard]] C2PY_IGNORE auto const &dlr_it() const { return dlr_->imt; }
279
281 [[nodiscard]] C2PY_IGNORE auto const &dlr_if() const { return dlr_->imf; }
282
284 [[nodiscard]] C2PY_PROPERTY_GET(mesh_hash) uint64_t mesh_hash() const noexcept { return mesh_hash_; }
285
287 [[nodiscard]] auto min_max_frequencies() const noexcept {
288 return std::pair<matsubara_freq, matsubara_freq>{(*this)(0).value(), (*this)(size() - 1).value()};
289 }
290
292 [[nodiscard]] long size() const noexcept { return (dlr_ ? dlr_->imf.get_ifnodes().size() : 0); }
293
295 [[nodiscard]] auto begin() const { return mesh_iterator<dlr_imfreq>{.mesh_ptr = this, .data_index = 0}; }
296
298 [[nodiscard]] auto cbegin() const { return begin(); }
299
301 [[nodiscard]] auto end() const { return mesh_iterator<dlr_imfreq>{.mesh_ptr = this, .data_index = size()}; }
302
304 [[nodiscard]] auto cend() const { return end(); }
305
313 friend std::ostream &operator<<(std::ostream &sout, dlr_imfreq const &m) {
314 auto stat_cstr = (m.stat_ == Boson ? "Boson" : "Fermion");
315 return sout << fmt::format("DLR imaginary frequency mesh of size {} with beta = {}, statistics = {}, w_max = {}, eps = {}, symmetrized = {}",
316 m.size(), m.beta_, stat_cstr, m.w_max_, m.eps_, m.symmetrize_);
317 }
318
323 void serialize(auto &ar) const {
324 EXPECTS(dlr_);
325 ar & beta_ & stat_ & w_max_ & eps_ & symmetrize_ & mesh_hash_ & dlr_->freq;
326 dlr_->imt.serialize(ar);
327 dlr_->imf.serialize(ar);
328 }
329
334 void deserialize(auto &ar) {
335 nda::vector<double> freq;
336 cppdlr::imtime_ops imt;
337 cppdlr::imfreq_ops imf;
338 ar & beta_ & stat_ & w_max_ & eps_ & symmetrize_ & mesh_hash_ & freq;
339 imt.deserialize(ar);
340 imf.deserialize(ar);
341 dlr_ = std::make_shared<detail::dlr_ops>(freq, imt, imf);
342 }
343
345 [[nodiscard]] static std::string hdf5_format() { return "MeshDLRImFreq"; }
346
354 friend void h5_write(h5::group g, std::string const &name, dlr_imfreq const &m) {
355 h5::group gr = g.create_group(name);
356 h5::write_hdf5_format(gr, m); // NOLINT (downcasting to base class)
357 h5::write(gr, "beta", m.beta_);
358 h5::write(gr, "statistic", (m.stat_ == Fermion ? "F" : "B"));
359 h5::write(gr, "w_max", m.w_max_);
360 h5::write(gr, "eps", m.eps_);
361 h5::write(gr, "symmetrize", m.symmetrize_);
362 h5::write(gr, "dlr_freq", m.dlr_freq());
363 h5::write(gr, "dlr_it", m.dlr_it());
364 h5::write(gr, "dlr_if", m.dlr_if());
365 }
366
374 friend void h5_read(h5::group g, std::string const &name, dlr_imfreq &m) {
375 h5::group gr = g.open_group(name);
376 h5::assert_hdf5_format(gr, m, true); // NOLINT (downcasting to base class)
377 auto b = h5::read<double>(gr, "beta");
378 auto stat = (h5::read<std::string>(gr, "statistic") == "F" ? Fermion : Boson);
379 auto wmax = h5::read<double>(gr, "w_max");
380 auto epsilon = h5::read<double>(gr, "eps");
381 bool sym = false;
382 h5::try_read(gr, "symmetrize", sym);
383 auto freq = h5::read<nda::vector<double>>(gr, "dlr_freq");
384 auto imt = h5::read<cppdlr::imtime_ops>(gr, "dlr_it");
385 auto imf = h5::read<cppdlr::imfreq_ops>(gr, "dlr_if");
386 m = dlr_imfreq(b, stat, wmax, epsilon, sym, {.freq = freq, .imt = imt, .imf = imf});
387 }
388
389 // Friend declarations.
390 friend class dlr_imtime;
391 friend class dlr;
392
393 private:
394 double beta_ = 1.0;
395 statistic_enum stat_ = Fermion;
396 double w_max_ = 0.0;
397 double eps_ = 1e-10;
398 bool symmetrize_ = false;
399 uint64_t mesh_hash_ = 0;
400 std::shared_ptr<const detail::dlr_ops> dlr_ = {};
401 };
402
407 double evaluate(dlr_imfreq const &m, ...) = delete;
408
410
411} // 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.
Mesh point of a triqs::mesh::dlr_imfreq mesh.
dlr_imfreq mesh_t
Parent mesh type.
long index() const
Get the Matsubara index of the mesh point.
mesh_point_t(double b, statistic_enum stat, long n_l, long d, uint64_t mhash)
Construct a mesh point with the given parameters.
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
uint64_t mesh_hash() const noexcept
Get the hash value of the parent mesh.
matsubara_freq const & value() const
Get the corresponding Matsubara frequency .
long data_index() const
Get the data index of the mesh point.
Imaginary frequency discrete Lehmann representation (DLR) mesh type.
bool is_index_valid(long l) const noexcept
Check if an index is valid.
long to_data_index(long l) const noexcept
Map an index to its corresponding data index .
mesh_point_t operator[](long d) const
Subscript operator to access a mesh point by its data index .
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
long index_t
Index type.
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
auto const & dlr_if() const
Get the Matsubara frequency DLR operations object (see also cppdlr::imfreq_ops).
dlr_imfreq(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize=true)
Construct an imaginary frequency DLR mesh with a given energy cutoff and error tolerance .
friend void h5_read(h5::group g, std::string const &name, dlr_imfreq &m)
Read a triqs::mesh::dlr_imfreq mesh from HDF5.
auto end() const
Get an iterator to the end of the mesh.
double w_max() const noexcept
Get the DLR energy cutoff .
statistic_enum statistic() const noexcept
Get the particle statistics.
auto cend() const
Get a const iterator to the end of the mesh.
long size() const noexcept
Get the size of the mesh, i.e. the DLR rank .
friend std::ostream & operator<<(std::ostream &sout, dlr_imfreq const &m)
Write a triqs::mesh::dlr_imfreq mesh to a std::ostream.
auto begin() const
Get an iterator to the beginning of the mesh.
bool symmetrize() const noexcept
Is the mesh symmetric around ?
friend void h5_write(h5::group g, std::string const &name, dlr_imfreq const &m)
Write a triqs::mesh::dlr_imfreq mesh to HDF5.
auto cbegin() const
Get a const iterator to the beginning of the mesh.
dlr_imfreq()=default
Default constructor constructs an empty mesh.
matsubara_freq to_value(long l) const noexcept
Map an index to its corresponding Matsubara frequency .
long to_index(long d) const noexcept
Map a data index to the corresponding index .
double beta() const noexcept
Get the inverse temperature .
matsubara_freq value_t
Value type.
mesh_point_t operator()(long l) const
Function call operator to access a mesh point by its index .
bool operator==(dlr_imfreq const &m) const
Equal-to comparison operator compares the hash values.
long data_index_t
Data index type.
dlr_imfreq(M const &m)
Construct an imaginary frequency DLR mesh from another DLR type mesh.
auto min_max_frequencies() const noexcept
Get a pair containing the smallest and largest Matsubara frequency in the mesh.
static std::string hdf5_format()
Get the HDF5 format tag.
uint64_t mesh_hash() const noexcept
Get the hash value of the mesh.
double eps() const noexcept
Get the DLR error tolerance .
auto const & dlr_freq() const
Get the array of DLR frequencies .
auto const & dlr_it() const
Get the imaginary time DLR operations object (see also cppdlr::imtime_ops).
Imaginary time discrete Lehmann representation (DLR) mesh type.
Discrete Lehmann representation (DLR) mesh type.
Definition dlr.hpp:96
statistic_enum
Enum to specify particle statistics.
Definition utils.hpp:163
std::size_t hash_bytes(std::span< std::byte const > bytes)
Hash the raw bytes of a span via the standard library's std::hash<std::string_view>.
Definition utils.hpp:80
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 a mesh type for the discrete Lehmann representation.
Provides various utilities used with Meshes.
Provides a generic random access iterator for 1D meshes.
Represents a Matsubara frequency .
long n
Matsubara index .
matsubara_freq()=default
Default constructor initializes the Matsubara frequency with zero index, zero inverse temperature and...
A generic random access iterator for 1D meshes.