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
2 changes: 1 addition & 1 deletion source/source_esolver/esolver_gets.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace ModuleESolver
{

class ESolver_GetS : public ESolver_KS<std::complex<double>>
class ESolver_GetS : public ESolver_KS
{
public:
ESolver_GetS();
Expand Down
100 changes: 34 additions & 66 deletions source/source_esolver/esolver_ks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,29 @@
#include "source_io/module_output/output_log.h" // use write_head
#include "source_estate/elecstate_print.h" // print_etot
#include "source_io/module_output/print_info.h" // print_parameters
#include "source_psi/setup_psi.h" // mohan add 20251009
#include "source_lcao/module_dftu/dftu.h" // mohan add 2025-11-07

namespace ModuleESolver
{

template <typename T, typename Device>
ESolver_KS<T, Device>::ESolver_KS(){}
ESolver_KS::ESolver_KS() {}


template <typename T, typename Device>
ESolver_KS<T, Device>::~ESolver_KS()
ESolver_KS::~ESolver_KS()
{
//****************************************************
// do not add any codes in this deconstructor funcion
//****************************************************
Setup_Psi<T>::deallocate_psi(this->psi);

//****************************************************
// do not add any codes in this deconstructor funcion
//****************************************************
delete this->p_hamilt;
delete this->p_chgmix;
this->ppcell.release_memory();

// mohan add 2025-10-18, should be put int clean() function
pw::teardown_pwwfc(this->pw_wfc);
}


template <typename T, typename Device>
void ESolver_KS<T, Device>::before_all_runners(UnitCell& ucell, const Input_para& inp)
void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para& inp)
{
ModuleBase::TITLE("ESolver_KS", "before_all_runners");

Expand Down Expand Up @@ -81,12 +75,10 @@ void ESolver_KS<T, Device>::before_all_runners(UnitCell& ucell, const Input_para

}

template <typename T, typename Device>
void ESolver_KS<T, Device>::hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr)
void ESolver_KS::hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr)
{}

template <typename T, typename Device>
void ESolver_KS<T, Device>::hamilt2rho(UnitCell& ucell, const int istep, const int iter, const double ethr)
void ESolver_KS::hamilt2rho(UnitCell& ucell, const int istep, const int iter, const double ethr)
{
// 1) use Hamiltonian to obtain charge density
this->hamilt2rho_single(ucell, istep, iter, diag_ethr);
Expand Down Expand Up @@ -126,8 +118,7 @@ void ESolver_KS<T, Device>::hamilt2rho(UnitCell& ucell, const int istep, const i
}
}

template <typename T, typename Device>
void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
void ESolver_KS::runner(UnitCell& ucell, const int istep)
{
ModuleBase::TITLE("ESolver_KS", "runner");
ModuleBase::timer::tick(this->classname, "runner");
Expand All @@ -142,14 +133,14 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
this->diag_ethr = PARAM.inp.pw_diag_thr;
this->scf_nmax_flag = false; // mohan add 2025-09-21
for (int iter = 1; iter <= this->maxniter; ++iter)
{
if(iter == this->maxniter)
{
this->scf_nmax_flag=true;
}
{
if(iter == this->maxniter)
{
this->scf_nmax_flag=true;
}

// 3) initialization of SCF iterations
this->iter_init(ucell, istep, iter);
// 3) initialization of SCF iterations
this->iter_init(ucell, istep, iter);

// 4) use Hamiltonian to obtain charge density
this->hamilt2rho(ucell, istep, iter, diag_ethr);
Expand All @@ -169,22 +160,20 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
}
} // end scf iterations

// 7) after scf
// 7) after scf
this->after_scf(ucell, istep, conv_esolver);

ModuleBase::timer::tick(this->classname, "runner");
return;
};

template <typename T, typename Device>
void ESolver_KS<T, Device>::before_scf(UnitCell& ucell, const int istep)
void ESolver_KS::before_scf(UnitCell& ucell, const int istep)
{
ModuleBase::TITLE("ESolver_KS", "before_scf");
ESolver_FP::before_scf(ucell, istep);
}

template <typename T, typename Device>
void ESolver_KS<T, Device>::iter_init(UnitCell& ucell, const int istep, const int iter)
void ESolver_KS::iter_init(UnitCell& ucell, const int istep, const int iter)
{
if(PARAM.inp.esolver_type != "tddft")
{
Expand All @@ -210,8 +199,7 @@ void ESolver_KS<T, Device>::iter_init(UnitCell& ucell, const int istep, const in
this->chr.save_rho_before_sum_band();
}

template <typename T, typename Device>
void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& iter, bool &conv_esolver)
void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& iter, bool &conv_esolver)
{

// 1.1) print out band gap
Expand All @@ -227,25 +215,25 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
// 1.2) print out eigenvalues and occupations
if (PARAM.inp.out_band[0])
{
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver)
{
ModuleIO::write_eig_iter(this->pelec->ekb,this->pelec->wg,*this->pelec->klist);
}
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver)
{
ModuleIO::write_eig_iter(this->pelec->ekb,this->pelec->wg,*this->pelec->klist);
}
}

// 2.1) compute magnetization, only for spin==2
ucell.magnet.compute_mag(ucell.omega, this->chr.nrxx, this->chr.nxyz, this->chr.rho,
this->pelec->nelec_spin.data());

// 2.2) charge mixing
// 2.2) charge mixing
// SCF will continue if U is not converged for uramping calculation
bool converged_u = true;
// to avoid unnecessary dependence on dft+u, refactor is needed
bool converged_u = true;
// to avoid unnecessary dependence on dft+u, refactor is needed
#ifdef __LCAO
if (PARAM.inp.dft_plus_u)
{
converged_u = this->dftu.u_converged();
}
if (PARAM.inp.dft_plus_u)
{
converged_u = this->dftu.u_converged();
}
#endif

module_charge::chgmixing_ks(iter, ucell, this->pelec, this->chr, this->p_chgmix,
Expand Down Expand Up @@ -296,8 +284,7 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
}

//! Something to do after SCF iterations when SCF is converged or comes to the max iter step.
template <typename T, typename Device>
void ESolver_KS<T, Device>::after_scf(UnitCell& ucell, const int istep, const bool conv_esolver)
void ESolver_KS::after_scf(UnitCell& ucell, const int istep, const bool conv_esolver)
{
ModuleBase::TITLE("ESolver_KS", "after_scf");

Expand All @@ -321,29 +308,10 @@ void ESolver_KS<T, Device>::after_scf(UnitCell& ucell, const int istep, const bo

}

template <typename T, typename Device>
void ESolver_KS<T, Device>::after_all_runners(UnitCell& ucell)
void ESolver_KS::after_all_runners(UnitCell& ucell)
{
// 1) write Etot information
ESolver_FP::after_all_runners(ucell);
}

//------------------------------------------------------------------------------
//! the 16th-20th functions of ESolver_KS
//! mohan add 2024-05-12
//------------------------------------------------------------------------------
//! This is for mixed-precision pw/LCAO basis sets.
template class ESolver_KS<std::complex<float>, base_device::DEVICE_CPU>;
template class ESolver_KS<std::complex<double>, base_device::DEVICE_CPU>;

//! This is for GPU codes.
#if ((defined __CUDA) || (defined __ROCM))
template class ESolver_KS<std::complex<float>, base_device::DEVICE_GPU>;
template class ESolver_KS<std::complex<double>, base_device::DEVICE_GPU>;
#endif

//! This is for LCAO basis set.
#ifdef __LCAO
template class ESolver_KS<double, base_device::DEVICE_CPU>;
#endif
} // namespace ModuleESolver
5 changes: 0 additions & 5 deletions source/source_esolver/esolver_ks.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
#include "source_basis/module_pw/pw_basis_k.h" // use plane wave
#include "source_cell/klist.h" // use k-points in Brillouin zone
#include "source_estate/module_charge/charge_mixing.h" // use charge mixing
#include "source_psi/psi.h" // use electronic wave functions
#include "source_hamilt/hamilt.h" // use Hamiltonian
#include "source_hamilt/hamilt_base.h" // use Hamiltonian base class
#include "source_lcao/module_dftu/dftu.h" // mohan add 20251107

namespace ModuleESolver
{

template <typename T, typename Device = base_device::DEVICE_CPU>
class ESolver_KS : public ESolver_FP
{
public:
Expand Down Expand Up @@ -60,9 +58,6 @@ class ESolver_KS : public ESolver_FP
//! nonlocal pseudopotentials
pseudopot_cell_vnl ppcell;

//! Electronic wavefunctions
psi::Psi<T>* psi = nullptr;

//! DFT+U method, mohan add 2025-11-07
Plus_U dftu;

Expand Down
14 changes: 8 additions & 6 deletions source/source_esolver/esolver_ks_lcao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "source_io/module_output/print_info.h"
#include "source_lcao/rho_tau_lcao.h" // mohan add 20251024
#include "source_lcao/LCAO_set.h" // mohan add 20251111
#include "source_psi/setup_psi.h" // use Setup_Psi for deallocate_psi

namespace ModuleESolver
{
Expand All @@ -40,6 +41,7 @@ ESolver_KS_LCAO<TK, TR>::~ESolver_KS_LCAO()
//****************************************************
// do not add any codes in this deconstructor funcion
//****************************************************
Setup_Psi<TK>::deallocate_psi(this->psi);
}

template <typename TK, typename TR>
Expand All @@ -49,7 +51,7 @@ void ESolver_KS_LCAO<TK, TR>::before_all_runners(UnitCell& ucell, const Input_pa
ModuleBase::timer::tick("ESolver_KS_LCAO", "before_all_runners");

// 1) before_all_runners in ESolver_KS
ESolver_KS<TK>::before_all_runners(ucell, inp);
ESolver_KS::before_all_runners(ucell, inp);

// 2) autoset nbands in ElecState before init_basis (for Psi 2d division)
if (this->pelec == nullptr)
Expand Down Expand Up @@ -105,7 +107,7 @@ void ESolver_KS_LCAO<TK, TR>::before_scf(UnitCell& ucell, const int istep)
ModuleBase::timer::tick("ESolver_KS_LCAO", "before_scf");

//! 1) call before_scf() of ESolver_KS.
ESolver_KS<TK>::before_scf(ucell, istep);
ESolver_KS::before_scf(ucell, istep);

//! 2) find search radius
double search_radius = atom_arrange::set_sr_NL(GlobalV::ofs_running,
Expand Down Expand Up @@ -269,7 +271,7 @@ void ESolver_KS_LCAO<TK, TR>::after_all_runners(UnitCell& ucell)
ModuleBase::TITLE("ESolver_KS_LCAO", "after_all_runners");
ModuleBase::timer::tick("ESolver_KS_LCAO", "after_all_runners");

ESolver_KS<TK>::after_all_runners(ucell);
ESolver_KS::after_all_runners(ucell);

auto* hamilt_lcao = dynamic_cast<hamilt::HamiltLCAO<TK, TR>*>(this->p_hamilt);
if(!hamilt_lcao)
Expand Down Expand Up @@ -301,7 +303,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_init(UnitCell& ucell, const int istep, const
ModuleBase::TITLE("ESolver_KS_LCAO", "iter_init");

// call iter_init() of ESolver_KS
ESolver_KS<TK>::iter_init(ucell, istep, iter);
ESolver_KS::iter_init(ucell, istep, iter);

module_charge::chgmixing_ks_lcao(iter, this->p_chgmix, this->dftu,
this->dmat.dm->get_DMR_pointer(1)->get_nnr(), PARAM.inp);
Expand Down Expand Up @@ -436,7 +438,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
// eig and occ are printed, magnetization is calculated,
// charge mixing is performed, potential is updated,
// HF and kS energies are computed, meta-GGA, Jason and restart
ESolver_KS<TK>::iter_finish(ucell, istep, iter, conv_esolver);
ESolver_KS::iter_finish(ucell, istep, iter, conv_esolver);

// mix density matrix if mixing_restart + mixing_dmr + not first
// mixing_restart at every iter except the last iter
Expand Down Expand Up @@ -474,7 +476,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(UnitCell& ucell, const int istep, const
}

//! 1) call after_scf() of ESolver_KS
ESolver_KS<TK>::after_scf(ucell, istep, conv_esolver);
ESolver_KS::after_scf(ucell, istep, conv_esolver);

//! 2) output of lcao every few ionic steps
ModuleIO::ctrl_scf_lcao<TK, TR>(ucell,
Expand Down
5 changes: 4 additions & 1 deletion source/source_esolver/esolver_ks_lcao.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace ModuleESolver
{

template <typename TK, typename TR>
class ESolver_KS_LCAO : public ESolver_KS<TK>
class ESolver_KS_LCAO : public ESolver_KS
{
public:
ESolver_KS_LCAO();
Expand Down Expand Up @@ -57,6 +57,9 @@ class ESolver_KS_LCAO : public ESolver_KS<TK>

virtual void others(UnitCell& ucell, const int istep) override;

//! Electronic wave functions (moved from base class)
psi::Psi<TK>* psi = nullptr;

//! Store information about Adjacent Atoms
Record_adj RA;

Expand Down
6 changes: 5 additions & 1 deletion source/source_esolver/esolver_ks_lcao_tddft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ ESolver_KS_LCAO_TDDFT<TR, Device>::~ESolver_KS_LCAO_TDDFT()
//*************************************************
// Do not add any code in this destructor function
//*************************************************
delete psi_laststep;
if (psi_laststep != nullptr)
{
delete psi_laststep;
psi_laststep = nullptr;
}

if (td_p != nullptr)
{
Expand Down
Loading
Loading