diff --git a/source/source_hamilt/module_gint/gint_dvlocal.cpp b/source/source_hamilt/module_gint/gint_dvlocal.cpp index fc32db32539..6fbe70e0e68 100644 --- a/source/source_hamilt/module_gint/gint_dvlocal.cpp +++ b/source/source_hamilt/module_gint/gint_dvlocal.cpp @@ -1,7 +1,5 @@ -#include #include "gint_dvlocal.h" #include "phi_operator.h" -#include "source_base/parallel_reduce.h" namespace ModuleGint { @@ -58,217 +56,4 @@ void Gint_dvlocal::cal_hr_gint_() } } -void Gint_dvlocal::cal_dvlocal_R_sparse( - const int nspin, - const int cspin, - const int nlocal, - const double sparse_thr, - const Parallel_Orbitals& pv, - const UnitCell& ucell, - const Grid_Driver& gdriver, - LCAO_HS_Arrays& hs_arrays) -{ - ModuleBase::TITLE("Gint", "cal_dvlocal_R_sparse"); - ModuleBase::timer::start("Gint", "cal_dvlocal_R_sparse"); - std::map, std::map>> pvdpRx_sparse; - std::map, std::map>> pvdpRy_sparse; - std::map, std::map>> pvdpRz_sparse; - - double tmp_d = 0.0; - - Vec3d tau1, dtau; - for (int iap = 0; iap < pvdpRx.size_atom_pairs(); iap++) - { - const auto& ap = pvdpRx.get_atom_pair(iap); - const int iat1 = ap.get_atom_i(); - const int iat2 = ap.get_atom_j(); - const int it1 = ucell.iat2it[iat1]; - const int it2 = ucell.iat2it[iat2]; - const Atom* atom1 = &ucell.atoms[it1]; - const Atom* atom2 = &ucell.atoms[it2]; - const int start1 = ucell.itiaiw2iwt(it1, ucell.iat2ia[iat1], 0); - const int start2 = ucell.itiaiw2iwt(it2, ucell.iat2ia[iat2], 0); - - for (int ir = 0; ir < ap.get_R_size(); ir++) - { - const ModuleBase::Vector3 R = ap.get_R_index(ir); - Abfs::Vector3_Order dR(R.x, R.y, R.z); - double* p_pvdpRx = pvdpRx.get_atom_pair(iap).get_pointer(ir); - double* p_pvdpRy = pvdpRy.get_atom_pair(iap).get_pointer(ir); - double* p_pvdpRz = pvdpRz.get_atom_pair(iap).get_pointer(ir); - - for (int iw = 0; iw < atom1->nw * npol_; iw++) - { - for (int iw2 = 0; iw2 < atom2->nw * npol_; iw2++) - { - const int nw = atom2->nw; - const int mug0 = iw / npol_; - const int nug0 = iw2 / npol_; - const int iw_nowg = mug0 * nw + nug0; - - double temp_value = p_pvdpRx[iw_nowg]; - if (std::abs(temp_value) > sparse_thr) - { - pvdpRx_sparse[dR][start1 + iw][start2 + iw2] = temp_value; - } - temp_value = p_pvdpRy[iw_nowg]; - if (std::abs(temp_value) > sparse_thr) - { - pvdpRy_sparse[dR][start1 + iw][start2 + iw2] = temp_value; - } - temp_value = p_pvdpRz[iw_nowg]; - if (std::abs(temp_value) > sparse_thr) - { - pvdpRz_sparse[dR][start1 + iw][start2 + iw2] = temp_value; - } - } - } - } - } - distribute_pvdpR_sparse(cspin, 0, nlocal, sparse_thr, pvdpRx_sparse, pv, hs_arrays); - distribute_pvdpR_sparse(cspin, 1, nlocal, sparse_thr, pvdpRy_sparse, pv, hs_arrays); - distribute_pvdpR_sparse(cspin, 2, nlocal, sparse_thr, pvdpRz_sparse, pv, hs_arrays); - ModuleBase::timer::end("Gint", "cal_dvlocal_R_sparse"); -} - - -void Gint_dvlocal::distribute_pvdpR_sparse( - const int cspin, - const int dim, - const int nlocal, - const double sparse_thr, - const std::map, - std::map>>& - pvdpR_sparse, - const Parallel_Orbitals& pv, - LCAO_HS_Arrays& hs_arrays) -{ - int total_R_num = hs_arrays.all_R_coor.size(); - std::vector nonzero_num(total_R_num); - std::vector minus_nonzero_num(total_R_num); - int count = 0; - for (const auto& R_coor: hs_arrays.all_R_coor) - { - auto iter = pvdpR_sparse.find(R_coor); - if (iter != pvdpR_sparse.end()) - { - for (auto& row_loop: iter->second) - { - nonzero_num[count] += row_loop.second.size(); - } - } - - auto minus_R_coor = -1 * R_coor; - - iter = pvdpR_sparse.find(minus_R_coor); - if (iter != pvdpR_sparse.end()) - { - for (auto& row_loop: iter->second) - { - minus_nonzero_num[count] += row_loop.second.size(); - } - } - count++; - } - - Parallel_Reduce::reduce_all(nonzero_num.data(), total_R_num); - Parallel_Reduce::reduce_all(minus_nonzero_num.data(), total_R_num); - - std::vector tmp(nlocal); - count = 0; - - const std::vector& trace_lo = gint_info_->get_trace_lo(); - for (const auto& R_coor: hs_arrays.all_R_coor) - { - if (nonzero_num[count] != 0 || minus_nonzero_num[count] != 0) - { - auto minus_R_coor = -1 * R_coor; - - for (int row = 0; row < nlocal; ++row) - { - tmp.assign(tmp.size(), 0); - - auto iter = pvdpR_sparse.find(R_coor); - if (iter != pvdpR_sparse.end()) - { - - if (trace_lo[row] >= 0) - { - auto row_iter = iter->second.find(row); - if (row_iter != iter->second.end()) - { - for (auto& value: row_iter->second) - { - tmp[value.first] = value.second; - } - } - } - } - - auto minus_R_iter = pvdpR_sparse.find(minus_R_coor); - if (minus_R_iter != pvdpR_sparse.end()) - { - for (int col = 0; col < row; ++col) - { - if (trace_lo[col] >= 0) - { - auto row_iter = minus_R_iter->second.find(col); - if (row_iter != minus_R_iter->second.end()) - { - auto col_iter = row_iter->second.find(row); - if (col_iter != row_iter->second.end()) - { - tmp[col] = col_iter->second; - } - } - } - } - } - - Parallel_Reduce::reduce_pool(tmp.data(), nlocal); - - if (pv.global2local_row(row) >= 0) - { - for (int col = 0; col < nlocal; ++col) - { - if (pv.global2local_col(col) >= 0) - { - if (std::abs(tmp[col]) > sparse_thr) - { - if (dim == 0) - { - double& value = hs_arrays.dHRx_sparse[cspin][R_coor][row][col]; - value += tmp[col]; - if (std::abs(value) <= sparse_thr) - { - hs_arrays.dHRx_sparse[cspin][R_coor][row].erase(col); - } - } - if (dim == 1) - { - double& value = hs_arrays.dHRy_sparse[cspin][R_coor][row][col]; - value += tmp[col]; - if (std::abs(value) <= sparse_thr) - { - hs_arrays.dHRy_sparse[cspin][R_coor][row].erase(col); - } - } - if (dim == 2) - { - double& value = hs_arrays.dHRz_sparse[cspin][R_coor][row][col]; - value += tmp[col]; - if (std::abs(value) <= sparse_thr) - { - hs_arrays.dHRz_sparse[cspin][R_coor][row].erase(col); - } - } - } - } - } - } - } - } - count++; - } } -} \ No newline at end of file diff --git a/source/source_hamilt/module_gint/gint_dvlocal.h b/source/source_hamilt/module_gint/gint_dvlocal.h index 26027b88e16..ee31f05f297 100644 --- a/source/source_hamilt/module_gint/gint_dvlocal.h +++ b/source/source_hamilt/module_gint/gint_dvlocal.h @@ -4,8 +4,6 @@ #include #include #include "source_hamilt/module_hcontainer/hcontainer.h" -#include "source_lcao/lcao_hs_arrays.h" -#include "source_lcao/module_ri/abfs_vector3_order.h" #include "gint.h" #include "gint_info.h" @@ -28,16 +26,6 @@ class Gint_dvlocal : public Gint void cal_dvlocal(); - void cal_dvlocal_R_sparse( - const int nspin, - const int cspin, - const int nlocal, - const double sparse_thr, - const Parallel_Orbitals& pv, - const UnitCell& ucell, - const Grid_Driver& gdriver, - LCAO_HS_Arrays& hs_arrays); - HContainer* get_pvdpRx() { return &pvdpRx; } HContainer* get_pvdpRy() { return &pvdpRy; } HContainer* get_pvdpRz() { return &pvdpRz; } @@ -47,17 +35,6 @@ class Gint_dvlocal : public Gint void cal_hr_gint_(); - void distribute_pvdpR_sparse( - const int cspin, - const int dim, - const int nlocal, - const double sparse_thr, - const std::map, - std::map>>& - pvdpR_sparse, - const Parallel_Orbitals& pv, - LCAO_HS_Arrays& HS_Arrays); - // input const double* vr_eff_ = nullptr; int nspin_; diff --git a/source/source_hamilt/module_gint/gint_interface.cpp b/source/source_hamilt/module_gint/gint_interface.cpp index bda93a17e21..e015690b330 100644 --- a/source/source_hamilt/module_gint/gint_interface.cpp +++ b/source/source_hamilt/module_gint/gint_interface.cpp @@ -9,7 +9,6 @@ #include "gint_rho.h" #include "gint_drho.h" #include "gint_tau.h" -#include "gint_dvlocal.h" #ifdef __CUDA #include "gint_vl_gpu.h" @@ -191,23 +190,4 @@ void cal_gint_fvl_meta( } } -void cal_dvlocal_R_sparse( - const int nspin, - const int npol, - const int current_spin, - const int nlocal, - const double sparse_thr, - const double* vr_eff, - const Parallel_Orbitals& pv, - const UnitCell& ucell, - const Grid_Driver& gdriver, - LCAO_HS_Arrays& hs_arrays) -{ - Gint_dvlocal gint_dvlocal(vr_eff, nspin, npol); - gint_dvlocal.cal_dvlocal(); - gint_dvlocal.cal_dvlocal_R_sparse( - nspin, current_spin, nlocal, sparse_thr, - pv, ucell, gdriver, hs_arrays); -} - } // namespace ModuleGint diff --git a/source/source_hamilt/module_gint/gint_interface.h b/source/source_hamilt/module_gint/gint_interface.h index c81605d6e18..6fae46fbc59 100644 --- a/source/source_hamilt/module_gint/gint_interface.h +++ b/source/source_hamilt/module_gint/gint_interface.h @@ -8,7 +8,6 @@ class Parallel_Orbitals; class UnitCell; class Grid_Driver; -class LCAO_HS_Arrays; namespace ModuleGint { @@ -71,19 +70,6 @@ void cal_gint_fvl_meta( ModuleBase::matrix* fvl, ModuleBase::matrix* svl); -void cal_dvlocal_R_sparse( - const int nspin, - const int npol, - const int current_spin, - const int nlocal, - const double sparse_thr, - const double* vr_eff, - const Parallel_Orbitals& pv, - const UnitCell& ucell, - const Grid_Driver& gdriver, - LCAO_HS_Arrays& hs_arrays); - - } // namespace ModuleGint #endif // ABACUS_SOURCE_HAMILT_MODULE_GINT_GINT_INTERFACE_H diff --git a/source/source_hamilt/module_vdw/vdwd2_parameters.h b/source/source_hamilt/module_vdw/vdwd2_parameters.h index 87d93273337..dc19895b6af 100644 --- a/source/source_hamilt/module_vdw/vdwd2_parameters.h +++ b/source/source_hamilt/module_vdw/vdwd2_parameters.h @@ -8,7 +8,7 @@ #define VDWD2_PARAMETERS_H #include "source_cell/unitcell.h" -#include "source_io/module_parameter/parameter.h" +#include "source_io/module_parameter/input_parameter.h" #include "vdw_parameters.h" #include diff --git a/source/source_hamilt/module_xc/kernels/xc_functional_op.h b/source/source_hamilt/module_xc/kernels/xc_functional_op.h index 1cdb65f0871..a8c7304a1b2 100644 --- a/source/source_hamilt/module_xc/kernels/xc_functional_op.h +++ b/source/source_hamilt/module_xc/kernels/xc_functional_op.h @@ -2,8 +2,8 @@ #define source_hamilt_MODULE_XC_KERNELS_H_ #include -#include #include +#include namespace hamilt { diff --git a/source/source_lcao/spar_dh.cpp b/source/source_lcao/spar_dh.cpp index e14e6c6ad4d..8769d001ab8 100644 --- a/source/source_lcao/spar_dh.cpp +++ b/source/source_lcao/spar_dh.cpp @@ -1,10 +1,234 @@ #include "spar_dh.h" +#include "source_base/parallel_reduce.h" +#include "source_base/timer.h" #include "source_io/module_parameter/parameter.h" #include "source_lcao/lcao_domain.h" -#include "source_hamilt/module_gint/gint_interface.h" +#include "source_hamilt/module_gint/gint_dvlocal.h" + +#include +#include #include +namespace +{ + +using SparseMatrixR = std::map, std::map>>; + +// Adds one Cartesian component of the grid-integrated to dH{x,y,z}R_sparse, +// completing the lower triangle from -R and reducing over the pool. +void distribute_pvdpR_sparse(const int cspin, + const int dim, + const int nlocal, + const double sparse_thr, + const SparseMatrixR& pvdpR_sparse, + const Parallel_Orbitals& pv, + LCAO_HS_Arrays& hs_arrays) +{ + int total_R_num = hs_arrays.all_R_coor.size(); + std::vector nonzero_num(total_R_num); + std::vector minus_nonzero_num(total_R_num); + int count = 0; + for (const auto& R_coor: hs_arrays.all_R_coor) + { + auto iter = pvdpR_sparse.find(R_coor); + if (iter != pvdpR_sparse.end()) + { + for (auto& row_loop: iter->second) + { + nonzero_num[count] += row_loop.second.size(); + } + } + + auto minus_R_coor = -1 * R_coor; + + iter = pvdpR_sparse.find(minus_R_coor); + if (iter != pvdpR_sparse.end()) + { + for (auto& row_loop: iter->second) + { + minus_nonzero_num[count] += row_loop.second.size(); + } + } + count++; + } + + Parallel_Reduce::reduce_all(nonzero_num.data(), total_R_num); + Parallel_Reduce::reduce_all(minus_nonzero_num.data(), total_R_num); + + std::vector tmp(nlocal); + count = 0; + + const std::vector& trace_lo = ModuleGint::Gint::get_gint_info().get_trace_lo(); + for (const auto& R_coor: hs_arrays.all_R_coor) + { + if (nonzero_num[count] != 0 || minus_nonzero_num[count] != 0) + { + auto minus_R_coor = -1 * R_coor; + + for (int row = 0; row < nlocal; ++row) + { + tmp.assign(tmp.size(), 0); + + auto iter = pvdpR_sparse.find(R_coor); + if (iter != pvdpR_sparse.end()) + { + + if (trace_lo[row] >= 0) + { + auto row_iter = iter->second.find(row); + if (row_iter != iter->second.end()) + { + for (auto& value: row_iter->second) + { + tmp[value.first] = value.second; + } + } + } + } + + auto minus_R_iter = pvdpR_sparse.find(minus_R_coor); + if (minus_R_iter != pvdpR_sparse.end()) + { + for (int col = 0; col < row; ++col) + { + if (trace_lo[col] >= 0) + { + auto row_iter = minus_R_iter->second.find(col); + if (row_iter != minus_R_iter->second.end()) + { + auto col_iter = row_iter->second.find(row); + if (col_iter != row_iter->second.end()) + { + tmp[col] = col_iter->second; + } + } + } + } + } + + Parallel_Reduce::reduce_pool(tmp.data(), nlocal); + + if (pv.global2local_row(row) >= 0) + { + for (int col = 0; col < nlocal; ++col) + { + if (pv.global2local_col(col) >= 0) + { + if (std::abs(tmp[col]) > sparse_thr) + { + if (dim == 0) + { + double& value = hs_arrays.dHRx_sparse[cspin][R_coor][row][col]; + value += tmp[col]; + if (std::abs(value) <= sparse_thr) + { + hs_arrays.dHRx_sparse[cspin][R_coor][row].erase(col); + } + } + if (dim == 1) + { + double& value = hs_arrays.dHRy_sparse[cspin][R_coor][row][col]; + value += tmp[col]; + if (std::abs(value) <= sparse_thr) + { + hs_arrays.dHRy_sparse[cspin][R_coor][row].erase(col); + } + } + if (dim == 2) + { + double& value = hs_arrays.dHRz_sparse[cspin][R_coor][row][col]; + value += tmp[col]; + if (std::abs(value) <= sparse_thr) + { + hs_arrays.dHRz_sparse[cspin][R_coor][row].erase(col); + } + } + } + } + } + } + } + } + count++; + } +} + +// Converts the grid-integrated local-potential derivative , held per atom pair in +// pvdpR{x,y,z}, to the global-index sparse format of LCAO_HS_Arrays. +void cal_dvlocal_R_sparse(const hamilt::HContainer& pvdpRx, + const hamilt::HContainer& pvdpRy, + const hamilt::HContainer& pvdpRz, + const int npol, + const int cspin, + const int nlocal, + const double sparse_thr, + const Parallel_Orbitals& pv, + const UnitCell& ucell, + LCAO_HS_Arrays& hs_arrays) +{ + ModuleBase::TITLE("Gint", "cal_dvlocal_R_sparse"); + ModuleBase::timer::start("Gint", "cal_dvlocal_R_sparse"); + SparseMatrixR pvdpRx_sparse; + SparseMatrixR pvdpRy_sparse; + SparseMatrixR pvdpRz_sparse; + + for (int iap = 0; iap < pvdpRx.size_atom_pairs(); iap++) + { + const auto& ap = pvdpRx.get_atom_pair(iap); + const int iat1 = ap.get_atom_i(); + const int iat2 = ap.get_atom_j(); + const int it1 = ucell.iat2it[iat1]; + const int it2 = ucell.iat2it[iat2]; + const Atom* atom1 = &ucell.atoms[it1]; + const Atom* atom2 = &ucell.atoms[it2]; + const int start1 = ucell.itiaiw2iwt(it1, ucell.iat2ia[iat1], 0); + const int start2 = ucell.itiaiw2iwt(it2, ucell.iat2ia[iat2], 0); + + for (int ir = 0; ir < ap.get_R_size(); ir++) + { + const ModuleBase::Vector3 R = ap.get_R_index(ir); + Abfs::Vector3_Order dR(R.x, R.y, R.z); + double* p_pvdpRx = pvdpRx.get_atom_pair(iap).get_pointer(ir); + double* p_pvdpRy = pvdpRy.get_atom_pair(iap).get_pointer(ir); + double* p_pvdpRz = pvdpRz.get_atom_pair(iap).get_pointer(ir); + + for (int iw = 0; iw < atom1->nw * npol; iw++) + { + for (int iw2 = 0; iw2 < atom2->nw * npol; iw2++) + { + const int nw = atom2->nw; + const int mug0 = iw / npol; + const int nug0 = iw2 / npol; + const int iw_nowg = mug0 * nw + nug0; + + double temp_value = p_pvdpRx[iw_nowg]; + if (std::abs(temp_value) > sparse_thr) + { + pvdpRx_sparse[dR][start1 + iw][start2 + iw2] = temp_value; + } + temp_value = p_pvdpRy[iw_nowg]; + if (std::abs(temp_value) > sparse_thr) + { + pvdpRy_sparse[dR][start1 + iw][start2 + iw2] = temp_value; + } + temp_value = p_pvdpRz[iw_nowg]; + if (std::abs(temp_value) > sparse_thr) + { + pvdpRz_sparse[dR][start1 + iw][start2 + iw2] = temp_value; + } + } + } + } + } + distribute_pvdpR_sparse(cspin, 0, nlocal, sparse_thr, pvdpRx_sparse, pv, hs_arrays); + distribute_pvdpR_sparse(cspin, 1, nlocal, sparse_thr, pvdpRy_sparse, pv, hs_arrays); + distribute_pvdpR_sparse(cspin, 2, nlocal, sparse_thr, pvdpRz_sparse, pv, hs_arrays); + ModuleBase::timer::end("Gint", "cal_dvlocal_R_sparse"); +} + +} // namespace + void sparse_format::cal_dS(const UnitCell& ucell, const Parallel_Orbitals& pv, LCAO_HS_Arrays& HS_Arrays, @@ -100,9 +324,18 @@ void sparse_format::cal_dH(const UnitCell& ucell, = v_eff.nc * v_eff.nr > 0 ? &(v_eff(current_spin, 0)) : nullptr; if (!gamma_only_local) { - ModuleGint::cal_dvlocal_R_sparse( - nspin, npol, current_spin, PARAM.globalv.nlocal, - sparse_thr, vr_eff1, pv, ucell, grid, HS_Arrays); + ModuleGint::Gint_dvlocal gint_dvlocal(vr_eff1, nspin, npol); + gint_dvlocal.cal_dvlocal(); + cal_dvlocal_R_sparse(*gint_dvlocal.get_pvdpRx(), + *gint_dvlocal.get_pvdpRy(), + *gint_dvlocal.get_pvdpRz(), + npol, + current_spin, + PARAM.globalv.nlocal, + sparse_thr, + pv, + ucell, + HS_Arrays); } } return; diff --git a/source/source_pw/module_pwdft/op_pw_exx.h b/source/source_pw/module_pwdft/op_pw_exx.h index 2715a497161..646a14cca15 100644 --- a/source/source_pw/module_pwdft/op_pw_exx.h +++ b/source/source_pw/module_pwdft/op_pw_exx.h @@ -8,7 +8,7 @@ #include "source_basis/module_pw/pw_basis.h" #include "source_basis/module_pw/pw_basis_k.h" #include "source_cell/klist.h" -#include "source_lcao/module_ri/conv_coulomb_pot_k.h" +#include "source_hamilt/module_xc/coulomb_config.h" #include "source_psi/psi.h" #include "source_base/module_container/ATen/kernels/lapack.h"