ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
pulay_hc.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_PULAY_HC_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_PULAY_HC_H
10
12{
13// add stess later
15 template<typename TK>
18 const UnitCell& ucell,
19 const std::vector<hamilt::HContainer<double>>& dHS,
20 const double& factor_force = 1.0)
21{
22 ModuleBase::matrix f(ucell.nat, 3);
23 const Parallel_Orbitals& pv = *dHS[0].get_paraV();
24 const int& npol = ucell.get_npol();
25 const int nspin_dmr = dm.get_dmr_vec().size();
26 for (int ixyz = 0;ixyz < 3;++ixyz)
27 {
28 for (int iat0 = 0;iat0 < ucell.nat;++iat0)
29 {
30 for (int iat1 = 0;iat1 < ucell.nat;++iat1)
31 {
32 hamilt::AtomPair<double>* ap = dHS[ixyz].find_pair(iat0, iat1);
33 if (ap)
34 {
35 for (int iR = 0;iR < ap->get_R_size();++iR)
36 {
37 const ModuleBase::Vector3<int>& R = ap->get_R_index(iR);
38 hamilt::BaseMatrix<double>& mat_dhs = ap->get_HR_values(R.x, R.y, R.z);
39 std::vector<const hamilt::BaseMatrix<double>*> mat_dmr;
40 for (int is = 0; is < nspin_dmr; ++is)
41 {
42 mat_dmr.push_back(dm.get_dmr_ptr(is + 1)->find_matrix(iat0, iat1, R.x, R.y, R.z));
43 }
44
45 for (int mu = 0; mu < pv.get_nrow_atom(iat0); mu += npol)
46 {
47 for (int nu = 0; nu < pv.get_ncol_atom(iat1); nu += npol)
48 {
49 double dm2d = 0.0;
50 for (int is = 0; is < nspin_dmr; ++is)
51 {
52 dm2d += mat_dmr[is]->get_value(mu, nu);
53 }
54 f(iat0, ixyz) += dm2d * factor_force * 2.0 * mat_dhs.get_value(mu, nu);
55 }
56 }
57 }
58 }
59 }
60 }
61 }
63 return f;
64}
65
67template<typename TK>
70 const UnitCell& ucell,
71 const LR::PotLRBase* pot
72)
73{
74 ModuleBase::matrix force(ucell.nat, 3);
75 ModuleBase::matrix stress_tmp(3, 3);
76
77 // The LR kernel yields a single potential channel, so the grid integrals run with
78 // nspin=1 on the first density-matrix channel -- the same thing the old
79 // `Gint_inout(is=0, ...)` calls did. `dm` may still carry two channels (the
80 // `test_force` path feeds in the ground-state DM and doubles the result afterwards);
81 // ModuleGint reads only the leading `nspin` entries of `dm_vec`, but `vr_eff` is
82 // indexed up to `nspin`, so passing the DM's spin count here would read past
83 // `p_vr_hxc` and segfault.
84 constexpr int nspin_gint = 1;
85
86 // 1. dm->rho
87 double** rho;
88 const int& nrxx = pot->nrxx;
89 LR_Util::_allocate_2order_nested_ptr(rho, nspin_gint, nrxx);
91 ModuleGint::cal_gint_rho(dm.get_dmr_vec(), nspin_gint, rho, false);
92
93 // 2. v_hxc = f_hxc * rho
94 ModuleBase::matrix vr_hxc(1, nrxx); //grid
95 pot->cal_v_eff(rho, ucell, vr_hxc);
97
98 // 3. v(r) -> force
99 // An empty local FFT slab has no (0, 0) element. Pass the storage pointer
100 // directly; the grid integrator has no local points to read on that rank.
101 const std::vector<const double*> p_vr_hxc(nspin_gint, vr_hxc.c);
102 ModuleGint::cal_gint_fvl(nspin_gint, p_vr_hxc, dm.get_dmr_vec(), /*isforce=*/true, /*isstress=*/false, &force, &stress_tmp);
103 // `cal_gint_fvl` only sums the grid points (and their atom pairs) this rank's share of the
104 // real-space FFT box touches; core ABACUS always follows it with this same reduction (see
105 // `force_stress_lcao.cpp`'s `Parallel_Reduce::reduce_pool` right after its own grid-based
106 // `cal_pulay_fs` call) -- omitted here, so this was silently wrong for nprocs>1.
107 Parallel_Reduce::reduce_pool(force.c, force.nr * force.nc);
108 return force;
109}
110
118template<typename TK>
121 const UnitCell& ucell,
122 const LR::PotLRBase* pot)
123{
124 ModuleBase::matrix force(ucell.nat, 3);
125 ModuleBase::matrix stress_tmp(3, 3);
126 constexpr int nspin_dm = 2;
127 assert(dm.get_dmr_vec().size() == nspin_dm);
128
129 // 1. dm -> rho, one channel each
130 double** rho;
131 const int& nrxx = pot->nrxx;
132 LR_Util::_allocate_2order_nested_ptr(rho, nspin_dm, nrxx);
133 for (int is = 0; is < nspin_dm; ++is)
134 {
135 ModuleBase::GlobalFunc::ZEROS(rho[is], nrxx);
136 }
137 ModuleGint::cal_gint_rho(dm.get_dmr_vec(), nspin_dm, rho, false);
138
139 // 2. $v_\sigma=\sum_{\sigma'}f^{\sigma\sigma'}\rho_{\sigma'}$
140 std::vector<ModuleBase::matrix> vr_hxc(nspin_dm, ModuleBase::matrix(1, nrxx));
141 for (int sl = 0; sl < nspin_dm; ++sl)
142 {
143 for (int sr = 0; sr < nspin_dm; ++sr)
144 {
145 double* rho_in[1] = { rho[sr] };
146 pot->cal_v_eff(rho_in, ucell, vr_hxc[sl], { sl, sr });
147 }
148 }
150
151 // 3. v(r) -> force, summed over the outer spin by `cal_gint_fvl`
152 std::vector<const double*> p_vr_hxc(nspin_dm);
153 // Keep empty-grid ranks in the integration and subsequent pool reduction.
154 for (int is = 0; is < nspin_dm; ++is)
155 {
156 p_vr_hxc[is] = vr_hxc[is].c;
157 }
158 ModuleGint::cal_gint_fvl(nspin_dm, p_vr_hxc, dm.get_dmr_vec(), /*isforce=*/true, false, &force, &stress_tmp);
159 Parallel_Reduce::reduce_pool(force.c, force.nr * force.nc); // see the closed-shell overload above
160 return force;
161}
162}
163
164#endif // ABACUS_SOURCE_LCAO_MODULE_LR_PULAY_HC_H
Definition pot_lr_base.h:18
virtual void cal_v_eff(double **rho, const UnitCell &ucell, ModuleBase::matrix &v_eff, const std::vector< int > &ispin_op={ 0, 0 }) const =0
const int & nrxx
Definition pot_lr_base.h:27
3 elements vector
Definition vector3.h:21
T x
Definition vector3.h:23
T y
Definition vector3.h:24
T z
Definition vector3.h:25
Definition matrix.h:15
int nr
Definition matrix.h:19
double * c
Definition matrix.h:21
int nc
Definition matrix.h:20
Definition parallel_orbitals.h:16
int get_nrow_atom(int iat) const
Definition parallel_orbitals.cpp:110
int get_ncol_atom(int iat) const
dimension getters for 2D-block-cyclic division of Hamiltonian matrix ncloc() : total number of column...
Definition parallel_orbitals.cpp:87
Provide the basic information about unitcell.
Definition unitcell.h:16
const int & get_npol() const
Get npol.
Definition unitcell.h:66
int nat
total number of atoms of all species in unitcell
Definition unitcell.h:155
Definition atom_pair.h:45
BaseMatrix< T > & get_HR_values(int rx_in, int ry_in, int rz_in)
get target BaseMatrix of target cell for const AtomPair, it will return a const BaseMatrix<T> object,...
Definition atom_pair.cpp:451
ModuleBase::Vector3< int > get_R_index(const int &index) const
Definition atom_pair.cpp:908
size_t get_R_size() const
Definition atom_pair.h:391
Definition base_matrix.h:20
T & get_value(const size_t &i_row, const size_t &j_col) const
get value from a whole matrix
Definition base_matrix.h:74
Definition hcontainer.h:147
BaseMatrix< T > * find_matrix(int i, int j, int rx, int ry, int rz)
find BaseMatrix with atom index atom_i and atom_j and R index (rx, ry, rz) This interface can be used...
Definition hcontainer.cpp:426
DensityMatrix Class <TK,TR> = <double,double> for Gamma-only calculation <TK,TR> = <std::complex<doub...
Definition density_matrix.h:172
const std::vector< hamilt::HContainer< TR > * > & get_dmr_vec() const
get pointer vector of DMR
Definition density_matrix.h:301
hamilt::HContainer< TR > * get_dmr_ptr(const int ispin) const
get pointer of DMR
Definition dm_getter.cpp:19
void _allocate_2order_nested_ptr(T **&p2, size_t size1, size_t size2)
new 2d pointer
Definition lr_util.hpp:44
void _deallocate_2order_nested_ptr(T **p2, size_t size)
delete 2d pointer
Definition lr_util.hpp:58
void ZEROS(std::complex< T > *u, const TI n)
Definition global_function.h:109
void cal_gint_fvl(const int nspin, const std::vector< const double * > &vr_eff, const std::vector< HContainer< double > * > &dm_vec, const bool isforce, const bool isstress, ModuleBase::matrix *fvl, ModuleBase::matrix *svl)
Definition gint_interface.cpp:142
void cal_gint_rho(const std::vector< HContainer< double > * > &dm_vec, const int nspin, double **rho, bool is_dm_symm)
Definition gint_interface.cpp:94
void reduce_all(T &object)
reduce in all process
Definition depend_mock.cpp:14
void reduce_pool(T &object)
Definition depend_mock.cpp:15
Definition pulay_hc.h:12
ModuleBase::matrix cal_pulay_fs_openshell(const module_dm::DensityMatrix< TK, double > &dm, const UnitCell &ucell, const LR::PotLRBase *pot)
Open-shell (spin-unrestricted) counterpart of the grid cal_pulay_fs above.
Definition pulay_hc.h:119
ModuleBase::matrix cal_pulay_fs(const module_dm::DensityMatrix< TK, double > &dm, const UnitCell &ucell, const std::vector< hamilt::HContainer< double > > &dHS, const double &factor_force=1.0)
for 2-center-integration terms, provided HS derivatives
Definition pulay_hc.h:16