From c3e78f75f706e362c2b23e4334465d736312a4bc Mon Sep 17 00:00:00 2001 From: dyzheng Date: Fri, 27 Dec 2024 10:05:39 +0800 Subject: [PATCH] Fix: read atomic magnetization error (#5772) --- source/module_cell/read_atoms.cpp | 71 ++++++++++++++++--------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/source/module_cell/read_atoms.cpp b/source/module_cell/read_atoms.cpp index 30ae5c504b..9f369cd3f5 100644 --- a/source/module_cell/read_atoms.cpp +++ b/source/module_cell/read_atoms.cpp @@ -676,45 +676,44 @@ bool UnitCell::read_atom_positions(std::ifstream &ifpos, std::ofstream &ofs_runn std::string mags; //cout<<"mag"< 1e-10 ) { - if(input_angle_mag) - { - atoms[it].m_loc_[ia].z = atoms[it].mag[ia] * - cos(atoms[it].angle1[ia]); - if(std::abs(sin(atoms[it].angle1[ia])) > 1e-10 ) - { - atoms[it].m_loc_[ia].x = atoms[it].mag[ia] * - sin(atoms[it].angle1[ia]) * cos(atoms[it].angle2[ia]); - atoms[it].m_loc_[ia].y = atoms[it].mag[ia] * - sin(atoms[it].angle1[ia]) * sin(atoms[it].angle2[ia]); - } - } - else if (input_vec_mag) - { - double mxy=sqrt(pow(atoms[it].m_loc_[ia].x,2)+pow(atoms[it].m_loc_[ia].y,2)); - atoms[it].angle1[ia]=atan2(mxy,atoms[it].m_loc_[ia].z); - if(mxy>1e-8) - { - atoms[it].angle2[ia]=atan2(atoms[it].m_loc_[ia].y,atoms[it].m_loc_[ia].x); - } - } - else - { - atoms[it].m_loc_[ia].x = 0; - atoms[it].m_loc_[ia].y = 0; - atoms[it].m_loc_[ia].z = atoms[it].mag[ia]; - } + atoms[it].m_loc_[ia].x = atoms[it].mag[ia] * + sin(atoms[it].angle1[ia]) * cos(atoms[it].angle2[ia]); + atoms[it].m_loc_[ia].y = atoms[it].mag[ia] * + sin(atoms[it].angle1[ia]) * sin(atoms[it].angle2[ia]); } - else + } + else if (input_vec_mag) + {// mx, my, mz are given, calculate angle1 and angle2 from mx, my, mz + double mxy=sqrt(pow(atoms[it].m_loc_[ia].x,2)+pow(atoms[it].m_loc_[ia].y,2)); + atoms[it].angle1[ia]=atan2(mxy,atoms[it].m_loc_[ia].z); + if(mxy>1e-8) { + atoms[it].angle2[ia]=atan2(atoms[it].m_loc_[ia].y,atoms[it].m_loc_[ia].x); + } + } + else// only one mag is given, assume it is z + { + atoms[it].m_loc_[ia].x = 0; + atoms[it].m_loc_[ia].y = 0; + atoms[it].m_loc_[ia].z = atoms[it].mag[ia]; + } + + if(PARAM.inp.nspin==4) + { + if(!PARAM.inp.noncolin) + { + //collinear case with nspin = 4, only z component is used atoms[it].m_loc_[ia].x = 0; atoms[it].m_loc_[ia].y = 0; - atoms[it].m_loc_[ia].z = atoms[it].mag[ia]; } - //print only ia==0 && mag>0 to avoid too much output //print when ia!=0 && mag[ia] != mag[0] to avoid too much output if(ia==0 || (ia!=0 @@ -734,8 +733,8 @@ bool UnitCell::read_atom_positions(std::ifstream &ifpos, std::ofstream &ofs_runn ModuleBase::GlobalFunc::ZEROS(magnet.ux_ ,3); } else if(PARAM.inp.nspin==2) - { - atoms[it].m_loc_[ia].x = atoms[it].mag[ia]; + {// collinear case with nspin = 2, only z component is used + atoms[it].mag[ia] = atoms[it].m_loc_[ia].z; //print only ia==0 && mag>0 to avoid too much output //print when ia!=0 && mag[ia] != mag[0] to avoid too much output if(ia==0 || (ia!=0 && atoms[it].mag[ia] != atoms[it].mag[0])) @@ -750,6 +749,8 @@ bool UnitCell::read_atom_positions(std::ifstream &ifpos, std::ofstream &ofs_runn ModuleBase::GlobalFunc::OUT(ofs_running, ss.str(),atoms[it].mag[ia]); } } + // end of calculating initial magnetization of each atom + // ---------------------------------------------------------------------------- if(Coordinate=="Direct") {