Skip to content

Commit 324e36f

Browse files
authored
Change pointers to vectors (#3980)
* initialize some variables * fix a semicolon * Update sincos.cpp * Update mathzone_add1.cpp * Update mathzone_add1.cpp * Update opt_DCsrch.cpp * Update broyden_mixing.cpp * Update pulay_mixing.cpp * Update math_sphbes_test.cpp * Update ORB_gen_tables.cpp * Update ORB_table_phi.cpp * Update math_sphbes_test.cpp * Change some pointers to vectors * Update formatter_contextfmt_test.cpp * Change pointers to vectors * Rollback * Replace some pointers with vectors * Change some pointers to vectors
1 parent 2ffb4e2 commit 324e36f

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

source/module_base/cubic_spline.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cmath>
66
#include <cstring>
77
#include <functional>
8+
#include <vector>
89

910
namespace ModuleBase
1011
{
@@ -124,13 +125,18 @@ void CubicSpline::build(const int n,
124125
}
125126
else
126127
{
127-
double* dx = new double[n - 1];
128-
double* dd = new double[n - 1];
128+
// double* dx = new double[n - 1];
129+
// double* dd = new double[n - 1];
130+
std::vector<double> dx(n - 1);
131+
std::vector<double> dd(n - 1);
129132

130133
// tridiagonal linear system (cyclic if using the periodic boundary condition)
131-
double* diag = new double[n];
132-
double* subdiag = new double[n - 1];
133-
double* supdiag = new double[n - 1];
134+
// double* diag = new double[n];
135+
// double* subdiag = new double[n - 1];
136+
// double* supdiag = new double[n - 1];
137+
std::vector<double> diag(n);
138+
std::vector<double> subdiag(n - 1);
139+
std::vector<double> supdiag(n - 1);
134140

135141
for (int i = 0; i != n - 1; ++i)
136142
{
@@ -154,7 +160,7 @@ void CubicSpline::build(const int n,
154160
supdiag[0] = dx[n - 2];
155161
subdiag[n - 2] = dx[0];
156162
s[0] = 3.0 * (dd[0] * dx[n - 2] + dd[n - 2] * dx[0]);
157-
solve_cyctri(n - 1, diag, supdiag, subdiag, s);
163+
solve_cyctri(n - 1, diag.data(), supdiag.data(), subdiag.data(), s);
158164
s[n - 1] = s[0];
159165
}
160166
else
@@ -202,14 +208,8 @@ void CubicSpline::build(const int n,
202208
int INFO = 0;
203209
int N = n;
204210

205-
dgtsv_(&N, &NRHS, subdiag, diag, supdiag, s, &LDB, &INFO);
211+
dgtsv_(&N, &NRHS, subdiag.data(), diag.data(), supdiag.data(), s, &LDB, &INFO);
206212
}
207-
208-
delete[] diag;
209-
delete[] subdiag;
210-
delete[] supdiag;
211-
delete[] dx;
212-
delete[] dd;
213213
}
214214
}
215215

source/module_base/math_ylmreal.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "tool_quit.h"
55
#include "realarray.h"
66
#include <cassert>
7+
#include <vector>
78
#include "ylm.h"
89
#include "module_base/kernels/math_op.h"
910
#include "module_psi/kernels/memory_op.h"
@@ -273,20 +274,18 @@ void YlmReal::Ylm_Real2
273274
//----------------------------------------------------------
274275
// Start CALC
275276
//----------------------------------------------------------
276-
double* rly = new double[lmax2];
277+
std::vector<double> rly(lmax2);
277278

278279
for (int ig = 0; ig < ng; ig++)
279280
{
280-
rlylm (lmax, g[ig].x, g[ig].y, g[ig].z, rly);
281+
rlylm (lmax, g[ig].x, g[ig].y, g[ig].z, rly.data());
281282

282283
for (int lm = 0; lm < lmax2; lm++)
283284
{
284285
ylm (lm, ig) = rly[lm];
285286
}
286287
}
287288

288-
delete [] rly;
289-
290289
return;
291290
}
292291

@@ -405,8 +404,8 @@ void YlmReal::Ylm_Real
405404
// NAME : cost = cos(theta),theta and phi are polar angles
406405
// NAME : phi
407406
//----------------------------------------------------------
408-
double *cost = new double[ng];
409-
double *phi = new double[ng];
407+
std::vector <double> cost(ng);
408+
std::vector <double> phi(ng);
410409

411410
#ifdef _OPENMP
412411
#pragma omp parallel for
@@ -613,9 +612,6 @@ void YlmReal::Ylm_Real
613612
*/
614613

615614

616-
delete [] cost;
617-
delete [] phi;
618-
619615
return;
620616
} // end subroutine ylmr2
621617

source/module_base/opt_CG.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "opt_CG.h"
2+
#include <vector>
23

34
namespace ModuleBase
45
{
@@ -172,20 +173,19 @@ void Opt_CG::stantard_CGdirect(double* pAd, double* rdirect)
172173
}
173174
else
174175
{
175-
double* temp_gradient = new double[this->nx_];
176+
std::vector<double> temp_gradient(this->nx_);
176177
for (int i = 0; i < this->nx_; ++i)
177178
{
178179
temp_gradient[i] = this->pgradient_old_[i] + this->alpha_ * pAd[i];
179180
}
180-
this->beta_ = this->inner_product(temp_gradient, temp_gradient, this->nx_) / this->gg_;
181+
this->beta_ = this->inner_product(temp_gradient.data(), temp_gradient.data(), this->nx_) / this->gg_;
181182
Parallel_Reduce::reduce_all(this->beta_);
182183
for (int i = 0; i < this->nx_; ++i)
183184
{
184185
this->pgradient_old_[i] = temp_gradient[i];
185186
rdirect[i] = -this->pgradient_old_[i] + this->beta_ * this->pdirect_old_[i];
186187
this->pdirect_old_[i] = rdirect[i];
187188
}
188-
delete[] temp_gradient;
189189
}
190190
this->gg_ = this->inner_product(this->pgradient_old_, this->pgradient_old_, this->nx_);
191191
Parallel_Reduce::reduce_all(this->gg_);

0 commit comments

Comments
 (0)