ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
hamilt_zequlr.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_HAMILT_ZEQULR_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_HAMILT_ZEQULR_H
7#include <cassert>
8#include <cstring>
9#include <vector>
10#include <algorithm>
11#include <cstdlib>
12#include <string>
13
14namespace LR
15{
29 template<typename T>
30 class ZeqULR
31 {
32 public:
33 // Dimensions include all k points and both concatenated spin channels.
34 // The caller prepares them; construction only binds immutable layout data.
35 ZeqULR(const std::vector<int>& nocc_in,
36 const std::vector<int>& nvirt_in,
37 const std::vector<Parallel_Blkcyclic>& pX_in,
38 const int nk_in,
39 const int local_dimension,
40 const int global_dimension)
41 : nocc(nocc_in), nvirt(nvirt_in), pX(pX_in), nk(nk_in),
42 ldim(local_dimension), gdim(global_dimension),
43 ops(4, nullptr)
44 {
45 }
46 virtual ~ZeqULR()
47 {
48 for (auto& op: this->ops)
49 {
50 delete op;
51 }
52 }
53
54 void hPsi(const T* const psi_in, T* const hpsi, const int ld_psi, const int nband)
55 {
56 this->apply(psi_in, hpsi, ld_psi, nband, nullptr);
57 }
58
59 // The nonlinear RHS may supply one additive AO density per state/spin/k.
60 void hPsi(const T* psi_in, T* hpsi, const int ld_psi, const int nband,
61 const std::vector<std::vector<ct::Tensor>>& window_density)
62 {
63 assert(window_density.empty() || window_density.size() == nband);
64 this->apply(psi_in, hpsi, ld_psi, nband, &window_density);
65 }
66
67 protected:
68 void apply(const T* const psi_in, T* const hpsi, const int ld_psi,
69 const int nband,
70 const std::vector<std::vector<ct::Tensor>>* window_density)
71 {
72 assert(ld_psi == this->ldim);
73 const std::vector<int> ldim_is = { static_cast<int>(nk * pX[0].size()), static_cast<int>(nk * pX[1].size()) };
74 for (int ib = 0;ib < nband;++ib)
75 {
76 const int offset_band = ib * ld_psi;
77 const auto* density_add = window_density == nullptr || window_density->empty()
78 ? nullptr : &window_density->at(ib);
79 // All four spin blocks accumulate into this band's output, and the diagonal
80 // `OperatorLRDiag`s now add rather than assign (see `operator_lr_diag.h`), so the
81 // buffer has to start clean.
82 std::fill(hpsi + offset_band, hpsi + offset_band + ld_psi, T(0));
83 // Terms that are not bilinear in a (out, in) spin pair -- currently only the
84 // $g^{xc}$ term of the right-hand side, which is quadratic in $D^X$ and needs
85 // both transition-density channels on the grid at once.
86 this->band_extra(psi_in + offset_band, hpsi + offset_band);
87 for (int is_in : {0, 1})
88 {
89 const int offset_in = offset_band + is_in * ldim_is[0];
90 this->set_dm(is_in, psi_in + offset_in, density_add);
91 typename OperatorLRHxc<T>::TransitionDensityCache density_cache;
92 for (int is_out : {0, 1})
93 {
94 const int offset_out = offset_band + is_out * ldim_is[0];
95 hamilt::Operator<T>* node(this->ops[(is_out << 1) + is_in]);
96 while (node != nullptr)
97 {
98 // `is_in` picks the density matrix (already done by `set_dm`), but the
99 // vector an operator consumes directly belongs to the OUT channel:
100 // $R_{ia\sigma}=-2\sum_{\sigma'}\sum_b X_{ib\sigma}
101 // K_{ab,\sigma\sigma'}[D^X_{\sigma'}]+\dots$
102 // -- only $D^X$ carries the summed spin. `OperatorLRHxc`'s CXC branch
103 // says the same thing in code: it reads `psi_in` through `pX[sl]`, the
104 // OUT channel's distribution.
105 const T* input = psi_in + offset_out;
106 T* output = hpsi + offset_out;
107 act_with_shared_density(node, ldim_is[is_out], input, output, density_cache);
108 node = (hamilt::Operator<T>*)(node->next_op);
109 }
110 }
111 }
112 }
113 }
114
115 public:
117 std::vector<T> matrix()
118 {
119 ModuleBase::TITLE("ZeqULR", "matrix");
120 const std::vector<int> npairs = { nocc[0] * nvirt[0], nocc[1] * nvirt[1] };
121 const std::vector<int> ldim_is = { static_cast<int>(nk * pX[0].size()), static_cast<int>(nk * pX[1].size()) };
122 const std::vector<int> gdim_is = { nk * npairs[0], nk * npairs[1] };
123 std::vector<T> mat_full(static_cast<std::size_t>(gdim) * gdim, T(0));
124 for (int is_in : {0, 1})
125 {
126 const auto& px = this->pX[is_in];
127 const int loffset_in = is_in * ldim_is[0];
128 const int goffset_in = is_in * gdim_is[0];
129 for (int ik_in = 0;ik_in < nk;++ik_in)
130 {
131 for (int j = 0;j < nocc[is_in];++j)
132 {
133 for (int b = 0;b < nvirt[is_in];++b)
134 {
135 const int gcol = goffset_in + ik_in * npairs[is_in] + j * nvirt[is_in] + b;
136 std::vector<T> X_col(this->ldim, T(0));
137 const int lj = px.icloc(j);
138 const int lb = px.irloc(b);
139 if (px.in_this_processor(b, j))
140 {
141 X_col[loffset_in + ik_in * px.size() + lj * px.nrloc() + lb] = T(1);
142 }
143 this->set_dm(is_in, X_col.data() + loffset_in, nullptr);
144 std::vector<T> col(this->ldim, T(0));
145 for (int is_out : {0, 1})
146 {
147 const int loffset_out = is_out * ldim_is[0];
148 const int goffset_out = is_out * gdim_is[0];
149 const auto& pax = this->pX[is_out];
150 hamilt::Operator<T>* node(this->ops[(is_out << 1) + is_in]);
151 while (node != nullptr)
152 {
153 node->act(1, ldim_is[is_in], /*npol=*/1,
154 X_col.data() + loffset_in, col.data() + loffset_out);
155 node = (hamilt::Operator<T>*)(node->next_op);
156 }
157#ifdef __MPI
158 for (int ik_out = 0;ik_out < this->nk;++ik_out)
159 {
161 col.data() + loffset_out + ik_out * pax.size(),
162 mat_full.data() + static_cast<std::size_t>(gcol) * gdim
163 + goffset_out + ik_out * npairs[is_out],
164 false, nvirt[is_out], nocc[is_out]);
165 }
166#else
167 std::memcpy(mat_full.data() + static_cast<std::size_t>(gcol) * gdim + goffset_out,
168 col.data() + loffset_out, gdim_is[is_out] * sizeof(T));
169#endif
170 }
171 }
172 }
173 }
174 }
175 return mat_full;
176 }
177
178 const std::vector<int>& nocc;
179 const std::vector<int>& nvirt;
180 const std::vector<Parallel_Blkcyclic>& pX;
181 const int nk = 1;
182 const int ldim = 1;
183 const int gdim = 1;
184
185 protected:
187 virtual void set_dm(const int is, const T* const X,
188 const std::vector<ct::Tensor>* density_add) = 0;
192 virtual void band_extra(const T* const X, T* const out) const {}
193 std::vector<hamilt::Operator<T>*> ops;
194 };
195}
196
197#endif // ABACUS_SOURCE_LCAO_MODULE_LR_HAMILT_ZEQULR_H
std::map< const module_dm::DensityMatrix< T, T > *, std::vector< std::vector< double > > > TransitionDensityCache
Definition operator_lr_hxc.h:73
std::vector< hamilt::Operator< T > * > ops
Definition hamilt_zequlr.h:193
virtual ~ZeqULR()
Definition hamilt_zequlr.h:46
const int gdim
Definition hamilt_zequlr.h:183
const std::vector< int > & nvirt
Definition hamilt_zequlr.h:179
ZeqULR(const std::vector< int > &nocc_in, const std::vector< int > &nvirt_in, const std::vector< Parallel_Blkcyclic > &pX_in, const int nk_in, const int local_dimension, const int global_dimension)
Definition hamilt_zequlr.h:35
const std::vector< int > & nocc
Definition hamilt_zequlr.h:178
void hPsi(const T *psi_in, T *hpsi, const int ld_psi, const int nband, const std::vector< std::vector< ct::Tensor > > &window_density)
Definition hamilt_zequlr.h:60
virtual void set_dm(const int is, const T *const X, const std::vector< ct::Tensor > *density_add)=0
rebuild the density matrices the operators read, from the is block of X
const int ldim
Definition hamilt_zequlr.h:182
void apply(const T *const psi_in, T *const hpsi, const int ld_psi, const int nband, const std::vector< std::vector< ct::Tensor > > *window_density)
Definition hamilt_zequlr.h:68
const int nk
Definition hamilt_zequlr.h:181
virtual void band_extra(const T *const X, T *const out) const
optional per-band contribution that does not fit the 2x2 block structure. NOTE matrix() deliberately ...
Definition hamilt_zequlr.h:192
std::vector< T > matrix()
The full (replicated) matrix, column by column. Only used by the LAPACK solver.
Definition hamilt_zequlr.h:117
void hPsi(const T *const psi_in, T *const hpsi, const int ld_psi, const int nband)
Definition hamilt_zequlr.h:54
const std::vector< Parallel_Blkcyclic > & pX
Definition hamilt_zequlr.h:180
Definition operator.h:42
Operator * next_op
interface type 3: return a Psi-type HPsi
Definition operator.h:112
virtual void act(const int nbands, const int nbasis, const int npol, const T *tmpsi_in, T *tmhpsi, const int ngk_ik=0, const bool is_first_node=false) const
Definition operator.h:95
Definition output.h:13
#define T
Definition exp.cpp:237
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
Definition esolver_lr_lcao_tddft.h:28
void act_with_shared_density(hamilt::Operator< T > *node, const int nbasis, const T *psi_in, T *hpsi, typename OperatorLRHxc< T >::TransitionDensityCache &density_cache)
Definition operator_lr_hxc.h:119
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
Input_para input
Definition test_io_sys_var.cpp:58