Skip to content

Commit 4154ec3

Browse files
authored
replace with vector (#3972)
* replace with vector * replace with vector
1 parent c1621de commit 4154ec3

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

source/module_hamilt_pw/hamilt_pwdft/stress_func_loc.cpp

+13-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void Stress_Func<FPTYPE, Device>::stress_loc(ModuleBase::matrix& sigma,
1616
ModuleBase::TITLE("Stress_Func","stress_loc");
1717
ModuleBase::timer::tick("Stress_Func","stress_loc");
1818

19-
FPTYPE *dvloc = new FPTYPE[rho_basis->npw];
19+
std::vector<FPTYPE> dvloc(rho_basis->npw);
2020
FPTYPE evloc=0.0;
2121
FPTYPE fact=1.0;
2222

@@ -26,7 +26,7 @@ void Stress_Func<FPTYPE, Device>::stress_loc(ModuleBase::matrix& sigma,
2626

2727

2828

29-
std::complex<FPTYPE> *aux = new std::complex<FPTYPE> [rho_basis->nmaxgr];
29+
std::vector<std::complex<FPTYPE>> aux(rho_basis->nmaxgr);
3030

3131
/*
3232
blocking rho_basis->nrxx for data locality.
@@ -58,7 +58,7 @@ void Stress_Func<FPTYPE, Device>::stress_loc(ModuleBase::matrix& sigma,
5858
}
5959
}
6060
}
61-
rho_basis->real2recip(aux,aux);
61+
rho_basis->real2recip(aux.data(),aux.data());
6262

6363
// if(INPUT.gamma_only==1) fact=2.0;
6464
// else fact=1.0;
@@ -87,7 +87,7 @@ void Stress_Func<FPTYPE, Device>::stress_loc(ModuleBase::matrix& sigma,
8787
//
8888
// special case: pseudopotential is coulomb 1/r potential
8989
//
90-
this->dvloc_coulomb (atom->ncpp.zv, dvloc, rho_basis);
90+
this->dvloc_coulomb (atom->ncpp.zv, dvloc.data(), rho_basis);
9191
//
9292
}
9393
else
@@ -96,7 +96,7 @@ void Stress_Func<FPTYPE, Device>::stress_loc(ModuleBase::matrix& sigma,
9696
// normal case: dvloc contains dV_loc(G)/dG
9797
//
9898
this->dvloc_of_g ( atom->ncpp.msh, atom->ncpp.rab, atom->ncpp.r,
99-
atom->ncpp.vloc_at, atom->ncpp.zv, dvloc, rho_basis);
99+
atom->ncpp.vloc_at, atom->ncpp.zv, dvloc.data(), rho_basis);
100100
//
101101
}
102102
#ifndef _OPENMP
@@ -156,8 +156,7 @@ void Stress_Func<FPTYPE, Device>::stress_loc(ModuleBase::matrix& sigma,
156156
sigma(m,l) = sigma(l,m);
157157
}
158158
}
159-
delete[] dvloc;
160-
delete[] aux;
159+
161160

162161

163162
ModuleBase::timer::tick("Stress_Func","stress_loc");
@@ -185,14 +184,14 @@ ModulePW::PW_Basis* rho_basis
185184
//FPTYPE dvloc[ngl];
186185
// the fourier transform dVloc/dG
187186
//
188-
FPTYPE *aux1;
187+
189188

190189
int igl0;
191190
// counter on erf functions or gaussians
192191
// counter on g shells vectors
193192
// first shell with g != 0
194-
195-
aux1 = new FPTYPE[msh];
193+
194+
std::vector<FPTYPE> aux1(msh);
196195

197196
// the G=0 component is not computed
198197
if (rho_basis->gg_uniq[0] < 1.0e-8)
@@ -223,8 +222,7 @@ ModulePW::PW_Basis* rho_basis
223222
aux1[i] = r [i] * vloc_at [i] + zp * ModuleBase::e2 * erf(r[i]);
224223
}
225224

226-
FPTYPE *aux;
227-
aux = new FPTYPE[msh];
225+
std::vector<FPTYPE> aux(msh);
228226
aux[0] = 0.0;
229227
#ifdef _OPENMP
230228
#pragma omp for
@@ -247,19 +245,19 @@ ModulePW::PW_Basis* rho_basis
247245
}
248246
FPTYPE vlcp=0;
249247
// simpson (msh, aux, rab, vlcp);
250-
ModuleBase::Integral::Simpson_Integral(msh, aux, rab, vlcp );
248+
ModuleBase::Integral::Simpson_Integral(msh, aux.data(), rab, vlcp );
251249
// DV(g^2)/Dg^2 = (DV(g)/Dg)/2g
252250
vlcp *= ModuleBase::FOUR_PI / GlobalC::ucell.omega / 2.0 / gx;
253251
// subtract the long-range term
254252
FPTYPE g2a = gx2 / 4.0;
255253
vlcp += ModuleBase::FOUR_PI / GlobalC::ucell.omega * zp * ModuleBase::e2 * ModuleBase::libm::exp ( - g2a) * (g2a + 1) / pow(gx2 , 2);
256254
dvloc [igl] = vlcp;
257255
}
258-
delete[] aux;
256+
259257
#ifdef _OPENMP
260258
}
261259
#endif
262-
delete[] aux1;
260+
263261

264262
return;
265263
}

source/module_io/berryphase.cpp

+21-18
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ double berryphase::stringPhase(int index_str,
318318

319319
std::complex<double> det(1.0,0.0);
320320
int info = 0;
321-
int *ipiv = new int[nbands];
322-
LapackConnector::zgetrf(nbands, nbands, mat, nbands, ipiv, &info);
321+
std::vector<int> ipiv(nbands);
322+
LapackConnector::zgetrf(nbands, nbands, mat, nbands, ipiv.data(), &info);
323323
for (int ib = 0; ib < nbands; ib++)
324324
{
325325
if (ipiv[ib] != (ib+1)) det = -det * mat(ib,ib);
@@ -328,7 +328,7 @@ double berryphase::stringPhase(int index_str,
328328

329329
zeta = zeta*det;
330330

331-
delete[] ipiv;
331+
// delete[] ipiv;
332332
}
333333
#ifdef __LCAO
334334
else if(GlobalV::BASIS_TYPE=="lcao")
@@ -395,12 +395,12 @@ void berryphase::Berry_Phase(int nbands,
395395
const K_Vectors& kv)
396396
{
397397
std::complex<double> cave = 0.0;
398+
398399
std::vector<double> phik(total_string);
399400
double phik_ave = 0.0;
400401
std::vector<std::complex<double>> cphik(total_string);
401402
std::vector<double> wistring(total_string);
402-
403-
403+
404404
// electron polarization
405405

406406
// get weight of every std::string
@@ -452,7 +452,6 @@ void berryphase::Berry_Phase(int nbands,
452452
mod_elec_tot = 1;
453453
}
454454

455-
456455

457456

458457

@@ -490,14 +489,18 @@ void berryphase::Macroscopic_polarization(const int npwx,
490489
ModuleBase::Vector3<double> rcell_1(GlobalC::ucell.G.e11,GlobalC::ucell.G.e12,GlobalC::ucell.G.e13);
491490
ModuleBase::Vector3<double> rcell_2(GlobalC::ucell.G.e21,GlobalC::ucell.G.e22,GlobalC::ucell.G.e23);
492491
ModuleBase::Vector3<double> rcell_3(GlobalC::ucell.G.e31,GlobalC::ucell.G.e32,GlobalC::ucell.G.e33);
493-
int *mod_ion = new int[GlobalC::ucell.nat];
494-
double *pdl_ion_R1 = new double[GlobalC::ucell.nat];
495-
double *pdl_ion_R2 = new double[GlobalC::ucell.nat];
496-
double *pdl_ion_R3 = new double[GlobalC::ucell.nat];
497-
ModuleBase::GlobalFunc::ZEROS(mod_ion,GlobalC::ucell.nat);
498-
ModuleBase::GlobalFunc::ZEROS(pdl_ion_R1,GlobalC::ucell.nat);
499-
ModuleBase::GlobalFunc::ZEROS(pdl_ion_R2,GlobalC::ucell.nat);
500-
ModuleBase::GlobalFunc::ZEROS(pdl_ion_R3,GlobalC::ucell.nat);
492+
//int *mod_ion = new int[GlobalC::ucell.nat];
493+
std::vector<int> mod_ion(GlobalC::ucell.nat);
494+
//double *pdl_ion_R1 = new double[GlobalC::ucell.nat];
495+
std::vector<double> pdl_ion_R1(GlobalC::ucell.nat);
496+
//double *pdl_ion_R2 = new double[GlobalC::ucell.nat];
497+
std::vector<double> pdl_ion_R2(GlobalC::ucell.nat);
498+
//double *pdl_ion_R3 = new double[GlobalC::ucell.nat];
499+
std::vector<double> pdl_ion_R3(GlobalC::ucell.nat);
500+
ModuleBase::GlobalFunc::ZEROS(mod_ion.data(),GlobalC::ucell.nat);
501+
ModuleBase::GlobalFunc::ZEROS(pdl_ion_R1.data(),GlobalC::ucell.nat);
502+
ModuleBase::GlobalFunc::ZEROS(pdl_ion_R2.data(),GlobalC::ucell.nat);
503+
ModuleBase::GlobalFunc::ZEROS(pdl_ion_R3.data(),GlobalC::ucell.nat);
501504

502505
bool lodd = false;
503506
int atom_index = 0;
@@ -566,10 +569,10 @@ void berryphase::Macroscopic_polarization(const int npwx,
566569
polarization_ion[2] = polarization_ion[2] - 2.0 * round(polarization_ion[2] / 2.0);
567570
}
568571

569-
delete[] mod_ion;
570-
delete[] pdl_ion_R1;
571-
delete[] pdl_ion_R2;
572-
delete[] pdl_ion_R3;
572+
// delete[] mod_ion;
573+
// delete[] pdl_ion_R1;
574+
// delete[] pdl_ion_R2;
575+
// delete[] pdl_ion_R3;
573576

574577
// ion polarization end
575578

0 commit comments

Comments
 (0)