Skip to content

Commit 137235d

Browse files
authored
UT for rw_cube (#5384)
1 parent e276480 commit 137235d

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

source/module_io/test_serial/rho_io_test.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ TEST_F(RhoIOTest, Read)
9999
EXPECT_DOUBLE_EQ(rho[0][46655], 1.33581335706e-02);
100100
}
101101

102+
TEST_F(RhoIOTest, Write)
103+
{
104+
int nx = 36;
105+
int ny = 36;
106+
int nz = 36;
107+
UcellTestPrepare utp = UcellTestLib["Si"];
108+
ucell = utp.SetUcellInfo();
109+
ucell->lat0 = 10.2;
110+
ucell->latvec = { -0.5,0,0.5,0,0.5,0.5,-0.5,0.5,0 };
111+
ucell->atoms[0].tau[0] = ModuleBase::Vector3<double>(0.0, 0.0, 0.0);
112+
ucell->atoms[0].tau[1] = ModuleBase::Vector3<double>(-0.75, 0.75, 0.75);
113+
ucell->atoms[0].ncpp.zv = 4;
114+
ucell->atoms[1].ncpp.zv = 4;
115+
Parallel_Grid pgrid(nx, ny, nz, nz, nrxx, nz, 1);
116+
ModuleIO::read_vdata_palgrid(pgrid, my_rank, ofs_running, "support/SPIN1_CHG.cube", rho[0], ucell->nat);
117+
ModuleIO::write_vdata_palgrid(pgrid, rho[0], 0, nspin, 0, "test_write_vdata_palgrid.cube", 0.461002, ucell, 11, 1);
118+
EXPECT_EQ(system("diff -q test_write_vdata_palgrid.cube support/SPIN1_CHG.cube"), 0);
119+
}
120+
102121
TEST_F(RhoIOTest, TrilinearInterpolate)
103122
{
104123
int nx = 36;
@@ -147,4 +166,57 @@ TEST_F(RhoIOTest, TrilinearInterpolate)
147166
EXPECT_DOUBLE_EQ(data[0], 0.0010824725010374092);
148167
EXPECT_DOUBLE_EQ(data[10], 0.058649850374240906);
149168
EXPECT_DOUBLE_EQ(data[100], 0.018931708073604996);
169+
}
170+
171+
struct CubeIOTest : public ::testing::Test
172+
{
173+
std::vector<std::string> comment;
174+
int natom = 0;
175+
std::vector<double> origin;
176+
std::vector<int> nvoxel;
177+
int nx_read = 0;
178+
int ny_read = 0;
179+
int nz_read = 0;
180+
std::vector<double> dx;
181+
std::vector<double> dy;
182+
std::vector<double> dz;
183+
std::vector<std::vector<double>> axis_vecs;
184+
std::vector<int> atom_type;
185+
std::vector<double> atom_charge;
186+
std::vector<std::vector<double>> atom_pos;
187+
std::vector<double> data_read;
188+
const std::string fn = "./support/SPIN1_CHG.cube";
189+
};
190+
191+
192+
TEST_F(CubeIOTest, ReadCube)
193+
{
194+
ModuleIO::read_cube(fn, comment, natom, origin, nx_read, ny_read, nz_read, dx, dy, dz, atom_type, atom_charge, atom_pos, data_read);
195+
EXPECT_EQ(comment[0], "STEP: 0 Cubefile created from ABACUS. Inner loop is z, followed by y and x");
196+
EXPECT_EQ(comment[1], "1 (nspin) 0.461002 (fermi energy, in Ry)");
197+
EXPECT_EQ(natom, 2);
198+
for (auto& o : origin) { EXPECT_EQ(o, 0.0); }
199+
EXPECT_EQ(nx_read, 36);
200+
EXPECT_EQ(ny_read, 36);
201+
EXPECT_EQ(nz_read, 36);
202+
EXPECT_DOUBLE_EQ(dx[0], -0.141667);
203+
EXPECT_DOUBLE_EQ(dy[2], 0.141667);
204+
EXPECT_DOUBLE_EQ(dz[1], 0.141667);
205+
EXPECT_EQ(atom_type.size(), natom);
206+
EXPECT_EQ(atom_charge.size(), natom);
207+
EXPECT_EQ(atom_pos.size(), natom);
208+
for (auto& t : atom_type) { EXPECT_EQ(t, 14); }
209+
for (auto& c : atom_charge) { EXPECT_DOUBLE_EQ(c, 4.0); }
210+
EXPECT_DOUBLE_EQ(atom_pos[1][1], 7.65);
211+
const int nxyz = nx_read * ny_read * nz_read;
212+
EXPECT_EQ(data_read.size(), nxyz);
213+
EXPECT_EQ(data_read[1], 2.64004483879e-03);
214+
EXPECT_EQ(data_read[nxyz - 1], 1.33581335706e-02);
215+
}
216+
217+
TEST_F(CubeIOTest, WriteCube)
218+
{
219+
ModuleIO::read_cube(fn, comment, natom, origin, nx_read, ny_read, nz_read, dx, dy, dz, atom_type, atom_charge, atom_pos, data_read);
220+
ModuleIO::write_cube("test_write.cube", comment, natom, origin, nx_read, ny_read, nz_read, dx, dy, dz, atom_type, atom_charge, atom_pos, data_read, 11);
221+
EXPECT_EQ(system("diff -q test_write.cube ./support/SPIN1_CHG.cube"), 0);
150222
}

source/module_io/test_serial/support/SPIN1_CHG.cube

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
STEP: 0 Cubefile created from ABACUS. Inner loop is z, followed by y and x
22
1 (nspin) 0.461002 (fermi energy, in Ry)
33
2 0.0 0.0 0.0
4-
36 -0.141667 0 0.141667
5-
36 0 0.141667 0.141667
6-
36 -0.141667 0.141667 0
7-
14 4 0 0 0
8-
14 4 -7.65 7.65 7.65
4+
36 -0.141667 0.000000 0.141667
5+
36 0.000000 0.141667 0.141667
6+
36 -0.141667 0.141667 0.000000
7+
14 4.000000 0.000000 0.000000 0.000000
8+
14 4.000000 -7.650000 7.650000 7.650000
99
1.27020863940e-03 2.64004483879e-03 7.45857574908e-03 1.69819929531e-02 3.09103717113e-02 4.60013609401e-02
1010
5.75824043038e-02 6.28259810566e-02 6.21334821617e-02 5.77319585491e-02 5.18034033720e-02 4.57409612273e-02
1111
4.02453701311e-02 3.55932306397e-02 3.18414068716e-02 2.89647599965e-02 2.69304328597e-02 2.57176061925e-02

0 commit comments

Comments
 (0)