Skip to content

Commit f42edfa

Browse files
committed
Refactor: Remove cal_ux from ucell.
1 parent 29b3abc commit f42edfa

File tree

20 files changed

+131
-101
lines changed

20 files changed

+131
-101
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ OBJS_ELECSTAT=elecstate.o\
224224
H_Hartree_pw.o\
225225
H_TDDFT_pw.o\
226226
pot_xc.o\
227+
cal_ux.o\
227228

228229
OBJS_ELECSTAT_LCAO=elecstate_lcao.o\
229230
elecstate_lcao_tddft.o\

source/module_cell/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ add_test(NAME cell_parallel_kpoints_test
102102
AddTest(
103103
TARGET cell_unitcell_test
104104
LIBS parameter ${math_libs} base device cell_info symmetry
105-
SOURCES unitcell_test.cpp ../../module_io/output.cpp
105+
SOURCES unitcell_test.cpp ../../module_io/output.cpp ../../module_elecstate/cal_ux.cpp
106106

107107
)
108108

source/module_cell/test/support/mock_unitcell.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
to avoid using UnitCell functions because there is GLobalC, which will bring
99
endless compile troubles like undefined behavior"
1010
*/
11-
void UnitCell::cal_ux() {}
12-
bool UnitCell::judge_parallel(double a[3], ModuleBase::Vector3<double> b) {
13-
return true;
14-
}
1511
void UnitCell::set_iat2iwt(const int& npol_in) {}
1612
UnitCell::UnitCell() {
1713
Coordinate = "Direct";

source/module_cell/test/unitcell_test.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "module_parameter/parameter.h"
55
#undef private
66
#include "module_cell/unitcell.h"
7+
#include "module_elecstate/cal_ux.h"
78

89
#include "memory"
910
#include "module_base/global_variable.h"
@@ -97,7 +98,7 @@ Magnetism::~Magnetism()
9798
* - UpdateVel
9899
* - update_vel(const ModuleBase::Vector3<double>* vel_in)
99100
* - CalUx
100-
* - cal_ux(): calculate magnetic moments of cell
101+
* - cal_ux(UnitCell& ucell): calculate magnetic moments of cell
101102
* - ReadOrbFile
102103
* - read_orb_file(): read header part of orbital file
103104
* - ReadOrbFileWarning
@@ -610,7 +611,7 @@ TEST_F(UcellTest, JudgeParallel)
610611
{
611612
ModuleBase::Vector3<double> b(1.0, 1.0, 1.0);
612613
double a[3] = {1.0, 1.0, 1.0};
613-
EXPECT_TRUE(ucell->judge_parallel(a, b));
614+
EXPECT_TRUE(elecstate::judge_parallel(a, b));
614615
}
615616

616617
TEST_F(UcellTest, Index)
@@ -1034,7 +1035,7 @@ TEST_F(UcellTest, CalUx1)
10341035
ucell->atoms[0].m_loc_[0].set(0, -1, 0);
10351036
ucell->atoms[1].m_loc_[0].set(1, 1, 1);
10361037
ucell->atoms[1].m_loc_[1].set(0, 0, 0);
1037-
ucell->cal_ux();
1038+
elecstate::cal_ux(*ucell);
10381039
EXPECT_FALSE(ucell->magnet.lsign_);
10391040
EXPECT_DOUBLE_EQ(ucell->magnet.ux_[0], 0);
10401041
EXPECT_DOUBLE_EQ(ucell->magnet.ux_[1], -1);
@@ -1050,7 +1051,7 @@ TEST_F(UcellTest, CalUx2)
10501051
ucell->atoms[1].m_loc_[0].set(1, 1, 1);
10511052
ucell->atoms[1].m_loc_[1].set(0, 0, 0);
10521053
//(0,0,0) is also parallel to (1,1,1)
1053-
ucell->cal_ux();
1054+
elecstate::cal_ux(*ucell);
10541055
EXPECT_TRUE(ucell->magnet.lsign_);
10551056
EXPECT_NEAR(ucell->magnet.ux_[0], 0.57735, 1e-5);
10561057
EXPECT_NEAR(ucell->magnet.ux_[1], 0.57735, 1e-5);

source/module_cell/unitcell.cpp

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -431,70 +431,6 @@ void UnitCell::bcast_atoms_tau() {
431431
#endif
432432
}
433433

434-
void UnitCell::cal_ux() {
435-
double amag, uxmod;
436-
int starting_it = 0;
437-
int starting_ia = 0;
438-
bool is_paraller;
439-
// do not sign feature in teh general case
440-
magnet.lsign_ = false;
441-
ModuleBase::GlobalFunc::ZEROS(magnet.ux_, 3);
442-
443-
for (int it = 0; it < ntype; it++) {
444-
for (int ia = 0; ia < atoms[it].na; ia++) {
445-
amag = pow(atoms[it].m_loc_[ia].x, 2)
446-
+ pow(atoms[it].m_loc_[ia].y, 2)
447-
+ pow(atoms[it].m_loc_[ia].z, 2);
448-
if (amag > 1e-6) {
449-
magnet.ux_[0] = atoms[it].m_loc_[ia].x;
450-
magnet.ux_[1] = atoms[it].m_loc_[ia].y;
451-
magnet.ux_[2] = atoms[it].m_loc_[ia].z;
452-
starting_it = it;
453-
starting_ia = ia;
454-
magnet.lsign_ = true;
455-
break;
456-
}
457-
}
458-
if (magnet.lsign_) {
459-
break;
460-
}
461-
}
462-
// whether the initial magnetizations is parallel
463-
for (int it = starting_it; it < ntype; it++) {
464-
for (int ia = 0; ia < atoms[it].na; ia++) {
465-
if (it > starting_it || ia > starting_ia) {
466-
magnet.lsign_
467-
= magnet.lsign_
468-
&& judge_parallel(magnet.ux_, atoms[it].m_loc_[ia]);
469-
}
470-
}
471-
}
472-
if (magnet.lsign_) {
473-
uxmod = pow(magnet.ux_[0], 2) + pow(magnet.ux_[1], 2)
474-
+ pow(magnet.ux_[2], 2);
475-
if (uxmod < 1e-6) {
476-
ModuleBase::WARNING_QUIT("cal_ux", "wrong uxmod");
477-
}
478-
for (int i = 0; i < 3; i++) {
479-
magnet.ux_[i] *= 1 / sqrt(uxmod);
480-
}
481-
// std::cout<<" Fixed quantization axis for GGA: "
482-
//<<std::setw(10)<<ux[0]<<" "<<std::setw(10)<<ux[1]<<"
483-
//"<<std::setw(10)<<ux[2]<<std::endl;
484-
}
485-
return;
486-
}
487-
488-
bool UnitCell::judge_parallel(double a[3], ModuleBase::Vector3<double> b) {
489-
bool jp = false;
490-
double cross;
491-
cross = pow((a[1] * b.z - a[2] * b.y), 2)
492-
+ pow((a[2] * b.x - a[0] * b.z), 2)
493-
+ pow((a[0] * b.y - a[1] * b.x), 2);
494-
jp = (fabs(cross) < 1e-6);
495-
return jp;
496-
}
497-
498434
//==============================================================
499435
// Calculate various lattice related quantities for given latvec
500436
//==============================================================

source/module_cell/unitcell.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class UnitCell {
1919

2020
bool set_atom_flag; // added on 2009-3-8 by mohan
2121
Magnetism magnet; // magnetism Yu Liu 2021-07-03
22-
void cal_ux();
23-
bool judge_parallel(double a[3], ModuleBase::Vector3<double> b);
2422
std::vector<std::vector<double>> atom_mulliken; //[nat][nspin]
2523
int n_mag_at;
2624

source/module_elecstate/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ list(APPEND objects
2626
fp_energy.cpp
2727
magnetism.cpp
2828
occupy.cpp
29+
cal_ux.cpp
2930
)
3031

3132
if(ENABLE_LCAO)

source/module_elecstate/cal_ux.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "cal_ux.h"
2+
3+
namespace elecstate {
4+
5+
void cal_ux(UnitCell& ucell) {
6+
double amag, uxmod;
7+
int starting_it = 0;
8+
int starting_ia = 0;
9+
bool is_paraller;
10+
// do not sign feature in teh general case
11+
ucell.magnet.lsign_ = false;
12+
ModuleBase::GlobalFunc::ZEROS(ucell.magnet.ux_, 3);
13+
14+
for (int it = 0; it < ucell.ntype; it++) {
15+
for (int ia = 0; ia < ucell.atoms[it].na; ia++) {
16+
amag = pow(ucell.atoms[it].m_loc_[ia].x, 2)
17+
+ pow(ucell.atoms[it].m_loc_[ia].y, 2)
18+
+ pow(ucell.atoms[it].m_loc_[ia].z, 2);
19+
if (amag > 1e-6) {
20+
ucell.magnet.ux_[0] = ucell.atoms[it].m_loc_[ia].x;
21+
ucell.magnet.ux_[1] = ucell.atoms[it].m_loc_[ia].y;
22+
ucell.magnet.ux_[2] = ucell.atoms[it].m_loc_[ia].z;
23+
starting_it = it;
24+
starting_ia = ia;
25+
ucell.magnet.lsign_ = true;
26+
break;
27+
}
28+
}
29+
if (ucell.magnet.lsign_) {
30+
break;
31+
}
32+
}
33+
// whether the initial magnetizations is parallel
34+
for (int it = starting_it; it < ucell.ntype; it++) {
35+
for (int ia = 0; ia < ucell.atoms[it].na; ia++) {
36+
if (it > starting_it || ia > starting_ia) {
37+
ucell.magnet.lsign_
38+
= ucell.magnet.lsign_
39+
&& judge_parallel(ucell.magnet.ux_, ucell.atoms[it].m_loc_[ia]);
40+
}
41+
}
42+
}
43+
if (ucell.magnet.lsign_) {
44+
uxmod = pow(ucell.magnet.ux_[0], 2) + pow(ucell.magnet.ux_[1], 2)
45+
+ pow(ucell.magnet.ux_[2], 2);
46+
if (uxmod < 1e-6) {
47+
ModuleBase::WARNING_QUIT("cal_ux", "wrong uxmod");
48+
}
49+
for (int i = 0; i < 3; i++) {
50+
ucell.magnet.ux_[i] *= 1 / sqrt(uxmod);
51+
}
52+
// std::cout<<" Fixed quantization axis for GGA: "
53+
//<<std::setw(10)<<ux[0]<<" "<<std::setw(10)<<ux[1]<<"
54+
//"<<std::setw(10)<<ux[2]<<std::endl;
55+
}
56+
return;
57+
}
58+
59+
bool judge_parallel(double a[3], ModuleBase::Vector3<double> b) {
60+
bool jp = false;
61+
double cross;
62+
cross = pow((a[1] * b.z - a[2] * b.y), 2)
63+
+ pow((a[2] * b.x - a[0] * b.z), 2)
64+
+ pow((a[0] * b.y - a[1] * b.x), 2);
65+
jp = (fabs(cross) < 1e-6);
66+
return jp;
67+
}
68+
}

source/module_elecstate/cal_ux.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef CAL_UX_H
2+
#define CAL_UX_H
3+
4+
#include "module_cell/unitcell.h"
5+
6+
namespace elecstate {
7+
8+
void cal_ux(UnitCell& ucell);
9+
10+
bool judge_parallel(double a[3], ModuleBase::Vector3<double> b);
11+
12+
}
13+
14+
#endif

source/module_esolver/esolver_ks_lcao.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "module_hamilt_lcao/module_dftu/dftu.h"
2929
#include "module_hamilt_pw/hamilt_pwdft/global.h"
3030
#include "module_io/print_info.h"
31+
#include "module_elecstate/cal_ux.h"
3132

3233
#include <memory>
3334
#ifdef __EXX
@@ -608,7 +609,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_init(const int istep, const int iter)
608609

609610
if (PARAM.inp.nspin == 4)
610611
{
611-
GlobalC::ucell.cal_ux();
612+
elecstate::cal_ux(GlobalC::ucell);
612613
}
613614

614615
//! update the potentials by using new electron charge density
@@ -891,7 +892,7 @@ void ESolver_KS_LCAO<TK, TR>::update_pot(const int istep, const int iter)
891892
{
892893
if (PARAM.inp.nspin == 4)
893894
{
894-
GlobalC::ucell.cal_ux();
895+
elecstate::cal_ux(GlobalC::ucell);
895896
}
896897
this->pelec->pot->update_from_charge(this->pelec->charge, &GlobalC::ucell);
897898
this->pelec->f_en.descf = this->pelec->cal_delta_escf();

0 commit comments

Comments
 (0)