31#include <cppdlr/cppdlr.hpp>
33#include <fmt/format.h>
34#include <itertools/itertools.hpp>
44namespace triqs::mesh {
50 nda::vector<double> freq;
51 cppdlr::imtime_ops imt;
52 cppdlr::imfreq_ops imf;
96 class C2PY_RENAME(MeshDLR) dlr {
130 mesh_point_t(
long l,
long d, uint64_t mhash,
double w_l) : index_(l), data_index_(d), mesh_hash_(mhash), value_(w_l) {}
133 [[nodiscard]]
long index()
const {
return index_; }
136 [[nodiscard]]
long data_index()
const {
return data_index_; }
139 [[nodiscard]]
double value()
const {
return value_; }
142 [[nodiscard]] uint64_t
mesh_hash() const noexcept {
return mesh_hash_; }
145 operator double()
const {
return value_; }
149 long data_index_ = 0;
150 uint64_t mesh_hash_ = 0;
156 dlr(
double beta, statistic_enum statistic,
double w_max,
double eps,
bool symmetrize, nda::vector<double>
const &dlr_freq)
157 : dlr(beta, statistic, w_max, eps, symmetrize,
158 detail::dlr_ops{.freq = dlr_freq,
159 .imt = {w_max * beta, dlr_freq, symmetrize},
160 .imf = {w_max * beta, dlr_freq,
static_cast<cppdlr::statistic_t
>(statistic), symmetrize}}) {}
163 dlr(
double beta, statistic_enum statistic,
double w_max,
double eps,
bool symmetrize, detail::dlr_ops ops)
168 symmetrize_(symmetrize),
169 mesh_hash_(
hash(beta, statistic, w_max, eps, symmetrize,
hash_bytes(ops.imf.get_ifnodes()), std::string_view{
"dlr"})),
170 dlr_{std::make_shared<detail::dlr_ops>(std::move(ops))} {}
199 template <nda::AnyOf<dlr_imtime, dlr_imfreq> M>
205 symmetrize_(m.symmetrize_),
206 mesh_hash_(
hash(beta_, stat_, w_max_, eps_, symmetrize_,
hash_bytes(m.dlr_->imf.get_ifnodes()), std::string_view{
"dlr"})),
210 bool operator==(dlr
const &m)
const {
return mesh_hash_ == m.mesh_hash_; }
237 [[nodiscard]]
long to_index(
long d)
const noexcept {
266 [[nodiscard]]
double to_value(
long l)
const noexcept {
268 return (dlr_->freq)[l];
272 [[nodiscard]] C2PY_PROPERTY_GET(
beta)
double beta() const noexcept {
return beta_; }
278 [[nodiscard]] C2PY_PROPERTY_GET(
w_max)
double w_max() const noexcept {
return w_max_; }
281 [[nodiscard]] C2PY_PROPERTY_GET(
eps)
double eps() const noexcept {
return eps_; }
287 [[nodiscard]] C2PY_PROPERTY_GET(
dlr_freq)
auto const &
dlr_freq()
const {
return dlr_->freq; }
290 [[nodiscard]] C2PY_IGNORE
auto const &
dlr_it()
const {
return dlr_->imt; }
293 [[nodiscard]] C2PY_IGNORE
auto const &
dlr_if()
const {
return dlr_->imf; }
296 [[nodiscard]] C2PY_PROPERTY_GET(
mesh_hash) uint64_t
mesh_hash() const noexcept {
return mesh_hash_; }
299 [[nodiscard]]
long size() const noexcept {
return (dlr_ ? dlr_->freq.size() : 0); }
311 [[nodiscard]]
auto cend()
const {
return end(); }
320 friend std::ostream &
operator<<(std::ostream &sout, dlr
const &m) {
321 auto stat_cstr = (m.stat_ == Boson ?
"Boson" :
"Fermion");
322 return sout << fmt::format(
"DLR coefficient mesh of size {} with beta = {}, statistics = {}, w_max = {}, eps = {}, symmetrized = {}", m.
size(),
323 m.beta_, stat_cstr, m.w_max_, m.eps_, m.symmetrize_);
332 ar & beta_ & stat_ & w_max_ & eps_ & symmetrize_ & mesh_hash_ & dlr_->freq;
333 dlr_->imt.serialize(ar);
334 dlr_->imf.serialize(ar);
342 nda::vector<double> freq;
343 cppdlr::imtime_ops imt;
344 cppdlr::imfreq_ops imf;
345 ar & beta_ & stat_ & w_max_ & eps_ & symmetrize_ & mesh_hash_ & freq;
348 dlr_ = std::make_shared<detail::dlr_ops>(freq, imt, imf);
352 [[nodiscard]]
static std::string
hdf5_format() {
return "MeshDLR"; }
361 friend void h5_write(h5::group g, std::string
const &name, dlr
const &m) {
362 h5::group gr = g.create_group(name);
363 h5::write_hdf5_format(gr, m);
364 h5::write(gr,
"beta", m.beta_);
365 h5::write(gr,
"statistic", (m.stat_ == Fermion ?
"F" :
"B"));
366 h5::write(gr,
"w_max", m.w_max_);
367 h5::write(gr,
"eps", m.eps_);
368 h5::write(gr,
"symmetrize", m.symmetrize_);
369 h5::write(gr,
"dlr_freq", m.
dlr_freq());
370 h5::write(gr,
"dlr_it", m.
dlr_it());
371 h5::write(gr,
"dlr_if", m.
dlr_if());
381 friend void h5_read(h5::group g, std::string
const &name, dlr &m) {
382 h5::group gr = g.open_group(name);
383 h5::assert_hdf5_format(gr, m,
true);
384 auto b = h5::read<double>(gr,
"beta");
385 auto stat = (h5::read<std::string>(gr,
"statistic") ==
"F" ? Fermion : Boson);
386 auto wmax = h5::read<double>(gr,
"w_max");
387 auto epsilon = h5::read<double>(gr,
"eps");
389 h5::try_read(gr,
"symmetrize", sym);
390 auto freq = h5::read<nda::vector<double>>(gr,
"dlr_freq");
391 auto imt = h5::read<cppdlr::imtime_ops>(gr,
"dlr_it");
392 auto imf = h5::read<cppdlr::imfreq_ops>(gr,
"dlr_if");
393 m = dlr(b, stat, wmax, epsilon, sym, {.freq = freq, .imt = imt, .imf = imf});
405 bool symmetrize_ =
false;
406 uint64_t mesh_hash_ = 0;
407 std::shared_ptr<const detail::dlr_ops> dlr_ = {};
429 auto evaluate(
dlr const &m,
auto const &f,
double tau) {
430 EXPECTS(m.
size() > 0);
431 EXPECTS(tau >= 0 and tau <= m.
beta());
432 return detail::sum_to_regular(nda::range(m.
size()), [&](
auto l) { return f(l) * cppdlr::k_it(tau / m.beta(), m.dlr_freq()[l]); });
454 EXPECTS(m.
size() > 0);
455 return detail::sum_to_regular(nda::range(m.
size()),
456 [&](
auto l) { return f(l) * cppdlr::k_if(iw.n, m.dlr_freq()[l], (cppdlr::statistic_t)iw.statistic) * m.beta(); });
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 mesh.
mesh_point_t(long l, long d, uint64_t mhash, double w_l)
Construct a mesh point with a given index , data index , hash value of the parent mesh and value .
uint64_t mesh_hash() const noexcept
Get the hash value of the parent mesh.
dlr mesh_t
Parent mesh type.
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
long index() const
Get the index of the mesh point.
long data_index() const
Get the data index of the mesh point.
double value() const
Get the value of the mesh point.
Imaginary frequency discrete Lehmann representation (DLR) mesh type.
Imaginary time discrete Lehmann representation (DLR) mesh type.
Discrete Lehmann representation (DLR) mesh type.
mesh_point_t operator[](long d) const
Subscript operator to access a mesh point by its data index .
double to_value(long l) const noexcept
Map an index to its corresponding value .
long to_data_index(long l) const noexcept
Map an index to its corresponding data index .
auto const & dlr_freq() const
Get the array of DLR frequencies .
friend std::ostream & operator<<(std::ostream &sout, dlr const &m)
Write a triqs::mesh::dlr mesh to a std::ostream.
double beta() const noexcept
Get the inverse temperature .
long size() const noexcept
Get the size of the mesh, i.e. the DLR rank .
dlr(M const &m)
Construct a DLR mesh from another DLR type mesh.
bool operator==(dlr const &m) const
Equal-to comparison operator compares the hash values.
statistic_enum statistic() const noexcept
Get the particle statistics.
double w_max() const noexcept
Get the DLR energy cutoff .
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
auto begin() const
Get an iterator to the beginning of the mesh.
mesh_point_t operator()(long l) const
Function call operator to access a mesh point by its index .
auto const & dlr_it() const
Get the imaginary time DLR operations object (see also cppdlr::imtime_ops).
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
uint64_t mesh_hash() const noexcept
Get the hash value of the mesh.
auto const & dlr_if() const
Get the Matsubara frequency DLR operations object (see also cppdlr::imfreq_ops).
long data_index_t
Data index type.
dlr(double beta, statistic_enum statistic, double w_max, double eps, bool symmetrize=true)
Construct a DLR mesh with a given energy cutoff and error tolerance .
auto cbegin() const
Get a const iterator to the beginning of the mesh.
bool symmetrize() const noexcept
Is the mesh symmetric around ?
double value_t
Value type.
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.
friend void h5_write(h5::group g, std::string const &name, dlr const &m)
Write a triqs::mesh::dlr mesh to HDF5.
double eps() const noexcept
Get the DLR error tolerance .
bool is_index_valid(long l) const noexcept
Check if an index is valid.
static std::string hdf5_format()
Get the HDF5 format tag.
dlr()=default
Default constructor constructs an empty mesh.
long to_index(long d) const noexcept
Map a data index to the corresponding index .
friend void h5_read(h5::group g, std::string const &name, dlr &m)
Read a triqs::mesh::dlr mesh from HDF5.
statistic_enum
Enum to specify particle statistics.
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>.
uint64_t hash(Ts &&...ts)
Generic hash function for multiple arguments.
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.
Represents a Matsubara frequency .
A generic random access iterator for 1D meshes.