Skip to content

Commit 4f451c4

Browse files
author
Zanthoxylum
committed
Refactor DFPT unit tests: share the cubic-cell/stru_lib fixture between pw_data and pw_run tests (dfpt_stru_fixture)
1 parent ae3e9d1 commit 4f451c4

5 files changed

Lines changed: 129 additions & 171 deletions

File tree

source/source_pw/module_dfpt/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ AddTest(
1313
../../../source_psi/psi.cpp
1414
# Shared ctor/dtor stubs for the cell/spepot/Plus_U link closures.
1515
dfpt_test_mocks.cpp
16+
# Shared cubic-cell / stru_lib fixture.
17+
dfpt_stru_fixture.cpp
1618
)
1719

1820
AddTest(
@@ -50,4 +52,6 @@ AddTest(
5052
../../../source_psi/psi.cpp
5153
# Shared ctor/dtor stubs for the cell/spepot/charge/Plus_U closures.
5254
dfpt_test_mocks.cpp
55+
# Shared cubic-cell / stru_lib fixture.
56+
dfpt_stru_fixture.cpp
5357
)

source/source_pw/module_dfpt/test/dfpt_pw_data_test.cpp

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#include "source_cell/unitcell.h"
1111
#include "source_cell/magnetism.h"
1212
#undef private
13-
#include "source_base/mathzone.h"
1413
#include "source_base/parallel_global.h"
1514
#include "source_base/global_variable.h"
1615
#include "source_pw/module_dfpt/dfpt_pw_data.h"
1716
#include "source_pw/module_pwdft/dftu_base.h"
17+
#include "dfpt_stru_fixture.h"
1818

1919
// ctor/dtor stubs for the cell/spepot link closures live in the shared
2020
// dfpt_test_mocks.cpp compiled into every DFPT test binary.
@@ -35,34 +35,7 @@
3535
* - per-(q, irrep) SCF ledger (B4: sunk from DFPT_IrrepData)
3636
*/
3737

38-
// abbreviated from module_symmetry/test/symm_test.cpp and klist_test.cpp
39-
struct atomtype_
40-
{
41-
std::string atomname;
42-
std::vector<std::vector<double>> coordinate;
43-
};
44-
45-
struct stru_
46-
{
47-
int ibrav;
48-
std::string point_group; // Schoenflies symbol
49-
std::string point_group_hm; // Hermann-Mauguin notation.
50-
std::string space_group;
51-
std::vector<double> cell;
52-
std::vector<atomtype_> all_type;
53-
};
54-
55-
std::vector<stru_> stru_lib{stru_{1,
56-
"O_h",
57-
"m-3m",
58-
"Pm-3m",
59-
std::vector<double>{1., 0., 0., 0., 1., 0., 0., 0., 1.},
60-
std::vector<atomtype_>{atomtype_{"C",
61-
std::vector<std::vector<double>>{
62-
{0., 0., 0.},
63-
}}}}};
64-
65-
class DFPT_PW_DataTest : public testing::Test
38+
class DFPT_PW_DataTest : public DFPTStruTestFixture
6639
{
6740
protected:
6841
ModuleCell::QList qlist;
@@ -72,63 +45,6 @@ class DFPT_PW_DataTest : public testing::Test
7245
std::ofstream ofs_running;
7346
std::string output;
7447

75-
UnitCell ucell;
76-
void construct_ucell(stru_& stru)
77-
{
78-
std::vector<atomtype_> coord = stru.all_type;
79-
ucell.a1 = ModuleBase::Vector3<double>(stru.cell[0], stru.cell[1], stru.cell[2]);
80-
ucell.a2 = ModuleBase::Vector3<double>(stru.cell[3], stru.cell[4], stru.cell[5]);
81-
ucell.a3 = ModuleBase::Vector3<double>(stru.cell[6], stru.cell[7], stru.cell[8]);
82-
ucell.ntype = stru.all_type.size();
83-
ucell.atoms = new Atom[ucell.ntype];
84-
ucell.nat = 0;
85-
ucell.latvec.e11 = ucell.a1.x;
86-
ucell.latvec.e12 = ucell.a1.y;
87-
ucell.latvec.e13 = ucell.a1.z;
88-
ucell.latvec.e21 = ucell.a2.x;
89-
ucell.latvec.e22 = ucell.a2.y;
90-
ucell.latvec.e23 = ucell.a2.z;
91-
ucell.latvec.e31 = ucell.a3.x;
92-
ucell.latvec.e32 = ucell.a3.y;
93-
ucell.latvec.e33 = ucell.a3.z;
94-
ucell.GT = ucell.latvec.Inverse();
95-
ucell.G = ucell.GT.Transpose();
96-
ucell.lat0 = 1.8897261254578281;
97-
for (int i = 0; i < coord.size(); i++)
98-
{
99-
ucell.atoms[i].label = coord[i].atomname;
100-
ucell.atoms[i].na = coord[i].coordinate.size();
101-
ucell.atoms[i].tau.resize(ucell.atoms[i].na);
102-
ucell.atoms[i].taud.resize(ucell.atoms[i].na);
103-
for (int j = 0; j < ucell.atoms[i].na; ++j)
104-
{
105-
std::vector<double> this_atom = coord[i].coordinate[j];
106-
ucell.atoms[i].tau[j] = ModuleBase::Vector3<double>(this_atom[0], this_atom[1], this_atom[2]);
107-
ModuleBase::Mathzone::Cartesian_to_Direct(ucell.atoms[i].tau[j].x,
108-
ucell.atoms[i].tau[j].y,
109-
ucell.atoms[i].tau[j].z,
110-
ucell.a1.x,
111-
ucell.a1.y,
112-
ucell.a1.z,
113-
ucell.a2.x,
114-
ucell.a2.y,
115-
ucell.a2.z,
116-
ucell.a3.x,
117-
ucell.a3.y,
118-
ucell.a3.z,
119-
ucell.atoms[i].taud[j].x,
120-
ucell.atoms[i].taud[j].y,
121-
ucell.atoms[i].taud[j].z);
122-
}
123-
ucell.nat += ucell.atoms[i].na;
124-
}
125-
}
126-
127-
void ClearUcell()
128-
{
129-
delete[] ucell.atoms;
130-
}
131-
13248
// build a reduced 2x2x2 q-mesh (4 irreducible q-points for O_h)
13349
void init_qlist()
13450
{

source/source_pw/module_dfpt/test/dfpt_pw_run_test.cpp

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
#include "source_cell/unitcell.h"
1111
#include "source_cell/magnetism.h"
1212
#undef private
13-
#include "source_base/mathzone.h"
1413
#include "source_base/parallel_global.h"
1514
#include "source_base/global_variable.h"
1615
#include "source_estate/module_charge/charge_mixing.h"
1716
#include "source_pw/module_pwdft/dftu_base.h"
1817
#include "source_pw/module_dfpt/dfpt_pw.h"
18+
#include "dfpt_stru_fixture.h"
1919

2020
// ctor/dtor stubs for the cell/spepot/charge link closures live in the
2121
// shared dfpt_test_mocks.cpp compiled into every DFPT test binary.
@@ -35,96 +35,13 @@
3535
* i.e. 3*nat entries for each q.
3636
*/
3737

38-
struct atomtype_
39-
{
40-
std::string atomname;
41-
std::vector<std::vector<double>> coordinate;
42-
};
43-
44-
struct stru_
45-
{
46-
int ibrav;
47-
std::string point_group; // Schoenflies symbol
48-
std::string point_group_hm; // Hermann-Mauguin notation.
49-
std::string space_group;
50-
std::vector<double> cell;
51-
std::vector<atomtype_> all_type;
52-
};
53-
54-
std::vector<stru_> stru_lib{stru_{1,
55-
"O_h",
56-
"m-3m",
57-
"Pm-3m",
58-
std::vector<double>{1., 0., 0., 0., 1., 0., 0., 0., 1.},
59-
std::vector<atomtype_>{atomtype_{"C",
60-
std::vector<std::vector<double>>{
61-
{0., 0., 0.},
62-
}}}}};
63-
64-
class DFPT_PWRunTest : public testing::Test
38+
class DFPT_PWRunTest : public DFPTStruTestFixture
6539
{
6640
protected:
6741
ModuleDFPT::DFPT_PW dfpt;
6842
std::ofstream ofs_running;
6943
std::string output;
7044

71-
UnitCell ucell;
72-
void construct_ucell(stru_& stru)
73-
{
74-
std::vector<atomtype_> coord = stru.all_type;
75-
ucell.a1 = ModuleBase::Vector3<double>(stru.cell[0], stru.cell[1], stru.cell[2]);
76-
ucell.a2 = ModuleBase::Vector3<double>(stru.cell[3], stru.cell[4], stru.cell[5]);
77-
ucell.a3 = ModuleBase::Vector3<double>(stru.cell[6], stru.cell[7], stru.cell[8]);
78-
ucell.ntype = stru.all_type.size();
79-
ucell.atoms = new Atom[ucell.ntype];
80-
ucell.nat = 0;
81-
ucell.latvec.e11 = ucell.a1.x;
82-
ucell.latvec.e12 = ucell.a1.y;
83-
ucell.latvec.e13 = ucell.a1.z;
84-
ucell.latvec.e21 = ucell.a2.x;
85-
ucell.latvec.e22 = ucell.a2.y;
86-
ucell.latvec.e23 = ucell.a2.z;
87-
ucell.latvec.e31 = ucell.a3.x;
88-
ucell.latvec.e32 = ucell.a3.y;
89-
ucell.latvec.e33 = ucell.a3.z;
90-
ucell.GT = ucell.latvec.Inverse();
91-
ucell.G = ucell.GT.Transpose();
92-
ucell.lat0 = 1.8897261254578281;
93-
for (int i = 0; i < coord.size(); i++)
94-
{
95-
ucell.atoms[i].label = coord[i].atomname;
96-
ucell.atoms[i].na = coord[i].coordinate.size();
97-
ucell.atoms[i].tau.resize(ucell.atoms[i].na);
98-
ucell.atoms[i].taud.resize(ucell.atoms[i].na);
99-
for (int j = 0; j < ucell.atoms[i].na; j++)
100-
{
101-
std::vector<double> this_atom = coord[i].coordinate[j];
102-
ucell.atoms[i].tau[j] = ModuleBase::Vector3<double>(this_atom[0], this_atom[1], this_atom[2]);
103-
ModuleBase::Mathzone::Cartesian_to_Direct(ucell.atoms[i].tau[j].x,
104-
ucell.atoms[i].tau[j].y,
105-
ucell.atoms[i].tau[j].z,
106-
ucell.a1.x,
107-
ucell.a1.y,
108-
ucell.a1.z,
109-
ucell.a2.x,
110-
ucell.a2.y,
111-
ucell.a2.z,
112-
ucell.a3.x,
113-
ucell.a3.y,
114-
ucell.a3.z,
115-
ucell.atoms[i].taud[j].x,
116-
ucell.atoms[i].taud[j].y,
117-
ucell.atoms[i].taud[j].z);
118-
}
119-
ucell.nat += ucell.atoms[i].na;
120-
}
121-
}
122-
123-
void ClearUcell()
124-
{
125-
delete[] ucell.atoms;
126-
}
127-
12845
void SetUp() override
12946
{
13047
construct_ucell(stru_lib[0]);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "dfpt_stru_fixture.h"
2+
3+
#include "source_base/mathzone.h"
4+
5+
DFPTStruTestFixture::DFPTStruTestFixture()
6+
{
7+
stru_lib.push_back(stru_{1,
8+
"O_h",
9+
"m-3m",
10+
"Pm-3m",
11+
std::vector<double>{1., 0., 0., 0., 1., 0., 0., 0., 1.},
12+
std::vector<atomtype_>{atomtype_{"C",
13+
std::vector<std::vector<double>>{
14+
{0., 0., 0.},
15+
}}}});
16+
}
17+
18+
void DFPTStruTestFixture::construct_ucell(stru_& stru)
19+
{
20+
std::vector<atomtype_> coord = stru.all_type;
21+
ucell.a1 = ModuleBase::Vector3<double>(stru.cell[0], stru.cell[1], stru.cell[2]);
22+
ucell.a2 = ModuleBase::Vector3<double>(stru.cell[3], stru.cell[4], stru.cell[5]);
23+
ucell.a3 = ModuleBase::Vector3<double>(stru.cell[6], stru.cell[7], stru.cell[8]);
24+
ucell.ntype = stru.all_type.size();
25+
ucell.atoms = new Atom[ucell.ntype];
26+
ucell.nat = 0;
27+
ucell.latvec.e11 = ucell.a1.x;
28+
ucell.latvec.e12 = ucell.a1.y;
29+
ucell.latvec.e13 = ucell.a1.z;
30+
ucell.latvec.e21 = ucell.a2.x;
31+
ucell.latvec.e22 = ucell.a2.y;
32+
ucell.latvec.e23 = ucell.a2.z;
33+
ucell.latvec.e31 = ucell.a3.x;
34+
ucell.latvec.e32 = ucell.a3.y;
35+
ucell.latvec.e33 = ucell.a3.z;
36+
ucell.GT = ucell.latvec.Inverse();
37+
ucell.G = ucell.GT.Transpose();
38+
ucell.lat0 = 1.8897261254578281;
39+
for (size_t i = 0; i < coord.size(); i++)
40+
{
41+
ucell.atoms[i].label = coord[i].atomname;
42+
ucell.atoms[i].na = coord[i].coordinate.size();
43+
ucell.atoms[i].tau.resize(ucell.atoms[i].na);
44+
ucell.atoms[i].taud.resize(ucell.atoms[i].na);
45+
for (int j = 0; j < ucell.atoms[i].na; ++j)
46+
{
47+
std::vector<double> this_atom = coord[i].coordinate[j];
48+
ucell.atoms[i].tau[j] = ModuleBase::Vector3<double>(this_atom[0], this_atom[1], this_atom[2]);
49+
ModuleBase::Mathzone::Cartesian_to_Direct(ucell.atoms[i].tau[j].x,
50+
ucell.atoms[i].tau[j].y,
51+
ucell.atoms[i].tau[j].z,
52+
ucell.a1.x,
53+
ucell.a1.y,
54+
ucell.a1.z,
55+
ucell.a2.x,
56+
ucell.a2.y,
57+
ucell.a2.z,
58+
ucell.a3.x,
59+
ucell.a3.y,
60+
ucell.a3.z,
61+
ucell.atoms[i].taud[j].x,
62+
ucell.atoms[i].taud[j].y,
63+
ucell.atoms[i].taud[j].z);
64+
}
65+
ucell.nat += ucell.atoms[i].na;
66+
}
67+
}
68+
69+
void DFPTStruTestFixture::ClearUcell()
70+
{
71+
delete[] ucell.atoms;
72+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef DFPT_STRU_FIXTURE_H
2+
#define DFPT_STRU_FIXTURE_H
3+
4+
#include <string>
5+
#include <vector>
6+
#include "source_cell/unitcell.h"
7+
#include "gtest/gtest.h"
8+
9+
// Shared gtest fixture for building a minimal cubic UnitCell from a
10+
// hand-written structure table (abbreviated from
11+
// module_symmetry/test/symm_test.cpp and klist_test.cpp). Used by the
12+
// MPI-side DFPT tests that drive the QList / DFPT_PW wiring
13+
// (dfpt_pw_data_test.cpp, dfpt_pw_run_test.cpp).
14+
//
15+
// NOTE ON INCLUDE ORDER: every test that needs UnitCell private members
16+
// includes the cell headers with `#define private public` BEFORE this
17+
// header; the include guards then keep the fixture header's own includes
18+
// inert. The fixture implementation (dfpt_stru_fixture.cpp) only touches
19+
// public members, so it compiles without the define.
20+
21+
struct atomtype_
22+
{
23+
std::string atomname;
24+
std::vector<std::vector<double>> coordinate;
25+
};
26+
27+
struct stru_
28+
{
29+
int ibrav;
30+
std::string point_group; // Schoenflies symbol
31+
std::string point_group_hm; // Hermann-Mauguin notation.
32+
std::string space_group;
33+
std::vector<double> cell;
34+
std::vector<atomtype_> all_type;
35+
};
36+
37+
class DFPTStruTestFixture : public testing::Test
38+
{
39+
protected:
40+
UnitCell ucell;
41+
std::vector<stru_> stru_lib;
42+
43+
DFPTStruTestFixture();
44+
45+
void construct_ucell(stru_& stru);
46+
void ClearUcell();
47+
};
48+
49+
#endif // DFPT_STRU_FIXTURE_H

0 commit comments

Comments
 (0)