Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: add getter functions for elecstate energy terms #2480

Merged
merged 3 commits into from
May 21, 2023
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: 2 additions & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ OBJS_DEEPKS=LCAO_deepks.o\

OBJS_ELECSTAT=elecstate.o\
elecstate_energy.o\
elecstate_energy_terms.o\
elecstate_exx.o\
elecstate_print.o\
elecstate_pw.o\
elecstate_pw_sdft.o\
Expand Down
2 changes: 2 additions & 0 deletions source/module_elecstate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ list(APPEND objects
elecstate.cpp
elecstate_getters.cpp
elecstate_energy.cpp
elecstate_energy_terms.cpp
elecstate_exx.cpp
elecstate_print.cpp
elecstate_pw.cpp
elecstate_pw_sdft.cpp
Expand Down
16 changes: 16 additions & 0 deletions source/module_elecstate/elecstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ class ElecState
void set_exx();
#endif //__LCAO
#endif //__EXX

double get_hartree_energy();
double get_etot_efield();
double get_etot_gatefield();

double get_solvent_model_Ael();
double get_solvent_model_Acav();

#ifdef __LCAO
double get_dftu_energy();
#endif

#ifdef __DEEPKS
double get_deepks_E_delta();
double get_deepks_E_delta_band();
#endif

fenergy f_en; ///< energies contribute to the total free energy
efermi eferm; ///< fermi energies
Expand Down
73 changes: 16 additions & 57 deletions source/module_elecstate/elecstate_energy.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#include <cmath>

#include "elecstate.h"
#include "elecstate_getters.h"
#include "module_base/global_variable.h"
#include "module_base/parallel_reduce.h"
#include "module_elecstate/potentials/H_Hartree_pw.h"
#include "module_elecstate/potentials/efield.h"
#include "module_elecstate/potentials/gatefield.h"
#include "module_hamilt_general/module_xc/xc_functional.h"
#include "module_hamilt_lcao/hamilt_lcaodft/global_fp.h"
#include "module_hamilt_lcao/module_deepks/LCAO_deepks.h"
#include "module_hamilt_lcao/module_dftu/dftu.h"
#include "module_hamilt_pw/hamilt_pwdft/global.h"

namespace elecstate
{
/// @brief calculate band gap
Expand Down Expand Up @@ -97,15 +91,15 @@ double ElecState::cal_delta_eband() const
const double* v_eff = this->pot->get_effective_v(0);
const double* v_fixed = this->pot->get_fixed_v();
const double* v_ofk = nullptr;
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
if (get_xc_func_type() == 3 || get_xc_func_type() == 5)
{
v_ofk = this->pot->get_effective_vofk(0);
}

for (int ir = 0; ir < this->charge->rhopw->nrxx; ir++)
{
deband_aux -= this->charge->rho[0][ir] * (v_eff[ir] - v_fixed[ir]);
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
if (get_xc_func_type() == 3 || get_xc_func_type() == 5)
{
deband_aux -= this->charge->kin_r[0][ir] * v_ofk[ir];
}
Expand All @@ -118,7 +112,7 @@ double ElecState::cal_delta_eband() const
for (int ir = 0; ir < this->charge->rhopw->nrxx; ir++)
{
deband_aux -= this->charge->rho[1][ir] * (v_eff[ir] - v_fixed[ir]);
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
if (get_xc_func_type() == 3 || get_xc_func_type() == 5)
{
deband_aux -= this->charge->kin_r[1][ir] * v_ofk[ir];
}
Expand Down Expand Up @@ -164,15 +158,15 @@ double ElecState::cal_delta_escf() const
const double* v_eff = this->pot->get_effective_v(0);
const double* v_fixed = this->pot->get_fixed_v();
const double* v_ofk = nullptr;
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
if (get_xc_func_type() == 3 || get_xc_func_type() == 5)
{
v_ofk = this->pot->get_effective_vofk(0);
}

for (int ir = 0; ir < this->charge->rhopw->nrxx; ir++)
{
descf -= (this->charge->rho[0][ir] - this->charge->rho_save[0][ir]) * (v_eff[ir] - v_fixed[ir]);
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
if (get_xc_func_type() == 3 || get_xc_func_type() == 5)
{
descf -= (this->charge->kin_r[0][ir] - this->charge->kin_r_save[0][ir]) * v_ofk[ir];
}
Expand All @@ -181,14 +175,14 @@ double ElecState::cal_delta_escf() const
if (GlobalV::NSPIN == 2)
{
v_eff = this->pot->get_effective_v(1);
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
if (get_xc_func_type() == 3 || get_xc_func_type() == 5)
{
v_ofk = this->pot->get_effective_vofk(1);
}
for (int ir = 0; ir < this->charge->rhopw->nrxx; ir++)
{
descf -= (this->charge->rho[1][ir] - this->charge->rho_save[1][ir]) * (v_eff[ir] - v_fixed[ir]);
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
if (get_xc_func_type() == 3 || get_xc_func_type() == 5)
{
descf -= (this->charge->kin_r[1][ir] - this->charge->kin_r_save[1][ir]) * v_ofk[ir];
}
Expand Down Expand Up @@ -232,24 +226,24 @@ void ElecState::cal_converged()
*/
void ElecState::cal_energies(const int type)
{
this->f_en.hartree_energy = elecstate::H_Hartree_pw::hartree_energy;
this->f_en.efield = elecstate::Efield::etotefield;
this->f_en.gatefield = elecstate::Gatefield::etotgatefield;
this->f_en.hartree_energy = get_hartree_energy();
this->f_en.efield = get_etot_efield();
this->f_en.gatefield = get_etot_gatefield();
if (GlobalV::imp_sol)
{
this->f_en.esol_el = GlobalC::solvent_model.cal_Ael(GlobalC::ucell, this->charge->nrxx, this->charge->nxyz);
this->f_en.esol_cav = GlobalC::solvent_model.cal_Acav(GlobalC::ucell, this->charge->nxyz);
this->f_en.esol_el = get_solvent_model_Ael();
this->f_en.esol_cav = get_solvent_model_Acav();
}
#ifdef __LCAO
if (GlobalV::dft_plus_u)
{
this->f_en.edftu = GlobalC::dftu.get_energy();
this->f_en.edftu = get_dftu_energy();
}
#endif
#ifdef __DEEPKS
if (GlobalV::deepks_scf)
{
this->f_en.edeepks_scf = GlobalC::ld.E_delta - GlobalC::ld.e_delta_band;
this->f_en.edeepks_scf = get_deepks_E_delta() - get_deepks_E_delta_band();
}
#endif
if (type == 1) // harris
Expand All @@ -262,39 +256,4 @@ void ElecState::cal_energies(const int type)
}
}

#ifdef __EXX
#ifdef __LCAO
/// @brief calculation if converged
/// @date Peize Lin add 2016-12-03
void ElecState::set_exx()
{
ModuleBase::TITLE("energy", "set_exx");

auto exx_energy = []() -> double {
if ("lcao_in_pw" == GlobalV::BASIS_TYPE)
{
return GlobalC::exx_lip.get_exx_energy();
}
else if ("lcao" == GlobalV::BASIS_TYPE)
{
if (GlobalC::exx_info.info_ri.real_number)
return GlobalC::exx_lri_double.Eexx;
else
return std::real(GlobalC::exx_lri_complex.Eexx);
}
else
{
throw std::invalid_argument(ModuleBase::GlobalFunc::TO_STRING(__FILE__)
+ ModuleBase::GlobalFunc::TO_STRING(__LINE__));
}
};
if (GlobalC::exx_info.info_global.cal_exx)
{
this->f_en.exx = GlobalC::exx_info.info_global.hybrid_alpha * exx_energy();
}

return;
}
#endif //__LCAO
#endif //__EXX
} // namespace elecstate
54 changes: 54 additions & 0 deletions source/module_elecstate/elecstate_energy_terms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "elecstate.h"
#include "module_elecstate/potentials/H_Hartree_pw.h"
#include "module_elecstate/potentials/efield.h"
#include "module_elecstate/potentials/gatefield.h"
#include "module_hamilt_lcao/module_deepks/LCAO_deepks.h"
#include "module_hamilt_lcao/module_dftu/dftu.h"

namespace elecstate
{

double ElecState::get_hartree_energy()
{
return H_Hartree_pw::hartree_energy;
}

double ElecState::get_etot_efield()
{
return Efield::etotefield;
}

double ElecState::get_etot_gatefield()
{
return Gatefield::etotgatefield;
}

double ElecState::get_solvent_model_Ael()
{
return GlobalC::solvent_model.cal_Ael(GlobalC::ucell, this->charge->nrxx, this->charge->nxyz);
}

double ElecState::get_solvent_model_Acav()
{
return GlobalC::solvent_model.cal_Acav(GlobalC::ucell, this->charge->nxyz);
}

#ifdef __LCAO
double ElecState::get_dftu_energy()
{
return GlobalC::dftu.get_energy();
}
#endif

#ifdef __DEEPKS
double ElecState::get_deepks_E_delta()
{
return GlobalC::ld.E_delta;
}
double ElecState::get_deepks_E_delta_band()
{
return GlobalC::ld.e_delta_band;
}
#endif

} // namespace elecstate
43 changes: 43 additions & 0 deletions source/module_elecstate/elecstate_exx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include "module_hamilt_lcao/hamilt_lcaodft/global_fp.h"

namespace elecstate
{

#ifdef __EXX
#ifdef __LCAO
/// @brief calculation if converged
/// @date Peize Lin add 2016-12-03
void ElecState::set_exx()
{
ModuleBase::TITLE("energy", "set_exx");

auto exx_energy = []() -> double {
if ("lcao_in_pw" == GlobalV::BASIS_TYPE)
{
return GlobalC::exx_lip.get_exx_energy();
}
else if ("lcao" == GlobalV::BASIS_TYPE)
{
if (GlobalC::exx_info.info_ri.real_number)
return GlobalC::exx_lri_double.Eexx;
else
return std::real(GlobalC::exx_lri_complex.Eexx);
}
else
{
throw std::invalid_argument(ModuleBase::GlobalFunc::TO_STRING(__FILE__)
+ ModuleBase::GlobalFunc::TO_STRING(__LINE__));
}
};
if (GlobalC::exx_info.info_global.cal_exx)
{
this->f_en.exx = GlobalC::exx_info.info_global.hybrid_alpha * exx_energy();
}

return;
}
#endif //__LCAO
#endif //__EXX

}
11 changes: 11 additions & 0 deletions source/module_elecstate/elecstate_getters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "module_cell/unitcell.h"
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include "module_io/input.h"
#include "module_hamilt_general/module_xc/xc_functional.h"

namespace elecstate
{
Expand All @@ -12,6 +13,16 @@ double get_ucell_omega()
return GlobalC::ucell.omega;
}

double get_ucell_tpiba()
{
return GlobalC::ucell.tpiba;
}

int get_xc_func_type()
{
return XC_Functional::get_func_type();
}

std::string get_input_vdw_method()
{
return INPUT.vdw_method;
Expand Down
5 changes: 5 additions & 0 deletions source/module_elecstate/elecstate_getters.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace elecstate

/// @brief get the value of GlobalC::ucell.omega
double get_ucell_omega();
/// @brief get the value of GlobalC::ucell.tpiba
double get_ucell_tpiba();
/// @brief get the value of XC_Functional::func_type
int get_xc_func_type();
/// @brief get the value of INPUT.vdw_method
std::string get_input_vdw_method();
/// @brief get the value of GlobalC::ucell.magnet.tot_magnetization
Expand All @@ -22,6 +26,7 @@ double get_ucell_tot_magnetization_nc_y();
double get_ucell_tot_magnetization_nc_z();
/// @brief get the type of KS_SOLVER
std::string get_ks_solver_type();
/// @brief get the value of GlobalC::solvent_model.cal_Ael

} // namespace elecstate

Expand Down
Empty file.