Skip to content

Commit aa6ec25

Browse files
author
abacus_fixer
committed
Replace raw new/delete with std::vector in module_xc
- xc_grad_assemble.cpp: replace 'double* dh = new double[]' with 'std::vector<double> dh', pass .data() to grad_dot - xc_grad_utils.cpp (grad_rho): replace gdrtmp raw pointer with std::vector<std::complex<double>>, use .data() for recip2real - xc_grad_utils.cpp (grad_dot): replace aux and gaux raw pointers with std::vector<std::complex<double>>, use .data() for FFT calls - xc_functional.cpp: minor comment typo fix (GGA, plane-wave) All replacements are zero-overhead (vector uses contiguous memory identical to new[]), exception-safe (auto-dealloc on scope exit), and consistent with the previous RAII migration in xc_grad.cpp. Verified: make -j 30 passes; ctest -R 'GRADCORR|VXC|PBE|SCAN|LAPL|HSE' 7/7 passed.
1 parent 556b836 commit aa6ec25

3 files changed

Lines changed: 11 additions & 16 deletions

File tree

source/source_hamilt/module_xc/xc_functional.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void XC_Functional::set_xc_type(const std::string xc_func_in)
7070
{
7171
ModuleBase::TITLE("XC_Functional", "set_xc_type");
7272
//Note : due to the separation of gcx_spin and gcc_spin,
73-
//when you are adding new GGA functionals,
73+
//when you are adding GGA functionals,
7474
//please put exchange first, followed by correlation,
7575
//such as for PBE we have:
7676
// func_id.push_back(XC_GGA_X_PBE);
@@ -347,7 +347,7 @@ void XC_Functional::set_xc_type(const std::string xc_func_in)
347347

348348
// if((func_type == 4 || func_type == 5) && basis_type == "pw")
349349
// {
350-
// ModuleBase::WARNING_QUIT("set_xc_type","hybrid functional not realized for planewave yet");
350+
// ModuleBase::WARNING_QUIT("set_xc_type","hybrid functional not realized for plane-wave yet");
351351
// }
352352

353353
// Hybrid functional is now supported for both PW and LCAO basis

source/source_hamilt/module_xc/xc_grad_assemble.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ void XC_Functional::gradcorr_assemble_vxc(
105105
// sum_alpha (D / D r_alpha) ( D(rho*Exc)/D(grad_alpha rho) )
106106

107107
// dh is in real sapce.
108-
double* dh = new double[rhopw->nrxx];
108+
std::vector<double> dh(rhopw->nrxx);
109109

110110
for(int is=0; is<nspin0; is++)
111111
{
112112
if(is==0)
113113
{
114-
XC_Functional::grad_dot(h1,dh,rhopw,ucell->tpiba);
114+
XC_Functional::grad_dot(h1, dh.data(), rhopw, ucell->tpiba);
115115
}
116116
if(is==1)
117117
{
118-
XC_Functional::grad_dot(h2,dh,rhopw,ucell->tpiba);
118+
XC_Functional::grad_dot(h2, dh.data(), rhopw, ucell->tpiba);
119119
}
120120
#ifdef _OPENMP
121121
#pragma omp parallel for schedule(static, 1024)
@@ -149,8 +149,6 @@ void XC_Functional::gradcorr_assemble_vxc(
149149
vtxcgc -= sum;
150150
}
151151

152-
delete[] dh;
153-
154152
vtxc += vtxcgc;
155153
etxc += etxcgc;
156154

source/source_hamilt/module_xc/xc_grad_utils.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void XC_Functional::grad_rho(
2121
const ModulePW::PW_Basis* rho_basis,
2222
const double tpiba)
2323
{
24-
std::complex<double> *gdrtmp = new std::complex<double>[rho_basis->nmaxgr];
24+
std::vector<std::complex<double>> gdrtmp(rho_basis->nmaxgr);
2525

2626
// the formula is : rho(r)^prime = int iG * rho(G)e^{iGr} dG
2727
for(int i = 0 ; i < 3 ; ++i)
@@ -36,7 +36,7 @@ void XC_Functional::grad_rho(
3636
}
3737

3838
// bring the gdr from G --> R
39-
rho_basis->recip2real(gdrtmp, gdrtmp);
39+
rho_basis->recip2real(gdrtmp.data(), gdrtmp.data());
4040

4141
// remember to multily 2pi/a0, which belongs to G vectors.
4242
#ifdef _OPENMP
@@ -48,7 +48,6 @@ void XC_Functional::grad_rho(
4848
}
4949
}
5050

51-
delete[] gdrtmp;
5251
return;
5352
}
5453

@@ -59,8 +58,8 @@ void XC_Functional::grad_dot(
5958
const ModulePW::PW_Basis* rho_basis,
6059
const double tpiba)
6160
{
62-
std::complex<double> *aux = new std::complex<double>[rho_basis->nmaxgr];
63-
std::complex<double> *gaux = new std::complex<double>[rho_basis->npw];
61+
std::vector<std::complex<double>> aux(rho_basis->nmaxgr);
62+
std::vector<std::complex<double>> gaux(rho_basis->npw);
6463

6564
for(int i = 0 ; i < 3 ; ++i)
6665
{
@@ -73,7 +72,7 @@ void XC_Functional::grad_dot(
7372
}
7473

7574
// bring to G space.
76-
rho_basis->real2recip(aux,aux);
75+
rho_basis->real2recip(aux.data(), aux.data());
7776
if (i == 0)
7877
{
7978
#ifdef _OPENMP
@@ -97,7 +96,7 @@ void XC_Functional::grad_dot(
9796
}
9897

9998
// bring back to R space
100-
rho_basis->recip2real(gaux,aux);
99+
rho_basis->recip2real(gaux.data(), aux.data());
101100

102101
#ifdef _OPENMP
103102
#pragma omp parallel for schedule(static, 1024)
@@ -107,8 +106,6 @@ void XC_Functional::grad_dot(
107106
dh[ir] = aux[ir].real() * tpiba;
108107
}
109108

110-
delete[] aux;
111-
delete[] gaux;
112109
return;
113110
}
114111

0 commit comments

Comments
 (0)