Skip to content

Commit 88ba2b0

Browse files
authored
Merge branch 'develop' into refactor/remove-dead-cross-module-includes
2 parents d9ea99f + 7a556a4 commit 88ba2b0

6 files changed

Lines changed: 86 additions & 83 deletions

File tree

source/source_io/module_ctrl/ctrl_output_pw.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell,
370370
pw_wfc,
371371
pw_rho,
372372
ucell,
373-
pelec->pot->get_eff_v(0));
373+
pelec->pot->get_eff_v(0),
374+
chr.nrxx);
374375
}
375376
#endif
376377

source/source_io/module_ml/cal_mlkedf_descriptors.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace ModuleIO
44
{
55

66
void Cal_MLKEDF_Descriptors::set_para(
7-
const int &nx,
7+
const int &nrxx,
88
const double &nelec,
99
const double &tf_weight,
1010
const double &vw_weight,
@@ -23,7 +23,7 @@ void Cal_MLKEDF_Descriptors::set_para(
2323
std::ostream& ofs_running
2424
)
2525
{
26-
this->nx = nx;
26+
this->nrxx = nrxx;
2727
this->nkernel = nkernel;
2828
this->chi_p = chi_p;
2929
this->chi_q = chi_q;
@@ -235,7 +235,7 @@ void Cal_MLKEDF_Descriptors::divergence(double ** pinput, const ModulePW::PW_Bas
235235
{
236236
std::complex<double> *recipContainer = new std::complex<double>[pw_rho->npw];
237237
std::complex<double> img(0.0, 1.0);
238-
ModuleBase::GlobalFunc::ZEROS(routput, this->nx);
238+
ModuleBase::GlobalFunc::ZEROS(routput, this->nrxx);
239239
for (int i = 0; i < 3; ++i)
240240
{
241241
pw_rho->real2recip(pinput[i], recipContainer);
@@ -251,7 +251,7 @@ void Cal_MLKEDF_Descriptors::divergence(double ** pinput, const ModulePW::PW_Bas
251251

252252
void Cal_MLKEDF_Descriptors::tanh(std::vector<double> &pinput, std::vector<double> &routput, double chi)
253253
{
254-
for (int i = 0; i < this->nx; ++i)
254+
for (int i = 0; i < this->nrxx; ++i)
255255
{
256256
routput[i] = std::tanh(pinput[i] * chi);
257257
}
@@ -264,15 +264,15 @@ double Cal_MLKEDF_Descriptors::dtanh(double tanhx, double chi)
264264

265265
void Cal_MLKEDF_Descriptors::getGamma(const double * const *prho, std::vector<double> &rgamma)
266266
{
267-
for(int ir = 0; ir < this->nx; ++ir)
267+
for(int ir = 0; ir < this->nrxx; ++ir)
268268
{
269269
rgamma[ir] = std::pow(prho[0][ir]/this->rho0, 1./3.);
270270
}
271271
}
272272

273273
void Cal_MLKEDF_Descriptors::getP(const double * const *prho, const ModulePW::PW_Basis *pw_rho, std::vector<std::vector<double>> &pnablaRho, std::vector<double> &rp)
274274
{
275-
for(int ir = 0; ir < this->nx; ++ir)
275+
for(int ir = 0; ir < this->nrxx; ++ir)
276276
{
277277
rp[ir] = 0.;
278278
for (int j = 0; j < 3; ++j)
@@ -294,7 +294,7 @@ void Cal_MLKEDF_Descriptors::getQ(const double * const *prho, const ModulePW::PW
294294
}
295295
pw_rho->recip2real(recipRho, rq.data());
296296

297-
for (int ir = 0; ir < this->nx; ++ir)
297+
for (int ir = 0; ir < this->nrxx; ++ir)
298298
{
299299
rq[ir] *= this->pqcoef / std::pow(prho[0][ir], 5.0/3.0);
300300
}
@@ -320,7 +320,7 @@ void Cal_MLKEDF_Descriptors::getQnl(const int ikernel, std::vector<double> &pq,
320320
// xi = gammanl/gamma
321321
void Cal_MLKEDF_Descriptors::getXi(std::vector<double> &pgamma, std::vector<double> &pgammanl, std::vector<double> &rxi)
322322
{
323-
for (int ir = 0; ir < this->nx; ++ir)
323+
for (int ir = 0; ir < this->nrxx; ++ir)
324324
{
325325
if (pgamma[ir] == 0)
326326
{
@@ -337,7 +337,7 @@ void Cal_MLKEDF_Descriptors::getXi(std::vector<double> &pgamma, std::vector<doub
337337
// tanhxi = tanh(gammanl/gamma)
338338
void Cal_MLKEDF_Descriptors::getTanhXi(const int ikernel, std::vector<double> &pgamma, std::vector<double> &pgammanl, std::vector<double> &rtanhxi)
339339
{
340-
for (int ir = 0; ir < this->nx; ++ir)
340+
for (int ir = 0; ir < this->nrxx; ++ir)
341341
{
342342
if (pgamma[ir] == 0)
343343
{
@@ -404,14 +404,14 @@ void Cal_MLKEDF_Descriptors::getF_KS(
404404
std::vector<double> &rpauli
405405
)
406406
{
407-
double *pauliED = new double[this->nx]; // Pauli Energy Density
408-
ModuleBase::GlobalFunc::ZEROS(pauliED, this->nx);
407+
double *pauliED = new double[this->nrxx]; // Pauli Energy Density
408+
ModuleBase::GlobalFunc::ZEROS(pauliED, this->nrxx);
409409

410-
double *pauliPot = new double[this->nx];
411-
ModuleBase::GlobalFunc::ZEROS(pauliPot, this->nx);
410+
double *pauliPot = new double[this->nrxx];
411+
ModuleBase::GlobalFunc::ZEROS(pauliPot, this->nrxx);
412412

413-
std::complex<double> *wfcr = new std::complex<double>[this->nx];
414-
ModuleBase::GlobalFunc::ZEROS(wfcr, this->nx);
413+
std::complex<double> *wfcr = new std::complex<double>[this->nrxx];
414+
ModuleBase::GlobalFunc::ZEROS(wfcr, this->nrxx);
415415

416416
double epsilonM = pelec->ekb(0,0);
417417
assert(PARAM.inp.nspin == 1);
@@ -438,14 +438,14 @@ void Cal_MLKEDF_Descriptors::getF_KS(
438438
// output one wf, to check KS equation
439439
if (ik == 0 && ibnd == 0)
440440
{
441-
std::vector<double> wf_real = std::vector<double>(this->nx);
442-
std::vector<double> wf_imag = std::vector<double>(this->nx);
443-
for (int ir = 0; ir < this->nx; ++ir)
441+
std::vector<double> wf_real = std::vector<double>(this->nrxx);
442+
std::vector<double> wf_imag = std::vector<double>(this->nrxx);
443+
for (int ir = 0; ir < this->nrxx; ++ir)
444444
{
445445
wf_real[ir] = wfcr[ir].real();
446446
wf_imag[ir] = wfcr[ir].imag();
447447
}
448-
const long unsigned cshape[] = {(long unsigned) this->nx}; // shape of container and containernl
448+
const long unsigned cshape[] = {(long unsigned) this->nrxx}; // shape of container and containernl
449449
}
450450

451451
if (w1 != 0.0)
@@ -474,7 +474,7 @@ void Cal_MLKEDF_Descriptors::getF_KS(
474474

475475
pw_psi->recip2real(wfcr, wfcr, ik);
476476

477-
for (int ir = 0; ir < this->nx; ++ir)
477+
for (int ir = 0; ir < this->nrxx; ++ir)
478478
{
479479
pauliED[ir] += w1 * norm(wfcr[ir]); // actually, here should be w1/2 * norm(wfcr[ir]), but we multiply 2 to convert Ha to Ry.
480480
}
@@ -484,13 +484,13 @@ void Cal_MLKEDF_Descriptors::getF_KS(
484484

485485
for (int j = 0; j < 3; ++j)
486486
{
487-
for (int ir = 0; ir < this->nx; ++ir)
487+
for (int ir = 0; ir < this->nrxx; ++ir)
488488
{
489489
pauliED[ir] -= nablaRho[j][ir] * nablaRho[j][ir] / (8. * pelec->charge->rho[0][ir]) * 2.; // convert Ha to Ry.
490490
}
491491
}
492492

493-
for (int ir = 0; ir < this->nx; ++ir)
493+
for (int ir = 0; ir < this->nrxx; ++ir)
494494
{
495495
rF[ir] = pauliED[ir] / (this->cTF * std::pow(pelec->charge->rho[0][ir], 5./3.));
496496
rpauli[ir] = (pauliED[ir] + pauliPot[ir])/pelec->charge->rho[0][ir] + epsilonM;

source/source_io/module_ml/cal_mlkedf_descriptors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Cal_MLKEDF_Descriptors
9090
std::vector<double> chi_pnl = {1.0};
9191
std::vector<double> chi_qnl = {1.0};
9292

93-
int nx = 0;
93+
int nrxx = 0;
9494
double dV = 0.;
9595
double rho0 = 0.; // average rho
9696
double kF = 0.; // Fermi vector kF = (3 pi^2 rho0)^(1/3)

source/source_io/module_ml/write_mlkedf_descriptors.cpp

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,50 @@ void Write_MLKEDF_Descriptors::generateTrainData_KS(
1414
ModulePW::PW_Basis_K *pw_psi,
1515
ModulePW::PW_Basis *pw_rho,
1616
UnitCell& ucell,
17-
const double* veff
17+
const double* veff,
18+
const int nrxx
1819
)
1920
{
20-
std::vector<std::vector<double>> nablaRho(3, std::vector<double>(this->cal_tool->nx, 0.));
21+
if (nrxx <= 0)
22+
{
23+
ModuleBase::WARNING_QUIT("Write_MLKEDF_Descriptors::generateTrainData_KS", "nrxx must be greater than 0");
24+
}
25+
26+
std::vector<std::vector<double>> drho(3, std::vector<double>(nrxx, 0.));
2127

22-
this->generate_descriptor(out_dir, pelec->charge->rho, pw_rho, nablaRho);
28+
this->generate_descriptor(out_dir, pelec->charge->rho, pw_rho, drho, nrxx);
2329

24-
std::vector<double> container(this->cal_tool->nx);
25-
std::vector<double> containernl(this->cal_tool->nx);
30+
std::vector<double> enhancement(nrxx);
31+
std::vector<double> pauli(nrxx);
2632

27-
const long unsigned cshape[] = {(long unsigned) this->cal_tool->nx}; // shape of container and containernl
28-
// enhancement factor of Pauli energy, and Pauli potential
29-
this->cal_tool->getF_KS(psi, pelec, pw_psi, pw_rho, ucell, nablaRho, container, containernl);
33+
this->cal_tool->getF_KS(psi, pelec, pw_psi, pw_rho, ucell, drho, enhancement, pauli);
3034

3135
Symmetry_rho srho;
3236

33-
Charge* ptempRho = new Charge();
34-
ptempRho->nspin = PARAM.inp.nspin;
35-
ptempRho->nrxx = this->cal_tool->nx;
36-
ptempRho->rho_core = pelec->charge->rho_core;
37-
ptempRho->rho = new double*[1];
38-
ptempRho->rho[0] = new double[this->cal_tool->nx];
39-
ptempRho->rhog = new std::complex<double>*[1];
40-
ptempRho->rhog[0] = new std::complex<double>[pw_rho->npw];
41-
42-
for (int ir = 0; ir < this->cal_tool->nx; ++ir){
43-
ptempRho->rho[0][ir] = container[ir];
44-
}
45-
srho.begin(0, *ptempRho, pw_rho, ucell.symm);
46-
for (int ir = 0; ir < this->cal_tool->nx; ++ir){
47-
container[ir] = ptempRho->rho[0][ir];
48-
}
37+
std::vector<double> rho_vec(nrxx);
38+
std::vector<std::complex<double>> rhog_vec(pw_rho->npw);
39+
double* rho_ptr = rho_vec.data();
40+
std::complex<double>* rhog_ptr = rhog_vec.data();
41+
42+
std::copy(enhancement.begin(), enhancement.end(), rho_vec.begin());
43+
srho.begin(0, &rho_ptr, &rhog_ptr, pw_rho->npw, nullptr, pw_rho, ucell.symm);
44+
std::copy(rho_vec.begin(), rho_vec.end(), enhancement.begin());
45+
46+
std::copy(pauli.begin(), pauli.end(), rho_vec.begin());
47+
srho.begin(0, &rho_ptr, &rhog_ptr, pw_rho->npw, nullptr, pw_rho, ucell.symm);
48+
std::copy(rho_vec.begin(), rho_vec.end(), pauli.begin());
4949

50-
for (int ir = 0; ir < this->cal_tool->nx; ++ir){
51-
ptempRho->rho[0][ir] = containernl[ir];
52-
}
53-
srho.begin(0, *ptempRho, pw_rho, ucell.symm);
54-
for (int ir = 0; ir < this->cal_tool->nx; ++ir){
55-
containernl[ir] = ptempRho->rho[0][ir];
56-
}
5750

58-
npy::SaveArrayAsNumpy(out_dir + "/enhancement.npy", false, 1, cshape, container);
59-
npy::SaveArrayAsNumpy(out_dir + "/pauli.npy", false, 1, cshape, containernl);
51+
// output data in .npy format
52+
const long unsigned cshape[] = {(long unsigned) nrxx};
53+
npy::SaveArrayAsNumpy(out_dir + "/enhancement.npy", false, 1, cshape, enhancement);
54+
npy::SaveArrayAsNumpy(out_dir + "/pauli.npy", false, 1, cshape, pauli);
6055

61-
for (int ir = 0; ir < this->cal_tool->nx; ++ir)
56+
for (int ir = 0; ir < nrxx; ++ir)
6257
{
63-
container[ir] = veff[ir];
58+
enhancement[ir] = veff[ir];
6459
}
65-
npy::SaveArrayAsNumpy(out_dir + "/veff.npy", false, 1, cshape, container);
66-
67-
delete ptempRho;
60+
npy::SaveArrayAsNumpy(out_dir + "/veff.npy", false, 1, cshape, enhancement);
6861
}
6962

7063
void Write_MLKEDF_Descriptors::generateTrainData_KS(
@@ -74,12 +67,13 @@ void Write_MLKEDF_Descriptors::generateTrainData_KS(
7467
ModulePW::PW_Basis_K *pw_psi,
7568
ModulePW::PW_Basis *pw_rho,
7669
UnitCell& ucell,
77-
const double* veff
70+
const double* veff,
71+
const int nrxx
7872
)
7973
{
8074
psi::Psi<std::complex<double>, base_device::DEVICE_CPU> psi_double(*psi);
8175

82-
this->generateTrainData_KS(out_dir, &psi_double, pelec, pw_psi, pw_rho, ucell, veff);
76+
this->generateTrainData_KS(out_dir, &psi_double, pelec, pw_psi, pw_rho, ucell, veff, nrxx);
8377
}
8478

8579
#if ((defined __CUDA) || (defined __ROCM))
@@ -90,12 +84,13 @@ void Write_MLKEDF_Descriptors::generateTrainData_KS(
9084
ModulePW::PW_Basis_K *pw_psi,
9185
ModulePW::PW_Basis *pw_rho,
9286
UnitCell& ucell,
93-
const double* veff
87+
const double* veff,
88+
const int nrxx
9489
)
9590
{
9691
psi::Psi<std::complex<double>, base_device::DEVICE_CPU> psi_cpu(*psi);
9792

98-
this->generateTrainData_KS(out_dir, &psi_cpu, pelec, pw_psi, pw_rho, ucell, veff);
93+
this->generateTrainData_KS(out_dir, &psi_cpu, pelec, pw_psi, pw_rho, ucell, veff, nrxx);
9994
}
10095

10196
void Write_MLKEDF_Descriptors::generateTrainData_KS(
@@ -105,34 +100,36 @@ void Write_MLKEDF_Descriptors::generateTrainData_KS(
105100
ModulePW::PW_Basis_K *pw_psi,
106101
ModulePW::PW_Basis *pw_rho,
107102
UnitCell& ucell,
108-
const double *veff
103+
const double *veff,
104+
const int nrxx
109105
)
110106
{
111107
psi::Psi<std::complex<double>, base_device::DEVICE_CPU> psi_cpu_double(*psi);
112108

113-
this->generateTrainData_KS(dir, &psi_cpu_double, pelec, pw_psi, pw_rho, ucell, veff);
109+
this->generateTrainData_KS(dir, &psi_cpu_double, pelec, pw_psi, pw_rho, ucell, veff, nrxx);
114110
}
115111
#endif
116112

117113
void Write_MLKEDF_Descriptors::generate_descriptor(
118114
const std::string& out_dir,
119115
const double * const *prho,
120116
ModulePW::PW_Basis *pw_rho,
121-
std::vector<std::vector<double>> &nablaRho
117+
std::vector<std::vector<double>> &nablaRho,
118+
const int nrxx
122119
)
123120
{
124121
// container which will contain gamma, p, q in turn
125-
std::vector<double> container(this->cal_tool->nx);
126-
std::vector<double> new_container(this->cal_tool->nx);
122+
std::vector<double> container(nrxx);
123+
std::vector<double> new_container(nrxx);
127124
// container contains gammanl, pnl, qnl in turn
128-
std::vector<double> containernl(this->cal_tool->nx);
129-
std::vector<double> new_containernl(this->cal_tool->nx);
125+
std::vector<double> containernl(nrxx);
126+
std::vector<double> new_containernl(nrxx);
130127

131-
const long unsigned cshape[] = {(long unsigned) this->cal_tool->nx}; // shape of container and containernl
128+
const long unsigned cshape[] = {(long unsigned) nrxx};
132129

133130
// rho
134-
std::vector<double> rho(this->cal_tool->nx);
135-
for (int ir = 0; ir < this->cal_tool->nx; ++ir){
131+
std::vector<double> rho(nrxx);
132+
for (int ir = 0; ir < nrxx; ++ir){
136133
rho[ir] = prho[0][ir];
137134
}
138135
npy::SaveArrayAsNumpy(out_dir + "/rho.npy", false, 1, cshape, rho);
@@ -236,4 +233,4 @@ std::string Write_MLKEDF_Descriptors::file_name(
236233

237234
}
238235

239-
#endif
236+
#endif

source/source_io/module_ml/write_mlkedf_descriptors.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class Write_MLKEDF_Descriptors
3030
ModulePW::PW_Basis_K *pw_psi,
3131
ModulePW::PW_Basis *pw_rho,
3232
UnitCell& ucell,
33-
const double *veff
33+
const double *veff,
34+
const int nrxx
3435
);
3536
void generateTrainData_KS(
3637
const std::string& out_dir,
@@ -39,7 +40,8 @@ class Write_MLKEDF_Descriptors
3940
ModulePW::PW_Basis_K *pw_psi,
4041
ModulePW::PW_Basis *pw_rho,
4142
UnitCell& ucell,
42-
const double *veff
43+
const double *veff,
44+
const int nrxx
4345
);
4446

4547
#if ((defined __CUDA) || (defined __ROCM))
@@ -50,7 +52,8 @@ class Write_MLKEDF_Descriptors
5052
ModulePW::PW_Basis_K *pw_psi,
5153
ModulePW::PW_Basis *pw_rho,
5254
UnitCell& ucell,
53-
const double *veff
55+
const double *veff,
56+
const int nrxx
5457
);
5558
void generateTrainData_KS(
5659
const std::string& dir,
@@ -59,15 +62,17 @@ class Write_MLKEDF_Descriptors
5962
ModulePW::PW_Basis_K *pw_psi,
6063
ModulePW::PW_Basis *pw_rho,
6164
UnitCell& ucell,
62-
const double *veff
65+
const double *veff,
66+
const int nrxx
6367
);
6468
#endif
6569

6670
void generate_descriptor(
6771
const std::string& out_dir,
6872
const double * const *prho,
6973
ModulePW::PW_Basis *pw_rho,
70-
std::vector<std::vector<double>> &nablaRho
74+
std::vector<std::vector<double>> &nablaRho,
75+
const int nrxx
7176
);
7277

7378
std::string file_name(

source/source_pw/module_ofdft/ml_base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ void ML_Base::dump_vector(std::string filename, const std::vector<double> &data)
285285
{
286286
npy::npy_data_ptr<double> d;
287287
d.data_ptr = data.data();
288-
d.shape = {(long unsigned) this->cal_tool->nx};
288+
d.shape = {(long unsigned) this->cal_tool->nrxx};
289289
d.fortran_order = false;
290290
npy::write_npy(filename, d);
291291
}

0 commit comments

Comments
 (0)