TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
dlr_imtime.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Simons Foundation
2// Copyright (c) 2023 Hugo U.R. 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 U.R. Strand, Nils Wentzell
18
23
24#pragma once
25
26#include "./mesh_iterator.hpp"
27#include "./utils.hpp"
28#include "./dlr.hpp"
29#include "../utility/macros.hpp"
30
31#include <cppdlr/cppdlr.hpp>
32#include <h5/h5.hpp>
33#include <fmt/format.h>
34#include <itertools/itertools.hpp>
35#include <nda/nda.hpp>
36
37#include <cstdint>
38#include <iostream>
39#include <memory>
40#include <string>
41#include <string_view>
42#include <utility>
43
44namespace triqs::mesh {
45
50
75 class C2PY_RENAME(MeshDLRImTime) dlr_imtime {
76 public:
78 using value_t = double;
79
81 using index_t = long;
82
84 using data_index_t = long;
85
92 class C2PY_IGNORE mesh_point_t {
93 public:
95 using mesh_t = dlr;
96
98 mesh_point_t() = default;
99
109 mesh_point_t(long l, long d, uint64_t mhash, double tau_l) : index_(l), data_index_(d), mesh_hash_(mhash), value_(tau_l) {}
110
112 [[nodiscard]] long index() const { return index_; }
113
115 [[nodiscard]] long data_index() const { return data_index_; }
116
118 [[nodiscard]] double value() const { return value_; }
119
121 [[nodiscard]] uint64_t mesh_hash() const noexcept { return mesh_hash_; }
122
124 operator double() const { return value_; }
125
126 private:
127 long index_ = 0;
128 long data_index_ = 0;
129 uint64_t mesh_hash_ = 0;
130 double value_ = {};
131 };
132
133 private:
134 // Construct an imaginary time DLR mesh with a given set of DLR frequencies.
135 dlr_imtime(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize, nda::vector<double> const &dlr_freq)
136 : dlr_imtime(beta, statistic, w_max, eps, symmetrize,
137 detail::dlr_ops{.freq = dlr_freq,
138 .imt = {w_max * beta, dlr_freq, symmetrize},
139 .imf = {w_max * beta, dlr_freq, static_cast<cppdlr::statistic_t>(statistic), symmetrize}}) {}
140
141 // Construct an imaginary time DLR mesh with given DLR operations.
142 dlr_imtime(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize, detail::dlr_ops ops)
143 : beta_(beta),
144 stat_(statistic),
145 w_max_(w_max),
146 eps_(eps),
147 symmetrize_(symmetrize),
148 mesh_hash_(hash(beta, statistic, w_max, eps, symmetrize, hash_bytes(ops.imf.get_ifnodes()), std::string_view{"dlr_imtime"})),
149 dlr_{std::make_shared<detail::dlr_ops>(std::move(ops))} {}
150
151 public:
153 dlr_imtime() = default;
154
169 dlr_imtime(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize = true)
170 : dlr_imtime(beta, statistic, w_max, eps, symmetrize, cppdlr::build_dlr_rf(w_max * beta, eps, symmetrize)) {}
171
178 template <nda::AnyOf<dlr_imfreq, dlr> M>
179 explicit dlr_imtime(M const &m)
180 : beta_(m.beta_),
181 stat_(m.stat_),
182 w_max_(m.w_max_),
183 eps_(m.eps_),
184 symmetrize_(m.symmetrize_),
185 mesh_hash_(hash(beta_, stat_, w_max_, eps_, symmetrize_, hash_bytes(m.dlr_->imf.get_ifnodes()), std::string_view{"dlr_imtime"})),
186 dlr_(m.dlr_) {}
187
189 bool operator==(dlr_imtime const &m) const { return mesh_hash_ == m.mesh_hash_ and stat_ == m.stat_; }
190
197 [[nodiscard]] bool is_index_valid(long l) const noexcept { return 0 <= l and l < size(); }
198
205 [[nodiscard]] long to_data_index(long l) const noexcept {
206 EXPECTS(is_index_valid(l));
207 return l;
208 }
209
211 [[nodiscard]] C2PY_IGNORE long to_data_index(closest_mesh_point_t<double> const &cmp) const = delete;
212
219 [[nodiscard]] long to_index(long d) const noexcept {
220 EXPECTS(is_index_valid(d));
221 return d;
222 }
223
231 [[nodiscard]] mesh_point_t operator[](long d) const { return (*this)(d); }
232
240 [[nodiscard]] mesh_point_t operator()(long l) const { return {l, l, mesh_hash_, to_value(l)}; }
241
248 [[nodiscard]] double to_value(long l) const noexcept {
249 EXPECTS(is_index_valid(l));
250 auto res = dlr_->imt.get_itnodes()[l] * beta_;
251 if (res < 0) res = beta_ + res;
252 return res;
253 }
254
256 [[nodiscard]] C2PY_PROPERTY_GET(beta) double beta() const noexcept { return beta_; }
257
259 [[nodiscard]] C2PY_PROPERTY_GET(statistic) statistic_enum statistic() const noexcept { return stat_; }
260
262 [[nodiscard]] C2PY_PROPERTY_GET(w_max) double w_max() const noexcept { return w_max_; }
263
265 [[nodiscard]] C2PY_PROPERTY_GET(eps) double eps() const noexcept { return eps_; }
266
268 [[nodiscard]] C2PY_PROPERTY_GET(symmetrize) bool symmetrize() const noexcept { return symmetrize_; }
269
271 [[nodiscard]] C2PY_PROPERTY_GET(dlr_freq) auto const &dlr_freq() const { return dlr_->freq; }
272
274 [[nodiscard]] C2PY_IGNORE auto const &dlr_it() const { return dlr_->imt; }
275
277 [[nodiscard]] C2PY_IGNORE auto const &dlr_if() const { return dlr_->imf; }
278
280 [[nodiscard]] C2PY_PROPERTY_GET(mesh_hash) uint64_t mesh_hash() const noexcept { return mesh_hash_; }
281
283 [[nodiscard]] long size() const noexcept { return (dlr_ ? dlr_->imt.get_itnodes().size() : 0); }
284
286 [[nodiscard]] auto begin() const { return mesh_iterator<dlr_imtime>{.mesh_ptr = this, .data_index = 0}; }
287
289 [[nodiscard]] auto cbegin() const { return begin(); }
290
292 [[nodiscard]] auto end() const { return mesh_iterator<dlr_imtime>{.mesh_ptr = this, .data_index = size()}; }
293
295 [[nodiscard]] auto cend() const { return end(); }
296
304 friend std::ostream &operator<<(std::ostream &sout, dlr_imtime const &m) {
305 auto stat_cstr = (m.stat_ == Boson ? "Boson" : "Fermion");
306 return sout << fmt::format("DLR imaginary time mesh of size {} with beta = {}, statistics = {}, w_max = {}, eps = {}, symmetrized = {}",
307 m.size(), m.beta_, stat_cstr, m.w_max_, m.eps_, m.symmetrize_);
308 }
309
314 void serialize(auto &ar) const {
315 EXPECTS(dlr_);
316 ar & beta_ & stat_ & w_max_ & eps_ & symmetrize_ & mesh_hash_ & dlr_->freq;
317 dlr_->imt.serialize(ar);
318 dlr_->imf.serialize(ar);
319 }
320
325 void deserialize(auto &ar) {
326 nda::vector<double> freq;
327 cppdlr::imtime_ops imt;
328 cppdlr::imfreq_ops imf;
329 ar & beta_ & stat_ & w_max_ & eps_ & symmetrize_ & mesh_hash_ & freq;
330 imt.deserialize(ar);
331 imf.deserialize(ar);
332 dlr_ = std::make_shared<detail::dlr_ops>(freq, imt, imf);
333 }
334
336 [[nodiscard]] static std::string hdf5_format() { return "MeshDLRImTime"; }
337
345 friend void h5_write(h5::group g, std::string const &name, dlr_imtime const &m) {
346 h5::group gr = g.create_group(name);
347 h5::write_hdf5_format(gr, m); // NOLINT (downcasting to base class)
348 h5::write(gr, "beta", m.beta_);
349 h5::write(gr, "statistic", (m.stat_ == Fermion ? "F" : "B"));
350 h5::write(gr, "w_max", m.w_max_);
351 h5::write(gr, "eps", m.eps_);
352 h5::write(gr, "symmetrize", m.symmetrize_);
353 h5::write(gr, "dlr_freq", m.dlr_freq());
354 h5::write(gr, "dlr_it", m.dlr_it());
355 h5::write(gr, "dlr_if", m.dlr_if());
356 }
357
365 friend void h5_read(h5::group g, std::string const &name, dlr_imtime &m) {
366 h5::group gr = g.open_group(name);
367 h5::assert_hdf5_format(gr, m, true); // NOLINT (downcasting to base class)
368 auto b = h5::read<double>(gr, "beta");
369 auto stat = (h5::read<std::string>(gr, "statistic") == "F" ? Fermion : Boson);
370 auto wmax = h5::read<double>(gr, "w_max");
371 auto epsilon = h5::read<double>(gr, "eps");
372 bool sym = false;
373 h5::try_read(gr, "symmetrize", sym);
374 auto freq = h5::read<nda::vector<double>>(gr, "dlr_freq");
375 auto imt = h5::read<cppdlr::imtime_ops>(gr, "dlr_it");
376 auto imf = h5::read<cppdlr::imfreq_ops>(gr, "dlr_if");
377 m = dlr_imtime{b, stat, wmax, epsilon, sym, {.freq = freq, .imt = imt, .imf = imf}};
378 }
379
380 // Friend declarations.
381 friend class dlr_imfreq;
382 friend class dlr;
383
384 private:
385 double beta_ = 1.0;
386 statistic_enum stat_ = Fermion;
387 double w_max_ = 0.0;
388 double eps_ = 1e-10;
389 bool symmetrize_ = false;
390 uint64_t mesh_hash_ = 0;
391 std::shared_ptr<const detail::dlr_ops> dlr_ = {};
392 };
393
398 double evaluate(dlr_imtime const &m, ...) = delete;
399
401
402} // 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.
Imaginary frequency discrete Lehmann representation (DLR) mesh type.
Mesh point of a triqs::mesh::dlr_imtime mesh.
double value() const
Get the value of the mesh point.
long index() const
Get the index of the mesh point.
mesh_point_t(long l, long d, uint64_t mhash, double tau_l)
Construct a mesh point with a given index , data index , hash value of the parent mesh and value .
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.
long data_index() const
Get the data index of the mesh point.
Imaginary time discrete Lehmann representation (DLR) mesh type.
double value_t
Value type.
static std::string hdf5_format()
Get the HDF5 format tag.
double beta() const noexcept
Get the inverse temperature .
mesh_point_t operator()(long l) const
Function call operator to access a mesh point by its index .
friend void h5_write(h5::group g, std::string const &name, dlr_imtime const &m)
Write a triqs::mesh::dlr_imtime mesh to HDF5.
bool symmetrize() const noexcept
Is the mesh symmetric around ?
bool operator==(dlr_imtime const &m) const
Equal-to comparison operator compares the hash values.
long to_index(long d) const noexcept
Map a data index to the corresponding index .
double w_max() const noexcept
Get the DLR energy cutoff .
dlr_imtime(M const &m)
Construct an imaginary time DLR mesh from another DLR type mesh.
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).
long data_index_t
Data index type.
statistic_enum statistic() const noexcept
Get the particle statistics.
friend void h5_read(h5::group g, std::string const &name, dlr_imtime &m)
Read a triqs::mesh::dlr_imtime mesh from HDF5.
long to_data_index(long l) const noexcept
Map an index to its corresponding data index .
bool is_index_valid(long l) const noexcept
Check if an index is valid.
auto cend() const
Get a const iterator to the end of the mesh.
auto end() const
Get an iterator to the end of the mesh.
double eps() const noexcept
Get the DLR error tolerance .
long index_t
Index type.
mesh_point_t operator[](long d) const
Subscript operator to access a mesh point by its data index .
auto const & dlr_it() const
Get the imaginary time DLR operations object (see also cppdlr::imtime_ops).
double to_value(long l) const noexcept
Map an index to its corresponding value .
auto cbegin() const
Get a const iterator to the beginning of the mesh.
auto begin() const
Get an iterator to the beginning of the mesh.
uint64_t mesh_hash() const noexcept
Get the hash value 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_imtime const &m)
Write a triqs::mesh::dlr_imtime mesh to a std::ostream.
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
dlr_imtime(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize=true)
Construct an imaginary time DLR mesh with a given energy cutoff and error tolerance .
auto const & dlr_freq() const
Get the array of DLR frequencies .
long to_data_index(closest_mesh_point_t< double > const &cmp) const =delete
Mapping of a value to the data index of the closest mesh point is deleted.
dlr_imtime()=default
Default constructor constructs an empty mesh.
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 mesh type for the discrete Lehmann representation.
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.