Skip to content

Commit 196d199

Browse files
mohanchenabacus_fixer
andauthored
Split vnl_pw.cpp into several smaller files (deepmodeling#7869)
* Refactor vnl_pw, step 1: extract getvnl into vnl_pw_getvnl.cpp - Move getvnl<FPTYPE, Device> template body + 4 explicit instantiations (CPU/GPU float/double) from vnl_pw.cpp to new file vnl_pw_getvnl.cpp (~165 lines) - Update CMakeLists.txt and Makefile.Objects accordingly - vnl_pw.cpp reduced from 1797 to 1641 lines Verification: cd build_max_para_test && make -j 30 => build passed, no undefined reference * Style: split multi-variable declarations in vnl_pw_getvnl.cpp - One variable per line for better readability and maintainability - Rename pointer variables: _tab -> tab_ptr, _indv -> indv_ptr, _nhtol -> nhtol_ptr, _nhtolm -> nhtolm_ptr (avoid leading underscore, use _ptr suffix) Verification: cd build_max_para_test && make -j 30 => build passed * Doc: add one-variable-per-line rule to AGENTS.md Rule deepmodeling#8 in Required Baseline: declare one variable per line; do not use comma-separated declarations. This improves readability, makes diffs cleaner, and helps debugging with single-step execution. * Refactor: replace new/delete with std::vector in vnl_pw_getvnl.cpp - Replace int* h_atom_nh/na/nb with std::vector<int> - Replace ModuleBase::Vector3<double>* _gk with std::vector<Vector3> - Use .data() to get raw pointer for GPU sync operations - Remove all delete[] calls (RAII handles cleanup automatically) Benefits: - Exception safety (no memory leak if exception thrown) - Cleaner code (no manual memory management) - Follows modern C++ best practices Verification: cd build_max_para_test && make -j 30 => build passed * Doc: add doxygen comments to vnl_pw_getvnl.cpp - Add file-level @file/@brief documentation - Add getvnl() function documentation with workflow steps - Add comment explaining explicit template instantiations * Step 2: extract qrad/radial_fft_q into vnl_pw_qrad.cpp - Move compute_qrad(), radial_fft_q() (CPU matrix version), radial_fft_q<FPTYPE,Device>() (template version), and 4 explicit template instantiations to vnl_pw_qrad.cpp - Replace new[]/delete[] with std::vector in compute_qrad() - Add doxygen comments for all functions - Update CMakeLists.txt and Makefile.Objects Verification: cd build_max_para_test && make -j 30 => build passed * Step 3: extract cal_effective_D/newq/newd_* into vnl_pw_deeq.cpp - Move cal_effective_D(), newq(), newd_so(), newd_nc() to vnl_pw_deeq.cpp - Replace new[]/delete[] qnorm with std::vector in newq() - Add doxygen comments for all functions - Update CMakeLists.txt and Makefile.Objects - Fix blas_connector.h include path (source_base/module_external/) Verification: cd build_max_para_test && make -j 30 => build passed * refactor(pwdft): move LCAO alpha-channel VNL helpers to vnl_pw_alpha.cpp Step 4 of vnl_pw.cpp split: - Move Cal_C(), CG(), init_vnl_alpha() into new vnl_pw_alpha.cpp - Wrap whole file with #ifdef __LCAO - Drop dead commented-out getvnl_alpha block - Replace new[]/delete[] with std::vector for jl/aux in init_vnl_alpha - Sync CMakeLists.txt and Makefile.Objects Verified: make -j 30 in build_max_para_test passes. * refactor(pwdft): move init_vnl into vnl_pw_init_vnl.cpp Step 5 of vnl_pw.cpp split: - Move init_vnl() into new vnl_pw_init_vnl.cpp - Replace new[]/delete[] with std::vector for jl/aux inside the tab fill loop - Sync CMakeLists.txt and Makefile.Objects Verified: make -j 30 in build_max_para_test passes. * refactor(pwdft): clean up vnl_pw.cpp after split, drop unused includes Step 6 of vnl_pw.cpp split (final): - Keep only ctor/dtor, release_memory(), init(), print_vnl(), rescale_vnl() and the get_*_data<T>() template specializations - Drop now-unused includes (clebsch_gordan, math_integral, math_polyint, math_sphbes, math_ylmreal, parallel_reduce, vnl_op, parallel_comm) - Add file-level doxygen comment describing the new module layout Verified: make -j 30 in build_max_para_test passes. * Remove accidentally added empty CONNECT file The empty CONNECT file was unintentionally added in commit 3b53f58 during the vnl_pw_qrad split. It is not referenced by any code, build file, or script, so it is safe to delete. * update format * refactor(pwdft): replace raw new/delete arrays with std::vector in vl/vnl pseudopotential code - convert pseudopot_cell_vl local buffers (vloc1d/aux1/aux) and member zp to std::vector - convert pseudopot_cell_vnl::indv_ijkb0 to std::vector and drop manual delete - use std::fill/std::copy instead of ZEROS/COPYARRAY helpers on vectors * update AGENTS.md * docs: add std::vector conversion guideline to AGENTS.md baseline * style(pwdft): replace tab indentation with 4 spaces in vl_pw files * refactor(pw): decouple print_vloc from PARAM and move it out of init_vloc - print_vloc takes out_element_info and global_out_dir explicitly instead of reading PARAM internally - callers invoke print_vloc right after init_vloc at all four call sites * refactor(ofdft): cache PARAM.inp.nspin into local const in KEDF_XWM::get_energy Cache PARAM.inp.nspin into a local const to reduce repeated global parameter accesses, and add the missing newline at end of file. --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 1b3baf0 commit 196d199

18 files changed

Lines changed: 1879 additions & 1718 deletions

AGENTS.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ rules. Read the complete governance document before making or reviewing changes:
88

99
## Required Baseline
1010

11-
- Follow the seven ABACUS coding rules summarized from the project governance:
11+
- Follow the nine ABACUS coding rules summarized from the project governance:
1212
1. Do not increase cross-layer control through `GlobalV`, `GlobalC`, or
1313
`PARAM`; pass dependencies explicitly where practical. Migration-neutral
1414
moves must keep the PR-level global dependency budget non-increasing and
@@ -23,6 +23,9 @@ rules. Read the complete governance document before making or reviewing changes:
2323
6. Add focused tests for key features, bug fixes, INPUT behavior changes,
2424
heterogeneous kernels, and core-module refactors.
2525
7. Keep code compatible with the repository C++11 baseline.
26+
8. Declare one variable per line; do not use comma-separated declarations.
27+
9. Do not call MPI routines directly; use the internally-guarded wrappers
28+
(e.g., `Parallel_Reduce::reduce_*`, `Parallel_Common::bcast_*`) instead.
2629
- Use LF line endings for text files. Only `.bat` and `.cmd` files may use CRLF.
2730
- Keep source file additions deterministic: update the relevant `CMakeLists.txt`
2831
or explain why the file is generated or included indirectly.
@@ -31,6 +34,10 @@ rules. Read the complete governance document before making or reviewing changes:
3134
is required.
3235
- Report the exact verification performed. Do not claim completion without
3336
fresh test or check output.
37+
- Prefer `std::vector` over raw `new`/`delete` for dynamic arrays; before
38+
converting class members, confirm no external code consumes them as raw
39+
pointers (e.g., `std::vector<bool>` has no `.data()`), and use
40+
`std::fill`/`std::copy` instead of `ZEROS`/`COPYARRAY` on vector buffers.
3441

3542
## Repository Map
3643

@@ -83,6 +90,9 @@ rules. Read the complete governance document before making or reviewing changes:
8390
test sufficiency, and exception approval require human review.
8491
- Exceptions must be recorded in the PR with reason, scope, risk, and a follow-up
8592
cleanup plan.
93+
- After a refactor, propose brief lessons worth recording in this file, then
94+
ask the developer whether to write them in; be cautious and skip unclear
95+
or unverified lessons.
8696

8797
## Local Commands
8898

source/Makefile.Objects

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,12 @@ OBJS_SRCPW=h_ewald_pw.o\
723723
dnrm2.o\
724724
vl_pw.o\
725725
vnl_pw.o\
726+
vnl_pw_alpha.o\
727+
vnl_pw_deeq.o\
728+
vnl_pw_getvnl.o\
726729
vnl_pw_grad.o\
730+
vnl_pw_init_vnl.o\
731+
vnl_pw_qrad.o\
727732
chgmixing.o\
728733
charge.o\
729734
charge_init.o\

source/source_esolver/esolver_fp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep)
168168

169169
// reset local pseudopotentials
170170
this->locpp.init_vloc(ucell, this->pw_rhod);
171+
this->locpp.print_vloc(ucell, this->pw_rhod,
172+
this->inp_->out_element_info, PARAM.globalv.global_out_dir);
171173
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL");
172174

173175
// perform symmetry analysis

source/source_esolver/esolver_of.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void ESolver_OF::before_all_runners(BaseCell& basecell, const Input_para& inp)
8686

8787
// initialize local pseudopotential
8888
this->locpp.init_vloc(ucell,pw_rho);
89+
this->locpp.print_vloc(ucell, pw_rho, inp.out_element_info, PARAM.globalv.global_out_dir);
8990
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL");
9091

9192

source/source_estate/setup_estate_pw.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ void setup_estate_pw_impl(
107107
}
108108

109109
locpp.init_vloc(ucell, pw_rhod);
110+
locpp.print_vloc(ucell, pw_rhod, inp.out_element_info, PARAM.globalv.global_out_dir);
110111
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL");
111112

112113
ppcell.init(ucell, &sf, pw_wfc);

source/source_lcao/lcao_set.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void LCAO_domain::set_pot(
6969
{
7070
//! 1) init local pseudopotentials
7171
locpp.init_vloc(ucell, &pw_rho);
72+
locpp.print_vloc(ucell, &pw_rho, inp.out_element_info, PARAM.globalv.global_out_dir);
7273

7374
//! 2) init potentials
7475
if (pelec->pot == nullptr)

source/source_pw/module_ofdft/kedf_xwm.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,23 @@ void KEDF_XWM::set_para(double dV,
6464
*/
6565
double KEDF_XWM::get_energy(const double* const* prho, ModulePW::PW_Basis* pw_rho)
6666
{
67-
double** w1Rho5_6 = new double*[PARAM.inp.nspin];
68-
for (int is = 0; is < PARAM.inp.nspin; ++is)
67+
const int nspin = PARAM.inp.nspin;
68+
double** w1Rho5_6 = new double*[nspin];
69+
for (int is = 0; is < nspin; ++is)
6970
{
7071
w1Rho5_6[is] = new double[pw_rho->nrxx];
7172
}
7273
this->multi_kernel(prho, this->kernel1_.data(), w1Rho5_6, this->kappa_5_6, pw_rho);
7374

74-
double** w2Rho5_6 = new double*[PARAM.inp.nspin];
75-
for (int is = 0; is < PARAM.inp.nspin; ++is)
75+
double** w2Rho5_6 = new double*[nspin];
76+
for (int is = 0; is < nspin; ++is)
7677
{
7778
w2Rho5_6[is] = new double[pw_rho->nrxx];
7879
}
7980
this->multi_kernel(prho, this->kernel2_.data(), w2Rho5_6, this->kappa_5_6, pw_rho);
8081

8182
double energy = 0.; // in Ry
82-
if (PARAM.inp.nspin == 1)
83+
if (nspin == 1)
8384
{
8485
for (int ir = 0; ir < pw_rho->nrxx; ++ir)
8586
{
@@ -88,14 +89,14 @@ double KEDF_XWM::get_energy(const double* const* prho, ModulePW::PW_Basis* pw_rh
8889
}
8990
energy += this->dV_;
9091
}
91-
else if (PARAM.inp.nspin == 2)
92+
else if (nspin == 2)
9293
{
9394
// TODO: spin polarized
9495
}
9596
this->xwm_energy = energy;
9697
Parallel_Reduce::reduce_all(this->xwm_energy);
9798

98-
for (int is = 0; is < PARAM.inp.nspin; ++is)
99+
for (int is = 0; is < nspin; ++is)
99100
{
100101
delete[] w1Rho5_6[is];
101102
delete[] w2Rho5_6[is];
@@ -379,4 +380,4 @@ void KEDF_XWM::fill_kernel(double tf_weight, double vw_weight, ModulePW::PW_Basi
379380
this->kernel1_[ig] = (this->c_0 * lindhard + this->c_2 * diff_lindhard) * this->c_kernel;
380381
this->kernel2_[ig] = this->c_1 * diff_lindhard * this->c_kernel;
381382
}
382-
}
383+
}

source/source_pw/module_pwdft/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ list(APPEND objects
4343
stress_pw.cpp
4444
vl_pw.cpp
4545
vnl_pw.cpp
46+
vnl_pw_alpha.cpp
47+
vnl_pw_deeq.cpp
48+
vnl_pw_getvnl.cpp
4649
vnl_pw_grad.cpp
50+
vnl_pw_init_vnl.cpp
51+
vnl_pw_qrad.cpp
4752
stru_fac.cpp
4853
stru_fac_k.cpp
4954
soc.cpp

0 commit comments

Comments
 (0)