Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/MGmol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ int MGmol<OrbitalsType>::initial()
current_orbitals_->setDataWithGhosts();
current_orbitals_->trade_boundaries();

// if(ct.restart_info <= 1)pot.initWithVnuc();
// initialize matrices S and invB

if (ct.numst > 0)
Expand Down Expand Up @@ -1379,14 +1378,14 @@ void MGmol<OrbitalsType>::update_pot(const Ions& ions)

const bool flag_mixing = (fabs(ct.mix_pot - 1.) > 1.e-3);

// evaluate potential correction
// update total potential
if (flag_mixing)
{
pot.delta_v(rho_->rho_);
pot.update(ct.mix_pot);
pot.computeDeltaV(rho_->rho_);
pot.updateVtot(ct.mix_pot);
}
else
pot.update(rho_->rho_);
pot.updateVtot(rho_->rho_);
}

template <class OrbitalsType>
Expand Down
70 changes: 21 additions & 49 deletions src/Potentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,19 @@ Potentials::Potentials()
#endif
}

void Potentials::initWithVnuc()
{
assert(size_ > 0);
if (verbosity_level_ > 2 && onpe0)
(*MPIdata::sout) << "Potentials::initWithVnuc()" << std::endl;
itindex_vxc_ = 0;
itindex_vh_ = 0;
int ione = 1;
Tcopy(&size_, &v_nuc_[0], &ione, &vtot_[0], &ione);
double one = 1.;
LinearAlgebraUtils<MemorySpace::Host>::MPaxpy(
size_, one, &v_ext_[0], &vtot_[0]);
// factor ha2ry to get total potential in [Ry] for calculations
LinearAlgebraUtils<MemorySpace::Host>::MPscal(size_, ha2ry, &vtot_[0]);
}

double Potentials::max() const
{
Mesh* mymesh = Mesh::instance();
const pb::PEenv& myPEenv = mymesh->peenv();
double vmax = (*max_element(vtot_.begin(), vtot_.end()));
vmax = myPEenv.double_max_all(vmax);
MGmol_MPI& mmpi = *(MGmol_MPI::instance());
double vmax = (*max_element(vtot_.begin(), vtot_.end()));
vmax = mmpi.allreduce(&vmax, 1, MPI_MAX);
return vmax;
}

double Potentials::min() const
{
Mesh* mymesh = Mesh::instance();
const pb::PEenv& myPEenv = mymesh->peenv();
double vmin = -(*min_element(vtot_.begin(), vtot_.end()));
vmin = -myPEenv.double_max_all(vmin);
MGmol_MPI& mmpi = *(MGmol_MPI::instance());
double vmin = -(*min_element(vtot_.begin(), vtot_.end()));
vmin = -mmpi.allreduce(&vmin, 1, MPI_MAX);
return vmin;
}

Expand All @@ -141,17 +123,16 @@ void Potentials::evalNormDeltaVtotRho(
mmpi.allreduce(&scf_dvrho_, 1, MPI_SUM);
}

double Potentials::update(const std::vector<std::vector<RHODTYPE>>& rho)
double Potentials::updateVtot(const std::vector<std::vector<RHODTYPE>>& rho)
{
assert(itindex_vxc_ >= 0);
assert(itindex_vh_ >= 0);
assert(itindex_vxc_ == itindex_vh_);

if (verbosity_level_ > 2 && onpe0)
(*MPIdata::sout) << "Potentials::update(rho)" << std::endl;
int ione = 1;
Mesh* mymesh = Mesh::instance();
const pb::PEenv& myPEenv = mymesh->peenv();
int ione = 1;
MGmol_MPI& mmpi = *(MGmol_MPI::instance());

// save old potentials
Tcopy(&size_, &vtot_[0], &ione, &vtot_old_[0], &ione);
Expand Down Expand Up @@ -181,23 +162,22 @@ double Potentials::update(const std::vector<std::vector<RHODTYPE>>& rho)
= LinearAlgebraUtils<MemorySpace::Host>::MPdot(size_, &dv_[0], &dv_[0]);

double sum = 0.;
int rc
= MPI_Allreduce(&dvdot, &sum, 1, MPI_DOUBLE, MPI_SUM, myPEenv.comm());
int rc = mmpi.allreduce(&dvdot, &sum, 1, MPI_SUM);
if (rc != MPI_SUCCESS)
{
std::cerr << "MPI_Allreduce double sum failed!!!" << std::endl;
MPI_Abort(myPEenv.comm(), EXIT_FAILURE);
mmpi.abort();
}
dvdot = sum;

scf_dv_ = 0.5 * sqrt(dvdot);
const double gsize = (double)size_ * (double)myPEenv.n_mpi_tasks();
const double gsize = (double)size_ * (double)mmpi.size();
scf_dv_ /= gsize;

return scf_dv_;
}

void Potentials::update(const double mix)
void Potentials::updateVtot(const double mix)
{
assert(itindex_vxc_ == itindex_vh_);

Expand All @@ -210,17 +190,16 @@ void Potentials::update(const double mix)
size_, potmix, &dv_[0], &vtot_[0]);
}

double Potentials::delta_v(const std::vector<std::vector<RHODTYPE>>& rho)
double Potentials::computeDeltaV(const std::vector<std::vector<RHODTYPE>>& rho)
{
assert(itindex_vxc_ == itindex_vh_);
assert(size_ > 0);

if (verbosity_level_ > 2 && onpe0)
(*MPIdata::sout) << "Potentials::delta_v()" << std::endl;

int ione = 1;
Mesh* mymesh = Mesh::instance();
const pb::PEenv& myPEenv = mymesh->peenv();
int ione = 1;
MGmol_MPI& mmpi = *(MGmol_MPI::instance());

// save old potentials
Tcopy(&size_, &vtot_[0], &ione, &vtot_old_[0], &ione);
Expand Down Expand Up @@ -249,17 +228,16 @@ double Potentials::delta_v(const std::vector<std::vector<RHODTYPE>>& rho)
= LinearAlgebraUtils<MemorySpace::Host>::MPdot(size_, &dv_[0], &dv_[0]);

double sum = 0.;
int rc
= MPI_Allreduce(&dvdot, &sum, 1, MPI_DOUBLE, MPI_SUM, myPEenv.comm());
int rc = mmpi.allreduce(&dvdot, &sum, 1, MPI_SUM);
if (rc != MPI_SUCCESS)
{
std::cerr << "MPI_Allreduce double sum failed!!!" << std::endl;
MPI_Abort(myPEenv.comm(), EXIT_FAILURE);
mmpi.abort();
}
dvdot = sum;

scf_dv_ = 0.5 * sqrt(dvdot);
const double gsize = (double)size_ * (double)myPEenv.n_mpi_tasks();
const double gsize = (double)size_ * (double)mmpi.size();
scf_dv_ /= gsize;

return scf_dv_;
Expand Down Expand Up @@ -437,7 +415,6 @@ void Potentials::readExternalPot(const std::string filename, const char type)
{
assert(index < size_);
(*from) >> v_ext_[index];
//(*MPIdata::sout)<<myPEenv.mytask();
//(*MPIdata::sout)<<",
// v_ext_["<<index<<"]="<<v_ext_[index]<<endl;
index++;
Expand Down Expand Up @@ -568,6 +545,7 @@ void Potentials::readAll(std::vector<Species>& sp)
isp++;
}
}

template <typename T>
void Potentials::setVxc(const T* const vxc, const int iterativeIndex)
{
Expand All @@ -576,13 +554,6 @@ void Potentials::setVxc(const T* const vxc, const int iterativeIndex)
itindex_vxc_ = iterativeIndex;
MPcpy(&vxc_rho_[0], vxc, size_);
}
void Potentials::setVh(const POTDTYPE* const vh, const int iterativeIndex)
{
assert(iterativeIndex >= 0);
int ione = 1;
itindex_vh_ = iterativeIndex;
Tcopy(&size_, vh, &ione, &vh_rho_[0], &ione);
}

void Potentials::setVh(
const pb::GridFunc<POTDTYPE>& vh, const int iterativeIndex)
Expand Down Expand Up @@ -932,6 +903,7 @@ int Potentials::read(HDFrestart& h5f_file)

// Read the hartree potential
h5f_file.read_1func_hdf5(vh_rho_.data(), "Hartree");
itindex_vh_ = 0;

// Read dielectric potential
if (ct.diel)
Expand Down
15 changes: 4 additions & 11 deletions src/Potentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class GridFunc;

class Potentials
{

int size_;
int gdim_[3];
int dim_[3];
Expand Down Expand Up @@ -167,36 +166,30 @@ class Potentials

double getChargeInCell() const { return charge_in_cell_; }

/*!
* initialize total potential as local pseudopotential
*/
void initWithVnuc();

void getVofRho(std::vector<POTDTYPE>& vrho) const;

/*!
* evaluate potential correction associated with a new rho
*/
double delta_v(const std::vector<std::vector<RHODTYPE>>& rho);
double computeDeltaV(const std::vector<std::vector<RHODTYPE>>& rho);

/*!
* update potentials based on argument rho
* update total potential with updated components
*/
double update(const std::vector<std::vector<RHODTYPE>>& rho);
double updateVtot(const std::vector<std::vector<RHODTYPE>>& rho);

/*!
* update potentials based on potential correction delta v and mixing
* parameter
*/
void update(const double mix);
void updateVtot(const double mix);

double max() const;
double min() const;
void readAll(std::vector<Species>& sp);

template <typename T>
void setVxc(const T* const vxc, const int iterativeIndex);
void setVh(const POTDTYPE* const vh, const int iterativeIndex);
void setVh(const pb::GridFunc<POTDTYPE>& vh, const int iterativeIndex);

void initialize(Ions& ions);
Expand Down