diff --git a/source/module_cell/module_neighbor/test/CMakeLists.txt b/source/module_cell/module_neighbor/test/CMakeLists.txt index 8a560cb540c..693dd7bcaa8 100644 --- a/source/module_cell/module_neighbor/test/CMakeLists.txt +++ b/source/module_cell/module_neighbor/test/CMakeLists.txt @@ -5,23 +5,41 @@ remove_definitions(-D__EXX) AddTest( TARGET cell_neighbor_sltk_adjacent_set - SOURCES ../sltk_adjacent_set.cpp ./sltk_adjacent_set_test.cpp + SOURCES sltk_adjacent_set_test.cpp ../sltk_adjacent_set.cpp ) AddTest( TARGET cell_neighbor_sltk_atom - SOURCES ../sltk_atom.cpp ./sltk_atom_test.cpp ../sltk_adjacent_set.cpp ./sltk_adjacent_set_test.cpp + SOURCES sltk_atom_test.cpp ../sltk_atom.cpp ../sltk_adjacent_set.cpp ) AddTest( - TARGET cell_neighbor_sltk_atom_arrange - LIBS MPI::MPI_CXX - SOURCES ./sltk_atom_arrange_test.cpp ../sltk_atom_arrange.cpp + TARGET cell_neighbor_sltk_atom_input + LIBS ${math_libs} base device + SOURCES sltk_atom_input_test.cpp ../sltk_atom_input.cpp ../sltk_atom.cpp ../sltk_adjacent_set.cpp + ../../unitcell.cpp ../../read_atoms.cpp ../../read_cell_pseudopots.cpp + ../../atom_spec.cpp ../../atom_pseudo.cpp ../../pseudo_nc.cpp + ../../read_pp.cpp ../../read_pp_upf201.cpp ../../read_pp_upf100.cpp + ../../read_pp_vwr.cpp ../../read_pp_blps.cpp ../../../module_io/output.cpp ) - AddTest( TARGET cell_neighbor_sltk_grid - LIBS MPI::MPI_CXX - SOURCES ./sltk_grid_test.cpp + LIBS ${math_libs} base device + SOURCES sltk_grid_test.cpp ../sltk_grid.cpp ../sltk_atom_input.cpp ../sltk_atom.cpp ../sltk_adjacent_set.cpp + ../../unitcell.cpp ../../read_atoms.cpp ../../read_cell_pseudopots.cpp + ../../atom_spec.cpp ../../atom_pseudo.cpp ../../pseudo_nc.cpp + ../../read_pp.cpp ../../read_pp_upf201.cpp ../../read_pp_upf100.cpp + ../../read_pp_vwr.cpp ../../read_pp_blps.cpp ../../../module_io/output.cpp +) + +AddTest( + TARGET cell_neighbor_sltk_atom_arrange + LIBS ${math_libs} base device + SOURCES sltk_atom_arrange_test.cpp ../sltk_atom_arrange.cpp ../sltk_grid_driver.cpp ../sltk_grid.cpp + ../sltk_atom_input.cpp ../sltk_atom.cpp ../sltk_adjacent_set.cpp + ../../unitcell.cpp ../../read_atoms.cpp ../../read_cell_pseudopots.cpp + ../../atom_spec.cpp ../../atom_pseudo.cpp ../../pseudo_nc.cpp + ../../read_pp.cpp ../../read_pp_upf201.cpp ../../read_pp_upf100.cpp + ../../read_pp_vwr.cpp ../../read_pp_blps.cpp ../../../module_io/output.cpp ) \ No newline at end of file diff --git a/source/module_cell/module_neighbor/test/for_test_sltk_atom_arrange.h b/source/module_cell/module_neighbor/test/for_test_sltk_atom_arrange.h deleted file mode 100644 index 9cb3c846be7..00000000000 --- a/source/module_cell/module_neighbor/test/for_test_sltk_atom_arrange.h +++ /dev/null @@ -1,123 +0,0 @@ -#include "../sltk_atom_arrange.h" -#include -#include - -namespace ModuleBase -{ - void TITLE(const std::string &class_name,const std::string &function_name,const bool disable) -{ - -} - void TITLE(std::ofstream &ofs,const std::string &class_name,const std::string &function_name,const bool disable){} - - class timer - { - private: - - public: - timer(); - ~timer(); - void tick(const std::string &class_name,const std::string &name); - }; - timer::timer(){} - timer::~timer(){} - void timer::tick(const std::string &class_name,const std::string &name){}; - - void Matrix3::Identity(void) - { - e11 = 1;e12 = 0;e13 = 0; - e21 = 0;e22 = 1;e23 = 0; - e31 = 0;e32 = 0;e33 = 1; - } - IntArray::~IntArray(){} - IntArray::IntArray(int, int){} -} -//constructions and destructions -Magnetism::Magnetism(){} -Magnetism::~Magnetism(){} -UnitCell::~UnitCell(){} -#ifdef __LCAO -InfoNonlocal::InfoNonlocal(){} -InfoNonlocal::~InfoNonlocal(){} -#endif -UnitCell::UnitCell(){} -Grid_Driver::Grid_Driver( - const int &test_d_in, - const int &test_gd_in, - const int &test_grid_in) -:test_deconstructor(test_d_in), -test_grid_driver(test_gd_in), -Grid(test_grid_in){} -Grid_Driver::~Grid_Driver(){} -AtomLink::AtomLink(const FAtom &atom, AtomLink* const pointNext) -: fatom(atom), next_p(pointNext) {} -Grid::Grid(const int &test_grid_in):test_grid(test_grid_in) -{ - init_cell_flag = false; - this->atomlink = new AtomLink[1]; -} -Grid::~Grid() -{ - delete[] atomlink; - this->delete_Cell(); - -} -CellSet::CellSet() -: address(NullPtr), length(0) -{ - in_grid[0] = 0; - in_grid[1] = 0; - in_grid[2] = 0; -} -FAtom::FAtom() -{ - d_x = 0.0; - d_y = 0.0; - d_z = 0.0; - as = nullptr; - type = 0; - natom = 0; -} -FAtom::~FAtom(){} -Atom_input::Atom_input -( - std::ofstream &ofs_in, - const UnitCell &ucell, - const int amount, - const int ntype, - const bool boundary_in, - const double radius_in, - const int &test_atom_in - ) -:d_amount(amount), -d_amount_expand(amount), -periodic_boundary(boundary_in), -radius(radius_in), -expand_flag(false), -glayerX(1), -glayerX_minus(0), -glayerY(1), -glayerY_minus(0), -glayerZ(1), -glayerZ_minus(0), -x_min_expand(0), -y_min_expand(0), -z_min_expand(0), -x_max_expand(0), -y_max_expand(0), -z_max_expand(0), -d_current(0), -test_atom_input(test_atom_in), -type(0), -natom(-1) -{} -Atom_input::~Atom_input(){} - - -void Grid::delete_vector(const Atom_input &input) -{ - std::cout<<"test"<<"\n"; -} -void Grid::init(std::basic_ofstream >&, UnitCell const&, Atom_input const&){}; -void Grid_Driver::Find_atom(UnitCell const&, ModuleBase::Vector3 const&, int const&, int const&, AdjacentAtomInfo*){}; - diff --git a/source/module_cell/module_neighbor/test/prepare_unitcell.h b/source/module_cell/module_neighbor/test/prepare_unitcell.h new file mode 100644 index 00000000000..5f217aadf2a --- /dev/null +++ b/source/module_cell/module_neighbor/test/prepare_unitcell.h @@ -0,0 +1,341 @@ +#ifndef PREPARE_UNITCELL_H +#define PREPARE_UNITCELL_H +#include +#include +#include "module_base/mathzone.h" + +class UcellTestPrepare +{ +public: + UcellTestPrepare()=default; + UcellTestPrepare(std::string latname_in, + int lmaxmax_in, + bool init_vel_in, + bool selective_dynamics_in, + bool relax_new_in, + std::string fixed_axes_in, + double lat0_in, + std::valarray latvec_in, + std::vector elements_in, + std::vector pp_files_in, + std::vector pp_types_in, + std::vector orb_files_in, + std::valarray natom_in, + std::vector atomic_mass_in, + std::string coor_type_in, + std::valarray coordinates_in); + UcellTestPrepare(std::string latname_in, + int lmaxmax_in, + bool init_vel_in, + bool selective_dynamics_in, + bool relax_new_in, + std::string fixed_axes_in, + double lat0_in, + std::valarray latvec_in, + std::vector elements_in, + std::vector pp_files_in, + std::vector pp_types_in, + std::vector orb_files_in, + std::valarray natom_in, + std::vector atomic_mass_in, + std::string coor_type_in, + std::valarray coordinates_in, + std::valarray mbl_in, + std::valarray velocity_in); + UcellTestPrepare(const UcellTestPrepare &utp); + + std::string latname; + int lmaxmax; + bool init_vel; + bool selective_dynamics; + bool relax_new; + std::string fixed_axes; + double lat0; + std::valarray latvec; + std::vector elements; + std::vector pp_files; + std::vector pp_types; + std::vector orb_files; + std::valarray natom; + std::vector atomic_mass; + std::string coor_type; + std::valarray coordinates; + std::valarray mbl; + std::valarray velocity; + // ntype + int ntype; + int atomic_index; + + UnitCell* SetUcellInfo() + { + //basic info + this->ntype = this->elements.size(); + UnitCell* ucell = new UnitCell; + ucell->setup(this->latname, + this->ntype, + this->lmaxmax, + this->init_vel, + this->fixed_axes); + delete[] ucell->atom_label; + delete[] ucell->atom_mass; + delete[] ucell->pseudo_fn; + delete[] ucell->pseudo_type; + delete[] ucell->orbital_fn; + delete[] ucell->magnet.start_magnetization; //mag set here + ucell->atom_label = new std::string[ucell->ntype]; + ucell->atom_mass = new double[ucell->ntype]; + ucell->pseudo_fn = new std::string[ucell->ntype]; + ucell->pseudo_type = new std::string[ucell->ntype]; + ucell->orbital_fn = new std::string[ucell->ntype]; + ucell->magnet.start_magnetization = new double[ucell->ntype]; //mag set here + ucell->magnet.ux_[0] = 0.0; // ux_ set here + ucell->magnet.ux_[1] = 0.0; + ucell->magnet.ux_[2] = 0.0; + for(int it=0;itntype;++it) + { + ucell->atom_label[it] = this->elements[it]; + ucell->atom_mass[it] = this->atomic_mass[it]; + ucell->pseudo_fn[it] = this->pp_files[it]; + ucell->pseudo_type[it] = this->pp_types[it]; + ucell->orbital_fn[it] = this->orb_files[it]; + ucell->magnet.start_magnetization[it] = 0.0; //mag set here + } + //lattice info + ucell->lat0 = this->lat0; + ucell->lat0_angstrom = ucell->lat0 * 0.529177; + ucell->tpiba = ModuleBase::TWO_PI/ucell->lat0; + ucell->tpiba2 = ucell->tpiba * ucell->tpiba; + ucell->latvec.e11 = this->latvec[0]; + ucell->latvec.e12 = this->latvec[1]; + ucell->latvec.e13 = this->latvec[2]; + ucell->latvec.e21 = this->latvec[3]; + ucell->latvec.e22 = this->latvec[4]; + ucell->latvec.e23 = this->latvec[5]; + ucell->latvec.e31 = this->latvec[6]; + ucell->latvec.e32 = this->latvec[7]; + ucell->latvec.e33 = this->latvec[8]; + ucell->a1.x = ucell->latvec.e11; + ucell->a1.y = ucell->latvec.e12; + ucell->a1.z = ucell->latvec.e13; + ucell->a2.x = ucell->latvec.e21; + ucell->a2.y = ucell->latvec.e22; + ucell->a2.z = ucell->latvec.e23; + ucell->a3.x = ucell->latvec.e31; + ucell->a3.y = ucell->latvec.e32; + ucell->a3.z = ucell->latvec.e33; + ucell->GT = ucell->latvec.Inverse(); + ucell->G = ucell->GT.Transpose(); + ucell->GGT = ucell->G*ucell->GT; + ucell->invGGT = ucell->GGT.Inverse(); + ucell->omega = std::abs(ucell->latvec.Det())*(ucell->lat0)*(ucell->lat0)*(ucell->lat0); + //atomic info + ucell->Coordinate = this->coor_type; + ucell->atoms = new Atom[ucell->ntype]; + ucell->set_atom_flag = true; + this->atomic_index = 0; + for(int it=0;itntype;++it) + { + ucell->atoms[it].label = this->elements[it]; + ucell->atoms[it].nw = 0; + ucell->atoms[it].nwl = 2; + delete[] ucell->atoms[it].l_nchi; + ucell->atoms[it].l_nchi = new int[ ucell->atoms[it].nwl+1]; + for(int L=0; Latoms[it].nwl+1; L++) + { + ucell->atoms[it].l_nchi[L] = 1; + ucell->atoms[it].nw += (2*L + 1) * ucell->atoms[it].l_nchi[L]; + } + ucell->atoms[it].na = this->natom[it]; + //coordinates and related physical quantities + delete[] ucell->atoms[it].tau; + delete[] ucell->atoms[it].dis; + delete[] ucell->atoms[it].taud; + delete[] ucell->atoms[it].vel; + delete[] ucell->atoms[it].mag; + delete[] ucell->atoms[it].angle1; + delete[] ucell->atoms[it].angle2; + delete[] ucell->atoms[it].m_loc_; + delete[] ucell->atoms[it].mbl; + ucell->atoms[it].tau = new ModuleBase::Vector3[ucell->atoms[it].na]; + ucell->atoms[it].dis = new ModuleBase::Vector3[ucell->atoms[it].na]; + ucell->atoms[it].taud = new ModuleBase::Vector3[ucell->atoms[it].na]; + ucell->atoms[it].vel = new ModuleBase::Vector3[ucell->atoms[it].na]; + ucell->atoms[it].mag = new double[ucell->atoms[it].na]; + ucell->atoms[it].angle1 = new double[ucell->atoms[it].na]; + ucell->atoms[it].angle2 = new double[ucell->atoms[it].na]; + ucell->atoms[it].m_loc_ = new ModuleBase::Vector3[ucell->atoms[it].na]; + ucell->atoms[it].mbl = new ModuleBase::Vector3[ucell->atoms[it].na]; + ucell->atoms[it].mass = ucell->atom_mass[it]; // mass set here + for(int ia=0; iaatoms[it].na; ++ia) + { + if (ucell->Coordinate == "Direct") + { + ucell->atoms[it].taud[ia].x = this->coordinates[this->atomic_index*3+0]; + ucell->atoms[it].taud[ia].y = this->coordinates[this->atomic_index*3+1]; + ucell->atoms[it].taud[ia].z = this->coordinates[this->atomic_index*3+2]; + ucell->atoms[it].tau[ia] = ucell->atoms[it].taud[ia]*ucell->latvec; + } + else if (ucell->Coordinate == "Cartesian") + { + ucell->atoms[it].tau[ia].x = this->coordinates[this->atomic_index*3+0]; + ucell->atoms[it].tau[ia].y = this->coordinates[this->atomic_index*3+1]; + ucell->atoms[it].tau[ia].z = this->coordinates[this->atomic_index*3+2]; + ModuleBase::Mathzone::Cartesian_to_Direct( + ucell->atoms[it].tau[ia].x, ucell->atoms[it].tau[ia].y, ucell->atoms[it].tau[ia].z, + ucell->latvec.e11, ucell->latvec.e12, ucell->latvec.e13, + ucell->latvec.e21, ucell->latvec.e22, ucell->latvec.e23, + ucell->latvec.e31, ucell->latvec.e32, ucell->latvec.e33, + ucell->atoms[it].taud[ia].x, ucell->atoms[it].taud[ia].y, ucell->atoms[it].taud[ia].z); + } + ucell->atoms[it].dis[ia].set(0, 0, 0); + if(this->init_vel) + { + ucell->atoms[it].vel[ia].x = this->velocity[this->atomic_index*3+0]; + ucell->atoms[it].vel[ia].y = this->velocity[this->atomic_index*3+1]; + ucell->atoms[it].vel[ia].z = this->velocity[this->atomic_index*3+2]; + } + else + { + ucell->atoms[it].vel[ia].set(0,0,0); + } + ucell->atoms[it].m_loc_[ia].set(0,0,0); + ucell->atoms[it].angle1[ia] = 0; + ucell->atoms[it].angle2[ia] = 0; + if(this->selective_dynamics) + { + ucell->atoms[it].mbl[ia].x = this->mbl[this->atomic_index*3+0]; + ucell->atoms[it].mbl[ia].y = this->mbl[this->atomic_index*3+1]; + ucell->atoms[it].mbl[ia].z = this->mbl[this->atomic_index*3+2]; + } + else + { + ucell->atoms[it].mbl[ia] = {1,1,1}; + } + ++(this->atomic_index); + } + } + ucell->nat = this->natom.sum(); + return ucell; + } +}; + +UcellTestPrepare::UcellTestPrepare(std::string latname_in, + int lmaxmax_in, + bool init_vel_in, + bool selective_dynamics_in, + bool relax_new_in, + std::string fixed_axes_in, + double lat0_in, + std::valarray latvec_in, + std::vector elements_in, + std::vector pp_files_in, + std::vector pp_types_in, + std::vector orb_files_in, + std::valarray natom_in, + std::vector atomic_mass_in, + std::string coor_type_in, + std::valarray coordinates_in): + latname(latname_in), + lmaxmax(lmaxmax_in), + init_vel(init_vel_in), + selective_dynamics(selective_dynamics_in), + relax_new(relax_new_in), + fixed_axes(fixed_axes_in), + lat0(lat0_in), + latvec(latvec_in), + elements(elements_in), + pp_files(pp_files_in), + pp_types(pp_types_in), + orb_files(orb_files_in), + natom(natom_in), + atomic_mass(atomic_mass_in), + coor_type(coor_type_in), + coordinates(coordinates_in) +{ + mbl = {0}; + velocity = {0}; +} + +UcellTestPrepare::UcellTestPrepare(std::string latname_in, + int lmaxmax_in, + bool init_vel_in, + bool selective_dynamics_in, + bool relax_new_in, + std::string fixed_axes_in, + double lat0_in, + std::valarray latvec_in, + std::vector elements_in, + std::vector pp_files_in, + std::vector pp_types_in, + std::vector orb_files_in, + std::valarray natom_in, + std::vector atomic_mass_in, + std::string coor_type_in, + std::valarray coordinates_in, + std::valarray mbl_in, + std::valarray velocity_in): + latname(latname_in), + lmaxmax(lmaxmax_in), + init_vel(init_vel_in), + selective_dynamics(selective_dynamics_in), + relax_new(relax_new_in), + fixed_axes(fixed_axes_in), + lat0(lat0_in), + latvec(latvec_in), + elements(elements_in), + pp_files(pp_files_in), + pp_types(pp_types_in), + orb_files(orb_files_in), + natom(natom_in), + atomic_mass(atomic_mass_in), + coor_type(coor_type_in), + coordinates(coordinates_in), + mbl(mbl_in), + velocity(velocity_in) // velocity assume the existence of mbl in print_stru_file() +{} + +UcellTestPrepare::UcellTestPrepare(const UcellTestPrepare &utp): + latname(utp.latname), + lmaxmax(utp.lmaxmax), + init_vel(utp.init_vel), + selective_dynamics(utp.selective_dynamics), + relax_new(utp.relax_new), + fixed_axes(utp.fixed_axes), + lat0(utp.lat0), + latvec(utp.latvec), + elements(utp.elements), + pp_files(utp.pp_files), + pp_types(utp.pp_types), + orb_files(utp.orb_files), + natom(utp.natom), + atomic_mass(utp.atomic_mass), + coor_type(utp.coor_type), + coordinates(utp.coordinates), + mbl(utp.mbl), + velocity(utp.velocity) // velocity assume the existence of mbl in print_stru_file() +{} + +std::map UcellTestLib +{ + {"Si", UcellTestPrepare( + "fcc", //latname + 2, //lmaxmax + true, //init_vel + true, //selective_dyanmics + true, //relax_new + "volume", //fixed_axes + 10.2, //lat0 + {-0.5,0.0,0.5, //latvec + 0.0,0.5,0.5, + -0.5,0.5,0.0}, + {"Si"}, //elements + {"Si.upf"}, //upf file + {"upf201"}, //upf types + {"Si.orb"}, //orb file + {2}, //number of each elements + {28.0}, //atomic mass + "Cartesian", //coordination type + {0.0,0.0,0.0, //atomic coordinates + 0.25,0.25,0.25})} +}; +#endif diff --git a/source/module_cell/module_neighbor/test/sltk_adjacent_set_test.cpp b/source/module_cell/module_neighbor/test/sltk_adjacent_set_test.cpp index feb7d7269a0..70aa8707b29 100644 --- a/source/module_cell/module_neighbor/test/sltk_adjacent_set_test.cpp +++ b/source/module_cell/module_neighbor/test/sltk_adjacent_set_test.cpp @@ -8,22 +8,30 @@ /** * - Tested Functions: - * - delete_vector() - * - delete vector offset and box - * - assertCoordinateIsRight() - * - confirm that the coordinate is right - * - Index() - * - Use ternary to represent coordinates + * - SetWithoutExpand() + * - set box index without expand + * - SetWithExpand() + * - set box index with expand */ -class sltkAdjacentSet : public testing::Test +class SltkAdjacentSetTest : public testing::Test { protected: AdjacentSet test; }; -TEST_F(sltkAdjacentSet,deleteVectorAndIndex) +TEST_F(SltkAdjacentSetTest, SetWithoutExpand) { + // set parameters + test.setDx(3); + test.setDy(3); + test.setDz(3); + test.setCenter(13); + test.setExpandFlag(false); + test.setTrueX(1); + test.setTrueY(1); + test.setTrueZ(1); + // set box and offset int x=1; int y=1; int z=1; @@ -31,22 +39,77 @@ TEST_F(sltkAdjacentSet,deleteVectorAndIndex) int test_grid_in=1; test.set(x,y,z,offset,test_grid_in); EXPECT_EQ(test.box[0],26); + // set box and offset x=0; y=1; z=-1; test.set(x,y,z,offset,test_grid_in); EXPECT_EQ(test.box[1],15); + // check length of box and offset + EXPECT_EQ(test.box.size(), 2); + EXPECT_EQ(test.offset.size(), 2); + EXPECT_EQ(test.getLength(), 2); + // getBox + int box_x = 0; + int box_y = 0; + int box_z = 0; + test.getBox(26, box_x, box_y, box_z); + EXPECT_EQ(box_x, 1); + EXPECT_EQ(box_y, 1); + EXPECT_EQ(box_z, 1); + test.getBox(15, box_x, box_y, box_z); + EXPECT_EQ(box_x, 0); + EXPECT_EQ(box_y, 1); + EXPECT_EQ(box_z, -1); + // delete_vector test.delete_vector(); EXPECT_EQ(test.box.size(),0); + EXPECT_EQ(test.offset.size(), 0); } -/*TEST_F(sltkAdjacentSet,assertCoordinateIsRight) + +TEST_F(SltkAdjacentSetTest, SetWithExpand) { - std::string output; - testing::internal::CaptureStdout(); - EXPECT_EXIT(ModuleBase::assertCoordinateIsRight(), ::testing::ExitedWithCode(0),""); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output,testing::HasSubstr(" - - ")); + // set parameters + test.setDx(6); + test.setDy(6); + test.setDz(6); + test.setCenter(43); + test.setExpandFlag(true); + test.setTrueX(1); + test.setTrueY(1); + test.setTrueZ(1); + // set box and offset + int x = 0; + int y = 0; + int z = 0; + int offset = 1; + int test_grid_in = 1; + test.set(x, y, z, offset, test_grid_in); + EXPECT_EQ(test.box[0], 43); + // set box and offset + x = 1; + y = 0; + z = 0; + test.set(x, y, z, offset, test_grid_in); + EXPECT_EQ(test.box[1], 79); + // check length of box and offset + EXPECT_EQ(test.box.size(), 2); + EXPECT_EQ(test.offset.size(), 2); + EXPECT_EQ(test.getLength(), 2); + // getBox + int box_x; + int box_y; + int box_z; + test.getBox(43, box_x, box_y, box_z); + EXPECT_EQ(box_x, 0); + EXPECT_EQ(box_y, 0); + EXPECT_EQ(box_z, 0); + test.getBox(79, box_x, box_y, box_z); + EXPECT_EQ(box_x, 1); + EXPECT_EQ(box_y, 0); + EXPECT_EQ(box_z, 0); + // delete_vector + test.delete_vector(); + EXPECT_EQ(test.box.size(), 0); + EXPECT_EQ(test.offset.size(), 0); } -*/ diff --git a/source/module_cell/module_neighbor/test/sltk_atom_arrange_test.cpp b/source/module_cell/module_neighbor/test/sltk_atom_arrange_test.cpp index 512f951e825..caf59aad876 100644 --- a/source/module_cell/module_neighbor/test/sltk_atom_arrange_test.cpp +++ b/source/module_cell/module_neighbor/test/sltk_atom_arrange_test.cpp @@ -1,10 +1,36 @@ -#include "gtest/gtest.h" -#include "gmock/gmock.h" -#include "./for_test_sltk_atom_arrange.h" #include "../sltk_atom_arrange.h" -#include -#include +#include +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "prepare_unitcell.h" + +#ifdef __LCAO +InfoNonlocal::InfoNonlocal() +{ +} +InfoNonlocal::~InfoNonlocal() +{ +} +LCAO_Orbitals::LCAO_Orbitals() +{ +} +LCAO_Orbitals::~LCAO_Orbitals() +{ +} +#endif +Magnetism::Magnetism() +{ + this->tot_magnetization = 0.0; + this->abs_magnetization = 0.0; + this->start_magnetization = nullptr; +} +Magnetism::~Magnetism() +{ + delete[] this->start_magnetization; +} /************************************************ * unit test of atom_arrange @@ -15,132 +41,80 @@ * - atom_arrange::delete_vector(void) * - delete vector * - atom_arrange::set_sr_NL - * - set the sr + * - set the sr: search radius including nonlocal beta */ -class sltkatomarrange : public testing::Test +void SetGlobalV() { + GlobalV::test_grid = 0; + GlobalV::test_grid_driver = 0; + GlobalV::test_deconstructor = 0; +} + +class SltkAtomArrangeTest : public testing::Test +{ + protected: + UnitCell* ucell; + UcellTestPrepare utp = UcellTestLib["Si"]; + std::ofstream ofs; + std::ifstream ifs; + bool pbc = true; + double radius = ((8 + 5.01) * 2.0 + 0.01) / 10.2; + int test_atom_in = 0; + std::string output; + void SetUp() + { + SetGlobalV(); + ucell = utp.SetUcellInfo(); + } + void TearDown() + { + delete ucell; + } }; -TEST_F(sltkatomarrange,setsrNL) +TEST_F(SltkAtomArrangeTest, setsrNL) { atom_arrange test; - std::string teststring="m"; - double rcutmax_Phi=1; - double rcutmax_Beta=2; - bool gamma_only_local=true; - double test_sr=0; + std::string teststring = "m"; + double rcutmax_Phi = 1; + double rcutmax_Beta = 2; + bool gamma_only_local = true; + double test_sr = 0; std::ofstream ofs; ofs.open("./to_test_arrange.txt"); - test_sr=test.set_sr_NL(ofs,teststring,rcutmax_Phi,rcutmax_Beta,gamma_only_local); - EXPECT_DOUBLE_EQ(test_sr,2.01); + test_sr = test.set_sr_NL(ofs, teststring, rcutmax_Phi, rcutmax_Beta, gamma_only_local); + EXPECT_DOUBLE_EQ(test_sr, 2.01); - gamma_only_local=false; - test_sr=test.set_sr_NL(ofs,teststring,rcutmax_Phi,rcutmax_Beta,gamma_only_local); - EXPECT_DOUBLE_EQ(test_sr,6.01); + gamma_only_local = false; + test_sr = test.set_sr_NL(ofs, teststring, rcutmax_Phi, rcutmax_Beta, gamma_only_local); + EXPECT_DOUBLE_EQ(test_sr, 6.01); - teststring="no"; - test_sr=test.set_sr_NL(ofs,teststring,rcutmax_Phi,rcutmax_Beta,gamma_only_local); + teststring = "no"; + test_sr = test.set_sr_NL(ofs, teststring, rcutmax_Phi, rcutmax_Beta, gamma_only_local); ofs.close(); std::ifstream ifs; - std::string test2,s; + std::string test2, s; ifs.open("./to_test_arrange.txt"); - while (getline(ifs,s)) - { - test2 += s; - } - std::string test3=" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" - " | |" - " | Search adjacent atoms: |" - " | Set the adjacent atoms for each atom and set the periodic boundary |" - " | condition for the atoms on real space FFT grid. For k-dependent |" - " | algorithm, we also need to set the sparse H and S matrix element |" - " | for each atom. |" - " | |" - " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" - " SETUP SEARCHING RADIUS FOR PROGRAM TO SEARCH ADJACENT ATOMS" - " longest orb rcut (Bohr) = 1 longest nonlocal projector rcut (Bohr) = 2"; - EXPECT_EQ(test2,test3); - + std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + EXPECT_THAT(str, testing::HasSubstr("longest orb rcut (Bohr) = 1")); + EXPECT_THAT(str, testing::HasSubstr("longest nonlocal projector rcut (Bohr) = 2")); ifs.close(); remove("./to_test_arrange"); } -TEST_F(sltkatomarrange,DeleteVector) -{ - atom_arrange test; - std::ofstream ofs("./testforarrange.txt"); - bool pbc_flag = true; - Grid_Driver grid_d(1,2,3); - UnitCell ucell; - double search_radius_bohr=2; - int test_atom_in= 0; - grid_d.init_cell_flag=false; - - int dx=1,dy=1,dz=1; - grid_d.Cell = new CellSet**[dx]; - for (int i = 0;i < dx;i++) - { - grid_d.Cell[i] = new CellSet*[dy]; - - for (int j = 0;j < dy;j++) - { - grid_d.Cell[i][j] = new CellSet[dz]; - - for (int k = 0;k < dz;k++) - { - grid_d.Cell[i][j][k].length = 0; - } - } - } - std::string output; - testing::internal::CaptureStdout(); - test.delete_vector(ofs,pbc_flag,grid_d,ucell,search_radius_bohr,test_atom_in); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output,testing::HasSubstr("test")); - ofs.close(); - -} - -TEST_F(sltkatomarrange,DeleteVector2) +TEST_F(SltkAtomArrangeTest, Search) { - std::ofstream ofs("./testforarrange.txt"); - atom_arrange test2; - bool pbc_flag = true; - Grid_Driver grid_d2(1,2,3); - UnitCell ucell; - double search_radius_bohr=2; - int test_atom_in= 0; - - int dx=1,dy=1,dz=1; - grid_d2.dx=1; - grid_d2.dy=1; - grid_d2.dz=1; - grid_d2.init_cell_flag=true; - grid_d2.Cell = new CellSet**[grid_d2.dx]; - for (int i = 0;i < grid_d2.dx;i++) - { - grid_d2.Cell[i] = new CellSet*[grid_d2.dy]; - - for (int j = 0;j < grid_d2.dy;j++) - { - grid_d2.Cell[i][j] = new CellSet[grid_d2.dz]; - - for (int k = 0;k < grid_d2.dz;k++) - { - grid_d2.Cell[i][j][k].length = 0; - } - } - } - EXPECT_EQ(grid_d2.Cell[0][0][0].length,0); - std::string output; - testing::internal::CaptureStdout(); - test2.delete_vector(ofs,pbc_flag,grid_d2,ucell,search_radius_bohr,test_atom_in); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output,testing::HasSubstr("test")); -// EXPECT_EQ(grid_d.Cell,NULL); - EXPECT_FALSE(grid_d2.init_cell_flag); - ofs.close(); -} - - + ucell->check_dtau(); + Grid_Driver grid_d(GlobalV::test_deconstructor, GlobalV::test_grid_driver, GlobalV::test_grid); + ofs.open("test.out"); + bool test_only = true; + atom_arrange::search(pbc, ofs, grid_d, *ucell, radius, test_atom_in, test_only); + EXPECT_EQ(grid_d.getType(0),0); + EXPECT_EQ(grid_d.getNatom(0), 1); // adjacent atom is 1 + ofs.close(); + ifs.open("test.out"); + std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + EXPECT_THAT(str, testing::HasSubstr("search neighboring atoms done.")); + remove("test.out"); +} \ No newline at end of file diff --git a/source/module_cell/module_neighbor/test/sltk_atom_input_test.cpp b/source/module_cell/module_neighbor/test/sltk_atom_input_test.cpp new file mode 100644 index 00000000000..617674256a8 --- /dev/null +++ b/source/module_cell/module_neighbor/test/sltk_atom_input_test.cpp @@ -0,0 +1,275 @@ +#include "../sltk_atom_input.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "prepare_unitcell.h" + +#ifdef __LCAO +InfoNonlocal::InfoNonlocal() +{ +} +InfoNonlocal::~InfoNonlocal() +{ +} +LCAO_Orbitals::LCAO_Orbitals() +{ +} +LCAO_Orbitals::~LCAO_Orbitals() +{ +} +#endif +Magnetism::Magnetism() +{ + this->tot_magnetization = 0.0; + this->abs_magnetization = 0.0; + this->start_magnetization = nullptr; +} +Magnetism::~Magnetism() +{ + delete[] this->start_magnetization; +} + +/************************************************ + * unit test of sltk_atom_input.cpp + * *********************************************/ + +/** + * - Tested Functions: + * - Constructor: + * - the constructor of Atom_input do almost everything + * - (1) it finds the boundary of atomic coordinates (x_min, x_max, etc.), + * - (2) expands the unitcell according to the boundary condition + * and the searching radius, which is (2 * rcutmax_Phi + 0.01) in + * gamma_only calculation and (2 * (rcutmax_Phi +rcutmax_Beta) + 0.01) + * in multi-k calculation (see atom_arrange::set_sr_NL). And determine + * the number of positive and negative layers in each direction in + * Check_Expand_Condition() + * - (3) records the amount of atoms after expansion (d_amount_expand) and + * sets the expanded lattice grids, including their coordinates and their + * atomic coordinates inside in Expand_Grid() + * - (4) and calculate the number of unitcells in x, y, z directions + * in calculate_cells() + * - Getters: + * - get the values obtained in constructor, for example + * - Clength0() is to get the length in x direction of the expanded big cell in + * unit of lat0 + * - minX() will return x_min if no expanding, or -glayerX_minus in case of + * expansion + * - getCellX() will return the number of unitcells in x direction + * - getCellXLength() will return search radius if no expanding, or 1 in case of + * expansion + * - getRadius() will return the search radius + * - getLatNow() will return lat0 in Bohr + * - getAmount() will return the total number of lattice grids after expansion + */ + +void SetGlobalV() +{ + GlobalV::test_grid = 0; +} + +class SltkAtomInputTest : public ::testing::Test +{ + protected: + UnitCell* ucell; + UcellTestPrepare utp = UcellTestLib["Si"]; + std::ofstream ofs; + std::ifstream ifs; + bool pbc = true; + double radius = ((8 + 5.01) * 2.0 + 0.01) / 10.2; + int test_atom_in = 0; + std::string output; + void SetUp() + { + SetGlobalV(); + ucell = utp.SetUcellInfo(); + } + void TearDown() + { + delete ucell; + } +}; + +using SltkAtomInputDeathTest = SltkAtomInputTest; + +TEST_F(SltkAtomInputTest, Constructor) +{ + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 2; + GlobalV::test_grid = 1; + Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in); + ofs.close(); + ifs.open("test.out"); + std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + EXPECT_THAT(str, testing::HasSubstr("ntype = 1")); + EXPECT_THAT(str, testing::HasSubstr("Amount(atom number) = 2")); + EXPECT_THAT(str, testing::HasSubstr("Periodic_boundary = 1")); + EXPECT_THAT(str, testing::HasSubstr("Searching radius(lat0) = 2.55")); + EXPECT_THAT(str, testing::HasSubstr("CellLength(unit: lat0) = [ 0.707107, 0.707107, 0.707107 ]")); + EXPECT_THAT(str, testing::HasSubstr("min_tau = [ -0.75, 0, 0 ]")); + EXPECT_THAT(str, testing::HasSubstr("max_tau = [ 0, 0.75, 0.75 ]")); + EXPECT_THAT(str, testing::HasSubstr("glayer+ = [ 6, 6, 6 ]")); + EXPECT_THAT(str, testing::HasSubstr("glayer- = [ 5, 5, 5 ]")); + EXPECT_THAT(str, testing::HasSubstr("CellDim = [ 11, 11, 11 ]")); + ifs.close(); + remove("test.out"); +} + +TEST_F(SltkAtomInputTest, Getters) +{ + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 2; + Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in); + EXPECT_TRUE(Atom_inp.getExpandFlag()); + EXPECT_EQ(Atom_inp.getAmount(), 2662); + EXPECT_EQ(Atom_inp.getBoundary(), 1); + EXPECT_DOUBLE_EQ(Atom_inp.getLatNow(), 10.2); + EXPECT_NEAR(Atom_inp.getRadius(), 2.55196, 1e-5); + EXPECT_DOUBLE_EQ(Atom_inp.getCellXLength(), 1.0); + EXPECT_DOUBLE_EQ(Atom_inp.getCellYLength(), 1.0); + EXPECT_DOUBLE_EQ(Atom_inp.getCellZLength(), 1.0); + EXPECT_NEAR(Atom_inp.Clength0(), 7.77817, 1e-5); + EXPECT_NEAR(Atom_inp.Clength1(), 7.77817, 1e-5); + EXPECT_NEAR(Atom_inp.Clength2(), 7.77817, 1e-5); + EXPECT_DOUBLE_EQ(Atom_inp.minX(), -5.0); + EXPECT_DOUBLE_EQ(Atom_inp.minY(), -5.0); + EXPECT_DOUBLE_EQ(Atom_inp.minZ(), -5.0); + EXPECT_EQ(Atom_inp.getCellX(), 11); + EXPECT_EQ(Atom_inp.getCellY(), 11); + EXPECT_EQ(Atom_inp.getCellZ(), 11); + EXPECT_EQ(Atom_inp.getGrid_layerX(), 6); + EXPECT_EQ(Atom_inp.getGrid_layerY(), 6); + EXPECT_EQ(Atom_inp.getGrid_layerZ(), 6); + EXPECT_EQ(Atom_inp.getGrid_layerX_minus(), 5); + EXPECT_EQ(Atom_inp.getGrid_layerY_minus(), 5); + EXPECT_EQ(Atom_inp.getGrid_layerZ_minus(), 5); + ofs.close(); + remove("test.out"); +} + +TEST_F(SltkAtomInputDeathTest, ConstructorWarning1) +{ + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 1; + radius = -1; + testing::internal::CaptureStdout(); + EXPECT_EXIT(Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in), + ::testing::ExitedWithCode(0), + ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("search radius < 0,forbidden")); + ofs.close(); + remove("test.out"); +} + +TEST_F(SltkAtomInputDeathTest, ConstructorWarning2) +{ + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 1; + ucell->atoms[0].taud[1].x = -0.25; + testing::internal::CaptureStdout(); + EXPECT_EXIT(Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in), + ::testing::ExitedWithCode(0), + ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("dminX<0.0")); + ofs.close(); + remove("test.out"); +} + +TEST_F(SltkAtomInputDeathTest, ConstructorWarning3) +{ + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 1; + ucell->atoms[0].taud[1].y = -0.25; + testing::internal::CaptureStdout(); + EXPECT_EXIT(Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in), + ::testing::ExitedWithCode(0), + ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("dminY<0.0")); + ofs.close(); + remove("test.out"); +} + +TEST_F(SltkAtomInputDeathTest, ConstructorWarning4) +{ + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 1; + ucell->atoms[0].taud[1].z = -0.25; + testing::internal::CaptureStdout(); + EXPECT_EXIT(Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in), + ::testing::ExitedWithCode(0), + ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("dminZ<0.0")); + ofs.close(); + remove("test.out"); +} + +TEST_F(SltkAtomInputTest, ConstructorNoExpand) +{ + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 1; + GlobalV::test_grid = 1; + // this is a bug if radius is too small + // because the expand_flag will be false! + radius = 1e-1000; + Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in); + EXPECT_FALSE(Atom_inp.getExpandFlag()); + // call set_FAtom and Load_atom + FAtom fa; + EXPECT_NO_THROW(Atom_inp.set_FAtom(*ucell, fa)); + ofs.close(); + ifs.open("test.out"); + std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + EXPECT_THAT(str, testing::HasSubstr("ntype = 1")); + EXPECT_THAT(str, testing::HasSubstr("Amount(atom number) = 2")); + EXPECT_THAT(str, testing::HasSubstr("Periodic_boundary = 1")); + EXPECT_THAT(str, testing::HasSubstr("Searching radius(lat0) = 0")); + EXPECT_THAT(str, testing::HasSubstr("CellLength(unit: lat0) = [ 0.707107, 0.707107, 0.707107 ]")); + EXPECT_THAT(str, testing::HasSubstr("min_tau = [ -0.75, 0, 0 ]")); + EXPECT_THAT(str, testing::HasSubstr("max_tau = [ 0, 0.75, 0.75 ]")); + EXPECT_THAT(str, testing::HasSubstr("glayer+ = [ 2, 2, 2 ]")); + EXPECT_THAT(str, testing::HasSubstr("glayer- = [ 0, 0, 0 ]")); + ifs.close(); + remove("test.out"); +} + +TEST_F(SltkAtomInputTest, ConstructorSmallSearchRadius) +{ + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 1; + GlobalV::test_grid = 1; + radius = 0.5; + Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in); + EXPECT_TRUE(Atom_inp.getExpandFlag()); + // call set_FAtom and Load_atom + FAtom fa; + EXPECT_NO_THROW(Atom_inp.set_FAtom(*ucell, fa)); + ofs.close(); + ifs.open("test.out"); + std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + EXPECT_THAT(str, testing::HasSubstr("ntype = 1")); + EXPECT_THAT(str, testing::HasSubstr("Amount(atom number) = 2")); + EXPECT_THAT(str, testing::HasSubstr("Periodic_boundary = 1")); + EXPECT_THAT(str, testing::HasSubstr("Searching radius(lat0) = 0.5")); + EXPECT_THAT(str, testing::HasSubstr("CellLength(unit: lat0) = [ 0.707107, 0.707107, 0.707107 ]")); + EXPECT_THAT(str, testing::HasSubstr("min_tau = [ -0.75, 0, 0 ]")); + EXPECT_THAT(str, testing::HasSubstr("max_tau = [ 0, 0.75, 0.75 ]")); + EXPECT_THAT(str, testing::HasSubstr("glayer+ = [ 2, 2, 2 ]")); + EXPECT_THAT(str, testing::HasSubstr("glayer- = [ 2, 2, 2 ]")); + EXPECT_THAT(str, testing::HasSubstr("CellDim = [ 4, 4, 4 ]")); + ifs.close(); + remove("test.out"); +} \ No newline at end of file diff --git a/source/module_cell/module_neighbor/test/sltk_atom_test.cpp b/source/module_cell/module_neighbor/test/sltk_atom_test.cpp index 9a7e0da2a48..6a8488f4e11 100644 --- a/source/module_cell/module_neighbor/test/sltk_atom_test.cpp +++ b/source/module_cell/module_neighbor/test/sltk_atom_test.cpp @@ -8,29 +8,52 @@ /** * - Tested Functions: - * - FAtom::delete_vector(void) - * - delete the vector + * - AllocateAdjacentSet + * - FAtom::allocate_AdjacentSet() + * - FAtom has a member "as", which is a shared_ptr of AdjacentSet + * - allocate_AdjacentSet() is a function of FAtom to + * allocate "as" + * - FAtom::delete_vector() + * - this function will call AdjacentSet::delete_vector() to + * - delete the box and offset vectors in AdjacentSet + * - SetterGetters: + * - the setter and getter of FAtom + * - including d_x, d_y, d_z, type, natom */ -class sltkatom : public testing::Test +class SltkAtomTest : public testing::Test { +protected: + FAtom test; }; -TEST_F(sltkatom,deleteVector) +TEST_F(SltkAtomTest, AllocateAdjacentSet) { - FAtom test; test.allocate_AdjacentSet(); - std::shared_ptr a=test.getAdjacentSet(); - int x=1; - int y=1; - int z=1; - int offset=1; - int test_grid_in=1; - a->set(x,y,z,offset,test_grid_in); - EXPECT_EQ(a->box[0],26); - + std::shared_ptr a = test.getAdjacentSet(); + int x = 1; + int y = 1; + int z = 1; + int offset = 1; + int test_grid_in = 1; + a->set(x, y, z, offset, test_grid_in); + EXPECT_EQ(a->box[0], 26); + test.delete_vector(); - std::shared_ptr b=test.getAdjacentSet(); - EXPECT_EQ(b->box.size(),0); + std::shared_ptr b = test.getAdjacentSet(); + EXPECT_EQ(b->box.size(), 0); } +TEST_F(SltkAtomTest, SetterGetters) +{ + test.setX(1.0); + test.setY(2.0); + test.setZ(3.0); + test.setType(4); + test.setNatom(5); + EXPECT_DOUBLE_EQ(test.x(), 1.0); + EXPECT_DOUBLE_EQ(test.y(), 2.0); + EXPECT_DOUBLE_EQ(test.z(), 3.0); + EXPECT_EQ(test.getType(), 4); + EXPECT_EQ(test.getNatom(), 5); +} diff --git a/source/module_cell/module_neighbor/test/sltk_grid_test.cpp b/source/module_cell/module_neighbor/test/sltk_grid_test.cpp index 7d7dc190952..ba26e42b0a5 100644 --- a/source/module_cell/module_neighbor/test/sltk_grid_test.cpp +++ b/source/module_cell/module_neighbor/test/sltk_grid_test.cpp @@ -1,6 +1,34 @@ -#include "gtest/gtest.h" #include "gmock/gmock.h" +#include "gtest/gtest.h" + +#define private public #include "../sltk_grid.h" +#include "prepare_unitcell.h" + +#ifdef __LCAO +InfoNonlocal::InfoNonlocal() +{ +} +InfoNonlocal::~InfoNonlocal() +{ +} +LCAO_Orbitals::LCAO_Orbitals() +{ +} +LCAO_Orbitals::~LCAO_Orbitals() +{ +} +#endif +Magnetism::Magnetism() +{ + this->tot_magnetization = 0.0; + this->abs_magnetization = 0.0; + this->start_magnetization = nullptr; +} +Magnetism::~Magnetism() +{ + delete[] this->start_magnetization; +} /************************************************ * unit test of sltk_grid @@ -8,57 +36,143 @@ /** * - Tested Functions: - * - Grid::get the argu ment - * - get the dx, dy, dz, dx_min, dy_min, dz_min + * - Init: Grid::init() + * - setMemberVariables: really set member variables + * (like dx, dy, dz and d_minX, d_minY, d_minZ) by + * reading from getters of Atom_input, and construct the + * member Cell as a 3D array of CellSet */ -AtomLink::AtomLink(const FAtom &atom, AtomLink* const pointNext) -: fatom(atom), next_p(pointNext) {} -Grid::Grid(const int &test_grid_in):test_grid(test_grid_in) +void SetGlobalV() { - init_cell_flag = false; - this->atomlink = new AtomLink[1]; + GlobalV::test_grid = 0; } -Grid::~Grid() + +class SltkGridTest : public testing::Test { - delete[] atomlink; - this->delete_Cell(); -} -FAtom::FAtom() + protected: + UnitCell* ucell; + UcellTestPrepare utp = UcellTestLib["Si"]; + std::ofstream ofs; + std::ifstream ifs; + bool pbc = true; + double radius = ((8 + 5.01) * 2.0 + 0.01) / 10.2; + int test_atom_in = 0; + std::string output; + void SetUp() + { + SetGlobalV(); + ucell = utp.SetUcellInfo(); + } + void TearDown() + { + delete ucell; + } +}; + +using SltkGridDeathTest = SltkGridTest; + +TEST_F(SltkGridTest, Init) { - d_x = 0.0; - d_y = 0.0; - d_z = 0.0; - as = nullptr; - type = 0; - natom = 0; + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 2; + GlobalV::test_grid = 1; + Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in); + Grid LatGrid(GlobalV::test_grid); + LatGrid.init(ofs, *ucell, Atom_inp); + EXPECT_TRUE(LatGrid.init_cell_flag); + EXPECT_EQ(LatGrid.getCellX(), 11); + EXPECT_EQ(LatGrid.getCellY(), 11); + EXPECT_EQ(LatGrid.getCellZ(), 11); + EXPECT_EQ(LatGrid.getD_minX(), -5); + EXPECT_EQ(LatGrid.getD_minY(), -5); + EXPECT_EQ(LatGrid.getD_minZ(), -5); + ofs.close(); + remove("test.out"); } -FAtom::~FAtom(){} -class sltkgrid : public testing::Test +TEST_F(SltkGridTest, InitSmall) { -}; + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 2; + GlobalV::test_grid = 1; + radius = 0.5; + Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in); + Grid LatGrid(GlobalV::test_grid); + LatGrid.setMemberVariables(ofs, Atom_inp); + EXPECT_EQ(LatGrid.natom, Atom_inp.getAmount()); + EXPECT_EQ(LatGrid.natom, 128); + EXPECT_EQ(LatGrid.pbc, Atom_inp.getBoundary()); + EXPECT_TRUE(LatGrid.pbc); + EXPECT_DOUBLE_EQ(LatGrid.sradius, Atom_inp.getRadius()); + EXPECT_DOUBLE_EQ(LatGrid.sradius, 0.5); + for (int i = 0; i < 3; i++) + { + EXPECT_DOUBLE_EQ(LatGrid.vec1[i], Atom_inp.vec1[i]); + EXPECT_DOUBLE_EQ(LatGrid.vec2[i], Atom_inp.vec2[i]); + EXPECT_DOUBLE_EQ(LatGrid.vec3[i], Atom_inp.vec3[i]); + } + EXPECT_EQ(LatGrid.vec1[0], -0.5); + EXPECT_EQ(LatGrid.vec1[1], 0.0); + EXPECT_EQ(LatGrid.vec1[2], 0.5); + EXPECT_EQ(LatGrid.vec2[0], 0.0); + EXPECT_EQ(LatGrid.vec2[1], 0.5); + EXPECT_EQ(LatGrid.vec2[2], 0.5); + EXPECT_EQ(LatGrid.vec3[0], -0.5); + EXPECT_EQ(LatGrid.vec3[1], 0.5); + EXPECT_EQ(LatGrid.vec3[2], 0.0); + // lattice grid length + EXPECT_DOUBLE_EQ(LatGrid.grid_length[0], Atom_inp.Clength0()); + EXPECT_DOUBLE_EQ(LatGrid.grid_length[1], Atom_inp.Clength1()); + EXPECT_DOUBLE_EQ(LatGrid.grid_length[2], Atom_inp.Clength2()); + EXPECT_NEAR(LatGrid.grid_length[0], 2.8284271247461903, 1e-15); + EXPECT_NEAR(LatGrid.grid_length[1], 2.8284271247461903, 1e-15); + EXPECT_NEAR(LatGrid.grid_length[2], 2.8284271247461903, 1e-15); + // minimal value of x, y, z + EXPECT_DOUBLE_EQ(LatGrid.d_minX, Atom_inp.minX()); + EXPECT_DOUBLE_EQ(LatGrid.d_minY, Atom_inp.minY()); + EXPECT_DOUBLE_EQ(LatGrid.d_minZ, Atom_inp.minZ()); + EXPECT_DOUBLE_EQ(LatGrid.d_minX, -2); + EXPECT_DOUBLE_EQ(LatGrid.d_minY, -2); + EXPECT_DOUBLE_EQ(LatGrid.d_minZ, -2); + // length of cells in x, y, z + EXPECT_DOUBLE_EQ(LatGrid.cell_x_length, Atom_inp.getCellXLength()); + EXPECT_DOUBLE_EQ(LatGrid.cell_y_length, Atom_inp.getCellYLength()); + EXPECT_DOUBLE_EQ(LatGrid.cell_z_length, Atom_inp.getCellZLength()); + EXPECT_DOUBLE_EQ(LatGrid.cell_x_length, 1.0); + EXPECT_DOUBLE_EQ(LatGrid.cell_y_length, 1.0); + EXPECT_DOUBLE_EQ(LatGrid.cell_z_length, 1.0); + // number of cells in x, y, z + EXPECT_EQ(LatGrid.dx, Atom_inp.getCellX()); + EXPECT_EQ(LatGrid.dy, Atom_inp.getCellY()); + EXPECT_EQ(LatGrid.dz, Atom_inp.getCellZ()); + EXPECT_EQ(LatGrid.dx, 4); + EXPECT_EQ(LatGrid.dy, 4); + EXPECT_EQ(LatGrid.dz, 4); + // init cell flag + EXPECT_TRUE(LatGrid.init_cell_flag); + + ofs.close(); + remove("test.out"); +} -TEST_F(sltkgrid,getTheArgument) +/* +// This test cannot pass because setAtomLinkArray() is unsuccessful +// if expand_flag is false +TEST_F(SltkGridTest, InitNoExpand) { - Grid test(1); - test.dx=1; - test.dy=2; - test.dz=3; - test.d_minX=4; - test.d_minY=5; - test.d_minZ=6; - double testx=test.getCellX(); - double testy=test.getCellY(); - double testz=test.getCellZ(); - double testminx=test.getD_minX(); - double testminy=test.getD_minY(); - double testminz=test.getD_minZ(); - EXPECT_EQ(testx,1); - EXPECT_EQ(testy,2); - EXPECT_EQ(testz,3); - EXPECT_EQ(testminx,4); - EXPECT_EQ(testminy,5); - EXPECT_EQ(testminz,6); + ofs.open("test.out"); + ucell->check_dtau(); + test_atom_in = 2; + GlobalV::test_grid = 1; + double radius = 1e-1000; + Atom_input Atom_inp(ofs, *ucell, ucell->nat, ucell->ntype, pbc, radius, test_atom_in); + Grid LatGrid(GlobalV::test_grid); + LatGrid.init(ofs, *ucell, Atom_inp); + ofs.close(); } +*/ +#undef private