30#include <fmt/format.h>
36#include <initializer_list>
42namespace triqs::mesh {
104 mesh_point_t(
long n,
long d, uint64_t mhash,
double w) : index_(n), data_index_(d), mesh_hash_(mhash), value_(w) {}
107 [[nodiscard]]
long index() const noexcept {
return index_; }
110 [[nodiscard]]
long data_index() const noexcept {
return data_index_; }
113 [[nodiscard]]
double value() const noexcept {
return value_; }
116 [[nodiscard]] uint64_t
mesh_hash() const noexcept {
return mesh_hash_; }
119 operator double()
const {
return value_; }
123 template <typename U> friend auto operator OP(mesh_point_t const &mp, U &&y) { return mp.value() OP std::forward<U>(y); } \
124 template <typename U> \
125 requires(not std::is_same_v<std::decay_t<U>, mesh_point_t>) \
126 friend auto operator OP(U &&x, mesh_point_t const &mp) { \
127 return std::forward<U>(x) OP mp.value(); \
137 long data_index_ = 0;
138 uint64_t mesh_hash_ = 0;
151 : pts_(std::move(pts)),
152 mesh_hash_(
hash(std::accumulate(pts_.
begin(), pts_.
end(), 0.0), pts_.empty() ? 0.0 : pts_.front(), pts_.empty() ? 0.0 : pts_.back(),
153 static_cast<long>(pts_.
size()))) {
154 if (not std::ranges::is_sorted(pts_))
TRIQS_RUNTIME_ERROR <<
"refreq_pts mesh must be constructed with a sorted list of points";
181 [[nodiscard]]
bool is_value_valid(
double w)
const noexcept {
return !pts_.empty() and pts_.front() <= w and w <= pts_.back(); }
224 auto itr_r = std::ranges::lower_bound(pts_, cmp.value);
225 long i_r = itr_r - pts_.begin();
227 if (i_r == 0) {
return 0; }
228 if (i_r ==
size()) {
return size() - 1; }
231 if (std::abs(cmp.value - pts_[i_l]) < std::abs(cmp.value - pts_[i_r]))
261 return {n, n, mesh_hash_, pts_[n]};
276 [[nodiscard]] C2PY_PROPERTY_GET(
mesh_hash) uint64_t
mesh_hash() const noexcept {
return mesh_hash_; }
279 [[nodiscard]]
long size() const noexcept {
return static_cast<long>(pts_.size()); }
282 [[nodiscard]] C2PY_PROPERTY_GET(
points) std::vector<double>
const &
points() const noexcept {
return pts_; }
294 [[nodiscard]]
auto cend()
const {
return end(); }
304 return sout << fmt::format(
"Real frequency point mesh of size {}", m.
size());
311 void serialize(
auto &ar)
const { ar & pts_ & mesh_hash_; }
320 [[nodiscard]]
static std::string
hdf5_format() {
return "MeshReFreqPts"; }
330 h5::group gr = g.create_group(name);
331 h5::write_hdf5_format(gr, m);
332 h5::write(gr,
"points", m.pts_);
343 h5::group gr = g.open_group(name);
344 h5::assert_hdf5_format(gr, m,
true);
345 auto pts = h5::read<std::vector<double>>(gr,
"points");
350 std::vector<double> pts_;
351 uint64_t mesh_hash_ = 0;
370 inline auto evaluate(
refreq_pts const &m,
auto const &f,
double w) {
373 auto const &pts = m.
points();
374 auto itr_r = std::ranges::lower_bound(pts, w);
375 long i_r = itr_r - pts.begin();
377 if (i_r == 0) {
return f(0); }
378 if (i_r == m.
size()) {
return f(m.
size() - 1); }
382 double w_l = pts[i_l];
383 double w_r = pts[i_r];
384 double del = w_r - w_l;
386 double wt_r = (w - w_l) / del;
387 double wt_l = (w_r - w) / del;
389 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_pts mesh.
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.
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.
refreq_pts mesh_t
Parent mesh type.
long index() const noexcept
Get the index of the mesh point.
long data_index() const noexcept
Get the data index of the mesh point.
double value() const noexcept
Get the value of the mesh point.
Real frequency mesh type from arbitrary sorted frequency points.
friend void h5_read(h5::group g, std::string const &name, refreq_pts &m)
Read a triqs::mesh::refreq_pts mesh from HDF5.
auto end() const
Get an iterator to the end of the mesh.
long data_index_t
Data index type.
refreq_pts()=default
Default constructor creates an empty mesh.
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
refreq_pts(std::initializer_list< double > l)
Construct a real frequency mesh from an initializer list of frequency points.
bool is_value_valid(double w) const noexcept
Check if a value is within the mesh range.
friend void h5_write(h5::group g, std::string const &name, refreq_pts const &m)
Write a triqs::mesh::refreq_pts mesh to HDF5.
data_index_t to_data_index(index_t n) const noexcept
Map an index to its corresponding data index .
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.
auto cend() const
Get a const iterator to the end of the mesh.
auto cbegin() const
Get a const iterator to the beginning of the mesh.
long size() const noexcept
Get the size of the mesh, i.e. the number of mesh points.
static std::string hdf5_format()
Get the HDF5 format tag.
friend std::ostream & operator<<(std::ostream &sout, refreq_pts const &m)
Write a triqs::mesh::refreq_pts mesh to a std::ostream.
uint64_t mesh_hash() const noexcept
Get the hash value of the mesh.
auto begin() const
Get an iterator to the beginning of the mesh.
index_t to_index(data_index_t d) const noexcept
Map a data index to the corresponding index .
double to_value(index_t n) const noexcept
Map an index to its corresponding value .
std::vector< double > const & points() const noexcept
Get the vector of frequency point values.
mesh_point_t operator()(index_t n) const noexcept
Function call operator to access a mesh point by its index.
refreq_pts(std::vector< double > pts)
Construct a real frequency mesh from a sorted vector of frequency points.
bool is_index_valid(index_t n) const noexcept
Check if an index is valid.
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.
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.
mesh_point_t operator[](long d) const noexcept
Subscript operator to access a mesh point by its data index.
bool operator==(refreq_pts const &) const =default
Equal-to comparison operator.
double value_t
Value type.
TRIQS exception hierarchy and related macros.
uint64_t hash(Ts &&...ts)
Generic hash function for multiple arguments.
#define TRIQS_RUNTIME_ERROR
Throw a triqs::runtime_error with the current source location.
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.