Skip to content

Commit

Permalink
Change pointers to vectors (#3980)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DylanWRh committed Apr 16, 2024
1 parent 2ffb4e2 commit 324e36f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
26 changes: 13 additions & 13 deletions source/module_base/cubic_spline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cmath>
#include <cstring>
#include <functional>
#include <vector>

namespace ModuleBase
{
Expand Down Expand Up @@ -124,13 +125,18 @@ void CubicSpline::build(const int n,
}
else
{
double* dx = new double[n - 1];
double* dd = new double[n - 1];
// double* dx = new double[n - 1];
// double* dd = new double[n - 1];
std::vector<double> dx(n - 1);
std::vector<double> dd(n - 1);

// tridiagonal linear system (cyclic if using the periodic boundary condition)
double* diag = new double[n];
double* subdiag = new double[n - 1];
double* supdiag = new double[n - 1];
// double* diag = new double[n];
// double* subdiag = new double[n - 1];
// double* supdiag = new double[n - 1];
std::vector<double> diag(n);
std::vector<double> subdiag(n - 1);
std::vector<double> supdiag(n - 1);

for (int i = 0; i != n - 1; ++i)
{
Expand All @@ -154,7 +160,7 @@ void CubicSpline::build(const int n,
supdiag[0] = dx[n - 2];
subdiag[n - 2] = dx[0];
s[0] = 3.0 * (dd[0] * dx[n - 2] + dd[n - 2] * dx[0]);
solve_cyctri(n - 1, diag, supdiag, subdiag, s);
solve_cyctri(n - 1, diag.data(), supdiag.data(), subdiag.data(), s);
s[n - 1] = s[0];
}
else
Expand Down Expand Up @@ -202,14 +208,8 @@ void CubicSpline::build(const int n,
int INFO = 0;
int N = n;

dgtsv_(&N, &NRHS, subdiag, diag, supdiag, s, &LDB, &INFO);
dgtsv_(&N, &NRHS, subdiag.data(), diag.data(), supdiag.data(), s, &LDB, &INFO);
}

delete[] diag;
delete[] subdiag;
delete[] supdiag;
delete[] dx;
delete[] dd;
}
}

Expand Down
14 changes: 5 additions & 9 deletions source/module_base/math_ylmreal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "tool_quit.h"
#include "realarray.h"
#include <cassert>
#include <vector>
#include "ylm.h"
#include "module_base/kernels/math_op.h"
#include "module_psi/kernels/memory_op.h"
Expand Down Expand Up @@ -273,20 +274,18 @@ void YlmReal::Ylm_Real2
//----------------------------------------------------------
// Start CALC
//----------------------------------------------------------
double* rly = new double[lmax2];
std::vector<double> rly(lmax2);

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

for (int lm = 0; lm < lmax2; lm++)
{
ylm (lm, ig) = rly[lm];
}
}

delete [] rly;

return;
}

Expand Down Expand Up @@ -405,8 +404,8 @@ void YlmReal::Ylm_Real
// NAME : cost = cos(theta),theta and phi are polar angles
// NAME : phi
//----------------------------------------------------------
double *cost = new double[ng];
double *phi = new double[ng];
std::vector <double> cost(ng);
std::vector <double> phi(ng);

#ifdef _OPENMP
#pragma omp parallel for
Expand Down Expand Up @@ -613,9 +612,6 @@ void YlmReal::Ylm_Real
*/


delete [] cost;
delete [] phi;

return;
} // end subroutine ylmr2

Expand Down
6 changes: 3 additions & 3 deletions source/module_base/opt_CG.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "opt_CG.h"
#include <vector>

namespace ModuleBase
{
Expand Down Expand Up @@ -172,20 +173,19 @@ void Opt_CG::stantard_CGdirect(double* pAd, double* rdirect)
}
else
{
double* temp_gradient = new double[this->nx_];
std::vector<double> temp_gradient(this->nx_);
for (int i = 0; i < this->nx_; ++i)
{
temp_gradient[i] = this->pgradient_old_[i] + this->alpha_ * pAd[i];
}
this->beta_ = this->inner_product(temp_gradient, temp_gradient, this->nx_) / this->gg_;
this->beta_ = this->inner_product(temp_gradient.data(), temp_gradient.data(), this->nx_) / this->gg_;
Parallel_Reduce::reduce_all(this->beta_);
for (int i = 0; i < this->nx_; ++i)
{
this->pgradient_old_[i] = temp_gradient[i];
rdirect[i] = -this->pgradient_old_[i] + this->beta_ * this->pdirect_old_[i];
this->pdirect_old_[i] = rdirect[i];
}
delete[] temp_gradient;
}
this->gg_ = this->inner_product(this->pgradient_old_, this->pgradient_old_, this->nx_);
Parallel_Reduce::reduce_all(this->gg_);
Expand Down

0 comments on commit 324e36f

Please sign in to comment.