TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
cyclat.hpp
Go to the documentation of this file.
1// Copyright (c) 2016-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2016-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2023 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Thomas Ayral, Philipp Dumitrescu, Michel Ferrero, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./mesh_iterator.hpp"
28#include "./utils.hpp"
30#include "../utility/macros.hpp"
31
32#include <fmt/ranges.h>
33#include <h5/h5.hpp>
34#include <nda/nda.hpp>
35
36#include <array>
37#include <cstdint>
38#include <iostream>
39#include <string>
40
41namespace triqs::mesh {
42
47 using lattice::bravais_lattice;
48
53
88 class C2PY_RENAME(MeshCycLat) cyclat {
89 public:
92
94 using index_t = std::array<long, 3>;
95
97 using data_index_t = long;
98
106 class C2PY_IGNORE mesh_point_t : public value_t {
107 public:
109 using mesh_t = cyclat;
110
112 mesh_point_t() = default;
113
123 mesh_point_t(std::array<long, 3> const &n, long d, uint64_t mhash, bravais_lattice const *bl_ptr)
124 : value_t(n, bl_ptr), data_index_(d), mesh_hash_(mhash) {}
125
127 [[nodiscard]] long data_index() const { return data_index_; }
128
130 [[nodiscard]] value_t const &value() const { return *this; }
131
133 [[nodiscard]] uint64_t mesh_hash() const noexcept { return mesh_hash_; }
134
142 friend std::ostream &operator<<(std::ostream &sout, mesh_point_t const &mp) { return sout << mp.value(); }
143
144 private:
145 long data_index_ = 0;
146 uint64_t mesh_hash_ = 0;
147 };
148
149 public:
157 C2PY_DEPRECATED_PARAMETER_NAME(lattice : bl)
158 cyclat(bravais_lattice const &bl, std::array<long, 3> const &dims)
159 : bl_(bl),
160 dims_(dims),
161 size_(nda::stdutil::product(dims)),
162 s2_(dims_[2]),
163 s1_(dims_[1] * dims_[2]),
164 units_(bl.units()),
165 units_inv_(nda::linalg::inv(units_)),
166 mesh_hash_(hash(nda::sum(bl.units()), dims[0], dims[1], dims[2])) {
167 EXPECTS(dims_[0] > 0 and dims_[1] > 0 and dims_[2] > 0);
168 }
169
179 C2PY_IGNORE cyclat(bravais_lattice const &bl, nda::matrix<long> const &M) : cyclat(bl, std::array{M(0, 0), M(1, 1), M(2, 2)}) {
180 EXPECTS((M.shape() == std::array{3l, 3l}));
181 EXPECTS(nda::is_matrix_diagonal(M));
182 }
183
190 cyclat(bravais_lattice const &bl, long L) : cyclat{bl, std::array{L, (bl.ndim() >= 2 ? L : 1l), (bl.ndim() >= 3 ? L : 1)}} {}
191
200 cyclat(long L1 = 1, long L2 = 1, long L3 = 1) : cyclat{bravais_lattice{nda::eye<double>(3)}, std::array{L1, L2, L3}} {}
201
209 [[nodiscard]] bool is_index_valid(index_t const &n) const noexcept {
210 for (auto i : nda::range(3))
211 if (n[i] < 0 or n[i] >= dims_[i]) return false;
212 return true;
213 }
214
221 [[nodiscard]] data_index_t to_data_index(index_t const &n) const {
222 EXPECTS(is_index_valid(n));
223 return n[0] * s1_ + n[1] * s2_ + n[2];
224 }
225
232 [[nodiscard]] C2PY_IGNORE data_index_t to_data_index(closest_mesh_point_t<value_t> const &cmp) const { return to_data_index(to_index(cmp)); }
233
242 [[nodiscard]] index_t to_index(data_index_t d) const {
243 EXPECTS(0 <= d and d < size());
244 long const r0 = d % s1_;
245 return {d / s1_, r0 / s2_, r0 % s2_};
246 }
247
254 [[nodiscard]] C2PY_IGNORE index_t to_index(closest_mesh_point_t<value_t> const &cmp) const { return cmp.value.index(); }
255
264 [[nodiscard]] mesh_point_t operator[](long d) const { return {to_index(d), d, mesh_hash_, &bl_}; }
265
274 [[nodiscard]] C2PY_IGNORE mesh_point_t operator[](closest_mesh_point_t<value_t> const &cmp) const { return (*this)[this->to_data_index(cmp)]; }
275
283 [[nodiscard]] mesh_point_t operator()(index_t const &n) const { return {n, to_data_index(n), mesh_hash_, &bl_}; }
284
291 [[nodiscard]] value_t to_value(index_t const &n) const {
292 EXPECTS(is_index_valid(n));
293 return {n, &bl_};
294 }
295
297 [[nodiscard]] C2PY_PROPERTY_GET(dims) auto const &dims() const { return dims_; }
298
302 [[nodiscard]] C2PY_PROPERTY_GET(units) auto units() const { return nda::matrix_const_view<double>{units_}; }
303
305 [[nodiscard]] C2PY_PROPERTY_GET(lattice) auto const &lattice() const noexcept { return bl_; }
306
308 [[nodiscard]] C2PY_PROPERTY_GET(mesh_hash) uint64_t mesh_hash() const { return mesh_hash_; }
309
311 [[nodiscard]] long size() const { return size_; }
312
320 [[nodiscard]] index_t index_modulo(index_t const &n_tilde) const {
321 return {positive_modulo(n_tilde[0], dims_[0]), positive_modulo(n_tilde[1], dims_[1]), positive_modulo(n_tilde[2], dims_[2])};
322 }
323
325 [[nodiscard]] auto begin() const { return mesh_iterator<cyclat>{.mesh_ptr = this, .data_index = 0}; }
326
328 [[nodiscard]] auto cbegin() const { return begin(); }
329
331 [[nodiscard]] auto end() const { return mesh_iterator<cyclat>{.mesh_ptr = this, .data_index = size()}; }
332
334 [[nodiscard]] auto cend() const { return end(); }
335
340 bool operator==(cyclat const &m) const { return bl_ == m.lattice() && dims_ == m.dims(); }
341
349 friend std::ostream &operator<<(std::ostream &sout, cyclat const &m) {
350 return sout << "Cyclic lattice mesh with linear dimensions " << m.dims() << " and an underlying " << m.lattice();
351 }
352
357 void serialize(auto &ar) const { ar & bl_ & dims_ & size_ & s2_ & s1_ & units_ & units_inv_ & mesh_hash_; }
358
363 void deserialize(auto &ar) { ar & bl_ & dims_ & size_ & s2_ & s1_ & units_ & units_inv_ & mesh_hash_; }
364
366 [[nodiscard]] static std::string hdf5_format() { return "MeshCyclicLattice"; }
367
375 friend void h5_write(h5::group g, std::string const &name, cyclat const &m) {
376 h5::group gr = g.create_group(name);
377 h5::write_hdf5_format(gr, m); // NOLINT (downcasting to base class)
378 h5::write(gr, "dims", m.dims_);
379 h5::write(gr, "bravais_lattice", m.bl_);
380 }
381
389 friend void h5_read(h5::group g, std::string const &name, cyclat &m) {
390 h5::group gr = g.open_group(name);
391 h5::assert_hdf5_format(gr, m, true); // NOLINT (downcasting to base class)
392
393 std::array<long, 3> dims{};
394 if (gr.has_key("dims")) {
395 h5::read(gr, "dims", dims);
396 } else {
397 // for backward compatibility
398 auto M = h5::read<nda::matrix<long>>(gr, "periodization_matrix");
399 dims = {M(0, 0), M(1, 1), M(2, 2)};
400 }
401
402 bravais_lattice bl{};
403 if (gr.has_key("bravais_lattice")) {
404 h5::read(gr, "bravais_lattice", bl);
405 } else {
406 // for backward compatibility
407 h5::read(gr, "bl", bl);
408 }
409
410 m = cyclat(bl, dims);
411 }
412
413 private:
414 bravais_lattice bl_ = {};
415 std::array<long, 3> dims_ = {0, 0, 0};
416 long size_ = 0;
417 long s2_ = 1;
418 long s1_ = 1;
419 nda::matrix<double> units_ = nda::eye<double>(3);
420 nda::matrix<double> units_inv_ = nda::eye<double>(3);
421 uint64_t mesh_hash_ = 0;
422 };
423
437 auto evaluate(cyclat const &m, auto const &f, cyclat::index_t const &n_tilde) { return f(m.index_modulo(n_tilde)); }
438
452 auto evaluate(cyclat const &m, auto const &f, cyclat::value_t const &r_n_tilde) { return evaluate(m, f, r_n_tilde.index()); }
453
455
456} // 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.
Provides a Bravais lattice class.
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
bravais_lattice::point_t value_t
Value type.
Definition cyclat.hpp:91
cyclat(bravais_lattice const &bl, std::array< long, 3 > const &dims)
Construct a cyclic lattice mesh on a Bravais lattice with the given supercell dimensions.
Definition cyclat.hpp:158
auto index() const
Get the index vector of the lattice point.
Lattice point of a Bravais lattice.
Mesh point of a triqs::mesh::cyclat mesh.
Definition cyclat.hpp:106
uint64_t mesh_hash() const noexcept
Get the hash value of the parent mesh.
Definition cyclat.hpp:133
value_t const & value() const
Get the lattice point of the mesh point.
Definition cyclat.hpp:130
mesh_point_t()=default
Default constructor leaves the mesh point uninitialized.
friend std::ostream & operator<<(std::ostream &sout, mesh_point_t const &mp)
Write triqs::mesh::cyclat::mesh_point_t to a std::ostream.
Definition cyclat.hpp:142
mesh_point_t(std::array< long, 3 > const &n, long d, uint64_t mhash, bravais_lattice const *bl_ptr)
Construct a mesh point with a given index , data index , hash value of the parent mesh and Bravais la...
Definition cyclat.hpp:123
long data_index() const
Get the data index of the mesh point.
Definition cyclat.hpp:127
cyclat mesh_t
Parent mesh type.
Definition cyclat.hpp:109
Cyclic lattice mesh type for Bravais lattices with Born-von Karman periodic boundary conditions.
Definition cyclat.hpp:88
index_t to_index(closest_mesh_point_t< value_t > const &cmp) const
Map a lattice point to its index .
Definition cyclat.hpp:254
friend void h5_write(h5::group g, std::string const &name, cyclat const &m)
Write a triqs::mesh::cyclat mesh to HDF5.
Definition cyclat.hpp:375
long data_index_t
Data index type.
Definition cyclat.hpp:97
uint64_t mesh_hash() const
Get the hash value of the mesh.
Definition cyclat.hpp:308
value_t to_value(index_t const &n) const
Map an index to its corresponding lattice point .
Definition cyclat.hpp:291
mesh_point_t operator()(index_t const &n) const
Function call operator to access a mesh point by its index .
Definition cyclat.hpp:283
void serialize(auto &ar) const
Serialize the mesh to a generic archive.
Definition cyclat.hpp:357
void deserialize(auto &ar)
Deserialize the mesh from a generic archive.
Definition cyclat.hpp:363
cyclat(long L1=1, long L2=1, long L3=1)
Construct a cyclic lattice mesh on a cubic Bravais lattice with and the given supercell dimensions.
Definition cyclat.hpp:200
friend std::ostream & operator<<(std::ostream &sout, cyclat const &m)
Write a triqs::mesh::cyclat mesh to a std::ostream.
Definition cyclat.hpp:349
bool operator==(cyclat const &m) const
Equal-to comparison operator compares the underlying Bravais lattice and the number of unit cells in ...
Definition cyclat.hpp:340
index_t to_index(data_index_t d) const
Map a data index to the corresponding index .
Definition cyclat.hpp:242
auto end() const
Get an iterator to the end of the mesh.
Definition cyclat.hpp:331
auto const & lattice() const noexcept
Get the underlying Bravais lattice.
Definition cyclat.hpp:305
friend void h5_read(h5::group g, std::string const &name, cyclat &m)
Read a triqs::mesh::cyclat mesh from HDF5.
Definition cyclat.hpp:389
bool is_index_valid(index_t const &n) const noexcept
Check if an index is valid, i.e. corresponds to a unit cell/lattice point in the supercell.
Definition cyclat.hpp:209
data_index_t to_data_index(index_t const &n) const
Map an index to its corresponding data index .
Definition cyclat.hpp:221
index_t index_modulo(index_t const &n_tilde) const
Map an arbitrary index to the unique index in the supercell.
Definition cyclat.hpp:320
mesh_point_t operator[](closest_mesh_point_t< value_t > const &cmp) const
Subscript operator to access a mesh point by a lattice point contained in a triqs::mesh::closest_mes...
Definition cyclat.hpp:274
cyclat(bravais_lattice const &bl, long L)
Construct a cyclic lattice mesh on a Bravais lattice with a cubic supercell.
Definition cyclat.hpp:190
cyclat(bravais_lattice const &bl, nda::matrix< long > const &M)
Construct a cyclic lattice mesh on a Bravais lattice with the given periodization matrix.
Definition cyclat.hpp:179
bravais_lattice::point_t value_t
Value type.
Definition cyclat.hpp:91
long size() const
Get the size of the mesh, i.e. the number of unit cells in the supercell.
Definition cyclat.hpp:311
auto cend() const
Get a const iterator to the end of the mesh.
Definition cyclat.hpp:334
std::array< long, 3 > index_t
Index type.
Definition cyclat.hpp:94
cyclat(bravais_lattice const &bl, std::array< long, 3 > const &dims)
Construct a cyclic lattice mesh on a Bravais lattice with the given supercell dimensions.
Definition cyclat.hpp:158
auto cbegin() const
Get a const iterator to the beginning of the mesh.
Definition cyclat.hpp:328
mesh_point_t operator[](long d) const
Subscript operator to access a mesh point by its data index .
Definition cyclat.hpp:264
auto units() const
Get the matrix containing the basis vectors of the Bravais lattice in its rows.
Definition cyclat.hpp:302
auto begin() const
Get an iterator to the beginning of the mesh.
Definition cyclat.hpp:325
static std::string hdf5_format()
Get the HDF5 format tag.
Definition cyclat.hpp:366
data_index_t to_data_index(closest_mesh_point_t< value_t > const &cmp) const
Map a lattice point to its data index .
Definition cyclat.hpp:232
auto const & dims() const
Get the number of unit cells in each of the three dimensions.
Definition cyclat.hpp:297
uint64_t hash(Ts &&...ts)
Generic hash function for multiple arguments.
Definition utils.hpp:70
long positive_modulo(long x, long y)
Calculate the positive modulo of two integer numbers.
Definition utils.hpp:111
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...
Definition utils.hpp:186
T value
Use the mesh point closest to this value.
Definition utils.hpp:188
A generic random access iterator for 1D meshes.