1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_ZEQ_SOLVER_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_ZEQ_SOLVER_H
13 void solve_Z_CG(
double*
const Z,
double* R,
const int& ld,
const int& nstates,
14 std::function<
void(
const double*
const,
double*
const)> f_LZ,
const bool test_force);
15 void solve_Z_CG(std::complex<double>*
const Z, std::complex<double>* R,
const int& ld,
const int& nstates,
16 std::function<
void(
const std::complex<double>*
const, std::complex<double>*
const)> f_LZ,
const bool test_force);
19 template<
typename THam>
23 for (
int is = 0; is < nspin_x; ++is)
25 n_global += hm.nk * hm.nocc[is] * hm.nvirt[is];
33 template<
typename T,
typename THam>
38 std::fill(full, full + n_global,
T(0));
41 for (
int is = 0;is < nspin_x;++is)
43 const int npairs = hm.nocc[is] * hm.nvirt[is];
44 const int lsize = hm.pX[is].size();
45 for (
int ik = 0;ik < hm.nk;++ik)
48 full + goffset + ik * npairs,
false, hm.nvirt[is], hm.nocc[is]);
50 loffset += hm.nk * lsize;
51 goffset += hm.nk * npairs;
57 template<
typename T,
typename THam>
62 for (
int is = 0;is < nspin_x;++is)
64 const int npairs = hm.nocc[is] * hm.nvirt[is];
65 const int lsize = hm.pX[is].size();
66 for (
int ik = 0;ik < hm.nk;++ik)
69 local + loffset + ik * lsize,
false);
71 loffset += hm.nk * lsize;
72 goffset += hm.nk * npairs;
85 template<
typename T,
typename THam>
86 inline void solve_Z_lapack(
T*
const Z,
const T*
const R,
const int& ld,
const int& nstates,
87 THam& hm,
const int nspin_x,
const bool test_force)
93 for (
int is = 0; is < nspin_x; ++is)
95 ld_expect += hm.nk * hm.pX[is].size();
97 assert(ld == ld_expect);
99 std::vector<T> hessian_full = hm.matrix();
100 std::vector<T> Z_full(
static_cast<std::size_t
>(n_global) * nstates,
T(0.0));
107 std::vector<T> R_full(
static_cast<std::size_t
>(n_global) * nstates,
T(0.0));
109 for (
int istate = 0; istate < nstates; ++istate)
114 std::copy(R, R +
static_cast<std::size_t
>(n_global) * nstates, R_full.begin());
122 throw std::runtime_error(
"LAPACK Z-vector solve failed (info=" + std::to_string(info)
123 +
"); the response Hessian may be singular. Excited-state forces are not valid.");
129 std::cout <<
"The full Z-vector solved by LAPACK:" << std::endl;
135 for (
int istate = 0; istate < nstates; ++istate)
140 std::copy(Z_full.begin(), Z_full.end(), Z);
144 std::cout <<
"The local Z-vector solved by LAPACK:" << std::endl;
156 template<
typename T,
typename THam>
161 const int n_global = ph.
nrglb();
162 std::vector<T> h_loc(ph.
size(),
T(0));
163 std::vector<T> e_full(n_global,
T(0));
164 std::vector<T> e_loc(ld,
T(0));
165 std::vector<T> he_loc(ld,
T(0));
166 std::vector<T> he_full(n_global,
T(0));
167 for (
int gcol = 0; gcol < n_global; ++gcol)
172 hm.hPsi(e_loc.data(), he_loc.data(), ld, 1);
174 const int lcol = ph.
icloc(gcol);
179 T*
const h_col = h_loc.data() +
static_cast<std::size_t
>(lcol) * ph.
nrloc();
180 for (
int lrow = 0; lrow < ph.
nrloc(); ++lrow)
182 h_col[lrow] = he_full[ph.
irglb(lrow)];
193 template<
typename T,
typename THam>
194 void solve_Z_2d(
T*
const Z,
const T*
const R,
const int ld,
const int nstates,
195 THam& hm,
const int nspin_x,
203 const int nproc_dim = std::max(px0.
nrproc(), px0.
ncproc());
204 const int nb = std::max(1, std::min(32, n_global / nproc_dim));
213 std::vector<T> z_loc(pz.
size(),
T(0));
214 std::vector<T> r_full(n_global,
T(0));
215 for (
int istate = 0; istate < nstates; ++istate)
218 const int lcol = pz.
icloc(istate);
223 T*
const z_col = z_loc.data() +
static_cast<std::size_t
>(lcol) * pz.
nrloc();
224 for (
int lrow = 0; lrow < pz.
nrloc(); ++lrow)
226 z_col[lrow] = r_full[pz.
irglb(lrow)];
230 linear_solver(h_loc.data(), z_loc.data(), ph, pz);
233 std::vector<T> z_full(
static_cast<std::size_t
>(n_global) * nstates,
T(0));
235 for (
int istate = 0; istate < nstates; ++istate)
237 zvec_full_to_local(hm, nspin_x, z_full.data() +
static_cast<std::size_t
>(istate) * n_global, Z + istate * ld);
245 template<
typename T,
typename THam>
247 THam& hm,
const int nspin_x)
252 throw std::runtime_error(
"Z-vector solver 'scalapack' needs an MPI build; use 'lapack' or 'cg'");
259 template<
typename T,
typename THam>
261 THam& hm,
const int nspin_x)
266 throw std::runtime_error(
"Z-vector solver 'scalapack_chol' needs an MPI build; use 'lapack' or 'cg'");
273 template<
typename T,
typename THamL>
275 THamL& ops_L,
const int nspin_x,
const std::string& zvec_solver,
const bool test_force)
277 for (
int i = 0; i < nstates * ld; ++i)
281 if (zvec_solver ==
"cg")
288 [&ops_L, ld, nstates](
const T*
const in,
T*
const out) {
289 ops_L.hPsi(in, out, ld, nstates);
293 else if (zvec_solver ==
"lapack")
297 else if (zvec_solver ==
"scalapack")
301 else if (zvec_solver ==
"scalapack_chol")
307 throw std::runtime_error(
"Unsupported Z-vector solver: " + zvec_solver);
315 template<
typename T,
typename TOpsR,
typename TOpsL>
318 const int nloc_per_band,
const int nstates,
const std::string& zvec_solver,
const bool test_force,
const std::vector<std::vector<ct::Tensor>>& window_density)
321 ops_R.hPsi(X, R.template data<T>(), nloc_per_band, nstates, window_density);
324 solve_zeq_with(Z, R.template data<T>(), nloc_per_band, nstates, ops_L, nspin_x, zvec_solver, test_force);
333 std::weak_ptr<PotHxcLR> pot,
334 const std::string& spin_type,
335 const std::string& in_dir,
336 const bool openshell,
337 const std::string& zvec_solver,
const std::vector<std::vector<ct::Tensor>>& window_density);
static void start(void)
Start total time calculation.
Definition timer.cpp:44
static void end(const std::string &class_name_in, const std::string &name_in)
Definition timer.cpp:109
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
int irglb(const int irloc) const
Definition parallel_blkcyc.h:88
int ncproc() const
Definition parallel_blkcyc.h:63
int nrproc() const
Definition parallel_blkcyc.h:58
int nrloc() const
Definition parallel_blkcyc.h:35
int ictxt() const
Definition parallel_blkcyc.h:130
std::size_t size() const
Definition parallel_blkcyc.h:48
int nrglb() const
Definition parallel_blkcyc.cpp:75
int icloc(const int icglb) const
Definition parallel_blkcyc.h:83
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
#define T
Definition exp.cpp:237
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
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
void setup_2d_division(Parallel_Blkcyclic &pv, int nb, int gr, int gc)
=================2D-block Parallel===============
Definition lr_util.cpp:436
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
int print_value(const T *ptr, const int &size)
Definition lr_util_print.h:91
Definition esolver_lr_lcao_tddft.h:28
void build_and_solve_zeq(TOpsR &ops_R, TOpsL &ops_L, const int nspin_x, const T *const X, container::Tensor &R, T *const Z, const int nloc_per_band, const int nstates, const std::string &zvec_solver, const bool test_force, const std::vector< std::vector< ct::Tensor > > &window_density)
Definition zeq_solver.h:316
void solve_zeq_with(T *const Z, T *const R, const int ld, const int nstates, THamL &ops_L, const int nspin_x, const std::string &zvec_solver, const bool test_force)
Run the configured solver (and then, for testing, every supported one). Shared by the closed- and ope...
Definition zeq_solver.h:274
void solve_Z_2d(T *const Z, const T *const R, const int ld, const int nstates, THam &hm, const int nspin_x, void(*linear_solver)(T *, T *, const Parallel_Blkcyclic &, const Parallel_Blkcyclic &))
Distributed dense Z-vector solve: the Hessian (zvec_hessian_2d) and the right-hand side are laid out ...
Definition zeq_solver.h:194
void scalapack_linear_solver(T *A, T *B, const Parallel_Blkcyclic &pA, const Parallel_Blkcyclic &pB)
Solve A Z = B with ScaLAPACK LU (p?gesv), the distributed counterpart of LR_Util::lapack_linear_solve...
Definition zeqlin_solv.cpp:35
void zvec_full_to_local(THam &hm, const int nspin_x, const T *const full, T *const local)
Inverse of zvec_local_to_full: pick this rank's entries of the global vector full into the hm....
Definition zeq_solver.h:58
int zvec_global_dim(THam &hm, const int nspin_x)
Global length of one state's Z-vector: $\sum_\sigma n_k n_{occ,\sigma} n_{virt,\sigma}...
Definition zeq_solver.h:20
void zvec_local_to_full(THam &hm, const int nspin_x, const T *const local, T *const full)
Gather one state's Z-vector from the hm.pX layout (local, length ld) into the global vector full (len...
Definition zeq_solver.h:34
void solve_Z_scalapack_chol(T *const Z, const T *const R, const int ld, const int nstates, THam &hm, const int nspin_x)
Distributed dense Z-vector solve with a ScaLAPACK Cholesky factorization (p?potrf),...
Definition zeq_solver.h:260
void scalapack_cholesky_linear_solver(T *A, T *B, const Parallel_Blkcyclic &pA, const Parallel_Blkcyclic &pB)
Solve A Z = B for a Hermitian positive-definite A with ScaLAPACK alone: Cholesky p?...
Definition zeqlin_solv.cpp:58
void Z_vector_equation(const GradientInputs< T > &inputs, const T *const X, T *const Z, const int &nstates, std::weak_ptr< PotHxcLR > pot, const std::string &spin_type, const std::string &in_dir, const bool openshell, const std::string &zvec_solver, const std::vector< std::vector< ct::Tensor > > &window_density)
Definition zeq_solver.cpp:7
void solve_Z_scalapack(T *const Z, const T *const R, const int ld, const int nstates, THam &hm, const int nspin_x)
Distributed dense Z-vector solve with ScaLAPACK LU (p?gesv). Same system as solve_Z_lapack,...
Definition zeq_solver.h:246
std::vector< T > zvec_hessian_2d(THam &hm, const int nspin_x, const int ld, const Parallel_Blkcyclic &ph)
Build this rank's block of the Z-vector Hessian on the 2D block-cyclic layout ph (n_global x n_global...
Definition zeq_solver.h:157
void solve_Z_CG(double *const Z, double *R, const int &ld, const int &nstates, std::function< void(const double *const, double *const)> f_LZ, const bool test_force)
Definition zeq_cg.cpp:8
void solve_Z_lapack(T *const Z, const T *const R, const int &ld, const int &nstates, THam &hm, const int nspin_x, const bool test_force)
Solve the Z-vector equation with a dense LAPACK solve.
Definition zeq_solver.h:86
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18