diff --git a/source/module_io/test_serial/rho_io_test.cpp b/source/module_io/test_serial/rho_io_test.cpp index 0fdb484627..cf13dea9a5 100644 --- a/source/module_io/test_serial/rho_io_test.cpp +++ b/source/module_io/test_serial/rho_io_test.cpp @@ -99,6 +99,25 @@ TEST_F(RhoIOTest, Read) EXPECT_DOUBLE_EQ(rho[0][46655], 1.33581335706e-02); } +TEST_F(RhoIOTest, Write) +{ + int nx = 36; + int ny = 36; + int nz = 36; + UcellTestPrepare utp = UcellTestLib["Si"]; + ucell = utp.SetUcellInfo(); + ucell->lat0 = 10.2; + ucell->latvec = { -0.5,0,0.5,0,0.5,0.5,-0.5,0.5,0 }; + ucell->atoms[0].tau[0] = ModuleBase::Vector3(0.0, 0.0, 0.0); + ucell->atoms[0].tau[1] = ModuleBase::Vector3(-0.75, 0.75, 0.75); + ucell->atoms[0].ncpp.zv = 4; + ucell->atoms[1].ncpp.zv = 4; + Parallel_Grid pgrid(nx, ny, nz, nz, nrxx, nz, 1); + ModuleIO::read_vdata_palgrid(pgrid, my_rank, ofs_running, "support/SPIN1_CHG.cube", rho[0], ucell->nat); + ModuleIO::write_vdata_palgrid(pgrid, rho[0], 0, nspin, 0, "test_write_vdata_palgrid.cube", 0.461002, ucell, 11, 1); + EXPECT_EQ(system("diff -q test_write_vdata_palgrid.cube support/SPIN1_CHG.cube"), 0); +} + TEST_F(RhoIOTest, TrilinearInterpolate) { int nx = 36; @@ -147,4 +166,57 @@ TEST_F(RhoIOTest, TrilinearInterpolate) EXPECT_DOUBLE_EQ(data[0], 0.0010824725010374092); EXPECT_DOUBLE_EQ(data[10], 0.058649850374240906); EXPECT_DOUBLE_EQ(data[100], 0.018931708073604996); +} + +struct CubeIOTest : public ::testing::Test +{ + std::vector comment; + int natom = 0; + std::vector origin; + std::vector nvoxel; + int nx_read = 0; + int ny_read = 0; + int nz_read = 0; + std::vector dx; + std::vector dy; + std::vector dz; + std::vector> axis_vecs; + std::vector atom_type; + std::vector atom_charge; + std::vector> atom_pos; + std::vector data_read; + const std::string fn = "./support/SPIN1_CHG.cube"; +}; + + +TEST_F(CubeIOTest, ReadCube) +{ + ModuleIO::read_cube(fn, comment, natom, origin, nx_read, ny_read, nz_read, dx, dy, dz, atom_type, atom_charge, atom_pos, data_read); + EXPECT_EQ(comment[0], "STEP: 0 Cubefile created from ABACUS. Inner loop is z, followed by y and x"); + EXPECT_EQ(comment[1], "1 (nspin) 0.461002 (fermi energy, in Ry)"); + EXPECT_EQ(natom, 2); + for (auto& o : origin) { EXPECT_EQ(o, 0.0); } + EXPECT_EQ(nx_read, 36); + EXPECT_EQ(ny_read, 36); + EXPECT_EQ(nz_read, 36); + EXPECT_DOUBLE_EQ(dx[0], -0.141667); + EXPECT_DOUBLE_EQ(dy[2], 0.141667); + EXPECT_DOUBLE_EQ(dz[1], 0.141667); + EXPECT_EQ(atom_type.size(), natom); + EXPECT_EQ(atom_charge.size(), natom); + EXPECT_EQ(atom_pos.size(), natom); + for (auto& t : atom_type) { EXPECT_EQ(t, 14); } + for (auto& c : atom_charge) { EXPECT_DOUBLE_EQ(c, 4.0); } + EXPECT_DOUBLE_EQ(atom_pos[1][1], 7.65); + const int nxyz = nx_read * ny_read * nz_read; + EXPECT_EQ(data_read.size(), nxyz); + EXPECT_EQ(data_read[1], 2.64004483879e-03); + EXPECT_EQ(data_read[nxyz - 1], 1.33581335706e-02); +} + +TEST_F(CubeIOTest, WriteCube) +{ + ModuleIO::read_cube(fn, comment, natom, origin, nx_read, ny_read, nz_read, dx, dy, dz, atom_type, atom_charge, atom_pos, data_read); + 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); + EXPECT_EQ(system("diff -q test_write.cube ./support/SPIN1_CHG.cube"), 0); } \ No newline at end of file diff --git a/source/module_io/test_serial/support/SPIN1_CHG.cube b/source/module_io/test_serial/support/SPIN1_CHG.cube index 4f0d2e8afc..6aa8d9205c 100644 --- a/source/module_io/test_serial/support/SPIN1_CHG.cube +++ b/source/module_io/test_serial/support/SPIN1_CHG.cube @@ -1,11 +1,11 @@ STEP: 0 Cubefile created from ABACUS. Inner loop is z, followed by y and x 1 (nspin) 0.461002 (fermi energy, in Ry) 2 0.0 0.0 0.0 -36 -0.141667 0 0.141667 -36 0 0.141667 0.141667 -36 -0.141667 0.141667 0 - 14 4 0 0 0 - 14 4 -7.65 7.65 7.65 +36 -0.141667 0.000000 0.141667 +36 0.000000 0.141667 0.141667 +36 -0.141667 0.141667 0.000000 + 14 4.000000 0.000000 0.000000 0.000000 + 14 4.000000 -7.650000 7.650000 7.650000 1.27020863940e-03 2.64004483879e-03 7.45857574908e-03 1.69819929531e-02 3.09103717113e-02 4.60013609401e-02 5.75824043038e-02 6.28259810566e-02 6.21334821617e-02 5.77319585491e-02 5.18034033720e-02 4.57409612273e-02 4.02453701311e-02 3.55932306397e-02 3.18414068716e-02 2.89647599965e-02 2.69304328597e-02 2.57176061925e-02