From 43cde6d9e97cfe32dc43f9a95318b8161b408c78 Mon Sep 17 00:00:00 2001 From: Linbo Cao <165685902+RobertRainbow@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:53:29 +0800 Subject: [PATCH] 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 --- source/module_io/write_cube.cpp | 20 +++++++------- source/module_relax/relax_old/bfgs_basic.cpp | 26 ++++++++----------- .../module_relax/relax_old/ions_move_sd.cpp | 23 +++++++--------- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/source/module_io/write_cube.cpp b/source/module_io/write_cube.cpp index 0acb5da938..7a2b88db46 100644 --- a/source/module_io/write_cube.cpp +++ b/source/module_io/write_cube.cpp @@ -1,5 +1,6 @@ #include "module_base/element_name.h" #include "module_io/cube_io.h" +#include void ModuleIO::write_cube( #ifdef __MPI @@ -126,8 +127,8 @@ void ModuleIO::write_cube( { /// for cube file int nxyz = nx * ny * nz; - double* data_cube = new double[nxyz]; - ModuleBase::GlobalFunc::ZEROS(data_cube, nxyz); + std::vector data_cube(nxyz); + ModuleBase::GlobalFunc::ZEROS(data_cube.data(), nxyz); /// for cube file // num_z: how many planes on processor 'ip' @@ -149,8 +150,8 @@ void ModuleIO::write_cube( } // which_ip: found iz belongs to which ip. - int *which_ip = new int[nz]; - ModuleBase::GlobalFunc::ZEROS(which_ip, nz); + std::vector which_ip(nz); + ModuleBase::GlobalFunc::ZEROS(which_ip.data(), nz); for(int iz=0; iz zpiece(nxy); // save the rho one z by one z. for(int iz=0; iznplane+iz=" << ir*rhopw->nplane+iz; } - MPI_Send(zpiece, nxy, MPI_DOUBLE, 0, tag, POOL_WORLD); + MPI_Send(zpiece.data(), nxy, MPI_DOUBLE, 0, tag, POOL_WORLD); } // case 2: > first part rho: processor 0 receive the rho // from other processors else if(GlobalV::RANK_IN_POOL==0) { - MPI_Recv(zpiece, nxy, MPI_DOUBLE, which_ip[iz], tag, POOL_WORLD, &ierror); + MPI_Recv(zpiece.data(), nxy, MPI_DOUBLE, which_ip[iz], tag, POOL_WORLD, &ierror); // GlobalV::ofs_running << "\n Receieve First number = " << zpiece[0]; } @@ -226,8 +227,6 @@ void ModuleIO::write_cube( /// for cube file } }// end iz - delete[] zpiece; - delete[] which_ip; delete[] num_z; delete[] start_z; // for cube file @@ -246,7 +245,6 @@ void ModuleIO::write_cube( } } } - delete[] data_cube; /// for cube file } MPI_Barrier(MPI_COMM_WORLD); diff --git a/source/module_relax/relax_old/bfgs_basic.cpp b/source/module_relax/relax_old/bfgs_basic.cpp index b870c7f522..911026b66c 100644 --- a/source/module_relax/relax_old/bfgs_basic.cpp +++ b/source/module_relax/relax_old/bfgs_basic.cpp @@ -3,6 +3,7 @@ #include "ions_move_basic.h" #include "module_base/global_function.h" #include "module_base/global_variable.h" +#include using namespace Ions_Move_Basic; 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) // ModuleBase::TITLE("Ions_Move_BFGS","update_inverse_hessian"); assert(dim > 0); - double *s = new double[dim]; - double *y = new double[dim]; - ModuleBase::GlobalFunc::ZEROS(s, dim); - ModuleBase::GlobalFunc::ZEROS(y, dim); + std::vector s(dim); + std::vector y(dim); + ModuleBase::GlobalFunc::ZEROS(s.data(), dim); + ModuleBase::GlobalFunc::ZEROS(y.data(), dim); for (int i = 0; i < dim; i++) { @@ -94,12 +95,12 @@ void BFGS_Basic::update_inverse_hessian(const double &lat0) return; } - double *Hs = new double[dim]; - double *Hy = new double[dim]; - double *yH = new double[dim]; - ModuleBase::GlobalFunc::ZEROS(Hs, dim); - ModuleBase::GlobalFunc::ZEROS(Hy, dim); - ModuleBase::GlobalFunc::ZEROS(yH, dim); + std::vector Hs(dim); + std::vector Hy(dim); + std::vector yH(dim); + ModuleBase::GlobalFunc::ZEROS(Hs.data(), dim); + ModuleBase::GlobalFunc::ZEROS(Hy.data(), dim); + ModuleBase::GlobalFunc::ZEROS(yH.data(), dim); for (int i = 0; i < dim; i++) { @@ -127,11 +128,6 @@ void BFGS_Basic::update_inverse_hessian(const double &lat0) } } - delete[] s; - delete[] y; - delete[] Hs; - delete[] Hy; - delete[] yH; return; } diff --git a/source/module_relax/relax_old/ions_move_sd.cpp b/source/module_relax/relax_old/ions_move_sd.cpp index f8f77f33cd..405aa63d83 100644 --- a/source/module_relax/relax_old/ions_move_sd.cpp +++ b/source/module_relax/relax_old/ions_move_sd.cpp @@ -3,6 +3,7 @@ #include "ions_move_basic.h" #include "module_base/global_function.h" #include "module_base/global_variable.h" +#include using namespace Ions_Move_Basic; @@ -38,18 +39,18 @@ void Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const assert(grad_saved != 0); assert(pos_saved != 0); - double* pos = new double[dim]; - double* grad = new double[dim]; - double* move = new double[dim]; - ModuleBase::GlobalFunc::ZEROS(pos, dim); - ModuleBase::GlobalFunc::ZEROS(grad, dim); - ModuleBase::GlobalFunc::ZEROS(move, dim); + std::vector pos(dim); + std::vector grad(dim); + std::vector move(dim); + ModuleBase::GlobalFunc::ZEROS(pos.data(), dim); + ModuleBase::GlobalFunc::ZEROS(grad.data(), dim); + ModuleBase::GlobalFunc::ZEROS(move.data(), dim); // 1: ediff = 0 // 0: ediff < 0 bool judgement = 0; setup_etot(etot_in, judgement); - setup_gradient(ucell, force, pos, grad); + setup_gradient(ucell, force, pos.data(), grad.data()); if (istep == 1 || etot_in <= energy_saved) { @@ -70,7 +71,7 @@ void Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const } } - Ions_Move_Basic::check_converged(ucell, grad); + Ions_Move_Basic::check_converged(ucell, grad.data()); if (Ions_Move_Basic::converged) { Ions_Move_Basic::terminate(ucell); @@ -82,14 +83,10 @@ void Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const { move[i] = -grad_saved[i] * trust_radius; } - move_atoms(ucell, move, pos_saved); + move_atoms(ucell, move.data(), pos_saved); Ions_Move_Basic::update_iter++; } - delete[] pos; - delete[] grad; - delete[] move; - return; }