From bce3cf2595a61bd6b5ad6992fc88ce315799202a Mon Sep 17 00:00:00 2001 From: ErjieWu Date: Wed, 26 Mar 2025 16:42:45 +0800 Subject: [PATCH 1/3] 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 1f450083a7..349cb1b6c0 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 d59684d397..50eb5324b0 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 e3058a15a7..c1104a9b7d 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 3d114354e3..6a3626712c 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 c58672b180..2be3a943f8 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 36108b3a91..6189c8acf6 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 0000000000..6ee15925c0 --- /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 0000000000..2ed2a3d5ab --- /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/3] 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 fc5a939a7f..f3fdd99725 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 50eb5324b0..3aaee830b4 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 c1104a9b7d..27eec83ae0 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 90936e88cc..0000000000 --- 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 98d365e70a..0000000000 --- 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 8baf298a46..6cbe38d49f 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 6c60fd1e96..d300d705ea 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 ffd6a65bf0..0000000000 --- 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 883a91ac7d..0000000000 --- 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 1db795c35d..0000000000 --- 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 2c3ef88b60..0000000000 --- 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/3] 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 6cbe38d49f..8baf298a46 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 d300d705ea..6c60fd1e96 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 c76a45dc2f..f060ada40b 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 df922c6980..dab6bf2c13 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