Skip to content

Commit 13850ba

Browse files
author
abacus_fixer
committed
refactor(cell): extract k-vec completion and symmetry-mismatch handling from K_Vectors::set
Move the full k-point Cartesian-coordinate completion (kc/kd branches) into KListIO::fill_full_kvec and the reciprocal/real lattice mismatch handling (symmetry_autoclose retry vs WARNING_QUIT) into a private K_Vectors::handle_symmetry_mismatch member. This drops the set() cyclomatic complexity penalty; klist.cpp score rises 45 -> 50.
1 parent 0c16dcc commit 13850ba

4 files changed

Lines changed: 91 additions & 28 deletions

File tree

source/source_cell/klist.cpp

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,14 @@ void K_Vectors::set(const UnitCell& ucell,
108108
std::string skpt1;
109109
std::string skpt2;
110110

111-
if (!this->kc_done && this->kd_done)
112-
{
113-
for (size_t ik = 0; ik != this->nkstot_full; ++ik)
114-
this->kvec_c_full[ik] = this->kvec_d[ik] * reciprocal_vec;
115-
}
116-
else if (this->kc_done && !this->kd_done)
117-
{
118-
for (size_t ik = 0; ik != this->nkstot_full; ++ik)
119-
this->kvec_c_full[ik] = this->kvec_c[ik];
120-
}
111+
// complement the Cartesian coordinates of the full k-point list
112+
KListIO::fill_full_kvec(this->kc_done,
113+
this->kd_done,
114+
this->nkstot_full,
115+
reciprocal_vec,
116+
this->kvec_c,
117+
this->kvec_d,
118+
this->kvec_c_full);
121119

122120

123121
// (2)
@@ -132,24 +130,7 @@ void K_Vectors::set(const UnitCell& ucell,
132130
#endif
133131
if (!match)
134132
{
135-
std::cout << "Optimized lattice type of reciprocal lattice cannot match the optimized real lattice. "
136-
<< std::endl;
137-
std::cout << "It is often because the inaccuracy of lattice parameters in STRU." << std::endl;
138-
if (ModuleSymmetry::Symmetry::symm_autoclose)
139-
{
140-
ModuleBase::WARNING("K_Vectors::ibz_kpoint", "Automatically set symmetry to 0 and continue ...");
141-
std::cout << "Automatically set symmetry to 0 and continue ..." << std::endl;
142-
ModuleSymmetry::Symmetry::symm_flag = 0;
143-
match = true;
144-
this->reduce_by_symmetry(ucell, symm, ModuleSymmetry::Symmetry::symm_flag, skpt1, match);
145-
} else {
146-
ModuleBase::WARNING_QUIT("K_Vectors::ibz_kpoint",
147-
"Possible solutions: \n \
148-
1. Refine the lattice parameters in STRU;\n \
149-
2. Use a different`symmetry_prec`. \n \
150-
3. Close symemtry: set `symmetry` to 0 in INPUT. \n \
151-
4. Set `symmetry_autoclose` to 1 in INPUT to automatically close symmetry when this error occurs.");
152-
}
133+
this->handle_symmetry_mismatch(ucell, symm, skpt1, match);
153134
}
154135
}
155136

@@ -207,6 +188,33 @@ void K_Vectors::set(const UnitCell& ucell,
207188
return;
208189
}
209190

191+
void K_Vectors::handle_symmetry_mismatch(const UnitCell& ucell,
192+
const ModuleSymmetry::Symmetry& symm,
193+
std::string& skpt,
194+
bool& match)
195+
{
196+
std::cout << "Optimized lattice type of reciprocal lattice cannot match the optimized real lattice. "
197+
<< std::endl;
198+
std::cout << "It is often because the inaccuracy of lattice parameters in STRU." << std::endl;
199+
if (ModuleSymmetry::Symmetry::symm_autoclose)
200+
{
201+
ModuleBase::WARNING("K_Vectors::ibz_kpoint", "Automatically set symmetry to 0 and continue ...");
202+
std::cout << "Automatically set symmetry to 0 and continue ..." << std::endl;
203+
ModuleSymmetry::Symmetry::symm_flag = 0;
204+
match = true;
205+
this->reduce_by_symmetry(ucell, symm, ModuleSymmetry::Symmetry::symm_flag, skpt, match);
206+
}
207+
else
208+
{
209+
ModuleBase::WARNING_QUIT("K_Vectors::ibz_kpoint",
210+
"Possible solutions: \n \
211+
1. Refine the lattice parameters in STRU;\n \
212+
2. Use a different`symmetry_prec`. \n \
213+
3. Close symemtry: set `symmetry` to 0 in INPUT. \n \
214+
4. Set `symmetry_autoclose` to 1 in INPUT to automatically close symmetry when this error occurs.");
215+
}
216+
}
217+
210218
// 1.reset the size of the K-point container according to spin_mult and nkstot
211219
// 2.reserve space for spin_mult>2 (symmetry)
212220
void K_Vectors::renew(const int& kpoint_number)

source/source_cell/klist.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,24 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
323323
std::vector<ModuleBase::Vector3<double>>& kvec,
324324
const bool cartesian);
325325

326+
/**
327+
* @brief Handle a reciprocal/real lattice Bravais-type mismatch after
328+
* IBZ reduction.
329+
*
330+
* When symmetry_autoclose is enabled, symmetry is switched off and the
331+
* IBZ reduction is retried; otherwise the run aborts with a WARNING_QUIT
332+
* listing the possible remedies.
333+
*
334+
* @param ucell unit cell used for the retried IBZ reduction
335+
* @param symm symmetry operations used for the retried reduction
336+
* @param skpt k-point option string forwarded to reduce_by_symmetry
337+
* @param match set to true when the autoclose retry succeeds
338+
*/
339+
void handle_symmetry_mismatch(const UnitCell& ucell,
340+
const ModuleSymmetry::Symmetry& symm,
341+
std::string& skpt,
342+
bool& match);
343+
326344
/**
327345
* @brief Adds k-points linearly between special points.
328346
*

source/source_cell/klist_io.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,28 @@ void bcast_kstars(std::vector<std::map<int, ModuleBase::Vector3<double>>>& kstar
343343
}
344344
}
345345

346+
void fill_full_kvec(const bool kc_done,
347+
const bool kd_done,
348+
const int nkstot_full,
349+
const ModuleBase::Matrix3& reciprocal_vec,
350+
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
351+
const std::vector<ModuleBase::Vector3<double>>& kvec_d,
352+
std::vector<ModuleBase::Vector3<double>>& kvec_c_full)
353+
{
354+
if (!kc_done && kd_done)
355+
{
356+
for (int ik = 0; ik < nkstot_full; ++ik)
357+
{
358+
kvec_c_full[ik] = kvec_d[ik] * reciprocal_vec;
359+
}
360+
}
361+
else if (kc_done && !kd_done)
362+
{
363+
for (int ik = 0; ik < nkstot_full; ++ik)
364+
{
365+
kvec_c_full[ik] = kvec_c[ik];
366+
}
367+
}
368+
}
369+
346370
} // namespace KListIO

source/source_cell/klist_io.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ void unpack_kpts(const std::vector<int>& isk_aux,
117117
std::vector<ModuleBase::Vector3<double>>& kvec_c,
118118
std::vector<ModuleBase::Vector3<double>>& kvec_d,
119119
std::vector<ModuleBase::Vector3<double>>& kvec_c_full);
120+
121+
/// Fill the full-list Cartesian k vectors when only one coordinate set is
122+
/// available: direct coordinates are converted via `reciprocal_vec` when
123+
/// Cartesian points are missing, otherwise the Cartesian points are copied.
124+
/// No-op when both coordinate sets are done. this-free helper called from
125+
/// K_Vectors::set() before IBZ reduction.
126+
void fill_full_kvec(bool kc_done,
127+
bool kd_done,
128+
int nkstot_full,
129+
const ModuleBase::Matrix3& reciprocal_vec,
130+
const std::vector<ModuleBase::Vector3<double>>& kvec_c,
131+
const std::vector<ModuleBase::Vector3<double>>& kvec_d,
132+
std::vector<ModuleBase::Vector3<double>>& kvec_c_full);
120133
} // namespace KListIO
121134

122135
#endif // KLIST_IO_H

0 commit comments

Comments
 (0)