TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
discrete.hpp
Go to the documentation of this file.
1// Copyright (c) 2022-2023 Simons Foundation
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You may obtain a copy of the License at
14// https://www.gnu.org/licenses/gpl-3.0.txt
15//
16// Authors: Olivier Parcollet, Nils Wentzell
17
22
23#pragma once
24
25#include "./mesh_iterator.hpp"
26#include "../utility/macros.hpp"
27
28#include <h5/h5.hpp>
29#include <itertools/itertools.hpp>
30#include <nda/nda.hpp>
31
32#include <cstdint>
33#include <iostream>
34#include <string>
35
36namespace triqs::mesh {
37
51 class C2PY_IGNORE discrete {
52 public:
54 using index_t = long;
55
57 using data_index_t = long;
58
64 public:
67
69 mesh_point_t() = default;
70
79 mesh_point_t(long n, long d, uint64_t mhash) : index_(n), data_index_(d), mesh_hash_(mhash) {}
80
82 [[nodiscard]] long index() const { return index_; }
83
85 [[nodiscard]] long data_index() const { return data_index_; }
86
88 [[nodiscard]] uint64_t mesh_hash() const noexcept { return mesh_hash_; }
89
90 private:
91 long index_ = 0;
92 long data_index_ = 0;
93 uint64_t mesh_hash_ = 0;
94 };
95
100 discrete(long N = 0) : N_(N), mesh_hash_(N) { EXPECTS(N >= 0); }
101
103 bool operator==(discrete const &) const = default;
104
111 [[nodiscard]] bool is_index_valid(index_t n) const noexcept { return 0 <= n and n < N_; }
112
119 [[nodiscard]] data_index_t to_data_index(index_t n) const noexcept {
120 EXPECTS(is_index_valid(n));
121 return n;
122 }
123
130 [[nodiscard]] index_t to_index(long d) const noexcept {
131 EXPECTS(is_index_valid(d));
132 return d;
133 }
134
141 [[nodiscard]] mesh_point_t operator[](long d) const { return {to_index(d), d, mesh_hash_}; }
142
149 [[nodiscard]] mesh_point_t operator()(index_t n) const { return {n, to_data_index(n), mesh_hash_}; }
150
152 [[nodiscard]] uint64_t mesh_hash() const { return mesh_hash_; }
153
155 [[nodiscard]] long size() const { return N_; }
156
158 [[nodiscard]] auto begin() const { return mesh_iterator<discrete>{.mesh_ptr = this, .data_index = 0}; }
159
161 [[nodiscard]] auto cbegin() const { return begin(); }
162
164 [[nodiscard]] auto end() const { return mesh_iterator<discrete>{.mesh_ptr = this, .data_index = size()}; }
165
167 [[nodiscard]] auto cend() const { return end(); }
168
176 friend std::ostream &operator<<(std::ostream &sout, discrete const &m) { return sout << "Discrete mesh of size " << m.size(); }
177
182 void serialize(auto &ar) const { ar & N_ & mesh_hash_; }
183
188 void deserialize(auto &ar) { ar & N_ & mesh_hash_; }
189
191 [[nodiscard]] static std::string hdf5_format() { return "MeshIndex"; }
192
200 friend void h5_write(h5::group g, std::string const &name, discrete const &m) {
201 h5::group gr = g.create_group(name);
202 h5::write_hdf5_format(gr, m); // NOLINT (downcasting to base class)
203 h5::write(gr, "size", m.size());
204 }
205
213 friend void h5_read(h5::group g, std::string const &name, discrete &m) {
214 h5::group gr = g.open_group(name);
215 h5::assert_hdf5_format(gr, m, true); // NOLINT (downcasting to base class)
216 long L = h5::read<long>(gr, "size");
217 m = discrete(L);
218 }
219
220 private:
221 long N_;
222 uint64_t mesh_hash_ = 0;
223 };
224
225} // 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 of a triqs::mesh::discrete mesh.
Definition discrete.hpp:63
mesh_point_t(long n, long d, uint64_t mhash)
Construct a mesh point with a given index , data index and hash value of the parent mesh.
Definition discrete.hpp:79
long data_index() const
Get the data index of the mesh point.
Definition discrete.hpp:85
long index() const
Get the index of the mesh point.
Definition discrete.hpp:82
discrete mesh_t
Parent mesh type.
Definition discrete.hpp:66
uint64_t mesh_hash() const noexcept
Get the hash value of the parent mesh.
Definition discrete.hpp:88
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
Definition discrete.hpp:188
bool operator==(discrete const &) const =default
Equal-to comparison operator compares the size of the meshes.
static std::string hdf5_format()
Get the HDF5 format tag.
Definition discrete.hpp:191
index_t to_index(long d) const noexcept
Map a data index to the corresponding index .
Definition discrete.hpp:130
bool is_index_valid(index_t n) const noexcept
Check if an index is valid.
Definition discrete.hpp:111
data_index_t to_data_index(index_t n) const noexcept
Map an index to its corresponding data index .
Definition discrete.hpp:119
mesh_point_t operator[](long d) const
Subscript operator to access a mesh point by its data index .
Definition discrete.hpp:141
friend void h5_write(h5::group g, std::string const &name, discrete const &m)
Write a triqs::mesh::discrete mesh to HDF5.
Definition discrete.hpp:200
long index_t
Index type.
Definition discrete.hpp:54
auto cend() const
Get a const iterator to the end of the mesh.
Definition discrete.hpp:167
friend void h5_read(h5::group g, std::string const &name, discrete &m)
Read a triqs::mesh::discrete mesh from HDF5.
Definition discrete.hpp:213
friend std::ostream & operator<<(std::ostream &sout, discrete const &m)
Write a triqs::mesh::discrete mesh to a std::ostream.
Definition discrete.hpp:176
long size() const
Get the size of the mesh, i.e. the number of mesh points.
Definition discrete.hpp:155
auto cbegin() const
Get a const iterator to the beginning of the mesh.
Definition discrete.hpp:161
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
Definition discrete.hpp:182
mesh_point_t operator()(index_t n) const
Function call operator to access a mesh point by its index .
Definition discrete.hpp:149
uint64_t mesh_hash() const
Get the hash value of the mesh.
Definition discrete.hpp:152
long data_index_t
Data index type.
Definition discrete.hpp:57
auto begin() const
Get an iterator to the beginning of the mesh.
Definition discrete.hpp:158
discrete(long N=0)
Construct a discrete mesh of a given size .
Definition discrete.hpp:100
auto end() const
Get an iterator to the end of the mesh.
Definition discrete.hpp:164
Common macros used in TRIQS.
Provides a generic random access iterator for 1D meshes.
A generic random access iterator for 1D meshes.