TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
bravais_lattice.cpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-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, Michel Ferrero, Olivier Parcollet, Nils Wentzell
19
24
25#include "./bravais_lattice.hpp"
27
28#include <h5/h5.hpp>
29#include <nda/h5.hpp>
30#include <nda/nda.hpp>
31
32#include <cmath>
33#include <string>
34#include <vector>
35
36namespace triqs::lattice {
37
38 bravais_lattice::bravais_lattice(nda::matrix<double> const &units, std::vector<r_t> orbital_positions, std::vector<std::string> atom_orb_name)
39 : atom_orb_pos_(std::move(orbital_positions)),
40 atom_orb_name_(atom_orb_name.empty() ? std::vector<std::string>(atom_orb_pos_.size()) : std::move(atom_orb_name)),
41 ndim_(static_cast<int>(nda::first_dim(units))) {
42 // consistency checks
43 EXPECTS(atom_orb_pos_.size() == atom_orb_name_.size());
44 if (ndim_ < 1 || ndim_ > 3) TRIQS_RUNTIME_ERROR << "Error in triqs::lattice::bravais_lattice: Basis vector matrix has wrong size: " << units;
45
46 // initialize basis vectors
47 auto rg = nda::range(ndim_);
48 units_(rg, rg) = units(rg, rg);
49
50 // complete the basis for 1D and 2D
51 if (ndim_ < 3) {
52 if (ndim_ == 1) units_(1, 1) = 1;
53 units_(2, nda::range::all) = nda::linalg::cross_product(units_(0, nda::range::all), units_(1, nda::range::all));
54 units_(2, nda::range::all) /= nda::linalg::norm(units_(2, nda::range::all));
55 }
56
57 // linear independence check
58 if (std::abs(nda::linalg::det(units_)) < 1e-10)
59 TRIQS_RUNTIME_ERROR << "Error in triqs::lattice::bravais_lattice: Basis vectors are not linearly independent" << units_;
60
61 // compute inverse (used for basis transformations)
62 units_inv_ = nda::linalg::inv(units_);
63 }
64
65 void h5_write(h5::group g, std::string const &name, bravais_lattice const &bl) {
66 auto gr = g.create_group(name);
67 h5::write_hdf5_format(gr, bl); // NOLINT (downcasting to base class)
68 auto rg = nda::range(bl.ndim());
69 h5::write(gr, "units", bl.units_(rg, rg));
70 h5::write(gr, "atom_orb_pos", bl.atom_orb_pos_);
71 h5::write(gr, "atom_orb_name", bl.atom_orb_name_);
72 }
73
74 void h5_read(h5::group g, std::string const &name, bravais_lattice &bl) {
75 h5::group gr = g.open_group(name);
76 nda::matrix<double> A_T;
77 h5::read(gr, "units", A_T);
78 auto orb_pos = std::vector<r_t>{{0, 0, 0}};
79 h5::try_read(gr, "atom_orb_pos", orb_pos);
80 auto orb_name = std::vector<std::string>(orb_pos.size(), "");
81 h5::try_read(gr, "atom_orb_name", orb_name);
82 bl = bravais_lattice{A_T, orb_pos, orb_name};
83 }
84
85} // namespace triqs::lattice
int size() const
Get the total number of blocks.
Provides a Bravais lattice class.
int ndim() const
Get the number of dimensions of the Bravais lattice.
auto const & orbital_positions() const
Get the list of atomic orbital positions .
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.
matrix_t const & units() const
Get the matrix containing basis vectors as its rows.
TRIQS exception hierarchy and related macros.
#define TRIQS_RUNTIME_ERROR
Throw a triqs::runtime_error with the current source location.