Skip to content
Merged
4 changes: 2 additions & 2 deletions source/source_cell/atom_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Atom
std::vector<int> 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<ModuleBase::Vector3<double>> tau; // Cartesian coordinates of each atom in this type.
std::vector<ModuleBase::Vector3<double>> dis; // direct displacements of each atom in this type in current step liuyu modift 2023-03-22
std::vector<ModuleBase::Vector3<double>> taud; // Direct coordinates of each atom in this type.
Expand All @@ -40,7 +40,7 @@ class Atom
std::vector<ModuleBase::Vector3<double>> force; // force acting on each atom in this type.
std::vector<ModuleBase::Vector3<double>> lambda; // Lagrange multiplier for each atom in this type. used in deltaspin
std::vector<ModuleBase::Vector3<int>> 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<double> mag;
std::vector<double> angle1; // spin angle, added by zhengdy-soc
Expand Down
8 changes: 6 additions & 2 deletions source/source_cell/bcast_cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]);
Expand Down
23 changes: 11 additions & 12 deletions source/source_cell/magnetism.cpp
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -14,21 +11,23 @@ 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);
assert(nxyz>0);

const double fac = omega / nxyz;

if (PARAM.inp.nspin==2)
if (nspin==2)
{
this->tot_mag = 0.00;
this->abs_mag = 0.00;
Expand All @@ -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++)
{
Expand Down
20 changes: 9 additions & 11 deletions source/source_cell/magnetism.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "source_base/global_function.h"
#include "source_base/vector3.h"
#include <vector>

class Magnetism
{
Expand All @@ -12,7 +13,7 @@ class Magnetism
~Magnetism();

// notice : bcast (MPI operation) is done in unitcell
double *start_mag=nullptr;
std::vector<double> start_mag;

// tot_mag : majority spin - minority spin (nelup - neldw).
double tot_mag;
Expand All @@ -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<double> *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};

Expand Down
3 changes: 0 additions & 3 deletions source/source_cell/module_neighbor/test/prepare_unitcell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/************************************************
Expand Down
2 changes: 0 additions & 2 deletions source/source_cell/module_neighbor/test/sltk_grid_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/************************************************
Expand Down
5 changes: 5 additions & 0 deletions source/source_cell/read_atoms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(ntype))
{
ucell.magnet.start_mag.resize(ntype, 0.0);
}

if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifpos, "ATOMIC_POSITIONS"))
{
ModuleBase::GlobalFunc::READ_VALUE(ifpos, Coordinate);
Expand Down
14 changes: 14 additions & 0 deletions source/source_cell/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
***********************************************/
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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];
}
Expand All @@ -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];
}
Expand All @@ -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];
}
Expand All @@ -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];
}
Expand All @@ -135,18 +133,20 @@ TEST_F(MagnetismTest, ComputeMagnetizationS4)

#ifdef __MPI
#include <mpi.h>
#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

3 changes: 0 additions & 3 deletions source/source_cell/test/prepare_unitcell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Loading
Loading