TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
bravais_lattice.hpp
Go to the documentation of this file.
1// Copyright (c) 2014-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2014-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: Michel Ferrero, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "../arrays.hpp"
28#include "../utility/macros.hpp"
29
30#include <fmt/ranges.h>
31#include <h5/h5.hpp>
32#include <nda/nda.hpp>
33
34#include <array>
35#include <iostream>
36#include <optional>
37#include <ranges>
38#include <string>
39#include <vector>
40
41namespace triqs::lattice {
42
47
49 using r_t = nda::vector<double>;
50
52 using matrix_t = nda::matrix<double>;
53
76 public:
78 using index_t = std::array<long, 3>;
79
97 class C2PY_IGNORE point_t {
98 public:
100 point_t() = default;
101
108 point_t(index_t const &n, bravais_lattice const *bl_ptr) : index_(n), bl_ptr_(bl_ptr) {}
109
111 [[nodiscard]] auto index() const { return index_; }
112
114 [[nodiscard]] auto const &lattice() const { return *bl_ptr_; }
115
120 explicit operator r_t() const {
121 if (rval_)
122 return *rval_;
123 else
124 return *(rval_ = bl_ptr_->lattice_to_real_coordinates(index_));
125 }
126
133 [[deprecated("() is deprecated for a cyclat::mesh_point_t. Use [] instead")]] double operator()(int d) const { return r_t(*this)[d]; }
134
142 double operator[](int i) const { return r_t(*this)[i]; }
143
151 [[nodiscard]] point_t operator+(point_t const &pt) const {
152 EXPECTS(*bl_ptr_ == *(pt.bl_ptr_));
153 return {index_ + pt.index_, bl_ptr_};
154 }
155
163 [[nodiscard]] point_t operator-(point_t const &pt) const {
164 EXPECTS(*bl_ptr_ == *(pt.bl_ptr_));
165 return {index_ - pt.index_, bl_ptr_};
166 }
167
172 [[nodiscard]] point_t operator-() const { return {-index_, bl_ptr_}; }
173
181 friend std::ostream &operator<<(std::ostream &sout, point_t const &pt) { return sout << static_cast<r_t>(pt); }
182
183 private:
184 index_t index_ = {0, 0, 0};
185 bravais_lattice const *bl_ptr_ = nullptr;
186 mutable std::optional<r_t> rval_ = {};
187 };
188
193 bravais_lattice() : bravais_lattice(nda::eye<double>(3)) {}
194
205 bravais_lattice(matrix_t const &units, std::vector<r_t> orbital_positions = std::vector<r_t>{{0, 0, 0}},
206 std::vector<std::string> atom_orb_name = {});
207
209 [[nodiscard]] bool contains(r_t const &) const { return true; }
210
212 [[nodiscard]] int ndim() const { return ndim_; }
213
215 [[nodiscard]] matrix_t const &units() const { return units_; }
216
218 [[nodiscard]] long n_orbitals() const { return static_cast<long>(atom_orb_pos_.size()); }
219
221 [[nodiscard]] auto const &orbital_positions() const { return atom_orb_pos_; }
222
224 [[nodiscard]] auto const &orbital_names() const { return atom_orb_name_; }
225
239 template <typename R> [[nodiscard]] r_t lattice_to_real_coordinates(R const &v) const {
240 return nda::transpose(units_)(nda::range::all, nda::range(ndim())) * nda::basic_array_view{v}(nda::range(ndim()));
241 }
242
253 template <typename R> [[nodiscard]] r_t real_to_lattice_coordinates(R const &v_tilde) const {
254 return nda::transpose(units_inv_)(nda::range::all, nda::range(ndim())) * nda::basic_array_view{v_tilde}(nda::range(ndim()));
255 }
256
263 [[nodiscard]] C2PY_IGNORE point_t to_point(index_t const &n) const { return {n, this}; }
264
270 bool operator==(bravais_lattice const &bl) const {
271 return units_ == bl.units() && ndim_ == bl.ndim() && atom_orb_pos_ == bl.orbital_positions() && atom_orb_name_ == bl.orbital_names();
272 }
273
281 friend std::ostream &operator<<(std::ostream &sout, bravais_lattice const &bl) {
282 auto str = fmt::format("Bravais Lattice in {} dimensions with {} orbital(s):\n", bl.ndim(), bl.n_orbitals());
283 str += fmt::format(" Basis vectors: {}\n",
284 std::views::transform(nda::range(bl.ndim()), [&bl](int i) { return bl.units()(i, nda::range(bl.ndim())); }));
285 str += fmt::format(" Orbital positions: {}\n", bl.orbital_positions());
286 str += fmt::format(" Orbital names: {}\n", bl.orbital_names());
287 return sout << str;
288 }
289
294 void serialize(auto &ar) const { ar & ndim_ & units_ & atom_orb_pos_ & atom_orb_name_; }
295
300 void deserialize(auto &ar) { ar & ndim_ & units_ & atom_orb_pos_ & atom_orb_name_; }
301
303 [[nodiscard]] static std::string hdf5_format() { return "bravais_lattice"; }
304
312 friend void h5_write(h5::group g, std::string const &name, bravais_lattice const &bl);
313
321 friend void h5_read(h5::group g, std::string const &name, bravais_lattice &bl);
322
323 private:
324 matrix_t units_ = matrix_t::zeros(3, 3);
325 matrix_t units_inv_ = matrix_t::zeros(3, 3);
326 std::vector<r_t> atom_orb_pos_;
327 std::vector<std::string> atom_orb_name_;
328 int ndim_;
329 };
330
332
333} // namespace triqs::lattice
Backward-compatibility umbrella header pulling in the nda array library.
Lattice point of a Bravais lattice.
point_t operator-(point_t const &pt) const
Subtract two lattice points and .
point_t operator+(point_t const &pt) const
Add two lattice points and .
double operator()(int d) const
Get the coordinate of the corresponding lattice vector .
auto index() const
Get the index vector of the lattice point.
point_t()=default
Default constructor leaves the lattice point uninitialized, i.e. belonging to no Bravais lattice.
point_t operator-() const
Invert the lattice point .
double operator[](int i) const
Get the coordinate of the corresponding lattice vector .
auto const & lattice() const
Get the underlying Bravais lattice.
point_t(index_t const &n, bravais_lattice const *bl_ptr)
Construct a lattice point with a given index vector on the given Bravais lattice.
friend std::ostream & operator<<(std::ostream &sout, point_t const &pt)
Write a lattice point to a std::ostream.
long n_orbitals() const
Get the number of atomic orbitals in the unit cell.
friend std::ostream & operator<<(std::ostream &sout, bravais_lattice const &bl)
Write a triqs::lattice::bravais_lattice to a std::ostream.
point_t to_point(index_t const &n) const
Convert an index vector to a lattice point.
r_t real_to_lattice_coordinates(R const &v_tilde) const
Transform a vector from the standard basis to the lattice basis .
bool operator==(bravais_lattice const &bl) const
Equal-to comparison operator.
std::array< long, 3 > index_t
Index type for lattice points on the Bravais lattice.
auto const & orbital_names() const
Get the list of orbital names.
r_t lattice_to_real_coordinates(R const &v) const
Transform a vector from the lattice basis to the standard basis .
int ndim() const
Get the number of dimensions of the Bravais lattice.
auto const & orbital_positions() const
Get the list of atomic orbital positions .
bool contains(r_t const &) const
Check if a given vector is part of the domain.
void deserialize(auto &ar)
Deserialize the Bravais lattice from a generic archive.
bravais_lattice()
Construct a simple cubic lattice with lattice constant .
friend void h5_write(h5::group g, std::string const &name, bravais_lattice const &bl)
Write a triqs::lattice::bravais_lattice to HDF5.
friend void h5_read(h5::group g, std::string const &name, bravais_lattice &bl)
Read a triqs::lattice::bravais_lattice from HDF5.
static std::string hdf5_format()
Get the HDF5 format tag.
void serialize(auto &ar) const
Serialize the Bravais lattice to a generic archive.
matrix_t const & units() const
Get the matrix containing basis vectors as its rows.
nda::matrix< double > matrix_t
Matrix type for transformations involving real and reciprocal space vectors.
nda::vector< double > r_t
Real space vector type.
Common macros used in TRIQS.