Skip to content

Commit 93ac7e3

Browse files
authored
Refactor: Remove cal_ux from ucell. (#5608)
1 parent 9bc8960 commit 93ac7e3

File tree

21 files changed

+133
-102
lines changed

21 files changed

+133
-102
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ OBJS_ELECSTAT=elecstate.o\
226226
H_Hartree_pw.o\
227227
H_TDDFT_pw.o\
228228
pot_xc.o\
229+
cal_ux.o\
229230

230231
OBJS_ELECSTAT_LCAO=elecstate_lcao.o\
231232
elecstate_lcao_cal_tau.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
@@ -473,70 +473,6 @@ void UnitCell::bcast_atoms_tau() {
473473
#endif
474474
}
475475

476-
void UnitCell::cal_ux() {
477-
double amag, uxmod;
478-
int starting_it = 0;
479-
int starting_ia = 0;
480-
bool is_paraller;
481-
// do not sign feature in teh general case
482-
magnet.lsign_ = false;
483-
ModuleBase::GlobalFunc::ZEROS(magnet.ux_, 3);
484-
485-
for (int it = 0; it < ntype; it++) {
486-
for (int ia = 0; ia < atoms[it].na; ia++) {
487-
amag = pow(atoms[it].m_loc_[ia].x, 2)
488-
+ pow(atoms[it].m_loc_[ia].y, 2)
489-
+ pow(atoms[it].m_loc_[ia].z, 2);
490-
if (amag > 1e-6) {
491-
magnet.ux_[0] = atoms[it].m_loc_[ia].x;
492-
magnet.ux_[1] = atoms[it].m_loc_[ia].y;
493-
magnet.ux_[2] = atoms[it].m_loc_[ia].z;
494-
starting_it = it;
495-
starting_ia = ia;
496-
magnet.lsign_ = true;
497-
break;
498-
}
499-
}
500-
if (magnet.lsign_) {
501-
break;
502-
}
503-
}
504-
// whether the initial magnetizations is parallel
505-
for (int it = starting_it; it < ntype; it++) {
506-
for (int ia = 0; ia < atoms[it].na; ia++) {
507-
if (it > starting_it || ia > starting_ia) {
508-
magnet.lsign_
509-
= magnet.lsign_
510-
&& judge_parallel(magnet.ux_, atoms[it].m_loc_[ia]);
511-
}
512-
}
513-
}
514-
if (magnet.lsign_) {
515-
uxmod = pow(magnet.ux_[0], 2) + pow(magnet.ux_[1], 2)
516-
+ pow(magnet.ux_[2], 2);
517-
if (uxmod < 1e-6) {
518-
ModuleBase::WARNING_QUIT("cal_ux", "wrong uxmod");
519-
}
520-
for (int i = 0; i < 3; i++) {
521-
magnet.ux_[i] *= 1 / sqrt(uxmod);
522-
}
523-
// std::cout<<" Fixed quantization axis for GGA: "
524-
//<<std::setw(10)<<ux[0]<<" "<<std::setw(10)<<ux[1]<<"
525-
//"<<std::setw(10)<<ux[2]<<std::endl;
526-
}
527-
return;
528-
}
529-
530-
bool UnitCell::judge_parallel(double a[3], ModuleBase::Vector3<double> b) {
531-
bool jp = false;
532-
double cross;
533-
cross = pow((a[1] * b.z - a[2] * b.y), 2)
534-
+ pow((a[2] * b.x - a[0] * b.z), 2)
535-
+ pow((a[0] * b.y - a[1] * b.x), 2);
536-
jp = (fabs(cross) < 1e-6);
537-
return jp;
538-
}
539-
540476
//==============================================================
541477
// Calculate various lattice related quantities for given latvec
542478
//==============================================================

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
@@ -31,6 +31,7 @@ list(APPEND objects
3131
fp_energy.cpp
3232
magnetism.cpp
3333
occupy.cpp
34+
cal_ux.cpp
3435
)
3536

3637
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
@@ -40,6 +40,7 @@
4040
#include "module_hamilt_lcao/module_dftu/dftu.h"
4141
#include "module_hamilt_pw/hamilt_pwdft/global.h"
4242
#include "module_io/print_info.h"
43+
#include "module_elecstate/cal_ux.h"
4344

4445
#include <memory>
4546
#ifdef __EXX
@@ -593,7 +594,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_init(UnitCell& ucell, const int istep, const
593594

594595
if (PARAM.inp.nspin == 4)
595596
{
596-
ucell.cal_ux();
597+
elecstate::cal_ux(ucell);
597598
}
598599

599600
//! update the potentials by using new electron charge density
@@ -829,7 +830,7 @@ void ESolver_KS_LCAO<TK, TR>::update_pot(UnitCell& ucell, const int istep, const
829830
{
830831
if (PARAM.inp.nspin == 4)
831832
{
832-
ucell.cal_ux();
833+
elecstate::cal_ux(ucell);
833834
}
834835
this->pelec->pot->update_from_charge(this->pelec->charge, &ucell);
835836
this->pelec->f_en.descf = this->pelec->cal_delta_escf();

0 commit comments

Comments
 (0)