Skip to content

Commit c7e46be

Browse files
dyzhengdyzhengpre-commit-ci-lite[bot]
authored
Refactor: replace raw pointers to vector in atom_spec.h (#5521)
* Refactor: new real space projection DeltaSpin method * Refactor: replace raw pointers to vector in atom_spec.h * Fix: add namespace * Fix: save mass * [pre-commit.ci lite] apply automatic fixes * Fix: error in CI test * Fix: error in CI * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: dyzheng <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 20b2214 commit c7e46be

File tree

107 files changed

+2645
-2901
lines changed

Some content is hidden

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

107 files changed

+2645
-2901
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ OBJS_HAMILT_LCAO=hamilt_lcao.o\
317317
op_dftu_lcao.o\
318318
deepks_lcao.o\
319319
op_exx_lcao.o\
320-
sc_lambda_lcao.o\
320+
dspin_lcao.o\
321321
dftu_lcao.o\
322322

323323
OBJS_HCONTAINER=base_matrix.o\
@@ -696,14 +696,11 @@ OBJS_DFTU=dftu.o\
696696
dftu_hamilt.o
697697

698698
OBJS_DELTASPIN=basic_funcs.o\
699-
cal_h_lambda.o\
700699
cal_mw_from_lambda.o\
701-
cal_mw_helper.o\
702700
cal_mw.o\
703701
init_sc.o\
704702
lambda_loop_helper.o\
705703
lambda_loop.o\
706-
sc_parse_json.o\
707704
spin_constrain.o\
708705
template_helpers.o\
709706

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 & 16 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,21 +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.
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
4244
std::string label_orb; // atomic Element symbol in the orbital file of lcao
4345

44-
double* mag;
45-
double* angle1;//spin angle, added by zhengdy-soc
46-
double* angle2;
47-
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_;
4850
// Coulomb potential v(r) = z/r
4951
// It is a local potentail, and has no non-local potential parts.
5052
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
}

0 commit comments

Comments
 (0)