Skip to content

Commit 25c3156

Browse files
author
abacus_fixer
committed
refactor(klist): extract EXX k-stars build into KListIO::build_kstars
Move the this-free triple loop that groups k points into EXX k-stars out of K_Vectors::reduce_by_symmetry into KListIO::build_kstars. The symmetry comparison is passed in as a std::function so klist_io.cpp does not depend on the Symmetry header; k restriction uses ModuleCell::restrict_kpt. Behavior is unchanged for symm_flag==1 (the only path that reaches this code). klist.cpp drops ~20 lines; the loop is now isolated and testable. Verified: cmake --build build_max_para_test -j30 (exit 0, __EXX on) and ctest -R "MODULE_CELL_(klist|reciprocal_grid|qlist)" -> 5/5.
1 parent 4dd1899 commit 25c3156

3 files changed

Lines changed: 64 additions & 27 deletions

File tree

source/source_cell/klist.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -622,35 +622,15 @@ void K_Vectors::reduce_by_symmetry(const UnitCell& ucell,
622622

623623
#ifdef __EXX
624624
// setup kstars according to the final (max-norm) kvec_d_ibz
625-
this->kstars.resize(nkstot_ibz);
626625
if (ModuleSymmetry::Symmetry::symm_flag == 1)
627626
{
628-
ModuleBase::Vector3<double> kvec_rot;
629-
for (int i = 0; i < this->nkstot; ++i)
630-
{
631-
int exist_number = -1;
632-
int isym = 0;
633-
for (int j = 0; j < nrotkm; ++j)
634-
{
635-
kvec_rot = this->kvec_d[i] * kgmatrix[j];
636-
ModuleCell::restrict_kpt(kvec_rot, symm.epsilon);
637-
for (int k = 0; k < nkstot_ibz; ++k)
638-
{
639-
if (symm.equal(kvec_rot.x, kvec_d_ibz[k].x) && symm.equal(kvec_rot.y, kvec_d_ibz[k].y)
640-
&& symm.equal(kvec_rot.z, kvec_d_ibz[k].z))
641-
{
642-
isym = j;
643-
exist_number = k;
644-
break;
645-
}
646-
}
647-
if (exist_number != -1)
648-
{
649-
break;
650-
}
651-
}
652-
this->kstars[exist_number].insert(std::make_pair(isym, this->kvec_d[i]));
653-
}
627+
KListIO::build_kstars(this->kvec_d,
628+
kgmatrix,
629+
nrotkm,
630+
kvec_d_ibz,
631+
symm.epsilon,
632+
[&symm](double a, double b) { return symm.equal(a, b); },
633+
this->kstars);
654634
}
655635
#endif
656636

source/source_cell/klist_io.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "source_base/formatter.h"
1010
#include "source_base/global_function.h"
11+
#include "source_cell/reciprocal_grid.h"
1112

1213
#include <sstream>
1314

@@ -137,4 +138,44 @@ LineK interp_line(std::ifstream& ifk, const int nks_special)
137138
return out;
138139
}
139140

141+
void build_kstars(const std::vector<ModuleBase::Vector3<double>>& kvec_d,
142+
const std::vector<ModuleBase::Matrix3>& kgmatrix,
143+
const int nrotkm,
144+
const std::vector<ModuleBase::Vector3<double>>& kvec_d_ibz,
145+
const double epsilon,
146+
const std::function<bool(double, double)>& equal,
147+
std::vector<std::map<int, ModuleBase::Vector3<double>>>& kstars)
148+
{
149+
const int nkstot = static_cast<int>(kvec_d.size());
150+
const int nkstot_ibz = static_cast<int>(kvec_d_ibz.size());
151+
kstars.resize(nkstot_ibz);
152+
153+
ModuleBase::Vector3<double> kvec_rot;
154+
for (int i = 0; i < nkstot; ++i)
155+
{
156+
int exist_number = -1;
157+
int isym = 0;
158+
for (int j = 0; j < nrotkm; ++j)
159+
{
160+
kvec_rot = kvec_d[i] * kgmatrix[j];
161+
ModuleCell::restrict_kpt(kvec_rot, epsilon);
162+
for (int k = 0; k < nkstot_ibz; ++k)
163+
{
164+
if (equal(kvec_rot.x, kvec_d_ibz[k].x) && equal(kvec_rot.y, kvec_d_ibz[k].y)
165+
&& equal(kvec_rot.z, kvec_d_ibz[k].z))
166+
{
167+
isym = j;
168+
exist_number = k;
169+
break;
170+
}
171+
}
172+
if (exist_number != -1)
173+
{
174+
break;
175+
}
176+
}
177+
kstars[exist_number].insert(std::make_pair(isym, kvec_d[i]));
178+
}
179+
}
180+
140181
} // namespace KListIO

source/source_cell/klist_io.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#ifndef KLIST_IO_H
22
#define KLIST_IO_H
33

4+
#include "source_base/matrix3.h"
45
#include "source_base/vector3.h"
56

67
#include <fstream>
8+
#include <functional>
9+
#include <map>
710
#include <string>
811
#include <vector>
912

@@ -35,6 +38,19 @@ struct LineK
3538
/// then linearly interpolate the line-mode k points. Pure function of the
3639
/// stream and `nks_special`; dies via WARNING_QUIT on malformed input.
3740
LineK interp_line(std::ifstream& ifk, int nks_special);
41+
42+
/// Build the EXX k-stars: for every k point, find the symmetry operation
43+
/// (index into `kgmatrix`) that rotates it onto an irreducible k point, and
44+
/// group k points by that IBZ representative. `equal` compares two doubles
45+
/// with the symmetry precision; `epsilon` is the k-restriction tolerance.
46+
/// this-free so the heavy triple loop is isolated and testable.
47+
void build_kstars(const std::vector<ModuleBase::Vector3<double>>& kvec_d,
48+
const std::vector<ModuleBase::Matrix3>& kgmatrix,
49+
int nrotkm,
50+
const std::vector<ModuleBase::Vector3<double>>& kvec_d_ibz,
51+
double epsilon,
52+
const std::function<bool(double, double)>& equal,
53+
std::vector<std::map<int, ModuleBase::Vector3<double>>>& kstars);
3854
} // namespace KListIO
3955

4056
#endif // KLIST_IO_H

0 commit comments

Comments
 (0)