32#include <itertools/itertools.hpp>
43namespace triqs::mesh::detail {
72 template <
typename M,
typename T>
73 requires std::totally_ordered<T>
111 mesh_point_t(
long n,
long d, uint64_t mhash,
double m) : index_(n), data_index_(d), mesh_hash_(mhash), value_(m) {}
114 [[nodiscard]]
long index()
const {
return index_; }
117 [[nodiscard]]
long data_index()
const {
return data_index_; }
123 [[nodiscard]] uint64_t
mesh_hash() const noexcept {
return mesh_hash_; }
130 template <typename U> friend auto operator OP(mesh_point_t const &mp, U &&y) { return mp.value() OP std::forward<U>(y); } \
132 template <typename U> \
133 requires(not std::is_same_v<std::decay_t<U>, mesh_point_t>) \
134 friend auto operator OP(U &&x, mesh_point_t const &mp) { \
135 return std::forward<U>(x) OP mp.value(); \
145 long data_index_ = 0;
146 uint64_t mesh_hash_ = 0;
167 delta_inv_ = std::numeric_limits<double>::infinity();
170 delta_ = (b_ - a_) / (N_ - 1);
171 delta_inv_ = 1 / delta_;
188 [[nodiscard]]
bool is_value_valid(
value_t m)
const noexcept {
return N_ > 0 and a_ <= m and m <= b_; }
228 EXPECTS(is_value_valid(cmp.value));
229 return (N_ == 1 ? 0 :
static_cast<index_t>((cmp.value - a_) * delta_inv_ + 0.5));
239 [[nodiscard]] mesh_point_t
operator[](
long d)
const noexcept {
return (*
this)(d); }
268 if (N_ == 1)
return a_;
269 auto const w =
static_cast<double>(n) / (N_ - 1);
270 return a_ * (1 - w) + b_ * w;
274 [[nodiscard]] C2PY_PROPERTY_GET(
mesh_hash) uint64_t
mesh_hash() const noexcept {
return mesh_hash_; }
277 [[nodiscard]]
long size() const noexcept {
return N_; }
301 [[nodiscard]]
auto cend()
const {
return end(); }
307 void serialize(
auto &ar)
const { ar & N_ & a_ & b_ & delta_ & delta_inv_ & mesh_hash_; }
313 void deserialize(
auto &ar) { ar & N_ & a_ & b_ & delta_ & delta_inv_ & mesh_hash_; }
323 void h5_write_impl(h5::group g, std::string
const &name,
const char *format)
const {
324 h5::group gr = g.create_group(name);
325 h5::write_hdf5_format_as_string(gr, format);
326 h5::write(gr,
"min", a_);
327 h5::write(gr,
"max", b_);
328 h5::write(gr,
"size", N_);
338 void h5_read_impl(h5::group g, std::string
const &name,
const char *exp_format) {
339 h5::group gr = g.open_group(name);
340 h5::assert_hdf5_format_as_string(gr, exp_format,
true);
341 auto a = h5::read<value_t>(gr,
"min");
342 auto b = h5::read<value_t>(gr,
"max");
343 auto N = h5::read<long>(gr,
"size");
365 EXPECTS(is_value_valid(x) and N_ > 1);
366 auto const n_dbl = (x - a_) * delta_inv_;
367 auto const n = std::clamp<index_t>(
static_cast<index_t>(n_dbl), 0, N_ - 2);
368 auto const w = std::min(n_dbl - n, 1.0);
369 return (1 - w) * f(n) + w * f(n + 1);
375 value_t delta_{0}, delta_inv_{0};
376 uint64_t mesh_hash_{
hash(N_, a_, b_)};
long data_index() const
Get the data index of the mesh point.
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
mesh_point_t(long n, long d, uint64_t mhash, double m)
Construct a mesh point with a given index , data index , hash value of the parent mesh and value .
M mesh_t
Parent mesh type.
long index() const
Get the index of the mesh point.
value_t value() const
Get the value of the mesh point.
uint64_t mesh_hash() const noexcept
Get the hash value of the parent mesh.
long last_index() const
Get the last index of the mesh, i.e. .
long data_index_t
Data index type.
mesh_point_t operator[](long d) const noexcept
Subscript operator to access a mesh point by its data index .
value_t delta_inv() const noexcept
Get the inverse of the step size of the mesh, i.e. .
value_t delta() const noexcept
data_index_t to_data_index(index_t n) const noexcept
Map an index to its corresponding data index .
linear(value_t a, value_t b, long N)
Construct a linear mesh on the interval of a given size .
mesh_point_t operator[](closest_mesh_point_t< value_t > const &cmp) const noexcept
Subscript operator to access a mesh point by a value contained in a triqs::mesh::closest_mesh_point_...
index_t to_index(data_index_t d) const noexcept
Map a data index to the corresponding index .
auto evaluate(auto const &f, double x) const
Linear interpolation of a function defined on a triqs::mesh::detail::linear mesh at a value .
long size() const noexcept
Get the size of the mesh, i.e. the number of mesh points.
bool operator==(linear const &rhs) const =default
Equal-to comparison operator compares and the interval .
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
auto cend() const
Get a const iterator to the end of the mesh.
void h5_write_impl(h5::group g, std::string const &name, const char *format) const
Write the mesh to HDF5.
long first_index() const
Get the first index of the mesh, i.e. .
uint64_t mesh_hash() const noexcept
mesh_point_t operator()(index_t n) const noexcept
Function call operator to access a mesh point by its index .
auto begin() const
Get an iterator to the beginning of the mesh.
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
auto end() const
Get an iterator to the end of the mesh.
void h5_read_impl(h5::group g, std::string const &name, const char *exp_format)
Read the mesh from HDF5.
linear()=default
Default construct an empty linear mesh of size .
index_t to_index(closest_mesh_point_t< value_t > const &cmp) const noexcept
Map a value to the closest mesh point and return its index .
bool is_index_valid(index_t n) const noexcept
Check if an index is valid.
value_t to_value(index_t n) const noexcept
Map an index to its corresponding value .
auto cbegin() const
Get a const iterator to the beginning of the mesh.
index_t to_data_index(closest_mesh_point_t< value_t > const &cmp) const noexcept
Map a value to the closest mesh point and return its data index .
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.