Skip to content

Commit 43cde6d

Browse files
Vector instead of New, changed by Linbo Cao (#3975)
* Vector instead of New, changed by Linbo Cao * Vector instead of New changed by Linbo Cao version2
1 parent 4154ec3 commit 43cde6d

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

source/module_io/write_cube.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "module_base/element_name.h"
22
#include "module_io/cube_io.h"
3+
#include<vector>
34

45
void ModuleIO::write_cube(
56
#ifdef __MPI
@@ -126,8 +127,8 @@ void ModuleIO::write_cube(
126127
{
127128
/// for cube file
128129
int nxyz = nx * ny * nz;
129-
double* data_cube = new double[nxyz];
130-
ModuleBase::GlobalFunc::ZEROS(data_cube, nxyz);
130+
std::vector<double> data_cube(nxyz);
131+
ModuleBase::GlobalFunc::ZEROS(data_cube.data(), nxyz);
131132
/// for cube file
132133

133134
// num_z: how many planes on processor 'ip'
@@ -149,8 +150,8 @@ void ModuleIO::write_cube(
149150
}
150151

151152
// which_ip: found iz belongs to which ip.
152-
int *which_ip = new int[nz];
153-
ModuleBase::GlobalFunc::ZEROS(which_ip, nz);
153+
std::vector<int> which_ip(nz);
154+
ModuleBase::GlobalFunc::ZEROS(which_ip.data(), nz);
154155
for(int iz=0; iz<nz; iz++)
155156
{
156157
for(int ip=0; ip<GlobalV::NPROC_IN_POOL; ip++)
@@ -172,14 +173,14 @@ void ModuleIO::write_cube(
172173

173174
int count=0;
174175
int nxy = nx * ny;
175-
double* zpiece = new double[nxy];
176+
std::vector<double> zpiece(nxy);
176177

177178
// save the rho one z by one z.
178179
for(int iz=0; iz<nz; iz++)
179180
{
180181
// std::cout << "\n iz=" << iz << std::endl;
181182
// tag must be different for different iz.
182-
ModuleBase::GlobalFunc::ZEROS(zpiece, nxy);
183+
ModuleBase::GlobalFunc::ZEROS(zpiece.data(), nxy);
183184
int tag = iz;
184185
MPI_Status ierror;
185186

@@ -205,14 +206,14 @@ void ModuleIO::write_cube(
205206
zpiece[ir] = data[ir*nplane+iz-startz_current];
206207
// GlobalV::ofs_running << "\n get zpiece[" << ir << "]=" << zpiece[ir] << " ir*rhopw->nplane+iz=" << ir*rhopw->nplane+iz;
207208
}
208-
MPI_Send(zpiece, nxy, MPI_DOUBLE, 0, tag, POOL_WORLD);
209+
MPI_Send(zpiece.data(), nxy, MPI_DOUBLE, 0, tag, POOL_WORLD);
209210
}
210211

211212
// case 2: > first part rho: processor 0 receive the rho
212213
// from other processors
213214
else if(GlobalV::RANK_IN_POOL==0)
214215
{
215-
MPI_Recv(zpiece, nxy, MPI_DOUBLE, which_ip[iz], tag, POOL_WORLD, &ierror);
216+
MPI_Recv(zpiece.data(), nxy, MPI_DOUBLE, which_ip[iz], tag, POOL_WORLD, &ierror);
216217
// GlobalV::ofs_running << "\n Receieve First number = " << zpiece[0];
217218
}
218219

@@ -226,8 +227,6 @@ void ModuleIO::write_cube(
226227
/// for cube file
227228
}
228229
}// end iz
229-
delete[] zpiece;
230-
delete[] which_ip;
231230
delete[] num_z;
232231
delete[] start_z;
233232
// for cube file
@@ -246,7 +245,6 @@ void ModuleIO::write_cube(
246245
}
247246
}
248247
}
249-
delete[] data_cube;
250248
/// for cube file
251249
}
252250
MPI_Barrier(MPI_COMM_WORLD);

source/module_relax/relax_old/bfgs_basic.cpp

+11-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ions_move_basic.h"
44
#include "module_base/global_function.h"
55
#include "module_base/global_variable.h"
6+
#include<vector>
67
using namespace Ions_Move_Basic;
78

89
double BFGS_Basic::relax_bfgs_w1 = -1.0; // default is 0.01
@@ -66,10 +67,10 @@ void BFGS_Basic::update_inverse_hessian(const double &lat0)
6667
// ModuleBase::TITLE("Ions_Move_BFGS","update_inverse_hessian");
6768
assert(dim > 0);
6869

69-
double *s = new double[dim];
70-
double *y = new double[dim];
71-
ModuleBase::GlobalFunc::ZEROS(s, dim);
72-
ModuleBase::GlobalFunc::ZEROS(y, dim);
70+
std::vector<double> s(dim);
71+
std::vector<double> y(dim);
72+
ModuleBase::GlobalFunc::ZEROS(s.data(), dim);
73+
ModuleBase::GlobalFunc::ZEROS(y.data(), dim);
7374

7475
for (int i = 0; i < dim; i++)
7576
{
@@ -94,12 +95,12 @@ void BFGS_Basic::update_inverse_hessian(const double &lat0)
9495
return;
9596
}
9697

97-
double *Hs = new double[dim];
98-
double *Hy = new double[dim];
99-
double *yH = new double[dim];
100-
ModuleBase::GlobalFunc::ZEROS(Hs, dim);
101-
ModuleBase::GlobalFunc::ZEROS(Hy, dim);
102-
ModuleBase::GlobalFunc::ZEROS(yH, dim);
98+
std::vector<double> Hs(dim);
99+
std::vector<double> Hy(dim);
100+
std::vector<double> yH(dim);
101+
ModuleBase::GlobalFunc::ZEROS(Hs.data(), dim);
102+
ModuleBase::GlobalFunc::ZEROS(Hy.data(), dim);
103+
ModuleBase::GlobalFunc::ZEROS(yH.data(), dim);
103104

104105
for (int i = 0; i < dim; i++)
105106
{
@@ -127,11 +128,6 @@ void BFGS_Basic::update_inverse_hessian(const double &lat0)
127128
}
128129
}
129130

130-
delete[] s;
131-
delete[] y;
132-
delete[] Hs;
133-
delete[] Hy;
134-
delete[] yH;
135131
return;
136132
}
137133

source/module_relax/relax_old/ions_move_sd.cpp

+10-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ions_move_basic.h"
44
#include "module_base/global_function.h"
55
#include "module_base/global_variable.h"
6+
#include<vector>
67

78
using namespace Ions_Move_Basic;
89

@@ -38,18 +39,18 @@ void Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const
3839
assert(grad_saved != 0);
3940
assert(pos_saved != 0);
4041

41-
double* pos = new double[dim];
42-
double* grad = new double[dim];
43-
double* move = new double[dim];
44-
ModuleBase::GlobalFunc::ZEROS(pos, dim);
45-
ModuleBase::GlobalFunc::ZEROS(grad, dim);
46-
ModuleBase::GlobalFunc::ZEROS(move, dim);
42+
std::vector<double> pos(dim);
43+
std::vector<double> grad(dim);
44+
std::vector<double> move(dim);
45+
ModuleBase::GlobalFunc::ZEROS(pos.data(), dim);
46+
ModuleBase::GlobalFunc::ZEROS(grad.data(), dim);
47+
ModuleBase::GlobalFunc::ZEROS(move.data(), dim);
4748

4849
// 1: ediff = 0
4950
// 0: ediff < 0
5051
bool judgement = 0;
5152
setup_etot(etot_in, judgement);
52-
setup_gradient(ucell, force, pos, grad);
53+
setup_gradient(ucell, force, pos.data(), grad.data());
5354

5455
if (istep == 1 || etot_in <= energy_saved)
5556
{
@@ -70,7 +71,7 @@ void Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const
7071
}
7172
}
7273

73-
Ions_Move_Basic::check_converged(ucell, grad);
74+
Ions_Move_Basic::check_converged(ucell, grad.data());
7475
if (Ions_Move_Basic::converged)
7576
{
7677
Ions_Move_Basic::terminate(ucell);
@@ -82,14 +83,10 @@ void Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const
8283
{
8384
move[i] = -grad_saved[i] * trust_radius;
8485
}
85-
move_atoms(ucell, move, pos_saved);
86+
move_atoms(ucell, move.data(), pos_saved);
8687
Ions_Move_Basic::update_iter++;
8788
}
8889

89-
delete[] pos;
90-
delete[] grad;
91-
delete[] move;
92-
9390
return;
9491
}
9592

0 commit comments

Comments
 (0)