From bce3cf2595a61bd6b5ad6992fc88ce315799202a Mon Sep 17 00:00:00 2001 From: ErjieWu Date: Wed, 26 Mar 2025 16:42:45 +0800 Subject: [PATCH 1/4] Add HR precalc functions for DeePKS and fix some bugs. --- source/Makefile.Objects | 1 + .../module_deepks/CMakeLists.txt | 1 + .../module_deepks/LCAO_deepks.h | 1 + .../module_deepks/LCAO_deepks_interface.cpp | 40 ++----- .../module_deepks/LCAO_deepks_io.cpp | 14 ++- .../module_deepks/deepks_vdpre.cpp | 10 +- .../module_deepks/deepks_vdrpre.cpp | 104 ++++++++++++++++++ .../module_deepks/deepks_vdrpre.h | 45 ++++++++ 8 files changed, 179 insertions(+), 37 deletions(-) create mode 100644 source/module_hamilt_lcao/module_deepks/deepks_vdrpre.cpp create mode 100644 source/module_hamilt_lcao/module_deepks/deepks_vdrpre.h diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 1f450083a79..349cb1b6c03 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -208,6 +208,7 @@ OBJS_DEEPKS=LCAO_deepks.o\ deepks_orbpre.o\ deepks_vdelta.o\ deepks_vdpre.o\ + deepks_vdrpre.o\ deepks_hmat.o\ deepks_pdm.o\ deepks_phialpha.o\ diff --git a/source/module_hamilt_lcao/module_deepks/CMakeLists.txt b/source/module_hamilt_lcao/module_deepks/CMakeLists.txt index d59684d397b..50eb5324b0a 100644 --- a/source/module_hamilt_lcao/module_deepks/CMakeLists.txt +++ b/source/module_hamilt_lcao/module_deepks/CMakeLists.txt @@ -11,6 +11,7 @@ if(ENABLE_DEEPKS) deepks_orbpre.cpp deepks_vdelta.cpp deepks_vdpre.cpp + deepks_vdrpre.cpp deepks_hmat.cpp deepks_pdm.cpp deepks_phialpha.cpp diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks.h b/source/module_hamilt_lcao/module_deepks/LCAO_deepks.h index e3058a15a76..c1104a9b7df 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks.h +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks.h @@ -15,6 +15,7 @@ #include "deepks_spre.h" #include "deepks_vdelta.h" #include "deepks_vdpre.h" +#include "deepks_vdrpre.h" #include "module_base/complexmatrix.h" #include "module_base/intarray.h" #include "module_base/matrix.h" diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp index 3d114354e3c..6a3626712ce 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp @@ -314,34 +314,18 @@ void LCAO_Deepks_Interface::out_deepks_labels(const double& etot, ofs_hr.close(); } - const std::string file_vdrpre = PARAM.globalv.global_out_dir + "deepks_vdrpre.csr"; - std::vector*> h_deltaR_pre(inlmax); - for (int i = 0; i < inlmax; i++) - { - h_deltaR_pre[i] = new hamilt::HContainer(*hR_tot); - h_deltaR_pre[i]->set_zero(); - } - // DeePKS_domain::cal_vdr_precalc(); - if (rank == 0) - { - std::ofstream ofs_hrp(file_vdrpre, std::ios::out); - for (int iat = 0; iat < nat; iat++) - { - ofs_hrp << "- Index of atom: " << iat << std::endl; - for (int nl = 0; nl < nlmax; nl++) - { - int inl = iat * nlmax + nl; - ofs_hrp << "-- Index of nl: " << nl << std::endl; - ofs_hrp << "Matrix Dimension of H_delta(R): " << h_deltaR_pre[inl]->get_nbasis() << std::endl; - ofs_hrp << "Matrix number of H_delta(R): " << h_deltaR_pre[inl]->size_R_loop() << std::endl; - hamilt::Output_HContainer out_hrp(h_deltaR_pre[inl], ofs_hrp, sparse_threshold, precision); - out_hrp.write(); - ofs_hrp << std::endl; - } - ofs_hrp << std::endl; - } - ofs_hrp.close(); - } + torch::Tensor phialpha_r_out; + torch::Tensor R_query; + DeePKS_domain::prepare_phialpha_r(nlocal, lmaxd, inlmax, nat, phialpha, ucell, orb, *ParaV, GridD, phialpha_r_out, R_query); + const std::string file_phialpha_r = PARAM.globalv.global_out_dir + "deepks_phialpha_r.npy"; + const std::string file_R_query = PARAM.globalv.global_out_dir + "deepks_R_query.npy"; + LCAO_deepks_io::save_tensor2npy(file_phialpha_r, phialpha_r_out, rank); + LCAO_deepks_io::save_tensor2npy(file_R_query, R_query, rank); + + torch::Tensor gevdm_out; + DeePKS_domain::prepare_gevdm(nat, lmaxd, inlmax, orb, gevdm, gevdm_out); + const std::string file_gevdm = PARAM.globalv.global_out_dir + "deepks_gevdm.npy"; + LCAO_deepks_io::save_tensor2npy(file_gevdm, gevdm_out, rank); } } diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_io.cpp b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_io.cpp index c58672b1806..2be3a943f8c 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_io.cpp +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_io.cpp @@ -275,11 +275,7 @@ void LCAO_deepks_io::save_tensor2npy(const std::string& file_name, const torch:: std::vector data(tensor.numel()); - if constexpr (std::is_same::value) - { - std::memcpy(data.data(), tensor.data_ptr(), tensor.numel() * sizeof(double)); - } - else + if constexpr (std::is_same>::value) { auto tensor_data = tensor.data_ptr>(); for (size_t i = 0; i < tensor.numel(); ++i) @@ -287,6 +283,10 @@ void LCAO_deepks_io::save_tensor2npy(const std::string& file_name, const torch:: data[i] = std::complex(tensor_data[i].real(), tensor_data[i].imag()); } } + else + { + std::memcpy(data.data(), tensor.data_ptr(), tensor.numel() * sizeof(T)); + } npy::SaveArrayAsNumpy(file_name, false, shape.size(), shape.data(), data); } @@ -313,6 +313,10 @@ template void LCAO_deepks_io::save_npy_h>(const std::vector const int nks, const int rank); +template void LCAO_deepks_io::save_tensor2npy(const std::string& file_name, + const torch::Tensor& tensor, + const int rank); + template void LCAO_deepks_io::save_tensor2npy(const std::string& file_name, const torch::Tensor& tensor, const int rank); diff --git a/source/module_hamilt_lcao/module_deepks/deepks_vdpre.cpp b/source/module_hamilt_lcao/module_deepks/deepks_vdpre.cpp index 36108b3a912..6189c8acf6b 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_vdpre.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_vdpre.cpp @@ -220,8 +220,8 @@ void DeePKS_domain::cal_v_delta_precalc(const int nlocal, std::vector v_delta_precalc_vector; for (int nl = 0; nl < nlmax; ++nl) { - torch::Tensor gevdm_complex = gevdm[nl].to(dtype); - v_delta_precalc_vector.push_back(at::einsum("kxyamn, avmn->kxyav", {v_delta_pdm_vector[nl], gevdm[nl]})); + torch::Tensor gevdm_totype = gevdm[nl].to(dtype); + v_delta_precalc_vector.push_back(at::einsum("kxyamn, avmn->kxyav", {v_delta_pdm_vector[nl], gevdm_totype})); } v_delta_precalc = torch::cat(v_delta_precalc_vector, -1); @@ -296,6 +296,8 @@ void DeePKS_domain::prepare_phialpha(const int nlocal, int nlmax = inlmax / nat; int mmax = 2 * lmaxd + 1; phialpha_out = torch::zeros({nat, nlmax, nks, nlocal, mmax}, dtype); + auto accessor + = phialpha_out.accessor::value, double, c10::complex>, 5>(); DeePKS_domain::iterate_ad1( ucell, @@ -348,13 +350,13 @@ void DeePKS_domain::prepare_phialpha(const int nlocal, { if constexpr (std::is_same::value) { - phialpha_out[iat][nl][ik][iw1_all][m1] = overlap->get_value(iw1, ib + m1); + accessor[iat][nl][ik][iw1_all][m1] = overlap->get_value(iw1, ib + m1); } else { c10::complex tmp; tmp = overlap->get_value(iw1, ib + m1) * kphase; - phialpha_out.index_put_({iat, nl, ik, iw1_all, m1}, tmp); + accessor[iat][nl][ik][iw1_all][m1] += tmp; } } ib += nm; diff --git a/source/module_hamilt_lcao/module_deepks/deepks_vdrpre.cpp b/source/module_hamilt_lcao/module_deepks/deepks_vdrpre.cpp new file mode 100644 index 00000000000..6ee15925c05 --- /dev/null +++ b/source/module_hamilt_lcao/module_deepks/deepks_vdrpre.cpp @@ -0,0 +1,104 @@ +// prepare_phialpha_r : prepare phialpha_r for outputting npy file + +#ifdef __DEEPKS + +#include "deepks_vdrpre.h" + +#include "LCAO_deepks_io.h" // mohan add 2024-07-22 +#include "deepks_iterate.h" +#include "module_base/blas_connector.h" +#include "module_base/constants.h" +#include "module_base/libm/libm.h" +#include "module_base/parallel_reduce.h" +#include "module_hamilt_lcao/module_hcontainer/atom_pair.h" +#include "module_parameter/parameter.h" + +void DeePKS_domain::prepare_phialpha_r(const int nlocal, + const int lmaxd, + const int inlmax, + const int nat, + const std::vector*> phialpha, + const UnitCell& ucell, + const LCAO_Orbitals& orb, + const Parallel_Orbitals& pv, + const Grid_Driver& GridD, + torch::Tensor& phialpha_r_out, + torch::Tensor& R_query) +{ + ModuleBase::TITLE("DeePKS_domain", "prepare_phialpha_r"); + ModuleBase::timer::tick("DeePKS_domain", "prepare_phialpha_r"); + constexpr torch::Dtype dtype = torch::kFloat64; + int nlmax = inlmax / nat; + int mmax = 2 * lmaxd + 1; + auto size_R = static_cast(phialpha[0]->size_R_loop()); + phialpha_r_out = torch::zeros({size_R, nat, nlmax, nlocal, mmax}, dtype); + R_query = torch::zeros({size_R, 3}, torch::kInt32); + auto accessor = phialpha_r_out.accessor(); + auto R_accessor = R_query.accessor(); + + for (int iR = 0; iR < size_R; ++iR) + { + phialpha[0]->loop_R(iR, R_accessor[iR][0], R_accessor[iR][1], R_accessor[iR][2]); + } + + DeePKS_domain::iterate_ad1( + ucell, + GridD, + orb, + false, // no trace_alpha + [&](const int iat, + const ModuleBase::Vector3& tau0, + const int ibt, + const ModuleBase::Vector3& tau, + const int start, + const int nw_tot, + ModuleBase::Vector3 dR) + { + if (phialpha[0]->find_matrix(iat, ibt, dR.x, dR.y, dR.z) == nullptr) + { + return; // to next loop + } + + // middle loop : all atomic basis on the adjacent atom ad + for (int iw1 = 0; iw1 < nw_tot; ++iw1) + { + const int iw1_all = start + iw1; + const int iw1_local = pv.global2local_row(iw1_all); + const int iw2_local = pv.global2local_col(iw1_all); + if (iw1_local < 0 || iw2_local < 0) + { + continue; + } + hamilt::BaseMatrix* overlap = phialpha[0]->find_matrix(iat, ibt, dR); + const int iR = phialpha[0]->find_R(dR); + + int ib = 0; + int nl = 0; + for (int L0 = 0; L0 <= orb.Alpha[0].getLmax(); ++L0) + { + for (int N0 = 0; N0 < orb.Alpha[0].getNchi(L0); ++N0) + { + const int nm = 2 * L0 + 1; + for (int m1 = 0; m1 < nm; ++m1) // nm = 1 for s, 3 for p, 5 for d + { + accessor[iR][iat][nl][iw1_all][m1] += overlap->get_value(iw1, ib + m1); + } + ib += nm; + nl++; + } + } + } // end iw + } + ); + +#ifdef __MPI + int size = size_R * nat * nlmax * nlocal * mmax; + double* data_ptr = phialpha_r_out.data_ptr(); + Parallel_Reduce::reduce_all(data_ptr, size); + +#endif + + ModuleBase::timer::tick("DeePKS_domain", "prepare_phialpha_r"); + return; +} +#endif diff --git a/source/module_hamilt_lcao/module_deepks/deepks_vdrpre.h b/source/module_hamilt_lcao/module_deepks/deepks_vdrpre.h new file mode 100644 index 00000000000..2ed2a3d5ab9 --- /dev/null +++ b/source/module_hamilt_lcao/module_deepks/deepks_vdrpre.h @@ -0,0 +1,45 @@ +#ifndef DEEPKS_VDRPRE_H +#define DEEPKS_VDRPRE_H + +#ifdef __DEEPKS + +#include "module_base/complexmatrix.h" +#include "module_base/intarray.h" +#include "module_base/matrix.h" +#include "module_base/timer.h" +#include "module_basis/module_ao/parallel_orbitals.h" +#include "module_basis/module_nao/two_center_integrator.h" +#include "module_cell/module_neighbor/sltk_grid_driver.h" +#include "module_hamilt_lcao/module_hcontainer/hcontainer.h" + +#include +#include + +namespace DeePKS_domain +{ +//------------------------ +// deepks_vdrpre.cpp +//------------------------ + +// This file contains 1 subroutine for calculating v_delta, +// cal_vdr_precalc : v_delta_r_precalc is used for training with v_delta_r label, +// which equals gevdm * v_delta_pdm, +// v_delta_pdm = overlap * overlap + +// for deepks_v_delta = -1 +// calculates v_delta_r_precalc +void prepare_phialpha_r(const int nlocal, + const int lmaxd, + const int inlmax, + const int nat, + const std::vector*> phialpha, + const UnitCell& ucell, + const LCAO_Orbitals& orb, + const Parallel_Orbitals& pv, + const Grid_Driver& GridD, + torch::Tensor& phialpha_r_out, + torch::Tensor& R_query); + +} // namespace DeePKS_domain +#endif +#endif \ No newline at end of file From 8fccc4e721bf412d3e6021287e7de0a9bf94421c Mon Sep 17 00:00:00 2001 From: ErjieWu Date: Thu, 27 Mar 2025 15:16:22 +0800 Subject: [PATCH 2/4] Remove and combine some files in DeePKS. --- source/Makefile.Objects | 1 - .../module_deepks/CMakeLists.txt | 1 - .../module_deepks/LCAO_deepks.h | 5 +- .../module_deepks/deepks_hmat.cpp | 117 ------------------ .../module_deepks/deepks_hmat.h | 31 ----- .../module_deepks/deepks_orbital.cpp | 81 ++++++++++++ .../module_deepks/deepks_orbital.h | 11 +- .../module_deepks/doxygen/conf_dpks | 9 -- .../module_deepks/sphinx/requirements.txt | 5 - .../module_deepks/sphinx/source/conf.py | 93 -------------- .../module_deepks/sphinx/source/index.rst | 25 ---- 11 files changed, 93 insertions(+), 286 deletions(-) delete mode 100644 source/module_hamilt_lcao/module_deepks/deepks_hmat.cpp delete mode 100644 source/module_hamilt_lcao/module_deepks/deepks_hmat.h delete mode 100644 source/module_hamilt_lcao/module_deepks/doxygen/conf_dpks delete mode 100644 source/module_hamilt_lcao/module_deepks/sphinx/requirements.txt delete mode 100644 source/module_hamilt_lcao/module_deepks/sphinx/source/conf.py delete mode 100644 source/module_hamilt_lcao/module_deepks/sphinx/source/index.rst diff --git a/source/Makefile.Objects b/source/Makefile.Objects index fc5a939a7f6..f3fdd997255 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -209,7 +209,6 @@ OBJS_DEEPKS=LCAO_deepks.o\ deepks_vdelta.o\ deepks_vdpre.o\ deepks_vdrpre.o\ - deepks_hmat.o\ deepks_pdm.o\ deepks_phialpha.o\ LCAO_deepks_io.o\ diff --git a/source/module_hamilt_lcao/module_deepks/CMakeLists.txt b/source/module_hamilt_lcao/module_deepks/CMakeLists.txt index 50eb5324b0a..3aaee830b44 100644 --- a/source/module_hamilt_lcao/module_deepks/CMakeLists.txt +++ b/source/module_hamilt_lcao/module_deepks/CMakeLists.txt @@ -12,7 +12,6 @@ if(ENABLE_DEEPKS) deepks_vdelta.cpp deepks_vdpre.cpp deepks_vdrpre.cpp - deepks_hmat.cpp deepks_pdm.cpp deepks_phialpha.cpp LCAO_deepks_io.cpp diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks.h b/source/module_hamilt_lcao/module_deepks/LCAO_deepks.h index c1104a9b7df..27eec83ae00 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks.h +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks.h @@ -7,7 +7,6 @@ #include "deepks_descriptor.h" #include "deepks_force.h" #include "deepks_fpre.h" -#include "deepks_hmat.h" #include "deepks_orbital.h" #include "deepks_orbpre.h" #include "deepks_pdm.h" @@ -57,10 +56,10 @@ class LCAO_Deepks public: ///(Unit: Ry) Correction energy provided by NN double E_delta = 0.0; - ///(Unit: Ry) \f$tr(\rho H_\delta), \rho = \sum_i{c_{i, \mu}c_{i,\nu}} \f$ (for gamma_only) + ///(Unit: Ry) \f$tr(\rho H_\delta), \rho = \sum_i{c_{i, \mu}c_{i,\nu}} \f$ double e_delta_band = 0.0; - /// Correction term to the Hamiltonian matrix: \f$\langle\phi|V_\delta|\phi\rangle\f$ (for gamma only) + /// Correction term to the Hamiltonian matrix: \f$\langle\phi|V_\delta|\phi\rangle\f$ /// The first dimension is for k-points V_delta(k) std::vector> V_delta; diff --git a/source/module_hamilt_lcao/module_deepks/deepks_hmat.cpp b/source/module_hamilt_lcao/module_deepks/deepks_hmat.cpp deleted file mode 100644 index 90936e88ccb..00000000000 --- a/source/module_hamilt_lcao/module_deepks/deepks_hmat.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#ifdef __DEEPKS - -#include "LCAO_deepks.h" -#include "module_base/parallel_reduce.h" -#include "module_parameter/parameter.h" - -template -void DeePKS_domain::collect_h_mat(const Parallel_Orbitals& pv, - const std::vector>& h_in, - std::vector& h_out, - const int nlocal, - const int nks) -{ - ModuleBase::TITLE("DeePKS_domain", "collect_h_tot"); - - // construct the total H matrix - for (int k = 0; k < nks; k++) - { -#ifdef __MPI - int ir = 0; - int ic = 0; - for (int i = 0; i < nlocal; i++) - { - std::vector lineH(nlocal - i, TK(0.0)); - - ir = pv.global2local_row(i); - if (ir >= 0) - { - // data collection - for (int j = i; j < nlocal; j++) - { - ic = pv.global2local_col(j); - if (ic >= 0) - { - int iic = 0; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) - { - iic = ir + ic * pv.nrow; - } - else - { - iic = ir * pv.ncol + ic; - } - lineH[j - i] = h_in[k][iic]; - } - } - } - else - { - // do nothing - } - - Parallel_Reduce::reduce_all(lineH.data(), nlocal - i); - - for (int j = i; j < nlocal; j++) - { - h_out[k](i, j) = lineH[j - i]; - h_out[k](j, i) = h_out[k](i, j); // H is a symmetric matrix - } - } -#else - for (int i = 0; i < nlocal; i++) - { - for (int j = i; j < nlocal; j++) - { - h_out[k](i, j) = h_in[k][i * nlocal + j]; - h_out[k](j, i) = h_out[k](i, j); // H is a symmetric matrix - } - } -#endif - } -} - -template -void DeePKS_domain::check_h_mat(const std::vector& H, const std::string& h_file, const int nlocal, const int nks) -{ - std::ofstream ofs(h_file.c_str()); - ofs << std::setprecision(10); - for (int k = 0; k < nks; k++) - { - for (int i = 0; i < nlocal; i++) - { - for (int j = 0; j < nlocal; j++) - { - ofs << H[k](i, j) << " "; - } - ofs << std::endl; - } - ofs << std::endl; - } - ofs.close(); -} - -template void DeePKS_domain::collect_h_mat(const Parallel_Orbitals& pv, - const std::vector>& h_in, - std::vector& h_out, - const int nlocal, - const int nks); - -template void DeePKS_domain::collect_h_mat, ModuleBase::ComplexMatrix>( - const Parallel_Orbitals& pv, - const std::vector>>& h_in, - std::vector& h_out, - const int nlocal, - const int nks); - -template void DeePKS_domain::check_h_mat(const std::vector& H, - const std::string& h_file, - const int nlocal, - const int nks); - -template void DeePKS_domain::check_h_mat(const std::vector& H, - const std::string& h_file, - const int nlocal, - const int nks); - -#endif diff --git a/source/module_hamilt_lcao/module_deepks/deepks_hmat.h b/source/module_hamilt_lcao/module_deepks/deepks_hmat.h deleted file mode 100644 index 98d365e70a7..00000000000 --- a/source/module_hamilt_lcao/module_deepks/deepks_hmat.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef DEEPKS_HMAT_H -#define DEEPKS_HMAT_H - -#ifdef __DEEPKS - -#include "module_base/complexmatrix.h" -#include "module_base/matrix.h" -#include "module_base/timer.h" -#include "module_basis/module_ao/parallel_orbitals.h" - -#include -#include -#include - -namespace DeePKS_domain -{ -// Collect data in h_in to matrix h_out. Note that left lower trianger in h_out is filled -template -void collect_h_mat(const Parallel_Orbitals& pv, - const std::vector>& h_in, - std::vector& h_out, - const int nlocal, - const int nks); - -// write h_mat to file h_file for checking // not used in the code now -template -void check_h_mat(const std::vector& H, const std::string& h_file, const int nlocal, const int nks); -} // namespace DeePKS_domain - -#endif -#endif diff --git a/source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp b/source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp index 8baf298a462..6cbe38d49f1 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp @@ -69,6 +69,73 @@ void DeePKS_domain::cal_o_delta(const std::vector& dm_hl, return; } +template +void DeePKS_domain::collect_h_mat(const Parallel_Orbitals& pv, + const std::vector>& h_in, + std::vector& h_out, + const int nlocal, + const int nks) +{ + ModuleBase::TITLE("DeePKS_domain", "collect_h_tot"); + + // construct the total H matrix + for (int k = 0; k < nks; k++) + { +#ifdef __MPI + int ir = 0; + int ic = 0; + for (int i = 0; i < nlocal; i++) + { + std::vector lineH(nlocal - i, TK(0.0)); + + ir = pv.global2local_row(i); + if (ir >= 0) + { + // data collection + for (int j = i; j < nlocal; j++) + { + ic = pv.global2local_col(j); + if (ic >= 0) + { + int iic = 0; + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) + { + iic = ir + ic * pv.nrow; + } + else + { + iic = ir * pv.ncol + ic; + } + lineH[j - i] = h_in[k][iic]; + } + } + } + else + { + // do nothing + } + + Parallel_Reduce::reduce_all(lineH.data(), nlocal - i); + + for (int j = i; j < nlocal; j++) + { + h_out[k](i, j) = lineH[j - i]; + h_out[k](j, i) = h_out[k](i, j); // H is a symmetric matrix + } + } +#else + for (int i = 0; i < nlocal; i++) + { + for (int j = i; j < nlocal; j++) + { + h_out[k](i, j) = h_in[k][i * nlocal + j]; + h_out[k](j, i) = h_out[k](i, j); // H is a symmetric matrix + } + } +#endif + } +} + template void DeePKS_domain::cal_o_delta(const std::vector& dm_hl, const std::vector>& h_delta, // std::vector& o_delta, @@ -84,4 +151,18 @@ template void DeePKS_domain::cal_o_delta, ModuleBase::Compl const Parallel_Orbitals& pv, const int nks); +template void DeePKS_domain::collect_h_mat( + const Parallel_Orbitals& pv, + const std::vector>& h_in, + std::vector& h_out, + const int nlocal, + const int nks); + +template void DeePKS_domain::collect_h_mat, ModuleBase::ComplexMatrix>( + const Parallel_Orbitals& pv, + const std::vector>>& h_in, + std::vector& h_out, + const int nlocal, + const int nks); + #endif diff --git a/source/module_hamilt_lcao/module_deepks/deepks_orbital.h b/source/module_hamilt_lcao/module_deepks/deepks_orbital.h index 6c60fd1e96a..d300d705eae 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_orbital.h +++ b/source/module_hamilt_lcao/module_deepks/deepks_orbital.h @@ -20,8 +20,9 @@ namespace DeePKS_domain // which is defind as sum_mu,nu rho^{hl}_mu,nu V(D) // where rho^{hl}_mu,nu = C_{L\mu}C_{L\nu} - C_{H\mu}C_{H\nu}, L for LUMO, H for HOMO -// There are 1 subroutines in this file: +// There are 2 subroutines in this file: // 1. cal_o_delta, which is used for O_delta calculation +// 2. collect_h_mat, which collect H(k) data from different processes template void cal_o_delta(const std::vector& dm_hl, @@ -30,6 +31,14 @@ void cal_o_delta(const std::vector& dm_hl, ModuleBase::matrix& o_delta, const Parallel_Orbitals& pv, const int nks); + +// Collect data in h_in to matrix h_out. Note that left lower trianger in h_out is filled +template +void collect_h_mat(const Parallel_Orbitals& pv, + const std::vector>& h_in, + std::vector& h_out, + const int nlocal, + const int nks); } // namespace DeePKS_domain #endif diff --git a/source/module_hamilt_lcao/module_deepks/doxygen/conf_dpks b/source/module_hamilt_lcao/module_deepks/doxygen/conf_dpks deleted file mode 100644 index ffd6a65bf00..00000000000 --- a/source/module_hamilt_lcao/module_deepks/doxygen/conf_dpks +++ /dev/null @@ -1,9 +0,0 @@ -PROJECT_NAME = module_deepks -INPUT = ../../module_hamilt_lcao/hamilt_lcaodft -PROJECT_BRIEF = "DeePKS: Generate descriptors, load a model and calculate energy and force." -OUTPUT_DIRECTORY = . -FILE_PATTERNS = LCAO_descriptor* -EXTRACT_ALL = YES -USE_MATHJAX = YES -GENERATE_XML = YES -ENABLE_PREPROCESSING = NO diff --git a/source/module_hamilt_lcao/module_deepks/sphinx/requirements.txt b/source/module_hamilt_lcao/module_deepks/sphinx/requirements.txt deleted file mode 100644 index 883a91ac7d0..00000000000 --- a/source/module_hamilt_lcao/module_deepks/sphinx/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -sphinx -breathe -exhale -sphinx_rtd_theme -myst_parser \ No newline at end of file diff --git a/source/module_hamilt_lcao/module_deepks/sphinx/source/conf.py b/source/module_hamilt_lcao/module_deepks/sphinx/source/conf.py deleted file mode 100644 index 1db795c35da..00000000000 --- a/source/module_hamilt_lcao/module_deepks/sphinx/source/conf.py +++ /dev/null @@ -1,93 +0,0 @@ -# Configuration file for the Sphinx documentation builder. -# -# This file only contains a selection of the most common options. For a full -# list see the documentation: -# https://www.sphinx-doc.org/en/master/usage/configuration.html - -# -- Path setup -------------------------------------------------------------- - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# -# import os -# import sys -# sys.path.insert(0, os.path.abspath('.')) -import os -import sys -import textwrap -sys.path.append( "/usr/local/lib/python3.9/site-packages/breathe" ) - -# -- Project information ----------------------------------------------------- - -project = 'ABACUS-DeePKS' -copyright = '2021, x' - -# The full version, including alpha/beta/rc tags -release = '0.1' - - -# -- General configuration --------------------------------------------------- - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ 'sphinx.ext.todo', 'breathe', 'exhale', 'sphinx.ext.mathjax', "sphinx_rtd_theme", 'myst_parser' -] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This pattern also affects html_static_path and html_extra_path. -exclude_patterns = [] - - -# -- Options for HTML output ------------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# -html_theme = "sphinx_rtd_theme" - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] - -# -- Options for breathe ------------------------------------------------- - -breathe_projects = { "ABACUS-DeePKS": "../../../doxygen/xml/" } -breathe_default_project = "ABACUS-DeePKS" - -#-- Options for exhale------------------------------------- -# Setup the exhale extension -exhale_args = { - # These arguments are required - "containmentFolder": "./DeePKS_API", - "rootFileName": "library_root.rst", - "rootFileTitle": "DeePKS", - "doxygenStripFromPath": "..", - # Suggested optional arguments - "createTreeView": True, - # TIP: if using the sphinx-bootstrap-theme, you need - "treeViewIsBootstrap": True, - "exhaleExecutesDoxygen": True, - "exhaleDoxygenStdin": textwrap.dedent(''' - PROJECT_NAME = module_deepks - INPUT = ../../../../module_hamilt_lcao/hamilt_lcaodft - PROJECT_BRIEF = "DeePKS: Generate descriptors, load a model and calculate energy and force." - FILE_PATTERNS = LCAO_descriptor* - EXTRACT_ALL = YES - USE_MATHJAX = YES - GENERATE_XML = YES - ENABLE_PREPROCESSING = NO - ''') -} - -# Tell sphinx what the primary language being documented is. -primary_domain = 'cpp' - -# Tell sphinx what the pygments highlight language should be. -highlight_language = 'cpp' diff --git a/source/module_hamilt_lcao/module_deepks/sphinx/source/index.rst b/source/module_hamilt_lcao/module_deepks/sphinx/source/index.rst deleted file mode 100644 index 2c3ef88b603..00000000000 --- a/source/module_hamilt_lcao/module_deepks/sphinx/source/index.rst +++ /dev/null @@ -1,25 +0,0 @@ -.. test_orb documentation master file, created by - sphinx-quickstart on Fri Jun 11 16:54:33 2021. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Welcome to ABACUS-DeePKS's documentation! -==================================================== - -.. toctree:: - :maxdepth: 2 - :caption: Contents: - - - DeePKS_API/library_root - - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` - - From f256cc82051b2846c2b090916266590df284355e Mon Sep 17 00:00:00 2001 From: ErjieWu Date: Thu, 27 Mar 2025 15:28:25 +0800 Subject: [PATCH 3/4] Fix wrong function position. --- .../module_deepks/deepks_orbital.cpp | 81 ------------------- .../module_deepks/deepks_orbital.h | 11 +-- .../module_deepks/deepks_vdelta.cpp | 81 +++++++++++++++++++ .../module_deepks/deepks_vdelta.h | 13 ++- 4 files changed, 94 insertions(+), 92 deletions(-) diff --git a/source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp b/source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp index 6cbe38d49f1..8baf298a462 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp @@ -69,73 +69,6 @@ void DeePKS_domain::cal_o_delta(const std::vector& dm_hl, return; } -template -void DeePKS_domain::collect_h_mat(const Parallel_Orbitals& pv, - const std::vector>& h_in, - std::vector& h_out, - const int nlocal, - const int nks) -{ - ModuleBase::TITLE("DeePKS_domain", "collect_h_tot"); - - // construct the total H matrix - for (int k = 0; k < nks; k++) - { -#ifdef __MPI - int ir = 0; - int ic = 0; - for (int i = 0; i < nlocal; i++) - { - std::vector lineH(nlocal - i, TK(0.0)); - - ir = pv.global2local_row(i); - if (ir >= 0) - { - // data collection - for (int j = i; j < nlocal; j++) - { - ic = pv.global2local_col(j); - if (ic >= 0) - { - int iic = 0; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) - { - iic = ir + ic * pv.nrow; - } - else - { - iic = ir * pv.ncol + ic; - } - lineH[j - i] = h_in[k][iic]; - } - } - } - else - { - // do nothing - } - - Parallel_Reduce::reduce_all(lineH.data(), nlocal - i); - - for (int j = i; j < nlocal; j++) - { - h_out[k](i, j) = lineH[j - i]; - h_out[k](j, i) = h_out[k](i, j); // H is a symmetric matrix - } - } -#else - for (int i = 0; i < nlocal; i++) - { - for (int j = i; j < nlocal; j++) - { - h_out[k](i, j) = h_in[k][i * nlocal + j]; - h_out[k](j, i) = h_out[k](i, j); // H is a symmetric matrix - } - } -#endif - } -} - template void DeePKS_domain::cal_o_delta(const std::vector& dm_hl, const std::vector>& h_delta, // std::vector& o_delta, @@ -151,18 +84,4 @@ template void DeePKS_domain::cal_o_delta, ModuleBase::Compl const Parallel_Orbitals& pv, const int nks); -template void DeePKS_domain::collect_h_mat( - const Parallel_Orbitals& pv, - const std::vector>& h_in, - std::vector& h_out, - const int nlocal, - const int nks); - -template void DeePKS_domain::collect_h_mat, ModuleBase::ComplexMatrix>( - const Parallel_Orbitals& pv, - const std::vector>>& h_in, - std::vector& h_out, - const int nlocal, - const int nks); - #endif diff --git a/source/module_hamilt_lcao/module_deepks/deepks_orbital.h b/source/module_hamilt_lcao/module_deepks/deepks_orbital.h index d300d705eae..6c60fd1e96a 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_orbital.h +++ b/source/module_hamilt_lcao/module_deepks/deepks_orbital.h @@ -20,9 +20,8 @@ namespace DeePKS_domain // which is defind as sum_mu,nu rho^{hl}_mu,nu V(D) // where rho^{hl}_mu,nu = C_{L\mu}C_{L\nu} - C_{H\mu}C_{H\nu}, L for LUMO, H for HOMO -// There are 2 subroutines in this file: +// There are 1 subroutines in this file: // 1. cal_o_delta, which is used for O_delta calculation -// 2. collect_h_mat, which collect H(k) data from different processes template void cal_o_delta(const std::vector& dm_hl, @@ -31,14 +30,6 @@ void cal_o_delta(const std::vector& dm_hl, ModuleBase::matrix& o_delta, const Parallel_Orbitals& pv, const int nks); - -// Collect data in h_in to matrix h_out. Note that left lower trianger in h_out is filled -template -void collect_h_mat(const Parallel_Orbitals& pv, - const std::vector>& h_in, - std::vector& h_out, - const int nlocal, - const int nks); } // namespace DeePKS_domain #endif diff --git a/source/module_hamilt_lcao/module_deepks/deepks_vdelta.cpp b/source/module_hamilt_lcao/module_deepks/deepks_vdelta.cpp index c76a45dc2f7..f060ada40b6 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_vdelta.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_vdelta.cpp @@ -77,6 +77,73 @@ void DeePKS_domain::cal_e_delta_band(const std::vector>& dm, return; } +template +void DeePKS_domain::collect_h_mat(const Parallel_Orbitals& pv, + const std::vector>& h_in, + std::vector& h_out, + const int nlocal, + const int nks) +{ + ModuleBase::TITLE("DeePKS_domain", "collect_h_tot"); + + // construct the total H matrix + for (int k = 0; k < nks; k++) + { +#ifdef __MPI + int ir = 0; + int ic = 0; + for (int i = 0; i < nlocal; i++) + { + std::vector lineH(nlocal - i, TK(0.0)); + + ir = pv.global2local_row(i); + if (ir >= 0) + { + // data collection + for (int j = i; j < nlocal; j++) + { + ic = pv.global2local_col(j); + if (ic >= 0) + { + int iic = 0; + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) + { + iic = ir + ic * pv.nrow; + } + else + { + iic = ir * pv.ncol + ic; + } + lineH[j - i] = h_in[k][iic]; + } + } + } + else + { + // do nothing + } + + Parallel_Reduce::reduce_all(lineH.data(), nlocal - i); + + for (int j = i; j < nlocal; j++) + { + h_out[k](i, j) = lineH[j - i]; + h_out[k](j, i) = h_out[k](i, j); // H is a symmetric matrix + } + } +#else + for (int i = 0; i < nlocal; i++) + { + for (int j = i; j < nlocal; j++) + { + h_out[k](i, j) = h_in[k][i * nlocal + j]; + h_out[k](j, i) = h_out[k](i, j); // H is a symmetric matrix + } + } +#endif + } +} + template void DeePKS_domain::cal_e_delta_band(const std::vector>& dm, const std::vector>& V_delta, const int nks, @@ -89,4 +156,18 @@ template void DeePKS_domain::cal_e_delta_band>( const Parallel_Orbitals* pv, double& e_delta_band); +template void DeePKS_domain::collect_h_mat( + const Parallel_Orbitals& pv, + const std::vector>& h_in, + std::vector& h_out, + const int nlocal, + const int nks); + +template void DeePKS_domain::collect_h_mat, ModuleBase::ComplexMatrix>( + const Parallel_Orbitals& pv, + const std::vector>>& h_in, + std::vector& h_out, + const int nlocal, + const int nks); + #endif diff --git a/source/module_hamilt_lcao/module_deepks/deepks_vdelta.h b/source/module_hamilt_lcao/module_deepks/deepks_vdelta.h index df922c6980e..dab6bf2c13b 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_vdelta.h +++ b/source/module_hamilt_lcao/module_deepks/deepks_vdelta.h @@ -2,6 +2,8 @@ #define DEEPKS_VDELTA_H #ifdef __DEEPKS +#include "module_base/complexmatrix.h" +#include "module_base/matrix.h" #include "module_basis/module_ao/parallel_orbitals.h" namespace DeePKS_domain @@ -10,8 +12,9 @@ namespace DeePKS_domain // deepks_vdelta.cpp //------------------------ -// This file contains 1 subroutine for calculating e_delta_bands +// This file contains 2 subroutine for calculating e_delta_bands // 1. cal_e_delta_band : calculates e_delta_bands +// 2. collect_h_mat, which collect H(k) data from different processes /// calculate tr(\rho V_delta) template @@ -20,6 +23,14 @@ void cal_e_delta_band(const std::vector>& dm, const int nks, const Parallel_Orbitals* pv, double& e_delta_band); + +// Collect data in h_in to matrix h_out. Note that left lower trianger in h_out is filled +template +void collect_h_mat(const Parallel_Orbitals& pv, + const std::vector>& h_in, + std::vector& h_out, + const int nlocal, + const int nks); } // namespace DeePKS_domain #endif #endif \ No newline at end of file From ad6647d7ff79b1362f9a30f466e2f5da63be28a7 Mon Sep 17 00:00:00 2001 From: ErjieWu Date: Mon, 31 Mar 2025 19:52:15 +0800 Subject: [PATCH 4/4] Fix: sign problem in DeePKS. --- .../module_deepks/deepks_force.cpp | 6 +-- .../module_deepks/deepks_fpre.cpp | 9 ++-- .../module_deepks/deepks_orbpre.cpp | 8 +-- .../module_deepks/deepks_pdm.cpp | 51 ++++++++++++------- .../module_deepks/deepks_spre.cpp | 6 +-- .../module_hcontainer/atom_pair.cpp | 4 +- .../603_NO_deepks_H2O_multik/result.ref | 8 +-- 7 files changed, 54 insertions(+), 38 deletions(-) diff --git a/source/module_hamilt_lcao/module_deepks/deepks_force.cpp b/source/module_hamilt_lcao/module_deepks/deepks_force.cpp index 1457632a27b..bd392b6651d 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_force.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_force.cpp @@ -102,9 +102,9 @@ void DeePKS_domain::cal_f_delta(const std::vector>& dm, int dRz = 0; if constexpr (std::is_same>::value) // for multi-k { - dRx = dR2.x - dR1.x; - dRy = dR2.y - dR1.y; - dRz = dR2.z - dR1.z; + dRx = dR1.x - dR2.x; + dRy = dR1.y - dR2.y; + dRz = dR1.z - dR2.z; } ModuleBase::Vector3 dR(dRx, dRy, dRz); diff --git a/source/module_hamilt_lcao/module_deepks/deepks_fpre.cpp b/source/module_hamilt_lcao/module_deepks/deepks_fpre.cpp index 2616fb5d1cb..61274c3d0d7 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_fpre.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_fpre.cpp @@ -70,14 +70,15 @@ void DeePKS_domain::cal_gdmx(const int lmaxd, int dRz = 0; if constexpr (std::is_same>::value) { - dRx = (dR2 - dR1).x; - dRy = (dR2 - dR1).y; - dRz = (dR2 - dR1).z; + dRx = (dR1 - dR2).x; + dRy = (dR1 - dR2).y; + dRz = (dR1 - dR2).z; } ModuleBase::Vector3 dR(dRx, dRy, dRz); hamilt::AtomPair dm_pair(ibt1, ibt2, dRx, dRy, dRz, &pv); - dm_pair.allocate(nullptr, 1); + dm_pair.allocate(nullptr, true); + // not support nspin = 2 now for (int ik = 0; ik < nks; ik++) { TK kphase = TK(0); diff --git a/source/module_hamilt_lcao/module_deepks/deepks_orbpre.cpp b/source/module_hamilt_lcao/module_deepks/deepks_orbpre.cpp index d679177b5be..7d1631a92c1 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_orbpre.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_orbpre.cpp @@ -197,15 +197,15 @@ void DeePKS_domain::cal_orbital_precalc(const std::vector& dm_hl, { hamilt::AtomPair dm_pair(ibt1, ibt2, - (dR2 - dR1).x, - (dR2 - dR1).y, - (dR2 - dR1).z, + (dR1 - dR2).x, + (dR1 - dR2).y, + (dR1 - dR2).z, &pv); dm_pair.allocate(&dm_array[ik * row_size * col_size], 0); const double arg - = -(kvec_d[ik] * ModuleBase::Vector3(dR2 - dR1)) * ModuleBase::TWO_PI; + = -(kvec_d[ik] * ModuleBase::Vector3(dR1 - dR2)) * ModuleBase::TWO_PI; double sinp, cosp; diff --git a/source/module_hamilt_lcao/module_deepks/deepks_pdm.cpp b/source/module_hamilt_lcao/module_deepks/deepks_pdm.cpp index 5374e82531e..54b23591da8 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_pdm.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_pdm.cpp @@ -273,36 +273,51 @@ void DeePKS_domain::cal_pdm(bool& init_pdm, } } // prepare DM from DMR - std::vector dm_array(row_size * col_size, 0.0); - const double* dm_current = nullptr; int dRx = 0, dRy = 0, dRz = 0; if constexpr (std::is_same>::value) { - dRx = dR2.x - dR1.x; - dRy = dR2.y - dR1.y; - dRz = dR2.z - dR1.z; + dRx = dR1.x - dR2.x; + dRy = dR1.y - dR2.y; + dRz = dR1.z - dR2.z; } - // dm_k + ModuleBase::Vector3 dR(dRx, dRy, dRz); + + hamilt::AtomPair dm_pair(ibt1, ibt2, dRx, dRy, dRz, &pv); + dm_pair.allocate(nullptr, true); auto dm_k = dm->get_DMK_vector(); - const int nrow = pv.nrow; - for (int ir = 0; ir < row_size; ir++) + + if constexpr (std::is_same::value) // for gamma-only + { + for (int is = 0; is < dm_k.size(); is++) + { + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) + { + dm_pair.add_from_matrix(dm_k[is].data(), pv.get_row_size(), 1.0, 1); + } + else + { + dm_pair.add_from_matrix(dm_k[is].data(), pv.get_col_size(), 1.0, 0); + } + } + } + else // for multi-k { - for (int ic = 0; ic < col_size; ic++) + for (int ik = 0; ik < dm_k.size(); ik++) { - int iglob = (pv.atom_begin_row[ibt1] + ir) + nrow * (pv.atom_begin_col[ibt2] + ic); - int iloc = ir * col_size + ic; - std::complex tmp = 0.0; - for(int ik = 0; ik < dm_k.size(); ik++) // dm_k.size() == _nk * _nspin + const double arg = -(kvec_d[ik] * dR) * ModuleBase::TWO_PI; + const std::complex kphase = std::complex(cos(arg), sin(arg)); + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) + { + dm_pair.add_from_matrix(dm_k[ik].data(), pv.get_row_size(), kphase, 1); + } + else { - const double arg = (kvec_d[ik] * ModuleBase::Vector3(dR1 - dR2)) * ModuleBase::TWO_PI; - const std::complex kphase = std::complex(cos(arg), sin(arg)); - tmp += dm_k[ik][iglob] * kphase; + dm_pair.add_from_matrix(dm_k[ik].data(), pv.get_col_size(), kphase, 0); } - dm_array[iloc] += tmp.real(); } } - dm_current = dm_array.data(); + const double* dm_current = dm_pair.get_pointer(); // use s_2t and dm_current to get g_1dmt // dgemm_: C = alpha * A * B + beta * C // C = g_1dmt, A = dm_current, B = s_2t diff --git a/source/module_hamilt_lcao/module_deepks/deepks_spre.cpp b/source/module_hamilt_lcao/module_deepks/deepks_spre.cpp index dfc83cad1e2..5ec34735d79 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_spre.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_spre.cpp @@ -78,9 +78,9 @@ void DeePKS_domain::cal_gdmepsl(const int lmaxd, int dRz = 0; if constexpr (std::is_same>::value) { - dRx = (dR2 - dR1).x; - dRy = (dR2 - dR1).y; - dRz = (dR2 - dR1).z; + dRx = (dR1 - dR2).x; + dRy = (dR1 - dR2).y; + dRz = (dR1 - dR2).z; } ModuleBase::Vector3 dR(dRx, dRy, dRz); diff --git a/source/module_hamilt_lcao/module_hcontainer/atom_pair.cpp b/source/module_hamilt_lcao/module_hcontainer/atom_pair.cpp index cef77a34acf..0a6d3162721 100644 --- a/source/module_hamilt_lcao/module_hcontainer/atom_pair.cpp +++ b/source/module_hamilt_lcao/module_hcontainer/atom_pair.cpp @@ -701,7 +701,7 @@ void AtomPair::add_from_matrix(const std::complex* hk, hk_real_pointer = (T*)hk_tmp; hk_imag_pointer = hk_real_pointer+1; BlasConnector::axpy(this->col_size, kphase.real(), hk_real_pointer, 2, hr_tmp, 1); - BlasConnector::axpy(this->col_size, kphase.imag(), hk_imag_pointer, 2, hr_tmp, 1); + BlasConnector::axpy(this->col_size, -kphase.imag(), hk_imag_pointer, 2, hr_tmp, 1); hk_tmp += ld_hk; hr_tmp += this->col_size; } @@ -715,7 +715,7 @@ void AtomPair::add_from_matrix(const std::complex* hk, hk_real_pointer = (T*)hk_tmp; hk_imag_pointer = hk_real_pointer+1; BlasConnector::axpy(this->col_size, kphase.real(), hk_real_pointer, ld_hk_2, hr_tmp, 1); - BlasConnector::axpy(this->col_size, kphase.imag(), hk_imag_pointer, ld_hk_2, hr_tmp, 1); + BlasConnector::axpy(this->col_size, -kphase.imag(), hk_imag_pointer, ld_hk_2, hr_tmp, 1); hk_tmp ++; hr_tmp += this->col_size; } diff --git a/tests/deepks/603_NO_deepks_H2O_multik/result.ref b/tests/deepks/603_NO_deepks_H2O_multik/result.ref index bbe886355d8..14532b6eea9 100644 --- a/tests/deepks/603_NO_deepks_H2O_multik/result.ref +++ b/tests/deepks/603_NO_deepks_H2O_multik/result.ref @@ -1,4 +1,4 @@ -etotref -466.8143178204656 -etotperatomref -155.6047726068 -totalforceref 10.244182 -totaltimeref 7.52 +etotref -466.8999964506086 +etotperatomref -155.6333321502 +totalforceref 10.047085 +totaltimeref 6.22