29#include <fmt/format.h>
39namespace triqs::mesh {
110 mesh_point_t(
long n,
long d, uint64_t mhash,
double w) : index_(n), data_index_(d), mesh_hash_(mhash), value_(w) {}
113 [[nodiscard]]
long index() const noexcept {
return index_; }
116 [[nodiscard]]
long data_index() const noexcept {
return data_index_; }
119 [[nodiscard]]
double value() const noexcept {
return value_; }
122 [[nodiscard]] uint64_t
mesh_hash() const noexcept {
return mesh_hash_; }
125 operator double()
const {
return value_; }
129 template <typename U> friend auto operator OP(mesh_point_t const &mp, U &&y) { return mp.value() OP std::forward<U>(y); } \
130 template <typename U> \
131 requires(not std::is_same_v<std::decay_t<U>, mesh_point_t>) \
132 friend auto operator OP(U &&x, mesh_point_t const &mp) { \
133 return std::forward<U>(x) OP mp.value(); \
143 long data_index_ = 0;
144 uint64_t mesh_hash_ = 0;
160 EXPECTS(w_max_ >= eps_);
161 EXPECTS(ratio_ > 1.0);
182 [[nodiscard]]
bool is_value_valid(
double w)
const noexcept {
return !pts_.empty() and pts_.front() <= w and w <= pts_.back(); }
225 auto itr_r = std::ranges::lower_bound(pts_, cmp.value);
226 long i_r = itr_r - pts_.begin();
228 if (i_r == 0) {
return 0; }
229 if (i_r ==
size()) {
return size() - 1; }
232 if (std::abs(cmp.value - pts_[i_l]) < std::abs(cmp.value - pts_[i_r]))
262 return {n, n, mesh_hash_, pts_[n]};
277 [[nodiscard]] C2PY_PROPERTY_GET(
eps)
double eps() const noexcept {
return eps_; }
280 [[nodiscard]] C2PY_PROPERTY_GET(
w_max)
double w_max() const noexcept {
return w_max_; }
283 [[nodiscard]] C2PY_PROPERTY_GET(
ratio)
double ratio() const noexcept {
return ratio_; }
286 [[nodiscard]] C2PY_PROPERTY_GET(
mesh_hash) uint64_t
mesh_hash() const noexcept {
return mesh_hash_; }
289 [[nodiscard]]
long size() const noexcept {
return static_cast<long>(pts_.size()); }
292 [[nodiscard]] C2PY_PROPERTY_GET(
points) std::vector<double>
const &
points() const noexcept {
return pts_; }
304 [[nodiscard]]
auto cend()
const {
return end(); }
314 return sout << fmt::format(
"Logarithmic real frequency mesh with eps = {}, w_max = {}, ratio = {}, N = {}", m.eps_, m.w_max_, m.ratio_,
322 void serialize(
auto &ar)
const { ar & eps_ & w_max_ & ratio_ & mesh_hash_; }
329 ar & eps_ & w_max_ & ratio_ & mesh_hash_;
330 if (w_max_ > 0) init_points();
334 [[nodiscard]]
static std::string
hdf5_format() {
return "MeshReFreqLog"; }
344 h5::group gr = g.create_group(name);
345 h5::write_hdf5_format(gr, m);
346 h5::write(gr,
"eps", m.eps_);
347 h5::write(gr,
"w_max", m.w_max_);
348 h5::write(gr,
"ratio", m.ratio_);
359 h5::group gr = g.open_group(name);
360 h5::assert_hdf5_format(gr, m,
true);
361 auto e = h5::read<double>(gr,
"eps");
362 auto w = h5::read<double>(gr,
"w_max");
363 auto r = h5::read<double>(gr,
"ratio");
371 for (
double w = w_max_; w >= eps_; w /= ratio_) {
375 std::ranges::sort(pts_);
381 uint64_t mesh_hash_ = 0;
382 std::vector<double> pts_;
401 inline auto evaluate(
refreq_log const &m,
auto const &f,
double w) {
404 auto const &pts = m.
points();
405 auto itr_r = std::ranges::lower_bound(pts, w);
406 long i_r = itr_r - pts.begin();
408 if (i_r == 0) {
return f(0); }
409 if (i_r == m.
size()) {
return f(m.
size() - 1); }
413 double w_l = pts[i_l];
414 double w_r = pts[i_r];
415 double del = w_r - w_l;
417 double wt_r = (w - w_l) / del;
418 double wt_l = (w_r - w) / del;
420 return f(i_l) * wt_l + f(i_r) * wt_r;
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 of a triqs::mesh::refreq_log mesh.
double value() const noexcept
Get the value of the mesh point.
mesh_point_t(long n, long d, uint64_t mhash, double w)
Construct a mesh point with a given index, data index, hash value and value.
refreq_log mesh_t
Parent mesh type.
long data_index() const noexcept
Get the data index of the mesh point.
long index() const noexcept
Get the index of the mesh point.
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.
Logarithmic real frequency mesh type.
friend std::ostream & operator<<(std::ostream &sout, refreq_log const &m)
Write a triqs::mesh::refreq_log mesh to a std::ostream.
bool is_value_valid(double w) const noexcept
Check if a value is within the mesh range.
mesh_point_t operator[](long d) const noexcept
Subscript operator to access a mesh point by its data index.
std::vector< double > const & points() const noexcept
Get the vector of frequency point values.
mesh_point_t operator[](closest_mesh_point_t< double > const &cmp) const noexcept
Subscript operator to access the mesh point closest to a given value.
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
mesh_point_t operator()(index_t n) const noexcept
Function call operator to access a mesh point by its index.
refreq_log()=default
Default constructor creates an empty mesh.
uint64_t mesh_hash() const noexcept
Get the hash value of the mesh.
double w_max() const noexcept
Get the largest frequency .
index_t to_index(data_index_t d) const noexcept
Map a data index to the corresponding index .
refreq_log(double eps, double w_max, double ratio)
Construct a logarithmic real frequency mesh.
data_index_t to_data_index(closest_mesh_point_t< double > const &cmp) const noexcept
Map a value to the closest mesh point and return its data index.
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
auto cbegin() const
Get a const iterator to the beginning of the mesh.
data_index_t to_data_index(index_t n) const noexcept
Map an index to its corresponding data index .
friend void h5_read(h5::group g, std::string const &name, refreq_log &m)
Read a triqs::mesh::refreq_log mesh from HDF5.
auto cend() const
Get a const iterator to the end of the mesh.
bool is_index_valid(index_t n) const noexcept
Check if an index is valid.
double to_value(index_t n) const noexcept
Map an index to its corresponding value .
double eps() const noexcept
Get the smallest positive frequency .
long data_index_t
Data index type.
friend void h5_write(h5::group g, std::string const &name, refreq_log const &m)
Write a triqs::mesh::refreq_log mesh to HDF5.
double value_t
Value type.
auto end() const
Get an iterator to the end of the mesh.
double ratio() const noexcept
Get the common ratio of the geometric sequence.
auto begin() const
Get an iterator to the beginning of the mesh.
index_t to_index(closest_mesh_point_t< double > const &cmp) const noexcept
Map a value to the closest mesh point and return its index using binary search.
static std::string hdf5_format()
Get the HDF5 format tag.
long size() const noexcept
Get the size of the mesh, i.e. the number of mesh points.
bool operator==(refreq_log const &) const =default
Equal-to comparison operator compares , and the ratio.
uint64_t hash(Ts &&...ts)
Generic hash function for multiple arguments.
Common macros used in TRIQS.
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...
A generic random access iterator for 1D meshes.