TRIQS/triqs_modest 3.3.0
Brillouin zone summation
Loading...
Searching...
No Matches
density.cpp
Go to the documentation of this file.
1// Copyright (c) 2025--present, The Simons Foundation
2// This file is part of TRIQS/modest and is licensed under the terms of GPLv3 or later.
3// SPDX-License-Identifier: GPL-3.0-or-later
4// See LICENSE in the root of this distribution for details.
5
6#include "./density.hpp"
7
8namespace triqs::modest {
9
10 double density_nk_matrix_valued_impl(one_body_elements_on_grid const &obe, double mu, double beta) {
11
12 auto Fermi = [](double x) { return (x > 0 ? exp(-x) / (1 + exp(-x)) : 1 / (1 + exp(x))); };
13 double KS_term = 0;
14
15 // MPI parallel only on this loop; eigenvalues inside the loop may thread via blas
16 mpi::communicator comm = {}; // for now using default comm in MPI
17 for (auto k_idx : mpi::chunk(nda::range(obe.H.n_k()), comm)) {
18 for (auto sigma : nda::range(obe.C_space.n_sigma())) {
19 double KS_term_acc = 0;
20 auto eps = nda::linalg::eigenvalues(nda::matrix<dcomplex>{obe.H.H(sigma, k_idx)});
21 for (auto nu : range(obe.H.N_nu(sigma, k_idx))) { KS_term_acc += Fermi(beta * (eps(nu) - mu)); }
22 KS_term += obe.H.k_weights(k_idx) * KS_term_acc;
23 }
24 }
25 KS_term = mpi::all_reduce(KS_term);
26 return KS_term;
27 }
28
29 double density_nk(one_body_elements_on_grid const &obe, double mu, double beta) {
30 // if band dispersion is matrix valued need to diagonalize H(k) at every k.
31 if (obe.H.matrix_valued) return density_nk_matrix_valued_impl(obe, mu, beta);
32
33 auto Fermi = [](double x) { return (x > 0 ? exp(-x) / (1 + exp(-x)) : 1 / (1 + exp(x))); };
34 double KS_term = 0;
35
36 mpi::communicator comm = {}; // for now using default comm in MPI
37#pragma omp parallel for collapse(2) reduction(+ : KS_term) default(none) shared(obe, beta, Fermi, comm, mu)
38 for (auto k_idx : mpi::chunk(nda::range(obe.H.n_k()), comm)) {
39 for (auto sigma : nda::range(obe.C_space.n_sigma())) {
40 double KS_term_acc = 0;
41 auto eps = obe.H.H(sigma, k_idx);
42 for (auto nu : range(obe.H.N_nu(sigma, k_idx))) { KS_term_acc += Fermi(beta * real(eps(nu, nu) - mu)); }
43 KS_term += obe.H.k_weights(k_idx) * KS_term_acc;
44 }
45 }
46 KS_term = mpi::all_reduce(KS_term);
47 return KS_term;
48 }
49} // namespace triqs::modest
long n_sigma() const
Dimension of the σ index.
double density_nk(one_body_elements_on_grid const &obe, double mu, double beta)
Compute number of particles n = ∑f(β(e(k)-μ))
Definition density.cpp:29
double density_nk_matrix_valued_impl(one_body_elements_on_grid const &obe, double mu, double beta)
Definition density.cpp:10
bool matrix_valued
Is the dispersion matrix-valued?
nda::array< double, 1 > k_weights
k_weights[k_idx]
long n_k() const
Number of k points in the grid.
long N_nu(long sigma, long k_idx) const
Number of bands #ν
nda::matrix_const_view< dcomplex > H(long sigma, long k_idx) const
H^σ(k)_ν, returned as a MATRIX in (ν, ν)
A one-body elements struct where all of the underlying data exists on a fixed momentum grid.