Skip to content

Commit d670348

Browse files
19helloFei Yang
andauthored
refactor: use BaseCell in ESolver interface (#7700)
* refactor: use BaseCell in ESolver interface * fix: align LCAO others override with BaseCell * fix: link BaseCell in MD unit tests --------- Co-authored-by: Fei Yang <2501213217@stu.pku.edu.cn>
1 parent 1ee758f commit d670348

44 files changed

Lines changed: 876 additions & 470 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

source/source_cell/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_subdirectory(module_neighlist)
55
add_library(
66
cell
77
OBJECT
8+
base_cell.cpp
89
atom_pseudo.cpp
910
atom_spec.cpp
1011
pseudo.cpp

source/source_cell/base_cell.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "source_cell/base_cell.h"
2+
3+
#include "source_base/tool_quit.h"
4+
5+
void BaseCell::require_kind(const Kind& expected, const char* caller) const
6+
{
7+
if (this->kind() != expected)
8+
{
9+
const char* required_cell = expected == Kind::unit_cell ? "UnitCell" : "MDCell";
10+
ModuleBase::WARNING_QUIT(caller, std::string("This operation only supports ") + required_cell + ".");
11+
}
12+
}

source/source_cell/base_cell.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef BASE_CELL_H
2+
#define BASE_CELL_H
3+
4+
#include "source_base/matrix3.h"
5+
6+
class BaseCell
7+
{
8+
public:
9+
enum class Kind
10+
{
11+
unit_cell,
12+
md_cell
13+
};
14+
15+
virtual ~BaseCell() = default;
16+
17+
Kind kind() const
18+
{
19+
return get_kind();
20+
}
21+
22+
int nat() const
23+
{
24+
return get_nat();
25+
}
26+
27+
double lat0() const
28+
{
29+
return get_lat0();
30+
}
31+
32+
double omega() const
33+
{
34+
return get_omega();
35+
}
36+
37+
const ModuleBase::Matrix3& latvec() const
38+
{
39+
return get_latvec();
40+
}
41+
42+
const ModuleBase::Matrix3& GT() const
43+
{
44+
return get_GT();
45+
}
46+
47+
void require_kind(const Kind& expected, const char* caller) const;
48+
49+
private:
50+
virtual Kind get_kind() const = 0;
51+
virtual int get_nat() const = 0;
52+
virtual double get_lat0() const = 0;
53+
virtual double get_omega() const = 0;
54+
virtual const ModuleBase::Matrix3& get_latvec() const = 0;
55+
virtual const ModuleBase::Matrix3& get_GT() const = 0;
56+
};
57+
58+
#endif

source/source_cell/unitcell.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
#include "source_cell/magnetism.h"
88
#include "module_symmetry/symmetry.h"
99
#include "source_cell/module_neighlist/atom_provider.h"
10+
#include "source_cell/base_cell.h"
1011
#include "source_cell/nonlocal_info_base.h"
1112

1213
/**
1314
* @brief Provide the basic information about unitcell.
1415
*/
15-
class UnitCell : public AtomProvider {
16+
class UnitCell : public AtomProvider, public BaseCell {
1617
public:
1718
double get_lat0() const override {
1819
return lat0;
@@ -287,6 +288,22 @@ class UnitCell : public AtomProvider {
287288
std::vector<ModuleBase::Vector3<double>> get_lambda() const;
288289
/// @brief get constrain for deltaspin
289290
std::vector<ModuleBase::Vector3<int>> get_constrain() const;
291+
292+
private:
293+
Kind get_kind() const override
294+
{
295+
return Kind::unit_cell;
296+
}
297+
298+
int get_nat() const override
299+
{
300+
return nat;
301+
}
302+
303+
const ModuleBase::Matrix3& get_GT() const override
304+
{
305+
return GT;
306+
}
290307
};
291308

292309
#endif // unitcell class

source/source_esolver/esolver.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,4 @@ ESolver* init_esolver(const Input_para& inp)
318318
+ " line " + std::to_string(__LINE__));
319319
}
320320

321-
322321
} // namespace ModuleESolver

source/source_esolver/esolver.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define ESOLVER_H
33

44
#include "source_base/matrix.h"
5+
#include "source_cell/base_cell.h"
56
#include "source_cell/unitcell.h"
67

78
struct Input_para;
@@ -24,26 +25,26 @@ class ESolver
2425
}
2526

2627
//! initialize the energy solver by using input parameters and cell modules
27-
virtual void before_all_runners(UnitCell& ucell, const Input_para& inp) = 0;
28+
virtual void before_all_runners(BaseCell& cell, const Input_para& inp) = 0;
2829

2930
//! run energy solver
30-
virtual void runner(UnitCell& cell, const int istep) = 0;
31+
virtual void runner(BaseCell& cell, const int istep) = 0;
3132

3233
//! perform post processing calculations
33-
virtual void after_all_runners(UnitCell& ucell) = 0;
34+
virtual void after_all_runners(BaseCell& cell) = 0;
3435

3536
//! deal with exx and other calculation than scf/md/relax/cell-relax:
3637
//! such as nscf, get_wf and get_pchg
37-
virtual void others(UnitCell& ucell, const int istep) {};
38+
virtual void others(BaseCell&, const int) {}
3839

3940
//! calculate total energy of a given system
4041
virtual double cal_energy() = 0;
4142

4243
//! calcualte forces for the atoms in the given cell
43-
virtual void cal_force(UnitCell& ucell, ModuleBase::matrix& force) = 0;
44+
virtual void cal_force(BaseCell& cell, ModuleBase::matrix& force) = 0;
4445

4546
//! calcualte stress of given cell
46-
virtual void cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) = 0;
47+
virtual void cal_stress(BaseCell& cell, ModuleBase::matrix& stress) = 0;
4748

4849
bool conv_esolver = true; // whether esolver is converged
4950

@@ -54,7 +55,6 @@ class ESolver
5455
* @brief A subrutine called in init_esolver()
5556
* This function returns type of ESolver
5657
* Based on PARAM.inp.basis_type and PARAM.inp.esolver_type
57-
*
5858
* @return [out] std::string The type of ESolver
5959
*/
6060
std::string determine_type();
@@ -70,8 +70,6 @@ std::string determine_type();
7070
*/
7171
ESolver* init_esolver(const Input_para& inp);
7272

73-
74-
7573
} // namespace ModuleESolver
7674

7775
#endif

source/source_esolver/esolver_dfpt_pw.cpp

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,97 @@
77
// ============================================================
88

99
#include "esolver_dfpt_pw.h"
10+
1011
#include "source_base/tool_quit.h"
1112

12-
namespace ModuleESolver {
13+
namespace ModuleESolver
14+
{
1315

14-
ESolver_DFPT_PW::ESolver_DFPT_PW() {
16+
ESolver_DFPT_PW::ESolver_DFPT_PW()
17+
{
1518
this->classname = "ESolver_DFPT_PW";
1619
this->basisname = "PW";
1720
gs_done_ = false;
1821
dfpt_ = nullptr;
1922
}
2023

21-
ESolver_DFPT_PW::~ESolver_DFPT_PW() {
22-
if (dfpt_ != nullptr) {
24+
ESolver_DFPT_PW::~ESolver_DFPT_PW()
25+
{
26+
if (dfpt_ != nullptr)
27+
{
2328
delete dfpt_;
2429
dfpt_ = nullptr;
2530
}
2631
}
2732

28-
void ESolver_DFPT_PW::before_all_runners(UnitCell& ucell, const Input_para& inp) {
33+
void ESolver_DFPT_PW::before_all_runners(BaseCell& basecell, const Input_para& inp)
34+
{
35+
basecell.require_kind(BaseCell::Kind::unit_cell, __FUNCTION__);
36+
UnitCell& ucell = static_cast<UnitCell&>(basecell);
37+
2938
ModuleBase::TITLE("ESolver_DFPT_PW", "before_all_runners");
30-
39+
3140
ESolver_KS_PW<std::complex<double>, base_device::DEVICE_CPU>::before_all_runners(ucell, inp);
32-
41+
3342
init_dfpt(ucell);
3443
}
3544

36-
void ESolver_DFPT_PW::runner(UnitCell& ucell, const int istep) {
45+
void ESolver_DFPT_PW::runner(BaseCell& basecell, const int istep)
46+
{
47+
basecell.require_kind(BaseCell::Kind::unit_cell, __FUNCTION__);
48+
UnitCell& ucell = static_cast<UnitCell&>(basecell);
49+
3750
ModuleBase::TITLE("ESolver_DFPT_PW", "runner");
38-
39-
if (!gs_done_) {
51+
52+
if (!gs_done_)
53+
{
4054
run_gs(ucell);
4155
gs_done_ = true;
4256
}
43-
44-
if (dfpt_ != nullptr) {
57+
58+
if (dfpt_ != nullptr)
59+
{
4560
dfpt_->run();
4661
}
47-
62+
4863
run_post_process(ucell);
4964
}
5065

51-
void ESolver_DFPT_PW::after_all_runners(UnitCell& ucell) {
66+
void ESolver_DFPT_PW::after_all_runners(BaseCell& basecell)
67+
{
68+
basecell.require_kind(BaseCell::Kind::unit_cell, __FUNCTION__);
69+
UnitCell& ucell = static_cast<UnitCell&>(basecell);
70+
5271
ModuleBase::TITLE("ESolver_DFPT_PW", "after_all_runners");
53-
72+
5473
ESolver_KS_PW<std::complex<double>, base_device::DEVICE_CPU>::after_all_runners(ucell);
5574
}
5675

57-
void ESolver_DFPT_PW::run_gs(UnitCell& ucell) {
76+
void ESolver_DFPT_PW::run_gs(UnitCell& ucell)
77+
{
5878
ModuleBase::TITLE("ESolver_DFPT_PW", "run_gs");
59-
79+
6080
ESolver_KS_PW<std::complex<double>, base_device::DEVICE_CPU>::runner(ucell, 0);
6181
}
6282

63-
void ESolver_DFPT_PW::init_dfpt(UnitCell& ucell) {
83+
void ESolver_DFPT_PW::init_dfpt(UnitCell& ucell)
84+
{
6485
ModuleBase::TITLE("ESolver_DFPT_PW", "init_dfpt");
65-
86+
6687
dfpt_ = new ModuleDFPT::DFPT_PW();
67-
68-
// dfpt_->init(ucell, *this->stp.psi, this->pelec->nelec, PARAM.inp.ecutwfc);
69-
88+
89+
// dfpt_->init(ucell, *this->stp.psi, this->pelec->nelec, PARAM.inp.ecutwfc);
90+
7091
dfpt_->set_parameters("dfpt.in");
71-
92+
7293
dfpt_->set_qmesh(1, 1, 1);
73-
94+
7495
dfpt_->set_conv_thr(1e-8);
7596
dfpt_->set_max_iter(100);
7697
}
7798

78-
void ESolver_DFPT_PW::run_post_process(UnitCell& ucell) {
99+
void ESolver_DFPT_PW::run_post_process(UnitCell& ucell)
100+
{
79101
ModuleBase::TITLE("ESolver_DFPT_PW", "run_post_process");
80102
}
81103

source/source_esolver/esolver_dfpt_pw.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,28 @@
1212
#include "esolver_ks_pw.h"
1313
#include "source_pw/module_dfpt/dfpt_pw.h"
1414

15-
namespace ModuleESolver {
15+
namespace ModuleESolver
16+
{
1617

17-
class ESolver_DFPT_PW : public ESolver_KS_PW<std::complex<double>, base_device::DEVICE_CPU> {
18-
public:
18+
class ESolver_DFPT_PW : public ESolver_KS_PW<std::complex<double>, base_device::DEVICE_CPU>
19+
{
20+
public:
1921
ESolver_DFPT_PW();
2022
~ESolver_DFPT_PW();
21-
22-
void before_all_runners(UnitCell& ucell, const Input_para& inp) override;
23-
void runner(UnitCell& ucell, const int istep) override;
24-
void after_all_runners(UnitCell& ucell) override;
25-
26-
protected:
23+
24+
void before_all_runners(BaseCell& basecell, const Input_para& inp) override;
25+
void runner(BaseCell& basecell, const int istep) override;
26+
void after_all_runners(BaseCell& basecell) override;
27+
28+
protected:
2729
ModuleDFPT::DFPT_PW* dfpt_ = nullptr;
28-
30+
2931
bool gs_done_ = false;
30-
32+
3133
void run_gs(UnitCell& ucell);
32-
34+
3335
void init_dfpt(UnitCell& ucell);
34-
36+
3537
void run_post_process(UnitCell& ucell);
3638
};
3739

0 commit comments

Comments
 (0)