Skip to content

Commit

Permalink
Vector instead of New, changed by Linbo Cao (#3975)
Browse files Browse the repository at this point in the history
* Vector instead of New, changed by Linbo Cao

* Vector instead of New changed by Linbo Cao version2
  • Loading branch information
RobertRainbow authored Apr 14, 2024
1 parent 4154ec3 commit 43cde6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
20 changes: 9 additions & 11 deletions source/module_io/write_cube.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "module_base/element_name.h"
#include "module_io/cube_io.h"
#include<vector>

void ModuleIO::write_cube(
#ifdef __MPI
Expand Down Expand Up @@ -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<double> data_cube(nxyz);
ModuleBase::GlobalFunc::ZEROS(data_cube.data(), nxyz);
/// for cube file

// num_z: how many planes on processor 'ip'
Expand All @@ -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<int> which_ip(nz);
ModuleBase::GlobalFunc::ZEROS(which_ip.data(), nz);
for(int iz=0; iz<nz; iz++)
{
for(int ip=0; ip<GlobalV::NPROC_IN_POOL; ip++)
Expand All @@ -172,14 +173,14 @@ void ModuleIO::write_cube(

int count=0;
int nxy = nx * ny;
double* zpiece = new double[nxy];
std::vector<double> zpiece(nxy);

// save the rho one z by one z.
for(int iz=0; iz<nz; iz++)
{
// std::cout << "\n iz=" << iz << std::endl;
// tag must be different for different iz.
ModuleBase::GlobalFunc::ZEROS(zpiece, nxy);
ModuleBase::GlobalFunc::ZEROS(zpiece.data(), nxy);
int tag = iz;
MPI_Status ierror;

Expand All @@ -205,14 +206,14 @@ void ModuleIO::write_cube(
zpiece[ir] = data[ir*nplane+iz-startz_current];
// GlobalV::ofs_running << "\n get zpiece[" << ir << "]=" << zpiece[ir] << " ir*rhopw->nplane+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];
}

Expand All @@ -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
Expand All @@ -246,7 +245,6 @@ void ModuleIO::write_cube(
}
}
}
delete[] data_cube;
/// for cube file
}
MPI_Barrier(MPI_COMM_WORLD);
Expand Down
26 changes: 11 additions & 15 deletions source/module_relax/relax_old/bfgs_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ions_move_basic.h"
#include "module_base/global_function.h"
#include "module_base/global_variable.h"
#include<vector>
using namespace Ions_Move_Basic;

double BFGS_Basic::relax_bfgs_w1 = -1.0; // default is 0.01
Expand Down Expand Up @@ -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<double> s(dim);
std::vector<double> y(dim);
ModuleBase::GlobalFunc::ZEROS(s.data(), dim);
ModuleBase::GlobalFunc::ZEROS(y.data(), dim);

for (int i = 0; i < dim; i++)
{
Expand All @@ -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<double> Hs(dim);
std::vector<double> Hy(dim);
std::vector<double> 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++)
{
Expand Down Expand Up @@ -127,11 +128,6 @@ void BFGS_Basic::update_inverse_hessian(const double &lat0)
}
}

delete[] s;
delete[] y;
delete[] Hs;
delete[] Hy;
delete[] yH;
return;
}

Expand Down
23 changes: 10 additions & 13 deletions source/module_relax/relax_old/ions_move_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ions_move_basic.h"
#include "module_base/global_function.h"
#include "module_base/global_variable.h"
#include<vector>

using namespace Ions_Move_Basic;

Expand Down Expand Up @@ -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<double> pos(dim);
std::vector<double> grad(dim);
std::vector<double> 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)
{
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit 43cde6d

Please sign in to comment.