Skip to content

Commit

Permalink
replace new-delete with stl::vector in H_Ewald_pw.cpp (#3996)
Browse files Browse the repository at this point in the history
* replace new-delete with stl::vector in H_Ewald_pw.cpp

* Another 2 more edit
  • Loading branch information
QztOc07 committed Apr 17, 2024
1 parent 64f4ccd commit 58aac37
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions source/module_esolver/esolver_dp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ namespace ModuleESolver
std::ifstream ifs(dp_file);
std::string word;
int ntype_dp = 0;
std::string* label = nullptr;
//std::string* label = nullptr;

if (ifs)
{
Expand Down Expand Up @@ -200,7 +200,9 @@ namespace ModuleESolver
GlobalV::ofs_running << "Determine the type map from DP model" << std::endl;
GlobalV::ofs_running << "ntype read from DP model: " << ntype_dp << std::endl;

label = new std::string[ntype_dp];
//label = new std::string[ntype_dp];
std::vector<std::string> label_vec(ntype_dp);
std::string* label = label_vec.data();
for (int it = 0; it < ntype_dp; ++it)
{
ss >> label[it];
Expand All @@ -224,7 +226,6 @@ namespace ModuleESolver
ModuleBase::WARNING_QUIT("ESolver_DP", "Unsupported atom types for the DP model");
}
}
delete[] label;
}
}
ifs.close();
Expand Down
18 changes: 9 additions & 9 deletions source/module_hamilt_general/module_ewald/H_Ewald_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ double H_Ewald_pw::compute_ewald(const UnitCell& cell,
double ewalds=0.0;

ModuleBase::Vector3<double> dtau ;
ModuleBase::Vector3<double> *r;
double *r2;
double rmax=0.0;
double rr=0.0;
double upperbound=0.0;
Expand All @@ -55,9 +53,15 @@ double H_Ewald_pw::compute_ewald(const UnitCell& cell,
// used to optimize alpha

if(GlobalV::test_energy)ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"mxr",mxr);
r = new ModuleBase::Vector3<double>[mxr];
r2 = new double[mxr];
int* irr = new int[mxr];
//r = new ModuleBase::Vector3<double>[mxr];
//r2 = new double[mxr];
//int* irr = new int[mxr];
std::vector<ModuleBase::Vector3<double>> vec_r(mxr);
std::vector<double> vec_r2(mxr);
std::vector<int> vec_irr(mxr);
int* irr = vec_irr.data();
ModuleBase::Vector3<double>* r = vec_r.data();
double* r2 = vec_r2.data();

// (1) calculate total ionic charge
double charge = 0.0;
Expand Down Expand Up @@ -231,10 +235,6 @@ double H_Ewald_pw::compute_ewald(const UnitCell& cell,
ModuleBase::GlobalFunc::OUT("ewalds",ewalds);
}

delete[] irr;
delete[] r;
delete[] r2;

ModuleBase::timer::tick("H_Ewald_pw","compute_ewald");
return ewalds;
} // end function ewald
Expand Down
6 changes: 3 additions & 3 deletions source/module_hamilt_pw/hamilt_pwdft/stress_func_us.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ void Stress_PW<FPTYPE, Device>::stress_us(ModuleBase::matrix& sigma,
ModuleBase::matrix ylmk0(ppcell_in->lmaxq * ppcell_in->lmaxq, npw);
ModuleBase::YlmReal::Ylm_Real(ppcell_in->lmaxq * ppcell_in->lmaxq, npw, rho_basis->gcar, ylmk0);

double* qnorm = new double[npw];
//double* qnorm = new double[npw];
std::vector<double> qnorm_vec(npw);
double* qnorm = qnorm_vec.data();
for (int ig = 0; ig < npw; ig++)
{
qnorm[ig] = rho_basis->gcar[ig].norm() * ucell.tpiba;
Expand Down Expand Up @@ -175,8 +177,6 @@ void Stress_PW<FPTYPE, Device>::stress_us(ModuleBase::matrix& sigma,
}
sigma += stressus;

delete[] qnorm;

ModuleBase::timer::tick("Stress_Func", "stress_us");
return;
}
Expand Down

0 comments on commit 58aac37

Please sign in to comment.