Skip to content

Commit 4737f8a

Browse files
Refactor: Remove set kvec funcs in K_Vectors
1 parent 009964b commit 4737f8a

File tree

7 files changed

+79
-84
lines changed

7 files changed

+79
-84
lines changed

source/module_cell/k_vector_utils.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ namespace KVectorUtils
1414
{
1515
void k_vec_d2c(K_Vectors& kv, const ModuleBase::Matrix3& reciprocal_vec)
1616
{
17-
for (int i = 0; i < kv.get_nks(); i++)
17+
if (kv.kvec_d.size() != kv.kvec_c.size())
18+
{
19+
ModuleBase::WARNING_QUIT("k_vec_d2c", "Size of Cartesian and Direct K vectors mismatch. ");
20+
}
21+
int nks = kv.kvec_d.size(); // always convert all k vectors
22+
23+
for (int i = 0; i < nks; i++)
1824
{
1925
// wrong!! kvec_c[i] = G * kvec_d[i];
2026
// mohan fixed bug 2010-1-10
@@ -50,8 +56,14 @@ void k_vec_d2c(K_Vectors& kv, const ModuleBase::Matrix3& reciprocal_vec)
5056
}
5157
void k_vec_c2d(K_Vectors& kv, const ModuleBase::Matrix3& latvec)
5258
{
59+
if (kv.kvec_d.size() != kv.kvec_c.size())
60+
{
61+
ModuleBase::WARNING_QUIT("k_vec_c2d", "Size of Cartesian and Direct K vectors mismatch. ");
62+
}
63+
int nks = kv.kvec_d.size(); // always convert all k vectors
64+
5365
ModuleBase::Matrix3 RT = latvec.Transpose();
54-
for (int i = 0; i < kv.get_nks(); i++)
66+
for (int i = 0; i < nks; i++)
5567
{
5668
// std::cout << " ik=" << i
5769
// << " kvec.x=" << kvec_c[i].x
@@ -128,9 +140,7 @@ void set_both_kvec(K_Vectors& kv, const ModuleBase::Matrix3& G, const ModuleBase
128140
return;
129141
}
130142

131-
void set_after_vc(K_Vectors& kv,
132-
const int& nspin_in,
133-
const ModuleBase::Matrix3& reciprocal_vec)
143+
void set_after_vc(K_Vectors& kv, const int& nspin_in, const ModuleBase::Matrix3& reciprocal_vec)
134144
{
135145
GlobalV::ofs_running << "\n SETUP K-POINTS" << std::endl;
136146
kv.nspin = nspin_in;

source/module_cell/k_vector_utils.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,48 @@ void k_vec_d2c(K_Vectors& kv, const ModuleBase::Matrix3& reciprocal_vec);
1616

1717
void k_vec_c2d(K_Vectors& kv, const ModuleBase::Matrix3& latvec);
1818

19+
/**
20+
* @brief Sets both the direct and Cartesian k-vectors.
21+
*
22+
* This function sets both the direct and Cartesian k-vectors based on the input parameters.
23+
* It also checks the k-point type and sets the corresponding flags.
24+
*
25+
* @param kv The K_Vectors object containing the k-point information.
26+
* @param G The reciprocal lattice matrix.
27+
* @param R The real space lattice matrix.
28+
* @param skpt A string to store the k-point table.
29+
*
30+
* @return void
31+
*
32+
* @note If the k-point type is neither "Cartesian" nor "Direct", an error message will be printed.
33+
* @note The function sets the flags kd_done and kc_done to indicate whether the direct and Cartesian k-vectors have
34+
* been set, respectively.
35+
* @note The function also prints a table of the direct k-vectors and their weights.
36+
* @note If the function is called by the master process (MY_RANK == 0), the k-point table is also stored in the
37+
* string skpt.
38+
*/
1939
void set_both_kvec(K_Vectors& kv, const ModuleBase::Matrix3& G, const ModuleBase::Matrix3& R, std::string& skpt);
2040

41+
/**
42+
* @brief Sets up the k-points after a volume change.
43+
*
44+
* This function sets up the k-points after a volume change in the system.
45+
* It sets the Cartesian and direct k-vectors based on the new reciprocal and real space lattice vectors.
46+
*
47+
* @param kv The K_Vectors object containing the k-point information.
48+
* @param nspin_in The number of spins. 1 for non-spin-polarized calculations and 2 for spin-polarized calculations.
49+
* @param reciprocal_vec The new reciprocal lattice matrix.
50+
*
51+
* @return void
52+
*
53+
* @note The function first sets the number of spins (nspin) to the input value.
54+
* @note The direct k-vectors have been set (kd_done = true) but the Cartesian k-vectors have not (kc_done =
55+
* false) after a volume change. The function calculates the Cartesian k-vectors by multiplying the direct k-vectors with the reciprocal
56+
* lattice matrix.
57+
* @note The function also prints a table of the direct k-vectors and their weights.
58+
* @note The function calls the print_klists function to print the k-points in both Cartesian and direct
59+
* coordinates.
60+
*/
2161
void set_after_vc(K_Vectors& kv, const int& nspin, const ModuleBase::Matrix3& G);
2262
} // namespace KVectorUtils
2363

source/module_cell/klist.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ void K_Vectors::set(const UnitCell& ucell,
126126
// Improve k point information
127127

128128
// Complement the coordinates of k point
129-
this->set_both_kvec(reciprocal_vec, latvec, skpt2);
130-
129+
// this->set_both_kvec(reciprocal_vec, latvec, skpt2);
130+
KVectorUtils::set_both_kvec(*this, reciprocal_vec, latvec, skpt2);
131131
std::cout << "after set_both_kvec" << std::endl;
132132
std::cout << "nks: " << nks << std::endl;
133133
std::cout << "nkstot: " << nkstot << std::endl;
@@ -1012,15 +1012,6 @@ void K_Vectors::ibz_kpoint(const ModuleSymmetry::Symmetry& symm,
10121012
return;
10131013
}
10141014

1015-
// complement coordinates of k-points according to existing coordinates
1016-
// if cartesian coordinates are given, then direct coordinates are calculated
1017-
// if direct coordinates are given, then cartesian coordinates are calculated
1018-
void K_Vectors::set_both_kvec(const ModuleBase::Matrix3& G, const ModuleBase::Matrix3& R, std::string& skpt)
1019-
{
1020-
KVectorUtils::set_both_kvec(*this, G, R, skpt);
1021-
1022-
}
1023-
10241015
void K_Vectors::normalize_wk(const int& degspin)
10251016
{
10261017
if (GlobalV::MY_RANK != 0) {
@@ -1263,16 +1254,3 @@ void K_Vectors::print_klists(std::ofstream& ofs)
12631254
GlobalV::ofs_running << "\n" << table << std::endl;
12641255
return;
12651256
}
1266-
1267-
// LiuXh add a new function here,
1268-
// 20180515
1269-
void K_Vectors::set_after_vc(const int& nspin_in,
1270-
const ModuleBase::Matrix3& reciprocal_vec,
1271-
const ModuleBase::Matrix3& latvec)
1272-
{
1273-
ModuleBase::TITLE("K_Vectors", "set_after_vc");
1274-
1275-
KVectorUtils::set_after_vc(*this, nspin_in, reciprocal_vec);
1276-
1277-
return;
1278-
}

source/module_cell/klist.h

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,8 @@ class K_Vectors
8080
std::string& skpt,
8181
const UnitCell& ucell,
8282
bool& match);
83-
// LiuXh add 20180515
8483

85-
/**
86-
* @brief Sets up the k-points after a volume change.
87-
*
88-
* This function sets up the k-points after a volume change in the system.
89-
* It sets the Cartesian and direct k-vectors based on the new reciprocal and real space lattice vectors.
90-
*
91-
* @param nspin_in The number of spins. 1 for non-spin-polarized calculations and 2 for spin-polarized calculations.
92-
* @param reciprocal_vec The new reciprocal lattice matrix.
93-
* @param latvec The new real space lattice matrix.
94-
*
95-
* @return void
96-
*
97-
* @note The function first sets the number of spins (nspin) to the input value.
98-
* @note If the direct k-vectors have been set (kd_done = true) and the Cartesian k-vectors have not (kc_done =
99-
* false), the function calculates the Cartesian k-vectors by multiplying the direct k-vectors with the reciprocal
100-
* lattice matrix.
101-
* @note If the Cartesian k-vectors have been set (kc_done = true) and the direct k-vectors have not (kd_done =
102-
* false), the function calculates the direct k-vectors by multiplying the Cartesian k-vectors with the transpose of
103-
* the real space lattice matrix.
104-
* @note The function also prints a table of the direct k-vectors and their weights.
105-
* @note The function calls the print_klists function to print the k-points in both Cartesian and direct
106-
* coordinates.
107-
*/
108-
void set_after_vc(const int& nspin, const ModuleBase::Matrix3& reciprocal_vec, const ModuleBase::Matrix3& latvec);
84+
10985

11086
int get_nks() const
11187
{
@@ -282,26 +258,7 @@ class K_Vectors
282258
const std::vector<ModuleBase::Vector3<double>>& kvec_d_ibz,
283259
const std::vector<double>& wk_ibz);
284260

285-
/**
286-
* @brief Sets both the direct and Cartesian k-vectors.
287-
*
288-
* This function sets both the direct and Cartesian k-vectors based on the input parameters.
289-
* It also checks the k-point type and sets the corresponding flags.
290-
*
291-
* @param G The reciprocal lattice matrix.
292-
* @param R The real space lattice matrix.
293-
* @param skpt A string to store the k-point table.
294-
*
295-
* @return void
296-
*
297-
* @note If the k-point type is neither "Cartesian" nor "Direct", an error message will be printed.
298-
* @note The function sets the flags kd_done and kc_done to indicate whether the direct and Cartesian k-vectors have
299-
* been set, respectively.
300-
* @note The function also prints a table of the direct k-vectors and their weights.
301-
* @note If the function is called by the master process (MY_RANK == 0), the k-point table is also stored in the
302-
* string skpt.
303-
*/
304-
void set_both_kvec(const ModuleBase::Matrix3& G, const ModuleBase::Matrix3& R, std::string& skpt);
261+
// void set_both_kvec(const ModuleBase::Matrix3& G, const ModuleBase::Matrix3& R, std::string& skpt);
305262

306263
/**
307264
* @brief Normalizes the weights of the k-points.

source/module_cell/test/klist_test.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,8 @@ TEST_F(KlistTest, SetAfterVC)
593593
kv->kvec_c[0].x = 0;
594594
kv->kvec_c[0].y = 0;
595595
kv->kvec_c[0].z = 0;
596-
kv->set_after_vc(PARAM.input.nspin, ucell.G, ucell.latvec);
596+
// kv->set_after_vc(PARAM.input.nspin, ucell.G, ucell.latvec);
597+
597598
EXPECT_TRUE(kv->kd_done);
598599
EXPECT_TRUE(kv->kc_done);
599600
EXPECT_DOUBLE_EQ(kv->kvec_d[0].x, 0);
@@ -613,7 +614,8 @@ TEST_F(KlistTest, PrintKlists)
613614
kv->kvec_c[0].x = 0;
614615
kv->kvec_c[0].y = 0;
615616
kv->kvec_c[0].z = 0;
616-
kv->set_after_vc(PARAM.input.nspin, ucell.G, ucell.latvec);
617+
// kv->set_after_vc(PARAM.input.nspin, ucell.G, ucell.latvec);
618+
KVectorUtils::set_after_vc(*kv, PARAM.input.nspin, ucell.G);
617619
EXPECT_TRUE(kv->kd_done);
618620
kv->print_klists(GlobalV::ofs_running);
619621
GlobalV::ofs_running.close();
@@ -653,24 +655,28 @@ TEST_F(KlistTest, SetBothKvecFinalSCF)
653655
kv->kc_done = false;
654656
// case 1
655657
kv->k_nkstot = 0;
656-
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
658+
// kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
659+
KVectorUtils::set_both_kvec(*kv, ucell.G, ucell.latvec, skpt);
657660
EXPECT_TRUE(kv->kd_done);
658661
EXPECT_TRUE(kv->kc_done);
659662
// case 2
660663
kv->k_nkstot = 1;
661664
kv->k_kword = "D";
662-
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
665+
// kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
666+
KVectorUtils::set_both_kvec(*kv, ucell.G, ucell.latvec, skpt);
663667
EXPECT_TRUE(kv->kd_done);
664668
EXPECT_TRUE(kv->kc_done);
665669
// case 3
666670
kv->k_kword = "C";
667-
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
671+
// kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
672+
KVectorUtils::set_both_kvec(*kv, ucell.G, ucell.latvec, skpt);
668673
EXPECT_TRUE(kv->kc_done);
669674
EXPECT_TRUE(kv->kd_done);
670675
// case 4
671676
GlobalV::ofs_warning.open("klist_tmp_warning_8");
672677
kv->k_kword = "arbitrary";
673-
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
678+
// kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
679+
KVectorUtils::set_both_kvec(*kv, ucell.G, ucell.latvec, skpt);
674680
GlobalV::ofs_warning.close();
675681
ifs.open("klist_tmp_warning_8");
676682
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -692,11 +698,13 @@ TEST_F(KlistTest, SetBothKvec)
692698
kv->kd_done = true;
693699
std::string skpt;
694700
PARAM.input.final_scf = false;
695-
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
701+
// kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
702+
KVectorUtils::set_both_kvec(*kv, ucell.G, ucell.latvec, skpt);
696703
EXPECT_TRUE(kv->kc_done);
697704
kv->kc_done = true;
698705
kv->kd_done = false;
699-
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
706+
// kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
707+
KVectorUtils::set_both_kvec(*kv, ucell.G, ucell.latvec, skpt);
700708
EXPECT_TRUE(kv->kd_done);
701709
}
702710

source/module_cell/test/klist_test_para.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ TEST_F(KlistParaTest, SetAfterVC)
309309
}
310310
// call set_after_vc here
311311
kv->kc_done = false;
312-
kv->set_after_vc(kv->nspin, ucell.G, ucell.latvec);
312+
// kv->set_after_vc(kv->nspin, ucell.G, ucell.latvec);
313+
KVectorUtils::set_after_vc(*kv, kv->nspin, ucell.G);
313314
EXPECT_TRUE(kv->kc_done);
314315
EXPECT_TRUE(kv->kd_done);
315316
// clear

source/module_esolver/esolver_fp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "module_io/write_elecstat_pot.h"
1818
#include "module_io/write_elf.h"
1919
#include "module_parameter/parameter.h"
20+
#include "module_cell/k_vector_utils.h"
2021

2122
namespace ModuleESolver
2223
{
@@ -292,7 +293,7 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep)
292293
}
293294

294295
// reset k-points
295-
kv.set_after_vc(PARAM.inp.nspin, ucell.G, ucell.latvec);
296+
KVectorUtils::set_after_vc(kv, PARAM.inp.nspin, ucell.G);
296297
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS");
297298
}
298299

0 commit comments

Comments
 (0)