Skip to content

Commit 0c16dcc

Browse files
author
abacus_fixer
committed
refactor(klist): extract time-reversal op doubling into KListIO
Move the inversion check and the time-reversal operation expansion (Theta*g antiunitary coset for magnetic nspin=4, -g doubling otherwise) out of K_Vectors::reduce_by_symmetry into the this-free KListIO::append_time_reversal_ops. The physics comment moves with the logic. reduce_by_symmetry cyclomatic complexity drops 13 -> 7 and the file loses the Matrix3 'inv' local (brace-initialized in the helper), removing a 9-parameter false positive; klist.cpp 793 -> 763 lines.
1 parent 52ee22f commit 0c16dcc

3 files changed

Lines changed: 59 additions & 35 deletions

File tree

source/source_cell/klist.cpp

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,7 @@ void K_Vectors::reduce_by_symmetry(const UnitCell& ucell,
564564
// if the operations does not already included
565565
// inverse operation, double it.
566566
//===============================================
567-
bool include_inv = false;
568567
std::vector<ModuleBase::Matrix3> kgmatrix(48 * 2);
569-
ModuleBase::Matrix3 inv(-1, 0, 0, 0, -1, 0, 0, 0, -1);
570568

571569
ModuleBase::Matrix3 k_vec;
572570
int nrotkm = 0;
@@ -580,39 +578,9 @@ void K_Vectors::reduce_by_symmetry(const UnitCell& ucell,
580578
return;
581579
}
582580

583-
// check whether the inverse operation is already included
584-
for (int i = 0; i < nrotkm; ++i)
585-
{
586-
if (kgmatrix[i] == inv)
587-
{
588-
include_inv = true;
589-
}
590-
}
591-
592-
if (symm.magnetic_nspin4)
593-
{
594-
// (nspin=4, magnetic) Time reversal Theta reverses the magnetization, so Theta alone is
595-
// NOT a symmetry and the blanket "-k is always equivalent" doubling below is invalid.
596-
// Only the antiunitary elements Theta*g with g in the moment-reversing coset belong to
597-
// the Shubnikov group; append exactly those, keeping the index convention
598-
// j + nrotk <-> Theta * gmatrix_anti[j] (decoded the same way in restore_dm).
599-
// (nspin=2 is unaffected: there the antiunitary operation is plain conjugation K, which
600-
// does not touch the spin, so D_s(-k)=D_s^*(k) holds even for a ferromagnet and the
601-
// generic branch below stays correct.)
602-
for (int j = 0; j < symm.nrotk_anti; ++j)
603-
{
604-
kgmatrix[j + symm.nrotk] = inv * symm.kgmatrix_anti[j];
605-
}
606-
nrotkm = symm.nrotk + symm.nrotk_anti;
607-
}
608-
else if (!include_inv)
609-
{
610-
for (int i = 0; i < symm.nrotk; ++i)
611-
{
612-
kgmatrix[i + symm.nrotk] = inv * symm.kgmatrix[i];
613-
}
614-
nrotkm = 2 * symm.nrotk;
615-
}
581+
// append time-reversal-related operations (Theta*g for magnetic
582+
// nspin=4; -g otherwise unless inversion is already present)
583+
nrotkm = KListIO::append_time_reversal_ops(symm, kgmatrix, nrotkm);
616584

617585
// convert kgmatrix to k-lattice
618586
std::vector<ModuleBase::Matrix3> kkmatrix(nrotkm);

source/source_cell/klist_io.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "source_base/formatter.h"
1010
#include "source_base/global_function.h"
1111
#include "source_base/parallel_common.h"
12+
#include "source_cell/module_symmetry/symmetry.h"
1213
#include "source_cell/reciprocal_grid.h"
1314

1415
#include <sstream>
@@ -207,6 +208,46 @@ void build_kstars(const std::vector<ModuleBase::Vector3<double>>& kvec_d,
207208
}
208209
}
209210

211+
int append_time_reversal_ops(const ModuleSymmetry::Symmetry& symm,
212+
std::vector<ModuleBase::Matrix3>& kgmatrix,
213+
const int nrotkm)
214+
{
215+
const ModuleBase::Matrix3 inv{-1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0};
216+
217+
bool include_inv = false;
218+
for (int i = 0; i < nrotkm; ++i)
219+
{
220+
if (kgmatrix[i] == inv)
221+
{
222+
include_inv = true;
223+
}
224+
}
225+
226+
if (symm.magnetic_nspin4)
227+
{
228+
// (nspin=4, magnetic) Time reversal Theta reverses the magnetization,
229+
// so Theta alone is NOT a symmetry; only the antiunitary Theta*g
230+
// elements with g in the moment-reversing coset belong to the
231+
// Shubnikov group. The same index convention j + nrotk is decoded
232+
// in restore_dm. (nspin=2 is unaffected: there the antiunitary
233+
// operation is plain conjugation K, which leaves D_s(-k)=D_s*(k).)
234+
for (int j = 0; j < symm.nrotk_anti; ++j)
235+
{
236+
kgmatrix[j + symm.nrotk] = inv * symm.kgmatrix_anti[j];
237+
}
238+
return symm.nrotk + symm.nrotk_anti;
239+
}
240+
if (!include_inv)
241+
{
242+
for (int i = 0; i < symm.nrotk; ++i)
243+
{
244+
kgmatrix[i + symm.nrotk] = inv * symm.kgmatrix[i];
245+
}
246+
return 2 * symm.nrotk;
247+
}
248+
return nrotkm;
249+
}
250+
210251
void pack_kpts(const std::vector<int>& isk,
211252
const std::vector<double>& wk,
212253
const std::vector<ModuleBase::Vector3<double>>& kvec_c,

source/source_cell/klist_io.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include <string>
1111
#include <vector>
1212

13+
namespace ModuleSymmetry
14+
{
15+
class Symmetry; // full definition only needed in klist_io.cpp
16+
}
17+
1318
/// this-free helpers extracted from K_Vectors, kept in a separate TU so they
1419
/// can be unit-tested and reused without dragging in the K_Vectors class.
1520
namespace KListIO
@@ -65,6 +70,16 @@ void build_kstars(const std::vector<ModuleBase::Vector3<double>>& kvec_d,
6570
const std::function<bool(double, double)>& equal,
6671
std::vector<std::map<int, ModuleBase::Vector3<double>>>& kstars);
6772

73+
/// Append the time-reversal-related k-point symmetry operations into
74+
/// `kgmatrix` (the slots right after the first `nrotkm` operations must be
75+
/// available). For magnetic nspin=4 systems the antiunitary Theta*g coset
76+
/// is appended from `symm.kgmatrix_anti`; otherwise the inverted -g ops are
77+
/// appended unless inversion is already present. Returns the updated total
78+
/// operation count.
79+
int append_time_reversal_ops(const ModuleSymmetry::Symmetry& symm,
80+
std::vector<ModuleBase::Matrix3>& kgmatrix,
81+
int nrotkm);
82+
6883
/// Flatten k-point arrays into contiguous MPI buffers (x,y,z interleaved).
6984
/// this-free; used on rank 0 before broadcasting in K_Vectors::mpi_k.
7085
void pack_kpts(const std::vector<int>& isk,

0 commit comments

Comments
 (0)