Skip to content

Commit 2ddef0c

Browse files
maki49mohanchen
andauthored
Refactor: Unify the interfaces of Pulay terms of force and stress (#5130)
* unify gint terms * extract edm * center2 terms * remove the useless files * flatting the directory * remove GlobalV::NSPIN * remove Chinese comments --------- Co-authored-by: Mohan Chen <[email protected]>
1 parent 7e6eee7 commit 2ddef0c

20 files changed

+513
-817
lines changed

source/Makefile.Objects

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,9 @@ OBJS_LCAO=evolve_elec.o\
568568
FORCE_STRESS.o\
569569
FORCE_gamma.o\
570570
FORCE_k.o\
571-
fvl_dphi_gamma.o\
572-
fvl_dphi_k.o\
573-
fedm_gamma.o\
574-
fedm_k.o\
575-
ftvnl_dphi_gamma.o\
576-
ftvnl_dphi_k.o\
571+
stress_tools.o\
572+
edm.o\
573+
pulay_force_stress_center2.o\
577574
fvnl_dbeta_gamma.o\
578575
fvnl_dbeta_k.o\
579576
grid_init.o\

source/module_hamilt_lcao/hamilt_lcaodft/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ if(ENABLE_LCAO)
1515
operator_lcao/td_nonlocal_lcao.cpp
1616
operator_lcao/sc_lambda_lcao.cpp
1717
operator_lcao/dftu_lcao.cpp
18+
pulay_force_stress_center2.cpp
1819
FORCE_STRESS.cpp
1920
FORCE_gamma.cpp
2021
FORCE_k.cpp
21-
fvl_dphi_gamma.cpp
22-
fvl_dphi_k.cpp
23-
fedm_gamma.cpp
24-
fedm_k.cpp
25-
ftvnl_dphi_gamma.cpp
26-
ftvnl_dphi_k.cpp
22+
stress_tools.cpp
23+
edm.cpp
2724
fvnl_dbeta_gamma.cpp
2825
fvnl_dbeta_k.cpp
2926
grid_init.cpp

source/module_hamilt_lcao/hamilt_lcaodft/FORCE.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Force_LCAO
9494
const bool isstress,
9595
ForceStressArrays& fsr,
9696
const UnitCell& ucell,
97-
const elecstate::DensityMatrix<T, double>* dm,
97+
const elecstate::DensityMatrix<T, double>& dm,
9898
const psi::Psi<T>* psi,
9999
const Parallel_Orbitals& pv,
100100
const elecstate::ElecState* pelec,
@@ -136,12 +136,16 @@ class Force_LCAO
136136
typename TGint<T>::type& gint,
137137
ModuleBase::matrix& fvl_dphi,
138138
ModuleBase::matrix& svl_dphi);
139+
140+
elecstate::DensityMatrix<T, double> cal_edm(const elecstate::ElecState* pelec,
141+
const psi::Psi<T>& psi,
142+
const elecstate::DensityMatrix<T, double>& dm,
143+
const K_Vectors& kv,
144+
const Parallel_Orbitals& pv,
145+
const int& nspin,
146+
const int& nbands,
147+
const UnitCell& ucell,
148+
Record_adj& ra) const;
139149
};
140150

141-
// this namespace used to store global function for some stress operation
142-
namespace StressTools
143-
{
144-
// set upper matrix to whole matrix
145-
void stress_fill(const double& lat0_, const double& omega_, ModuleBase::matrix& stress_matrix);
146-
} // namespace StressTools
147151
#endif

source/module_hamilt_lcao/hamilt_lcaodft/FORCE_gamma.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "module_elecstate/elecstate_lcao.h"
1414
#include "module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h"
1515
#include "module_io/write_HS.h"
16+
#include "module_hamilt_lcao/hamilt_lcaodft/pulay_force_stress.h"
1617

1718
template <>
1819
void Force_LCAO<double>::allocate(const Parallel_Orbitals& pv,
@@ -215,10 +216,17 @@ void Force_LCAO<double>::ftable(const bool isforce,
215216
// allocate DHloc_fixed_x, DHloc_fixed_y, DHloc_fixed_z
216217
this->allocate(pv, fsr, two_center_bundle, orb);
217218

219+
const double* dSx[3] = { fsr.DSloc_x, fsr.DSloc_y, fsr.DSloc_z };
220+
const double* dSxy[6] = { fsr.DSloc_11, fsr.DSloc_12, fsr.DSloc_13, fsr.DSloc_22, fsr.DSloc_23, fsr.DSloc_33 };
218221
// calculate the force related to 'energy density matrix'.
219-
this->cal_fedm(isforce, isstress, fsr, ucell, dm, psi, pv, pelec, foverlap, soverlap);
222+
PulayForceStress::cal_pulay_fs(foverlap, soverlap,
223+
this->cal_edm(pelec, *psi, *dm, *kv, pv, PARAM.inp.nspin, PARAM.inp.nbands, ucell, *ra),
224+
ucell, pv, dSx, dSxy, isforce, isstress);
220225

221-
this->cal_ftvnl_dphi(dm, pv, ucell, fsr, isforce, isstress, ftvnl_dphi, stvnl_dphi);
226+
const double* dHx[3] = { fsr.DHloc_fixed_x, fsr.DHloc_fixed_y, fsr.DHloc_fixed_z };
227+
const double* dHxy[6] = { fsr.DHloc_fixed_11, fsr.DHloc_fixed_12, fsr.DHloc_fixed_13, fsr.DHloc_fixed_22, fsr.DHloc_fixed_23, fsr.DHloc_fixed_33 };
228+
//tvnl_dphi
229+
PulayForceStress::cal_pulay_fs(ftvnl_dphi, stvnl_dphi, *dm, ucell, pv, dHx, dHxy, isforce, isstress);
222230

223231
this->cal_fvnl_dbeta(dm,
224232
pv,
@@ -231,9 +239,9 @@ void Force_LCAO<double>::ftable(const bool isforce,
231239
fvnl_dbeta,
232240
svnl_dbeta);
233241

234-
this->cal_fvl_dphi(isforce, isstress, pelec->pot, gint, fvl_dphi, svl_dphi);
242+
// vl_dphi
243+
PulayForceStress::cal_pulay_fs(fvl_dphi, svl_dphi, *dm, ucell, pelec->pot, gint, isforce, isstress, false/*reset dm to gint*/);
235244

236-
// caoyu add for DeePKS
237245
#ifdef __DEEPKS
238246
if (PARAM.inp.deepks_scf)
239247
{
@@ -314,23 +322,3 @@ void Force_LCAO<double>::ftable(const bool isforce,
314322
ModuleBase::timer::tick("Force_LCAO", "ftable");
315323
return;
316324
}
317-
318-
namespace StressTools
319-
{
320-
void stress_fill(const double& lat0_, const double& omega_, ModuleBase::matrix& stress_matrix)
321-
{
322-
assert(omega_ > 0.0);
323-
double weight = lat0_ / omega_;
324-
for (int i = 0; i < 3; ++i)
325-
{
326-
for (int j = 0; j < 3; ++j)
327-
{
328-
if (j > i)
329-
{
330-
stress_matrix(j, i) = stress_matrix(i, j);
331-
}
332-
stress_matrix(i, j) *= weight;
333-
}
334-
}
335-
}
336-
} // namespace StressTools

source/module_hamilt_lcao/hamilt_lcaodft/FORCE_k.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h"
1313
#include "module_hamilt_pw/hamilt_pwdft/global.h"
1414
#include "module_io/write_HS.h"
15+
#include "module_hamilt_lcao/hamilt_lcaodft/pulay_force_stress.h"
1516

1617
#include <map>
1718
#include <unordered_map>
@@ -312,14 +313,21 @@ void Force_LCAO<std::complex<double>>::ftable(const bool isforce,
312313
kv->get_nks(),
313314
kv->kvec_d);
314315

316+
const double* dSx[3] = { fsr.DSloc_Rx, fsr.DSloc_Ry, fsr.DSloc_Rz };
315317
// calculate the energy density matrix
316318
// and the force related to overlap matrix and energy density matrix.
317-
this->cal_fedm(isforce, isstress, fsr, ucell, dm, psi, pv, pelec, foverlap, soverlap, kv, ra);
319+
PulayForceStress::cal_pulay_fs(foverlap, soverlap,
320+
this->cal_edm(pelec, *psi, *dm, *kv, pv, PARAM.inp.nspin, PARAM.inp.nbands, ucell, *ra),
321+
ucell, pv, dSx, fsr.DH_r, isforce, isstress, ra, -1.0, 1.0);
318322

319-
this->cal_ftvnl_dphi(dm, pv, ucell, fsr, isforce, isstress, ftvnl_dphi, stvnl_dphi, ra);
323+
const double* dHx[3] = { fsr.DHloc_fixedR_x, fsr.DHloc_fixedR_y, fsr.DHloc_fixedR_z }; // T+Vnl
324+
const double* dHxy[6] = { fsr.stvnl11, fsr.stvnl12, fsr.stvnl13, fsr.stvnl22, fsr.stvnl23, fsr.stvnl33 }; //T
325+
// tvnl_dphi
326+
PulayForceStress::cal_pulay_fs(ftvnl_dphi, stvnl_dphi, *dm, ucell, pv, dHx, dHxy, isforce, isstress, ra, 1.0, -1.0);
320327

321328
// doing on the real space grid.
322-
this->cal_fvl_dphi(isforce, isstress, pelec->pot, gint, fvl_dphi, svl_dphi);
329+
// vl_dphi
330+
PulayForceStress::cal_pulay_fs(fvl_dphi, svl_dphi, *dm, ucell, pelec->pot, gint, isforce, isstress, false/*reset dm to gint*/);
323331

324332
this->cal_fvnl_dbeta(dm,
325333
pv,
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "FORCE.h"
2+
#include "module_elecstate/module_dm/cal_dm_psi.h"
3+
#include "module_base/memory.h"
4+
#include "module_parameter/parameter.h"
5+
template<>
6+
elecstate::DensityMatrix<double, double> Force_LCAO<double>::cal_edm(const elecstate::ElecState* pelec,
7+
const psi::Psi<double>& psi,
8+
const elecstate::DensityMatrix<double, double>& dm,
9+
const K_Vectors& kv,
10+
const Parallel_Orbitals& pv,
11+
const int& nspin,
12+
const int& nbands,
13+
const UnitCell& ucell,
14+
Record_adj& ra) const
15+
{
16+
ModuleBase::matrix wg_ekb;
17+
wg_ekb.create(nspin, nbands);
18+
19+
for(int is=0; is<nspin; is++)
20+
{
21+
for(int ib=0; ib<nbands; ib++)
22+
{
23+
wg_ekb(is,ib) = pelec->wg(is,ib) * pelec->ekb(is, ib);
24+
}
25+
}
26+
27+
// construct a DensityMatrix for Gamma-Only
28+
elecstate::DensityMatrix<double, double> edm(&pv, nspin);
29+
30+
#ifdef __PEXSI
31+
if (PARAM.inp.ks_solver == "pexsi")
32+
{
33+
auto pes = dynamic_cast<const elecstate::ElecStateLCAO<double>*>(pelec);
34+
for (int ik = 0; ik < nspin; ik++)
35+
{
36+
edm.set_DMK_pointer(ik, pes->get_DM()->pexsi_EDM[ik]);
37+
}
38+
39+
}
40+
else
41+
#endif
42+
{
43+
elecstate::cal_dm_psi(edm.get_paraV_pointer(), wg_ekb, psi, edm);
44+
}
45+
return edm;
46+
}
47+
48+
template<>
49+
elecstate::DensityMatrix<std::complex<double>, double> Force_LCAO<std::complex<double>>::cal_edm(const elecstate::ElecState* pelec,
50+
const psi::Psi<std::complex<double>>& psi,
51+
const elecstate::DensityMatrix<std::complex<double>, double>& dm,
52+
const K_Vectors& kv,
53+
const Parallel_Orbitals& pv,
54+
const int& nspin,
55+
const int& nbands,
56+
const UnitCell& ucell,
57+
Record_adj& ra) const
58+
{
59+
60+
// construct a DensityMatrix object
61+
elecstate::DensityMatrix<std::complex<double>, double> edm(&kv, &pv, nspin);
62+
63+
//--------------------------------------------
64+
// calculate the energy density matrix here.
65+
//--------------------------------------------
66+
67+
ModuleBase::matrix wg_ekb;
68+
wg_ekb.create(kv.get_nks(), nbands);
69+
ModuleBase::Memory::record("Force::wg_ekb", sizeof(double) * kv.get_nks() * nbands);
70+
#ifdef _OPENMP
71+
#pragma omp parallel for collapse(2) schedule(static, 1024)
72+
#endif
73+
for (int ik = 0; ik < kv.get_nks(); ik++)
74+
{
75+
for (int ib = 0; ib < nbands; ib++)
76+
{
77+
wg_ekb(ik, ib) = pelec->wg(ik, ib) * pelec->ekb(ik, ib);
78+
}
79+
}
80+
81+
// use the original formula (Hamiltonian matrix) to calculate energy density matrix
82+
if (dm.EDMK.size())
83+
{
84+
#ifdef _OPENMP
85+
#pragma omp parallel for schedule(static)
86+
#endif
87+
for (int ik = 0; ik < kv.get_nks(); ++ik)
88+
{
89+
edm.set_DMK_pointer(ik, dm.EDMK[ik].c);
90+
}
91+
}
92+
else
93+
{
94+
// cal_dm_psi
95+
elecstate::cal_dm_psi(edm.get_paraV_pointer(), wg_ekb, psi, edm);
96+
}
97+
98+
// cal_dm_2d
99+
edm.init_DMR(ra, &ucell);
100+
edm.cal_DMR();
101+
// edm.sum_DMR_spin();
102+
return edm;
103+
}

source/module_hamilt_lcao/hamilt_lcaodft/fedm_gamma.cpp

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)