Skip to content

Commit 118485b

Browse files
author
dyzheng
committed
Refactor: replace raw pointers to vector in atom_spec.h
1 parent d581a97 commit 118485b

File tree

72 files changed

+522
-1654
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+522
-1654
lines changed

docs/advanced/input_files/input-main.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2925,7 +2925,7 @@ These variables are used to control DFT+U correlated parameters
29252925

29262926
- where $\gamma$ is a parameter that adjusts the relative weight of the error function to the derivative error function.
29272927
- **Unit**: Bohr
2928-
- **Default**: 5.0
2928+
- **Default**: 3.0
29292929

29302930
[back to top](#full-list-of-input-keywords)
29312931

source/Makefile.Objects

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ OBJS_HAMILT_LCAO=hamilt_lcao.o\
319319
op_dftu_lcao.o\
320320
deepks_lcao.o\
321321
op_exx_lcao.o\
322-
sc_lambda_lcao.o\
322+
dspin_lcao.o\
323323
dftu_lcao.o\
324324

325325
OBJS_HCONTAINER=base_matrix.o\
@@ -697,12 +697,10 @@ OBJS_DFTU=dftu.o\
697697

698698
OBJS_DELTASPIN=basic_funcs.o\
699699
cal_mw_from_lambda.o\
700-
cal_mw_helper.o\
701700
cal_mw.o\
702701
init_sc.o\
703702
lambda_loop_helper.o\
704703
lambda_loop.o\
705-
sc_parse_json.o\
706704
spin_constrain.o\
707705
template_helpers.o\
708706

source/module_cell/atom_spec.cpp

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,20 @@ Atom::Atom()
1313
type = 0;
1414
stapos_wf = 0;
1515
mass = 0.0;
16-
tau = new ModuleBase::Vector3<double>[1];
17-
dis = new ModuleBase::Vector3<double>[1];
18-
taud = new ModuleBase::Vector3<double>[1];
19-
vel = new ModuleBase::Vector3<double>[1];
20-
mag = new double[1];
21-
angle1 = new double[1];
22-
angle2 = new double[1];
23-
m_loc_ = new ModuleBase::Vector3<double>[1];
24-
l_nchi = new int[1];
25-
iw2l = new int[1];
26-
iw2n = new int[1];
27-
iw2m = new int[1];
28-
iw2_ylm = new int[1];
29-
iw2_new = new bool[1];
30-
mbl = new ModuleBase::Vector3<int>[1];
3116
}
3217

3318
Atom::~Atom()
3419
{
35-
delete[] tau;
36-
delete[] dis;
37-
delete[] taud;
38-
delete[] vel;
39-
delete[] mag;
40-
delete[] angle1;
41-
delete[] angle2;
42-
delete[] m_loc_;
43-
delete[] l_nchi;
44-
delete[] iw2l;
45-
delete[] iw2n;
46-
delete[] iw2m;
47-
delete[] iw2_ylm;
48-
delete[] iw2_new;
49-
delete[] mbl;
5020
}
5121

5222
void Atom::set_index(void)
5323
{
5424
assert(nw != 0);
55-
delete[] iw2l;
56-
delete[] iw2n;
57-
delete[] iw2m;
58-
delete[] iw2_ylm;
59-
delete[] iw2_new;
60-
iw2l = new int[nw];
61-
iw2n = new int[nw];
62-
iw2m = new int[nw];
63-
iw2_ylm = new int[nw];
64-
iw2_new = new bool[nw];
25+
this->iw2l.resize(nw, 0);
26+
this->iw2n.resize(nw, 0);
27+
this->iw2m.resize(nw, 0);
28+
this->iw2_ylm.resize(nw, 0);
29+
this->iw2_new.resize(nw, false); // bool array to check if the local orbital is new
6530

6631
int index = 0;
6732
for (int L = 0; L <= nwl; L++)
@@ -103,7 +68,7 @@ void Atom::print_Atom(std::ofstream& ofs)
10368
ModuleBase::GlobalFunc::OUT(ofs, "mass", mass);
10469
ofs << std::endl;
10570

106-
output::printv31_d(ofs, "atom_position(cartesian)", tau, na);
71+
output::printv31_d(ofs, "atom_position(cartesian)", tau.data(), na);
10772
/*
10873
for (int i = 0;i < na;i++)
10974
{
@@ -131,34 +96,26 @@ void Atom::bcast_atom(void)
13196
Parallel_Common::bcast_bool(coulomb_potential);
13297
if (GlobalV::MY_RANK != 0)
13398
{
134-
delete[] l_nchi;
135-
l_nchi = new int[nwl + 1];
99+
this->l_nchi.resize(nwl + 1, 0);
136100
}
137-
Parallel_Common::bcast_int(l_nchi, nwl + 1);
101+
Parallel_Common::bcast_int(l_nchi.data(), nwl + 1);
138102
Parallel_Common::bcast_bool(this->flag_empty_element);
139103
Parallel_Common::bcast_double(mass);
140104

141105
if (GlobalV::MY_RANK != 0)
142106
{
143107
assert(na != 0);
144-
delete[] tau;
145-
delete[] dis;
146-
delete[] taud;
147-
delete[] vel;
148-
delete[] mag;
149-
delete[] angle1;
150-
delete[] angle2;
151-
delete[] m_loc_;
152-
delete[] mbl;
153-
tau = new ModuleBase::Vector3<double>[na];
154-
dis = new ModuleBase::Vector3<double>[na];
155-
taud = new ModuleBase::Vector3<double>[na];
156-
vel = new ModuleBase::Vector3<double>[na];
157-
mag = new double[na];
158-
angle1 = new double[na];
159-
angle2 = new double[na];
160-
m_loc_ = new ModuleBase::Vector3<double>[na];
161-
mbl = new ModuleBase::Vector3<int>[na];
108+
this->tau.resize(na, ModuleBase::Vector3<double>(0, 0, 0));
109+
this->dis.resize(na, ModuleBase::Vector3<double>(0, 0, 0));
110+
this->taud.resize(na, ModuleBase::Vector3<double>(0, 0, 0));
111+
this->vel.resize(na, ModuleBase::Vector3<double>(0, 0, 0));
112+
this->mag.resize(na, 0);
113+
this->angle1.resize(na, 0);
114+
this->angle2.resize(na, 0);
115+
this->m_loc_.resize(na, ModuleBase::Vector3<double>(0, 0, 0));
116+
this->mbl.resize(na, ModuleBase::Vector3<int>(0, 0, 0));
117+
this->lambda.resize(na, ModuleBase::Vector3<double>(0, 0, 0));
118+
this->constrain.resize(na, ModuleBase::Vector3<int>(0, 0, 0));
162119
}
163120

164121
for (int i = 0; i < na; i++)
@@ -184,6 +141,12 @@ void Atom::bcast_atom(void)
184141
Parallel_Common::bcast_int(mbl[i].x);
185142
Parallel_Common::bcast_int(mbl[i].y);
186143
Parallel_Common::bcast_int(mbl[i].z);
144+
Parallel_Common::bcast_double(lambda[i].x);
145+
Parallel_Common::bcast_double(lambda[i].y);
146+
Parallel_Common::bcast_double(lambda[i].z);
147+
Parallel_Common::bcast_int(constrain[i].x);
148+
Parallel_Common::bcast_int(constrain[i].y);
149+
Parallel_Common::bcast_int(constrain[i].z);
187150
}
188151

189152
return;

source/module_cell/atom_spec.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class Atom
1313

1414
Atom_pseudo ncpp;
1515
double mass; // the mass of atom
16-
ModuleBase::Vector3<int> *mbl; // whether the atoms can move or not
16+
std::vector<ModuleBase::Vector3<int>> mbl; // whether the atoms can move or not
1717
bool flag_empty_element = false; // whether is the empty element for bsse. Peize Lin add 2021.04.07
1818

19-
int *iw2m; // use iw to find m
20-
int *iw2n; // use iw to find n
21-
int *iw2l; // use iw to find L
22-
int *iw2_ylm;
23-
bool *iw2_new;
19+
std::vector<int> iw2m; // use iw to find m
20+
std::vector<int> iw2n; // use iw to find n
21+
std::vector<int> iw2l; // use iw to find L
22+
std::vector<int> iw2_ylm;
23+
std::vector<bool> iw2_new;
2424
int nw; // number of local orbitals (l,n,m) of this type
2525

2626
void set_index(void);
@@ -30,23 +30,23 @@ class Atom
3030

3131
int nwl; // max L(Angular momentum) (for local basis)
3232
double Rcut; //pengfei Li 16-2-29
33-
int *l_nchi; // number of chi for each L
33+
std::vector<int> l_nchi; // number of chi for each L
3434
int stapos_wf; // start position of wave functions
3535

3636
std::string label; // atomic symbol
37-
ModuleBase::Vector3<double> *tau;// Cartesian coordinates of each atom in this type.
38-
ModuleBase::Vector3<double> *dis;// direct displacements of each atom in this type in current step liuyu modift 2023-03-22
39-
ModuleBase::Vector3<double> *taud;// Direct coordinates of each atom in this type.
40-
ModuleBase::Vector3<double> *vel;// velocities of each atom in this type.
41-
ModuleBase::Vector3<double> *force; // force acting on each atom in this type.
42-
ModuleBase::Vector3<double> *lambda; // Lagrange multiplier for each atom in this type. used in deltaspin
43-
ModuleBase::Vector3<int> *constrain; // constrain for each atom in this type. used in deltaspin
37+
std::vector<ModuleBase::Vector3<double>> tau;// Cartesian coordinates of each atom in this type.
38+
std::vector<ModuleBase::Vector3<double>> dis;// direct displacements of each atom in this type in current step liuyu modift 2023-03-22
39+
std::vector<ModuleBase::Vector3<double>> taud;// Direct coordinates of each atom in this type.
40+
std::vector<ModuleBase::Vector3<double>> vel;// velocities of each atom in this type.
41+
std::vector<ModuleBase::Vector3<double>> force; // force acting on each atom in this type.
42+
std::vector<ModuleBase::Vector3<double>> lambda; // Lagrange multiplier for each atom in this type. used in deltaspin
43+
std::vector<ModuleBase::Vector3<int>> constrain; // constrain for each atom in this type. used in deltaspin
4444
std::string label_orb; // atomic Element symbol in the orbital file of lcao
4545

46-
double* mag;
47-
double* angle1;//spin angle, added by zhengdy-soc
48-
double* angle2;
49-
ModuleBase::Vector3<double> *m_loc_;
46+
std::vector<double> mag;
47+
std::vector<double> angle1;//spin angle, added by zhengdy-soc
48+
std::vector<double> angle2;
49+
std::vector<ModuleBase::Vector3<double>> m_loc_;
5050
// Coulomb potential v(r) = z/r
5151
// It is a local potentail, and has no non-local potential parts.
5252
bool coulomb_potential = false;

source/module_cell/module_neighbor/test/prepare_unitcell.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,34 +138,25 @@ class UcellTestPrepare
138138
ucell->atoms[it].label = this->elements[it];
139139
ucell->atoms[it].nw = 0;
140140
ucell->atoms[it].nwl = 2;
141-
delete[] ucell->atoms[it].l_nchi;
142-
ucell->atoms[it].l_nchi = new int[ ucell->atoms[it].nwl+1];
141+
ucell->atoms[it].l_nchi.resize(ucell->atoms[it].nwl+1);
143142
for(int L=0; L<ucell->atoms[it].nwl+1; L++)
144143
{
145144
ucell->atoms[it].l_nchi[L] = 1;
146145
ucell->atoms[it].nw += (2*L + 1) * ucell->atoms[it].l_nchi[L];
147146
}
148147
ucell->atoms[it].na = this->natom[it];
149148
//coordinates and related physical quantities
150-
delete[] ucell->atoms[it].tau;
151-
delete[] ucell->atoms[it].dis;
152-
delete[] ucell->atoms[it].taud;
153-
delete[] ucell->atoms[it].vel;
154-
delete[] ucell->atoms[it].mag;
155-
delete[] ucell->atoms[it].angle1;
156-
delete[] ucell->atoms[it].angle2;
157-
delete[] ucell->atoms[it].m_loc_;
158-
delete[] ucell->atoms[it].mbl;
159-
ucell->atoms[it].tau = new ModuleBase::Vector3<double>[ucell->atoms[it].na];
160-
ucell->atoms[it].dis = new ModuleBase::Vector3<double>[ucell->atoms[it].na];
161-
ucell->atoms[it].taud = new ModuleBase::Vector3<double>[ucell->atoms[it].na];
162-
ucell->atoms[it].vel = new ModuleBase::Vector3<double>[ucell->atoms[it].na];
163-
ucell->atoms[it].mag = new double[ucell->atoms[it].na];
164-
ucell->atoms[it].angle1 = new double[ucell->atoms[it].na];
165-
ucell->atoms[it].angle2 = new double[ucell->atoms[it].na];
166-
ucell->atoms[it].m_loc_ = new ModuleBase::Vector3<double>[ucell->atoms[it].na];
167-
ucell->atoms[it].mbl = new ModuleBase::Vector3<int>[ucell->atoms[it].na];
149+
ucell->atoms[it].tau.resize(ucell->atoms[it].na);
150+
ucell->atoms[it].dis.resize(ucell->atoms[it].na);
151+
ucell->atoms[it].taud.resize(ucell->atoms[it].na);
152+
ucell->atoms[it].vel.resize(ucell->atoms[it].na);
153+
ucell->atoms[it].mag.resize(ucell->atoms[it].na);
154+
ucell->atoms[it].angle1.resize(ucell->atoms[it].na);
155+
ucell->atoms[it].angle2.resize(ucell->atoms[it].na);
156+
ucell->atoms[it].m_loc_.resize(ucell->atoms[it].na);
157+
ucell->atoms[it].mbl.resize(ucell->atoms[it].na);
168158
ucell->atoms[it].mass = ucell->atom_mass[it]; // mass set here
159+
169160
for(int ia=0; ia<ucell->atoms[it].na; ++ia)
170161
{
171162
if (ucell->Coordinate == "Direct")

source/module_cell/module_symmetry/test/symmetry_test.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ void SymmetryTest::construct_ucell(stru_ &stru)
2525
{
2626
ucell.atoms[i].label = coord[i].atomname;
2727
ucell.atoms[i].na = coord[i].coordinate.size();
28-
ucell.atoms[i].tau = new ModuleBase::Vector3<double>[ucell.atoms[i].na];
29-
ucell.atoms[i].taud = new ModuleBase::Vector3<double>[ucell.atoms[i].na];
28+
ucell.atoms[i].tau.resize(ucell.atoms[i].na);
29+
ucell.atoms[i].taud.resize(ucell.atoms[i].na);
3030
for (int j = 0; j < ucell.atoms[i].na; j++)
3131
{
3232
std::vector<double> this_atom = coord[i].coordinate[j];
@@ -76,10 +76,5 @@ void SymmetryTest::construct_ucell(stru_ &stru)
7676

7777
void SymmetryTest::ClearUcell()
7878
{
79-
for (int i = 0; i < ucell.ntype; i++)
80-
{
81-
delete[] ucell.atoms[i].tau;
82-
delete[] ucell.atoms[i].taud;
83-
}
8479
delete[] ucell.atoms;
8580
}

source/module_cell/read_atoms.cpp

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ int UnitCell::read_atom_species(std::ifstream &ifa, std::ofstream &ofs_running)
102102
&&(PARAM.inp.psi_initializer)
103103
&&(PARAM.inp.init_wfc.substr(0, 3) == "nao")
104104
)
105+
|| PARAM.inp.onsite_radius > 0.0
105106
)
106107
{
107108
if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "NUMERICAL_ORBITAL") )
@@ -468,8 +469,7 @@ bool UnitCell::read_atom_positions(std::ifstream &ifpos, std::ofstream &ofs_runn
468469
{
469470
this->atoms[it].nwl = lmaxmax;
470471
}
471-
delete[] this->atoms[it].l_nchi;
472-
this->atoms[it].l_nchi = new int[ this->atoms[it].nwl+1];
472+
this->atoms[it].l_nchi.resize(this->atoms[it].nwl+1, 0);
473473
for(int L=0; L<atoms[it].nwl+1; L++)
474474
{
475475
this->atoms[it].l_nchi[L] = 1;
@@ -506,30 +506,17 @@ bool UnitCell::read_atom_positions(std::ifstream &ifpos, std::ofstream &ofs_runn
506506
}
507507
if (na > 0)
508508
{
509-
delete[] atoms[it].tau;
510-
delete[] atoms[it].dis;
511-
delete[] atoms[it].taud;
512-
delete[] atoms[it].vel;
513-
delete[] atoms[it].mbl;
514-
delete[] atoms[it].mag;
515-
delete[] atoms[it].angle1;
516-
delete[] atoms[it].angle2;
517-
delete[] atoms[it].m_loc_;
518-
delete[] atoms[it].lambda;
519-
delete[] atoms[it].constrain;
520-
atoms[it].tau = new ModuleBase::Vector3<double>[na];
521-
atoms[it].dis = new ModuleBase::Vector3<double>[na];
522-
atoms[it].taud = new ModuleBase::Vector3<double>[na];
523-
atoms[it].vel = new ModuleBase::Vector3<double>[na];
524-
atoms[it].mbl = new ModuleBase::Vector3<int>[na];
525-
atoms[it].mag = new double[na];
526-
atoms[it].angle1 = new double[na];
527-
atoms[it].angle2 = new double[na];
528-
atoms[it].m_loc_ = new ModuleBase::Vector3<double>[na];
529-
atoms[it].mass = this->atom_mass[it]; //mohan add 2011-11-07
530-
atoms[it].lambda = new ModuleBase::Vector3<double>[na];
531-
atoms[it].constrain = new ModuleBase::Vector3<int>[na];
532-
ModuleBase::GlobalFunc::ZEROS(atoms[it].mag,na);
509+
atoms[it].tau.resize(na, ModuleBase::Vector3<double>(0,0,0));
510+
atoms[it].dis.resize(na, ModuleBase::Vector3<double>(0,0,0));
511+
atoms[it].taud.resize(na, ModuleBase::Vector3<double>(0,0,0));
512+
atoms[it].vel.resize(na, ModuleBase::Vector3<double>(0,0,0));
513+
atoms[it].mbl.resize(na, ModuleBase::Vector3<int>(0,0,0));
514+
atoms[it].mag.resize(na, 0);
515+
atoms[it].angle1.resize(na, 0);
516+
atoms[it].angle2.resize(na, 0);
517+
atoms[it].m_loc_.resize(na, ModuleBase::Vector3<double>(0,0,0));
518+
atoms[it].lambda.resize(na, ModuleBase::Vector3<double>(0,0,0));
519+
atoms[it].constrain.resize(na, ModuleBase::Vector3<int>(0,0,0));
533520
for (int ia = 0;ia < na; ia++)
534521
{
535522
// modify the reading of frozen ions and velocities -- Yuanbo Li 2021/8/20
@@ -681,12 +668,11 @@ bool UnitCell::read_atom_positions(std::ifstream &ifpos, std::ofstream &ofs_runn
681668
{
682669
if(PARAM.inp.noncolin)
683670
{
684-
//if magnetization only along z-axis, default settings are DOMAG_Z=true and DOMAG=false
685671
if(input_angle_mag)
686672
{
687673
atoms[it].m_loc_[ia].z = atoms[it].mag[ia] *
688674
cos(atoms[it].angle1[ia]);
689-
if(sin(atoms[it].angle1[ia]) > 1e-10 )
675+
if(std::abs(sin(atoms[it].angle1[ia])) > 1e-10 )
690676
{
691677
atoms[it].m_loc_[ia].x = atoms[it].mag[ia] *
692678
sin(atoms[it].angle1[ia]) * cos(atoms[it].angle2[ia]);
@@ -1197,8 +1183,8 @@ void UnitCell::read_orb_file(int it, std::string &orb_file, std::ofstream &ofs_r
11971183
if (strcmp("Lmax", word) == 0)
11981184
{
11991185
ModuleBase::GlobalFunc::READ_VALUE(ifs, atom->nwl);
1200-
delete[] atom->l_nchi;
1201-
atom->l_nchi = new int[ atom->nwl+1];
1186+
1187+
atom->l_nchi.resize(atom->nwl+1, 0);
12021188
}
12031189
assert(atom->nwl<10);
12041190
if (strcmp("Cutoff(a.u.)", word) == 0) // pengfei Li 16-2-29

0 commit comments

Comments
 (0)