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

Test: add unit test for potential_new.cpp #2495

Merged
merged 9 commits into from
May 24, 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
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ OBJS_ELECSTAT=elecstate.o\
efield.o\
gatefield.o\
potential_new.o\
potential_types.o\
pot_local.o\
H_Hartree_pw.o\
H_TDDFT_pw.o\
Expand Down
1 change: 1 addition & 0 deletions source/module_elecstate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ list(APPEND objects
potentials/pot_xc.cpp
potentials/pot_local.cpp
potentials/potential_new.cpp
potentials/potential_types.cpp
potentials/write_pot.cpp
module_charge/charge.cpp
module_charge/charge_broyden.cpp
Expand Down
2 changes: 1 addition & 1 deletion source/module_elecstate/elecstate_getters.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define ELECSTATE_GETTERS_H

#include <string>

// Description: Getters for elecstate module
namespace elecstate
{
Expand All @@ -26,7 +27,6 @@ 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
13 changes: 13 additions & 0 deletions source/module_elecstate/potentials/H_Hartree_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,17 @@ ModuleBase::matrix H_Hartree_pw::v_hartree(const UnitCell &cell,
return v;
} // end subroutine v_h

PotHartree::PotHartree(const ModulePW::PW_Basis* rho_basis_in)
{
this->rho_basis_ = rho_basis_in;
this->dynamic_mode = true;
this->fixed_mode = false;
}

void PotHartree::cal_v_eff(const Charge* chg, const UnitCell* ucell, ModuleBase::matrix& v_eff)
{
v_eff += H_Hartree_pw::v_hartree(*ucell, const_cast<ModulePW::PW_Basis*>(this->rho_basis_), v_eff.nr, chg->rho);
return;
}

} // namespace elecstate
16 changes: 3 additions & 13 deletions source/module_elecstate/potentials/H_Hartree_pw.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,9 @@ namespace elecstate
class PotHartree : public PotBase
{
public:
PotHartree(const ModulePW::PW_Basis *rho_basis_in)
{
this->rho_basis_ = rho_basis_in;
this->dynamic_mode = true;
this->fixed_mode = false;
}

void cal_v_eff(const Charge *chg, const UnitCell *ucell, ModuleBase::matrix &v_eff) override
{
v_eff
+= H_Hartree_pw::v_hartree(*ucell, const_cast<ModulePW::PW_Basis *>(this->rho_basis_), v_eff.nr, chg->rho);
return;
}
PotHartree(const ModulePW::PW_Basis* rho_basis_in);

void cal_v_eff(const Charge* chg, const UnitCell* ucell, ModuleBase::matrix& v_eff);
};

} // namespace elecstate
Expand Down
58 changes: 5 additions & 53 deletions source/module_elecstate/potentials/potential_new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@
#include "module_base/tool_quit.h"
#include "module_base/tool_title.h"

#include <map>
#include "module_elecstate/elecstate_getters.h"

#include "H_Hartree_pw.h"
#include "efield.h"
#include "gatefield.h"
#include "pot_local.h"
#include "pot_surchem.hpp"
#include "pot_xc.h"
#ifdef __LCAO
#include "H_TDDFT_pw.h"
#endif
#include <map>

namespace elecstate
{
Expand Down Expand Up @@ -82,51 +74,11 @@ void Potential::pot_register(std::vector<std::string>& components_list)
//---------------------------
// mapping for register
//---------------------------
std::map<string, int> pot_register_map
= {
{"local", 1},
{"hartree", 2},
{"xc", 3},
{"surchem", 4},
{"efield", 5},
{"gatefield", 6},
{"tddft", 7}
};
for (auto comp: components_list)
{
PotBase* tmp = nullptr;
int key = pot_register_map[comp];
switch (key)
{
case 1: //"local"
tmp = new PotLocal(this->vloc_, this->structure_factors_, this->rho_basis_);
break;
case 2: //"hartree"
tmp = new PotHartree(this->rho_basis_);
break;
case 3: //"xc"
tmp = new PotXC(this->rho_basis_, this->etxc_, this->vtxc_, &(this->vofk_effective));
break;
case 4: //"surchem"
tmp = new PotSurChem(this->rho_basis_, this->v_effective_fixed.data(), &GlobalC::solvent_model);
break;
case 5: //"efield"
tmp = new PotEfield(this->rho_basis_, this->ucell_, GlobalV::DIP_COR_FLAG);
break;
case 6: //"gatefield"
tmp = new PotGate(this->rho_basis_, this->ucell_);
break;
#ifdef __LCAO
case 7: //tddft
tmp = new H_TDDFT_pw(this->rho_basis_, this->ucell_);
break;
#endif
default:
ModuleBase::WARNING_QUIT("Potential::Init", "Please input correct component of potential!");
break;
}
PotBase* tmp = this->get_pot_type(comp);
this->components.push_back(tmp);
// GlobalV::ofs_running << "Successful completion of Potential's registration : " << comp << std::endl;
// GlobalV::ofs_running << "Successful completion of Potential's registration : " << comp << std::endl;
}

// after register, reset fixed_done to false
Expand All @@ -148,7 +100,7 @@ void Potential::allocate()
this->v_effective.create(GlobalV::NSPIN, nrxx);
ModuleBase::Memory::record("Pot::veff", sizeof(double) * GlobalV::NSPIN * nrxx);

if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
if (elecstate::get_xc_func_type() == 3 || elecstate::get_xc_func_type() == 5)
{
this->vofk_effective.create(GlobalV::NSPIN, nrxx);
ModuleBase::Memory::record("Pot::vofk", sizeof(double) * GlobalV::NSPIN * nrxx);
Expand Down
2 changes: 2 additions & 0 deletions source/module_elecstate/potentials/potential_new.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Potential : public PotBase
// interface for SCF-converged, etxc vtxc for Energy, vnew for force_scc
void get_vnew(const Charge* chg, ModuleBase::matrix& vnew);

PotBase* get_pot_type(const std::string& pot_type);

// interfaces to get values
ModuleBase::matrix& get_effective_v()
{
Expand Down
61 changes: 61 additions & 0 deletions source/module_elecstate/potentials/potential_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "H_Hartree_pw.h"
#include "efield.h"
#include "gatefield.h"
#include "module_base/global_function.h"
#include "module_base/global_variable.h"
#include "module_base/memory.h"
#include "module_base/timer.h"
#include "module_base/tool_quit.h"
#include "module_base/tool_title.h"
#include "pot_local.h"
#include "pot_surchem.hpp"
#include "pot_xc.h"
#include "potential_new.h"
#ifdef __LCAO
#include "H_TDDFT_pw.h"
#endif

namespace elecstate
{

PotBase* Potential::get_pot_type(const std::string& pot_type)
{
ModuleBase::TITLE("Potential", "get_pot_type");
if (pot_type == "local")
{
return new PotLocal(this->vloc_, this->structure_factors_, this->rho_basis_);
}
else if (pot_type == "hartree")
{
return new PotHartree(this->rho_basis_);
}
else if (pot_type == "xc")
{
return new PotXC(this->rho_basis_, this->etxc_, this->vtxc_, &(this->vofk_effective));
}
else if (pot_type == "surchem")
{
return new PotSurChem(this->rho_basis_, this->v_effective_fixed.data(), &GlobalC::solvent_model);
}
else if (pot_type == "efield")
{
return new PotEfield(this->rho_basis_, this->ucell_, GlobalV::DIP_COR_FLAG);
}
else if (pot_type == "gatefield")
{
return new PotGate(this->rho_basis_, this->ucell_);
}
#ifdef __LCAO
else if (pot_type == "tddft")
{
return new H_TDDFT_pw(this->rho_basis_, this->ucell_);
}
#endif
else
{
ModuleBase::WARNING_QUIT("Potential::get_pot_type", "Please input correct component of potential!");
__builtin_unreachable();
}
}

} // namespace elecstate
16 changes: 11 additions & 5 deletions source/module_elecstate/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ AddTest(
SOURCES elecstate_base_test.cpp ../elecstate.cpp ../occupy.cpp ../../module_psi/psi.cpp
)

AddTest(
TARGET potentials_base
SOURCES potentials_base_test.cpp
)

AddTest(
TARGET elecstate_pw
LIBS ${math_libs} base device
Expand All @@ -73,3 +68,14 @@ AddTest(
LIBS ${math_libs} base device
SOURCES elecstate_energy_test.cpp ../elecstate_energy.cpp ../fp_energy.cpp
)

AddTest(
TARGET potentials_base
SOURCES potentials_base_test.cpp
)

AddTest(
TARGET potentials_new
LIBS ${math_libs} base device
SOURCES potential_new_test.cpp ../potentials/potential_new.cpp
)
Loading