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 elecstate_print.cpp #2458

Merged
merged 2 commits into from
May 17, 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: 1 addition & 1 deletion source/module_elecstate/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
list(APPEND objects
elecstate.cpp
elecstate_getters.cpp
elecstate_energy.cpp
elecstate_print.cpp
elecstate_pw.cpp
Expand All @@ -20,7 +21,6 @@ list(APPEND objects
module_charge/symmetry_rhog.cpp
fp_energy.cpp
magnetism.cpp
elecstate_getters.cpp
occupy.cpp
)

Expand Down
38 changes: 38 additions & 0 deletions source/module_elecstate/elecstate_getters.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "module_elecstate/elecstate_getters.h"

#include "module_cell/unitcell.h"
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include "module_io/input.h"

namespace elecstate
{
Expand All @@ -9,4 +12,39 @@ double get_ucell_omega()
return GlobalC::ucell.omega;
}

std::string get_input_vdw_method()
hongriTianqi marked this conversation as resolved.
Show resolved Hide resolved
{
return INPUT.vdw_method;
}

double get_ucell_tot_magnetization()
{
return GlobalC::ucell.magnet.tot_magnetization;
}

double get_ucell_abs_magnetization()
{
return GlobalC::ucell.magnet.abs_magnetization;
}

double get_ucell_tot_magnetization_nc_x()
{
return GlobalC::ucell.magnet.tot_magnetization_nc[0];
}

double get_ucell_tot_magnetization_nc_y()
{
return GlobalC::ucell.magnet.tot_magnetization_nc[1];
}

double get_ucell_tot_magnetization_nc_z()
{
return GlobalC::ucell.magnet.tot_magnetization_nc[2];
}

std::string get_ks_solver_type()
{
return GlobalV::KS_SOLVER;
}

} // namespace elecstate
22 changes: 21 additions & 1 deletion source/module_elecstate/elecstate_getters.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
#ifndef ELECSTATE_GETTERS_H
#define ELECSTATE_GETTERS_H

#include <string>
// Description: Getters for elecstate module
namespace elecstate
{

// get the value of GlobalC::ucell.omega
/// @brief get the value of GlobalC::ucell.omega
double get_ucell_omega();
/// @brief get the value of INPUT.vdw_method
std::string get_input_vdw_method();
/// @brief get the value of GlobalC::ucell.magnet.tot_magnetization
double get_ucell_tot_magnetization();
/// @brief get the value of GlobalC::ucell.magnet.abs_magnetization
double get_ucell_abs_magnetization();
/// @brief get the value of GlobalC::ucell.magnet.tot_magnetization_nc[0]
double get_ucell_tot_magnetization_nc_x();
/// @brief get the value of GlobalC::ucell.magnet.tot_magnetization_nc[1]
double get_ucell_tot_magnetization_nc_y();
/// @brief get the value of GlobalC::ucell.magnet.tot_magnetization_nc[2]
double get_ucell_tot_magnetization_nc_z();
/// @brief get the type of KS_SOLVER
std::string get_ks_solver_type();

} // namespace elecstate

#endif // ELECSTATE_GETTERS_H
53 changes: 27 additions & 26 deletions source/module_elecstate/elecstate_print.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "elecstate.h"
#include "elecstate_getters.h"
#include "module_base/global_variable.h"
#include "module_cell/unitcell.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/module_deepks/LCAO_deepks.h"
#include "module_io/input.h"
namespace elecstate
{
/// @brief print and check for band energy and occupations
Expand Down Expand Up @@ -39,9 +38,10 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
ModuleBase::TITLE("ESolver_KS_PW", "print_eigenvalue");

ofs << "\n STATE ENERGY(eV) AND OCCUPATIONS ";
ofs << std::setprecision(5);
for (int ik = 0; ik < this->klist->nks; ik++)
{
ofs << std::setprecision(5);
ofs << std::setiosflags(ios::showpoint);
if (ik == 0)
{
ofs << " NSPIN == " << GlobalV::NSPIN << std::endl;
Expand All @@ -62,9 +62,8 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
{
if (this->klist->isk[ik] == 0)
{
ofs << " " << ik + 1 << "/" << this->klist->nks / 2
<< " kpoint (Cartesian) = " << this->klist->kvec_c[ik].x << " " << this->klist->kvec_c[ik].y << " "
<< this->klist->kvec_c[ik].z << " (" << this->klist->ngk[ik] << " pws)" << std::endl;
ofs << " " << ik + 1 << "/" << this->klist->nks / 2
<< " kpoint (Cartesian) = " << this->klist->kvec_c[ik].x << " " << this->klist->kvec_c[ik].y << " " << this->klist->kvec_c[ik].z << " (" << this->klist->ngk[ik] << " pws)" << std::endl;

ofs << std::setprecision(6);
}
Expand All @@ -86,8 +85,8 @@ void ElecState::print_eigenvalue(std::ofstream& ofs)
ofs << std::setprecision(6);
}

GlobalV::ofs_running << std::setprecision(6);
GlobalV::ofs_running << std::setiosflags(ios::showpoint);
ofs << std::setprecision(6);
ofs << std::setiosflags(ios::showpoint);
for (int ib = 0; ib < this->ekb.nc; ib++)
{
ofs << std::setw(8) << ib + 1 << std::setw(15) << this->ekb(ik, ib) * ModuleBase::Ry_to_eV << std::setw(15)
Expand Down Expand Up @@ -185,11 +184,12 @@ void ElecState::print_etot(const bool converged,
this->print_format("E_Ewald", this->f_en.ewald_energy);
this->print_format("E_demet", this->f_en.demet); // mohan add 2011-12-02
this->print_format("E_descf", this->f_en.descf);
if (INPUT.vdw_method == "d2") // Peize Lin add 2014-04, update 2021-03-09
std::string vdw_method = get_input_vdw_method();
if (vdw_method == "d2") // Peize Lin add 2014-04, update 2021-03-09
{
this->print_format("E_vdwD2", this->f_en.evdw);
}
else if (INPUT.vdw_method == "d3_0" || INPUT.vdw_method == "d3_bj") // jiyy add 2019-05, update 2021-05-02
else if (vdw_method == "d3_0" || vdw_method == "d3_bj") // jiyy add 2019-05, update 2021-05-02
{
this->print_format("E_vdwD3", this->f_en.evdw);
}
Expand Down Expand Up @@ -255,33 +255,34 @@ void ElecState::print_etot(const bool converged,

// xiaohui add 2013-09-02, Peize Lin update 2020.11.14
std::string label;
if (GlobalV::KS_SOLVER == "cg")
std::string ks_solver_type = get_ks_solver_type();
if (ks_solver_type == "cg")
{
label = "CG";
}
else if (GlobalV::KS_SOLVER == "lapack")
else if (ks_solver_type == "lapack")
{
label = "LA";
}
else if (GlobalV::KS_SOLVER == "genelpa")
else if (ks_solver_type == "genelpa")
{
label = "GE";
}
else if (GlobalV::KS_SOLVER == "dav")
else if (ks_solver_type == "dav")
{
label = "DA";
}
else if (GlobalV::KS_SOLVER == "scalapack_gvx")
else if (ks_solver_type == "scalapack_gvx")
{
label = "GV";
}
else if (GlobalV::KS_SOLVER == "cusolver")
else if (ks_solver_type == "cusolver")
{
label = "CU";
}
else
{
ModuleBase::WARNING_QUIT("Energy", "print_etot");
ModuleBase::WARNING_QUIT("Energy", "print_etot found unknown ks_solver_type");
}
ss << label << iter;
// xiaohui add 2013-09-02
Expand Down Expand Up @@ -310,16 +311,16 @@ void ElecState::print_etot(const bool converged,
if (GlobalV::NSPIN == 2)
{
std::cout << std::setprecision(2);
std::cout << std::setw(10) << GlobalC::ucell.magnet.tot_magnetization;
std::cout << std::setw(10) << GlobalC::ucell.magnet.abs_magnetization;
std::cout << std::setw(10) << get_ucell_tot_magnetization();
std::cout << std::setw(10) << get_ucell_abs_magnetization();
}
else if (GlobalV::NSPIN == 4 && GlobalV::NONCOLIN)
{
std::cout << std::setprecision(2);
std::cout << std::setw(10) << GlobalC::ucell.magnet.tot_magnetization_nc[0] << std::setw(10)
<< GlobalC::ucell.magnet.tot_magnetization_nc[1] << std::setw(10)
<< GlobalC::ucell.magnet.tot_magnetization_nc[2];
std::cout << std::setw(10) << GlobalC::ucell.magnet.abs_magnetization;
std::cout << std::setw(10) << get_ucell_tot_magnetization_nc_x() << std::setw(10)
<< get_ucell_tot_magnetization_nc_y() << std::setw(10)
<< get_ucell_tot_magnetization_nc_z();
std::cout << std::setw(10) << get_ucell_abs_magnetization();
}
if (scf_thr > 1.0)
{
Expand Down Expand Up @@ -348,8 +349,8 @@ void ElecState::print_etot(const bool converged,
if (GlobalV::NSPIN == 2)
{
std::cout << std::setprecision(2);
std::cout << std::setw(10) << GlobalC::ucell.magnet.tot_magnetization;
std::cout << std::setw(10) << GlobalC::ucell.magnet.abs_magnetization;
std::cout << std::setw(10) << get_ucell_tot_magnetization();
std::cout << std::setw(10) << get_ucell_abs_magnetization();
}
std::cout << std::setprecision(6);
std::cout << std::setw(15) << this->f_en.etot * ModuleBase::Ry_to_eV;
Expand All @@ -369,7 +370,7 @@ void ElecState::print_etot(const bool converged,
return;
}

/// @brief function to print name and value
/// @brief function to print name, value and value*Ry_to_eV
/// @param name: name
/// @param value: value
void ElecState::print_format(const std::string& name, const double& value)
Expand Down
12 changes: 9 additions & 3 deletions source/module_elecstate/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ AddTest(
)

AddTest(
TARGET fp_energy_UT
TARGET elecstate_fp_energy
LIBS ${math_libs} base device
SOURCES fp_energy_test.cpp ../fp_energy.cpp
)
SOURCES elecstate_fp_energy_test.cpp ../fp_energy.cpp
)

AddTest(
TARGET elecstate_print
LIBS ${math_libs} base device
SOURCES elecstate_print_test.cpp ../elecstate_print.cpp
)
Loading