Skip to content

Commit

Permalink
Refactor: Replace the pointers in pseudo and Pseudopot_upf with s…
Browse files Browse the repository at this point in the history
…td::vector. (#4862)

* Refactor: Replace the pointers in `pseudo` and `Pseudopot_upf` with std::vector.

* Test: Update corresponding unit tests.

* [pre-commit.ci lite] apply automatic fixes

* Fix: Fix the bug caused by assert(!has_soc)

* Refactor: Remove redundant `clear()` from `pseudo` and `Pseudopot_upf`, since assignment operator will free the original memory of the vector.

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
sunliang98 and pre-commit-ci-lite[bot] authored Aug 3, 2024
1 parent 3e6ab1b commit 0f87658
Show file tree
Hide file tree
Showing 27 changed files with 290 additions and 450 deletions.
81 changes: 26 additions & 55 deletions source/module_cell/atom_pseudo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,10 @@

Atom_pseudo::Atom_pseudo()
{
for (int is = 0; is < 4; is++) {
this->index1_soc[is] = nullptr;
}
for (int is = 0; is < 4; is++) {
this->index2_soc[is] = nullptr;
}
}

Atom_pseudo::~Atom_pseudo()
{
for (int is = 0; is < 4; is++)
{
if (this->index1_soc[is] != nullptr) {
delete[] this->index1_soc[is];
}
if (this->index2_soc[is] != nullptr) {
delete[] this->index2_soc[is];
}
}
}

// mohan add 2021-05-07
Expand All @@ -42,10 +27,8 @@ void Atom_pseudo::set_d_so(ModuleBase::ComplexMatrix& d_so_in,
for (int is = 0; is < spin_dimension; is++)
{
this->non_zero_count_soc[is] = 0;
delete[] this->index1_soc[is];
this->index1_soc[is] = new int[nproj_soc * nproj_soc];
delete[] this->index2_soc[is];
this->index2_soc[is] = new int[nproj_soc * nproj_soc];
this->index1_soc[is] = std::vector<int>(nproj_soc * nproj_soc, 0);
this->index2_soc[is] = std::vector<int>(nproj_soc * nproj_soc, 0);
}

if (!has_so)
Expand Down Expand Up @@ -189,26 +172,20 @@ void Atom_pseudo::bcast_atom_pseudo()

if (GlobalV::MY_RANK != 0)
{
delete[] jjj;
delete[] els;
delete[] lchi;
delete[] oc;
delete[] jchi;
delete[] nn;
jjj = new double[nbeta];
els = new std::string[nchi];
lchi = new int[nchi];
oc = new double[nchi];
jchi = new double[nchi];
nn = new int[nchi];
jjj = std::vector<double>(nbeta, 0.0);
els = std::vector<std::string>(nchi, "");
lchi = std::vector<int>(nchi, 0);
oc = std::vector<double>(nchi, 0.0);
jchi = std::vector<double>(nchi, 0.0);
nn = std::vector<int>(nchi, 0);
}

Parallel_Common::bcast_double(jjj, nbeta);
Parallel_Common::bcast_string(els, nchi);
Parallel_Common::bcast_int(lchi, nchi);
Parallel_Common::bcast_double(oc, nchi);
Parallel_Common::bcast_double(jchi, nchi);
Parallel_Common::bcast_int(nn, nchi);
Parallel_Common::bcast_double(jjj.data(), nbeta);
Parallel_Common::bcast_string(els.data(), nchi);
Parallel_Common::bcast_int(lchi.data(), nchi);
Parallel_Common::bcast_double(oc.data(), nchi);
Parallel_Common::bcast_double(jchi.data(), nchi);
Parallel_Common::bcast_int(nn.data(), nchi);
// == end of pseudo_h

// == pseudo_atom ==
Expand All @@ -217,31 +194,26 @@ void Atom_pseudo::bcast_atom_pseudo()
if (GlobalV::MY_RANK != 0)
{
assert(mesh != 0);
delete[] r;
delete[] rab;
delete[] rho_atc;
delete[] rho_at;
r = new double[mesh];
rab = new double[mesh];
rho_atc = new double[mesh];
rho_at = new double[mesh];
r = std::vector<double>(mesh, 0.0);
rab = std::vector<double>(mesh, 0.0);
rho_atc = std::vector<double>(mesh, 0.0);
rho_at = std::vector<double>(mesh, 0.0);
chi.create(nchi, mesh);
}

Parallel_Common::bcast_double(r, mesh);
Parallel_Common::bcast_double(rab, mesh);
Parallel_Common::bcast_double(rho_atc, mesh);
Parallel_Common::bcast_double(rho_at, mesh);
Parallel_Common::bcast_double(r.data(), mesh);
Parallel_Common::bcast_double(rab.data(), mesh);
Parallel_Common::bcast_double(rho_atc.data(), mesh);
Parallel_Common::bcast_double(rho_at.data(), mesh);
Parallel_Common::bcast_double(chi.c, nchi * mesh);
// == end of pseudo_atom ==

// == pseudo_vl ==
if (GlobalV::MY_RANK != 0)
{
delete[] vloc_at;
vloc_at = new double[mesh];
vloc_at = std::vector<double>(mesh, 0.0);
}
Parallel_Common::bcast_double(vloc_at, mesh);
Parallel_Common::bcast_double(vloc_at.data(), mesh);
// == end of pseudo_vl ==

// == pseudo ==
Expand All @@ -251,10 +223,9 @@ void Atom_pseudo::bcast_atom_pseudo()

if (GlobalV::MY_RANK != 0)
{
delete[] lll;
lll = new int[nbeta];
lll = std::vector<int>(nbeta, 0);
}
Parallel_Common::bcast_int(lll, nbeta);
Parallel_Common::bcast_int(lll.data(), nbeta);
Parallel_Common::bcast_int(kkbeta);
Parallel_Common::bcast_int(nh);

Expand Down
17 changes: 8 additions & 9 deletions source/module_cell/atom_pseudo.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#ifndef ATOM_PSEUDO_H
#define ATOM_PSEUDO_H

#include "../module_base/global_function.h"
#include "../module_base/global_variable.h"
#include "../module_base/vector3.h"
#include "../module_io/output.h"
#include "../module_base/complexarray.h"
#include "../module_base/complexmatrix.h"
#include "module_base/global_variable.h"
#include "module_base/vector3.h"
#include "module_io/output.h"
#include "module_base/complexarray.h"
#include "module_base/complexmatrix.h"
#include "pseudo.h"


Expand All @@ -22,9 +21,9 @@ class Atom_pseudo : public pseudo
ModuleBase::matrix d_real; //(:,:), non-spin-orbit case
int nproj;
int nproj_soc; // dimension of D_ij^so
int non_zero_count_soc[4];
int *index1_soc[4];
int *index2_soc[4];
std::vector<int> non_zero_count_soc = {0, 0, 0, 0};
std::vector<std::vector<int>> index1_soc = {{}, {}, {}, {}};
std::vector<std::vector<int>> index2_soc = {{}, {}, {}, {}};

void set_d_so( // mohan add 2021-05-07
ModuleBase::ComplexMatrix &d_so_in,
Expand Down
30 changes: 9 additions & 21 deletions source/module_cell/pseudo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ pseudo::pseudo()

pseudo::~pseudo()
{
delete[] els;
delete[] lchi;
delete[] oc;
delete[] jjj;
delete[] jchi;
delete[] nn;
delete[] vloc_at;
delete[] r;
delete[] rab;
delete[] rho_at;
delete[] rho_atc;
delete[] lll;
}

void pseudo::print_pseudo(std::ofstream& ofs)
Expand All @@ -27,7 +15,7 @@ void pseudo::print_pseudo(std::ofstream& ofs)
ofs << "\n pseudo : ";
ofs << "\n kkbeta " << kkbeta;
ofs << "\n nh " << nh;
output::printr1_d(ofs, " lll : ", lll, nbeta);
output::printr1_d(ofs, " lll : ", lll.data(), nbeta);
output::printrm(ofs, " betar : ", betar);
output::printrm(ofs, " dion : ", dion);
ofs << "\n ----------------------";
Expand All @@ -39,11 +27,11 @@ void pseudo::print_pseudo_atom(std::ofstream &ofs)
ofs << "\n pseudo_atom : ";
ofs << "\n msh " << msh;
// ofs << "\n nchi " << nchi;
output::printr1_d(ofs, " r : ", r, mesh);
output::printr1_d(ofs, " rab : ", rab, mesh);
output::printr1_d(ofs, " rho_atc : ", rho_atc, mesh);
output::printr1_d(ofs, " rho_at : ", rho_at, mesh);
output::printr1_d(ofs," jchi : ", jchi, nchi);
output::printr1_d(ofs, " r : ", r.data(), mesh);
output::printr1_d(ofs, " rab : ", rab.data(), mesh);
output::printr1_d(ofs, " rho_atc : ", rho_atc.data(), mesh);
output::printr1_d(ofs, " rho_at : ", rho_at.data(), mesh);
output::printr1_d(ofs," jchi : ", jchi.data(), nchi);
output::printrm(ofs, " chi : ", chi);
ofs << "\n ----------------------";
}
Expand All @@ -53,7 +41,7 @@ void pseudo::print_pseudo_vl(std::ofstream &ofs)
{
ofs << "\n pseudo_vl:";
print_pseudo_atom(ofs);
output::printr1_d(ofs, "vloc_at : ", vloc_at, mesh);
output::printr1_d(ofs, "vloc_at : ", vloc_at.data(), mesh);
ofs << "\n ----------------------------------- ";
}

Expand All @@ -75,8 +63,8 @@ void pseudo::print_pseudo_h(std::ofstream &ofs)
ofs << "\n nchi " << nchi;
ofs << "\n nbeta " << nbeta;
// out.printr1_d(ofs," els: ", els, nchi);
output::printr1_d(ofs, " lchi: ", lchi, nchi);
output::printr1_d(ofs, " oc: ", oc, nchi);
output::printr1_d(ofs, " lchi: ", lchi.data(), nchi);
output::printr1_d(ofs, " oc: ", oc.data(), nchi);
ofs << "\n ----------------------";
}

24 changes: 12 additions & 12 deletions source/module_cell/pseudo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ class pseudo
int nqlc = 0; // number of angular momenta in Q
int kkbeta = 0; // kkbeta, point where the beta are zero

std::string* els = nullptr; // els[nchi]
int* lchi = nullptr; // lchi[nchi]
double* oc = nullptr; // oc[nchi]
std::vector<std::string> els = {}; // els[nchi]
std::vector<int> lchi = {}; // lchi[nchi]
std::vector<double> oc = {}; // oc[nchi]

double* jjj = nullptr; // total angual momentum, jjj[nbeta]
double* jchi = nullptr; // jchi(nwfc), added by zhengdy-soc
int* nn = nullptr;
std::vector<double> jjj = {}; // total angual momentum, jjj[nbeta]
std::vector<double> jchi = {}; // jchi(nwfc), added by zhengdy-soc
std::vector<int> nn = {};

// Local pseudopotentials
double* vloc_at = nullptr; // [mesh], local potential( = pseudopot_upf.vloc )
std::vector<double> vloc_at = {}; // [mesh], local potential( = pseudopot_upf.vloc )

// <PP_MESH>
double* r = nullptr; // radial logaritmic mesh, r[0:mesh-1]
double* rab = nullptr; // derivative of the radial mesh, rab[0:mesh-1]
std::vector<double> r = {}; // radial logaritmic mesh, r[0:mesh-1]
std::vector<double> rab = {}; // derivative of the radial mesh, rab[0:mesh-1]

//<PP_NLCC>
double* rho_atc = nullptr; // radial core charge density, rho_atc[0:mesh-1]
std::vector<double> rho_atc = {}; // radial core charge density, rho_atc[0:mesh-1]

//<PP_RHOATOM>
double* rho_at = nullptr; // radial atomic charge density, rho_at[0:mesh-1]
std::vector<double> rho_at = {}; // radial atomic charge density, rho_at[0:mesh-1]

// <PP_PSWFC>
ModuleBase::matrix chi; // radial atomic orbitals, chi(nchi, mesh)
Expand All @@ -62,7 +62,7 @@ class pseudo
double rcut = 0.0; // cut-off radius

// <PP_BETA>
int* lll = nullptr; // lll(nbeta), angular momentum of the beta function
std::vector<int> lll = {}; // lll(nbeta), angular momentum of the beta function

// <PP_DIJ>
ModuleBase::matrix dion; // dion(nbeta,nbeta)
Expand Down
11 changes: 1 addition & 10 deletions source/module_cell/read_pp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ Pseudopot_upf::Pseudopot_upf()

Pseudopot_upf::~Pseudopot_upf()
{
delete[] kbeta;
delete[] els_beta;
delete[] nchi;
delete[] epseu;
delete[] rcut_chi;
delete[] rcutus_chi;
delete[] rinner;
delete[] rcut;
delete[] rcutus;
}

int Pseudopot_upf::init_pseudo_reader(const std::string &fn, std::string &type, Atom_pseudo& pp)
Expand Down Expand Up @@ -460,7 +451,7 @@ void Pseudopot_upf::set_upf_q(Atom_pseudo& pp)
break;
}
}
this->setqfnew(nqf, ilast, l, 2, &(qfcoef(nb, mb, l, 0)), pp.r, &(pp.qfuncl(l, nmb, 0)));
this->setqfnew(nqf, ilast, l, 2, &(qfcoef(nb, mb, l, 0)), pp.r.data(), &(pp.qfuncl(l, nmb, 0)));
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions source/module_cell/read_pp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
#include <string>

#include "atom_pseudo.h"
#include "module_base/global_function.h"
#include "module_base/global_variable.h"
#include "module_base/matrix.h"
#include "module_base/realarray.h"
#include "module_io/output.h"

class Pseudopot_upf
{
Expand Down Expand Up @@ -43,19 +40,19 @@ class Pseudopot_upf
// need 'new' and 'delete'
bool coulomb_potential = false; // coulomb potentail : z/r
ModuleBase::matrix chi; // chi(nwfc,mesh) atomic wavefcts
int* kbeta = nullptr; // kbeta(nbeta):number of mesh points for projector i (must be .le. mesh )
std::string* els_beta = nullptr; // els_beta(nwfc):label for the beta
int* nchi = nullptr; // nchi(nwfc) value of pseudo-n for wavefcts
double* epseu = nullptr; // epseu(nwfc) pseudo one-particle energy
double* rcut_chi = nullptr; // rcut_chi(nwfc) cutoff inner radius
double* rcutus_chi = nullptr; // rcutus_chi(nwfc) ultrasoft outer radius
double* rinner = nullptr; // rinner(2*lmax+1) r_L
std::vector<int> kbeta = {}; // kbeta(nbeta):number of mesh points for projector i (must be .le. mesh )
std::vector<std::string> els_beta = {}; // els_beta(nwfc):label for the beta
std::vector<int> nchi = {}; // nchi(nwfc) value of pseudo-n for wavefcts
std::vector<double> epseu = {}; // epseu(nwfc) pseudo one-particle energy
std::vector<double> rcut_chi = {}; // rcut_chi(nwfc) cutoff inner radius
std::vector<double> rcutus_chi = {}; // rcutus_chi(nwfc) ultrasoft outer radius
std::vector<double> rinner = {}; // rinner(2*lmax+1) r_L
ModuleBase::matrix qfunc; // qfunc(nbeta*(nbeta+1)/2,mesh) Q_{mu,nu}(|r|) function for |r|> r_L
ModuleBase::realArray qfcoef; // qfcoef(nbeta,nbeta,2*lmax+1,nqf) coefficients for Q for |r|<r_L
// ModuleBase::matrix aewfc; // wfc(nbeta,mesh) all-electron wfc
// ModuleBase::matrix pswfc; // wfc(nbeta,mesh) pseudo wfc
double* rcut = nullptr; // cut-off radius(nbeta)
double* rcutus = nullptr; // ultrasoft cut-off radius (nbeta)
std::vector<double> rcut = {}; // cut-off radius(nbeta)
std::vector<double> rcutus = {}; // ultrasoft cut-off radius (nbeta)

int nd; // nl_5 // Number of nonzero Dij

Expand Down
Loading

0 comments on commit 0f87658

Please sign in to comment.