diff --git a/source/source_cell/atom_spec.h b/source/source_cell/atom_spec.h index 96730b00de1..18ec8284976 100644 --- a/source/source_cell/atom_spec.h +++ b/source/source_cell/atom_spec.h @@ -31,7 +31,7 @@ class Atom std::vector l_nchi; // number of chi for each L int stapos_wf = 0; // start position of wave functions - std::string label = "\0"; // atomic symbol + std::string label; ///< atomic symbol std::vector> tau; // Cartesian coordinates of each atom in this type. std::vector> dis; // direct displacements of each atom in this type in current step liuyu modift 2023-03-22 std::vector> taud; // Direct coordinates of each atom in this type. @@ -40,7 +40,7 @@ class Atom std::vector> force; // force acting on each atom in this type. std::vector> lambda; // Lagrange multiplier for each atom in this type. used in deltaspin std::vector> constrain; // constrain for each atom in this type. used in deltaspin - std::string label_orb = "\0"; // atomic Element symbol in the orbital file of lcao + std::string label_orb; ///< atomic element symbol in the orbital file of lcao std::vector mag; std::vector angle1; // spin angle, added by zhengdy-soc diff --git a/source/source_cell/bcast_cell.cpp b/source/source_cell/bcast_cell.cpp index 5c2a2e268f8..57529fbc455 100644 --- a/source/source_cell/bcast_cell.cpp +++ b/source/source_cell/bcast_cell.cpp @@ -81,7 +81,7 @@ namespace unitcell Parallel_Common::bcast_double(lat.a2[i]); Parallel_Common::bcast_double(lat.a3[i]); Parallel_Common::bcast_double(lat.latcenter[i]); - Parallel_Common::bcast_int(lat.lc[i]); + Parallel_Common::bcast_int(lat.lat_axis_free[i]); } // distribute superlattice vectors. @@ -103,7 +103,11 @@ namespace unitcell { #ifdef __MPI MPI_Barrier(MPI_COMM_WORLD); - Parallel_Common::bcast_double(magnet.start_mag, ntype); + if (GlobalV::MY_RANK != 0) + { + magnet.start_mag.resize(ntype, 0.0); + } + Parallel_Common::bcast_double(magnet.start_mag.data(), ntype); if (PARAM.inp.nspin == 4) { Parallel_Common::bcast_double(magnet.ux_[0]); diff --git a/source/source_cell/magnetism.cpp b/source/source_cell/magnetism.cpp index a31bec1f796..09e8b7830bb 100644 --- a/source/source_cell/magnetism.cpp +++ b/source/source_cell/magnetism.cpp @@ -1,8 +1,5 @@ #include "magnetism.h" - #include "source_base/parallel_reduce.h" -#include "source_io/module_parameter/parameter.h" -//#include "source_estate/module_charge/charge.h" Magnetism::Magnetism() { @@ -14,13 +11,15 @@ Magnetism::Magnetism() Magnetism::~Magnetism() { - delete[] start_mag; } void Magnetism::compute_mag(const double& omega, - const int& nrxx, - const int& nxyz, - const double* const * rho, + const int& nrxx, + const int& nxyz, + const double* const * rho, + const int& nspin, + const bool& two_fermi, + const double& nelec, double* nelec_spin) { assert(omega>0.0); @@ -28,7 +27,7 @@ void Magnetism::compute_mag(const double& omega, const double fac = omega / nxyz; - if (PARAM.inp.nspin==2) + if (nspin==2) { this->tot_mag = 0.00; this->abs_mag = 0.00; @@ -51,17 +50,17 @@ void Magnetism::compute_mag(const double& omega, //update number of electrons for each spin //if TWO_EFERMI, no need to update - if(!PARAM.globalv.two_fermi) + if(!two_fermi) { - nelec_spin[0] = (PARAM.inp.nelec + this->tot_mag) / 2; - nelec_spin[1] = (PARAM.inp.nelec - this->tot_mag) / 2; + nelec_spin[0] = (nelec + this->tot_mag) / 2; + nelec_spin[1] = (nelec - this->tot_mag) / 2; ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"Electron number for spin up", nelec_spin[0]); ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"Electron number for spin down", nelec_spin[1]); } } // noncolliear : - else if(PARAM.inp.nspin==4) + else if(nspin==4) { for(int i=0;i<3;i++) { diff --git a/source/source_cell/magnetism.h b/source/source_cell/magnetism.h index b2cee2d6a95..679cc504d13 100644 --- a/source/source_cell/magnetism.h +++ b/source/source_cell/magnetism.h @@ -3,6 +3,7 @@ #include "source_base/global_function.h" #include "source_base/vector3.h" +#include class Magnetism { @@ -12,7 +13,7 @@ class Magnetism ~Magnetism(); // notice : bcast (MPI operation) is done in unitcell - double *start_mag=nullptr; + std::vector start_mag; // tot_mag : majority spin - minority spin (nelup - neldw). double tot_mag; @@ -22,16 +23,13 @@ class Magnetism double abs_mag; void compute_mag(const double& omega, - const int& nrxx, - const int& nxyz, - const double* const * rho, - double* nelec_spin = nullptr); - - ModuleBase::Vector3 *m_loc_=nullptr; //magnetization for each element along c-axis - - double *angle1_=nullptr; //angle between c-axis and real spin std::vector - - double *angle2_=nullptr; //angle between a-axis and real spin std::vector projection in ab-plane + const int& nrxx, + const int& nxyz, + const double* const * rho, + const int& nspin, + const bool& two_fermi, + const double& nelec, + double* nelec_spin); double ux_[3]={0.0}; diff --git a/source/source_cell/module_neighbor/test/prepare_unitcell.h b/source/source_cell/module_neighbor/test/prepare_unitcell.h index 11bd94ba7bd..1851ea4a062 100644 --- a/source/source_cell/module_neighbor/test/prepare_unitcell.h +++ b/source/source_cell/module_neighbor/test/prepare_unitcell.h @@ -77,13 +77,11 @@ class UcellTestPrepare this->init_vel, this->fixed_axes); - delete[] ucell->magnet.start_mag; //mag set here ucell->atom_label.resize(ucell->ntype); ucell->atom_mass.resize(ucell->ntype); ucell->pseudo_fn.resize(ucell->ntype); ucell->pseudo_type.resize(ucell->ntype); ucell->orbital_fn.resize(ucell->ntype); - ucell->magnet.start_mag = 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; @@ -94,7 +92,6 @@ class UcellTestPrepare 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_mag[it] = 0.0; //mag set here } //lattice info ucell->lat0 = this->lat0; diff --git a/source/source_cell/module_neighbor/test/sltk_atom_arrange_test.cpp b/source/source_cell/module_neighbor/test/sltk_atom_arrange_test.cpp index 329554d62c3..33039d87d7a 100644 --- a/source/source_cell/module_neighbor/test/sltk_atom_arrange_test.cpp +++ b/source/source_cell/module_neighbor/test/sltk_atom_arrange_test.cpp @@ -28,11 +28,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } /************************************************ diff --git a/source/source_cell/module_neighbor/test/sltk_grid_test.cpp b/source/source_cell/module_neighbor/test/sltk_grid_test.cpp index 044feafc2de..d903d5d7bd4 100644 --- a/source/source_cell/module_neighbor/test/sltk_grid_test.cpp +++ b/source/source_cell/module_neighbor/test/sltk_grid_test.cpp @@ -25,11 +25,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } /************************************************ diff --git a/source/source_cell/read_atoms.cpp b/source/source_cell/read_atoms.cpp index 7da6abefae9..0fd3dd0ee02 100644 --- a/source/source_cell/read_atoms.cpp +++ b/source/source_cell/read_atoms.cpp @@ -25,6 +25,11 @@ bool unitcell::read_atom_positions(UnitCell& ucell, const int nspin = PARAM.inp.nspin; assert (nspin==1 || nspin==2 || nspin==4); + if (ucell.magnet.start_mag.size() != static_cast(ntype)) + { + ucell.magnet.start_mag.resize(ntype, 0.0); + } + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifpos, "ATOMIC_POSITIONS")) { ModuleBase::GlobalFunc::READ_VALUE(ifpos, Coordinate); diff --git a/source/source_cell/test/CMakeLists.txt b/source/source_cell/test/CMakeLists.txt index 881a0cc179c..5b6f91e1bb0 100644 --- a/source/source_cell/test/CMakeLists.txt +++ b/source/source_cell/test/CMakeLists.txt @@ -4,6 +4,14 @@ abacus_disable_feature_definitions(__ROCM) abacus_disable_feature_definitions(__EXX) find_program(BASH bash) +file(COPY support DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +configure_file(bcast_atom_pseudo_test.sh ${CMAKE_CURRENT_BINARY_DIR}/bcast_atom_pseudo_test.sh COPYONLY) +configure_file(bcast_atom_spec_test.sh ${CMAKE_CURRENT_BINARY_DIR}/bcast_atom_spec_test.sh COPYONLY) +configure_file(parallel_kpoints_test.sh ${CMAKE_CURRENT_BINARY_DIR}/parallel_kpoints_test.sh COPYONLY) +configure_file(klist_test_para.sh ${CMAKE_CURRENT_BINARY_DIR}/klist_test_para.sh COPYONLY) +configure_file(unitcell_test_parallel.sh ${CMAKE_CURRENT_BINARY_DIR}/unitcell_test_parallel.sh COPYONLY) +configure_file(bcast_read_sep_test.sh ${CMAKE_CURRENT_BINARY_DIR}/bcast_read_sep_test.sh COPYONLY) +configure_file(bcast_sep_cell_test.sh ${CMAKE_CURRENT_BINARY_DIR}/bcast_sep_cell_test.sh COPYONLY) install(DIRECTORY support DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) install(FILES bcast_atom_pseudo_test.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) install(FILES bcast_atom_spec_test.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) @@ -193,3 +201,9 @@ add_test(NAME MODULE_CELL_sep_cell_parallel COMMAND ${BASH} bcast_sep_cell_test.sh WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} ) + +AddTest( + TARGET MODULE_CELL_magnetism + LIBS base device + SOURCES magnetism_test.cpp ../magnetism.cpp +) diff --git a/source/source_estate/test/elecstate_magnetism_test.cpp b/source/source_cell/test/magnetism_test.cpp similarity index 74% rename from source/source_estate/test/elecstate_magnetism_test.cpp rename to source/source_cell/test/magnetism_test.cpp index 0765591db65..e95afac9dc2 100644 --- a/source/source_estate/test/elecstate_magnetism_test.cpp +++ b/source/source_cell/test/magnetism_test.cpp @@ -6,10 +6,6 @@ // mohan add 2025-04-12 #include "source_estate/module_charge/charge.h" -#define private public -#include "source_io/module_parameter/parameter.h" -#undef private - /************************************************ * unit test of magnetism.cpp ***********************************************/ @@ -20,8 +16,8 @@ * - Magnetism::~Magnetism() * - Magnetism::judge_parallel() * - Magnetism::compute_mag() - * - compute mag for spin-polarized system when PARAM.input.nspin = 2 - * - and non-collinear case with PARAM.input.nspin = 4 + * - compute mag for spin-polarized system when nspin = 2 + * - and non-collinear case with nspin = 4 */ #define private public @@ -53,7 +49,7 @@ TEST_F(MagnetismTest, Magnetism) { EXPECT_EQ(0.0, magnetism->tot_mag); EXPECT_EQ(0.0, magnetism->abs_mag); - EXPECT_EQ(nullptr, magnetism->start_mag); + EXPECT_TRUE(magnetism->start_mag.empty()); } TEST_F(MagnetismTest, JudgeParallel) @@ -67,15 +63,15 @@ TEST_F(MagnetismTest, JudgeParallel) TEST_F(MagnetismTest, ComputeMagnetizationS2) { - PARAM.input.nspin = 2; - PARAM.sys.two_fermi = false; - PARAM.input.nelec = 10.0; + const int nspin = 2; + const bool two_fermi = false; + const double nelec = 10.0; Charge* chr = new Charge; chr->nrxx = 100; chr->nxyz = 1000; - chr->rho = new double*[PARAM.input.nspin]; - for (int i=0; i< PARAM.input.nspin; i++) + chr->rho = new double*[nspin]; + for (int i=0; i< nspin; i++) { chr->rho[i] = new double[chr->nrxx]; } @@ -85,13 +81,14 @@ TEST_F(MagnetismTest, ComputeMagnetizationS2) chr->rho[1][ir] = 1.01; } double* nelec_spin = new double[2]; - magnetism->compute_mag(500.0,chr->nrxx, chr->nxyz, chr->rho, nelec_spin); + magnetism->compute_mag(500.0,chr->nrxx, chr->nxyz, chr->rho, + nspin, two_fermi, nelec, nelec_spin); EXPECT_DOUBLE_EQ(-0.5, magnetism->tot_mag); EXPECT_DOUBLE_EQ(0.5, magnetism->abs_mag); EXPECT_DOUBLE_EQ(4.75, nelec_spin[0]); EXPECT_DOUBLE_EQ(5.25, nelec_spin[1]); delete[] nelec_spin; - for (int i=0; i< PARAM.input.nspin; i++) + for (int i=0; i< nspin; i++) { delete[] chr->rho[i]; } @@ -101,13 +98,13 @@ TEST_F(MagnetismTest, ComputeMagnetizationS2) TEST_F(MagnetismTest, ComputeMagnetizationS4) { - PARAM.input.nspin = 4; + const int nspin = 4; Charge* chr = new Charge; - chr->rho = new double*[PARAM.input.nspin]; + chr->rho = new double*[nspin]; chr->nrxx = 100; chr->nxyz = 1000; - for (int i=0; i< PARAM.input.nspin; i++) + for (int i=0; i< nspin; i++) { chr->rho[i] = new double[chr->nrxx]; } @@ -119,13 +116,14 @@ TEST_F(MagnetismTest, ComputeMagnetizationS4) chr->rho[3][ir] = 1.00; } double* nelec_spin = new double[4]; - magnetism->compute_mag(500.0,chr->nrxx, chr->nxyz, chr->rho, nelec_spin); + magnetism->compute_mag(500.0,chr->nrxx, chr->nxyz, chr->rho, + nspin, false, 0.0, nelec_spin); EXPECT_DOUBLE_EQ(100.0, magnetism->abs_mag); EXPECT_DOUBLE_EQ(50.0*std::sqrt(2.0), magnetism->tot_mag_nc[0]); EXPECT_DOUBLE_EQ(50.0, magnetism->tot_mag_nc[1]); EXPECT_DOUBLE_EQ(50.0, magnetism->tot_mag_nc[2]); delete[] nelec_spin; - for (int i=0; i< PARAM.input.nspin; i++) + for (int i=0; i< nspin; i++) { delete[] chr->rho[i]; } @@ -135,18 +133,20 @@ TEST_F(MagnetismTest, ComputeMagnetizationS4) #ifdef __MPI #include +#include "source_base/parallel_comm.h" int main(int argc, char **argv) { - MPI_Init(&argc, &argv); - MPI_Comm_size(MPI_COMM_WORLD,&GlobalV::NPROC); - MPI_Comm_rank(MPI_COMM_WORLD,&GlobalV::MY_RANK); + MPI_Comm_size(MPI_COMM_WORLD, &GlobalV::NPROC); + MPI_Comm_rank(MPI_COMM_WORLD, &GlobalV::MY_RANK); + MPI_Comm_dup(MPI_COMM_WORLD, &POOL_WORLD); testing::InitGoogleTest(&argc, argv); int result = RUN_ALL_TESTS(); + MPI_Comm_free(&POOL_WORLD); MPI_Finalize(); - return result; } #endif + diff --git a/source/source_cell/test/prepare_unitcell.h b/source/source_cell/test/prepare_unitcell.h index 73914644086..abfc9cf8def 100644 --- a/source/source_cell/test/prepare_unitcell.h +++ b/source/source_cell/test/prepare_unitcell.h @@ -76,13 +76,11 @@ class UcellTestPrepare this->init_vel, this->fixed_axes); - delete[] ucell->magnet.start_mag; //mag set here ucell->atom_label.resize(ucell->ntype); ucell->atom_mass.resize(ucell->ntype); ucell->pseudo_fn.resize(ucell->ntype); ucell->pseudo_type.resize(ucell->ntype); ucell->orbital_fn.resize(ucell->ntype); - ucell->magnet.start_mag = 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; @@ -93,7 +91,6 @@ class UcellTestPrepare 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_mag[it] = 0.0; //mag set here } //lattice info ucell->lat0 = this->lat0; diff --git a/source/source_cell/test/unitcell_test.cpp b/source/source_cell/test/unitcell_test.cpp index e9ca7fa10b9..20f9651a64e 100644 --- a/source/source_cell/test/unitcell_test.cpp +++ b/source/source_cell/test/unitcell_test.cpp @@ -38,11 +38,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } /************************************************ @@ -206,58 +204,58 @@ TEST_F(UcellTest, Setup) EXPECT_EQ(ucell->init_vel, init_vel_in); if (fixed_axes_in[i] == "None" || fixed_axes_in[i] == "volume" || fixed_axes_in[i] == "shape") { - EXPECT_EQ(ucell->lc[0], 1); - EXPECT_EQ(ucell->lc[1], 1); - EXPECT_EQ(ucell->lc[2], 1); + EXPECT_EQ(ucell->lat_axis_free[0], 1); + EXPECT_EQ(ucell->lat_axis_free[1], 1); + EXPECT_EQ(ucell->lat_axis_free[2], 1); EXPECT_TRUE(ucell->if_cell_can_change()); } else if (fixed_axes_in[i] == "a") { - EXPECT_EQ(ucell->lc[0], 0); - EXPECT_EQ(ucell->lc[1], 1); - EXPECT_EQ(ucell->lc[2], 1); + EXPECT_EQ(ucell->lat_axis_free[0], 0); + EXPECT_EQ(ucell->lat_axis_free[1], 1); + EXPECT_EQ(ucell->lat_axis_free[2], 1); EXPECT_TRUE(ucell->if_cell_can_change()); } else if (fixed_axes_in[i] == "b") { - EXPECT_EQ(ucell->lc[0], 1); - EXPECT_EQ(ucell->lc[1], 0); - EXPECT_EQ(ucell->lc[2], 1); + EXPECT_EQ(ucell->lat_axis_free[0], 1); + EXPECT_EQ(ucell->lat_axis_free[1], 0); + EXPECT_EQ(ucell->lat_axis_free[2], 1); EXPECT_TRUE(ucell->if_cell_can_change()); } else if (fixed_axes_in[i] == "c") { - EXPECT_EQ(ucell->lc[0], 1); - EXPECT_EQ(ucell->lc[1], 1); - EXPECT_EQ(ucell->lc[2], 0); + EXPECT_EQ(ucell->lat_axis_free[0], 1); + EXPECT_EQ(ucell->lat_axis_free[1], 1); + EXPECT_EQ(ucell->lat_axis_free[2], 0); EXPECT_TRUE(ucell->if_cell_can_change()); } else if (fixed_axes_in[i] == "ab") { - EXPECT_EQ(ucell->lc[0], 0); - EXPECT_EQ(ucell->lc[1], 0); - EXPECT_EQ(ucell->lc[2], 1); + EXPECT_EQ(ucell->lat_axis_free[0], 0); + EXPECT_EQ(ucell->lat_axis_free[1], 0); + EXPECT_EQ(ucell->lat_axis_free[2], 1); EXPECT_TRUE(ucell->if_cell_can_change()); } else if (fixed_axes_in[i] == "ac") { - EXPECT_EQ(ucell->lc[0], 0); - EXPECT_EQ(ucell->lc[1], 1); - EXPECT_EQ(ucell->lc[2], 0); + EXPECT_EQ(ucell->lat_axis_free[0], 0); + EXPECT_EQ(ucell->lat_axis_free[1], 1); + EXPECT_EQ(ucell->lat_axis_free[2], 0); EXPECT_TRUE(ucell->if_cell_can_change()); } else if (fixed_axes_in[i] == "bc") { - EXPECT_EQ(ucell->lc[0], 1); - EXPECT_EQ(ucell->lc[1], 0); - EXPECT_EQ(ucell->lc[2], 0); + EXPECT_EQ(ucell->lat_axis_free[0], 1); + EXPECT_EQ(ucell->lat_axis_free[1], 0); + EXPECT_EQ(ucell->lat_axis_free[2], 0); EXPECT_TRUE(ucell->if_cell_can_change()); } else if (fixed_axes_in[i] == "abc") { - EXPECT_EQ(ucell->lc[0], 0); - EXPECT_EQ(ucell->lc[1], 0); - EXPECT_EQ(ucell->lc[2], 0); + EXPECT_EQ(ucell->lat_axis_free[0], 0); + EXPECT_EQ(ucell->lat_axis_free[1], 0); + EXPECT_EQ(ucell->lat_axis_free[2], 0); EXPECT_FALSE(ucell->if_cell_can_change()); } } @@ -1294,9 +1292,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsS1) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1326,9 +1321,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsS2) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1359,9 +1351,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsS4Noncolin) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1392,9 +1381,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsS4Colin) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1424,9 +1410,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsC) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1456,9 +1439,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCA) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1488,9 +1468,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCACXY) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1520,9 +1497,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCACXZ) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1552,9 +1526,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCACYZ) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1584,9 +1555,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCACXYZ) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1617,9 +1585,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCAU) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); ofs_running.close(); ofs_warning.close(); @@ -1649,9 +1614,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsAutosetMag) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); for (int it = 0; it < ucell->ntype; it++) { @@ -1663,8 +1625,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsAutosetMag) } // for nspin == 4 PARAM.input.nspin = 4; - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning); for (int it = 0; it < ucell->ntype; it++) { @@ -1703,9 +1663,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning1) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning)); ofs_running.close(); ofs_warning.close(); @@ -1747,9 +1704,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning2) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning)); ofs_running.close(); ofs_warning.close(); @@ -1784,9 +1738,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning3) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell,ifa, ofs_running, GlobalV::ofs_warning)); ofs_running.close(); GlobalV::ofs_warning.close(); @@ -1822,9 +1773,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning4) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; testing::internal::CaptureStdout(); EXPECT_EXIT(unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning), ::testing::ExitedWithCode(1), ""); output = testing::internal::GetCapturedStdout(); @@ -1857,9 +1805,6 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning5) EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); - // mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell,ifa, ofs_running, GlobalV::ofs_warning)); ofs_running.close(); GlobalV::ofs_warning.close(); diff --git a/source/source_cell/test/unitcell_test_para.cpp b/source/source_cell/test/unitcell_test_para.cpp index ca6b3bad1f0..b60ab29bb90 100644 --- a/source/source_cell/test/unitcell_test_para.cpp +++ b/source/source_cell/test/unitcell_test_para.cpp @@ -31,11 +31,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } #define private public #include "source_io/module_parameter/parameter.h" diff --git a/source/source_cell/test/unitcell_test_readpp.cpp b/source/source_cell/test/unitcell_test_readpp.cpp index 77ab7a21a8b..eaa47425f25 100644 --- a/source/source_cell/test/unitcell_test_readpp.cpp +++ b/source/source_cell/test/unitcell_test_readpp.cpp @@ -25,9 +25,8 @@ InfoNonlocal::~InfoNonlocal() {} Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } -Magnetism::~Magnetism() { delete[] this->start_mag; } +Magnetism::~Magnetism() { } #define private public #include "source_io/module_parameter/parameter.h" #undef private diff --git a/source/source_cell/test/unitcell_test_setupcell.cpp b/source/source_cell/test/unitcell_test_setupcell.cpp index 3bb2cd7e859..d27c15f60f6 100644 --- a/source/source_cell/test/unitcell_test_setupcell.cpp +++ b/source/source_cell/test/unitcell_test_setupcell.cpp @@ -23,11 +23,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } /************************************************ @@ -148,11 +146,7 @@ TEST_F(UcellTest,SetupCellAfterVC) std::ofstream ofs_running; ofs_running.open("setup_cell.tmp"); PARAM.input.nspin = 1; - - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; - ucell->setup_cell(fn,ofs_running); ucell->lat0 = 1.0; ucell->latvec.Zero(); diff --git a/source/source_cell/test_pw/CMakeLists.txt b/source/source_cell/test_pw/CMakeLists.txt index 941a7bb3eeb..7c1d79318e7 100644 --- a/source/source_cell/test_pw/CMakeLists.txt +++ b/source/source_cell/test_pw/CMakeLists.txt @@ -4,6 +4,8 @@ abacus_disable_feature_definitions(__ROCM) abacus_disable_feature_definitions(__EXX) abacus_disable_feature_definitions(__LCAO) +file(COPY support DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +configure_file(unitcell_test_pw_para.sh ${CMAKE_CURRENT_BINARY_DIR}/unitcell_test_pw_para.sh COPYONLY) install(DIRECTORY support DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) install(FILES unitcell_test_pw_para.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/source/source_cell/test_pw/unitcell_test_pw.cpp b/source/source_cell/test_pw/unitcell_test_pw.cpp index 52d52236a73..c710096f9e8 100644 --- a/source/source_cell/test_pw/unitcell_test_pw.cpp +++ b/source/source_cell/test_pw/unitcell_test_pw.cpp @@ -15,11 +15,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } /************************************************ @@ -101,9 +99,6 @@ if(GlobalV::MY_RANK==0) EXPECT_DOUBLE_EQ(ucell->latvec.e11,4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22,4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33,4.27957); - //mandatory preliminaries - delete[] ucell->magnet.start_mag; - ucell->magnet.start_mag = new double[ucell->ntype]; //call read_atom_positions EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell,ifa, ofs_running, ofs_warning)); ofs_running.close(); diff --git a/source/source_cell/unitcell.cpp b/source/source_cell/unitcell.cpp index 0af6fbf6fb8..d921f037554 100644 --- a/source/source_cell/unitcell.cpp +++ b/source/source_cell/unitcell.cpp @@ -14,7 +14,6 @@ #include "source_base/element_elec_config.h" #include "source_base/global_file.h" #include "source_base/parallel_common.h" -#include "source_io/module_parameter/parameter.h" #include "source_cell/sep_cell.h" #ifdef __MPI @@ -188,12 +187,9 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { ModuleBase::TITLE("UnitCell", "setup_cell"); - // (1) init mag assert(ntype > 0); - delete[] magnet.start_mag; - magnet.start_mag = new double[this->ntype]; - // (2) init *Atom class array. + // (1) init *Atom class array. this->atoms = new Atom[this->ntype]; // atom species. this->set_atom_flag = true; @@ -399,7 +395,7 @@ bool UnitCell::if_atoms_can_move() const bool UnitCell::if_cell_can_change() const { // need to be fixed next - if (this->lc[0] || this->lc[1] || this->lc[2]) + if (this->lat_axis_free[0] || this->lat_axis_free[1] || this->lat_axis_free[2]) { return true; } @@ -413,49 +409,50 @@ void UnitCell::setup(const std::string& latname_in, const std::string& fixed_axes_in) { this->latName = latname_in; this->ntype = ntype_in; + this->magnet.start_mag.resize(ntype_in, 0.0); this->lmaxmax = lmaxmax_in; this->init_vel = init_vel_in; // pengfei Li add 2018-11-11 if (fixed_axes_in == "None") { - this->lc[0] = 1; - this->lc[1] = 1; - this->lc[2] = 1; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "volume") { - this->lc[0] = 1; - this->lc[1] = 1; - this->lc[2] = 1; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "shape") { - this->lc[0] = 1; - this->lc[1] = 1; - this->lc[2] = 1; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "a") { - this->lc[0] = 0; - this->lc[1] = 1; - this->lc[2] = 1; + this->lat_axis_free[0] = 0; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "b") { - this->lc[0] = 1; - this->lc[1] = 0; - this->lc[2] = 1; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 0; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "c") { - this->lc[0] = 1; - this->lc[1] = 1; - this->lc[2] = 0; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 0; } else if (fixed_axes_in == "ab") { - this->lc[0] = 0; - this->lc[1] = 0; - this->lc[2] = 1; + this->lat_axis_free[0] = 0; + this->lat_axis_free[1] = 0; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "ac") { - this->lc[0] = 0; - this->lc[1] = 1; - this->lc[2] = 0; + this->lat_axis_free[0] = 0; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 0; } else if (fixed_axes_in == "bc") { - this->lc[0] = 1; - this->lc[1] = 0; - this->lc[2] = 0; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 0; + this->lat_axis_free[2] = 0; } else if (fixed_axes_in == "abc") { - this->lc[0] = 0; - this->lc[1] = 0; - this->lc[2] = 0; + this->lat_axis_free[0] = 0; + this->lat_axis_free[1] = 0; + this->lat_axis_free[2] = 0; } else { ModuleBase::WARNING_QUIT( "Input", diff --git a/source/source_cell/unitcell.h b/source/source_cell/unitcell.h index 3bdb067b7af..ec8f92cc964 100644 --- a/source/source_cell/unitcell.h +++ b/source/source_cell/unitcell.h @@ -58,7 +58,7 @@ class UnitCell : public AtomProvider { double& tpiba = lat.tpiba; double& tpiba2 = lat.tpiba2; double& omega = lat.omega; - int*& lc = lat.lc; + std::vector& lat_axis_free = lat.lat_axis_free; ModuleBase::Matrix3& latvec = lat.latvec; ModuleBase::Vector3&a1 = lat.a1, &a2 = lat.a2, &a3 = lat.a3; @@ -193,11 +193,13 @@ class UnitCell : public AtomProvider { ModuleBase::Matrix3 GGT0; ModuleBase::Matrix3 invGGT0; - // I'm doing a bad thing here! Will change later + // TODO(abacus-team): encapsulate ionic_position_updated and + // cell_parameter_updated with setters that enforce state invariants; + // currently exposed as mutable flags that can be toggled from anywhere. bool ionic_position_updated - = false; // whether the ionic position has been updated + = false; ///< whether the ionic position has been updated bool cell_parameter_updated - = false; // whether the cell parameters are updated + = false; ///< whether the cell parameters are updated //============================================================ // meshx : max number of mesh point in pseudopotential file diff --git a/source/source_cell/unitcell_data.h b/source/source_cell/unitcell_data.h index 522d638fd26..d94d241c729 100644 --- a/source/source_cell/unitcell_data.h +++ b/source/source_cell/unitcell_data.h @@ -1,33 +1,30 @@ #ifndef UNITCELL_DATA_H #define UNITCELL_DATA_H +#include + #include "source_base/intarray.h" #include "source_base/matrix3.h" /// @brief info of lattice struct Lattice { - std::string Coordinate = "Direct"; // "Direct" or "Cartesian" or "Cartesian_angstrom" - std::string latName = "user_defined_lattice"; // Lattice name - double lat0 = 0.0; // Lattice constant(bohr)(a.u.) - double lat0_angstrom = 0.0; // Lattice constant(angstrom) - double tpiba = 0.0; // 2*pi / lat0; - double tpiba2 = 0.0; // tpiba ^ 2 - double omega = 0.0; // the volume of the unit cell - int* lc = new int[3]; // Change the lattice vectors or not - - ModuleBase::Matrix3 latvec = ModuleBase::Matrix3(); // Unitcell lattice vectors - ModuleBase::Vector3 a1, a2, a3; // Same as latvec, just at another form. - ModuleBase::Vector3 latcenter; // (a1+a2+a3)/2 the center of vector - ModuleBase::Matrix3 latvec_supercell = ModuleBase::Matrix3(); // Supercell lattice vectors - ModuleBase::Matrix3 G = ModuleBase::Matrix3(); // reciprocal lattice vector (2pi*inv(R) ) - ModuleBase::Matrix3 GT = ModuleBase::Matrix3(); // traspose of G - ModuleBase::Matrix3 GGT = ModuleBase::Matrix3(); // GGT = G*GT - ModuleBase::Matrix3 invGGT = ModuleBase::Matrix3(); // inverse G + std::string Coordinate = "Direct"; ///< "Direct" or "Cartesian" or "Cartesian_angstrom" + std::string latName = "user_defined_lattice"; ///< Lattice name + double lat0 = 0.0; ///< Lattice constant(bohr)(a.u.) + double lat0_angstrom = 0.0; ///< Lattice constant(angstrom) + double tpiba = 0.0; ///< 2*pi / lat0; + double tpiba2 = 0.0; ///< tpiba ^ 2 + double omega = 0.0; ///< the volume of the unit cell + std::vector lat_axis_free{0, 0, 0}; ///< whether each lattice axis (a,b,c) is allowed to relax (0=fixed, 1=free) - ~Lattice() - { - delete[] lc; - } + ModuleBase::Matrix3 latvec = ModuleBase::Matrix3(); ///< Unitcell lattice vectors + ModuleBase::Vector3 a1, a2, a3; ///< Same as latvec, just at another form. + ModuleBase::Vector3 latcenter; ///< (a1+a2+a3)/2 the center of vector + ModuleBase::Matrix3 latvec_supercell = ModuleBase::Matrix3(); ///< Supercell lattice vectors + ModuleBase::Matrix3 G = ModuleBase::Matrix3(); ///< reciprocal lattice vector (2pi*inv(R) ) + ModuleBase::Matrix3 GT = ModuleBase::Matrix3(); ///< traspose of G + ModuleBase::Matrix3 GGT = ModuleBase::Matrix3(); ///< GGT = G*GT + ModuleBase::Matrix3 invGGT = ModuleBase::Matrix3(); ///< inverse G }; //======================================================== diff --git a/source/source_esolver/esolver_ks.cpp b/source/source_esolver/esolver_ks.cpp index 286b08f8454..c4c3665da64 100644 --- a/source/source_esolver/esolver_ks.cpp +++ b/source/source_esolver/esolver_ks.cpp @@ -221,6 +221,7 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& iter, bool & // 2.1) compute magnetization, only for spin==2 ucell.magnet.compute_mag(ucell.omega, this->chr.nrxx, this->chr.nxyz, this->chr.rho, + PARAM.inp.nspin, PARAM.globalv.two_fermi, PARAM.inp.nelec, this->pelec->nelec_spin.data()); // 2.2) charge mixing diff --git a/source/source_estate/module_dm/test/prepare_unitcell.h b/source/source_estate/module_dm/test/prepare_unitcell.h index 0cbbd28905c..ae8a582f00c 100644 --- a/source/source_estate/module_dm/test/prepare_unitcell.h +++ b/source/source_estate/module_dm/test/prepare_unitcell.h @@ -74,14 +74,11 @@ class UcellTestPrepare static UnitCell ucell; ucell.setup(this->latname, this->ntype, this->lmaxmax, this->init_vel, this->fixed_axes); - delete[] ucell.magnet.start_mag; // mag set here - ucell.atom_label.resize(ucell.ntype); ucell.atom_mass.resize(ucell.ntype); ucell.pseudo_fn.resize(ucell.ntype); ucell.pseudo_type.resize(ucell.ntype); ucell.orbital_fn.resize(ucell.ntype); - ucell.magnet.start_mag = 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; @@ -92,7 +89,6 @@ class UcellTestPrepare 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_mag[it] = 0.0; // mag set here } // lattice info ucell.lat0 = this->lat0; diff --git a/source/source_estate/module_dm/test/test_dm_io.cpp b/source/source_estate/module_dm/test/test_dm_io.cpp index 380595429e1..89bbc2bd262 100644 --- a/source/source_estate/module_dm/test/test_dm_io.cpp +++ b/source/source_estate/module_dm/test/test_dm_io.cpp @@ -25,11 +25,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } #include "source_cell/klist.h" diff --git a/source/source_estate/test/CMakeLists.txt b/source/source_estate/test/CMakeLists.txt index aa4a8825d45..b9118b01a55 100644 --- a/source/source_estate/test/CMakeLists.txt +++ b/source/source_estate/test/CMakeLists.txt @@ -23,12 +23,6 @@ AddTest( SOURCES elecstate_occupy_test.cpp ../occupy.cpp ) -AddTest( - TARGET MODULE_ESTATE_elecstate_magnetism - LIBS parameter base device - SOURCES elecstate_magnetism_test.cpp ../../source_cell/magnetism.cpp -) - AddTest( TARGET MODULE_ESTATE_elecstate_fp_energy LIBS parameter base device diff --git a/source/source_estate/test/charge_extra_test.cpp b/source/source_estate/test/charge_extra_test.cpp index c2951c1b5a9..1b2c8505b8b 100644 --- a/source/source_estate/test/charge_extra_test.cpp +++ b/source/source_estate/test/charge_extra_test.cpp @@ -19,11 +19,9 @@ InfoNonlocal::~InfoNonlocal() #endif Magnetism::Magnetism() { - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } Parallel_Grid::~Parallel_Grid(){}; diff --git a/source/source_estate/test/charge_test.cpp b/source/source_estate/test/charge_test.cpp index 261bcc5e3e7..c85c6f46f0e 100644 --- a/source/source_estate/test/charge_test.cpp +++ b/source/source_estate/test/charge_test.cpp @@ -21,11 +21,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } // mock functions for Charge diff --git a/source/source_estate/test/prepare_unitcell.h b/source/source_estate/test/prepare_unitcell.h index eec2fa9a521..5d0d19e0d0e 100644 --- a/source/source_estate/test/prepare_unitcell.h +++ b/source/source_estate/test/prepare_unitcell.h @@ -60,14 +60,11 @@ class UcellTestPrepare this->lmaxmax, this->init_vel, this->fixed_axes); - delete[] ucell->magnet.start_mag; //mag set here - ucell->atom_label.resize(ucell->ntype); ucell->atom_mass.resize(ucell->ntype); ucell->pseudo_fn.resize(ucell->ntype); ucell->pseudo_type.resize(ucell->ntype); ucell->orbital_fn.resize(ucell->ntype); - ucell->magnet.start_mag = 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; @@ -78,7 +75,6 @@ class UcellTestPrepare 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_mag[it] = 0.0; //mag set here } //lattice info ucell->lat0 = this->lat0; diff --git a/source/source_io/module_dm/test/write_dmk_test.cpp b/source/source_io/module_dm/test/write_dmk_test.cpp index e0edb2cb41d..3831181a835 100644 --- a/source/source_io/module_dm/test/write_dmk_test.cpp +++ b/source/source_io/module_dm/test/write_dmk_test.cpp @@ -23,9 +23,8 @@ LCAO_Orbitals::~LCAO_Orbitals() {} Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } -Magnetism::~Magnetism() { delete[] this->start_mag; } +Magnetism::~Magnetism() { } /************************************************ * unit test of read_dmk and write_dmk diff --git a/source/source_io/module_json/test/para_json_test.cpp b/source/source_io/module_json/test/para_json_test.cpp index 3667c529f28..86511f8707a 100644 --- a/source/source_io/module_json/test/para_json_test.cpp +++ b/source/source_io/module_json/test/para_json_test.cpp @@ -257,11 +257,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } TEST(AbacusJsonTest, InitInfo) { diff --git a/source/source_io/test/for_testing_input_conv.h b/source/source_io/test/for_testing_input_conv.h index 56b55f407c3..bde70ab38f5 100644 --- a/source/source_io/test/for_testing_input_conv.h +++ b/source/source_io/test/for_testing_input_conv.h @@ -168,49 +168,49 @@ void UnitCell::setup(const std::string& latname_in, this->init_vel = init_vel_in; // pengfei Li add 2018-11-11 if (fixed_axes_in == "None") { - this->lc[0] = 1; - this->lc[1] = 1; - this->lc[2] = 1; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "volume") { - this->lc[0] = 1; - this->lc[1] = 1; - this->lc[2] = 1; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 1; // Note: fixed_axes="volume" is now supported with relax_new=false // (see commit cdc3457f5a8546cda869655c3faabd8b29687aff) } else if (fixed_axes_in == "shape") { // Note: fixed_axes="shape" is now supported with relax_new=false // (see commit cdc3457f5a8546cda869655c3faabd8b29687aff) - this->lc[0] = 1; - this->lc[1] = 1; - this->lc[2] = 1; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "a") { - this->lc[0] = 0; - this->lc[1] = 1; - this->lc[2] = 1; + this->lat_axis_free[0] = 0; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "b") { - this->lc[0] = 1; - this->lc[1] = 0; - this->lc[2] = 1; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 0; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "c") { - this->lc[0] = 1; - this->lc[1] = 1; - this->lc[2] = 0; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 0; } else if (fixed_axes_in == "ab") { - this->lc[0] = 0; - this->lc[1] = 0; - this->lc[2] = 1; + this->lat_axis_free[0] = 0; + this->lat_axis_free[1] = 0; + this->lat_axis_free[2] = 1; } else if (fixed_axes_in == "ac") { - this->lc[0] = 0; - this->lc[1] = 1; - this->lc[2] = 0; + this->lat_axis_free[0] = 0; + this->lat_axis_free[1] = 1; + this->lat_axis_free[2] = 0; } else if (fixed_axes_in == "bc") { - this->lc[0] = 1; - this->lc[1] = 0; - this->lc[2] = 0; + this->lat_axis_free[0] = 1; + this->lat_axis_free[1] = 0; + this->lat_axis_free[2] = 0; } else if (fixed_axes_in == "abc") { - this->lc[0] = 0; - this->lc[1] = 0; - this->lc[2] = 0; + this->lat_axis_free[0] = 0; + this->lat_axis_free[1] = 0; + this->lat_axis_free[2] = 0; } else { ModuleBase::WARNING_QUIT( "Input", diff --git a/source/source_io/test/prepare_unitcell.h b/source/source_io/test/prepare_unitcell.h index af3cffb383d..152a018a021 100644 --- a/source/source_io/test/prepare_unitcell.h +++ b/source/source_io/test/prepare_unitcell.h @@ -76,14 +76,12 @@ class UcellTestPrepare this->lmaxmax, this->init_vel, this->fixed_axes); - delete[] ucell->magnet.start_mag; //mag set here ucell->atom_label.resize(ucell->ntype); ucell->atom_mass.resize(ucell->ntype); ucell->pseudo_fn.resize(ucell->ntype); ucell->pseudo_type.resize(ucell->ntype); ucell->orbital_fn.resize(ucell->ntype); - ucell->magnet.start_mag = 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; @@ -94,7 +92,6 @@ class UcellTestPrepare 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_mag[it] = 0.0; //mag set here } //lattice info ucell->lat0 = this->lat0; diff --git a/source/source_io/test/write_orb_info_test.cpp b/source/source_io/test/write_orb_info_test.cpp index c56c772c2f3..508a2b8a7a5 100644 --- a/source/source_io/test/write_orb_info_test.cpp +++ b/source/source_io/test/write_orb_info_test.cpp @@ -18,11 +18,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } /************************************************ diff --git a/source/source_io/test_serial/prepare_unitcell.h b/source/source_io/test_serial/prepare_unitcell.h index 7e73e718928..476fb8af7f3 100644 --- a/source/source_io/test_serial/prepare_unitcell.h +++ b/source/source_io/test_serial/prepare_unitcell.h @@ -76,14 +76,12 @@ class UcellTestPrepare this->lmaxmax, this->init_vel, this->fixed_axes); - delete[] ucell->magnet.start_mag; //mag set here ucell->atom_label.resize(ucell->ntype); ucell->atom_mass.resize(ucell->ntype); ucell->pseudo_fn.resize(ucell->ntype); ucell->pseudo_type.resize(ucell->ntype); ucell->orbital_fn.resize(ucell->ntype); - ucell->magnet.start_mag = 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; @@ -94,7 +92,6 @@ class UcellTestPrepare 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_mag[it] = 0.0; //mag set here } //lattice info ucell->lat0 = this->lat0; diff --git a/source/source_io/test_serial/rho_io_test.cpp b/source/source_io/test_serial/rho_io_test.cpp index 20f5a843e68..171cbb43cf7 100644 --- a/source/source_io/test_serial/rho_io_test.cpp +++ b/source/source_io/test_serial/rho_io_test.cpp @@ -27,13 +27,11 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } Parallel_Grid::~Parallel_Grid() {} diff --git a/source/source_lcao/module_deepks/test/deepks_test_prep.cpp b/source/source_lcao/module_deepks/test/deepks_test_prep.cpp index e606475c854..46357f13b21 100644 --- a/source/source_lcao/module_deepks/test/deepks_test_prep.cpp +++ b/source/source_lcao/module_deepks/test/deepks_test_prep.cpp @@ -23,11 +23,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } namespace GlobalC { diff --git a/source/source_lcao/module_hcontainer/test/prepare_unitcell.h b/source/source_lcao/module_hcontainer/test/prepare_unitcell.h index 3df427d83b2..8708c6a41e9 100644 --- a/source/source_lcao/module_hcontainer/test/prepare_unitcell.h +++ b/source/source_lcao/module_hcontainer/test/prepare_unitcell.h @@ -72,14 +72,12 @@ class UcellTestPrepare this->ntype = this->elements.size(); static UnitCell ucell; ucell.setup(this->latname, this->ntype, this->lmaxmax, this->init_vel, this->fixed_axes); - delete[] ucell.magnet.start_mag; // mag set here ucell.atom_label.resize(ucell.ntype); ucell.atom_mass.resize(ucell.ntype); ucell.pseudo_fn.resize(ucell.ntype); ucell.pseudo_type.resize(ucell.ntype); ucell.orbital_fn.resize(ucell.ntype); - ucell.magnet.start_mag = 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; @@ -90,7 +88,6 @@ class UcellTestPrepare 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_mag[it] = 0.0; // mag set here } // lattice info ucell.lat0 = this->lat0; diff --git a/source/source_lcao/module_hcontainer/test/test_hcontainer_readCSR.cpp b/source/source_lcao/module_hcontainer/test/test_hcontainer_readCSR.cpp index 3ce249e0254..4599716b67e 100644 --- a/source/source_lcao/module_hcontainer/test/test_hcontainer_readCSR.cpp +++ b/source/source_lcao/module_hcontainer/test/test_hcontainer_readCSR.cpp @@ -26,11 +26,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } // mocke functions diff --git a/source/source_lcao/module_rt/test/snap_psibeta_half_tddft_test.cpp b/source/source_lcao/module_rt/test/snap_psibeta_half_tddft_test.cpp index f8b2f8d48f4..a85283c5498 100644 --- a/source/source_lcao/module_rt/test/snap_psibeta_half_tddft_test.cpp +++ b/source/source_lcao/module_rt/test/snap_psibeta_half_tddft_test.cpp @@ -35,7 +35,6 @@ Magnetism::Magnetism() Magnetism::~Magnetism() { - delete[] start_mag; } UnitCell::UnitCell() diff --git a/source/source_lcao/wavefunc_in_pw.cpp b/source/source_lcao/wavefunc_in_pw.cpp index ce6ea0544e5..1ff08b2e5e7 100644 --- a/source/source_lcao/wavefunc_in_pw.cpp +++ b/source/source_lcao/wavefunc_in_pw.cpp @@ -360,8 +360,6 @@ void Wavefunc_in_pw::produce_local_basis_in_pw(const UnitCell& ucell, } } //and construct the starting wavefunctions as in the noncollinear case. - //alpha = ucell.magnet.angle1_[it]; - //gamma = -1 * ucell.magnet.angle2_[it] + 0.5 * ModuleBase::PI; alpha = ucell.atoms[it].angle1[ia]; gamma = -1 * ucell.atoms[it].angle2[ia] + 0.5 * ModuleBase::PI; for(int m = 0;m<2*L+1;m++) @@ -403,8 +401,6 @@ void Wavefunc_in_pw::produce_local_basis_in_pw(const UnitCell& ucell, {//atomic_wfc_nc double alpha = 0.0, gamman = 0.0; std::complex fup = 0.0, fdown = 0.0; - //alpha = ucell.magnet.angle1_[it]; - //gamman = -ucell.magnet.angle2_[it] + 0.5*ModuleBase::PI; alpha = ucell.atoms[it].angle1[ia]; gamman = -ucell.atoms[it].angle2[ia] + 0.5*ModuleBase::PI; for(int m = 0;m<2*L+1;m++) diff --git a/source/source_md/test/setcell.h b/source/source_md/test/setcell.h index 3841b8501b9..b36151ee763 100644 --- a/source/source_md/test/setcell.h +++ b/source/source_md/test/setcell.h @@ -13,11 +13,9 @@ Magnetism::Magnetism() { this->tot_mag = 0.0; this->abs_mag = 0.0; - this->start_mag = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_mag; } class Setcell diff --git a/source/source_psi/psi_init_atomic.cpp b/source/source_psi/psi_init_atomic.cpp index 7b2d8ed84d6..fa110f970a6 100644 --- a/source/source_psi/psi_init_atomic.cpp +++ b/source/source_psi/psi_init_atomic.cpp @@ -418,8 +418,6 @@ void psi_init_atomic::init_psig(T* psig, const int& ik) double alpha=0.0; double gamman=0.0; std::complex fup, fdown; - //alpha = this->p_ucell_->magnet.angle1_[it]; - //gamman = -this->p_ucell_->magnet.angle2_[it] + 0.5*ModuleBase::PI; alpha = this->p_ucell_->atoms[it].angle1[ia]; gamman = -1 * this->p_ucell_->atoms[it].angle2[ia] + 0.5 * ModuleBase::PI; for(int m = 0; m < 2*l+1; m++) diff --git a/source/source_relax/lattice_change_basic.cpp b/source/source_relax/lattice_change_basic.cpp index fdfb88485e5..ee18a1aa796 100644 --- a/source/source_relax/lattice_change_basic.cpp +++ b/source/source_relax/lattice_change_basic.cpp @@ -36,7 +36,7 @@ void Lattice_Change_Basic::setup_gradient(const UnitCell &ucell, double *lat, do stress(1, 1) = stress(1, 1) - stress_aver; stress(2, 2) = stress(2, 2) - stress_aver; } - // Note: Axis constraints ("a", "b", "c", etc.) are handled via ucell.lc[] flags below + // Note: Axis constraints ("a", "b", "c", etc.) are handled via ucell.lat_axis_free[] flags below lat[0] = ucell.latvec.e11 * ucell.lat0; lat[1] = ucell.latvec.e12 * ucell.lat0; @@ -49,7 +49,7 @@ void Lattice_Change_Basic::setup_gradient(const UnitCell &ucell, double *lat, do lat[8] = ucell.latvec.e33 * ucell.lat0; // Calculate gradients for each lattice vector, or zero them if fixed - if (ucell.lc[0] == 1) + if (ucell.lat_axis_free[0] == 1) { grad[0] = -(lat[0] * stress(0, 0) + lat[1] * stress(1, 0) + lat[2] * stress(2, 0)); grad[1] = -(lat[0] * stress(0, 1) + lat[1] * stress(1, 1) + lat[2] * stress(2, 1)); @@ -63,7 +63,7 @@ void Lattice_Change_Basic::setup_gradient(const UnitCell &ucell, double *lat, do grad[2] = 0.0; } - if (ucell.lc[1] == 1) + if (ucell.lat_axis_free[1] == 1) { grad[3] = -(lat[3] * stress(0, 0) + lat[4] * stress(1, 0) + lat[5] * stress(2, 0)); grad[4] = -(lat[3] * stress(0, 1) + lat[4] * stress(1, 1) + lat[5] * stress(2, 1)); @@ -77,7 +77,7 @@ void Lattice_Change_Basic::setup_gradient(const UnitCell &ucell, double *lat, do grad[5] = 0.0; } - if (ucell.lc[2] == 1) + if (ucell.lat_axis_free[2] == 1) { grad[6] = -(lat[6] * stress(0, 0) + lat[7] * stress(1, 0) + lat[8] * stress(2, 0)); grad[7] = -(lat[6] * stress(0, 1) + lat[7] * stress(1, 1) + lat[8] * stress(2, 1)); @@ -128,19 +128,19 @@ void Lattice_Change_Basic::change_lattice(UnitCell &ucell, double *move, double } } - if (ucell.lc[0] != 0) + if (ucell.lat_axis_free[0] != 0) { ucell.latvec.e11 = (move[0] + lat[0]) / ucell.lat0; ucell.latvec.e12 = (move[1] + lat[1]) / ucell.lat0; ucell.latvec.e13 = (move[2] + lat[2]) / ucell.lat0; } - if (ucell.lc[1] != 0) + if (ucell.lat_axis_free[1] != 0) { ucell.latvec.e21 = (move[3] + lat[3]) / ucell.lat0; ucell.latvec.e22 = (move[4] + lat[4]) / ucell.lat0; ucell.latvec.e23 = (move[5] + lat[5]) / ucell.lat0; } - if (ucell.lc[2] != 0) + if (ucell.lat_axis_free[2] != 0) { ucell.latvec.e31 = (move[6] + lat[6]) / ucell.lat0; ucell.latvec.e32 = (move[7] + lat[7]) / ucell.lat0; @@ -238,7 +238,7 @@ bool Lattice_Change_Basic::check_converged(const UnitCell &ucell, ModuleBase::ma Lattice_Change_Basic::largest_grad = 0.0; double stress_ii_max = 0.0; - if (ucell.lc[0] == 1 && ucell.lc[1] == 1 && ucell.lc[2] == 1) + if (ucell.lat_axis_free[0] == 1 && ucell.lat_axis_free[1] == 1 && ucell.lat_axis_free[2] == 1) { for (int i = 0; i < 3; i++) { @@ -276,7 +276,7 @@ bool Lattice_Change_Basic::check_converged(const UnitCell &ucell, ModuleBase::ma ofs << " Largest stress is 0, movement is impossible." << std::endl; return true; } - else if (ucell.lc[0] == 1 && ucell.lc[1] == 1 && ucell.lc[2] == 1) + else if (ucell.lat_axis_free[0] == 1 && ucell.lat_axis_free[1] == 1 && ucell.lat_axis_free[2] == 1) { if (Lattice_Change_Basic::largest_grad < PARAM.inp.stress_thr && stress_ii_max < PARAM.inp.stress_thr) { diff --git a/source/source_relax/relax_sync.cpp b/source/source_relax/relax_sync.cpp index 9d6bfa0f97d..2e18ea7f012 100644 --- a/source/source_relax/relax_sync.cpp +++ b/source/source_relax/relax_sync.cpp @@ -196,19 +196,19 @@ bool Relax::setup_gradient(const UnitCell& ucell, const ModuleBase::matrix& forc // So we need to first convert to Cartesian and then apply the constraint ModuleBase::matrix stress_cart = ucell.latvec.to_matrix() * stress_ev; - if (ucell.lc[0] == 0) + if (ucell.lat_axis_free[0] == 0) { stress_cart(0, 0) = 0; stress_cart(0, 1) = 0; stress_cart(0, 2) = 0; } - if (ucell.lc[1] == 0) + if (ucell.lat_axis_free[1] == 0) { stress_cart(1, 0) = 0; stress_cart(1, 1) = 0; stress_cart(1, 2) = 0; } - if (ucell.lc[2] == 0) + if (ucell.lat_axis_free[2] == 0) { stress_cart(2, 0) = 0; stress_cart(2, 1) = 0; @@ -539,19 +539,19 @@ void Relax::move_cell_ions(UnitCell& ucell, const bool is_new_dir, std::ofstream ModuleBase::Matrix3 move_cell = latvec_save * sr_dr_cell; // should be close to 0, but set again to avoid numerical issues - if (ucell.lc[0] == 0) + if (ucell.lat_axis_free[0] == 0) { move_cell.e11 = 0; move_cell.e12 = 0; move_cell.e13 = 0; } - if (ucell.lc[1] == 0) + if (ucell.lat_axis_free[1] == 0) { move_cell.e21 = 0; move_cell.e22 = 0; move_cell.e23 = 0; } - if (ucell.lc[2] == 0) + if (ucell.lat_axis_free[2] == 0) { move_cell.e31 = 0; move_cell.e32 = 0; diff --git a/source/source_relax/test/for_test.h b/source/source_relax/test/for_test.h index 5696476d491..3cdd6af5735 100644 --- a/source/source_relax/test/for_test.h +++ b/source/source_relax/test/for_test.h @@ -30,7 +30,6 @@ UnitCell::UnitCell() iwt2iw = nullptr; itia2iat.create(1, 1); - lc = new int[3]; latvec = ModuleBase::Matrix3(); latvec_supercell = ModuleBase::Matrix3(); diff --git a/source/source_relax/test/lattice_change_basic_test.cpp b/source/source_relax/test/lattice_change_basic_test.cpp index a75a7662a7a..1e22845e5fa 100644 --- a/source/source_relax/test/lattice_change_basic_test.cpp +++ b/source/source_relax/test/lattice_change_basic_test.cpp @@ -44,9 +44,9 @@ class LatticeChangeBasicTest : public ::testing::Test TEST_F(LatticeChangeBasicTest, SetupGradientVolume) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; stress(0, 0) = 1.0; stress(0, 1) = 2.0; stress(0, 2) = 3.0; @@ -94,9 +94,9 @@ TEST_F(LatticeChangeBasicTest, SetupGradientVolume) TEST_F(LatticeChangeBasicTest, SetupGradientNone) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; stress(0, 0) = 1.0; stress(0, 1) = 2.0; stress(0, 2) = 3.0; @@ -127,9 +127,9 @@ TEST_F(LatticeChangeBasicTest, SetupGradientNone) TEST_F(LatticeChangeBasicTest, ChangeLattice) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; lat[0] = 1.0; lat[1] = 0.0; lat[2] = 0.0; @@ -217,16 +217,16 @@ TEST_F(LatticeChangeBasicTest, ChangeLattice) EXPECT_DOUBLE_EQ(ucell.invGGT.e33, 0.36); } -// Test for check_converged when ucell.lc[0] == 1 && ucell.lc[1] == 1 && ucell.lc[2] == 1, but not converged +// Test for check_converged when ucell.lat_axis_free[0] == 1 && ucell.lat_axis_free[1] == 1 && ucell.lat_axis_free[2] == 1, but not converged TEST_F(LatticeChangeBasicTest, CheckConvergedCase1) { // Set up test data Lattice_Change_Basic::update_iter = 0; PARAM.input.stress_thr = 10.0; std::ofstream ofs("test_check_converged_case1.log"); - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; stress(0, 0) = 1.0; stress(0, 1) = 2.0; stress(0, 2) = 3.0; @@ -254,16 +254,16 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase1) std::remove("test_check_converged_case1.log"); } -// Test for check_converged when ucell.lc[0] == 1 && ucell.lc[1] == 1 && ucell.lc[2] == 1 && largest_grad == 0 +// Test for check_converged when ucell.lat_axis_free[0] == 1 && ucell.lat_axis_free[1] == 1 && ucell.lat_axis_free[2] == 1 && largest_grad == 0 TEST_F(LatticeChangeBasicTest, CheckConvergedCase2) { // Set up test data Lattice_Change_Basic::update_iter = 0; PARAM.input.stress_thr = 10.0; std::ofstream ofs("test_check_converged_case2.log"); - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; stress(0, 0) = 0.0; stress(0, 1) = 0.0; stress(0, 2) = 0.0; @@ -291,16 +291,16 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase2) std::remove("test_check_converged_case2.log"); } -// Test for check_converged when ucell.lc[0] == 1 && ucell.lc[1] == 1 && ucell.lc[2] == 1, and converged +// Test for check_converged when ucell.lat_axis_free[0] == 1 && ucell.lat_axis_free[1] == 1 && ucell.lat_axis_free[2] == 1, and converged TEST_F(LatticeChangeBasicTest, CheckConvergedCase3) { // Set up test data Lattice_Change_Basic::update_iter = 0; PARAM.input.stress_thr = 10.0; std::ofstream ofs("test_check_converged_case3.log"); - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; stress(0, 0) = 0.000001; stress(0, 1) = 0.0; stress(0, 2) = 0.0; @@ -328,16 +328,16 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase3) std::remove("test_check_converged_case3.log"); } -// Test for check_converged when ucell.lc != 1, but not converged +// Test for check_converged when ucell.lat_axis_free != 1, but not converged TEST_F(LatticeChangeBasicTest, CheckConvergedCase4) { // Set up test data Lattice_Change_Basic::update_iter = 0; PARAM.input.stress_thr = 10.0; std::ofstream ofs("test_check_converged_case4.log"); - ucell.lc[0] = 0; - ucell.lc[1] = 0; - ucell.lc[2] = 0; + ucell.lat_axis_free[0] = 0; + ucell.lat_axis_free[1] = 0; + ucell.lat_axis_free[2] = 0; grad[0] = 1.0; grad[1] = 1.0; grad[2] = 1.0; @@ -365,16 +365,16 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase4) std::remove("test_check_converged_case4.log"); } -// Test for check_converged when ucell.lc != 1, and largest_grad == 0 +// Test for check_converged when ucell.lat_axis_free != 1, and largest_grad == 0 TEST_F(LatticeChangeBasicTest, CheckConvergedCase5) { // Set up test data Lattice_Change_Basic::update_iter = 0; PARAM.input.stress_thr = 10.0; std::ofstream ofs("test_check_converged_case5.log"); - ucell.lc[0] = 0; - ucell.lc[1] = 0; - ucell.lc[2] = 0; + ucell.lat_axis_free[0] = 0; + ucell.lat_axis_free[1] = 0; + ucell.lat_axis_free[2] = 0; grad[0] = 0.0; grad[1] = 0.0; grad[2] = 0.0; @@ -402,16 +402,16 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase5) std::remove("test_check_converged_case5.log"); } -// Test for check_converged when ucell.lc != 1, and converged +// Test for check_converged when ucell.lat_axis_free != 1, and converged TEST_F(LatticeChangeBasicTest, CheckConvergedCase6) { // Set up test data Lattice_Change_Basic::update_iter = 0; PARAM.input.stress_thr = 10.0; std::ofstream ofs("test_check_converged_case6.log"); - ucell.lc[0] = 0; - ucell.lc[1] = 0; - ucell.lc[2] = 0; + ucell.lat_axis_free[0] = 0; + ucell.lat_axis_free[1] = 0; + ucell.lat_axis_free[2] = 0; grad[0] = 0.000001; grad[1] = 0.0; grad[2] = 0.0; @@ -497,9 +497,9 @@ TEST_F(LatticeChangeBasicTest, SetupEtot) TEST_F(LatticeChangeBasicTest, SetupGradientShape) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; // Non-isotropic stress tensor stress(0, 0) = 1.0; @@ -536,9 +536,9 @@ TEST_F(LatticeChangeBasicTest, SetupGradientShape) TEST_F(LatticeChangeBasicTest, ChangeLatticeVolumeRescaling) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.lat0 = 10.0; // Set initial lattice (cubic, volume = 1000) @@ -594,9 +594,9 @@ TEST_F(LatticeChangeBasicTest, ChangeLatticeVolumeRescaling) TEST_F(LatticeChangeBasicTest, ChangeLatticeVolumeRescalingNonCubic) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.lat0 = 10.0; // Set initial lattice (non-cubic, volume = 1200) @@ -646,9 +646,9 @@ TEST_F(LatticeChangeBasicTest, ChangeLatticeVolumeRescalingNonCubic) TEST_F(LatticeChangeBasicTest, ChangeLatticeNoVolumeConstraint) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.lat0 = 10.0; // Set initial lattice @@ -703,9 +703,9 @@ TEST_F(LatticeChangeBasicTest, ChangeLatticeNoVolumeConstraint) TEST_F(LatticeChangeBasicTest, ChangeLatticeFixedIbravSimpleCubic) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.lat0 = 10.0; ucell.latName = "sc"; @@ -772,9 +772,9 @@ TEST_F(LatticeChangeBasicTest, ChangeLatticeFixedIbravSimpleCubic) TEST_F(LatticeChangeBasicTest, ChangeLatticeFixedIbravFCC) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.lat0 = 10.0; ucell.latName = "fcc"; @@ -844,9 +844,9 @@ TEST_F(LatticeChangeBasicTest, ChangeLatticeFixedIbravFCC) TEST_F(LatticeChangeBasicTest, ChangeLatticeVolumeAndIbrav) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.lat0 = 10.0; ucell.latName = "sc"; @@ -917,9 +917,9 @@ TEST_F(LatticeChangeBasicTest, ChangeLatticeVolumeAndIbrav) TEST_F(LatticeChangeBasicTest, SetupGradientAxisA) { // Initialize variables - ucell.lc[0] = 0; // First axis fixed - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 0; // First axis fixed + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; stress(0, 0) = 1.0; stress(0, 1) = 2.0; @@ -954,9 +954,9 @@ TEST_F(LatticeChangeBasicTest, SetupGradientAxisA) TEST_F(LatticeChangeBasicTest, ChangeLatticeFixedAxisA) { // Initialize variables - ucell.lc[0] = 0; // First axis fixed - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 0; // First axis fixed + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.lat0 = 10.0; // Set initial lattice @@ -1016,9 +1016,9 @@ TEST_F(LatticeChangeBasicTest, ChangeLatticeFixedAxisA) TEST_F(LatticeChangeBasicTest, ChangeLatticeNoFixedIbrav) { // Initialize variables - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.lat0 = 10.0; ucell.latName = "sc"; diff --git a/source/source_relax/test/lattice_change_cg_test.cpp b/source/source_relax/test/lattice_change_cg_test.cpp index 084dcc75099..bf82d6f4014 100644 --- a/source/source_relax/test/lattice_change_cg_test.cpp +++ b/source/source_relax/test/lattice_change_cg_test.cpp @@ -70,9 +70,9 @@ TEST_F(LatticeChangeCGTest, TestStartConverged) { // setup data UnitCell ucell; - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ModuleBase::matrix stress(3, 3); double etot = 0.0; @@ -98,9 +98,9 @@ TEST_F(LatticeChangeCGTest, TestStartSd) { // setup data UnitCell ucell; - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ModuleBase::matrix stress(3, 3); stress(0, 0) = 0.01; double etot = 0.0; @@ -126,9 +126,9 @@ TEST_F(LatticeChangeCGTest, TestStartTrialGoto) { // setup data UnitCell ucell; - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ModuleBase::matrix stress(3, 3); stress(0, 1) = 0.01; double etot = 0.0; @@ -161,9 +161,9 @@ TEST_F(LatticeChangeCGTest, TestStartTrial) { // setup data UnitCell ucell; - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ModuleBase::matrix stress(3, 3); stress(0, 1) = 0.01; double etot = 0.0; @@ -194,9 +194,9 @@ TEST_F(LatticeChangeCGTest, TestStartNoTrialGotoCase1) { // setup data UnitCell ucell; - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ModuleBase::matrix stress(3, 3); stress(0, 1) = 0.01; double etot = 0.0; @@ -231,9 +231,9 @@ TEST_F(LatticeChangeCGTest, TestStartNoTrialGotoCase2) { // setup data UnitCell ucell; - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ModuleBase::matrix stress(3, 3); stress(0, 1) = 0.01; double etot = 0.0; @@ -271,9 +271,9 @@ TEST_F(LatticeChangeCGTest, TestStartNoTrial) { // setup data UnitCell ucell; - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ModuleBase::matrix stress(3, 3); stress(0, 1) = 0.01; double etot = 0.0; diff --git a/source/source_relax/test/relax_test.cpp b/source/source_relax/test/relax_test.cpp index ed8689d1a63..d7b8f703eac 100644 --- a/source/source_relax/test/relax_test.cpp +++ b/source/source_relax/test/relax_test.cpp @@ -52,7 +52,6 @@ class Test_SETGRAD : public testing::Test ucell.atoms[0].dis.resize(nat); ucell.atoms[0].mag.resize(nat); ucell.atoms[0].vel.resize(nat); - ucell.lc = new int[3]; ucell.iat2it[0] = 0; ucell.iat2it[1] = 0; @@ -80,9 +79,9 @@ class Test_SETGRAD : public testing::Test ucell.atoms[0].tau.resize(nat); - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; rl.init_relax(nat); rl.relax_step(ucell,force_in,stress_in,0.0, ofs); @@ -114,9 +113,9 @@ class Test_SETGRAD : public testing::Test input.fixed_axes = "a"; //anything other than "None" input.fixed_ibrav = true; ucell.latName = "sc"; - ucell.lc[0] = 0; - ucell.lc[1] = 0; - ucell.lc[2] = 0; + ucell.lat_axis_free[0] = 0; + ucell.lat_axis_free[1] = 0; + ucell.lat_axis_free[2] = 0; rl.init_relax(nat); rl.relax_step(ucell,force_in,stress_in,0.0, ofs); @@ -290,10 +289,9 @@ class Test_RELAX : public testing::Test ucell.atoms[2].taud[1] = {0.5,0 ,0.479348999999274 }; ucell.atoms[2].taud[2] = {0 ,0 ,0.958854000000429 }; - ucell.lc = new int[3]; - ucell.lc[0] = 1; - ucell.lc[1] = 1; - ucell.lc[2] = 1; + ucell.lat_axis_free[0] = 1; + ucell.lat_axis_free[1] = 1; + ucell.lat_axis_free[2] = 1; ucell.latvec.e11 = 3.96; ucell.latvec.e12 = 0; diff --git a/tools/03_code_analysis/agent_governance_check.py b/tools/03_code_analysis/agent_governance_check.py index 19488c16cd1..6dc9b87f22b 100644 --- a/tools/03_code_analysis/agent_governance_check.py +++ b/tools/03_code_analysis/agent_governance_check.py @@ -296,9 +296,66 @@ def check_global_dependencies( ) +def _has_default_arg_in_parens(stripped: str) -> bool: + paren_depth = 0 + in_string = False + string_char = "" + in_line_comment = False + in_block_comment = False + i = 0 + while i < len(stripped): + c = stripped[i] + nxt = stripped[i + 1] if i + 1 < len(stripped) else "" + + if in_line_comment: + break + + if in_block_comment: + if c == "*" and nxt == "/": + in_block_comment = False + i += 2 + continue + i += 1 + continue + + if not in_string: + if c == "/" and nxt == "/": + in_line_comment = True + i += 2 + continue + if c == "/" and nxt == "*": + in_block_comment = True + i += 2 + continue + + if in_string: + if c == "\\": + i += 2 + continue + if c == string_char: + in_string = False + i += 1 + continue + if c in ('"', "'"): + in_string = True + string_char = c + i += 1 + continue + if c == "(": + paren_depth += 1 + elif c == ")": + if paren_depth > 0: + paren_depth -= 1 + elif c == "=" and paren_depth > 0: + return True + i += 1 + return False + + def check_default_parameters(findings: List[Finding], lines: Iterable[DiffLine]) -> None: default_arg = re.compile(r"[(,]\s*[^()=;,{}]+\b\w+\s*=\s*[^,);{}]+") control_flow = re.compile(r"^(for|if|while|switch|catch)\s*\(") + comment_strip_re = re.compile(r"//.*$|/\*.*?\*/") for line in lines: if Path(line.path).suffix.lower() not in HEADER_EXTENSIONS: continue @@ -307,11 +364,18 @@ def check_default_parameters(findings: List[Finding], lines: Iterable[DiffLine]) continue if control_flow.match(stripped): continue - if "(" in stripped and ")" in stripped and default_arg.search(stripped): + if "=" not in stripped: + continue + code_only = comment_strip_re.sub("", stripped) + if "=" not in code_only: + continue + if not _has_default_arg_in_parens(code_only): + continue + if "(" in code_only and ")" in code_only and default_arg.search(code_only): add_finding( findings, "No new default parameters", - BLOCK, + WARN, line.path, line.line, "Header diff adds a function declaration with a default argument.", diff --git a/tools/03_code_analysis/test_agent_governance_check.py b/tools/03_code_analysis/test_agent_governance_check.py index d5b6bff299a..fffd20b3860 100644 --- a/tools/03_code_analysis/test_agent_governance_check.py +++ b/tools/03_code_analysis/test_agent_governance_check.py @@ -148,13 +148,13 @@ def test_allows_global_names_in_governance_checker_tests(self): self.assertEqual(result.returncode, 0, result.stdout + result.stderr) - def test_blocks_default_parameters_added_to_headers(self): + def test_warns_for_default_parameters_added_to_headers(self): self.write("source/source_base/defaults.h", "void update_solver(int step = 0);\n") head = self.commit_change() result = self.run_checker("--base", self.base, "--head", head) - self.assert_blocked_by(result, "No new default parameters") + self.assert_warns_with_success(result, "No new default parameters") def test_ignores_crlf_to_lf_only_changes_for_semantic_added_lines(self): self.write("source/source_base/defaults.h", b"void update_solver(int step = 0);\r\n", mode="wb")