From b9ca6a5ff37ca1db88f128df2e5ab9d5acbcc184 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 22 May 2023 06:32:06 +0000 Subject: [PATCH 1/6] Test: add unit test for potential_new.cpp --- source/Makefile.Objects | 1 + source/module_elecstate/CMakeLists.txt | 1 + source/module_elecstate/elecstate_getters.h | 2 +- .../potentials/H_Hartree_pw.cpp | 13 ++++ .../potentials/H_Hartree_pw.h | 16 +---- .../potentials/potential_new.cpp | 58 ++---------------- .../potentials/potential_new.h | 2 + .../potentials/potential_types.cpp | 61 +++++++++++++++++++ source/module_elecstate/test/CMakeLists.txt | 16 +++-- .../test/potential_new_test.cpp | 47 ++++++++++++++ 10 files changed, 145 insertions(+), 72 deletions(-) create mode 100644 source/module_elecstate/potentials/potential_types.cpp create mode 100644 source/module_elecstate/test/potential_new_test.cpp diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 79e37d44f8..0da978a7ce 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -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\ diff --git a/source/module_elecstate/CMakeLists.txt b/source/module_elecstate/CMakeLists.txt index 8cfca2ee29..0520618143 100644 --- a/source/module_elecstate/CMakeLists.txt +++ b/source/module_elecstate/CMakeLists.txt @@ -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 diff --git a/source/module_elecstate/elecstate_getters.h b/source/module_elecstate/elecstate_getters.h index f13075e18d..4ee4ca57c0 100644 --- a/source/module_elecstate/elecstate_getters.h +++ b/source/module_elecstate/elecstate_getters.h @@ -2,6 +2,7 @@ #define ELECSTATE_GETTERS_H #include + // Description: Getters for elecstate module namespace elecstate { @@ -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 diff --git a/source/module_elecstate/potentials/H_Hartree_pw.cpp b/source/module_elecstate/potentials/H_Hartree_pw.cpp index 2fb01ffe0d..f97958a098 100644 --- a/source/module_elecstate/potentials/H_Hartree_pw.cpp +++ b/source/module_elecstate/potentials/H_Hartree_pw.cpp @@ -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(this->rho_basis_), v_eff.nr, chg->rho); + return; +} + } // namespace elecstate diff --git a/source/module_elecstate/potentials/H_Hartree_pw.h b/source/module_elecstate/potentials/H_Hartree_pw.h index 454163c657..23ebf04738 100644 --- a/source/module_elecstate/potentials/H_Hartree_pw.h +++ b/source/module_elecstate/potentials/H_Hartree_pw.h @@ -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(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 diff --git a/source/module_elecstate/potentials/potential_new.cpp b/source/module_elecstate/potentials/potential_new.cpp index b2d13b4364..9b633c5836 100644 --- a/source/module_elecstate/potentials/potential_new.cpp +++ b/source/module_elecstate/potentials/potential_new.cpp @@ -7,17 +7,9 @@ #include "module_base/tool_quit.h" #include "module_base/tool_title.h" -#include +#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 namespace elecstate { @@ -82,51 +74,11 @@ void Potential::pot_register(std::vector& components_list) //--------------------------- // mapping for register //--------------------------- - std::map 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 @@ -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); diff --git a/source/module_elecstate/potentials/potential_new.h b/source/module_elecstate/potentials/potential_new.h index 4485ffa5f2..6e6d51e016 100644 --- a/source/module_elecstate/potentials/potential_new.h +++ b/source/module_elecstate/potentials/potential_new.h @@ -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() { diff --git a/source/module_elecstate/potentials/potential_types.cpp b/source/module_elecstate/potentials/potential_types.cpp new file mode 100644 index 0000000000..f67689a2ca --- /dev/null +++ b/source/module_elecstate/potentials/potential_types.cpp @@ -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 \ No newline at end of file diff --git a/source/module_elecstate/test/CMakeLists.txt b/source/module_elecstate/test/CMakeLists.txt index 6f806d5ce4..b912b7a835 100644 --- a/source/module_elecstate/test/CMakeLists.txt +++ b/source/module_elecstate/test/CMakeLists.txt @@ -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 @@ -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 +) \ No newline at end of file diff --git a/source/module_elecstate/test/potential_new_test.cpp b/source/module_elecstate/test/potential_new_test.cpp new file mode 100644 index 0000000000..a3159da76f --- /dev/null +++ b/source/module_elecstate/test/potential_new_test.cpp @@ -0,0 +1,47 @@ +#include "module_elecstate/potentials/potential_new.h" +#include "module_hamilt_general/module_surchem/surchem.h" + +#include "gtest/gtest.h" + +namespace elecstate +{ +int tmp_xc_func_type = 1; +int get_xc_func_type() +{ + return tmp_xc_func_type; +} + +PotBase* Potential::get_pot_type(const std::string& pot_type) +{ + return new PotBase; +} +} // namespace elecstate + +/************************************************ + * unit test of potential_new.cpp + ***********************************************/ + +/** + * - Tested Functions: + */ + +class PotentialNewTest : public ::testing::Test +{ + protected: + virtual void SetUp() + { + // set up test data + // set up test data + // set up test data + } + + virtual void TearDown() + { + // do nothing + } +}; + +TEST_F(PotentialNewTest, Constructor) +{ + // test constructor +} \ No newline at end of file From b5cdb3e9d8b6377a7efcb629121acccaea37c8de Mon Sep 17 00:00:00 2001 From: root Date: Mon, 22 May 2023 10:00:36 +0000 Subject: [PATCH 2/6] add more tests --- .../test/potential_new_test.cpp | 373 +++++++++++++++++- 1 file changed, 363 insertions(+), 10 deletions(-) diff --git a/source/module_elecstate/test/potential_new_test.cpp b/source/module_elecstate/test/potential_new_test.cpp index a3159da76f..19ded475bd 100644 --- a/source/module_elecstate/test/potential_new_test.cpp +++ b/source/module_elecstate/test/potential_new_test.cpp @@ -1,8 +1,63 @@ -#include "module_elecstate/potentials/potential_new.h" -#include "module_hamilt_general/module_surchem/surchem.h" +#include #include "gtest/gtest.h" +#define private public +#include "module_elecstate/potentials/potential_new.h" +// mock functions +void ModulePW::PW_Basis::initgrids(const double lat0_in, // unit length (unit in bohr) + const ModuleBase::Matrix3 latvec_in, // Unitcell lattice vectors (unit in lat0) + const double gridecut // unit in Ry, ecut to set up grids +) +{ +} +void ModulePW::PW_Basis::initgrids(const double lat0_in, + const ModuleBase::Matrix3 latvec_in, // Unitcell lattice vectors + const int nx_in, + int ny_in, + int nz_in) +{ +} +void ModulePW::PW_Basis::distribute_r() +{ +} +ModulePW::PW_Basis::PW_Basis() +{ +} +ModulePW::PW_Basis::~PW_Basis() +{ +} +ModulePW::FFT::FFT() +{ +} +ModulePW::FFT::~FFT() +{ +} +UnitCell::UnitCell() +{ +} +UnitCell::~UnitCell() +{ +} +Magnetism::Magnetism() +{ +} +Magnetism::~Magnetism() +{ +} +InfoNonlocal::InfoNonlocal() +{ +} +InfoNonlocal::~InfoNonlocal() +{ +} +Charge::Charge() +{ +} +Charge::~Charge() +{ +} + namespace elecstate { int tmp_xc_func_type = 1; @@ -15,6 +70,13 @@ PotBase* Potential::get_pot_type(const std::string& pot_type) { return new PotBase; } + +void Set_GlobalV_Default() +{ + GlobalV::NSPIN = 1; + GlobalV::device_flag = "cpu"; + GlobalV::precision_flag = "double"; +} } // namespace elecstate /************************************************ @@ -28,20 +90,311 @@ PotBase* Potential::get_pot_type(const std::string& pot_type) class PotentialNewTest : public ::testing::Test { protected: + ModulePW::PW_Basis* rhopw = nullptr; + UnitCell* ucell = nullptr; + ModuleBase::matrix* vloc = nullptr; + ModuleBase::ComplexMatrix* structure_factors = nullptr; + double* etxc = nullptr; + double* vtxc = nullptr; + elecstate::Potential* pot = nullptr; virtual void SetUp() { - // set up test data - // set up test data - // set up test data + rhopw = new ModulePW::PW_Basis; + ucell = new UnitCell; + vloc = new ModuleBase::matrix; + structure_factors = new ModuleBase::ComplexMatrix; + etxc = new double; + vtxc = new double; + elecstate::Set_GlobalV_Default(); } - virtual void TearDown() { - // do nothing + if (rhopw != nullptr) + delete rhopw; + if (ucell != nullptr) + delete ucell; + if (vloc != nullptr) + delete vloc; + if (structure_factors != nullptr) + delete structure_factors; + if (etxc != nullptr) + delete etxc; + if (vtxc != nullptr) + delete vtxc; + if (pot != nullptr) + delete pot; } }; -TEST_F(PotentialNewTest, Constructor) +TEST_F(PotentialNewTest, ConstructorCPUDouble) +{ + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); +} + +TEST_F(PotentialNewTest, ConstructorCPUSingle) { - // test constructor -} \ No newline at end of file + rhopw->nrxx = 100; + GlobalV::precision_flag = "single"; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); +} + +TEST_F(PotentialNewTest, ConstructorNRXX0) +{ + rhopw->nrxx = 0; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); +} + +TEST_F(PotentialNewTest, ConstructorXC3) +{ + elecstate::tmp_xc_func_type = 3; + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); + EXPECT_EQ(pot->vofk_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->vofk_effective.nc, 100); +} + +TEST_F(PotentialNewTest, ConstructorGPUDouble) +{ + // this is just a trivial call to the GPU code + rhopw->nrxx = 100; + GlobalV::device_flag = "gpu"; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); +} + +TEST_F(PotentialNewTest, ConstructorGPUSingle) +{ + // this is just a trivial call to the GPU code + rhopw->nrxx = 100; + GlobalV::device_flag = "gpu"; + GlobalV::precision_flag = "single"; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); +} + +TEST_F(PotentialNewTest, Getters) +{ + pot = new elecstate::Potential; + pot->v_effective.create(10, 10); + pot->vofk_effective.create(10,10); + float* foo; + foo = pot->get_v_effective_data(); + EXPECT_EQ(foo, pot->s_v_effective); + foo = pot->get_vofk_effective_data(); + EXPECT_EQ(foo, pot->s_vofk_effective); + double* doo; + doo = pot->get_v_effective_data(); + EXPECT_EQ(doo, pot->d_v_effective); + doo = pot->get_vofk_effective_data(); + EXPECT_EQ(doo, pot->d_vofk_effective); + delete foo; + delete doo; +} + +TEST_F(PotentialNewTest, PotRegister) +{ + pot = new elecstate::Potential; + elecstate::PotBase* pot0 = new elecstate::PotBase; + pot->components.push_back(pot0); + EXPECT_EQ(pot->components.size(), 1); + std::vector compnents_list = {"hartree", "xc"}; + pot->pot_register(compnents_list); + EXPECT_EQ(pot->components.size(), 2); + EXPECT_FALSE(pot->fixed_done); +} + +TEST_F(PotentialNewTest, CalFixedV) +{ + // construct potential + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); + // + std::vector compnents_list = { + "local", + "hartree", + "xc", + "surchem", + "gatefield" + }; + std::vector fixed = {true, false, false, false, true}; + pot->pot_register(compnents_list); + for (int i = 0; icomponents[i]->fixed_mode = fixed[i]; + } + double* vl_pseudo = new double[1000]; + pot->cal_fixed_v(vl_pseudo); + for (int i = 0; i < pot->v_effective_fixed.size(); i++) + { + EXPECT_DOUBLE_EQ(pot->v_effective_fixed[i], 0.0); + } + delete[] vl_pseudo; +} + +TEST_F(PotentialNewTest, CalVeff) +{ + // construct potential + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); + // + std::vector compnents_list = { + "local", + "hartree", + "xc", + "surchem", + "gatefield" + }; + std::vector dynamic = {false, true, true, true, false}; + pot->pot_register(compnents_list); + for (int i = 0; icomponents[i]->dynamic_mode = dynamic[i]; + } + Charge* chg = new Charge; + ModuleBase::matrix v_eff; + v_eff.create(2, 100); + pot->cal_v_eff(chg,this->ucell,v_eff); + for (int i = 0; i < pot->v_effective_fixed.size(); i++) + { + EXPECT_DOUBLE_EQ(pot->v_effective_fixed[i], 0.0); + } + delete chg; +} + +TEST_F(PotentialNewTest, UpdateFromCharge) +{ + // construct potential + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); + // + std::vector compnents_list = { + "local", + "hartree", + "xc", + "surchem", + "gatefield" + }; + std::vector fixed = {true, false, false, false, true}; + std::vector dynamic = {false, true, true, true, false}; + pot->pot_register(compnents_list); + for (int i = 0; icomponents[i]->fixed_mode = fixed[i]; + pot->components[i]->dynamic_mode = dynamic[i]; + } + Charge* chg = new Charge; + EXPECT_FALSE(pot->fixed_done); + pot->update_from_charge(chg, this->ucell); + EXPECT_TRUE(pot->fixed_done); + delete chg; +} + +TEST_F(PotentialNewTest, InitPot) +{ + // construct potential + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); + // + std::vector compnents_list = { + "local", + "hartree", + "xc", + "surchem", + "gatefield" + }; + std::vector fixed = {true, false, false, false, true}; + std::vector dynamic = {false, true, true, true, false}; + pot->pot_register(compnents_list); + for (int i = 0; icomponents[i]->fixed_mode = fixed[i]; + pot->components[i]->dynamic_mode = dynamic[i]; + } + Charge* chg = new Charge; + EXPECT_FALSE(pot->fixed_done); + pot->init_pot(1,chg); + EXPECT_TRUE(pot->fixed_done); + delete chg; +} + +TEST_F(PotentialNewTest, GetVnew) +{ + // construct potential + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); + // + std::vector compnents_list = { + "local", + "hartree", + "xc", + "surchem", + "gatefield" + }; + std::vector fixed = {true, false, false, false, true}; + std::vector dynamic = {false, true, true, true, false}; + pot->pot_register(compnents_list); + for (int i = 0; icomponents[i]->fixed_mode = fixed[i]; + pot->components[i]->dynamic_mode = dynamic[i]; + } + Charge* chg = new Charge; + ModuleBase::matrix vnew; + pot->get_vnew(chg, vnew); + EXPECT_EQ(vnew.nr, GlobalV::NSPIN); + EXPECT_EQ(vnew.nc, 100); + delete chg; +} + +#undef private \ No newline at end of file From 52c212882e47207aa3cb3452ce37be661ca10793 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 23 May 2023 00:43:58 +0000 Subject: [PATCH 3/6] fix compilation error --- source/module_elecstate/test/potential_new_test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/module_elecstate/test/potential_new_test.cpp b/source/module_elecstate/test/potential_new_test.cpp index 19ded475bd..e2a73e5f4b 100644 --- a/source/module_elecstate/test/potential_new_test.cpp +++ b/source/module_elecstate/test/potential_new_test.cpp @@ -45,12 +45,14 @@ Magnetism::Magnetism() Magnetism::~Magnetism() { } +#ifdef __LCAO InfoNonlocal::InfoNonlocal() { } InfoNonlocal::~InfoNonlocal() { } +#endif Charge::Charge() { } From 122b73185a1331b9d311326645765e1f6145b1d3 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 23 May 2023 05:35:32 +0000 Subject: [PATCH 4/6] add more tests --- .../test/potential_new_test.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/source/module_elecstate/test/potential_new_test.cpp b/source/module_elecstate/test/potential_new_test.cpp index e2a73e5f4b..7bfd4d6ee9 100644 --- a/source/module_elecstate/test/potential_new_test.cpp +++ b/source/module_elecstate/test/potential_new_test.cpp @@ -399,4 +399,29 @@ TEST_F(PotentialNewTest, GetVnew) delete chg; } +TEST_F(PotentialNewTest, GetEffectiveV) +{ + // construct potential + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); + EXPECT_EQ(pot->v_effective.nc, 100); + ModuleBase::matrix v_eff_tmp = pot->get_effective_v(); + EXPECT_EQ(v_eff_tmp.nr, GlobalV::NSPIN); + EXPECT_EQ(v_eff_tmp.nc, 100); + for (int ir = 0; ir < v_eff_tmp.nr; ir++) + { + for (int ic = 0; ic < v_eff_tmp.nc; ic++) + { + EXPECT_DOUBLE_EQ(v_eff_tmp(ir, ic), pot->v_effective(ir, ic)); + } + } + //pot->v_effective(0, 0) = 1.0; + //v_eff_tmp(0, 0) = 1.0; + //EXPECT_DOUBLE_EQ(pot->v_effective(0, 0), 1.0); + //EXPECT_DOUBLE_EQ(v_eff_tmp(0, 0), 1.0); +} #undef private \ No newline at end of file From 1dcd6fdbebe2c84cf678809058fe327c9ed0ee24 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 23 May 2023 08:56:08 +0000 Subject: [PATCH 5/6] add more tests --- .../test/potential_new_test.cpp | 137 +++++++++++++----- 1 file changed, 102 insertions(+), 35 deletions(-) diff --git a/source/module_elecstate/test/potential_new_test.cpp b/source/module_elecstate/test/potential_new_test.cpp index 7bfd4d6ee9..dac1fa7b08 100644 --- a/source/module_elecstate/test/potential_new_test.cpp +++ b/source/module_elecstate/test/potential_new_test.cpp @@ -236,11 +236,6 @@ TEST_F(PotentialNewTest, CalFixedV) // construct potential rhopw->nrxx = 100; pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); - EXPECT_TRUE(pot->fixed_mode); - EXPECT_TRUE(pot->dynamic_mode); - EXPECT_EQ(pot->v_effective_fixed.size(), 100); - EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); - EXPECT_EQ(pot->v_effective.nc, 100); // std::vector compnents_list = { "local", @@ -269,11 +264,6 @@ TEST_F(PotentialNewTest, CalVeff) // construct potential rhopw->nrxx = 100; pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); - EXPECT_TRUE(pot->fixed_mode); - EXPECT_TRUE(pot->dynamic_mode); - EXPECT_EQ(pot->v_effective_fixed.size(), 100); - EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); - EXPECT_EQ(pot->v_effective.nc, 100); // std::vector compnents_list = { "local", @@ -304,11 +294,6 @@ TEST_F(PotentialNewTest, UpdateFromCharge) // construct potential rhopw->nrxx = 100; pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); - EXPECT_TRUE(pot->fixed_mode); - EXPECT_TRUE(pot->dynamic_mode); - EXPECT_EQ(pot->v_effective_fixed.size(), 100); - EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); - EXPECT_EQ(pot->v_effective.nc, 100); // std::vector compnents_list = { "local", @@ -337,11 +322,6 @@ TEST_F(PotentialNewTest, InitPot) // construct potential rhopw->nrxx = 100; pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); - EXPECT_TRUE(pot->fixed_mode); - EXPECT_TRUE(pot->dynamic_mode); - EXPECT_EQ(pot->v_effective_fixed.size(), 100); - EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); - EXPECT_EQ(pot->v_effective.nc, 100); // std::vector compnents_list = { "local", @@ -370,11 +350,6 @@ TEST_F(PotentialNewTest, GetVnew) // construct potential rhopw->nrxx = 100; pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); - EXPECT_TRUE(pot->fixed_mode); - EXPECT_TRUE(pot->dynamic_mode); - EXPECT_EQ(pot->v_effective_fixed.size(), 100); - EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); - EXPECT_EQ(pot->v_effective.nc, 100); // std::vector compnents_list = { "local", @@ -399,29 +374,121 @@ TEST_F(PotentialNewTest, GetVnew) delete chg; } -TEST_F(PotentialNewTest, GetEffectiveV) +TEST_F(PotentialNewTest, GetEffectiveVmatrix) { // construct potential rhopw->nrxx = 100; pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); - EXPECT_TRUE(pot->fixed_mode); - EXPECT_TRUE(pot->dynamic_mode); - EXPECT_EQ(pot->v_effective_fixed.size(), 100); - EXPECT_EQ(pot->v_effective.nr, GlobalV::NSPIN); - EXPECT_EQ(pot->v_effective.nc, 100); + // ModuleBase::matrix v_eff_tmp = pot->get_effective_v(); + const ModuleBase::matrix v_eff_tmp_const = pot->get_effective_v(); EXPECT_EQ(v_eff_tmp.nr, GlobalV::NSPIN); EXPECT_EQ(v_eff_tmp.nc, 100); + EXPECT_EQ(v_eff_tmp_const.nr, GlobalV::NSPIN); + EXPECT_EQ(v_eff_tmp_const.nc, 100); for (int ir = 0; ir < v_eff_tmp.nr; ir++) { for (int ic = 0; ic < v_eff_tmp.nc; ic++) { EXPECT_DOUBLE_EQ(v_eff_tmp(ir, ic), pot->v_effective(ir, ic)); + EXPECT_DOUBLE_EQ(v_eff_tmp_const(ir, ic), pot->v_effective(ir, ic)); } } - //pot->v_effective(0, 0) = 1.0; - //v_eff_tmp(0, 0) = 1.0; - //EXPECT_DOUBLE_EQ(pot->v_effective(0, 0), 1.0); - //EXPECT_DOUBLE_EQ(v_eff_tmp(0, 0), 1.0); +} + +TEST_F(PotentialNewTest, GetEffectiveVarray) +{ + // construct potential + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + // + double* v_eff_tmp = pot->get_effective_v(0); + const double* v_eff_tmp_const = pot->get_effective_v(0); + for (int ic = 0; ic < rhopw->nrxx; ic++) + { + EXPECT_DOUBLE_EQ(v_eff_tmp[ic], pot->v_effective(0, ic)); + EXPECT_DOUBLE_EQ(v_eff_tmp_const[ic], pot->v_effective(0, ic)); + } + v_eff_tmp[0] = 1.0; + EXPECT_DOUBLE_EQ(pot->v_effective(0, 0), 1.0); + EXPECT_DOUBLE_EQ(v_eff_tmp_const[0], 1.0); +} + +TEST_F(PotentialNewTest, GetEffectiveVarrayNullptr) +{ + pot = new elecstate::Potential; + EXPECT_EQ(pot->v_effective.nc, 0); + double* v_eff_tmp = pot->get_effective_v(0); + const double* v_eff_tmp_const = pot->get_effective_v(0); + EXPECT_EQ(v_eff_tmp, nullptr); + EXPECT_EQ(v_eff_tmp_const, nullptr); +} + +TEST_F(PotentialNewTest, GetEffectiveVofkmatrix) +{ + // construct potential + elecstate::tmp_xc_func_type = 3; + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + // + ModuleBase::matrix vofk_eff_tmp = pot->get_effective_vofk(); + const ModuleBase::matrix vofk_eff_tmp_const = pot->get_effective_vofk(); + EXPECT_EQ(vofk_eff_tmp.nr, GlobalV::NSPIN); + EXPECT_EQ(vofk_eff_tmp.nc, 100); + EXPECT_EQ(vofk_eff_tmp_const.nr, GlobalV::NSPIN); + EXPECT_EQ(vofk_eff_tmp_const.nc, 100); + for (int ir = 0; ir < vofk_eff_tmp.nr; ir++) + { + for (int ic = 0; ic < vofk_eff_tmp.nc; ic++) + { + EXPECT_DOUBLE_EQ(vofk_eff_tmp(ir, ic), pot->vofk_effective(ir, ic)); + EXPECT_DOUBLE_EQ(vofk_eff_tmp_const(ir, ic), pot->vofk_effective(ir, ic)); + } + } +} + +TEST_F(PotentialNewTest, GetEffectiveVofkarray) +{ + // construct potential + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + // + double* vofk_eff_tmp = pot->get_effective_vofk(0); + const double* vofk_eff_tmp_const = pot->get_effective_vofk(0); + for (int ic = 0; ic < rhopw->nrxx; ic++) + { + EXPECT_DOUBLE_EQ(vofk_eff_tmp[ic], pot->vofk_effective(0, ic)); + EXPECT_DOUBLE_EQ(vofk_eff_tmp_const[ic], pot->vofk_effective(0, ic)); + } + vofk_eff_tmp[0] = 1.0; + EXPECT_DOUBLE_EQ(pot->vofk_effective(0, 0), 1.0); + EXPECT_DOUBLE_EQ(vofk_eff_tmp_const[0], 1.0); +} + +TEST_F(PotentialNewTest, GetEffectiveVofkarrayNullptr) +{ + pot = new elecstate::Potential; + EXPECT_EQ(pot->v_effective.nc, 0); + double* vofk_eff_tmp = pot->get_effective_vofk(0); + const double* vofk_eff_tmp_const = pot->get_effective_vofk(0); + EXPECT_EQ(vofk_eff_tmp, nullptr); + EXPECT_EQ(vofk_eff_tmp_const, nullptr); +} + +TEST_F(PotentialNewTest, GetFixedV) +{ + rhopw->nrxx = 100; + pot = new elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc); + EXPECT_TRUE(pot->fixed_mode); + EXPECT_TRUE(pot->dynamic_mode); + EXPECT_EQ(pot->v_effective_fixed.size(), 100); + double* v_eff_fixed_tmp = pot->get_fixed_v(); + const double* v_eff_fixed_tmp_const = pot->get_fixed_v(); + for (int ic = 0; ic < rhopw->nrxx; ic++) + { + v_eff_fixed_tmp[ic] = ic; + EXPECT_DOUBLE_EQ(v_eff_fixed_tmp[ic], pot->v_effective_fixed[ic]); + EXPECT_DOUBLE_EQ(v_eff_fixed_tmp_const[ic], pot->v_effective_fixed[ic]); + } } #undef private \ No newline at end of file From e86caaf8b93b516e075095da91d970c835eca283 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 24 May 2023 01:55:35 +0000 Subject: [PATCH 6/6] add comments in potential_new_test.cpp --- .../test/potential_new_test.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/source/module_elecstate/test/potential_new_test.cpp b/source/module_elecstate/test/potential_new_test.cpp index dac1fa7b08..ad69336e23 100644 --- a/source/module_elecstate/test/potential_new_test.cpp +++ b/source/module_elecstate/test/potential_new_test.cpp @@ -87,6 +87,30 @@ void Set_GlobalV_Default() /** * - Tested Functions: + * - Constructor: elecstate::Potential(rhopw, ucell, vloc, structure_factors, etxc, vtxc) and elecstate::Potential::allocate() + * - potentials are divided into 2 types: fixed and dynamic + * - fixed potentials: loc, gatefield that are independent of rho + * - dynamic potentials: hartree, xc, surchem that are dependent of rho + * - Getters: elecstate::Potential::get_v_effective_data() and elecstate::Potential::get_vofk_effective_data() + * - get the pointers to v_effective and vofk_effective + * - PotRegist: elecstate::Potential::pot_egist(components_list) + * - add new objects of potentials that are derived classes of PotBase + * - CalFixedV: elecstate::Potential::cal_fixed_v() + * - calculate the fixed potentials: v_effective_fixed + * - CalVeff: elecstate::Potential::cal_v_eff() + * - calculate v_effective by adding v_effective_fixed and adding the dynamic potentials + * - UpdateFromCharge: elecstate::Potential::update_from_charge() + * - calls cal_fixed_v and cal_v_eff to update v_effective from rho + * - InitPot: elecstate::Potential::init_pot() + * - using istep and update_from_charge to initialize v_effective + * - GetVnew: elecstate::Potential::get_vnew() + * - used later for scf correction to the forces + * - GetEffective: elecstate::Potential::get_effective_v() + * - get the matrix reference or double pointer v_effective + * - GetEffectiveVOfK: elecstate::Potential::get_effective_vofk() + * - get the matrix reference or double pointer vofk_effective + * - GetFixedV: elecstate::Potential::get_fixed_v() + * - get the double pointer to v_effective_fixed */ class PotentialNewTest : public ::testing::Test