ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
lr_util.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_H
3
4#include <cstddef>
5#include <complex>
6#include <vector>
7#include <utility>
11#include "source_psi/psi.h"
12#include <ATen/core/tensor.h>
13#include <ATen/ops/linalg_op.h>
14#include <set>
15
16class UnitCell;
17
20
21#ifndef TO_COMPLEX_H
22#define TO_COMPLEX_H
23template <typename T> struct ToComplex;
24template <> struct ToComplex<double> { using type = std::complex<double>; };
25template <> struct ToComplex<std::complex<double>> { using type = std::complex<double>; };
26template <> struct ToComplex<float> { using type = std::complex<float>; };
27template <> struct ToComplex<std::complex<float>> { using type = std::complex<float>; };
28#endif
29
30namespace LR_Util
31{
33 double hse_sigma_floor();
34
37
39 void check_force_pp(const UnitCell& cell, bool cal_force, const std::string& calculation);
40
42 const std::vector<double>& prepare_xc_sigma(const std::vector<double>& sigma,
43 bool is_hse06,
44 std::vector<double>& hse_buffer);
45
56 const std::vector<double> &prepare_xc_sigma(
57 const std::vector<double> &sigma,
58 bool is_hse06,
59 std::vector<double> &hse_buffer,
60 std::vector<double> &jacobian);
61
72 inline const std::set<std::string>& hybrid_xc_list()
73 {
74 static const std::set<std::string> l = { "hf", "hse", "pbe0", "b3lyp",
75 "cam_pbeh", "lc_pbe", "lc_wpbe", "lrc_wpbe", "lrc_wpbeh" };
76 return l;
77 }
78
80 inline bool has_local_xc(const std::string& name)
81 {
82 if (std::set<std::string>({"lda", "pwlda", "pbe"}).count(name))
83 {
84 return true;
85 }
86 // Every hybrid but pure HF keeps a semilocal remainder: the KS exchange left over after
87 // the EXX part is taken out, $(1-\alpha)E_x^\text{KS-LR}+[1-(\alpha+\beta)]E_x^
88 // \text{KS-SR}$. libxc returns exactly that once `f_xc_libxc` hands the functional its
89 // external parameters, so no per-functional code is needed here -- only the name.
90 return name != "hf" && hybrid_xc_list().count(name);
91 }
92
96 template <typename TCell>
97 int cal_nelec(const TCell& ucell);
98
101 int cal_nocc(int nelec);
102
104 int cal_nocc(double nelec, int nspin, int nupdown);
105
107 int cal_nocc_window(int requested_nocc, int nocc_max);
108
110 std::string force_window_warning(int basis_size, int gs_bands, int skipped_occupied,
111 const std::vector<int>& x_virtual, const std::vector<int>& z_virtual);
112
120 std::pair<ModuleBase::matrix, std::vector<std::pair<int, int>>>
121 set_ix_map_diagonal(bool mode, int nc, int nv);
122
123 // Operators to calculate xc kernel have been moved into lr_util_xc.hpp.
125
126 inline double inner_product(const double* vec1, const double* vec2, const int& size)
127 {
128 double result = 0.0;
129 for (int i = 0; i < size; ++i)
130 {
131 result += vec1[i] * vec2[i];
132 }
133 return result;
134 }
135
136 inline std::complex<double> inner_product(const std::complex<double>* vec1,
137 const std::complex<double>* vec2,
138 const int& size)
139 {
140 std::complex<double> result = 0.0;
141 for (int i = 0; i < size; ++i)
142 {
143 result += std::conj(vec1[i]) * vec2[i];
144 }
145 return result;
146 }
147
148 //====== newers and deleters========
150 template <typename T>
151 void _deallocate_2order_nested_ptr(T** p2, size_t size);
153 template <typename T>
154 void _allocate_2order_nested_ptr(T**& p2, size_t size1, size_t size2);
155
160
163 template<typename T>
164 void matsym(const T* in, const int n, T* out);
166 template<typename T>
167 void matsym(T* inout, const int n);
168#ifdef __MPI
169 template<typename T>
170 void matsym(const T* in, const int n, const Parallel_Blkcyclic& pmat, T* out);
171 template<typename T>
172 void matsym(T* inout, const int n, const Parallel_Blkcyclic& pmat);
173 template<typename T>
174 void mattrans(const T* in, const int n, const Parallel_Blkcyclic& pmat, T* out);
175 template<typename T>
176 void mattrans(T* inout, const int n, const Parallel_Blkcyclic& pmat);
177
178 // calculate (A-A^T)/2 (in-place version)
179 template<typename T>
180 void matantisym(T* inout, const int n, const Parallel_Blkcyclic& pmat);
181#endif
182 template<typename T>
183 bool is_hermitian(const T* mat, const Parallel_Blkcyclic& pmat, const double threshold, const int my_rank);
184
185 template<typename T>
186 bool is_symmetric(const T* mat, const Parallel_Blkcyclic& pmat, const double threshold, const int my_rank);
187
190 template<typename T, typename Device>
191 psi::Psi<T, Device> k1_to_bfirst_wrapper(const psi::Psi<T, Device>& psi_kfirst, int nk_in, int nbasis_in);
193 template<typename T, typename Device>
195
197 // pack the process to setup 2d divion reusing blacs_ctxt of a new 2d-matrix
198 void setup_2d_division(Parallel_Blkcyclic& pv, int nb, int gr, int gc);
199
202 template <typename T>
203 void global2local_X(T* local_X, const T* global_X, const int nband, const int nk,
204 const std::vector<int>& nocc, const std::vector<int>& nvirt, const std::vector<Parallel_Blkcyclic>& pX,
205 const bool openshell);
206#ifdef __MPI
207 // pack the process to setup 2d divion reusing blacs_ctxt of an existing 2d-matrix
208 void setup_2d_division(Parallel_Blkcyclic& pv, int nb, int gr, int gc, const int& blacs_ctxt_in);
209
211 template <typename T>
212 struct MPIType {
213 static MPI_Datatype value() { return MPI_DATATYPE_NULL; }
214 };
215 template <>
216 struct MPIType<int> {
217 static MPI_Datatype value() { return MPI_INT; }
218 };
219 template <>
220 struct MPIType<double> {
221 static MPI_Datatype value() { return MPI_DOUBLE; }
222 };
223 template <>
224 struct MPIType<std::complex<double>> {
225 static MPI_Datatype value() { return MPI_DOUBLE_COMPLEX; }
226 };
227
228 template <typename T>
229 void pA2pX(T* X_pX, const T* X_pA, const int nband, const int nk,
230 const std::vector<int>& nocc, const std::vector<int>& nvirt,
231 const std::vector<Parallel_Blkcyclic>& pX, const Parallel_Blkcyclic& pA,
232 const int row_offset, const int col_offset, const bool openshell,
233 const int my_rank, const int nproc);
234
237 template <typename T>
238 void gather_2d_to_full(const Parallel_Blkcyclic& pv, const T* submat, T* fullmat,
239 const bool row_major, const std::size_t global_nrow, const std::size_t global_ncol);
240
242 template <typename T>
243 void scatter_full_to_2d(const Parallel_Blkcyclic& pv, const T* fullmat, T* submat, const bool col_first = false);
244#endif
245
248 template<typename T>
249 void diag_lapack(const int& n, T* mat, double* eig);
250
252 template<typename T>
253 void diag_lapack_nh(const int& n, T* mat, std::complex<double>* eig);
256 template<typename T>
257 int lapack_linear_solver(const T* A, T* x, const T* b, const int n, const int nrhs);
259 std::string tolower(const std::string& str);
260 std::string toupper(const std::string& str);
261}
263template<typename T>
264std::vector<T> operator+(const std::vector<T>& a, const std::vector<T>& b)
265{
266 const int maxsize = std::max(a.size(), b.size());
267 const int minsize = std::min(a.size(), b.size());
268 std::vector<T> c(maxsize);
269 for (int i = 0; i < minsize; ++i)
270 {
271 c[i] = a[i] + b[i];
272 }
273 for (int i = minsize; i < maxsize; ++i)
274 {
275 c[i] = (a.size() > b.size() ? a[i] : b[i]);
276 }
277 return c;
278}
279#include "lr_util.hpp"
280
281#endif // ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_H
Definition matrix.h:15
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
Provide the basic information about unitcell.
Definition unitcell.h:16
A class for representing the shape of a tensor.
Definition tensor_shape.h:13
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
Definition psi.h:37
#define complex
Definition elpa_new.h:28
#define T
Definition exp.cpp:237
std::vector< T > operator+(const std::vector< T > &a, const std::vector< T > &b)
=================operators======================= (should ot in namespace LR_Util)
Definition lr_util.h:264
container::DeviceType DEV
Definition bse_util.h:63
container::DataType DAT
================ Container ===============
Definition bse_util.h:62
Definition exciton_plotter.cpp:368
void scatter_full_to_2d(const Parallel_Blkcyclic &pv, const T *fullmat, T *submat, const bool col_first=false)
scatter full matrix to 2d block-cyclic distributed matrix
Definition lr_util.hpp:593
void global2local_X(T *local_X, const T *global_X, const int nband, const int nk, const std::vector< int > &nocc, const std::vector< int > &nvirt, const std::vector< Parallel_Blkcyclic > &pX, const bool openshell)
assign global X to 2d-matrix, its col is band(excition state), and row is { spin, k-point,...
Definition lr_util.hpp:292
int lapack_linear_solver(const T *A, T *x, const T *b, const int n, const int nrhs)
solve linear equations Ax=b using LAPACK
double hse_sigma_floor()
Existing HSE sigma floor (bohr^-8), shared by LR and the GS fallback.
Definition lr_util.cpp:12
int cal_nelec(const TCell &ucell)
calculate the number of electrons
Definition lr_util.hpp:22
ct::Tensor newTensor(const ct::TensorShape &shape)
Definition lr_util.h:156
int cal_nocc_window(int requested_nocc, int nocc_max)
Retain a positive user window, otherwise use all occupied orbitals.
Definition lr_util.cpp:167
void matsym(const T *in, const int n, T *out)
Definition lr_util.hpp:79
bool is_hermitian(const T *mat, const Parallel_Blkcyclic &pmat, const double threshold, const int my_rank)
Definition lr_util.hpp:107
void setup_2d_division(Parallel_Blkcyclic &pv, int nb, int gr, int gc)
=================2D-block Parallel===============
Definition lr_util.cpp:436
void diag_lapack(const int &n, T *mat, double *eig)
diagonalize a hermitian matrix
int cal_nocc(int nelec)
=================PHYSICS====================
Definition lr_util.cpp:149
bool has_local_xc(const std::string &name)
check if the xc functional has local xc kernel
Definition lr_util.h:80
std::string toupper(const std::string &str)
Definition lr_util.cpp:575
std::string tolower(const std::string &str)
=================string option====================
Definition lr_util.cpp:569
void matantisym(T *inout, const int n, const Parallel_Blkcyclic &pmat)
void diag_lapack_nh(const int &n, T *mat, std::complex< double > *eig)
diagonalize a general matrix
double inner_product(const double *vec1, const double *vec2, const int &size)
=================ALGORITHM====================
Definition lr_util.h:126
std::string force_window_warning(const int basis_size, const int gs_bands, const int skipped_occupied, const std::vector< int > &x_virtual, const std::vector< int > &z_virtual)
Diagnose resolved force windows without modifying X, Z or their energies.
Definition lr_util.cpp:52
psi::Psi< T, Device > bfirst_to_k1_wrapper(const psi::Psi< T, Device > &psi_bfirst)
psi(nb, nk, nbasis) -> psi(nk=1, nbands=nb, nk * nbasis) without memory copy
Definition lr_util.hpp:271
psi::Psi< T, Device > k1_to_bfirst_wrapper(const psi::Psi< T, Device > &psi_kfirst, int nk_in, int nbasis_in)
psi(nk=1, nbands=nb, nk * nbasis) -> psi(nb, nk, nbasis) without memory copy
Definition lr_util.hpp:252
void _allocate_2order_nested_ptr(T **&p2, size_t size1, size_t size2)
new 2d pointer
Definition lr_util.hpp:44
void gather_2d_to_full(const Parallel_Blkcyclic &pv, const T *submat, T *fullmat, const bool row_major, const std::size_t global_nrow, const std::size_t global_ncol)
gather 2d matrix to full matrix the defination of row and col is consistent with setup_2d_division
Definition lr_util.hpp:571
void check_force_pp(const UnitCell &cell, const bool cal_force, const std::string &calculation)
Reject unsupported NLCC force requests after pseudopotentials have been loaded.
Definition lr_util.cpp:91
void _deallocate_2order_nested_ptr(T **p2, size_t size)
delete 2d pointer
Definition lr_util.hpp:58
int spin_population_difference(const ModuleBase::matrix &wg, const int kpar, const int rank_in_pool)
Sum k-point spin populations before rounding; count each pool only once.
Definition lr_util.cpp:13
void pA2pX(T *X_pX, const T *X_pA, const int nband, const int nk, const std::vector< int > &nocc, const std::vector< int > &nvirt, const std::vector< Parallel_Blkcyclic > &pX, const Parallel_Blkcyclic &pA, const int row_offset, const int col_offset, const bool openshell, const int my_rank, const int nproc)
assign X in pA to X in pX
Definition lr_util.hpp:367
bool is_symmetric(const T *mat, const Parallel_Blkcyclic &pmat, const double threshold, const int my_rank)
Definition lr_util.hpp:172
std::pair< ModuleBase::matrix, std::vector< std::pair< int, int > > > set_ix_map_diagonal(bool mode, int nocc, int nvirt)
set the index map: ix to (ic, iv) and vice versa by diagonal traverse the c-v pairs leftdown -> right...
Definition lr_util.cpp:177
void mattrans(const T *in, const int n, const Parallel_Blkcyclic &pmat, T *out)
const std::vector< double > & prepare_xc_sigma(const std::vector< double > &sigma, const bool is_hse06, std::vector< double > &hse_buffer)
Preserve signed sigma invariants except for the existing HSE06 stabilization.
Definition lr_util.cpp:111
const std::set< std::string > & hybrid_xc_list()
the xc kernels that carry an exact-exchange (EXX) term
Definition lr_util.h:72
DataType
Definition tensor_enums.h:15
DeviceType
Definition tensor_enums.h:28
#define threshold
Definition sph_bessel_rec_test.cpp:4
static MPI_Datatype value()
Definition lr_util.h:221
static MPI_Datatype value()
Definition lr_util.h:217
static MPI_Datatype value()
Definition lr_util.h:225
Struct to get MPI_traits for different data types.
Definition lr_util.h:212
static MPI_Datatype value()
Definition lr_util.h:213
std::complex< double > type
Definition lr_util.h:24
std::complex< float > type
Definition lr_util.h:26
std::complex< double > type
Definition lr_util.h:25
std::complex< float > type
Definition lr_util.h:27
Definition lr_util.h:23
static constexpr DataType value
Definition tensor_enums.h:61
static constexpr DeviceType value
Definition tensor_enums.h:36
int rank_in_pool
Definition pw_test.cpp:12