Skip to content
Open
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
2 changes: 1 addition & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ target_link_libraries(
cell
parameter
psi_overall_init
psi_initializer
psi_init
psi
dftu
deltaspin
Expand Down
2 changes: 1 addition & 1 deletion source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ OBJS_ORBITAL=ORB_atomic.o\

OBJS_PSI=psi.o\

OBJS_PSI_INITIALIZER=psi_initializer.o\
OBJS_PSI_INITIALIZER=psi_base.o\
psi_init_random.o\
psi_init_file.o\
psi_init_atomic.o\
Expand Down
2 changes: 1 addition & 1 deletion source/source_esolver/esolver_ks_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void ESolver_KS_PW<T, Device>::before_all_runners(BaseCell& basecell, const Inpu
this->solvent,
inp);

this->stp.before_runner(ucell, this->kv, this->sf, *this->pw_wfc, this->ppcell, PARAM.inp);
this->stp.before_runner(ucell, this->kv, this->sf, *this->pw_wfc, this->ppcell.lmaxkb, PARAM.inp);

ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS");

Expand Down
4 changes: 3 additions & 1 deletion source/source_io/module_ctrl/ctrl_output_pw.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "source_base/module_device/device.h" // use Device
#include "source_psi/psi.h" // define psi
#include "source_estate/elecstate_lcao.h" // use pelec
#include "source_psi/setup_psi_pw.h" // use Setup_Psi class
#include "source_psi/setup_psi_pw.h" // use Setup_Psi class

class pseudopot_cell_vnl;

namespace ModuleIO
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ void ReadInput::item_postprocess()

In the future lcao_in_pw will have its own ESolver.

2023/12/22 use new psi_initializer to expand numerical
2023/12/22 use new psi_base to expand numerical
atomic orbitals, ykhuang
*/
if (para.input.towannier90 && para.input.basis_type == "lcao_in_pw")
Expand Down
6 changes: 4 additions & 2 deletions source/source_io/module_wannier/to_wannier90_lcao_in_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ void toWannier90_LCAO_IN_PW::calculate(
Structure_Factor* sf_ptr = const_cast<Structure_Factor*>(&sf);
ModulePW::PW_Basis_K* wfcpw_ptr = const_cast<ModulePW::PW_Basis_K*>(wfcpw);
delete this->psi_initer_;
this->psi_initer_ = new psi_init_nao<std::complex<double>>();
this->psi_initer_->initialize(sf_ptr, wfcpw_ptr, &ucell, &kv, 1, nullptr, GlobalV::MY_RANK);
psi_init_nao<std::complex<double>>* nao_initer = new psi_init_nao<std::complex<double>>();
nao_initer->prepare_params(PARAM.globalv.nqx, PARAM.globalv.dq, PARAM.inp.nspin, PARAM.inp.orbital_dir);
this->psi_initer_ = nao_initer;
this->psi_initer_->initialize(sf_ptr, wfcpw_ptr, &ucell, kv.ik2iktot, kv.get_nkstot(), 1, 0, GlobalV::MY_RANK, PARAM.globalv.npol, PARAM.inp.nbands);
this->psi_initer_->tabulate();
delete this->psi;
const int nks_psi = (PARAM.inp.calculation == "nscf" && PARAM.inp.mem_saver == 1)? 1 : wfcpw->nks;
Expand Down
4 changes: 2 additions & 2 deletions source/source_io/module_wannier/to_wannier90_lcao_in_pw.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#ifdef __LCAO
#include "source_basis/module_ao/parallel_orbitals.h"
#include "source_psi/psi_initializer.h"
#include "source_psi/psi_base.h"

class toWannier90_LCAO_IN_PW : public toWannier90_PW
{
Expand Down Expand Up @@ -59,7 +59,7 @@ class toWannier90_LCAO_IN_PW : public toWannier90_PW
protected:
const Parallel_Orbitals* ParaV = nullptr;
/// @brief psi initializer for expanding nao in planewave basis
psi_initializer<std::complex<double>>* psi_initer_ = nullptr;
psi_base<std::complex<double>>* psi_initer_ = nullptr;

psi::Psi<std::complex<double>, base_device::DEVICE_CPU>* psi = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion source/source_lcao/module_rdmft/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif()

# if(ENABLE_COVERAGE)
# add_coverage(psi)
# add_coverage(psi_initializer)
# add_coverage(psi_init)
# endif()

# if (BUILD_TESTING)
Expand Down
2 changes: 1 addition & 1 deletion source/source_lcao/module_ri/exx_lip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "source_estate/elecstate.h"
#include "source_basis/module_pw/pw_basis_k.h"
#include "source_cell/module_symmetry/symmetry.h"
#include "source_psi/psi_initializer.h"
#include "source_psi/psi_base.h"
#include "source_pw/module_pwdft/structure_factor.h"
#include "source_base/tool_title.h"
#include "source_base/timer.h"
Expand Down
6 changes: 3 additions & 3 deletions source/source_psi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ add_library(
)

add_library(
psi_initializer
psi_init
OBJECT
psi_initializer.cpp
psi_base.cpp
psi_init_random.cpp
psi_init_file.cpp
psi_init_atomic.cpp
Expand All @@ -27,7 +27,7 @@ add_library(

if(ENABLE_COVERAGE)
add_coverage(psi)
add_coverage(psi_initializer)
add_coverage(psi_init)
endif()

if (BUILD_TESTING)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "psi_initializer.h"
#include "psi_base.h"

#include <vector>
#include <cassert>
#include <cstdlib>

#include "source_pw/module_pwdft/structure_factor.h"
#include "source_cell/unitcell.h"
#include "source_basis/module_pw/pw_basis_k.h"
#include "source_base/parallel_global.h"
// basic functions support
#include "source_base/timer.h"
Expand All @@ -13,38 +20,44 @@
#endif

template <typename T>
void psi_initializer<T>::initialize(const Structure_Factor* sf,
void psi_base<T>::initialize(const Structure_Factor* sf,
const ModulePW::PW_Basis_K* pw_wfc,
const UnitCell* p_ucell,
const K_Vectors* p_kv_in,
const std::vector<int>& ik2iktot,
const int& nkstot,
const int& random_seed,
const pseudopot_cell_vnl* p_pspot_nl,
const int& rank)
const int& lmaxkb,
const int& rank,
const int& npol,
const int& nbands)
{
this->sf_ = sf;
this->pw_wfc_ = pw_wfc;
this->p_ucell_ = p_ucell;
this->p_kv = p_kv_in;
this->ik2iktot_ = ik2iktot;
this->nkstot_ = nkstot;
this->random_seed_ = random_seed;
this->p_pspot_nl_ = p_pspot_nl;
this->lmaxkb_ = lmaxkb;
this->npol_ = npol;
this->nbands_ = nbands;
}

template <typename T>
void psi_initializer<T>::random_t(T* psi, const int iw_start, const int iw_end, const int ik, const int mode)
void psi_base<T>::random_t(T* psi, const int iw_start, const int iw_end, const int ik, const int mode)
{
ModuleBase::timer::start("psi_init", "random_t");
assert(mode <= 1);
assert(iw_start >= 0);
const int ng = this->pw_wfc_->npwk[ik];
const int npwk_max = this->pw_wfc_->npwk_max;
const int npol = PARAM.globalv.npol;
const int npol = this->npol_;

// If random seed is specified, then generate random wavefunction satisfying that
// it can generate the same results using different number of processors.
if (this->random_seed_ > 0) // qianrui add 2021-8-13
{
#ifdef __MPI
srand(unsigned(this->random_seed_ + this->p_kv->ik2iktot[ik]));
srand(unsigned(this->random_seed_ + this->ik2iktot_[ik]));
#else
srand(unsigned(this->random_seed_ + ik));
#endif
Expand Down Expand Up @@ -184,7 +197,7 @@ void psi_initializer<T>::random_t(T* psi, const int iw_start, const int iw_end,

#ifdef __MPI
template <typename T>
void psi_initializer<T>::stick_to_pool(Real* stick, const int& ir, Real* out) const
void psi_base<T>::stick_to_pool(Real* stick, const int& ir, Real* out) const
{
ModuleBase::timer::start("psi_init", "stick_to_pool");
MPI_Status ierror;
Expand All @@ -211,7 +224,7 @@ void psi_initializer<T>::stick_to_pool(Real* stick, const int& ir, Real* out) co
}
else
{
ModuleBase::WARNING_QUIT("psi_initializer", "stick_to_pool: Real type not supported");
ModuleBase::WARNING_QUIT("psi_base", "stick_to_pool: Real type not supported");
}
for (int iz = 0; iz < nz; iz++)
{
Expand All @@ -230,7 +243,7 @@ void psi_initializer<T>::stick_to_pool(Real* stick, const int& ir, Real* out) co
}
else
{
ModuleBase::WARNING_QUIT("psi_initializer", "stick_to_pool: Real type not supported");
ModuleBase::WARNING_QUIT("psi_base", "stick_to_pool: Real type not supported");
}
}

Expand All @@ -240,8 +253,8 @@ void psi_initializer<T>::stick_to_pool(Real* stick, const int& ir, Real* out) co
#endif

// explicit instantiation
template class psi_initializer<std::complex<double>>;
template class psi_initializer<std::complex<float>>;
template class psi_base<std::complex<double>>;
template class psi_base<std::complex<float>>;
// gamma point calculation
template class psi_initializer<double>;
template class psi_initializer<float>;
template class psi_base<double>;
template class psi_base<float>;
Original file line number Diff line number Diff line change
@@ -1,68 +1,71 @@
#ifndef PSI_INITIALIZER_H
#define PSI_INITIALIZER_H
// data structure support
#include "source_basis/module_pw/pw_basis_k.h" // for kpoint related data structure
#include "source_pw/module_pwdft/vnl_pw.h"
#ifndef PSI_BASE_H
#define PSI_BASE_H
#include "source_basis/module_pw/pw_basis_k.h"
#include "source_pw/module_pwdft/structure_factor.h"
#include "source_psi/psi.h" // for psi data structure
// smart pointer for auto-memory management
#include "source_psi/psi.h"
#include <memory>
// numerical algorithm support
#ifdef __MPI
#include <mpi.h>
#endif
#include "source_base/macros.h"
#include "source_cell/klist.h"
#include "source_cell/unitcell.h"

#include <type_traits>
#include <vector>

using namespace std;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using namespace std; in a header should be dropped before merge.

This header is included fairly widely -- psi_base.h -> psi_prepare.h -> setup_psi_pw.h -> ctrl_output_pw.h -- so this injects all of std into the global namespace across a large number of translation units.

The practical risk is ambiguous-overload and name-collision errors that are hard to trace back to their origin, since the failure surfaces in some unrelated file that merely includes this one transitively. Names like count, size, data, left, plus and complex are common enough in this codebase that a future free function or variable with a matching name can break the build in a way that gives no hint about where it came from.

It also is not needed here: every declaration in this file is already fully qualified (std::vector<int>, std::unique_ptr, std::string). Suggest simply deleting the line.


/*
Psi (planewave based wavefunction) initializer
Psi (planewave based wavefunction) base class
Auther: Kirk0830
Institute: AI for Science Institute, BEIJING

This class is used to allocate memory and give initial guess for psi
therefore only double datatype is needed to be supported.
Following methods are available:
1. file: use wavefunction file to initialize psi
implemented in psi_initializer_file.h
implemented in psi_init_file.h
2. random: use random number to initialize psi
implemented in psi_initializer_random.h
implemented in psi_init_random.h
3. atomic: use pseudo-wavefunction in pseudopotential file to initialize psi
implemented in psi_initializer_atomic.h
implemented in psi_init_atomic.h
4. atomic+random: mix 'atomic' with some random numbers to initialize psi
5. nao: use numerical orbitals to initialize psi
implemented in psi_initializer_nao.h
implemented in psi_init_nao.h
6. nao+random: mix 'nao' with some random numbers to initialize psi

To use:
- WAVEFUNCTION INITIALIZATION
A practical example would be in ESolver_KS_PW, because polymorphism is achieved by
pointer, while a raw pointer is risky, therefore std::unique_ptr is a better
choice.
1. new a std::unique_ptr<psi_initializer<T> with specific derived class
2. initialize() to link psi_initializer with external data and methods
1. new a std::unique_ptr<psi_base<T> with specific derived class
2. initialize() to link psi_base with external data and methods
3. tabulate() to calculate the interpolate table
4. init_psig() to calculate projection of atomic radial function onto planewave basis
In summary:
new->initialize->tabulate->init_psig
*/
template <typename T>
class psi_initializer
class psi_base
{
private:
using Real = typename GetTypeReal<T>::type;

public:
psi_initializer(){};
virtual ~psi_initializer(){};
/// @brief initialize the psi_initializer with external data and methods
virtual void initialize(const Structure_Factor*, //< structure factor
const ModulePW::PW_Basis_K*, //< planewave basis
const UnitCell*, //< unit cell
const K_Vectors* = nullptr, //< parallel kpoints
const int& = 1, //< random seed
const pseudopot_cell_vnl* = nullptr, //< nonlocal pseudopotential
const int& = 0); //< rank
psi_base(){};
virtual ~psi_base(){};
/// @brief initialize the psi_base with external data and methods
virtual void initialize(const Structure_Factor* sf, //< structure factor
const ModulePW::PW_Basis_K* pw_wfc, //< planewave basis
const UnitCell* p_ucell, //< unit cell
const std::vector<int>& ik2iktot, //< ik2iktot: local->global k-point mapping
const int& nkstot, //< nkstot: total number of k-points
const int& random_seed, //< random seed
const int& lmaxkb, //< lmaxkb: max angular momentum for non-local projectors
const int& rank, //< rank
const int& npol, //< npol
const int& nbands); //< nbands

/// @brief CENTRAL FUNCTION: calculate the interpolate table if needed
virtual void tabulate()
Expand Down Expand Up @@ -112,6 +115,7 @@ class psi_initializer
}

protected:

#ifdef __MPI // MPI additional implementation
/// @brief mapping from (ix, iy) to is
void stick_to_pool(Real* stick, //< stick
Expand All @@ -123,17 +127,35 @@ class psi_initializer
const int iw_end, ///< iw_end, ending band index
const int ik, ///< ik, kpoint index
const int mode = 1); ///< mode, 0 for rr*exp(i*arg), 1 for rr/(1+gk2)*exp(i*arg)

const Structure_Factor* sf_ = nullptr; ///< Structure_Factor

const ModulePW::PW_Basis_K* pw_wfc_ = nullptr; ///< use |k+G>, |G>, getgpluskcar and so on in PW_Basis_K

const UnitCell* p_ucell_ = nullptr; ///< UnitCell
const K_Vectors* p_kv = nullptr; ///< Parallel_Kpoints
const pseudopot_cell_vnl* p_pspot_nl_ = nullptr; ///< pseudopot_cell_vnl

int lmaxkb_ = 0; ///< max angular momentum for non-local projectors

std::vector<int> ik2iktot_; ///< local->global k-point mapping

int nkstot_ = 0; ///< total number of k-points

int random_seed_ = 1; ///< random seed, shared by random, atomic+random, nao+random

std::vector<int> ixy2is_; ///< used by stick_to_pool function

int mem_saver_ = 0; ///< if save memory, only for nscf

std::string method_ = "none"; ///< method name

int nbands_complem_ = 0; ///< complement number of bands, which is nbands_start_ - ucell.natomwfc

double mixing_coef_ = 0; ///< mixing coefficient for atomic+random and nao+random

int nbands_start_ = 0; ///< starting nbands, which is no less than PARAM.inp.nbands

int npol_ = 1; ///< number of polarizations

int nbands_ = 1; ///< number of bands
};
#endif
#endif
Loading
Loading